Hi people.
I’m currently coding a space management sim (in the like of X3: reunion) and i would like some advises on the way to keep tracks AI Ships.
Right now, the game is a galaxy composed of sectors. Ships can fly in sector and from one sector to another, to reach infrastructure. The aim is to generate some life with useless travels, but also simulate a real economy with AI Ships looking for opportunity.
There is no graphic, so no 2d representation of where a ship is, only logic and references.
When the player has intel on a sector, the game needs to quickly list the ships in the current sector. So here is my “problem” (“” because it’s not a real problem right now, i just want to know which way is better performance wise).
How can i track theses ships on the global galaxy ?
For that i have three ideas.
1- Either i use my variable Location of type Sector that every ship have, so i loop through all ships (contained in a hashset) to find which one are in the sector. It should work at first, but i fear that in the more advanced stage of the game, where more ships will be available, this loop will be costly. I do that once to fill a temporary list available only to the sector the player is looking at. From now on, every time a ship will move, it will send an alarm to check if the ship entered or left the current sector, to update the local List.
2- The other solution is that each sector have a list of ships. So i just have to ask the current sector about his list that will constantly update real time. The thing is, I will end up with a lot of various size List that will change a lot through the game. I’m not sure this is performance friendly to have the list resize again and again to remove object and add object every 5 or 10 seconds. What do you think ? We’re talking about about several dozens sectors, maybe a hundred (so 50 to 100 lists).
3- I could also store a single List of KeyValuePair, containing ships and their location, in the static GameManager. But is that even more efficient than a loop on a List of Ships to check their Location value ?
I got the exact same problematic with stations. They need to know which ships are docked. Sure, their list won’t change as often as sectors, but we are still talking hundreds of small list that will change size and content through the game.
Overall i think solution 1, ships knowing their location, and a method to loop through all of them and generate a filtered list based on the selected sector seems to be the best choice. Did any of you encountered such problem ?