Crash Detection

Crash Detection
0

#1

I need help trying to figure out how to detect whether an information unit from the opposing player has hit my wall. I’ve read the other forum posts that explain how to do it, but I just can’t figure it out in python. Can someone give me example code so I can use it and tailor it to what I need? Many thanks!


#2

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.


How to get data on information units?
#3

Thank you so much! This really helps!


#4

Ok, so I tried implementing the code above, but when I play the Algo, it will either not place anything during the deploy stage after the first round, or the rounds will quickly go back and forth until 100 rounds. What did I do? The Algo runs fine when I comment out the extra code in AlgoStrategy. Also, the editor claims that ‘frame_state’ variable doesn’t exist when recalling the function.


#5

Are you still submitting the game_state’s turn after each frame? To me it sounds like you are submitting after each frame and queuing up the stdout (how the engine communicates) with empty commands, meaning you never build anything and it seems to skip as if nothing happened. Make sure you do not submit anything (eg game_state.submit_turn()) on each frame.


#6

Wouldn’t the other player’s turn still run though?


#7

Hmmm, yeah, it should unless they are both the same algo. You said it complains that frame_state doesn’t exist, do you pass in state to the function? Or maybe you forgot to write self in the function declaration?

The principle of what I wrote above works, there must be some small typo or bug either in what I wrote or how you implemented it.