ID:1523181
 
(See the best response by Ter13.)
I try not to post here but eh. I've been trying to edit Ter13's movement keys system to use diagonal directions, but I haven't been successful all day. I know what the problem is, I just don't know how to solve it.

var/list/opposite_dirs = list(SOUTH, NORTH, null, WEST, SOUTHWEST, NORTHWEST, null, EAST, SOUTHEAST, NORTHEAST)

client
var/movement
var/movement_direction = 0
var/movement_keys = 0

North()
Northeast()
Northwest()
South()
Southeast()
Southwest()
East()
West()

verb/movekey(var/d as num, var/keystate as num)
set hidden = 1

if(!movement_direction)
. = 1

var/opposite = opposite_dirs[d]

if(keystate)
movement_direction |= d
movement_keys |= d
if(opposite & movement_keys)
movement_direction &= ~opposite
else
movement_direction &= ~d
movement_keys &= ~d
if(opposite & movement_keys)
movement_direction |= opposite

if(. && movement_direction)
begin_movement()


An example is if I hold down northeast, and then hold down southeast but then release it, I'll begin moving northwest (its opposite direction). I'm trying to get it so I go back to moving northeast when southeast is released.
Doesn't his movement library support holding down two keys at once, and thus diagonal?
It does, but I want the diagonal keys to also work. You can't move northwest with combination movement due to something having to do with Windows.
Instead of removing opposite direction or whatever, prevent pressing keys that would cancel each-other out?
In response to Jittai
I'll see how that goes when I wake up and post the results.
Hang on. Let me modify a bit:


var/list/opposite_dirs = list(SOUTH, NORTH, null, WEST, SOUTHWEST, NORTHWEST, null, EAST, SOUTHEAST, NORTHEAST)

client
var/movement
var/movement_direction = 0
var/movement_keys = 0

North()
Northeast()
Northwest()
South()
Southeast()
Southwest()
East()
West()

verb/movekey(var/d as num, var/keystate as num)
set hidden = 1

. = d & d - 1
if(.)
var/d2 = d ^ .
d = .
spawn(0)
movekey(d2,keystate)
. = 0

if(!movement_direction)
. = 1
var/opposite = opposite_dirs[d]

if(keystate)
movement_direction |= d
movement_keys |= d
if(opposite & movement_keys)
movement_direction &= ~opposite
else
movement_direction &= ~d
movement_keys &= ~d
if(opposite & movement_keys)
movement_direction |= opposite

if(. && movement_direction)
begin_movement()


Set up your macros like this:

(NORTHEAST) movekey 5 1
(NORTHEAST+UP) movekey 5 0
(NORTHWEST) movekey 9 1
(NORTHWEST+UP) movekey 9 0
(SOUTHEAST) movekey 6 1
(SOUTHEAST+UP) movekey 6 0
(SOUTHWEST) movekey 10 1
(SOUTHWEST+UP) movekey 10 0

In theory, this should work, although, I'm not entirely sure about the outcome.
Now, instead of going northwest, I start moving north.

Edit: It depends on what two keys I press. Going northwest down -> northeast down -> northeast up makes me move west.
Bump. Any ideas, anyone?
Best response
Okay, I just thought this over a bit better. My approach won't work at all for you, because my approach depends on the movement_keys bitflags not having duplicate values. Since WEST & NORTHWEST share a direction, they can't be stored in the same bit as keys. Unfortunately, there's no elegant way to do that without blowing the entire thing up and using an 8bit word, and then converting the aggregate direction based on said word.
In response to Ter13
Alright, no problem.

Thanks for you help you guys.