ID:173912
 
How do I make it so if a monster attacks diagnal it stops it from attacking?
If get_dist() returns 1, I think the target must be right next to the sorce. That is, if it only adds the X distance and the Y distance.
In response to Yota (#1)
that won't work, straight up attacks are ok, but if they attack diagnally the player can't attack back
In response to DarkCampainger (#2)
that won't work, straight up attacks are ok, but if they attack diagnally the player can't attack back

Before the AI attack use get_dir() to check the direction from the AI to the player. Then do the following test on the result

if(!(dir & dir-1))
// Then dir is not a diagonal direction
// so it's safe to attack.
In response to DarkCampainger (#2)
This is what I added:
get_dir(src,M) 
if(!(dir & dir-1))
//I put the attack stuff here



It says "warning: statement has no effect"

what am I doing wrong?
In response to DarkCampainger (#4)
DarkCampainger wrote:
This is what I added:
get_dir(src,M) 
if(!(dir & dir-1))
//I put the attack stuff here
It says "warning: statement has no effect"

Two problems. First, you didn't take the return value from get_dir()--that value should be a new var, and that should go in your if() instead of dir.

The second problem is indentation. The if() is indented too far.

Lummox JR
In response to Lummox JR (#5)
It Worked!
Thanks! How do I get the badguy to move if he can't attack?
In response to DarkCampainger (#6)
DarkCampainger wrote:
It Worked!
Thanks! How do I get the badguy to move if he can't attack?

Try this one:
var/d = get_dir(src, M)
if(d & d-1)
step(src, d & rand(3, 12))
return
... // attack here
In response to Lummox JR (#7)
Thanks! That Worked Great =D
In response to DarkCampainger (#8)
How did you ever come up with that equation?

D & D-1

I'm gonna try to figure that out.

1st, it returns true if both sides of "&" are true.
2nd, if D = 2 (South), both will be true.
3rd, if D = 5 (Northeast), both are still true.

HOW DOES THAT WORK!?

EDIT: After more look, 1 (North) and 0 (Center) will return false.
In response to Yota (#9)
Yota wrote:
How did you ever come up with that equation?

D & D-1

1st, it returns true if both sides of "&" are true.
& returns the bitwise and of two variables not the logical and (ie &&).

All the cardinal directions(NORTH, SOUTH, EAST, and WEST) directions all have only one leading bit whereas the other directions have some combination. If you and a value with itself minus one you will only get a non-zero value if there is more than one bit set.
In response to Yota (#9)
Yota wrote:
How did you ever come up with that equation?

D & D-1

I'm gonna try to figure that out.

1st, it returns true if both sides of "&" are true.
2nd, if D = 2 (South), both will be true.
3rd, if D = 5 (Northeast), both are still true.

HOW DOES THAT WORK!?

EDIT: After more look, 1 (North) and 0 (Center) will return false.

D=2 returns false (2&1==0), so there's a minor flaw in your analysis, but here's how it looks in binary:
  20:  10100
<u>& 19: 10011</u>
16 10000
By subtracting 1 from a number, the rightmost "on" bit is turned off, and all others to the right turn on. AND those two together and the result is to turn off the rightmost bit.

So the test if(D&D-1) returns a nonzero value only if D has 2 or more bits set. If D is 0 or a power of 2, the test is false. Since all of BYOND's directions are bit flags, they all have a single on bit; the diagonal directions combine those, for two on bits.

Lummox JR