ID:179266
 
I was wondering if there is any logic to direction values?

I did some tests and these are the corresponding values:

9 1 5
8 4
10 2 6

What I want to do is to find out the opposite direction that the player is facing. I could just make a proc and do it manually but it seems like there should be a better way or some sort of trick to it.
the only logic seems to be that the opposite direction is either double or half conversely the valuse of the direction faced.
I have been over movement with a fine tooth comb and there doesn't seem to do be a way to do that simply,
Illya
a bunch of annoying IF statements later ...
In response to Illyena
Yes, the main directions are:


S = 1
N = 2
E = 4
W = 8

Now the cool part diagonals:

NW = N + W = 10-1 = 9
SW = S + W = 9+1 = 10
NE = N + E = 8-3 = 5
SE = S + E = 5+1 = 6

There's actually a patern here, can you see it?
In response to Nadrew
Sorry to correct you Nadrew but the math makes more sence this way, you had N & S reversed in value

NW = N + W = 1-8 = 9
SW = S + W = 2+8 = 10
NE = N + E = 1+4 = 5
SE = S + E = 2+4 = 6

S = 2
N = 1
E = 4
W = 8

Also I thought this was interesting, being for pregramming language and all.

Bits Binary Hexidecimal
(N)1+(S)2+(E)4+(W)8=15 1111 F

NE(5)+SW(10)=15 1111 F

NW(9)+SE(6)=15 1111 F

I really don't know if any of this will help English but here it is anyway.

In response to Nadrew
Ah ok, I see the pattern now :)

Unfortunately I don't think there is anyway to use it to easily convert directions so I guess we'll have to stick with the imbedded if statements.
In response to Illyena
Actually SOUTH is 1, here's a way to see all the values:


client/Move()
mob<<"Your direction number now equals [mob.dir]"
return..()
In response to Nadrew
really ?? I get 1 for north and 2 for south
In response to Illyena
Illyena wrote:
really ?? I get 1 for north and 2 for south

You're right. ;)

From stddef.dm:

var/const
NORTH = 1
SOUTH = 2
EAST = 4
WEST = 8
NORTHEAST = 5
NORTHWEST = 9
SOUTHEAST = 6
SOUTHWEST = 10
UP = 16 //reserved for future use
DOWN = 32

In response to Illyena
Illyena wrote:
NW = N + W = 1+8 = 9
SW = S + W = 2+8 = 10
NE = N + E = 1+4 = 5
SE = S + E = 2+4 = 6

S = 2
N = 1
E = 4
W = 8

Nice observation, Illyena! You can use the binary operators to manage directions. Think of it this way:
        WESN
N = 1 = 0001
S = 2 = 0010
E = 4 = 0100
W = 8 = 1000


So that each direction has one corresponding bit field. So for the pairs, we have:
             WESN
NE = N | E = 0101 = 5
NW = N | W = 1001 = 9
SE = S | E = 0110 = 6
SW = S | W = 1010 = 10

Given a number, you can easily see if it contains a particular direction by using the & operator. The expression:
res = lhs & rhs
compares lhs and rhs bit-by bit, putting a 1 in the result if the bits are both 1, or a 0 if either is 0. So:
NORTHWEST & SOUTH == 1001 & 0010 == 0000 (FALSE) since no bits match, wheras
SOUTHWEST & SOUTH == 1010 & 0010 == 0010 (TRUE)

In code, you could use this like:
if(usr.dir & SOUTH) // a nonzero result means we are facing SOUTH or SOUTHWEST
...

You can also employ the various other bitwise operations to manipulate things of this sort efficiently. For instance, here's one way to reverse a direction:
var/dir
if(usr.dir & SOUTH) dir |= NORTH // dir = dir | NORTH
else if(usr.dir & NORTH) dir |= SOUTH
if(usr.dir & EAST) dir |= WEST
else if(usr.dir & WEST) dir |= EAST
usr.dir = dir

Where the | operator works similar to &, except that it puts a 1 for the result when either the lhs or rhs has a 1, and a 0 otherwise (see the WESN chart above). For our purpose, the |= is the same as the +=.

Hopefully that's an interesting aside that makes some sense.
In response to Vortezz
Then why when running my tests I was getting it the other way around?
In response to Nadrew
I was that weird thing that was happening, they weren't changing at the right time...I got confused...