;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; G A M E T O O L S
;; scripted by impulZ
;;
;; visit #ircscript for more informations!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; code
on *:start:{ .timergtools 0 1 gtools_timer }
on *:unload:{ .timergtools off }

alias -l fixpath { return $+(",$scriptdir,$1-,") }
alias -l gdll { dll $fixpath(game.dll) $1- }
alias -l process { return $dll($fixpath(process.dll),$1,_) }
alias -l ingame { if ($process(CS) || $process(Q3) || $process(ET)) { return 1 } }

on *:text:*:*:{
  if ($readini($fixpath(settings.ini),Misc,Highlights) == 1) {
    if ($+(*,$me,*) iswm $1- && $chan) { gdll SendConsole echo $+(^3,$replace($nick,^,`),^1,$chr(40),HIGHLIGHT,$chr(41),^3:^7) $strip($1-) }
  }
  if ($readini($fixpath(settings.ini),Misc,Query) == 1) {
    if (!$chan) { gdll SendConsole echo $+(^3,$replace($nick,^,`),^1,$chr(40),QUERY,$chr(41),^3:^7) $strip($1-) }
  }
}
on *:appactive:{ if (!$timer(gtools)) { .timergtools 0 1 gtools_timer } }
alias gtools_timer {
  if ($status == connected && $readini($fixpath(settings.ini),Autoaway,Away) == 1) {
    if (!$away) {
      if ($readini($fixpath(settings.ini),Autoaway,CS) == 1) {
        if ($process(CS) == $true && !%game.away) {
          set %game.away 1
          .away playing Counter-Strike
          kte_echo -a You've been marked as being away because of starting started Counter-Strike.
          if ($readini($fixpath(settings.ini),Autoaway,Nick) == 1) { nick $me $+ `cs }
        }
      }
      if ($readini($fixpath(settings.ini),Autoaway,Q3) == 1) {
        if ($process(Q3) == $true && !%game.away) {
          set %game.away 1
          .away playing Quake3
          kte_echo -a You've been marked as being away because of starting started Quake3.
          if ($readini($fixpath(settings.ini),Autoaway,Nick) == 1) { nick $me $+ `q3 }
        }
      }
      if ($readini($fixpath(settings.ini),Autoaway,ET) == 1) {
        if ($process(ET) == $true && !%game.away) {
          set %game.away 1
          .away playing Enemy-Territory
          kte_echo -a You've been marked as being away because of starting started Enemy-Territory.
          if ($readini($fixpath(settings.ini),Autoaway,Nick) == 1) { nick $me $+ `et }
        }
      }
    }
    if (%game.away == 1 && $readini($fixpath(settings.ini),Autoaway,Back) == 1) {
      if (!$ingame) {
        if ($away) {
          .away
          if ($readini($fixpath(settings.ini),Autoaway,Nick) == 1) { nick $remove($me,`cs,`q3,`et) }
          kte_echo -a Resetted your status to "Back".
        }
        unset %game.*
      }
    }
  }
}

alias gtools { dialog $iif($dialog(gtools),-v,-m) gtools gtools }
dialog gtools {
  title "Setup Gametools"
  size -1 -1 148 112
  option dbu
  box "Game Autoaway", 1, 1 0 146 67
  check "Set &back if game is closed", 2, 5 46 73 10, disable
  check "Set &away if one of the following games is started:", 3, 5 8 130 10
  check "&Counter-Strike 1.6 / Source", 4, 14 18 77 10, disable
  check "&Quake 3: Arena", 5, 14 27 50 10, disable
  check "&Enemy-Territory", 6, 14 36 50 10, disable
  check "Change &nick on gamestart (`cs, `q3, ` et)", 7, 5 55 114 10, disable
  box "Misc options", 8, 1 68 146 29
  check "Show &highlights in Quake", 9, 5 76 70 10
  check "Show &query messages in Quake", 10, 5 85 87 10
  button "&Done", 11, 71 99 37 12, ok
  button "&Cancel", 12, 110 99 37 12, cancel
}
on *:dialog:gtools:*:*:{
  if ($devent == INIT) {
    if ($readini($fixpath(settings.ini),Autoaway,Away) == 1) { did -c $dname 3 | did -e $dname 4,5,6,2,7 }
    if ($readini($fixpath(settings.ini),Autoaway,CS) == 1) { did -c $dname 4 }
    if ($readini($fixpath(settings.ini),Autoaway,Q3) == 1) { did -c $dname 5 }
    if ($readini($fixpath(settings.ini),Autoaway,ET) == 1) { did -c $dname 6 }
    if ($readini($fixpath(settings.ini),Autoaway,Back) == 1) { did -c $dname 2 }
    if ($readini($fixpath(settings.ini),Autoaway,Nick) == 1) { did -c $dname 7 }
    if ($readini($fixpath(settings.ini),Misc,Highlights) == 1) { did -c $dname 9 }
    if ($readini($fixpath(settings.ini),Misc,Query) == 1) { did -c $dname 10 }
  }
  if ($devent == SCLICK) {
    if ($did == 3) {
      if ($did($dname,3).state == 1) { did -e $dname 4,5,6,2,7 }
      else { did -b $dname 4,5,6,2,7 }
    }
    if ($did == 11) {
      writeini -n $fixpath(settings.ini) Autoaway Away $did($dname,3).state
      writeini -n $fixpath(settings.ini) Autoaway CS $did($dname,4).state
      writeini -n $fixpath(settings.ini) Autoaway Q3 $did($dname,5).state
      writeini -n $fixpath(settings.ini) Autoaway ET $did($dname,6).state
      writeini -n $fixpath(settings.ini) Autoaway Back $did($dname,2).state
      writeini -n $fixpath(settings.ini) Autoaway Nick $did($dname,7).state
      writeini -n $fixpath(settings.ini) Misc Highlights $did($dname,9).state
      writeini -n $fixpath(settings.ini) Misc Query $did($dname,10).state
    }
  }
}
