I am trying to add a portion of code to my algo that removes destructors if they fall below half their maximum stability. I am relatively new to coding in python and would greatly appreciate some guidance. Here is what I have tried along with some slight variations.
if unit_type == DESTRUCTOR and (stability < max_stability / 2):
game_state.attempt_removal(DESTRUCTOR)
Did you assign values to your variables unit_type, stability, and max stability earlier in the code? For example:
unit_type = some_unit.unit_type
If you did not do something like that, your code will have no concept of what ‘unit_type’ is and will crash. In addition, attempt_removal takes a location as input, not a unit_type.
You are pretty close, but here is an example of what a chunk of code that does what you want to might look like. Note that this is psuedocode, and will not work if copy/pasted, it just outlines the general logic.
for each location in possible_locations_for_destructors:
x, y = location
current_unit = game_state.game_map[x,y][0]
if current_unit.unit_type = DESTRUCTOR and (current_unit.stability < current_unit.max_stability / 2):
game_state.attempt_removal(location)
This code would work because we have a reference to a specific unit. I’ll leave it to you to create a list of possible locations. There are tons of resources online to help you learn more python as well, good luck!
3 Likes
You can also modify the add_unit
function in game_map.py
to push each GameUnit
to a one dimensional array, and iterate through that easily and quickly instead of iterating through a diamond shaped, two dimensional array.
The gamelib libraries are just a bunch of python files in a folder, you can modify and add to them as necessary. (If you do add a file, make sure it’s listed in __init__.py
)
2 Likes
Thank you, that was very helpful!