ID:262194
 
mob
proc/Choice()
for(var/mob/M in world)
if(M==src)

else
if(M.P1==1)
world<<"[M] (P1): [M.HP]%"
if(M.P2==1)
world<<"[M] (P2): [M.HP]%"
choice = input("What will you do?")in list("Attack","Defend","Desperate Attack")
switch(choice)
if("Attack")
if(src.P1==1)
P1choice="Attack"
P1chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P2chosen==1)
spawn(0)
src.action()
M.action()

else if(src.P2==1)
P2choice="Attack"
P2chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P1chosen==1)
spawn(0)
src.action()
M.action()
if("Defend")
if(src.P1==1)
P1choice="Defend"
P1chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P2chosen==1)
spawn(0)
src.action()
M.action()

else if(src.P2==1)
P2choice="Defend"
P2chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P1chosen==1)
spawn(0)
src.action()
M.action()
if("Desperate Attack")
if(src.P1==1)
P1choice="Desperate Attack"
P1chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P2chosen==1)
spawn(0)
src.action()
M.action()

else if(src.P2==1)
P2choice="Desperate Attack"
P2chosen=1
for(var/mob/M in world)
if(M.P2==1)
if(P1chosen==1)
spawn(0)
src.action()
M.action()
if("Surrender")
world<<"[src] has surrendered."
for(var/mob/M in world)
if(src.P1==1)
if(M.P2==1)
world<<"[M] has won."
if(src.P2==1)
if(M.P1==1)
world<<"[M] has won."
for(var/mob/M in world)
M.loc=locate(5,7,1)
if(M.player==1)
M.player=0
M.icon=0
M.client.eye=locate(5,5,1)

mob
proc
action()
spawn(50)
src.Choice()
if(src.P1==1)
P1chosen=0
if(P1choice=="Attack")
world<<"[src](P1) attacks!"
if(P2choice=="Defend")
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
flick("atk",src)
for(var/mob/M in world)
if(M.P2==1)
flick("hit",M)
M.HP-=10
M.RefreshHP()
sleep(5)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)

else
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
flick("atk",src)
for(var/mob/M in world)
if(M.P2==1)
flick("hit",M)
M.HP-=20
M.RefreshHP()
sleep(5)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)


else if(P1choice=="Desperate Attack")
world<<"[src](P1) launches a fierce blow!"
if(P2choice=="Defend")
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
flick("atk",src)
world<<"... But (P2) defended, and countered with an equally fierce one!"
for(var/mob/M in world)
if(M.P2==1)
flick("dodge",M)
sleep(3)
flick("desp",M)
flick("hit",src)
src.HP-=30
src.RefreshHP()
sleep(5)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)

else
step(src,EAST)
sleep(1)
step(src,EAST)
sleep(1)
step(src,EAST)
flick("atk",src)
for(var/mob/M in world)
if(M.P2==1)
M.HP-=30
flick("hit",M)
M.RefreshHP()
sleep(5)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)
step(src,WEST)
src.dir=EAST
sleep(1)


else if(P1choice=="Defend")
world<<"[src](P1) defends against incoming attacks!"
src.icon_state="def"
spawn(10)
src.icon_state=""

else if(src.P2==1)
P2chosen=0
if(P2choice=="Attack")
world<<"[src](P2) attacks!"
if(P1choice=="Defend")
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
flick("atk",src)
for(var/mob/M in world)
if(M.P1==1)
flick("hit",M)
M.HP-=10
M.RefreshHP()
sleep(5)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)

else
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
flick("atk",src)
for(var/mob/M in world)
if(M.P1==1)
flick("hit",M)
M.HP-=20
M.RefreshHP()
sleep(5)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)

else if(P2choice=="Desperate Attack")
world<<"[src](P2) launches a fierce blow!"
if(P1choice=="Defend")
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
flick("atk",src)
world<<"... But (P1) defended, and countered with an equally fierce one!"
for(var/mob/M in world)
if(M.P1==1)
flick("dodge",M)
sleep(3)
flick("desp",M)
flick("hit",src)
src.HP-=30
src.RefreshHP()
sleep(5)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)

else
step(src,WEST)
sleep(1)
step(src,WEST)
sleep(1)
step(src,WEST)
flick("atk",src)
for(var/mob/M in world)
if(M.P1==1)
M.HP-=30
flick("hit",M)
M.RefreshHP()
sleep(5)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)
step(src,EAST)
src.dir=WEST
sleep(1)


else if(P2choice=="Defend")
src.icon_state="def"
world<<"[src](P2) defends against incoming attacks!"
src.icon_state="def"
spawn(10)
src.icon_state=""


RefreshHP()
for(var/obj/life/o in client.screen)
o.icon_state="[src.HP]"
if(src.HP<=0)
src.HP=0
world<<"[src] is knocked out!"
spawn(10)
for(var/mob/M in world)
if(M==usr)
else
if(src.P1==1)
if(M.P2==1)
if(M.HP<=0)
world<<"Double KO! Play again."
else
world<<"[M] (P2) is the winner!"
world.Reboot()

else if(src.P2==1)
if(M.P1==1)
if(M.HP<=0)
world<<"Double KO! Play again."
else
world<<"[M] (P1) is the winner!"
world.Reboot()


The idea is that each player (maximum of 2) gets a choice asking them what they want to do (Attack, defend, or desperate attack). It then stores their choice in a variable ("P1choice" if they're player 1, "P2choice" if they're player 2) and calls the Action() proc. From there it checks to see if they are P1 or P2, and does their choice accordingly.

Problem is, after the first round, whoever chose first gets to pick BOTH player's choices. And sometimes it just makes Player2 attack twice... It's very confusing. Any help at all would be appreciated...

--Ray
You've made a few design mistakes here that make this code about a million times more complicated than it has to be, which is why this bug is as difficult as it is. Examine this tiny snippet:
if(M==src)

else
if(M.P1==1)
world<<"[M] (P1): [M.HP]%"
if(M.P2==1)
world<<"[M] (P2): [M.HP]%"

Now there are two big problems there. First off is the if/else. Why would you use an empty if() but put everything in the else? The correct way to do that would be:
if(M!=src)
...

Or, alternatively:
if(M==src) continue     // keep looping
...

But worse is the set of vars P1 and P2. There is absolutely no reason in the world you need two vars for this. You could do this any number of better ways. One good choice would be to use a single var, player, which is 0 for non-players and either 1 or 2 for the players. Then your that entire snippet could look like this:
var/mob/M
for(M)
if(M!=src && M.player)
world << "[M] (P[M.player]): [M.HP]%"
break

I put in the break command because it's quite clear in your code that you want to know who the other player is. Your code searches again and again for that other player, but all you need to do is look once. Once that's done, you don't need to loop anymore, because M will always be the opponent. (Of course, to be on the safe side you should bail out if M is null at the end of that loop.)

Then there's the matter of those similar vars P1choice, P1chosen, P2choice, P2chosen, etc. That gets a little ridiculous. For one thing, you don't need the chosen var at all; just the choice var is enough. If it's null or an empty string, that's enough to show no choice has been made. But also, the choice var shouldn't have separate names for P1 and P2. Instead, that var should belong to the mob.
/*
Also very important is to make sure this input() is sent to src, not usr.
usr is the default target, so you need to specify src when you call input().

Make sure choice is set to null for both players before this proc is called.
*/

choice = input(src, "What will you do?") in list("Attack","Defend","Desperate Attack")
// make sure M the opponent is still valid
if(M && M.choice)
spawn(0) action()
M.action()


After you've made the appropriate changes, it should be easy to figure out how to change action() as well and then your bug will be much much easier to spot.

Lummox JR
In response to Lummox JR
Thanks for your help.

--Ray