Sign in to follow this  
jhoward

Simcity clone - powering the city

Recommended Posts

jhoward    122
I've been developing a SimCity clone for java based phones: The problem I am having is the design of the power system, and how a building knows if it is powered properly. This is the algorithm that I originally thought up: for(every tile in the map) { powered = false if(tile is power source) then powered = true else if(neighbouring tile is powered) then powered = true } This is fine for starting off, but a problem becomes apparent when disconnecting a power line, or the power source is destroyed. Buildings that should not be powered remain powered. This is because buildings next to each other effectively power each other. The only other idea of mine is to keep a table of power sources, and then every now and again branch from each power source and power buildings. Any other ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Best way would be checking if every cell that needs power is connected to a plant that emits enough power.
You would only need to calculate that if something happens to a powerline or to a powerplant, and then do it like Maxis did, dont check this for every second.
My favorite, the SNES version, takes ~10 seconds to recognize that something as power. As far as i remember SC2k needed 30 seconds for a power update.
Maybe do this as a background task, dont know if Java for mobile is capable of that, though. If not, cheat and let him test only one map line per call ;)

Greetings from the friendly lion! ^_^

Share this post


Link to post
Share on other sites
jonahrowley    300
If only the buildings must know if they're powered (not individual power line tiles), then you can optimize this quite a bit. For every group of buildings touching eachother, search every adjoining powerline for either a path to a power plant, or a path to a powered building group.

Either that or go the other way. For every line leading from the power plant, mark the building group it leads to as powered, and do the same from every line from that group.

Share this post


Link to post
Share on other sites
This isn't the answer to your problem, but a diesign issue.
If your buildings must be powered by another building that is next to it, roads would effectivly block all power, unless roads used power. If I remember correctly, SC3K let power 'jump' from five tiles away.

You may already have figured that out, but just in case...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Alpha_ProgDes said:
would a linked list be a feasible?

tile {
bool powered;
tile *north, *south, *west, *east;
}
then if the tile is not powered, unpower the adjoining tiles.
----
I already tried an idea like this, but there isn't enough memory available to do this kind of thing.

Servant of the Lord said
This isn't the answer to your problem, but a diesign issue.
If your buildings must be powered by another building that is next to it, roads would effectivly block all power, unless roads used power. If I remember correctly, SC3K let power 'jump' from five tiles away.

You may already have figured that out, but just in case...
----
You can build power lines across roads, like in the original SimCity. Power can be transfered by either adjacent buildings or power lines. Roads and empty space do not transmit power.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Servant of the Lord
If your buildings must be powered by another building that is next to it, roads would effectivly block all power, unless roads used power. If I remember correctly, SC3K let power 'jump' from five tiles away.

Don't know about SC3k, but in the original Sim City and SC2k you would just build a cable across the road. Buildings are powered, either by being right next to a powered building, or connected to one via power cables.

Share this post


Link to post
Share on other sites
Iftah    413
you run:

for(every tile in the map)
{
powered = false
if(tile is power source) then powered = true
else if(neighbouring tile is powered) then powered = true
}

every frame?

sounds to me its better to update the tile data when power related tiles change,
for example when power source is added do a power "fill" to all its connected tiles.

this way when something is updated to no-power (source or cable destroyed) you fill "no-power" to connected tiles (unless you come up to a power source, then you fill back to power).

Share this post


Link to post
Share on other sites
TANSTAAFL    1160
Actually, I do something like this in my game Hexircuit. Hexircuit is a much simpler example, but the algorithm should be the same.

In Hexircuit, the central node becomes the source of power, and is told to light itself.

The light function takes the following steps:

it first checks to see if it is already lit. if it is, it bails.
if next sets its "lit" property to true.
if then checks to see who it is connected to. It has to actually be connected, and not just next to.
it tells each of the cells to which it is connected to light itself.

so, I use a recursive function to do it. it is pretty fast, even in flash.

Share this post


Link to post
Share on other sites
RobotechII    100
Quote:
Original post by jhoward
I've been developing a SimCity clone for java based phones:


The problem I am having is the design of the power system, and how a building knows if it is powered properly. This is the algorithm that I originally thought up:

for(every tile in the map)
{
powered = false
if(tile is power source) then powered = true
else if(neighbouring tile is powered) then powered = true
}

This is fine for starting off, but a problem becomes apparent when disconnecting a power line, or the power source is destroyed. Buildings that should not be powered remain powered. This is because buildings next to each other effectively power each other.
The only other idea of mine is to keep a table of power sources, and then every now and again branch from each power source and power buildings.
Any other ideas?


Your problem is that you need to go through all the tiles and assign all of them to powered=false first. Then work your way recursively from the powerplant(s) to check to see if the tile is power source or neighbor tiles is powered.

You may be already be doing this but if not I think it would be cool to limit the power a powerplant can supply. Let's say you assign a number to each power plant based on how much power it can generate(ie. a nuclear reactor would have more power than a coal plant) and each connected building will subtract from that number so that when you have too many buildings connected you have a brownout.

Screeny looks nice BTW.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this