If you haven't already read Tutorial #6, you should check it out first. Or, start at the beginning, if you're new to the series!
Attacking
To start off, we'll create a common Attack verb, which can be used by both NPCs and Players.
Step 1.1 The following code can be inserted at the end of Verbs.dm (under the Who() verb)
Attack()
flick("Attack",src)
for(var/mob/M in get_step(src,src.dir))
var/Damage=max(0,src.Str-M.Def)
view(M)<<"[src] hit [M] for [Damage] Damage!"
M.TakeDamage(Damage,src)
flick() is a built in proc that will play an icon_state from start to finish on an object.
Since we don't have an attack animation for our player, this will just cause us to disappear.
get_step() is another built in proc, it will return the tile that is one step in the specified direction from a specified object.
In our case, we use get_step() to find the tile in front of the attacker.
We use a for() loop to go through every mob in front of the attacker, and set them to M.
Next we figure out how much damage we dealt. This just uses a basic Strength-Defense formula.
We use max() here to make sure the damage isn't negative.
Then we output a simple message to everyone nearby about the attack and damage.
The last line calls a new proc (TakeDamage) which we will create in the next section.
Step 1.2 Now that we can attack, lets process the actual damage. The following code can be inserted at the end of Procs.dm (under the LevelCheck() proc)
TakeDamage(var/Damage,var/mob/Attacker)
src.HP-=Damage
src.DeathCheck(Attacker)
The TakeDamage() proc takes 2 arguments. One for the damage being taken, and a /mob type variable for the person dealing it.
It then does a simple subtraction of the damage from the targets HP.
Finally it runs a DeathCheck proc on them, which is created below.
Step 1.3 Now we need to check if you're dead! The following code can be also inserted at the end of Procs.dm (under the TakeDamage() proc)
DeathCheck(var/mob/Killer)
if(src.HP<=0)
if(src.client)
world<<"[Killer] Killed [src]!"
src.HP=src.MaxHP
src.loc=locate(5,5,1)
else
Killer<<"<b>You Killed [src] for [src.Exp] Exp"
Killer.Exp+=src.Exp
Killer.LevelCheck()
del src
First, we check if you're actually dead, if your HP is less than or equal to zero.
We then do a quick check if its a player (with a client) or just an NPC, since we'll want to handle their deaths differently.
If a Player dies, we'll let the entire server know =(
Then we'll reset their HP to max.
Finally, we'll relocate them, preferably somewhere safe.
If an NPC was killed, we give whoever killed it some Exp.
We'll use the Exp variable on the Enemy to represent how much they're worth.
Then we'll run the LevelCheck() proc on the Killer to see if they have enough exp to level up.
Then we delete the NPC from existence!
Continue to Tutorial #8: Enemy AI