ID:158279
 
Code:


Problem description:

Title basically says it all, I want to make a special skill for rogues "Backstab".
z
This belongs in Developer How-To, but I guess you would compare the loc's.
mob/verb/Back_Stab()
for(var/mob/A in get_step(src,src.dir))
if(istype(A,/mob/Monster)
if(A.dir==src.dir)
//Put whatever you do to hurt things here

For future reference this would be a developer how to not a code problem as you posted no code...
In response to Chowder
Chowder wrote:
> mob/verb/Back_Stab()
> for(var/mob/A in get_step(src,src.dir))
> if(istype(A,/mob/Monster)
> if(A.dir==src.dir)
> //Put whatever you do to hurt things here
>

For future reference this would be a developer how to not a code problem as you posted no code...

thx
Karnaji wrote:
Code:
>

Problem description:

Title basically says it all, I want to make a special skill for rogues "Backstab".


mob
verb
WhoIsBehindMe()
for(var/mob/M in get_step(src, ReverseDir(dir)))
world << M.name


proc
ReverseDir(dir)
return turn(dir, 180)


Something like that?

I haven't really tested this (Too lazy to setup an environment), but it should work and if not it does give an idea of the procedures to use.
If you don't have code to provide, this doesn't belong in Code Problems but rather in Developer How-To. I'll move it this time but in the future I may outright delete it, so be wary of where you post.

The index has descriptions of each forum.

As for your problem, you can find a mob in front of you with locate() and get_step().

mob/proc/mob_in_front()   // returns the mob in front of src or null
var/mob/m = locate(/mob) in get_step(src, src.dir)
return m

mob/proc/is_behind(mob/m) // m is assumed to be in front of of src
if(src.loc == get_step(m, turn(m.dir, 180)))
return 1
else
return 0

mob/verb/backstab()
var/mob/other_mob = src.mob_in_front()
if(other_mob)
if(src.is_behind(other_mob))
usr << "You stab [other_mob.name] in the back!"
else
usr << "You are not behind [other_mob.name]!"
else
usr << "There is nobody in front of you!"


Of course, you can make both is_behind() and mob_in_front() into single-statement procs, like so:
mob/proc/mob_in_front()
return (locate(/mob) in get_step(src, src.dir))

mob/proc/is_behind(mob/m) // m is assumed to be in front of src
return (src.loc == get_step(m, turn(m.dir, 180)))


The first examples were lengthy to be more understandable.

Edit: AJX pointed out a logic error and Kaioken wanted to be a pain in the ass.
Uhh...
var/atom/a = locate() in get_step(ref,turn(ref,180))

I've always just used that. What's with all the complicated-ness?

EDIT
Kuraudo did a similar method (I just looked at his post closer haha), but making a separate proc to do one line's worth of programming is pointless in my opinion.
In response to Spunky_Girl
Spunky_Girl wrote:
Kuraudo did a similar method (I just looked at his post closer haha), but making a separate proc to do one line's worth of programming is pointless in my opinion.

When you're teaching someone that doesn't know what the heck you're doing, it is of course better to be more demonstrative. Specifically declaring which part locates the mob in front of you and which part checks if you're behind it is a good example.
In response to Kuraudo
Your opposite dir is checking to see if they are facing the opposite of you, which doesn't mean you're behind them.

Example:

A B
Both are facing EAST.

A is behind B. Their dir's are identical. Your proc would return false, because it would check to see if his dir is WEST.

You'd actually want to check two things:
1: Are you facing them?
If yes:
2: Are they facing the same dir as you?
If yes: You are behind!
In response to AJX
Yes what they are posting is not what he wanted, but what bugs me more is I already solved his problem...
In response to Chowder
Chowder wrote:
Yes what they are posting is not what he wanted, but what bugs me more is I already solved his problem...

Kuraudo's method is much more 'appropriate' than yours.

It is a good idea to be as modular as possible, in case you need to make alterations or exceptions to existing procs.

EDIT: Forgot to mention for reusing the same procs for later functionality. Then when you have to come back and make changes it is all done at the same time instead of having to search through your code.

It may seem 'excessive' to divide up procs like that, but it is a good programming practice.
In response to AJX
No one even cared to appreciate what I wrote :(
In response to AJX
As for as correctly ascertaining being behind something/someone goes: in actuality, you don't need to be facing someone to be behind him, nor to be close to him. You just need to be... behind him. And you'd detect it as follows:
//is atom A behind atom B?
#define is_behind(A,B) (get_dir(A,B) == B.dir)

obj/verb/am_I_behind_you()
set src in view()
if(is_behind(usr,src))
usr << "You are behind [src]."

(That detects being directly behind something, not say behind and a little to the side)
Of course, for a sneak attack to hit you'd want the player to have to face the target player, but that's besides the point. If you have something implemented like stealth where you can sneak behind NPCs, you need to correctly determine whether a player is behind an NPC or not, and that is obviously independent from where the player is facing himself - it's only dependent on where the NPC is facing.
In response to AJX
Oops, that's what I get for posting and playing Go at the same time. I'll fix it in my post.
In response to Chowder
Chowder wrote:
Yes what they are posting is not what he wanted, but what bugs me more is I already solved his problem...

Granted using locate() is a better idea than using a for() loop like that.
In response to Kuraudo
Still wrong, see my post. Whether a person X is behind person Y doesn't relate to where person X is facing at all. It only depends on person X's position in regards to person Y's facing.
You can be facing away from a person but still be behind him (in that case, incidentally, he is also behind you).
In response to Kaioken
Kaioken wrote:
Still wrong, see my post. Whether a person X is behind person Y doesn't relate to where person X is facing at all. It only depends on person X's position in regards to person Y's.
You can be facing away from a person but still be behind him (in that case, incidentally, he is also behind you).

But you can't stab the person in the back unless you're one tile behind them, facing them. That is what the OP was looking for. 50 tiles EAST of a player facing west doesn't help in that regard.

The functions weren't meant to be taken in a very very general approach but rather for the specific example, in sequential groupings. I could have grouped them by comments, but that would make them less understandable I think to a beginner.
In response to Haywire
Haywire wrote:
No one even cared to appreciate what I wrote :(

What I got from the OP was that he wanted to be able to stab someone in the back. Your method would find someone behind you...so you could force yourself to get stabbed in the back?
In response to Kaioken
What about the other person being beside you and facing the same way?

<-
<-

Rather than...

<- <-

Not to mention that the person could be in FRONT of you as well and facing the same direction and that will return true.

EDIT
I honestly don't see how checking their direction alone is safe enough to assume that a person is directly behind you haha.
In response to Kuraudo
Kuraudo wrote:
Haywire wrote:
No one even cared to appreciate what I wrote :(

What I got from the OP was that he wanted to be able to stab someone in the back. Your method would find someone behind you...so you could force yourself to get stabbed in the back?

Topic title "How would I find out if a player is BEHIND a mob?"

Page: 1 2