Power System (Creating)

I have a workable idea on a power system I just need a primer on the way the engine functions so I can add it in. Right now I am planning only for a VERY VERY bare bones system. But one that can easily be expanded on later on.

Each building is powered by the plant closest to it. If that plant is on, then that building has power, if not, it doesn’t. Simple.

This means if a city is split between plants one house can be powered up, and next door the power off. This happens real life too!

Power plants can go offline after X+/-Y hours. Some can be restarted later on with a sufficient Computers/Electronics check. Powered buildings will have working lights, and refrigerators. Other appliances can come later on. At first, maybe only refrigerators. The lighting code is likely complex.

The initial plant will just be a solar power plant, since those are easy to place, and require little upkeep. It will likely have zombies inside.

Implementation:
Basically. We generate power plants and then calculate the nearest plant to each building. We need to record this in both the building and the station (list in station, pointer in building). In the station so if the power goes off, we turn off all appliances in all connected buildings. In the building so we can check if the power exists when toggling power on appliances.

They will need to be turned back on once and while if they go offline. Most will be off at the start, others will go off within 2-4 days. Restarting will toggle it back on for another 4-12 days. I assume a high computer (8) or electronics (6) skill will be needed to restart the plant.

I assume only 2-4 plants will be generated on initial map gen. These will be placed similar to apartments, towers, or hospitals. But forced to generate (since they are a required structure).

This would be best stored as map data, but would be more backwards compatible if it was only calculated when loading.

If a new plant is generated, recalculate the closest buildings. Initially I will only power building map tiles.

Stuff that will not be added initially:
Player created buildings won’t receive power (which makes sense really). But this system will provide something of a backbone if someone wants to later add player created power (for their base/etc).
Power usage/strain. This makes sense for player power, not for the national grid. Adding it would complicate plant generation.
Repairing/Damaging stations, for now they won’t be broken. Just offline.
Power Lines/Substations. I have an idea for substations (plants that generate no power, but receive it), but power lines would really complicate the system.

I have heard this before, until you start, go away!
Right, right, I understand this point of view. This system however isn’t very complex. I also have 14-15 years programming experience under my belt. The hardest part will be figuring out how the engine is put together and overcoming any issues that pop up because of it’s design.

Thoughts, and so forth.

+15 years?!?!

…Good luck.

Actually I did the math, and it is closer to almost exactly 14-15.

First let me voice my support: It is great that you try to tackle such a large undertaking! It would definitely improve the game and open new options.

My thoughts on this matter: I would suggest starting perhaps with something different then refrigerators. I am not sure how the refrigerator it is implemented, but in the case of powered refrigerators you would have to check regularly if the power plant is still running. If the power plant goes off while you are not around, the refrigerators would have to be switched off even outside your reality bubble (that is the blocks that are updated in your vicinity). Perhaps lighting would be an easier start? You only ever need to update lighting if the player is close.

Edit: Some typos.

It would be easier to keep track of when the power plant shut down. Since that is precisely when the fridge turned off as well (unless the player turned it off, but that will come later). But your right, that ties in with the spoiling system.

Can’t we simplify it and have 1 power plant per city? Or, if the city is really big, 2-3 per city?

Remember, the game starts 5 days after the Cataclysm, so most plants would be offline.
I like the restarting idea - what would be the electronics/mechanics level required? Would it be done via a console or something?
The power plant could work as a “storage battery” in a vehicle, with the refrigerators drawing on it - basically, it’d be a copy of the RV minifridge coding I assume. For the lighting, we could have lamps work like floodlights in cars, again, reusing bits of vehicle code.

Something like this could easily be pushed into the mainline assuming no other problems crop up with it (and we’d welcome the help :D).

Yep, just slowly reading through the code. I expect this will take me a awhile (I donno, week or two), unless someone points me in the right direction as to where I cna start hooking in the code.

In my minds eye, the base code looks a bit like this.

But maybe I am expecting the DDA code to be a bit to neat huh?

Maybe. :stuck_out_tongue:

You’ll definitely need to add new data stashed in the map data that monitors whether or not a building is powered, as well as the location of the power plant doing the powering. As for the power up/shutdown code it would probably need to go into the iexamine.cpp file somewhere for when you examine the main switch. Alternatively you could make a new computer function that would handle it, and then utilize the already existing computer code to set up the switching (which is ok, I guess). You would also probably want to handle multi-tile buildings, so you didn’t end up with a mall where some times are powered by one plant and others by another.

Also are you planning on addressing the fact that most power plants have limited fuel available in this project? Or are you waiting for a later project?

The limited fuel thing might take place as a later update, right now the only planned power plant is one with infinite renewable energy (solar). I expect exhaustible plants will be added in the future, but for now…

I suppose I should make a checklist of how I will go about making it.

Step 0. Read over the code a bit to get a feel for how the code base is setup.
Step 1. Generate the power plant on the map (add zombies, walls, etc).
Step 2. Figure out how to access the map tiles and determine what is in them.
Step 3. Add in the above code in some manner.
Step 4. Add a console to the power plant to ‘turn the power back on’.
Step 5. Figure out how to generate light only inside houses in powered map squares.
Step 6. Figure out some way to toggle those lights on and off (probably on a whole house basis at first).
Step 7. Allow appliances/items to access the power grid in powered squares for their own purposes (whatever that may be).

Very happy to see this! :smiley:

There was also someone elce attempting something similar, not sure what progress he got but heres the post http://smf.cataclysmdda.com/index.php?topic=5050.0

Hi Chase,

I’m also pretty new to the project, but I’ve spent some time digging through the source. A couple things I’ve noted that you’ll probably need to account for:

  1. AFAIK There’s not really a system in place for ensuring one per city or per area; it’s more like “the city is this big, place random things within”. If you could implement something like that as part of this project, that would probably also help with more logical city generation down the line.

  2. As has been mentioned, the game engine only really accounts for/simulates things that happen within a “reality bubble” of a few dozen tiles around the character. For example, if you do something like light a bush on fire a few blocks away from your base and then walk away, the the thing will still be burning every time you come back, even days or weeks later. The turn counter only ticks when you’re close to the object. This is also why random things don’t generally spawn in areas you’ve run through but aren’t usually in. Again, this system could be improved drastically. This causes problems with the “plant stays on for X days” proposal.

  3. The map generation and crunching code is pretty messy. The majority of the content generation is done in a massive switch case statement that’s on the order of ten thousand lines of code. If you could lend your experience to cleaning that up a bit (and perhaps even taking a look at how to adapt the current system to generate and track map content in three dimensions) that would be a huge boon. Additionally I don’t believe that there’s currently a regional metadata system in place, although that would again be awesome to have and could be leveraged to address (2).

Overall, though- this would be great. As a simpler way to approach it, perhaps you could add smaller substations or something along those lines, rather than a major power plant? Similar logic would apply, but this would keep the scale a bit smaller and make things like the “on switch” more realistic. It also removes the “fuel source” problem, as the implication is that there’s a hydroelectric/thermal/nuclear power plant out there somewhere.

I’d imagine a solar plant I’d have capacitors that would keep the power on for a single survivor and some left on devices for 4-7 days. And then at 12:00AM the plant’s own decay (time since restarted) would lower.

Maybe place a decaying switch on a pedistool that is crafted with high mechanics and electronics? And it checks (like an actual growing plant) how long the thing has decayed.

Wow, thanks Kevin for the support, and this thread is so full of good ideas…

[quote=“ClockworkZombie, post:14, topic:4966”]1) AFAIK There’s not really a system in place for ensuring one per city or per area; it’s more like “the city is this big, place random things within”. If you could implement something like that as part of this project, that would probably also help with more logical city generation down the line.[/quote] I would have to read through most if not all of the map generation code, it might require a rewrite.

[quote=“ClockworkZombie, post:14, topic:4966”]2) As has been mentioned, the game engine only really accounts for/simulates things that happen within a “reality bubble” of a few dozen tiles around the character. For example, if you do something like light a bush on fire a few blocks away from your base and then walk away, the the thing will still be burning every time you come back, even days or weeks later. The turn counter only ticks when you’re close to the object. This is also why random things don’t generally spawn in areas you’ve run through but aren’t usually in. Again, this system could be improved drastically. This causes problems with the “plant stays on for X days” proposal.[/quote] What needs to be done is record the last time it was ‘active’ and use that to calculate if something that was suppose to happened, already happened, and apply that if and when the character comes into range.

This works even for a power plant, since if they come across a house, and we check the power we see the plant went off at time X, which was 2 years previous, so all the refrigerators went off 2 years ago as well. So all the food is spoiled.

I see there may be a bit more work involved then I though. I would like to get that sorted out first. I notice there is a mapgen json, would it be possible to move all the current entries to there? If so I could spend some time doing that first, then we could review the map generation code to see if we can support static buildings.

3 dimensional generation of this type is rather simple, that is if I was writing it myself, it would be. But there might be some blocks (assumptions) in the code that may prevent it being easily implemented. Basically when we generate a building with multiple floors, we generate the ground floor, then generate the basement a Z level down, and any higher stories a +Z levels up.

There isn’t really a major issue with simulating other Z levels while on another Z level. That is in this kind of game you don’t really need to. All you need is some kind of event system, so mobs will occasionally wonder up/down the stairs. Mark the ground level as damaged, and when they revisit it, cause some damage where monsters broke through the barricades. Perhaps add a few corpses in the traps in a path to the stairs. But this could be annoying if they somehow completely walled in the lower stairs. I would say after X levels, there should be no events at all, say if you are on the 30th floor of a (cleared) office building, it is unlikely a monster wondered all the way up there.

I had an idea for a substation as mentioned in the OP, it could be placed near the center of the city to power it all with just one station (since the station near the middle will likely be the nearest plant to all the buildings in the city.)

If you don’t want to sort out the map generation (which would be a big task on its own) I would suggest simply using existing map generation for generators. Sure, there would not be a generator for each town or two close together, but it would work well enough for now. The player can always move to a town with a generator nearby.

Unfortunately there isn’t much one can do about it. According to Clockwork there is no metadata system, so I need to at least do something about that.

Chase- all good thoughts! Some other input:

-Yes, the JSON maps system is now in place, and moving buildings to that system would probably help quite a bit in the long run. I don’t think the JSON system currently supports “stitched” (multi-zone or multi-story) construction, though.
-I had the same thought on a “time passed since this was last updated” mechanic, again it comes back to that regional metadata requirement. Making it work well would probably also require being able to access general information about adjacent regions, once again back to the metadata.
-Yeah, generating the upper stories of a building isn’t too bad, the current issue is more all the other stuff that’s implied by having floors aboveground. For example, the current system doesn’t have a good way for the map generator to access the footprint of another region, so even figuring out things like “where the heck are the stairs” or “where does the second story sit in this plot” gets tough. This is why basements have absolutely no correlation to the footprint of the house above them. Then you get into “why can’t I see what’s happening on the ground outside this window” and all that sort of thing, It’s all doable but it’s something that people are going to have to work on together. Offloading more of the content to JSON seems like a good way to walk through and incrementally improve the mapgen system, though.

The current expansion of multilevel content (Labs, anthills, mines) is all belowground stuff, because the logic is so much simpler. You can’t see other levels, you’re limited to interacting with things on your level, the footprint doesn’t really matter because it’s underground, etc.

Anyways, this is great! It seems like adding regional metadata would be a massive step towards getting all of these proposed systems implemented!

Also, here’s a link to an earlier proposal I had for aboveground buildings, some good info there:

http://smf.cataclysmdda.com/index.php?topic=5089.0

A lot of discussions on what’s currently being worked on also happen in the cataclysm IRC and github, FWIW.