AlgoCore
's start
method runs continuously and waits for information sent from the engine. Part of the information sent is what frame/turn the algo is on. Thus, you can see in the while loop, it checks if stateType == <SOME NUMBER>
for multiple cases. The AlgoStrategy
class overrides the function written in the AlgoCore
class, but you can see (in AlgoCore
) that it runs self.on_turn(game_state_string)
. In other words, when it is the start of a new turn (eg stateType == 0
) then you run all of your logic.
There is another stateType, 1, that means it is a frame. You can run logic every single frame, not just every turn. If you send anything to the engine it won’t work, since you can’t build (it may even crash, haven’t tried it) in between turns, but you can read and save information that the engine gives.
Thus, I created a new method in AlgoCore
called on_frame
and then overrode it in my AlgoStrategy
class just like the on_turn
method, only it is called every single frame. Here you can check whether any breaches occurred. So some example code:
# inside AlgoCore:
class AlgoCore(object):
# ... other methods and stuff
# this method, like on_turn will be overridden, so we do nothing in it
def on_frame(self, frame_state):
pass
def start(self):
# other stuff here ommited for clarity
while True:
# ... other if statements (this is already written)
elif stateType == 1:
"""
If stateType == 1, this game_state_string string represents the results of an action phase
"""
self.on_frame(state) # here we call our new method
#---------------------------------------------------------------------------------------------------------------------------------
# inside AlgoStrategy:
class AlgoStrategy(gamelib.AlgoCore):
# ... other methods and stuff
# here we overload on_frame from the AlgoCore class
def on_frame(self, frame_state):
# here you can get any breaches that occurred in this frame
breaches = frame_state['events']['breach']
You can then store these breaches in a class variable and use them on the next turn when you are trying to use them. There is a lot more information you can get on these frames, take a look here to get an idea for what other stuff is available (specifically the events area). I also recommend just printing stuff out when you are first playing with it.