ID:1608212
 
(See the best response by Ter13.)
I've been working on a power system for some time now, and it works, quite well, actually. Until I get stuck in it's placement.
It's been a problem for weeks now, and every time I thought of something that may work, I keep over-complicating it, or it just doesn't work at all.

My system works off of the cable's "cabledirs" list, which indicates, based on the icon state's number, what directions it can accept other lines from. Done so:
/obj/cable/New()
..()
spawn(10)
switch(icon_state)
if("0-1")
if(master)
for(var/obj/machinery/M in src.loc.contents)
master.machinery.Add(M)
M.connected = 1
cabledirs = list(SOUTH)
if("0-2")
if(master)
for(var/obj/machinery/M in src.loc.contents)
master.machinery.Add(M)
M.connected = 1
cabledirs = list(NORTH)
if("0-4")
if(master)
for(var/obj/machinery/M in src.loc.contents)
master.machinery.Add(M)
M.connected = 1
cabledirs = list(EAST)
if("0-8")
if(master)
for(var/obj/machinery/M in src.loc.contents)
master.machinery.Add(M)
M.connected = 1
cabledirs = list(WEST)
if("1-2")
cabledirs = list(NORTH, SOUTH)
if("1-4")
cabledirs = list(NORTH, EAST)
if("1-8")
cabledirs = list(NORTH, WEST)
if("2-4")
cabledirs = list(SOUTH, EAST)
if("2-8")
cabledirs = list(SOUTH, WEST)
if("4-8")
cabledirs = list(EAST, WEST)
if("1-2-4")
cabledirs = list(NORTH, SOUTH, EAST)
if("1-4-8")
cabledirs = list(NORTH, EAST, WEST)
if("2-4-8")
cabledirs = list(SOUTH, EAST, WEST)
if("1-2-8")
cabledirs = list(NORTH, SOUTH, WEST)
if("1-2-4-8")
cabledirs = list(NORTH, SOUTH, EAST, WEST)


That works fine, as it is, but I am just completely stuck on how I'll be handling cable placement. So I hope someone here may have the will to think of something that will get me there.
Best response
Why are you using lists instead of binary?

switch(icon_state)
if("1","2","4","8")
if(master)
for(var/obj/machinery/M in src.loc)
master.machinery.Add(M)
M.connected = 1
cabledirs = text2num(icon_state)


Do you see how much easier that is, to use binary?
In response to Ter13
Ter13 wrote:
Why are you using lists instead of binary?

I haven't really thought of doing that, this seemed to be the best and easiest method at the time, so I went with it.
Edited my message, to show you how easy it would be to use binary.
I guess it'd be easier, since looping through the lists is turning out to be somewhat annoying. I'll change that around, but it still leaves me with the main problem.
I'm not sure what you mean by handling cable placement.
Placing the cable on the floor in the proper direction, I guess it's also a question for the "Design Philosophy" area.

But this is what it currently is:
http://puu.sh/9H3JX/580e4a74af.png

And this is what it should turn out to be:
http://puu.sh/9H3Px/14c3c3e3c2.png

I can't think of a way to make cable placement work the way it's supposed to be. which has turned from a challenge into a headache.

EDIT: I believe it'd be easier for me to kick it down to only 2 directions, and not 3.
At the simplest level, what you are looking for is an autotiling solution. Unfortunately, with objects, it's never going to be all that fast.

var/obj/cable/c
var/cdirs
var/turf/t
for(var/tdir=1;tdir<=8;tdir*=2)
t = get_step(src,tdir)
if(t)
c = locate(/obj/cable/c) in t
if(c)
cdirs |= tdir
src.icon_state = "[cdirs]"


Unfortunately, in your case, cables can be layed side by side and not link up. That's going to make the above algorithm fail in a number of cases.

You are going to have to create a method of distinguishing which cables are linked, and which are unlinked in order to make this algorithm work.
Whilist I've been thinking of ways to ease it up, I decided to go with one-way cables, the system seems to be mostly compatible with the code I already have in place, and makes the cable placement almost entirely without restrictions.

Voted for best answer because you did help me out, so thank you :D