I built this code for my latest mission as a way to check wheter a ship was headed towards an other or not (I've removed any way for the player to change the velocity, which is set always at 200)
The thing is, the code doesn't work too well. It often gives false readings, like 0, or very low numbers or too high ones.
I was hoping you guys could help me to improve it.
Rule event tick
name "HeadingCheck"
:action
Dist1:=Distance(m.al, m.ac); //first, we take the initial distance between the alkesh and the achilles
delay(0,0.5,
Dist2:=Distance(m.al, m.ac); //we wait half a second and then we take it again
dif:=dist1-dist2; //and we compare and take the difference of those two distances
debug(dif); //we show it on the console, for debugging purposes
if(Dist1-dist2>100, //if the difference is higher than 100, it means that the distance between the two ships is decreasing rapidly, which means one thing: you're headed straight to the alkesh. Bad mojo, bro.
Debug("too close for confort");
counter2:=counter2+1; //we take a counter of how many times this crossing appears
if(warn1!=1,
LocalEvent(TooCloseForConfort); //if the difference is higher than 100 this event is issued (see that event for the use of the "warn" variable)
);
, //this coma means "else"
if(dist1-dist2!=0&warn1=1&nonrepeated=1, //sometimes the code malfunctions for some extrange reason and it gives a "0" reading, as both distances were the same, it also gives false readings (lower than the real deal)
milestone:=counter2; //so, in order to counter that, we take a "milestone" of how many crossings took place and we re-check it 5 secons later
nonrepeated:=0; //this is meant so that this doesn't happen over and over again
delay(0,5,
Milestone2:=counter2;
if(milestone=milestone2, //if the milestone 5 seconds later is the same as the one five seconds earlier, it means that "counter" hasn't changed, which means that no number higher than 100 has appeared, and then we can safely assume that the player has changed course (in order to come up with this I spent a long time looking at the debugging windown...)
ReRouted:=1; //we "tell" the script that the ship has rerouted
);
nonrepeated:=1; //with this we re-open the check for future porpuses
,0);
);
);
,0);
:end
END
tick 0.5
EDIT: Nevermind, solved it... I turned the tick rule into a regular one and added a localevent(*rule in question*) in the bottom of it to make it a little like a "tick" rule of my own, hehehehehe