"Wires"
Started by theepicgeno, Feb 14 2012 04:39 AM
4 replies to this topic
#1
Posted 14 February 2012 - 04:39 AM
I would like to create a wiring system for a mini game I am making. Since I'm here, ill tell you about it. It's basically a game where you have to get the power to a certain spot. Your running around, placing wires, and then turning on the power to see if it goes through. And it is top down. Anyway, back to what i was saying. I need ideas on how to send the power to the next wire. If you can provide help and/or ideas, that'd be great!
#2
Posted 14 February 2012 - 05:47 AM
So, bioshock hacking basically?
Well, you just give each block an "entrance" and "Exit" variable, according to the block's sprite. These entrance and exits would be like 0-3 corresponding to right,left,up,down. When Checking the power, do something like this pseudocode
So... Maybe that was a wee bit confusing.
Well, you just give each block an "entrance" and "Exit" variable, according to the block's sprite. These entrance and exits would be like 0-3 corresponding to right,left,up,down. When Checking the power, do something like this pseudocode
CoordX = the first tiles x grid
CoordY = the first tiles y grid
nextCoordX = CoordX
nextCoordY = CoordY
while (true)
{
theCurTile = GetTileAt(CoordX,CoordY) //basically make a function that returns the id of the object at grid point CoordX,CoordY)
if (theCurTile.Entrance = 0)
{
nextCoordX = CoordX + 1 //right
}
if (theCurTile.Entrance = 1)
{
nextCoordY = CoordY - 1 //Up
}
if (theCurTile.Entrance = 0)
{
nextCoordX = CoordX - 1 //Left
}
if (theCurTile.Entrance = 0)
{
nextCoordY = CoordY + 1 //Down
}
theNextTile = GetTileAt(nextCoordX,nextCoorY)
if (theCurTile.entrance = 0)
{
if (theNextTile.entrance != 2) //you went right but the next thing's entrance is not left!
{
You Lose
}
}
//repeat for the next 3 curTile entrances
if (theNextTile = theExit)
{
you Win!
}
CoordX = nextCoordX
CoordY = nextCoordY
}
So... Maybe that was a wee bit confusing.
Edited by greep, 14 February 2012 - 05:50 AM.
#3
Posted 14 February 2012 - 02:30 PM
So, bioshock hacking...
I don't know what that is, but ill search it.
CoordX = the first tiles x grid CoordY = the first tiles y grid nextCoordX = CoordX nextCoordY = CoordY while (true) { theCurTile = GetTileAt(CoordX,CoordY) //basically make a function that returns the id of the object at grid point CoordX,CoordY) if (theCurTile.Entrance = 0) { nextCoordX = CoordX + 1 //right } if (theCurTile.Entrance = 1) { nextCoordY = CoordY - 1 //Up } if (theCurTile.Entrance = 0) { nextCoordX = CoordX - 1 //Left } if (theCurTile.Entrance = 0) { nextCoordY = CoordY + 1 //Down } theNextTile = GetTileAt(nextCoordX,nextCoorY) if (theCurTile.entrance = 0) { if (theNextTile.entrance != 2) //you went right but the next thing's entrance is not left! { You Lose } } //repeat for the next 3 curTile entrances if (theNextTile = theExit) { you Win! } CoordX = nextCoordX CoordY = nextCoordY }
So... Maybe that was a wee bit confusing.
Yeah, just a little. I don't understand that level of coding.
#4
Posted 15 February 2012 - 03:15 PM
There are data structures with corresponding functions that allow you to find a path from one place to another while only moving across areas that have a wire object, but if you don't understand greep's code you wouldn't be able to figure that out. My advice would be to have the player maneuver themself through the wires. There would be less logic involved in coding it that way, and it might make the game funner.
#5
Posted 17 February 2012 - 01:37 AM
This is not the right forum for this...but I'll help anyway before reporting.
You need a depth-first search. Technically, breadth-first would work as well, but for wires depth-first just makes more sense for me.
Basically, you create a stack and push the first wire onto it ("first" meaning the one directly contacting the power source). Then, while the stack is not empty, you pop the first wire off the stack, energize it, look at every space it leads to, and push any unvisited, connected wires onto the stack. You also should have a list of visited wires so you don't end up backtracking in an infinite loop.
In the below example code, each wire instance has an array called canConnect, which specifies whether it can connect to a wire in a certain direction. 0=right, 1=up, 2=left, and 3=down.
energize(firstWireInstanceID):
That will set the "on" variable of every wire_obj connected to the initial wire to true.
A breadth-first search does the same thing only with a queue instead of a stack.
*EDIT* Added some comments. I suggest you look up the terms "depth-first search" and "breadth-first search".
-IMP
You need a depth-first search. Technically, breadth-first would work as well, but for wires depth-first just makes more sense for me.
Basically, you create a stack and push the first wire onto it ("first" meaning the one directly contacting the power source). Then, while the stack is not empty, you pop the first wire off the stack, energize it, look at every space it leads to, and push any unvisited, connected wires onto the stack. You also should have a list of visited wires so you don't end up backtracking in an infinite loop.
In the below example code, each wire instance has an array called canConnect, which specifies whether it can connect to a wire in a certain direction. 0=right, 1=up, 2=left, and 3=down.
energize(firstWireInstanceID):
var stack, list, wire, next;
stack = ds_stack_create();
/* Add the initial wire to the stack */
ds_stack_push(argument0);
list = ds_list_create();
/* While there's an unchecked wire on the stack... */
while (!ds_stack_empty(stack)) {
/* Pop the last wire, add it to the list of visited wires, and turn it on. */
wire = ds_stack_pop(stack);
ds_list_add(list, wire);
wire.on = true;
/* If it can connect to the right, any wire to the right that has not been visited is added to the stack. */
if (wire.canConnect[0]) {
next = instance_place(wire.x + 16, wire.y, wire_obj);
if (instance_exists(next) && ds_list_find_index(list, next) < 0) { ds_stack_push(stack, next); }
}
/* Rinse and repeat that for up, left, and down */
if (wire.canConnect[1]) {
next = instance_place(wire.x, wire.y - 16, wire_obj);
if (instance_exists(next) && ds_list_find_index(list, next) < 0) { ds_stack_push(stack, next); }
}
if (wire.canConnect[2]) {
next = instance_place(wire.x - 16, wire.y, wire_obj);
if (instance_exists(next) && ds_list_find_index(list, next) < 0) { ds_stack_push(stack, next); }
}
if (wire.canConnect[3]) {
next = instance_place(wire.x, wire.y + 16, wire_obj);
if (instance_exists(next) && ds_list_find_index(list, next) < 0) { ds_stack_push(stack, next); }
}
}
/* And a simple cleanup */
ds_stack_destroy(stack);
ds_list_destroy(list);That will set the "on" variable of every wire_obj connected to the initial wire to true.
A breadth-first search does the same thing only with a queue instead of a stack.
*EDIT* Added some comments. I suggest you look up the terms "depth-first search" and "breadth-first search".
-IMP
Edited by IceMetalPunk, 17 February 2012 - 01:40 AM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











