on *:SIGNAL:*:{
  if ($signal isnum 100-200) {
    if ($signal == 100) {
      ;new acc
    }
    elseif ($signal == 101) {
      ;dec acc
    }
    elseif ($signal == 102) {
      ;login
    }
    elseif ($signal == 103) {
      ;logout
    }
    elseif ($signal == 104) { 
      ;found dead [$1 was killed by $2]
      if (npc.* iswm $1) {
        if ($numtok(%fighting,32) <= 2) && ($numtok(%fighting,32) > 0) {
          if (!$wildtok(%fighting,npc.*,0,32)) .timernpc_create 1 $r(3,8) npc_create
        }
      }
      if (npc.* iswm $2) npc_only_check
    }
    elseif ($signal == 105) {
      ; entered combat
      if (npc.* iswm $1) npc_only_check
      else {
        if ($timer(npc.dall)) .timernpc.dall off
        if ($numtok(%fighting,32) <= 1) .timernpc_create 1 $r(2,6) npc_create
      }
    }
    elseif ($signal == 106) {
      ; left combat
      npc_only_check
    }
    elseif ($signal == 107) {
      ; $1 attacked $2 using $3
      if (npc.* iswm $2) {
        if ($r(1,2) == 2) {
          if (!$timer($+($2,.atk))) {
            $+(.timer,$2,.atk) 1 $r(2,6) npc_randatk $2 $iif($r(1,3) isnum 1-2,$1)
          }
        }
      }
    }
  }
}
alias npc_destroy_all {
  var %n = 1
  while ($wildtok(%fighting,npc.*,%n,32)) {
    var %name = $v1
    npc_destroy $v1 $1-
    inc %n
  }
}
alias npc_only_check {
  if ($wildtok(%fighting,npc.*,0,32) == $numtok(%fighting,32)) {
    if ($timer(npc.dall)) .timernpc.dall off
    .timernpc.dall 1 30 npc_destroy_all Lack of users
  }
}
on *:text:*:*:{
  if (!$chan) close -m $nick
  var %t = $left($1,1),%1 = $right($1,-1)
  if (%t !isalnum) { 
    if ($istok(fight enter play,%1,32)) {
      if ($hget($nick)) {
        if ($u.cgund($nick)) {
          if (!$istok(%fighting,$nick,32)) {
            set %fighting $addtok(%fighting,$nick,32)
            amsgx ! $_nick($nick) entered combat!
            .signal -n 105 $nick
          }
          else _msg $nick You're already in combat
        }
        else _msg $nick You do not have a Gundam. Type .Browse Gundams to find one.
      }
    }
    if ($istok(leave exit,%1,32)) {
      if ($istok(%fighting,$nick,32)) {
        amsgx ! $_nick($nick) is attempting to leave combat...
        $+(.timerleave.,$nick) 1 $r(5,10) _leave $nick 
      }
    }
    if ($istok(entered fighting playing,%1,32)) {
      var %n = 1,%names
      while ($gettok(%fighting,%n,32)) {
        var %c = $v1
        var %names = %names %c $+ ( $+ $ug.hp(%c) $+ HP)
        inc %n
      }
      _msg $nick In Combat: $iif(%names,$v1,N/A)
    }
  }
}
alias _leave {
  if ($timer($+(leave.,$1))) $+(.timer,leave.,$1) off
  set %fighting $remtok(%fighting,$1,32)
  hadd $1 $+($u.cgund($1),.hp) $ug.fhp($1)
  hdel -s $1 $+($u.cgund($1),.shield) 
  amsgx * $_nick($1) has left combat! $iif($2-,( $+ $2- $+ ))
  if ($hget($1,name) == NPC) npc_destroy $1 Left Combat
  .signal 106 $1
}
on *:disconnect:unset %fighting
on *:exit:unset %fighting
