4
\$\begingroup\$

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:

enter image description here

  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?

\$\endgroup\$
2
  • 10
    \$\begingroup\$ 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. \$\endgroup\$ Jan 21, 2016 at 15:48
  • 1
    \$\begingroup\$ @AlexandreVaillancourt Yeah, did that :) \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 16:23

2 Answers 2

8
\$\begingroup\$

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
tile.checkAdjacents = function(){
    var adjacents = adjacents(this);

    for(var a in adjacents)
    if(adjacents[a].type == "cable" || adjacents[a].type == "electricItem"){
       adjacents[a].connected = true;
       adjacents[a].checkAdjacents();
       return true;
   }
}

return false;

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

\$\endgroup\$
6
  • \$\begingroup\$ Uhm, not sure if this belongs here but how does the getTile function work? \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 18:07
  • 1
    \$\begingroup\$ @BiiX that function would return the tile asked for by x and y coords, the example above is just theorical pseudocode \$\endgroup\$
    – PRDeving
    Jan 21, 2016 at 18:43
  • \$\begingroup\$ So, it returns a variable with the type of "tile" ? How does that work o.o Any links? \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 18:56
  • \$\begingroup\$ @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 \$\endgroup\$
    – PRDeving
    Jan 21, 2016 at 18:58
  • 4
    \$\begingroup\$ This algorithm is called "flood fill". \$\endgroup\$
    – Philipp
    Jan 21, 2016 at 20:37
3
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ How would I get the connected generator/cable object so i can check if they "isPowerSource" ? \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 16:22
  • \$\begingroup\$ 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) \$\endgroup\$
    – Mayuso
    Jan 21, 2016 at 16:24
  • \$\begingroup\$ 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 ) \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 16:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mayuso
    Jan 21, 2016 at 16:33
  • \$\begingroup\$ 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 \$\endgroup\$
    – BiiX
    Jan 21, 2016 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .