Add check to see if simulation has reached stasis
Closed this issue · 3 comments
A callback or eventEmitter would be very useful in this case. Consider running terrarium.stop()
by default.
Unlike #14, I don't think it makes sense to do this on a step-loop basis. It would take far too long to do deep comparisons of each creature with their past state.
Right now I think the best way is to have some grid-level variable hasChanged
set to false
at the beginning of each step, and call if (!hasChanged) hasChanged = true;
whenever an action happens. It's unfortunate to have to trigger this manually, but the alternative would take more resources than anything else in the step loop...
If anybody has ideas on how this could be handled elegantly I'm all ears!
That idea seems like it would work really good, I think you'd have to you'd just have to put
hasChanged = false
in a function that runs each time a creature is moved, so the issue is knowing where to put it to avoid redundancy/unnecessary calls.
A simulation has reached stasis if:
- No creatures die at the beginning of a step
- All creatures return a falsey value from
process
, or an object with a falsyobserve
property - No creatures experience an energy change from waiting
This is just the default behaviour, and as the process
method is fully customizable it also allows for more tailored stopping requirements as outlined in #14.