- Home /
How to model electrical grid/power grid?
I am currently trying to make a simple game where various piece must be connected to form a circuit. there are pieces that generates electricity, pieces that simply conduct it and pieces that consume it.
Each object have a stat called PowerGeneration. Generator have a PowerGeneration= +X, conductors PowerGeneration= 0 (or -0,1 to account for losses of power due to ohmic resistance) and consumer such as motors have PowerGeneration= -2 or higher values. But i can't really think of a way to "limit" the consumption based on the generator.
Do you have any suggestion how to handle the thing?
I can't deter$$anonymous$$e what's being asked. What do you mean by "limit the consumption based on the generator"? From what you describe, it sounds like that's exactly what the negative PowerGeneration variable does already. Can you try to clarify the problem?
i am trying to implement some kind of power grid in my game. something like "SimCity" where power plant had to power various houses/utilities. there will be some player-placeable generators and consumers. what i want to do is that , for every grid, the script check continually every consumer and generator and adds the "PowerGeneration" value, at the end of the cycle if the PowerGeneration value is <0, then the script must return an error. "Grid XX overloaded"
Problem is that i must calculate the sum for every "electrical grid".so, if 2 generators are on 2 different grids (basically 2 circuit never connected between them), they must calculate the usage only based on the effective consumer on that specific grid. the biggest problem is that the player can make a variable number of grid and connect/disconnect them afterwards, it that case, 2 different grids must merge/divide accordingly.
Answer by AlwaysSunny · Dec 09, 2016 at 03:42 PM
Ah, now I grasp what you're wanting. As with all things, there are many ways to handle this. The "best" way would be optimized to account for a potentially huge number of objects, and would be directly integrated with any other systems that work in a similar way (water, traffic, etc) but that is actually a very substantial topic.
Managing a small network (~250 objects) is much simpler, because you can almost ignore optimization (spreading the job of power calculation over multiple frames) and just use the most obvious approach (doing it all in a single frame). If you're eventually wanting many hundreds or thousands of objects in a single network, you'll need to do some serious research into organization / optimization of complex networks / data sets.
The first optimization that comes to mind is a structure which sub-divides a logical network (like each individual power grid) into "chunks" to examine each chunk individually. Thus the whole network (any network: power, water, traffic) is only updated every X frames, where X is the total number of chunks. (I'm fairly confident this is how most systems in most city simulator games distribute work, but they're probably also multi-threading.) But that's getting really deep, and you can totally ignore this depth unless you need that optimization to keep the game running at your target FPS.
Are you planning on a grid-based approach to everything like ANNO, or a freeform approach like Cities:Skylines? A grid-based approach is convenient because you'll already have a data set that is directly related to the logic of other systems (power, water, etc).
I've never done anything quite like this, but there are some logical first steps. I can't tell your level of programming experience so this may sound obvious, but it's very wise to keep collections of any instantiated objects as they are created / destroyed. So your map manager will presumably have a collection of buildings / structures, some of which will participate in the power-logic layer.
When these objects are added or removed from the scene, also add / remove them from a collection of powered_objects. The power (water, traffic,) network(s) will examine only those objects relevant to them so you can safely ignore the rest.
With this in mind, you have to make your own informed decision about the best way to represent your network. I've only ever worked with simple networks of tiles where they are un/walkable, but what you're doing is kind of similar. You want to check that every power-consuming object is connected via an imaginary wire to a power-producing object.
I can't comment much on the best way to represent your networks without knowing more about the game. But however you go about doing so, you'll want a collection of networks (fulfilling the requirement of separate independent networks) and each network has a collection of its power_producers and power_consumers. Then you can just keep a running tally of their consumption / production (as they are added, removed, activated, deactivated) and do a simple difference check.
We can talk more about how to actually network them if you want, but I'll need to understand more about your game and how it's structured.
RSVP or good luck,