# How to do placeable electricity cables that work like the ones in "Prison Architect"

I would like to know one (of the probably many) ways to code a (sort of) Prison-Architect-ish electricity cable building system.

Here's a picture of what I mean:

1. How can I detect that cables are connected?
2. How can I detect if the block it's connected to is a power source?

And these 2 (above) will all be placed inside a dictionary when they are placed.

That dictionary does not yet have a key type, but the second type is a sprite.

How should I do this?

• Likely the cables use a floodfill algorithm that starts at the generator and fills outwards until it finds a machine that uses power (adding it to a list). Things like switches would cause an update and a recalc when flipped. Jan 21, 2016 at 15:48
• @AlexandreVaillancourt Yeah, did that :)
– BiiX
Jan 21, 2016 at 16:23

The easiest way is giving a variable "conected = false" to every electric item and then, starting from a energy source, do the following:

function adjacents(tile){
return [
getTile(tile.x-1,tile.y-1), getTile(tile.x,tile.y-1), getTile(tile.x+1,tile.y-1),
getTile(tile.x-1,tile.y), tile, getTile(tile.x+1,tile.y),
getTile(tile.x-1,tile.y+1), getTile(tile.x,tile.y+1), getTile(tile.x+1,tile.y+1),
]
}

// DO FOR EVERY TILE DIRECTLY CONECTED TO THE ELECTRICITY SOURCE

return true;
}
}

return false;


So it'll be setting all conected cables to "connected=true"

• Uhm, not sure if this belongs here but how does the getTile function work?
– BiiX
Jan 21, 2016 at 18:07
• @BiiX that function would return the tile asked for by x and y coords, the example above is just theorical pseudocode Jan 21, 2016 at 18:43
• So, it returns a variable with the type of "tile" ? How does that work o.o Any links?
– BiiX
Jan 21, 2016 at 18:56
• @BiiX thats theorical pseudocode, the main thing is getting all the adjacent tiles, i tried to point out how to detect all the connected wires, you would have to do de implementation Jan 21, 2016 at 18:58
• This algorithm is called "flood fill". Jan 21, 2016 at 20:37

Prison Architect uses "pieces" of cables, it is not all one long cable, it is a sum of lots of "pieces" (or squares, not sure how to define it):

Image here (The bright green conected pieces are cables)

I don´t know how they do it, and I bet they are not going to tell us, but I can tell you how I would do it.

1. Assign each "piece" of cable an state (an integer, for example).

You assign each of this states to two sides (Up and down, Up and left(For turns, for example), Left and Right...., all the combinations).

(Edit: You can also do this with four booleans, up, down, left, right, This might be a lot easier)

You then can run a loop checking this state in each piece of cable and determine if they are connected to the left, right, up or down. (They will be conected if there is cable in this position).

2. Assuming (1) is implemented, you only need to check if one of those conected cables is a power source. (For example adding each piece of cable a boolean isPowerSource)

I hope I explained that well, my english is not really good.

• How would I get the connected generator/cable object so i can check if they "isPowerSource" ?
– BiiX
Jan 21, 2016 at 16:22
• I´m talking about object oriented programming now, as I am more experienced with it: Add a boolean isPowerSource to the Cable class, every "normal" cable will have this variable as false, the power source will have this variable as true. (And most likely a different Texture/Image/Icon) Jan 21, 2016 at 16:24
• Yes, (sorry for my horrible explaining... 3: ) I mean : Let's say I add a generator, then I add a cable next to it and then a second generator, how would the game know which one I'm checking? I need some way to check CHICH instance of cable I am actually trying to connect it to. (hopefully this makes sense... If not I'll try again. lol :D )
– BiiX
Jan 21, 2016 at 16:28
• I don´t know what you don´t understand, each "piece" has information if left, right, up and down are connected (including generator), so you can know what generator is connected to the "normal" cable. Jan 21, 2016 at 16:33
• But, I somehow need to check if there actually is a cable next to it and IF it is, then I can set the variables to true depending on where they are located. Or am I just being horrible at thinking? lol
– BiiX
Jan 21, 2016 at 18:06