on *:text:*:#:{
  var %t = $left($1,1),%1 = $right($1,-1)
  if (%t !isalnum) {
    if ($hget($nick)) {
      if (%1 == Accept) {
        if ($timer(beginfight)) {
          set %team1 $addtok(%team1,$nick,44)
          _msg $nick You're ready to fight!
        }
      }
    }
  }
}
alias pre.fight {
  var %name = $+(npc.,$r(A,Z),$r(10,99))
  set %team2 %name
  .amsg %name wants to fight! type "!Accept" to begin!
  .timerbeginfight 1 20 begin.fight
}
alias begin.fight {
  if (%team1) && (%team2) {
    var %n = 1
    while ($gettok(%team2,%n,44)) {
      npc_create $+(name=,$v1) level=2
      inc %n
    }
    .amsg Battle setup. Team1: %team1 Team2: %team2
    tb.new
  }
  else { 
    .amsg Not enough participants!
    unset %team*
  }
}
alias tb.new {
  if (%team1) && (%team2) {
    ; if ($wildtok($1-,team1=*,1,32)) var %t1 = $gettok($v1,2,61)
    ; if ($wildtok($1-,team2=*,1,32)) var %t2 = $gettok($v1,2,61)
    var %t1 = %team1,%t2 = %team2
    unset %team*
    if (%t1 && %t2) {
      hmake battle
      hadd battle team1 %t1
      hadd battle team2 %t2
      ; Choose who goes first randomly
      var %fteam = $iif($r(1,2) == 1,team1,team2)
      ; LOL 
      hadd battle team.turn %fteam
      hadd battle user.turn $gettok($hget(battle,%fteam),1,44)
      .signal 203 %team1 %team2
    }
    else return Syntax example: /tb.new TEAM1=USER1,USER2 TEAM2=USER1,USER2
  }
  else return Not yet ready for battle
}
alias tb.next.turn {
  if ($hget(battle)) {
    if ($findtok($hget(battle,$tb.tturn),$tb.uturn,1,44) != $numtok($hget(battle,$tb.tturn),44)) {
      hadd battle user.turn $gettok($hget(battle,$tb.tturn),$calc($v1 + 1),44)
      .signal 200 $tb.uturn
    }
    else { 
      hadd battle team.turn $iif(*2 iswm $tb.tturn,team1,team2)
      hadd battle user.turn $gettok($hget(battle,$tb.tturn),1,44)
      .signal 200 $tb.uturn
      .signal 201 $tb.tturn
    }
  }
}
alias tb.end {
  if ($hget(battle)) {
    hfree battle 
    return Battle Finished
  }
}
alias tb.team1 return $hget(battle,team1)
alias tb.team2 return $hget(battle,team2)
alias tb.tturn return $hget(battle,team.turn)
alias tb.uturn return $hget(battle,user.turn)

on *:SIGNAL:*:{
  if ($signal == 200) {
    if ($u.name($1) == NPC) {
      ; It's now $1's turn
      npc_randatk $1
    }
    else { 
      .amsg It's now $1 $+ 's turn!
      timernextturn. $+ $1 1 30 tb.next.turn
    }
  }
  elseif ($signal == 201) {
    ; It's now the next team's turn
    .amsg It's now the opposite party's turn!
  }
}
alias npc_randatk {
  if (!$hget($1,paused)) {
    var %melee_firearm = 2
    if (%melee_firearm == 1) && ($u.h2h($1)) {
      var %n = $numtok($u.h2h($1),44)
      var %atk = $gettok($u.h2h($1),$r(1,%n),44)
      %atk $1 $iif($2,$2,$gettok($tb.team1,$r(1,$numtok($tb.team1,44)),44))
    }
    elseif (%melee_firearm == 2) && ($u.guns($1)) {
      var %n = $numtok($u.guns($1),44)
      var %atk = $gettok($u.guns($1),$r(1,%n),44)
      %atk $1 $iif($2,$2,$gettok($tb.team1,$r(1,$numtok($tb.team1,44)),44))
    }
  }
}
