ID:560715
 
(See the best response by Zecronious.)
I wrote my battle system

My battle system:
-Damage= strength - defense
-Luck may increase damage by three depending on the luck points, the
probability rises
-If the speed is twice is high than the attacker, only 25% of the
damage gets through. If its three times higher or more, only 16%
of the damage gets through. If its four, the victim dodges the
attack.

But this coding, not surprisingly, has many problems and doesn't do what I want it to do.

mob/verb/CAttack3(mob/M as mob)
dmg=usr.str-M.def
if(usr.luck<=10) //if luck is 20 or less do regular damage
M.hp-=dmg
if(usr.luck==20|19|18|17|16|15|14|13|12|11) //if luck is 20 or more do x3
if(prob(25)) //probabiliy 25% damage will increase threefold
dmg*=3
if(usr.luck==30|29|28|27|26|25|24|23|22|21) //if luck 21-30
if(prob(35)) //probability damage *3
dmg*=3
if(usr.luck==40|39|38|37|36|35|34|33|32|31) //if luck 31-40
if(prob(50)) //probability 50
dmg*=3
if(M.luck<=10) //if luck is 20 or less speed increase no chance
M.speed=M.speed
if(M.speed==usr.speed*2)
dmg/=4
if(M.speed==usr.speed*3)
dmg/=6
if(M.speed>=usr.speed*4)
dmg=0
usr << "[M] has dodged your attack" //this always says for some reason
if(M.luck==20|19|18|17|16|15|14|13|12|11) //if luck 11-20 speed increase 25%
if(prob(25))
M.speed*=2
if(M.luck==30|29|28|27|26|25|24|23|22|21)
if(prob(35))
M.speed*=2
if(M.luck==40|39|38|37|36|35|34|33|32|31)
if(prob(50))
M.speed*=2
if(M.hp<=0)
usr.r=1
usr.exp+=M.exp
usr.level_up(usr)
usr.wn=0
M.hp-=dmg
usr << "[M] was hit for [dmg] damage!"
if(usr.luck<=10)//below is a check to return to regular values
M.hp-=dmg
if(usr.luck==2)
dmg/=3
M.hp-=dmg
if(usr.luck==3)
dmg/=3
M.hp-=dmg
if(M.luck==20|19|18|17|16|15|14|13|12|11) //if luck 11-20 speed increase 25%
if(prob(25))
M.speed/=2
if(M.luck==30|29|28|27|26|25|24|23|22|21)
if(prob(35))
M.speed/=2
if(M.luck==40|39|38|37|36|35|34|33|32|31)
if(prob(50))
speed/=2
you can use this. It saves you from all those lines
mob
var
attacking=0
luck = 0
strenght=0
hp=0
speed=3
verb
CAttack3()
for(var/mob/M in get_step(usr,usr.dir))// if M is 1 step away from you
if(usr.attacking)return // if you are not allready attacking
if(prob(20)) // probability 20 to dodge
view()<<"[usr] attempt to attack [M] but he dodged"
else if(prob("[M.luck]")) // probability depends on luck
var/damage = round(usr.strength+rand(15,20)) // random dmg + usr strenght
M.hp -= damage
view()<<"<font color=#ff0000>[usr] hits [M] for [damage] power"
else
var/damage = round(usr.strength+rand(5,10))
M.hp -= damage
view()<<"<font color=#ff0000>[usr] hits [M] for [damage] power"
usr.attacking = 1//
sleep("[usr.speed]")// ATTACK DELAY
usr.attacking = 0//
In response to LaNuiit (#1)
LaNuiit wrote:
you can use this. It saves you from all those lines
>   if(prob("[M.luck]"))
> if(prob(35))
> M.speed*=2
>

How does that work? Doesn't seem to include different ifs of luck
Best response
Alright that piece of code is riddled with errors. It's seriously, seriously bad. That aside, it's totally fixable.

What I'll provide is NOT a full solution but it's going to make it easier for the next poor soul who writes it even better.

Here's why this code is WRONG. One, that's not how you use the 'or' operator. Instead use if(X >= Y & X <= Z). This way you cover the minimum value and the maximum value. Second of all, SEPARATE your code. Running things together doesn't make your program go any faster, it just makes it harder for anyone like ME to read it and help you. It's seriously bad practice to do this unless you're writing something that doesn't matter at all.

Finally, read the comments I left. They're helpful.


mob/verb/CAttack3(mob/M as mob)

dmg = (usr.str - M.def) // Bracketing improves readability.

if(usr.luck <= 10) // Notice you said if less than 20 but it was 10.
M.hp -= dmg // Spacing improves readability making it easier to maintain.

if(usr.luck >= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
dmg *= 3
if(usr.luck >= 21 & usr.luck <= 30)
if(prob(35))
dmg *= 3
if(usr.luck >= 31 & usr.luck <= 40)
if(prob(50))
dmg *= 3

if(M.luck <= 10)
M.speed = M.speed

if(M.speed == usr.speed * 2)
dmg /= 4
if(M.speed == usr.speed* 3)
dmg /= 6
if(M.speed >= usr.speed * 4)
dmg=0
usr << "[M] has dodged your attack" // If is always said, it's because M.speed is always greater the usr.speed * 4. Work out why.

if(M.luck >= 11 & M.luck <= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
M.speed *= 2
if(M.luck <= 30 & M.luck >= 20)
if(prob(35))
M.speed *= 2
if(M.luck <= 40 & M.luck >= 31)
if(prob(50))
M.speed *= 2

if(M.hp <= 0)
usr.r = 1
usr.exp += M.exp
usr.level_up(usr)
usr.wn = 0

M.hp -= dmg
usr << "[M] was hit for [dmg] damage!"

if(usr.luck<=10)
M.hp -= dmg
if(usr.luck == 2)
dmg /= 3
M.hp -= dmg
if(usr.luck == 3)
dmg /= 3
M.hp -= dmg

if(M.luck <= 20 & M.luck >= 11 //if luck 11-20 speed increase 25%
if(prob(25))
M.speed /= 2
if(M.luck <= 30 & M.luck >= 21)
if(prob(35))
M.speed /= 2
if(M.luck <= 40 & M.luck >= 31)
if(prob(50))
speed /= 2
Zec- I appreciate your help. The reason this is so bad is because I'm a beginner. I'm a beginner and trying to do a big project, maybe its too soon.
I edit the code go and look :P it works fine
Uhm... I see several problems in here, aside from the already pointed out ORs
For instance:

if(usr.luck <= 10)
M.hp -= dmg


what this does is damaging the target immediately if luck is 10 or less, then it would still damage it again at the end of the proc. The snippet shouldn't be there at all actually. Just like:

if(M.luck <= 10)
M.speed = M.speed


Literally, if luck is 10 or less do nothing. So why checking at all? ^__^;

I'm a bit short of time to chek the rest now.
In response to TheDarkChakra (#4)
Oh yeah, I understand that. Think big man, you put yourself out there have a great achievement because of it. Dam, keep this going by all means.

I meant to be critical of the code but certainly not you. I think you're on exactly the right track, keep posting segments whenever you need help and we can all lift as we climb.
Lanuiiit- Your code doesn't look anything like mine. I just want mine fixed and shortened to do what I want it to do. But it has many problems. I wish to learn from it.
I will go and look youre code and try to fix it right away just give me some time
His code could possibly be better than mine but I'm running out of time to study it. The use of the for() loop shows it's very possible it's better.
I just cant figure how to include speed and luck at the same time. I want nothing to happen if my speed is four times or more higher than whoever attacks me. But I cannot seem to think how to do that as it gets in the way of other coding here
You probably need to use an 'else' statement. This means either one thing would happen or another.

For example

if(raining)
usr << "We can't can't go camping."

if(sun_is_out)
usr << "The sun is out."

Remember though, the sun is always out even on a rainy day. it doesn't go away. We only might not see it.

Instead what would be better is :

if(raining)
usr << "We can't can't go camping."

else

if(sun_is_out)
usr << "The sun is out."

This way we are only the told the sun is out if it's not raining.
mob
verb
CAttack3(mob/M as mob)
var/dmg = round(str-M.def)
if(M.luck<=20)//if luck is 20 or less do regular damage
M.hp-=dmg
if(M.luck >= 20 && M.luck<=31) //if luck is 21 or 31
if(prob(25))//probabiliy 25% damage will increase threefold
M.hp-=dmg*3//*3 more dmg from org
else //else
M.hp-=dmg // normal


I fixed some things by now

And i advice you just use deadcheck proc which you have to create it
M.DeathCheck()

insted of
   if(M.hp <= 0)
usr.r = 1
usr.exp += M.exp
usr.level_up(usr)
usr.wn = 0
OK, let's see it...

mob/verb/CAttack3(mob/M as mob)
var dmg=usr.str-M.def
var chance = 0 // chance to hit x3
if(usr.luck>=11 && usr.luck <=20) //if luck is 20 or more do x3
chance = 25
else if(usr.luck>=21 && usr.luck <= 40) //if luck 21-30
chance = 35
else if(usr.luck>=31) // can it actually be more than 40? otherwise no need to check anything else but the >= 31
chance = 50
if (prob(chance))
dmg*=3
M.speed*=2

if(M.speed>=usr.speed*4)
dmg = 0
else if(M.speed>=usr.speed*3)
dmg/=6
else if(M.speed>=usr.speed*2)
dmg/=4

if (dmg <= 0)
// whatever message you want to appear
else
M.hp-=dmg
usr << "[M] was hit for [dmg] damage!"
if(M.hp<=0)
usr.r=1
usr.exp+=M.exp
usr.level_up(usr)
usr.wn=0


I don't understand what you are doing here
    
if(usr.luck<=10)//below is a check to return to regular values
M.hp-=dmg
if(usr.luck==2)
dmg/=3
M.hp-=dmg
if(usr.luck==3)
dmg/=3
M.hp-=dmg
if(M.luck==20|19|18|17|16|15|14|13|12|11) //if luck 11-20 speed increase 25%
if(prob(25))
M.speed/=2
if(M.luck==30|29|28|27|26|25|24|23|22|21)
if(prob(35))
M.speed/=2
if(M.luck==40|39|38|37|36|35|34|33|32|31)
if(prob(50))
speed/=2
I am spending hours in trying to make this coding right. I cleaned it up a bit I think. This is seems to work like I want it to. I see order of each counts. This is really challenging for me but I think I did it. Took about 5 hours for this:

   dmg = (usr.str - M.def) // Bracketing improves readability.
maxdmg = (usr.str-M.def)


if(usr.luck <= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
dmg *= 3
if(usr.luck >= 21 & usr.luck <= 30)
if(prob(35))
dmg *= 3
if(usr.luck >= 31 & usr.luck <= 40)
if(prob(50))
dmg *= 3

if(M.luck >= 11 & M.luck <= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
M.speed *= 2
if(M.luck <= 30 & M.luck >= 21)
if(prob(35))
M.speed *= 2
if(M.luck <= 40 & M.luck >= 31)
if(prob(50))
M.speed *= 2

if(M.speed == usr.speed * 2 & M.speed <= usr.speed *3)
dmg /= 4
if(M.speed == usr.speed * 3 & M.speed <= usr.speed *4)
dmg /= 6
if(M.speed >= usr.speed * 4)
dmg=0
usr << "[M] has dodged your attack" // If is always said, it's because M.speed is always greater the usr.speed * 4. Work out why.


if(M.hp <= 0)
usr.r = 1
usr.exp += M.exp
usr.level_up(usr)
usr.wn = 0

M.hp -= dmg
usr << "[M] was hit for [dmg] damage!"

if( M.speed >= M.maxspeed )
M.speed= M.maxspeed

if(M.luck <= 20 & M.luck >= 11) //if luck 11-20 speed increase 25%
dmg=maxdmg
if(M.luck <= 30 & M.luck >= 21)
dmg=maxdmg
if(M.luck <= 40 & M.luck >= 31)
dmg=maxdmg

if(usr.dmg>>usr.maxdmg)
usr.dmg=usr.maxdmg

if(M.speed>>M.maxspeed)
M.speed=M.maxspeed
You haven't read my previous post, have you? :-)
I already re-coded the whole first part of yours to make it more performant and less heavy
Well I seem to be doing it. I just need some help with my current coding. It seems to work well now.

mob/verb/CAttack3(mob/M as mob)

dmg = (usr.str - M.def) // Bracketing improves readability.
maxdmg = (usr.str-M.def)


if(usr.luck <= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
dmg *= 3
if(usr.luck >= 21 & usr.luck <= 30)
if(prob(35))
dmg *= 3
if(usr.luck >= 31 & usr.luck <= 40)
if(prob(50))
dmg *= 3

if(M.luck >= 11 & M.luck <= 20) // These three pieces could probably be improved as they do the same thing...
if(prob(25))
M.speed *= 2
if(M.luck <= 30 & M.luck >= 21)
if(prob(35))
M.speed *= 2
if(M.luck <= 40 & M.luck >= 31)
if(prob(50))
M.speed *= 2

if(M.speed == usr.speed * 2 & M.speed < usr.speed *3)
dmg /= 4
if(M.speed == usr.speed * 3 & M.speed < usr.speed *4)
dmg /= 6
if(M.speed >= usr.speed * 4)
dmg=0
usr << "[M] has dodged your attack" //It never says this. This never happens

if(M.hp <= 0)
usr.r = 1
usr.exp += M.exp
usr.level_up(usr)
usr.wn = 0

M.hp -= dmg
usr << "[M] was hit for [dmg] damage!"


if(M.luck <= 20 & M.luck >= 11) //if luck 11-20 speed increase 25%
dmg=usr.str-M.def
if(M.luck <= 30 & M.luck >= 21)
dmg=usr.str-M.def
if(M.luck <= 40 & M.luck >= 31)
dmg=usr.str-M.def

if(usr.dmg>usr.maxdmg)
usr.dmg=usr.maxdmg

if(M.speed>M.maxspeed)
M.speed=M.maxspeed
Look at my post, it already was exactly what you are asking for.
proc
CalcDmg()
var/baseD=Strength-Defense
if(baseD<0)baseD=1
return baseD

CalcBonusDmg(var/currdmg)//pass curr dmg value here.
if(prob(Luck))//remove this line if you want it to calculate regardless
var/luckmod=MaxLuck/100//
var/perc=luckmod*Luck//convert to a percent. if luck is full, 100% or double dmg.
var/newestD=perc+1.0//modifier. modifies the curr dmg, adding one ensures damage increase and also up to triple (3x) dmg
var/newD=currdmg*luckmod//if full luck, double dmg plus 1.0 tripple dmg.
return newD

CalcSpeedReduct(var/mob/User,var/mob/Target,var/dmg)
var/toofast=Target.Speed*2
var/superfast=Target.Speed*3
var/untouchable=Target.Speed*4
if(User.Speed>=untouchable)
return//no dmg to target
else if(User.Speed>=superfast)
//deal 16% of dmg to target
else if(User.Speed>=toofast)
//deal 25% of dmg to target
else
//deal full dmg to target


var/dmg=CalcDmg()
var/d2=CalcBonusDmg(dmg)
CalcSpeedReduct()//add appropriate vars here



This is untested code. But this is the way I would approach it to give you an idea.
Page: 1 2