• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM HELP: Power cable continuity detection (Rimworld-like)

B

Belho

Guest
Hi,
I am trying to build a Rimwold-like game.
For now, my colonists can build walls, structures that produce and require energy, and power cables to connect them.
However, I can't seem to figure out a functional and elegant solution to check the continuity of a power cable between, let's say, an iron extractor (which needs energy) and a power source (which produces energy).
The system has to check the continuity of the cable between those two structures and tell the iron extractor that it is plugged to a power source.

Maybe it has been ask before here, but I didn't find any help.
How would you do that?

Thanks a lot!

PS: this is what I would like to obtain, structures connected by cables (blue lines)

 

FrostyCat

Redemption Seeker
This is quite clearly just a pathfinding problem. Get the A* scripts from this post and apply the compatibility fix on the post after, then put a node on each appliance, generator and junction. An appliance is powered if and only if some generator has a path to it.

Edit: Added missing link.
 
Last edited:
M

Maximus

Guest
As it sounds like you don't want to actually create a circuit, but 'send energy from a to b' you would want a tree like structure with 'nodes' at each point where there is a branch. Each node has one parent node that connects back to the power source and as many child nodes attached to them. The power source turns on all its child nodes, then they turn on all their child nodes and so on.
 
B

Belho

Guest
Thank you for the quick and helpful answers guys!
Just some precision: the game world is divided in 32*32 tiles, and for now all structures and obj_power_cable are 32*32 objects.
I actually want to send energy from A to B indeed, thanks to multiple power cable objects create a path. But it seems that you both figured it out already ;)

Regarding the code, what form a 'node' would take? Is it a boolean?
@FrostyCat : would it be feasible to use the built-in pathfinding functions of GM for this problem? I already use it for the movements of my colonists.

Thanks!
 

FrostyCat

Redemption Seeker
@FrostyCat : would it be feasible to use the built-in pathfinding functions of GM for this problem? I already use it for the movements of my colonists.

Thanks!
Theoretically you can do it using mp_grids by first marking the entire board solid, then punching out rectangles over wires, appliances and generators, then finding a path over that.

Also, I fixed the link in my previous post, so try that one if you want a more compact solution.
 
B

Belho

Guest
Theoretically you can do it using mp_grids by first marking the entire board solid, then punching out rectangles over wires, appliances and generators, then finding a path over that.

Also, I fixed the link in my previous post, so try that one if you want a more compact solution.
Wow thanks, your first post is now much more understandable ahah!
I will try to implement it this week, and keep you in touch, thanks again.
 
B

Belho

Guest
So, it was quite simple actually, thanks to the pathfinding methode.
Here is the code for each structure that needs power:
Code:
var i;
for(i = 0; i <= instance_number(obj_power_prod_structure); i++)
{
    var inst = instance_find(obj_power_prod_structure, i);
   
    if mp_grid_path(global.power_grid, power_path, x+16, y+16, inst.x+16, inst.y+16, false)
    {
        powered = true
        break;
    }
    else
    {
        powered = false
    }
}
I have a grid controller object that sets all tiles as forbidden in the power grid:
Code:
global.power_grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32)
mp_grid_add_rectangle(global.power_grid, 0, 0, room_width, room_height)
Finally, in the create event of the power cable objet and in each structure object, we free some space in the power grid, allowing the computation of the path at this location:
Code:
mp_grid_clear_rectangle(global.power_grid, x, y, x + sprite_width/2, y + sprite_height/2)
Thanks again guys!
 
Top