Detecting Breaches

Can someone help me with this?
What would the code look like if I wanted to detect the coordinate of the breach?
I mean when some unit gets through my defences.

In gamelib/algocore.py, in the function start you will see a variable called state. On frames that a unit breaches on any side, state["events"]["breach"] will contain a list of all breaches, each of the form [[x,y], damage, unit_type, unit_id, player].

So, you might write something like:

for (x,y), _, _, _, player in state["events"]["breach"]:
    if player == 2:
        # Enemy breached my defences at pos (x,y)
        # Do stuff

Thanks for the reply but I cant seem to get it to work (sorry I am really new to python )

So I tried this:

for (4,9) player in state[“events”][“breach”]:
if player == 2:
gamelib.debug_write(‘hcgf’)

and its invalid syntax (please ignore the indent mistakes)
can someone help me?

X and Y are the variables that are returned by the call I think. (Havent looked at states at all yet. Its on my todo list). Basically the function returns a structure where the first value is x, the second y and the last player. Did the example code work?

A thread about detecting breaches can be seen here: https://forum.c1games.com/t/how-to-find-where-my-or-my-enemys-information-units-scored/240/13

Ok sorry I really just cant seem to get it to work.
What if I wanted to just display it in the console?
I have this:

gamelib.debug_write(state["events"]["breach"][0])

but now It crashes with the error: state is not defined
How can I define state?

Where are you placing this code? If it isn’t in AlgoCore, it won’t be recognized. Inside of AlgoCore, you will want to place that line inside of the elif stateType == 1: block, which has results from the action phase. If you do it in a different if block, it will be blank.

In the algocore.py use:

self.on_action(game_state_string)

instead of continue (under stateType == 1)

in the algo_strategy.py use:

global all_turn_states
all_turn_states = []

under the def on_game_start

under that:

def on_action(self, game_state):
frame = json.loads(game_state)
try:
all_turn_states[frame[“turnInfo”][1]][frame[“turnInfo”][2]] = frame
except Exception:
all_turn_states.append([])
all_turn_states[frame[“turnInfo”][1]].append(frame)

make a def for breaches and use:

if game_state.turn_number != 0:
list_breaches = []
for frame in all_turn_states[game_state.turn_number -1]:
for breach in frame[“events”][“breach”]:
if breach[4] == 2:
list_breaches.append(breach[0])
list_breaches_turns.append(list_breaches)

I am certain this works

1 Like

ok thanks, where should I put the last peace of code (what file)?

I have it in the algo_strategy.py

what does the all_turn_states[frame[“turnInfo”][1]][frame[“turnInfo”][2]] = frame do?
this may be a stupid question, but im not very good

From a quick look above, I believe all_turn_states is a python list. This means frame[“turnInfo”][1]][frame[“turnInfo”][2] returns an integer (which it does).

Thus, it may be a little bit clearer to see the code this way:

index = frame[“turnInfo”][1]][frame[“turnInfo”][2]
all_turn_states[index] = frame

where frame is a python dictionary (frame = json.loads(game_state)).

Thus, this is code to store previous game_state information in list format, hence the name all_turn_states.

whta does “turninfo” return exactly?

frame is a python dictionary, which means it stores information using key->val format. Essentially, a dictionary has a key which returns a val. So generally you could do:

myDict = {} # create an empty python dictionary
myDict[0] = 'test' # set the key 0 to the val 'test'
myDict['key'] = 5 # set the key 'key' to the val 5

and then to get that information:

print (myDict[0])
print (myDict['key'])

which would output:

test
0

Thus, turnInfo is a key for the dictionary frame. It returns information about that turn as given by the game engine. You can see a lot more about how the information is formatted here. You can also try printing it out (eg print (frame['turnInfo']))

what does the if breach[4]==2 do?
is it looking for a specific condition?

breach[4] == 2 is checking the player index. when you loop through frame['events']['breach'] you are looping through every single breach, for both players. Inside breach, specifically at index 4, the player index is recorded. So this checks to see if the owner of the breaching unit is player 2 (the other player). If it is, you record it by adding it to list_breaches.

I highly recommend using this topic as a reference for the structure of this data:

What is wrong with this? Im just bad at coding xD sorry.

the “turnInfo” quotes look kinda weird, don’t you think?
Isn’t it supposed to make the text look green?
This happens when you copy-paste the stuff above directly into your code.

Oh yeah I saw that but is everything else correct?
Also were do I call detectBreach?

i believe so did you run it in terimnal?