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.
ID:179266
Jan 26 2002, 11:21 am
|
|
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() |
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:
|
In response to Illyena
|
|
Illyena wrote:
NW = N + W = 1+8 = 9 Nice observation, Illyena! You can use the binary operators to manage directions. Think of it this way: WESN So that each direction has one corresponding bit field. So for the pairs, we have: WESN 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 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...
|
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 ...