my friend and I are currently working on a city builder game like Anno/Cities Skylines but with some own ideas and tweaks. (of course, on a much smaller scale)
Currently, we are struggling a bit with implementing the economy system and hoped that we could get some ideas for the implementation.
Residence buildings and all other buildings (production/service) are connected by roads. For our service buildings (fire stations, police, etc.) we would like to have a distance-based system instead of an area of influence. That means, depending on how many tiles a residence building is away from a service building the lower the effect.
Currently, I tried one approach with a breath deep algorithm.
The algorithm starts at the service building and progresses along the road. The algorithm first searches for adjacent roads and then writes them into a list of roads. It then saves the current tile distance into the road gameobject before moving to the next road in the list. (I tried to illustrate it in the picture)
Although the algorithm works well and is compatible with multiple service building overlapping, the main drawback of this approach is the performance.
Testing with 10 buildings or so, caused strong frame drops/freezing of the game.
I would also have to run this regularly as the player can remove roads and service buildings so I have to update it to account for any changes in buildings. (--> back to problem 1)
Since the values are saved on the road. All buildings also have to check the road they are connected to regularly. (Didn’t test it yet but I guess this would also take a heavy toll on the performance.)
I could try to use threading/ the job system to reduce the workload from the main thread and prevent freezing of the game. But my current implementation relies on game objects and the Unity API which is not compatible with threading (afaik). I think I could rewrite it to be compatible but I also think that the approach itself is flawed.
Another idea I had was to use an A* algorithm to check the distance of the residential building to the nearest service building. That way I would not have to save the data on the roads but it would still be pretty computation intensive. (I already started to implement this, (also with threading in mind) but I have a few bugs and the algorithm currently does not work properly at all.
As I said I don’t think my approach is the most elegant one. Does someone have a better algorithm or a better approach to store the data in our game to allow faster access and calculations?
If you need further details I will try to provide more details on my current implementation.
We also appreciate every idea and suggestion on the general data management structure to allow efficient data handling for the economy (production, good consummation, taxes, etc.)