I didn't realize it was a logical and gate, i never got it to work correctly; plus as for an and gate having a time stipulation doesn't make alot of sense.
As far as only triggering once per tick i'll clarify.
Start by having a Tick count. Create a variable call it Tick and initialize it to 0 on server start. Every logic tick increment the tick (tick++ depending on lang).
Now only calculate a components output once per tick, How?
I'll do it in Java:
Code: Select all
public class logicComponent {
int lastRan = 0;
int value = 0;
HashMap<String,logicComponent> inputs = new HashMap<String,logicComponent>();
HashMap<String,logicComponent> outputs = new HashMap<String,logicComponent>();
public void triggerInput(String key) {
int curTick = Server.getTick();
if(curTick == lastRan){
return;
}
triggerOutput();
}
public void triggerOuput(){
computeValue();
logicComponent chip;
for(String key : values.keys()){
chip = outputs.get(key);
if(chip != None){
chip.triggerInput(key);
}
}
}
public void computeValue(){
//Do Nothing
}
public int getValue(String inputKey){
logicComponent chip = inputs.get(inputKey);
if(chip == None){
return 0;
}
return chip.value;
}
}
public class GateAND extends logicComponent {
@Override
public void computeValue(){
value = ((getValue("A") != 0) && (getValue("B') != 0)) ? 1 : 0;
}
}
public class GateOR extends logicComponent {
@Override
public void computeValue(){
value = ((getValue("A") != 0) || (getValue("B') != 0)) ? 1 : 0;
}
}
Basically boils down to Any time an input is changed, or even pulsed the gate will recalculate (triggering other gate's inputs) only if it hasn't been recalculated this frame(tick).
This simple solution however does have a draw back, and that is if more then one input is triggered per tick, the gate will not recalculate next tick unless an input is triggered again.
I'll outline:
Tick 1, Time 0ms: AndGate.inputA = 1; inputA = 1, inputB = 0;
Tick 1, Time 0ms: AndGate recalculate(); AndGate.output = 0;
Tick 1, Time 1ms: AndGate.inputB = 1; inputA = 1, inputB = 1;
//The gate does not recalculate because it is still on Tick 1
Tick 2, Time 2ms: AndGate.inputA = 1; inputA = 1, inputB = 1;//AKA the inputs are still the same but it forces the gate to recalculate.
Tick 2, Time 2ms: AndGate recalculate(); AndGate.output = 1;
Tick 3, Time 4ms: AndGate.inputB = 0; inputA = 1, inputB = 0;
Tick 3, Time 4ms: AndGate recalculate(); AndGate.output = 0;
Solution for this problem? Create a "Must Recalculate Que", That is, Every time a gate is NOT recalculated due to the tick issue; Put it into the "Must Recalculate Que".
Now at the start of every logic tick first remove the component from the Que, Then trigger its Input, Then move on to the next component in the Que.
The way this will work is pretty cooly, It will recalculate a bunch of components, and if any of the components should need to be calculated next tick they will automatically have re-added themselves to the "Must Recalculate Que".
If you wanted, I could write all the fixes you need, but I'd need an outline, and the language it needs to be written in.