ID:147760
 
mob/verb
kill(mob/M)
Fight(M)

mob/proc
Fight(mob/M)
if(enemy == M) return //we are already fighting this guy
else if(!enemy) spawn Swing() //start swinging
enemy = M
Swing()
usr = src //so AddLife gives credit where it is due
enemy.Fight(src) //make the enemy fight back
while(enemy)
enemy.AddLife(-roll(strength)) //do the damage
sleep(rand(30)) //wait for up to 3 seconds
AddLife(N)
life += N
if(life <= 0)
view() << "[usr] destroys [src]!"
Die()
else if(N < 0)
view() << "[usr] hits [src]."
else usr << "You feel stronger!"
Die()
//dump treasure and die
for(var/T in treasure)
new T(loc)
del src


This is a direct cut from Dan.mud. The Swing() proc has confused me since the day I started using it. (Trinity Star ;) Why is usr = src in there, and how is usr made different from src in this case, and furthermore, you can set usr? Essentially I need an explination of the dynamics here.


~Polatrite~
The AddLife proc probably uses usr. Dan sets it instead of passing a variable.
It's an example of what you have to do if you really, really want to use usr in a proc that can be called by "NPCs"... it's kind of a weird way to handle it (I'm guessing his priority was to not define unnecessary vars or procedures), but yes, it does work.

usr is normally the player who initiates the given chain of events, it's never a computer controlled mob by default... so if a player triggers something that makes a mob attack them, then the player is the usr even inside the mob's own procs.

usr and src are the same within the body of the Swing() proc, but when it calls AddLife(), src is now the mob who was src.enemy, however, it's still the same stack of events, so usr is still usr, and thus, "[usr] destroys [src]!" will output what it ought to output.
In response to Hedgemistress
Hedgemistress wrote:
usr is normally the player who initiates the given chain of events, it's never a computer controlled mob by default... so if a player triggers something that makes a mob attack them, then the player is the usr even inside the mob's own procs.

Aha. I never knew this. I think I'm just going to entirely rewrite that style of combat system with something a little more... Polaish. Or something. Thanks for the aid.


~Polatrite~