How Would I Go About Creating A "Closed Number System?"

I am attempting to create an RTS game inspired by Forts on Steam, where two teams of players must build a base and fight each other, winning only by destroying all spawn locations and finishing off the enemy team. One major factor in building bases is electricity, which powers just about everything that’s important including spawners.

When i say “Number System” i am referring to a system with the following:

- Generators: Adds numbers or “power units” to the system at a set rate per second.
- Storages: Sets the max size of the system by defining the number of units that can fit.
- Consumers: Subtracts units from the system at a set rate per second.

See the picture below for an example:


The system above generates 2 units of power per second, consumes 1 per second, and has a max storage of 200 units.

Now setting up a system to work like that is easy, I currently use a data folder for each team that contains a NumberValue for the power level, and both generators and consumers add and subtract from it. There is also a NumberValue defining the max storage allowed that updates with a for-loop whenever a folder containing all the physical storages is updated, and the power level cant go any higher then what the max storage value allows.

But here’s the problem… I intend for players to expand past their base and build outposts, rather it be to collect resources, or just to expand and gain a possible edge over the enemy. I would rather these outposts not draw from a unified power grid and instead need to have their own grids built from scratch, hence the “Closed” part of the system that im struggling with. Basically, players should be able to create separate grids that don’t affect each other unless physically connected, such as in the picture below:


The two grids above are separate from each other, the objects only affect what their connected to. They can be merged into one grid, or split into more (Note that the wires are not necessarily physically there, they just represent the connections indicated in the objects data)

Another game on Roblox that does this PERFECTLY is Eclipsis (amazing game) which i will link below.

In Eclipsis, players collect a fluid resource called Iridium which is collected from wells and extractors and carried by player-built pipes into storage containers, and then buildings that consume it like fabricators and turrets. Multiple different grids can be built and they don’t affect each other unless physically linked by pipe. Grids can also be split in half or into multiple pieces either intentionally, or from incoming damage severing the pipes. This is basically what im trying to create.

How would i go about creating a system like that? Specifically, how would I define separate grids and their data? Especially when merging or splitting? Any help is greatly appreciated!

5 Likes

I would probably use one script to control all the different things. Since there is probably going to be a lot of calculations, I suggest using an actor to multi thread.
Now I think using object values to connect things would work. Create inputs for each thing that connects to a component and create outputs for each component it connects to. Keep these in seperate folders so that when looping through each value, we get them in order they were connected. Then when calculating power generation and consumption I would start by calculating generation then consumption.
Start off by getting each object that generates power and transfer it to connected storages, if a storage is full, move on to the next output. For machines that need power, check each input, if it’s a storage and has enough power then consume the power and move on to the next output. If the input of a power consuming component is another power consuming component I would just move on and check that components inputs.

The issue would be if something is connected like this
Generator → storage → light → storage
For situations like this I’m not quite sure what I would do. Maybe limiting what can connect to what would be a good idea or making the check for power even more advanced.
This system might not be the most optimized way of doing it, but you could probably improve it and make changes to fit your liking.

Things this system wouldn’t do:

  • Super fast power consumption calculations, unless you really optimize it with multi threading. A generator connected to two seperate storages which in turn is connected to another storage would make power consumption slower.
  • Generator → power consuming compinent. Now this could be fixed by having a small temporary storage in generators.
  • Generator → storage → power consumer → storage

among other things and situations I haven’t accounted for

3 Likes

You can use floodfill algorithm (fluid get it haha) tp detect connected networks.

You will have a global table containing data on all existing connected component and you can iterate through it to see which is connected together or not.

Example floodfill code:

1 Like

You can treat the grid system as a graph where the cables are connections/paths and the generators/storages/consumers are nodes/points. Then you create a loop that loops through all nodes that haven’t been visited and recursively follows the cables and marks every node it finds as “visited”, when a loop doesn’t find any more nodes, it creates a closed system. Then after that the “bigger loop” resumes for the nodes that haven’t been skipped because they were not marked as “visited”(due to a connection not being there relating them with the start search node) and keeps creating closed systems and marking nodes as visited. The bigger loop finished once its out of nodes.

Then you write a big loop that runs per second and for each current system it finds it performs the following sub-functions:

  1. Calculate power to add from generators
  2. Calculate max power from storages
  3. Calculate energy to use from consumers

After those are calculated you calculate the current energy as following:

currentEnergy = math.min(currentEnergy+energyFromGenerators, maxPower)
if currentEnergy >= consumersEnergyPerLoop then
	currentEnergy -= consumersEnergyPerLoop
	print("Power used!")
else
	print("Not enough power to power this closed system!")
end
2 Likes

Let’s say you have a solar panel (2U/s), let’s call it Point A. Let’s also say you have a battery (100U), Point B. The player creates a wire between them to connect them. In that physical wire you can determine their relationship which is the trivial part. You just make a table that keeps track of this data and handles it on game tick accordingly. Now, let’s say another battery (Point C) was added and a wire was connected, again, pretty easy to figure out. But then comes a question of priority, which battery gets filled first? Ideally, you’d say both, but this is heavily up for interpretation. You can either store the distances of every wire and prioritize closest or prioritize least filled, or anything else. But if another network with a solar panel (Point D) and battery (Point E) which was already connected to each other gets connected to the first grid, you can merge their tables and redetermine relationships.

Optimization of a system like this is a whole different beast, but that is really going to be a problem for how you handle everything in considerstion

1 Like

Thank you everybody for your responses, I spent the last week or so experimenting and doing further research on the proposed concepts. Unfortunately, I believe my knowledge of coding isn’t advanced enough to fully understand and practice them in any meaningful way to my situation. Basically, its like reading Chinese lol.

So I did some brainstorming, my problem is specifically creating the tables and knowing how to merge and split them, so I think I found a work-around that is satisfying to me.

Players will now be able to build something called a “Substation” which contains a folder inside that will have all the necessary grid data. Basically, grids are now created when a substation is created, and deleted when one is destroyed. Generators, Consumers, and Storages are all connected to a grid through the substation which now acts as the central heart of the grid. Players will also be able to monitor the grids supply and demand using the substation.

To prevent players from connecting buildings to a substation from all the way across the map, im thinking about adding a maximum connection distance of say… 50 studs around the station, or a building connected to it. This means you cant build far from a substation unless you first create a link of buildings for the power lines to move through. Utility Poles could even be used to specifically extend the radius and could connect from much further then 50 studs.

This would also make substations a primary target for defense and offense alongside other critical buildings which could make things interesting.

Anyways yeah im starting to ramble about my brainstorming process lol, maybe someone else reading this thread with a similar problem will be able to make use of the more advanced concepts, but me personally I had to come up with a work around. Its not perfect but its still pretty good. Thanks again everyone for trying to help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.