ID:171782
 
See the diagram below:

AAAXXX
XXXXBX
AAAXXX
AAXAXA
AXAAXA


B is the target. From every X it is a straight line (eight directions) to B. I want to be able to use get_dist while excluding all paths that aren't straight. Is this possible?
Abyssdragon has a library for this type of stuff...
In response to Teh Governator
Not exactly, Chuck. get_line() will work even if the two targets aren't in a perfectly straight line. I also considered using get_dir until I noticed this:

Returns:
The direction from Loc1 to Loc2. Possible results are NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, and SOUTHWEST. An approximation will be made if the true direction is not exactly in line with one of these.

It would be nifty if there was an arguement in get_dir to make it return 0 if it isn't a completely straight line.
In response to HavenMaster
What do you mean by, 'straight line'? I'm getting very confused here.
In response to JohnReaper
He means cardinal directions only I'm guessing.
In response to JohnReaper
Take a look at the diagram. This is a straight line:
A-----------------B

or

A
-
-
-
-
B


These are not straight lines:

A-------
--------B

or

A
-
-
-
B


Straight lines, to me, are the cardinal directions and the main inbetweens with no detours.
In response to Teh Governator
No, I mean cardinal directions and the four in between directions.
In response to HavenMaster
So you want to check for anything in a straight line from each of your directions?

I X X I X X I
X I X I X I X
X X I I I X X
I I I O I I I
X X I I I X X
X I X I X I X
I X X I X X I

X = What's not straight.
I = What is straight.
O = User.

This correct?
In response to JohnReaper
I X X I X X I
X I X I X I X
X X I I I X X
I I I O I I I
X X I I I X X
X I X I X I X
I X X I X X I


Correct.

EDIT:

Too tired, didn't read thoroughly. What I actually want is:

AIIIIIII
IXIIIIII
IIXIIIII
IIIXIIII
IIIIXIII
IIIIIXII
IIIIIIBI
A = Point A
B = Point B
X = Path
I = Uselesss


So rather than returning all directions, just return a number of tiles from A to B, or 0 if there is no straight path from A to B.
B is the target. From every X it is a straight line (eight directions) to B. I want to be able to use get_dist while excluding all paths that aren't straight. Is this possible?

proc/get_s_dist(atom/a, atom/b)
if(a == b)
return 0 //Same object!
if(a.loc == b.loc)
return 0 //Same position
var/xdist = abs(a.x-b.x)
var/ydist = abs(a.y-b.y)
if(!xdist || !ydist) // Straight line that is either vertical or horizontal
return max(xdist,ydist)
else if(xdist != ydist) //Not straight
return 0
else
return xdist // Straight diagonal
In response to Theodis
That's exactly it. Thank you, Theo.
While Theodis's code will work, I question why you'd want to force this straight direction check into get_dist() instead of another proc. Essentially you're asking it two different things: 1) Is it on a straight line to the target, and 2) how far is it?

Since get_dist() would be the "how far" part of that, I would think no good could come of having get_dist() return 0 for the other cases. What about when the distance really is 0? -1 would be a better choice, because it's invalid, but that still leaves the question of why you'd use a modified get_dist() to answer the first part. The code to handle a not-a-straight-line case would be more complicated than if you used some other proc to do this.

What it sounds like you want is a nested if() instead:
if(straight_line(a, b))
... // do something with get_dist()
else
... // do whatever you'd do otherwise
I can't imagine any scenario that would make a 2-in-1 proc for this a better choice than a nested if(). Essentially your code now has to do this instead:
var/d = get_dist_my_way(a, b)
if(d)
... // do something with d
else
... // handle the other case
So you see you haven't saved any effort. Better to keep the line check and the get_dist() call separate.

Lummox JR