Initial Version

This commit is contained in:
ThomasFooteDQ
2014-02-02 16:39:08 -05:00
commit dc6d494ff4
304 changed files with 35696 additions and 0 deletions

View File

@@ -0,0 +1,207 @@
package com.droidquest.chipstuff;
import com.droidquest.Wire;
import com.droidquest.devices.Device;
import com.droidquest.devices.FlipFlop;
import com.droidquest.devices.PrototypeChip;
import com.droidquest.devices.SmallChip;
import com.droidquest.items.Item;
public class ChipCompiler extends Thread
{
public static int chipSpeed=1;
public ChipCompiler(PrototypeChip pc, SmallChip sc)
{
pc.grabbable = false;
sc.grabbable = false;
int a;
sc.Empty();
for (a=0; a<pc.InternalRoom.wires.size(); a++)
sc.signals.addElement(new Signal());
Signal dummy = new Signal();
dummy.working = false;
sc.signals.addElement(dummy);
for (a=0; a<8; a++)
{
Wire wire = pc.portdevices[a].ports[0].myWire;
int index = pc.InternalRoom.wires.indexOf(wire);
if (index>=0)
{
Signal sig = (Signal) sc.signals.elementAt(index);
sc.portSignals[a].internalSignal= sig;
}
sc.ports[a].type = pc.ports[a].type;
sc.portSignals[a].type = pc.ports[a].type;
}
for (a=0; a<pc.level.items.size(); a++)
{
Item item = (Item) pc.level.items.elementAt(a);
if (item.room == pc.InternalRoom)
if (item.isDevice())
{
Device device = (Device) item;
Gate gate=null;
String type = item.getClass().toString();
if (type.endsWith("ANDGate"))
gate = new Gate("AND");
if (type.endsWith("ORGate"))
gate = new Gate("OR");
if (type.endsWith("NOTGate"))
gate = new Gate("NOT");
if (type.endsWith("XORGate"))
gate = new Gate("XOR");
if (type.endsWith("FlipFlop"))
{
gate = new Gate("FF");
gate.state = ((FlipFlop)device).state;
}
if (type.endsWith("Node"))
gate = new Gate("NODE");
if (type.endsWith("SmallChip"))
gate = new Gate((SmallChip)device);
if (gate != null)
{
sc.gates.addElement(gate);
for (int p=0; p<device.ports.length; p++)
{
if (device.ports[p].myWire != null)
{
int index = pc.InternalRoom.wires.indexOf(device.ports[p].myWire);
Signal sig = (Signal) sc.signals.elementAt(index);
gate.portSignals[p].externalSignal= sig;
// System.out.println("Signal "
// + index
// + " attached to "
// + gate.type
// + " Gate["
// + p
// + "]");
}
else
gate.portSignals[p].externalSignal=dummy;
}
}
}
}
// Remove Node Gates, and transfer Signals
for (a=0; a<sc.gates.size(); a++) //For every Gate in the chip
{
Gate gate1 = (Gate) sc.gates.elementAt(a);
if (gate1.type == "NODE")
{
for (int ap=1; ap<4; ap++) // For every output Signal in the Node
{
Signal s1 = gate1.portSignals[ap].externalSignal;
if (s1!= null)
{
for (int b=0; b<sc.gates.size(); b++) // For every other Gate in the Chip
{
Gate gate2 = (Gate) sc.gates.elementAt(b);
if (gate1 != gate2)
{
for (int bp=0; bp<8; bp++) // For every Signal connection in that other gate
{
Signal s2 = gate2.portSignals[bp].externalSignal;
if (s1==s2) // If Signal is an output Node signal
{
// System.out.println("Changing " + gate2.type
// + "[" + bp + "] = Signal "
// + sc.signals.indexOf(s2)
// + " to Signal "
// + sc.signals.indexOf(gate1.portSignals[0]));
gate2.portSignals[bp].externalSignal
= gate1.portSignals[0].externalSignal;
}
}
}
}
for (int ps=0; ps<8; ps++)
if (sc.portSignals[ps].internalSignal==s1)
sc.portSignals[ps].internalSignal
= gate1.portSignals[0].externalSignal;
}
}
sc.gates.removeElement(gate1);
a--;
}
}
// Remove unused Signals
// System.out.println("Starting with " + sc.signals.size() + " signals");
// System.out.println("Starting with " + sc.gates.size() + " gates");
for (a=0; a<sc.signals.size(); a++)
{
boolean used = false;
Signal sig1 = (Signal) sc.signals.elementAt(a);
for (int g = 0; g< sc.gates.size(); g++)
{
Gate gate = (Gate) sc.gates.elementAt(g);
for (int s = 0; s< 8; s++)
{
Signal sig2 = gate.portSignals[s].externalSignal;
if (sig2!=null)
// System.out.println(gate.type + "Gate["+s+"] connected to Signal "
// + sc.signals.indexOf(sig2));
if (sig1==sig2)
{
// System.out.println("Signal " + a + " is used by " + gate.type + "Gate ");
used = true;
}
}
}
for (int ps = 0; ps<8; ps++)
if (sc.portSignals[ps].internalSignal==sig1)
used=true;
if (used == false)
{
// System.out.println("Removing unused Signal at " + a);
sc.signals.removeElement(sig1);
a--;
}
}
// Set Signal types
for (a=0; a<8; a++)
if (sc.portSignals[a]!=null)
sc.portSignals[a].type = sc.ports[a].type;
// Debug report
System.out.println(sc.signals.size() + " Signals");
System.out.println(sc.gates.size() + " Gates");
for(a=0; a<sc.gates.size(); a++)
{
Gate gate1 = (Gate) sc.gates.elementAt(a);
gate1.DebugReport(1);
for (int b=0; b<8; b++)
if (gate1.portSignals[b].externalSignal!=null)
System.out.println(a + ": " + gate1.type
+ " gate["
+ b
+ "] = Signal "
+ sc.signals.indexOf(gate1.portSignals[b].externalSignal));
}
for (a=0; a<8; a++)
if (sc.portSignals[a].internalSignal != null)
System.out.println("PortSignal "
+ a
+ " = Signal "
+ sc.signals.indexOf(sc.portSignals[a].internalSignal));
sc.speed=chipSpeed;
if (pc.label != null)
sc.label = new String(pc.label);
if (pc.description != null)
sc.description = new String(pc.description);
sc.GenerateIcons();
pc.grabbable = true;
sc.grabbable = true;
}
}

View File

@@ -0,0 +1,310 @@
package com.droidquest.chipstuff;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;
import com.droidquest.devices.SmallChip;
public class Gate implements Serializable
{
public transient PortSignal[] portSignals = new PortSignal[8];
public boolean state;
public String type;
public int speed;
public Vector mySignals = new Vector();
public Vector myGates = new Vector();
public Gate(String t)
{
// Called whenever a non-chip gate is created.
type = t;
speed = 1;
for (int a=0; a<8; a++)
portSignals[a] = new PortSignal();
}
public Gate(SmallChip sc)
{
// Called by ChipCompiler to put a nested chip into a gate
speed = sc.speed;
type = "Chip";
Signal dummySignal;
portSignals = new PortSignal[8];
for (int a=0; a<8; a++)
portSignals[a] = new PortSignal();
for (int a=0; a<sc.signals.size(); a++)
{
Signal newsig = new Signal();
Signal oldsig = (Signal) sc.signals.elementAt(a);
newsig.Set(oldsig.Get());
newsig.working = oldsig.working;
mySignals.addElement(newsig);
if (newsig.working == false)
dummySignal=newsig;
}
for (int a=0; a<sc.gates.size(); a++)
{
Gate oldgate = (Gate) sc.gates.elementAt(a);
Gate newgate = new Gate(oldgate);
myGates.addElement(newgate);
for (int b=0; b<8; b++)
if (oldgate.portSignals[b].externalSignal!=null)
{
int sigIndex = sc.signals.indexOf(oldgate.portSignals[b].externalSignal);
Signal sig = (Signal) mySignals.elementAt(sigIndex);
newgate.portSignals[b].externalSignal = sig;
}
}
for (int a=0; a<8; a++)
{
if (sc.portSignals[a].internalSignal!=null)
{
int sigIndex = sc.signals.indexOf(sc.portSignals[a].internalSignal);
Signal sig = (Signal) mySignals.elementAt(sigIndex);
portSignals[a].internalSignal = sig;
portSignals[a].type = sc.portSignals[a].type;
}
}
}
public Gate(Gate g)
{
// Create a new Gate based off an existing one
type=g.type;
state=g.state;
speed = g.speed;
portSignals = new PortSignal[8];
for (int a=0; a<8; a++)
portSignals[a] = new PortSignal();
if (type.equalsIgnoreCase("Chip"))
{
for (int a=0; a<g.mySignals.size(); a++)
{
Signal newsig = new Signal();
Signal oldsig = (Signal) g.mySignals.elementAt(a);
newsig.Set(oldsig.Get());
newsig.working = oldsig.working;
mySignals.addElement(newsig);
}
for (int a=0; a<g.myGates.size(); a++)
{
Gate oldgate = (Gate) g.myGates.elementAt(a);
Gate newgate = new Gate(oldgate);
myGates.addElement(newgate);
for (int b=0; b<8; b++)
{
int signalIndex = g.mySignals.indexOf(oldgate.portSignals[b].externalSignal);
if (signalIndex != -1)
newgate.portSignals[b].externalSignal = (Signal)mySignals.elementAt(signalIndex);
}
}
for (int a=0; a<8; a++)
{
if (g.portSignals[a].internalSignal != null)
{
int sigIndex = g.mySignals.indexOf(g.portSignals[a].internalSignal);
portSignals[a].internalSignal = (Signal) mySignals.elementAt(sigIndex);
portSignals[a].type = g.portSignals[a].type;
}
}
}
}
public void writeRef(ObjectOutputStream s) throws IOException
{
for (int a=0; a<8; a++)
s.writeInt(mySignals.indexOf(portSignals[a].internalSignal));
for (int a=0; a<myGates.size(); a++)
{
Gate gate = (Gate) myGates.elementAt(a);
for (int b=0; b<8; b++)
{
s.writeInt(mySignals.indexOf(gate.portSignals[b].externalSignal));
s.writeInt(gate.portSignals[b].type);
}
gate.writeRef(s);
}
}
public void readRef(ObjectInputStream s) throws IOException
{
for (int a=0; a<8; a++)
{
int portIndex = s.readInt();
if (portIndex>=0)
portSignals[a].internalSignal = (Signal) mySignals.elementAt(portIndex);
}
for (int a=0; a<myGates.size(); a++)
{
Gate gate = (Gate) myGates.elementAt(a);
gate.portSignals = new PortSignal[8];
for (int b=0; b<8; b++)
{
gate.portSignals[b] = new PortSignal();
int sigIndex = s.readInt();
if (sigIndex>=0)
gate.portSignals[b].externalSignal = (Signal) mySignals.elementAt(sigIndex);
gate.portSignals[b].type = s.readInt();
}
gate.readRef(s);
}
}
public void Function()
{
if (type.equalsIgnoreCase("AND"))
portSignals[2].externalSignal.Set(portSignals[0].externalSignal.Get()
& portSignals[1].externalSignal.Get());
if (type.equalsIgnoreCase("OR"))
portSignals[2].externalSignal.Set(portSignals[0].externalSignal.Get()
| portSignals[1].externalSignal.Get());
if (type.equalsIgnoreCase("NOT"))
portSignals[1].externalSignal.Set(!portSignals[0].externalSignal.Get());
if (type.equalsIgnoreCase("XOR"))
portSignals[2].externalSignal.Set(portSignals[0].externalSignal.Get()
^ portSignals[1].externalSignal.Get());
if (type.equalsIgnoreCase("FF"))
{
if (portSignals[0].externalSignal.Get() ^ portSignals[1].externalSignal.Get())
state = portSignals[0].externalSignal.Get();
portSignals[2].externalSignal.Set(state);
portSignals[3].externalSignal.Set(!state);
}
if (type.equalsIgnoreCase("NODE"))
{
portSignals[1].externalSignal.Set(portSignals[0].externalSignal.Get());
portSignals[2].externalSignal.Set(portSignals[0].externalSignal.Get());
if (portSignals[3].externalSignal!=null)
portSignals[3].externalSignal.Set(portSignals[0].externalSignal.Get());
}
if (type.equalsIgnoreCase("Chip"))
{
for (int s=0; s<speed; s++)
{
for (int a=0; a<mySignals.size(); a++)
((Signal) mySignals.elementAt(a)).Flip();
for (int a=0; a<8; a++)
if (portSignals[a].externalSignal!=null
&& portSignals[a].internalSignal!=null)
if (portSignals[a].type==Port.TYPE_INPUT)
portSignals[a].internalSignal.Set(portSignals[a].externalSignal.Get());
for (int a=0; a<myGates.size(); a++)
((Gate) myGates.elementAt(a)).Function();
for (int a=0; a<8; a++)
if (portSignals[a].externalSignal!=null
&& portSignals[a].internalSignal!=null)
if (portSignals[a].type==Port.TYPE_OUTPUT)
portSignals[a].externalSignal.Set(portSignals[a].internalSignal.Get());
}
}
}
public void SaveSubGate(ObjectOutputStream s) throws IOException
{
s.writeInt(mySignals.size());
for (int a=0; a<mySignals.size(); a++)
{
Signal sig = (Signal)mySignals.elementAt(a);
s.writeBoolean(sig.Get());
s.writeBoolean(sig.working);
}
for (int a=0; a<8; a++)
{
s.writeInt(mySignals.indexOf(portSignals[a].internalSignal));
s.writeInt(portSignals[a].type);
}
s.writeInt(myGates.size());
for (int a=0; a<myGates.size(); a++)
{
Gate gate = (Gate) myGates.elementAt(a);
s.writeObject(gate.type);
s.writeBoolean(gate.state);
for (int b=0; b<8; b++)
s.writeInt(mySignals.indexOf(gate.portSignals[b].externalSignal));
if (gate.type.equalsIgnoreCase("Chip"))
gate.SaveSubGate(s);
}
s.writeInt(speed);
}
public void LoadSubGate(ObjectInputStream s) throws IOException, ClassNotFoundException
{
int numSignals = s.readInt();
mySignals = new Vector();
for (int a=0; a<numSignals; a++)
{
Signal newSignal = new Signal();
newSignal.Set(s.readBoolean());
newSignal.working = s.readBoolean();
mySignals.addElement(newSignal);
}
for (int a=0; a<8; a++)
{
int sigIndex = s.readInt();
if (sigIndex>=0)
portSignals[a].internalSignal = (Signal) mySignals.elementAt(sigIndex);
portSignals[a].type = s.readInt();
}
int numGates = s.readInt();
for (int a=0; a<numGates; a++)
{
Gate newGate = new Gate((String) s.readObject());
newGate.state = s.readBoolean();
myGates.addElement(newGate);
for (int b=0; b<8; b++)
{
int sigIndex = s.readInt();
if (sigIndex>=0)
newGate.portSignals[b].externalSignal = (Signal) mySignals.elementAt(sigIndex);
}
if (newGate.type.equalsIgnoreCase("Chip"))
newGate.LoadSubGate(s);
}
speed = s.readInt();
}
public void DebugReport(int indent)
{
// String ind = "";
// for (int a=0; a<indent; a++)
// ind += " ";
// System.out.println(ind + type + "Gate");
// System.out.println(ind + mySignals.size() + " Signals");
// System.out.println(ind + myGates.size() + " Gates");
// for(int a=0; a<myGates.size(); a++)
// {
// Gate gate1 = (Gate) myGates.elementAt(a);
// gate1.DebugReport(indent+1);
// for (int b=0; b<8; b++)
// if (gate1.myPortSignals[b]!=null)
// System.out.println(ind + a + ": " + gate1.type
// + " gate["
// + b
// + "] = Signal "
// + mySignals.indexOf(gate1.myPortSignals[b]));
// }
// for (int a=0; a<8; a++)
// if (portSignals[a] != null)
// System.out.println(ind + "PortSignal "
// + a
// + " = Signal "
// + mySignals.indexOf(portSignals[a]));
}
}

View File

@@ -0,0 +1,194 @@
package com.droidquest.chipstuff;
import java.awt.Color;
import java.awt.Graphics;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.droidquest.Wire;
import com.droidquest.devices.Device;
import com.droidquest.items.Item;
import com.droidquest.levels.Level;
public class Port implements Serializable
{
// Input or Output of a Device
public int type; // 0=Input, 1=Output, 2=Undefined
public static final int TYPE_INPUT = 0;
public static final int TYPE_OUTPUT = 1;
public static final int TYPE_UNDEFINED = 2;
private int size; // Short or Long port 0=Short, 1=Long
boolean standard; // Shows Graphics?
public int rotation; //0=Up, 1=Left, 2=Down, 3=Right;
public static final int ROT_UP = 0;
public static final int ROT_RIGHT = 1;
public static final int ROT_DOWN = 2;
public static final int ROT_LEFT = 3;
public boolean value; // True or False, what else?
public int x,y;
public int width, height; // width & height
public int wireX, wireY;
public transient Item myDevice;
public transient Wire myWire;
public boolean locked; // False = Reverts to Undefined with no connections
public Port() {}
public Port(int X, int Y, int Type, int Size, int rot, Device d)
{
// There are realy only two types of Ports: Standard and Chip. A
// Standard Port is what appears on all Gates, Robot Devices, and
// the Prototype Chip. 20 images are defined for these conditions:
// (Input/Output/Undefined) * (U/D/L/R) * (On/Off). Undefined is
// always Off. If the type is not Standard, it's a chip port, and
// uses no graphics.
x=X; y=Y;
type = Type;
size = Size;
rotation = rot;
standard = (size>0);
myDevice = d;
if (standard)
{
width = 12; height=size;
wireX = x + 6; wireY = y + height - 6;
}
else
{
width = 2; height = 2;
wireX = x; wireY = y;
}
if (type==TYPE_UNDEFINED)
locked=false;
else
locked=true;
}
public void writeRef(ObjectOutputStream s) throws IOException
{
Level level = myDevice.level;
s.writeInt(level.items.indexOf(myDevice));
if (myDevice.room != null)
s.writeInt(myDevice.room.wires.indexOf(myWire));
}
public void readRef(ObjectInputStream s, Level level) throws IOException
{
myDevice = (Item) level.FindItem(s.readInt());
if (myDevice != null)
if (myDevice.room != null)
myWire = myDevice.room.FindWire(s.readInt());
}
public void Draw(Graphics g, int rot)
{
if (value)
g.setColor(new Color(255,128,0));
else
g.setColor(Color.white);
int relRot = (rotation + rot)%4;
if (standard)
{
switch (type)
{
case TYPE_INPUT:
switch(relRot)
{
case 0: // Up
g.fillRect(x+0,y+4,4,size);
g.fillRect(x-4,y+0,4,4);
g.fillRect(x+4,y+0,4,4);
g.fillRect(x+0,y-2,4,2);
break;
case 1: // Right
g.fillRect(x-size-3,y+0,size,4);
g.fillRect(x-3,y-4,4,4);
g.fillRect(x-3,y+4,4,4);
g.fillRect(x+1,y+0,2,4);
break;
case 2: // Down
g.fillRect(x-3,y-size-3,4,size);
g.fillRect(x-7,y-3,4,4);
g.fillRect(x+1,y-3,4,4);
g.fillRect(x-3,y+1,4,2);
break;
case 3: // Left
g.fillRect(x+4,y-3,size,4);
g.fillRect(x+0,y-7,4,4);
g.fillRect(x+0,y+1,4,4);
g.fillRect(x-2,y-3,2,4);
break;
}
break;
case TYPE_OUTPUT:
switch(relRot)
{
case 0: // Up
g.fillRect(x+0,y+0,4,size);
g.fillRect(x-4,y+2,12,2);
g.fillRect(x-8,y+4,4,2);
g.fillRect(x+8,y+4,4,2);
break;
case 1: // Right
g.fillRect(x-size+1,y+0,size,4);
g.fillRect(x-3,y-4,2,12);
g.fillRect(x-5,y-8,2,4);
g.fillRect(x-5,y+8,2,4);
break;
case 2: // Down
g.fillRect(x-3,y-size,4,size);
g.fillRect(x-7,y-3,12,2);
g.fillRect(x-11,y-5,4,2);
g.fillRect(x+5,y-5,4,2);
break;
case 3: // Left
g.fillRect(x,y-3,size,4);
g.fillRect(x+2,y-7,2,12);
g.fillRect(x+4,y+5,2,4);
g.fillRect(x+4,y-11,2,4);
break;
}
break;
case TYPE_UNDEFINED:
switch(relRot)
{
case 0: // Up
g.fillRect(x-2,y-4,8,12);
g.fillRect(x,y+8,4,size);
g.setColor(Color.black);
g.fillRect(x,y,4,4);
break;
case 1: // Right
g.fillRect(x-7,y-2,12,8);
g.fillRect(x-7-size,y,size,4);
g.setColor(Color.black);
g.fillRect(x-3,y,4,4);
break;
case 2: // Down
g.fillRect(x-5,y-7,8,12);
g.fillRect(x-3,y-7-size,4,size);
g.setColor(Color.black);
g.fillRect(x-3,y-3,4,4);
break;
case 3: // Left
g.fillRect(x-4,y-5,12,8);
g.fillRect(x+8,y-3,size,4);
g.setColor(Color.black);
g.fillRect(x,y-3,4,4);
break;
}
break;
}
}
}
}

View File

@@ -0,0 +1,12 @@
package com.droidquest.chipstuff;
public class PortSignal
{
public Signal externalSignal;
public Signal internalSignal;
public int type;
public PortSignal() {}
}

View File

@@ -0,0 +1,27 @@
package com.droidquest.chipstuff;
import java.io.Serializable;
public class Signal implements Serializable
{
transient private int index;
private boolean[] value = new boolean[2];
public boolean working;
public Signal()
{
index=0;
working=true;
}
public void Flip()
{
index = 1-index;
}
public boolean Get()
{
return value[index] && working;
}
public void Set(boolean v)
{
value[1-index] = v && working;
}
}