Pathfinding with specific map

My question is:
Can I make my own game_map object and have a function that finds the best path for this specific map without having to code it my self?
Because in the documentation I can only find a function that finds the best path with the actual current map.
But I also found this:

class gamelib.navigation.ShortestPathFinder

which takes in game_map as attribute, but I dont see a way of really using this.
So do I need to make my own?

The ShortestPathFinder class in the starter code is (intentionally) slow, so yes, you should definitely make your own. Thereā€™s a thread or two on how to make a faster pathfinder, but Iā€™d recommend making a separate class to implement your own then running them side by side to make sure the paths match up.

Iā€™m not exactly sure what you meant by ā€œfinds the best path for this specific map without having to code it myself,ā€ but you can modify game_map however you please. Right now I think the pathfinder is attached to the game_state and you call it from there, if Iā€™m not mistaken.

OK thanks, what I mean is can I for example say there are only firewalls on [something, something] and [something, something] (on those locations)
Now how do I run the pathing algorithm if the map were to exactly look like that?

Iā€™ll probably make my own, like you suggested, but out of interest I want to know.

You should be able to create a new game_map object and change the positions of the firewalls in arbitrary ways

All good, I get it now, thanks for the help

To expand on that, you could either create an entire new game map or you could make a copy of the one you currently have (if it was a slight alteration of the game state you wanted to path for). The add_unit() and remove_unit() functions are really helpful for this.

If copying, make sure you either copy.deepcopy() or implement your own copy constructor so your copy doesnā€™t change the original.

so can I just write something like this?

game_map_copy = copy.deepcopy(game_state.game_map)

1 Like

Yep. Thatā€™ll be slower than implementing your own copy constructor, but itā€™ll do the trick.

Ok, nice, maybe someday Iā€™ll make my own but for now its fine

I got an error that I donā€™t really understand:

if game_state_copy.find_path_to_edge(location, game_state_copy.game_map.TOP_RIGHT)[-1] in [[ 14, 27],[ 15, 26],[ 16, 25],[ 17, 24],[ 18, 23],[ 19, 22]]:
TypeError: ā€˜NoneTypeā€™ object is not subscriptable

can anyone tell me how to fix this, or at least what is is causing this error please?

hmmā€¦ first thing that comes to mind is that find_path_to_edge() returns None if the start location is blocked - so you should check for that first.

2 Likes

Generally speaking, in python itā€™s always a good idea to check if a list is None or is empty before attempting to access elements in the list. @emmā€™s comment is what would cause the error in this specific example though.

1 Like

I donā€™t really think that can be though, since that part of the code loops through attack_locations, which is created like this:

for location in my_edges:
if game_state_copy.contains_stationary_unit(location):
pass
else:
attack_locations.append(location)

please ignore the weird indent

Edit:
Nevermind, I fixed it, the problem was, that I still saved the attack_locations from last round and didnā€™t overwrite them just added new values.

2 Likes