Simcity style management

Blitz3D Forums/Blitz3D Programming/Simcity style management

AdsAdamJ(Posted 2006) [#1]
I have an idea of a short, quick game I want to make, but it involves a bit of 'SimCity' style management. Here's a simplified idea of what I want:

What I have is a 2d grid map; one or more squares are set as 'generators' and produce power, others are 'buildings' and have to be connected to another powered structure such as another building, or a generator to receive power.

Now, I can get a simple 'powering up' to work, but it's not perfect since if you shut the generator down, the buildings don't power down properly. I'm doing this by checking the buildings next to the current one and if one of them is powered, then the current building also gets powered. Of course, this doesn't work when you turn off the generator because two or more buildings will 'recharge' themselves. I've tried several variations on the basic theory, and none of them seem to work properly.

I hope this makes sense, if not I'll post a simple example. There has to be a simple way of doing this, I can't see using a complex pathfinding routine for every single building as that would really slow everything down and over complicate things.

Any thoughts? Ta. :)


tonyg(Posted 2006) [#2]
Use a simpler pathfinder like breadth/depth first or Dijkstra and have a 'powered' field to your building type. Each time a generator is created run the algo and add 1 to the powered field. Each time it is down decrease the powered field. Not sure how many nodes you have but shouldn't take long. You could always show an electrical arc anim on the generator while you're doing it.


big10p(Posted 2006) [#3]
Unless I'm mis-understanding the problem (never played SimCity, myself), I don't see why you can't just use types/linked lists.

Have a generator type and a building type. The generator type should contain a linked list of all buildings it's powering, either directly, or in-directly.

The building type should contain a field identifying the generator it's being powered by, if any.

When you power-down a generator, go through the linked list and power-down all buildings.

When you create a new building next to a powered one, find which generator that building is being powered by and add the new building to it's linked list.


octothorpe(Posted 2006) [#4]
In SimCity, multiple power plants supply power to the same grid. The problem relates to the tile map of buildings and power lines. Removing a tile might shut off the power to a whole suburb, or there might be piles of redundancy in place.

Because of the myriad of different paths electricity can take through your buildings and power lines, I think some sort of "pathfinding" is the only way to properly solve this problem.

This hypothesis is supported by the fact that even the newer versions (e.g. 4) of SimCity take a few moments to decide that a building has lost or gained power, suggesting that there is pathfinding going on, spread out over the course of many frames to avoid slowdown.

My suggested approach would be to send out "pulses" of power from your generators - a very similar approach to your original strategy. All your buildings would have a "most recently powered" field, and if the power is more than two pulses old, the building is unpowered. Only one pulse would go out at a time. Every frame, you'd spend a certain amount of time extending the pulses to adjacent buildings.

I'll do up a quick and dirty demo to show you what I mean.


octothorpe(Posted 2006) [#5]


Left mouse button: add a house/powerline/whatever (blue)
Middle mouse button: add a powerplant (yellow)
Right mouse button: clear the tile

A simple optimization would be to have the pulse propagation occur from (MAP_WIDTH,MAP_HEIGHT) to (0,0) every other time, instead of only the other way. Because I'm only going down and right from the top-left corner, pulses take a lot longer to propagate up and left.


octothorpe(Posted 2006) [#6]
Here's an example Data set that illustrates the "problem".



Oh, and I guess the other two directions (NE and SW) would be a good idea too, as this would be slow even with the fix suggested above:



Hope this helps you, have fun!


AdsAdamJ(Posted 2006) [#7]
That's fantastic thanks! I'll have a mess around with this and see what I can do. Something I didn't mention in my original post was that each object had 'power stored' variable, and that filled up as time went by. This is going to be important in what I am doing so I'll try to incorperate it into the code you gave above.

Thanks again!


AdsAdamJ(Posted 2006) [#8]
Sweet - got it working. :)
I wasn't far off really! Thanks for giving me the nudge in the right direction though, got the old brain cells cooperating again.

I uploaded the original program I made with Octothorpes modifications to it; you can find it here.

I'm going to modify this even further; perhaps making each power station have an effective range and/or a total power output, etc.


Scherererer(Posted 2006) [#9]
instead of updating the power every frame, why don't you reset all of the tiles to unpowered whenever something is destroyed, then recalculate using whatever method you used before.


octothorpe(Posted 2006) [#10]
Because that's theoretically slow. Finding out which tiles are connected via a whole bunch of adjacent tiles could take a while and cause the game to "hiccup" whenever you needed to recalculate everything all at once.