ID:148001
 
My battle system has two problems:

1)-----I can't fight the other monsters beside Goo without getting this Runtime Error:

runtime error: Cannot read null.Attack
proc name: initiate combat (/mob/proc/initiate_combat)
usr: A Happy Person (/mob)
src: A Happy Person (/mob)
call stack:
A Happy Person (/mob): initiate combat(A Happy Person (/mob), null)
A Happy Person (/mob): Fight Monster()

2)-----Sometimes, the monsters "forget to stop fighting" and the battle doesn't end until they die like 7 times.

here's my code:

mob/Goo
Health = 5
Defense = 2
Attack = 4
Level = 1
mob/Rat
Health = 25
Defense = 10
Attack = 20
Level = 3
mob/Mr_Smith
Health = 50
Defense = 20
Attack = 40
Level = 7
mob/dog
Health = 250
Defense = 100
Attack = 200
Level = 20
mob/verb/Fight_Monster()
var/monstertype = input("Fight what monster?") in list ("Goo -- 1","Rat -- 2","Mr. Smith from next door -- 5","Cute Innosent Doggie -- 10")
var/mob/monster
if(monstertype == "Goo -- 1")
monster = new /mob/Goo()
if(monstertype == "Rat -- 3")
monster = new /mob/Rat()
if(monstertype == "Mr. Smith from next door -- 7")
monster = new /mob/Mr_Smith()
if(monstertype == "Cute Innosent Doggie -- 20")
monster = new /mob/dog()
usr.Health = usr.MaxHealth
usr.Magic = usr.MaxMagic
initiate_combat(usr,monster)
mob/proc/combat(usr,M)
usr << "Magic Not Implemented. Starting Nonmagic battle"
M << "Magic Not Implemented. Starting Nonmagic battle"
initiate_combat(usr,M)
mob/proc/initiate_combat(usr,M)
var/damage
var/mob/A
var/mob/B
var/turn = rand(1,2)
if(turn == 1)
A = usr
B = M
else
A = M
B = usr
sleep(5)
damage = (A.Attack - B.Defense + rand((-1*A.Level),A.Level))
if(damage > 0)
B.Health -= damage
A << "[B] is hit for [damage] damage!"
B << "You are hit for [damage] damage!"
else
A << "The attack bounced off [B]"
B << "[A]'s attack had no effect!"
if(B.Health <= 0)
if(!B.key && B.type != /mob/Goo)
if(B.type == /mob/Rat)
A.Experiance += 2
if(B.type == /mob/Mr_Smith)
A.Experiance += 5
if(B.type == /mob/dog)
A.Experiance += 10
else
A.Experiance += 1
A << "You Win!"
B << "You Lose"
A.levelup()
B.levelup()
else if(A.Health >> 0)
sleep(5)
damage = (B.Attack - A.Defense + rand((-1*B.Level),B.Level))
if(damage > 0)
A.Health -= damage
B << "[A] is hit for [damage] damage!"
A << "You are hit for [damage] damage!"
else
B << "The attack bounced off [B]"
A << "[B]'s attack had no effect!"
if(A.Health <= 0)
if(!B.key && B.type != /mob/Goo)
if(B.type == /mob/Rat)
A.Experiance += 2
if(B.type == /mob/Mr_Smith)
A.Experiance += 5
if(B.type == /mob/dog)
A.Experiance += 10
else
A.Experiance += 1
A << "You Lose"
B << "You Win!"
A.levelup()
B.levelup()
else if(A.Health >= 1)
sleep(5)
initiate_combat(usr,M)
mob/proc/levelup()
if(usr.Experiance >= usr.Experiance_Requirement)
usr << "Level up!"
usr.Level += 1
usr.Experiance_Requirement += rand(3,usr.Level)
usr.MaxHealth += rand(5,15)
usr.MaxMagic += rand(5,15)
usr.Defense += rand(5,15)
usr.Attack += rand(5,15)

if it helps, here's my PvP code which also uses the initiate_combat() proc (and also explaines the one above that)

mob/verb/Fight(mob/M in world)
var/battletype = input("What type of battle? (With/without magic)")in list("Magic","Nonmagic")
M << input(M,"Fight with [usr] in a [battletype] battle?")in list("Yes","No")
if("Yes")
usr.Health = usr.MaxHealth
usr.Magic = usr.MaxMagic
M.Health = M.MaxHealth
M.Magic = M.MaxMagic
if(usr != M)
if(battletype == "Magic") combat(usr,M)
else initiate_combat(usr,M)
else
usr << "No"

If you can answer either of these questions, it would really help. thanks
No put usr in proc. Ungh.

Lummox JR
//Comment your code

//Use descriptive variables, like attacker and target instead
// of A and B

// I think you have this line wrong else if(A.Health >> 0)
// I highly doubt you want to do a bit wise shift

// What big L said about the usr in proc, as in don't

//Why are you awarding exp in a if chain?
// At the very least make them "else if"
// even better, why not give monsters a variable named
// exp_value and then referance that?

//Why are you calling LevelUp() on the loser?
// I don't see them gaining any exp.
// Also, it may cause problems when you del() a dead mob

-Salarn