Ways to optimize the starter_algo

Is there a way to shorten certain sentences in the starter_algo?

because:

firewall_locations = [[1, 1], [2, 2], [3, 3], [4, 4]]
for location in firewall_locations:
    if game_state.can_spawn(DESTRUCTOR, location):
        game_state.attempt_spawn(DESTRUCTOR, location)

seems very long to deploy 4 destructors

That code seems pretty concise. If you really wanted to shorten it, you could change:

firewall_locations = [[1, 1], [2, 2], [3, 3], [4, 4]]
for location in firewall_locations:
if game_state.can_spawn(DESTRUCTOR, location):
game_state.attempt_spawn(DESTRUCTOR, location)

to something like:

for l in range(4):
if game_state.can_spawn(DESTRUCTOR, [l, l]):
game_state.attempt_spawn(DESTRUCTOR, [l, l])

but it would not improve performance. I am not aware of any ways to spawn multiple firewalls through one call, so that’s about as good as it gets.

The starter_algo also takes very little time to run compared to engine.jar so it would be better to optimize the engine if you are going for local performance.

I am interpreting when you say ‘long’ you mean the actual code, not execution time because as @lbertocchini says, performance wise this is about as fast as you’re going to get.

If you look at the attempt_spawn function you can see that it actually converts any single input to a list, and processes a list of locations. Basically, if you pass a list to attempt_spawn then it will try to spawn each one in the list.

For example:

firewall_locations = [[1, 1], [2, 2], [3, 3], [4, 4]]
game_state.attempt_spawn(DESTRUCTOR, firewall_locations)

You’ll notice, however, that I am no longer checking to see if the game state can spawn. Again, if you look inside the attempt_spawn function you’ll see that when it tries to create each unit it calls if self.can_spawn(unit_type, location): inside the loop. Thus, there should be no functionality difference in the small snippet of code posted.

An instance of where you would not want to do this is if you wanted to do something else if a creation failed. Then you would need to loop through and check each location and have an else statement.

In regards to list length (eg [[1, 1], [2, 2], [3, 3], [4, 4]]), as @lbertocchini demonstrates you can do some things to create a list, but this will only work if you can apply some function to the locations, for example, a line, a curve, etc. If you have a really massive list I would just save the list somewhere else and then reference it to avoid cluttering your code.

I would also recommend looking at list comprehension for python. To use @lbertocchini’s example above, it would let you do things like this:

game_state.attempt_spawn(DESTRUCTOR, [(x+1,x+1) for x in range(4)])

(I added 1 since range(4) returns 0,1,2,3)

5 Likes

You can actually just do
game_state.attempt_spawn(DESTRUCTOR, firewall_locations)
But it will print warnings if the locations aren’t correct or you don’t have enough resources. But these warnings don’t do anything, but are there to help new players understand their attempt_spawn command didn’t spawn anything. You can disable the warnings though with
game_state.suppress_warnings(True)

A common thing I do for example is have a big list of things to attempt_spawn in order of priority and it will just build what it can.

1 Like