ID:142563
 
Code:
mob
verb
Attack()

set category="Combat"
for(var/mob/E in get_step(usr,usr.dir))
if(E.NPC==0)
E.HP-= (usr.Str) - (E.Def)
view()<<"[usr] attacks [E] for [usr.Str] HP!"
if(E.player==1)
if(E.HP<=0)
E<<"<I><small>You died!"
E.HP=E.MHP
E.FP=E.MFP
E.loc=locate(2,2,1)
E.credits-=E.credits
E.credits=0
usr.Exp=E.Expg
E.PK=0
LvlCheck(usr)
return


Problem description: The problem is that when you get attacked if your defence is higher than the enemys strength you will gain health instead of not losing any, but if anyone knows how to can they explain to me how to make the defence only subtract a set percent of the damage

Your attack verb has a small problem, there isn't damage check. When you use damage based on (your strenght)-(opponent strenght) damage can simpy go to negative number if the opponent's str is higher than yours.
Like:
Your Str = 10
Enemy Str = 20

Enemy HP -= (Your Str)-(Enemy Str)
Enemy HP -= 10-20
Enemy HP -= -10 // - and - = +
that's why there should be damage check

Check out this code
mob
verb
Attack()
set category="Combat"
for(var/mob/E in get_step(usr,usr.dir))
if(E.NPC==0)
var/damage - (usr.Str-E.Def)*0.1//0.1 = percent of the damage, 0.1 = 10%, 0.01 = 1%, 2 = 200% etc.
if(damage<0)
damage=0
E.HP-= damage
view()<<"[usr] attacks [E] for [usr.Str] HP!"
if(E.player==1)
if(E.HP<=0)
E<<"<I><small>You died!"
E.HP=E.MHP
E.FP=E.MFP
E.loc=locate(2,2,1)
E.credits-=E.credits
E.credits=0
usr.Exp=E.Expg
E.PK=0
LvlCheck(usr)
return





PS. I don't get why did you make deathproc in attack verb, that way you have to add that lines for every attack. Can't you simply write DeathCheck proc?

mob
verb
Attack()
set category="Combat"
for(var/mob/E in get_step(usr,usr.dir))
if(E.NPC==0)
var/damage - (usr.Str-E.Def)*0.1//0.1 = percent of the damage, 0.1 = 10%, 0.01 = 1%, 2 = 200% etc.
if(damage<0)
damage=0
E.HP-= damage
view()<<"[usr] attacks [E] for [usr.Str] HP!"
DeathCheck(E)
mob
proc
DeathCheck(mob/E)
if(E.player==1)
if(E.HP<=0)
E<<"<I><small>You died!"
E.HP=E.MHP
E.FP=E.MFP
E.loc=locate(2,2,1)
E.credits-=E.credits
E.credits=0
src.Exp=E.Expg
E.PK=0
LvlCheck(src)

how to make the defence only subtract a set percent of the damage

Here's one way:
damage = usr.Str
percent = (100 - E.Def) / 100
E.HP -= damage * percent


In the above,
  • when Def == 0, percent == 1. damage * 1 means full damage.
  • when Def == 100, percent == 0. damage * 0 means no damage.
  • When Def == 50, percent == .5. damage *.5 means half damage.


Now ask yourself what is the range of Def? Is it from 0 to 100? Or is it some other range?

For example, if your game allows Def from 0 to 20, you might want to compute percent like this:
percent = (20 - E.Def) / 100


And as the other guy said, make sure you check for HP <= 0. But if you use the above percent calculation, your game better make sure Def never goes above the maximum value.
In response to Kisioj
You wrote that deathcheck() backwards. src is supposed to be the mob dying, and the argument is the one doing the killing. So:

mob
proc
DeathCheck(mob/killer)
if(player==1)
if(HP<=0)
src<<"<I><small>You died!"
HP=MHP
FP=MFP
loc=locate(2,2,1)
credits=0
killer.Exp+=Expg
PK=0
killer.LvlCheck()


When calling it, then, it should be E.DeathCheck(src)

I've also changed a few things. LvlCheck() shouldn't have any argument at all.
In response to Garthor
thankyou all for helping me