on *:text:*:#:{
  var %t = $left($1,1),%1 = $right($1,-1)
  if (%t !isalnum) {
    if ($hget($nick)) {
      if (%1 == Fight) {
        if ($timer(beginfight)) {
          var %cmd = $timer(beginfight).com
          ; .timerbeginfight off
          ; %cmd
          set %team1 $addtok(%team1,$nick,44)
          _amsg $nick has stepped 07forward!
        }
      }
      elseif (%1 == shoot) {
        if ($tb.uturn == $nick) {
          _shoot $nick $2 $iif($u.cgun($nick),$v1,$$3)
        }
        elseif ($istok($+($tb.team1,$chr(44),$tb.team2),$nick,44)) _msg $nick It's not your turn!
      }
      elseif (%1 == turn) { 
        if ($hget(battle)) {
          _msg $chan It's currently $tb.uturn $+ 's turn on $tb.tturn
        }
        else _msg $nick There are currently no battles.
      }
      elseif (%1 == skip) { 
        if (($tb.uturn == $nick) || ($nick == Pass)) { 
          tb.next.turn
          .timernextturn 1 30 tb.next.turn
        }
      }
      elseif (%1 $2 == npc battles) $iif($istok($true 1 on positive Yes,$3,32),begin_npc_battle,end_npc_battle)
    }
  }
}
alias begin_npc_battle {
  if ($timer(npc_battle)) .timernpc_battle off
  set %npc_battles $true
  .signal 208 60
  .timernpc_battle 1 60 pre.npc 
  _amsg * Mob encounters have started!
}
alias end_npc_battle {
  .timernpc* off
  set %npc_battles $false
  _amsg * Mob encounters have stopped.
}
alias pre.npc {
  var %total = $r(1,3)
  var %total = 1
  set %team2
  while (%total) {
    var %name = $+(NPC.,$r(A,Z),$r(10,99))
    set %team2 $addtok(%team2,%name,44)
    dec %total
  }
  _amsg A brawl is beginning! Type "!Fight" to enter within the next 30 seconds!
  .timerbeginfight 1 30 begin.fight
}
alias begin.fight {
  if (%team1) && (%team2) {
    var %n = $numtok(%team2,44)
    while ($gettok(%team2,%n,44)) {
      if (NPC.* iswm $v1) npc_create $+(name=,$v2)
      dec %n
    }
    var %n = $numtok(%team1,44)
    while ($gettok(%team1,%n,44)) {
      if (NPC.* iswm $v1) npc_Create $+(name=,$v2)
      dec %n
    }
    .amsg Battle setup. Team1: $replace(%team1,$chr(44),$chr(32)) Team2: $replace(%team2,$chr(44),$chr(32))
    tb.new
  }
  else { 
    .amsg Not enough participants!
    unset %team*
    if (%npc_battles) {
      .signal 208 60
      .timernpc_battle 1 60 pre.npc 
    }
  }
}
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
      var %n = 1
      while ($gettok(%t2,%n,44)) {
        if (NPC.* iswm $v1) npc_create $v2
        inc %N
      }
      var %n = 1
      while ($gettok(%t1,%n,44)) {
        if (NPC.* iswm $v1) npc_create $v2
        inc %n
      }
      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 round 1
      ; hadd battle nextround 2
      round.check
      hadd battle user.turn $gettok($hget(battle,%fteam),1,44)
      ; .signal 204 1
      .signal 203 %team1 %team2
      .signal 200 $gettok($hget(battle,%fteam),1,44)
      .signal 201 %fteam
      unset %team*
    }
    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
    }
    .timernexturn off
  }
}
alias tb.end {
  if ($hget(battle)) {
    hfree battle 
    .timer* off
    _amsg ! Battle has ended. $iif(!$1-,No winners/losers.,$1-)
    if (%npc_battles) {
      .signal 208 60
      .timernpc_battle 1 60 pre.npc 
    }
  }
}

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 == 106) {
    ; $1 killed $2!!
    if ($u.name($2) == NPC) {
      npc_destroy $2
    }
  }
  if ($signal == 200) {
    if ($hget(battle,nextround) <= 0) .timer 1 2 _amsg It's now $1 $+ 's turn!
    elseif ($tb.team1) && ($tb.team2) _amsg It's now $1 $+ 's turn!
    if ($u.name($1) == NPC) {
      ; It's now $1's turn
      .timernpcatk. $+ $1 1 $r(1,3) npc_randatk $1
    }
    .timernextturn 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!
    round.check
  }
  elseif ($signal == 204) {
    ; NEXT ROUND
    if ($1 >= 5) {
      tb.end
    }
    else _amsg - Round $+(,$1,) -
  }
  elseif ($signal == 107) {
    ; $1 just shot $2 with $3
    .timernextturn 1 2 tb.next.turn
  }
  elseif ($signal == 208) {
    ; $1 seconds until encounter
    _amsg Prepare for battle in04 $1 seconds!
  }
}
alias round.check { 
  if ($hget(battle,nextround) <= 0) {
    hinc battle round 
    hadd battle nextround $calc($numtok($tb.team1,44) + $numtok($tb.team2,44) - 1)
    .signal 204 $hget(battle,round)
  }
  else hdec battle nextround
}
alias npc_randatk {
  if (!$hget($1,paused)) {
    var %targets = $iif($istok($tb.team1,$1,44),$tb.team2,$tb.team1)
    var %melee_firearm = 2
    if (%melee_firearm == 1) && ($u.mel($1)) {
      var %n = $numtok($u.mel($1),44)
      var %atk = $gettok($u.mel($1),$r(1,%n),44)
      ;echo -a %atk $1 $iif($2,$2,$gettok(%targets,$r(1,$numtok(%targets,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)
      _shoot $1 $iif($2,$2,$gettok(%targets,$r(1,$numtok(%targets,44)),44)) %atk
    }
  }
}
alias npc_create {
  if ($wildtok($1-,copy=*,1,32)) var %total = $gettok($v1,2,61)
  else var %total = 1
  var %n = 1
  while (%n <= %total) {
    var %money = $r(500,1000),%s = $r(100,200)
    if ($wildtok($1-,name=*,1,32)) && (%total == 1) var %name = $gettok($wildtok($1-,name=*,1,32),2,61)
    else var %name = NPC. $+ $r(a,z) $+ $r(10,99)
    if (!$hget(%name)) && (%name) {
      var %a = hadd -m %name
      %a name NPC
      %a output return
      %a accuracy $r(1,5)
      %a strength $r(2,8)
      %a toughness $r(2,8)
      %a hp %s
      %a fhp %s
      %a nexp 100
      %a exp 0
      %a money $r(50,200)
      %a level 1
      %a commands punch,kick
      .signal 105 %name
      var %weap = $ini(weapons.ini,$r(1,$ini(weapons.ini,0)))
      add_weapon %name %weap
      if ($w.type(%weap) == gun) %a cgun %weap
    }
    inc %n
  }
  return Successully created $calc(%n -1) NPC
}
alias npc_destroy {
  if ($hget($1)) {
    if ($2-) _amsg * $_nick($1) has been destroyed ( $+ $2- $+ )
    hfree $1
  }
}
