Created
November 11, 2012 22:48
-
-
Save magnifikus/4056590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.squig.plc.logic.simulator; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.script.ScriptEngineFactory; | |
import javax.script.ScriptEngineManager; | |
import de.squig.plc.logic.Circuit; | |
import de.squig.plc.logic.Signal; | |
import de.squig.plc.logic.elements.CircuitElement; | |
import de.squig.plc.logic.elements.Line; | |
import de.squig.plc.logic.helper.LogHelper; | |
public class CircuitSimulator { | |
private long nextTick = 0; | |
private short runEvery = 10; | |
private Circuit circuit; | |
public CircuitSimulator(Circuit circuit) { | |
this.circuit = circuit; | |
} | |
public synchronized void onTick(long tick) { | |
if (nextTick > tick) | |
return; | |
nextTick = tick + runEvery; | |
for (CircuitElement element : circuit.getMap().getAllElement()) | |
element.resetEvaluated(); | |
for (int y = 0; y < circuit.getMap().getHeight(); y++) { | |
for (int x = 0; x < circuit.getMap().getWidth(); x++) { | |
CircuitElement ele = circuit.getMap().getElementAt(x, y); | |
if (ele != null) { | |
if (ele.getInputPin() != null) { | |
List<CircuitElement> deps = null; | |
CircuitElement eleIn = circuit.getMap().getElementAt( | |
x - 1, y); | |
if (eleIn instanceof Line) { | |
deps = ((Line) eleIn) | |
.getConnectedInputs(); | |
} else { | |
if (eleIn != null) { | |
deps = new ArrayList<CircuitElement>(); | |
deps.add(eleIn); | |
} else | |
deps = null; | |
} | |
Signal signal = Signal.OFF; | |
if (deps != null) { | |
for (CircuitElement dep : deps) { | |
signal = dep.getSignal(); | |
if (signal == Signal.ON) | |
break; | |
} | |
} | |
ele.getInputPin().onSignal(signal); | |
LogHelper.info("result on "+signal.toString()+" on "+ele.toString()); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment