Simcity clone - powering the city
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?
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! ^_^
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! ^_^
would a linked list be a feasible?
then if the tile is not powered, unpower the adjoining tiles.
tile { bool powered; tile *north, *south, *west, *east;}
then if the tile is not powered, unpower the adjoining tiles.
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.
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.
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...
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...
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.
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.
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.
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).
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).
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement