ID:160099
 
So I made a turn system that can handle 1v1 turn battles, however I'm wondering how I would go about it so that whenever any other NPC or player enters the view or area of one of the battling mob's, they can also join the battle, with the turns now taking in to account this player.


You don't necessarily have to regard this, it's just what I have so far:
mob
var
turn
time_left
ai
playing
canattack=1
battle_handler
var
timeleft
mob
player_one
player_two
New(mob/a,mob/b) //handling two players for now.
if(!a||!b)
return //obviously if neither player is there, we do nothing.
if(rand(1,2)==1)
player_one=a
player_two=b
else
player_one=b
player_two=a
player_one<<"<b><font color=blue>It's your turn!</font></b>"
player_one.turn=1
player_one.playing=1
player_two.playing=1
spawn timer(player_one,player_two) //You're limited on time!




proc
timer(mob/player_one,mob/player_two)
player_one.ai()
timeleft=30
for()
if(!player_one)
return
if(!player_two)
return
if(!player_one.turn||!timeleft)
turn_switch(player_one,player_two)
break
if(!player_one.playing||!player_two.playing)
world<<"one of the players isn't playing."
return //the battle's over.
sleep(10)
timeleft-=1
time_track(player_one,player_two) //Keep track of time, updating their status.
time_track(mob/a,mob/b)
a<<"[timeleft]"
//update players status bars to tell them time left...
turn_switch(mob/player_one,mob/player_two)
player_one<<"<b><font color=red>Switching to [player_two]'s turn.</font></b>"
player_two<<"<b><font color=blue>It's your turn!</font></b>"
player_one.turn=0
player_two.moves=player_two.max_moves
player_two.turn=1
timer(player_two,player_one)
It appears you've hardcoded a system for two players. My I instead suggest my Turns library? It has been tried and tested in multiple projects. Otherwise, you might want to build something new using lists instead of specific player vars.
In response to ACWraith
I have no idea how this works.

mob
BASIC_BOT
icon='BOT.dmi'
New()
..()
spawn for()
sleep(10)
for(var/mob/m in get_step(src,SOUTH))
m<<"You have been challenged by [src]!"
switch(alert(m,"Accept challenge?","Accept?","Yes","No"))
if("Yes")
view()<<"<b>[src] and [m] have commenced battle!"
Battle(m)
break
sleep(30)
proc
Battle(mob/m)
var/datum/TurnHandler/a=new/datum/TurnHandler()
a.AddPlayer(src)
a.AddPlayer(m)
a.turns_BeginTurn()
a.GetCurrentPlayer()<<"It's your turn?"



So how do I change variables on the current player? And what's the difference between rounds and turns? It's hard using somebody's work. >_> I don't even know where to begin...
In response to Speedro
A round refers to a single loop of player turns. Usually, a round begins with the first turn taker and ends with the last turn taker. I also allow developers to give players multiple turns per round, but don't let it confuse you.

Make sure's you're using the correct procs. StartTurn() is the TurnHandler proc that makes things take turns. The turns_BeginTurn() proc is a trigger owned by each player and is where the player should handle whatever it does during its turn. It's the code's equivalent of your "It's your turn" notice. You'll want to define this and some of the other triggers listed in the help file. The TurnHandler is passed along in the arguments. When the player is done, call the TurnHandler's StopWaitingForTurn() proc with StartNext set to TRUE and the next turn will automatically start.

To add a new player after the battle has already started, keep track of your TurnHandler and use AddPlayer(). The new player will start taking turns in the next round.