/* ==========================================================
= DragonBall Z - v1.0 - by Epsilon
=============================================================
=============================================================
=============================================================
= Please do not change [[[anything]]]] in this file =========
=============================================================
=============================================================
=
= For more mIRC games, visit www.picwin.tk 
=
=============================================================
*/

/*
=============================================================
====== MAIN =================================================
=============================================================
*/

on *:LOAD:{
  if ($version < 6.16) {
    echo DragonBall Z requires mIRC v6.16 or newer to run.    
    unload -rs " $+ $script $+ "
    return
  }
  echo -s DragonBall Z v1.0 version loaded - To play type: /dbz    
}

on *:CLOSE:@DragonBall:close.dbz

alias -l dbzdir {
  return $+(",$scriptdir,$1,")
}
alias -l tick {
  return $dll($+(",$scriptdirdlls/game_tools.dll"),ticks,.)  
}
alias -l playsound {
  if ($hget(dragonball.config,playsound)) dll $+(",$scriptdirdlls/game_tools.dll") splay $scriptdirsounds $+ \ $+ $1-  
}
alias -l playmusic {
  if (!$hget(dragonball.config,playmusic)) return
  splay -m stop
  splay -m $dbzdir(music $+ \ $+ $1-)  
}
alias -l x-gui {
  var %xgui = $dll($+(",$scriptdirdlls/x-gui.dll"),$1,$2-)
  if ($left(%xgui,5) == ERROR) echo -s x-GUI DLL (DBZ): %xgui
  if ($isid) return %xgui  
}
alias -l print {
  if ($1) x-gui Richedit @DragonBall > Echo 1 ( $+ $time $+ ) $1-
}
alias dbz.debug {
  window -od @debug-tcp -1 -1 500 200
  window -od @debug-udp -1 -1 500 200
  db debug 1
}
alias reload.dbz {
  window -c @DragonBall  
  dbz
}
alias dbz {
  if ($window(@DragonBall)) {
    window -a @DragonBall
    return
  }
  .localinfo -uh 
  .timerdbz-* off
  .sockclose dbz_*
  if ($hget(dragonball)) {
    var %reload = 1
    hfree dragonball
  }
  hmake dragonball 10
  db intro loading 
  db version 1.0
  window -hfpodk0B +btn @DragonBall 100 200 518 226 verdana 7 $+(",$scriptdirsprites/dbz.ico")
  window -a @DragonBall
  titlebar @DragonBall Z -  v $+ $db(version)
  drawrect -rf @DragonBall 0 1 0 0 $window(@DragonBall).w $window(@DragonBall).h
  drawtext -r @DragonBall $rgb(255,255,255) Verdana -7 $calc($window(@DragonBall).w / 2 - $width(loading ...,verdana,-7) /2)) 100 Loading ... 
  dll -u $+(",$scriptdirx-gui.dll")
  x-gui Mark @DragonBall
  if ($hget(dragonball.config)) hfree dragonball.config
  hmake dragonball.config 10
  hload dragonball.config $+(",$scriptdirconfig.cfg,")
  if (!$hget(dragonball.config,nick)) hadd dragonball.config nick $me
  db bgcolor $rgb(80,100,150)
  db bgcolor2 $rgb(100,120,170)
  db bgcolor3 $rgb(140,160,210)
  db color $rgb(0)
  db color2 $rgb(160,180,230)
  hadd dragonball fps 0
  hadd dragonball fps.counter 0
  hadd dragonball loop 0
  window -fhpB +d @dbz.tmp.buffer 0 0 526 250
  window -fhpB +d @dbz.tmp.fx 0 0 194 113
  drawrect -rf @dbz.tmp.fx $rgb(0,255,255) 1 0 0 194 113
  drawpic @dbz.tmp.fx 0 0 $+(",$scriptdir/sprites/fx.png")
  drawpic @dbz.tmp.fx 65 0 $+(",$scriptdir/sprites/fx.png")
  drawpic @dbz.tmp.fx 130 0 $+(",$scriptdir/sprites/fx.png")
  drawreplace -r @dbz.tmp.fx $rgb(242,242,242) $rgb(255,250,188) 65 0 64 113
  drawreplace -r @dbz.tmp.fx $rgb(242,242,242) $rgb(204,153,204) 130 0 64 113
  drawreplace -r @dbz.tmp.fx $rgb(216,216,216) $rgb(255,226,66) 65 0 64 113
  drawreplace -r @dbz.tmp.fx $rgb(216,216,216) $rgb(153,102,204) 130 0 64 113
  drawreplace -r @dbz.tmp.fx $rgb(191,191,191) $rgb(255,198,0) 65 0 64 113
  drawreplace -r @dbz.tmp.fx $rgb(191,191,191) $rgb(153,51,204) 130 0 64 113
  window -fhpB +d @dbz.tmp.char 0 0 400 50
  drawpic @dbz.tmp.char 0 0 $+(",$scriptdir/sprites/characters.png") 
  window -fhpB +d @dbz.tmp.font 0 0 560 16
  drawpic @dbz.tmp.font 0 0 $+(",$scriptdir/sprites/font.png")
  window -fhpB +d @dbz.tmp.final 0 0 500 64
  drawrect -rf @dbz.tmp.final $rgb(192,192,192) 1 0 0 500 64
  ;specialwin
  window -fhpB +d @dbz.tmp.special 0 0 164 206
  var %special = special
  if ($hget(dragonball.config,trans) == 1) %special = special2 
  drawpic @dbz.tmp.special 0 0 0 0 164 129 $+(",$scriptdir/sprites/,%special,.png")
  drawpic @dbz.tmp.special 0 129 0 0 164 64 $+(",$scriptdir/sprites/,%special,.png")
  drawpic @dbz.tmp.special 0 193 0 128 164 13 $+(",$scriptdir/sprites/,%special,.png")
  drawreplace -r @dbz.tmp.special $rgb(255,198,0) $rgb(175,49,207) 0 130 164 64
  drawreplace -r @dbz.tmp.special $rgb(242,242,242) $rgb(247,233,241) 0 130 164 64
  drawreplace -r @dbz.tmp.special $rgb(255,250,188) $rgb(247,233,251) 0 130 164 64
  drawreplace -r @dbz.tmp.special $rgb(255,226,66) $rgb(196,102,220) 0 130 164 64
  window -fhpB +d @dbz.tmp.special.mirror 0 0 164 206
  var %i = 0
  while (%i <= 164) {
    drawcopy @dbz.tmp.special %i 0 1 242 @dbz.tmp.special.mirror $calc($window(@dbz.tmp.special.mirror).w - %i -1) 0
    inc %i
  }
  window -fhpB +d @dbz.tmp.txt 0 0 200 172
  drawpic @dbz.tmp.txt 0 0 $+(",$scriptdir/sprites/text.png")
  window -fhpB +d @dbz.tmp.intro 0 0 266 310
  drawpic @dbz.tmp.intro 0 0 $+(",$scriptdir/sprites/intro.png")
  db intro 0
  db intro.r 0
  db intro.s 0
  db dx 0
  playmusic tapion.mid
  db fps.ticks $tick
  db ticks $tick
  intro
  if (%reload) && ($isfile($dbzdir(versions.txt))) {
    window -n @DragonBall
    run $dbzdir(versions.txt)
  }
}

alias -l intro {
  var %ticks = $tick
  drawrect -rf @dbz.tmp.buffer 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  if ($hget(dragonball.config,fps)) drawtext -nr @dbz.tmp.buffer $rgb(255,255,255) verdana -7 4 4 fps: $hget(dragonball,fps)
  if ($calc($tick - $hget(dragonball,fps.ticks)) >= 1000) { 
    hadd dragonball fps $hget(dragonball,fps.counter)
    hadd dragonball fps.counter 0
    hadd dragonball fps.ticks $tick
  }
  if ($db(intro) <= 1) {
    if ($db(intro.s) == 0) var %r = $calc($db(intro.r) + $db(dx))
    else var %r = $calc($db(intro.r) - $db(dx))    
    if (%r > 255) %r = 255
    if (%r < 0) %r = 0 
    db intro.r %r
    var %text
    if ($db(intro) == 0) %text = Epsilon
    else %text = presents
    drawtext -r @dbz.tmp.buffer $rgb(%r,%r,%r) verdana -10 $calc($window(@DragonBall).w / 2 - $width(%text,verdana,-10) /2)) 100 %text
    if ((%r == 255) && ($db(intro.s) == 0)) {
      db intro.s 1
      if (%text == Epsilon) playsound epsilon
    }
    if ((%r == 0) && ($db(intro.s) == 1)) {
      db intro.s 0
      if ($db(intro) == 0) db intro 1
      else db intro 2
    }
  }
  if ($db(intro) == 2) {
    var %r = $calc($db(intro.r) + $db(dx))
    if (%r > 200) %r = 200
    db intro.r %r
    drawcopy -t @dbz.tmp.txt 0 0 0 191 32 @dbz.tmp.buffer $calc(20+ %r) 20
    if (%r == 200) {
      db intro.r 0
      db intro 3      
    }
  }
  if ($db(intro) == 3) {
    var %r = $calc($db(intro.r) + $db(dx))
    if (%r > 300) %r = 300
    db intro.r %r
    drawcopy -t @dbz.tmp.txt 0 0 0 191 32 @dbz.tmp.buffer 220 20
    drawcopy -t @dbz.tmp.txt 0 0 33 126 32 @dbz.tmp.buffer $calc(20+ %r) 70
    if (%r == 300) {
      db intro.r 170
      db intro 4      
    }
  }
  if ($db(intro) == 4) {
    var %r = $calc($db(intro.r) - $db(dx))
    if (%r < 70) %r = 70
    db intro.r %r
    drawcopy -t @dbz.tmp.txt 0 0 0 191 32 @dbz.tmp.buffer 220 20
    drawcopy -t @dbz.tmp.txt 0 0 33 126 32 @dbz.tmp.buffer 320 70
    drawcopy -t @dbz.tmp.txt 0 145 33 31 32 @dbz.tmp.buffer 464 %r
    if (%r == 70) {
      db intro.r 0
      db intro 5      
    }
  }
  if ($db(intro) == 5) {
    db intro.r $calc($db(intro.r) + $db(dx))
    var %r = $int($calc($db(intro.r) / 30))
    ; echo r %r
    if (%r >= 4) {
      %r = 0
      db intro.r 0
    }
    var %rgb = $calc(%r * 46)
    drawcopy -t @dbz.tmp.txt 0 0 0 191 32 @dbz.tmp.buffer 220 20
    drawcopy -t @dbz.tmp.txt 0 0 33 126 32 @dbz.tmp.buffer 320 70
    drawcopy -t @dbz.tmp.txt 0 145 33 31 32 @dbz.tmp.buffer 464 70
    drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 0 214 126 60 @dbz.tmp.buffer 40 166
    drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 0 274 226 36 @dbz.tmp.buffer 240 156
    drawtext -r @dbz.tmp.buffer $rgb(%rgb,$calc(%rgb +100),255) verdana -7 310 126 - click to begin -   
    if (%r == 0) drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 25 107 119 106 @dbz.tmp.buffer 64 60
    elseif (%r == 1) drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 25 0 119 106 @dbz.tmp.buffer 64 60
    elseif (%r == 2) drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 145 0 119 106 @dbz.tmp.buffer 64 60
    elseif (%r == 3) drawcopy -t @dbz.tmp.intro $rgb(255,0,255) 145 168 119 106 @dbz.tmp.buffer 64 60
  }
  drawcopy @dbz.tmp.buffer 0 0 526 250 @DragonBall 0 0
  hinc dragonball fps.counter
  var %loop_time = $calc($tick - $hget(dragonball,ticks))
  var %dx = $round($calc(200/1000 * %loop_time),2) + $hget(dragonball,rest) 
  db dx $calc(0 + $int(%dx))
  db rest $calc(%dx - $int(%dx))
  hadd dragonball ticks $tick
  .timerdbz-main -ho 1 0 intro
}
alias -l main.menu {
  db intro 6
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 216 31
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 236 31
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 282 31
    drawtext -nr @DragonBall 0 verdana -8 244 40 PLAY
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 244 40 PLAY
  if ($db(menu.sel) == 2) { 
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 206 75
    drawcopy -n @dbz.tmp.txt 21 100 70 32 @DragonBall 226 75
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 296 75
    drawtext -nr @DragonBall 0 verdana -8 234 84 OPTIONS
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 234 84 OPTIONS
  if ($db(menu.sel) == 3) { 
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 206 119
    drawcopy -n @dbz.tmp.txt 21 100 70 32 @DragonBall 226 119
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 296 119
    drawtext -nr @DragonBall 0 verdana -8 234 128 CREDITS
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 234 128 CREDITS
  if ($db(menu.sel) == 4) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 216 163
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 236 163
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 283 163
    drawtext -nr @DragonBall 0 verdana -8 245 172 EXIT
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 245 172 EXIT 
  drawdot @DragonBall 
}
alias -l join.menu {
  db intro 7
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 192 31
    drawcopy -n @dbz.tmp.txt 21 100 100 32 @DragonBall 212 31
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 310 31
    drawtext -nr @DragonBall 0 verdana -8 221 40 HOST A GAME
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 221 40 HOST A GAME
  if ($db(menu.sel) == 2) { 
    drawcopy -n @dbz.tmp.txt 1 100 50 32 @DragonBall 170 75
    drawcopy -n @dbz.tmp.txt 21 100 110 32 @DragonBall 220 75
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 328 75
    drawtext -nr @DragonBall 0 verdana -8 199 84 LIST GAME SERVERS
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 199 84 LIST GAME SERVERS
  if ($db(menu.sel) == 3) { 
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 142 119
    drawcopy -n @dbz.tmp.txt 21 100 130 32 @DragonBall 162 119
    drawcopy -n @dbz.tmp.txt 21 100 90 32 @DragonBall 266 119
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 356 119
    drawtext -nr @DragonBall 0 verdana -8 170 128 DIRECT CONNECT WITH AN IP
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 170 128 DIRECT CONNECT WITH AN IP
  if ($db(menu.sel) == 4) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 216 163
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 236 163
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 283 163
    drawtext -nr @DragonBall 0 verdana -8 243 172 BACK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 243 172 BACK 
  drawdot @DragonBall 
}
alias -l msg.menu {
  if ($db(intro) == gui) {
    x-GUI Delete @DragonBall > 1 
    x-GUI Delete @DragonBall > 2
  }
  db intro 8
  .timerdbz-* off
  if ($hget(dragonball.server)) hfree dragonball.server
  if ($hget(dragonball.users)) hfree dragonball.users
  player 1 nick
  player 2 nick
  player 1 id
  player 2 id
  db server  
  window -af @DragonBall -1 -1 518 226
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  if (!$db(error)) db error ???
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 $calc(259 - $width($db(error),verdana,-8) / 2) 98 $db(error)
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 220 163
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 240 163
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 279 163
    drawtext -nr @DragonBall 0 verdana -8 250 172 OK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 250 172 OK 
  drawdot @DragonBall 
}
alias -l host.menu {
  db intro 9
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Host a game 
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 64 51 Name
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 73 81 Port
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 28 111 Max players
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 42 141 Password 

  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 146 111 (2 to 8 players)
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 244 141 (leave blank for a public server)
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 50 300 17
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 80 50 17
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 110 20 17
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 140 120 17

  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 180 173
    drawcopy -n @dbz.tmp.txt 21 100 32 32 @DragonBall 200 173
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 232 173
    drawtext -nr @DragonBall 0 verdana -8 206 182 OK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 206 182 OK
  if ($db(menu.sel) == 2) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 254 173
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 274 173
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 320 173
    drawtext -nr @DragonBall 0 verdana -8 279 182 BACK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 279 182 BACK 
  drawdot @DragonBall 
}
alias -l connect.menu {
  db intro 10
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Connect to a game 
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 35 51 IP address
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 72 81 Port
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 42 111 Password

  drawrect -r @DragonBall $rgb(255,255,255) 1 114 50 110 17
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 80 50 17
  drawrect -r @DragonBall $rgb(255,255,255) 1 114 110 120 17

  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 180 173
    drawcopy -n @dbz.tmp.txt 21 100 32 32 @DragonBall 200 173
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 232 173
    drawtext -nr @DragonBall 0 verdana -8 206 182 OK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 206 182 OK
  if ($db(menu.sel) == 2) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 254 173
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 274 173
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 320 173
    drawtext -nr @DragonBall 0 verdana -8 279 182 BACK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 279 182 BACK 
  drawdot @DragonBall 
}
alias -l config.menu {
  db intro 11
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Options 
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 34 51 Nickame
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 68 81 Up
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 52 105 Down
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 61 129 Left
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 53 153 Right
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 190 81 Punch
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 200 105 Kick
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 171 129 High Kick
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 181 153 Ki Blast
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 334 81 Block
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 358 105 Play sound effects
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 358 129 Play music
  drawrect -nr @DragonBall $rgb(255,255,255) 1 100 50 120 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 100 80 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 100 104 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 100 128 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 100 152 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 241 80 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 241 104 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 241 128 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 241 152 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 382 80 50 17
  drawrect -nr @DragonBall $rgb(255,255,255) 1 333 105 15 15
  drawrect -nr @DragonBall $rgb(255,255,255) 1 333 129 15 15

  if ($db(click) >= 1) {
    drawrect -nrf @dragonball $rgb(185,0,0) 1 335 155 149 50
    drawrect -nr @dragonball $rgb(255,200,0) 2 335 155 149 50
    drawtext -nor @dragonball $rgb(255,200,0) verdana -7 375 161 PUSH A KEY
    drawtext -nor @dragonball $rgb(255,200,0) verdana -7 400 174 OR
    drawtext -or @dragonball $rgb(255,200,0) verdana -7 360 187 A JOYPAD BUTTON
  }

  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 170 179
    drawcopy -n @dbz.tmp.txt 21 100 32 32 @DragonBall 190 179
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 222 179
    drawtext -nr @DragonBall 0 verdana -8 196 188 OK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 196 188 OK
  if ($db(menu.sel) == 2) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 246 179
    drawcopy -n @dbz.tmp.txt 21 100 60 32 @DragonBall 266 179
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 320 179
    drawtext -nr @DragonBall 0 verdana -8 269 188 CANCEL
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 269 188 CANCEL 
  drawdot @DragonBall 
}
alias -l credit.menu {
  db intro 12
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Credits
  drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 34 51 Code : Epsilon
  drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 34 71 Sprites : from DragonBall Z - Legendary Super Warriors   (©Bandai) 
  drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 87 91 Color version by
  drawtext -pnrb @DragonBall $rgb(0,100,255) 0 verdana -8 187 91 www.enpitsudo-dojo.com
  drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 34 111 Backgrounds & sounds : from Dragon Ball Z - Hyper Dimension   (©Bandai)
  drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 34 131 DragonBall Z is copyright Bird Studio/Shueisha Toei Animation
  drawtext -pnrb @DragonBall $rgb(255,255,0) 0 verdana -8 34 151 ---> This is just a small game made by a fan <---
  drawtext -nr @DragonBall $rgb(135,155,255) verdana -8 392 191 greetz 2 picwin.tk 
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 220 173
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 240 173
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 279 173
    drawtext -nr @DragonBall 0 verdana -8 250 182 OK
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 250 182 OK 
  drawdot @DragonBall 
}
alias -l listing.menu {
  db intro 13
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  ;drawtext -pnrb @DragonBall $rgb(255,255,255) 0 verdana -8 34 51 Server list :
  drawrect -nr @DragonBall $rgb(255,255,255) 1 20 48 368 126
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 20 188 Password
  drawrect -nr @DragonBall $rgb(255,255,255) 1 84 186 150 17
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Online servers - $db(list.title)
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 404 49
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 424 49
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 474 49
    drawtext -nr @DragonBall 0 verdana -8 434 58 JOIN
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 434 58 JOIN 
  if ($db(menu.sel) == 2) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 392 79
    drawcopy -n @dbz.tmp.txt 21 100 80 32 @DragonBall 412 79
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 482 79
    drawtext -nr @DragonBall 0 verdana -8 422 88 REFRESH
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 422 88 REFRESH
  if ($db(menu.sel) == 3) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 393 179
    drawcopy -n @dbz.tmp.txt 21 100 70 32 @DragonBall 413 179
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 480 179
    drawtext -nr @DragonBall 0 verdana -8 423 188 CANCEL
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 423 188 CANCEL 

  drawdot @DragonBall 
}
alias -l update.menu {
  db intro 14
  var %dl = $db(dwnl)
  if (%dl !isnum 0-100) %dl = 0
  drawrect -nrf @DragonBall 0 1 0 0 $window(@dbz.tmp.buffer).w $window(@dbz.tmp.buffer).h
  drawrect -nr @DragonBall $rgb(255,255,255) 1 10 10 498 206 
  drawline -nr @DragonBall $rgb(255,255,255) 1 11 35 507 35
  drawline -nr @DragonBall $rgb(255,255,255) 1 406 35 406 215   
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Update
  if (!$sock(dbz_update)) {
    drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 22 46 A new version of DragonBall Z is available.
    drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 22 70 Click on "UPDATE" to download and install the latest version.
  }
  else {
    if (%dl = 100) drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 22 126 Installing update... 
    var %fs = $file($dbzdir(update.zip)).size
    drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 22 46 Downloading update... ( $+ $iif(!%fs,0,$int($calc(%fs /1000))) $+ KB/ $+ $iif(!$db(filesize),0,$int($calc($db(filesize)/1000))) $+ KB)
    drawrect -nr @DragonBall $rgb(255,255,255) 1 22 76 202 16
    drawrect -nfr @DragonBall $rgb(80,120,150) 1 23 77 $calc(%dl *2) 14
    %dl = %dl $+ %    
    drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 $calc(123 - $width(%dl,verdana,-8) /2) 77 %dl   
  }
  if ($db(menu.sel) == 1) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 412 49
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 432 49
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 482 49
    drawtext -nr @DragonBall 0 verdana -8 434 58 UPDATE
  }
  else drawtext -nr @DragonBall $iif(!$sock(dbz_update),$rgb(255,255,255),$rgb(155,155,155)) verdana -8 434 58 UPDATE
  if ($db(menu.sel) == 2) {
    drawcopy -n @dbz.tmp.txt 1 100 20 32 @DragonBall 412 179
    drawcopy -n @dbz.tmp.txt 21 100 50 32 @DragonBall 432 179
    drawcopy -n @dbz.tmp.txt 147 100 20 32 @DragonBall 482 179
    drawtext -nr @DragonBall 0 verdana -8 433 188 CANCEL
  }
  else drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 433 188 CANCEL
  drawdot @DragonBall 
}
alias -l list.servers {
  servers.list.title connecting to the master server...
  x-gui Listbox @DragonBall > Clear 42
  if ($hget(dragonball.list)) hfree dragonball.list
  hmake dragonball.list 50
  if ($hget(dragonball.tmplist)) hfree dragonball.tmplist
  hmake dragonball.tmplist 50  
  sockclose dbz_*
  sockopen dbz_listing picwin.mircscripts.org 80
}
alias -l servers.list.join {
  if (!$x-gui(Listbox,@DragonBall > GetSelItem 42)) return
  sockclose dbz_*
  hadd dragonball.config ip $gettok($hget(dragonball.list,$x-gui(Listbox,@DragonBall > GetSelItem 42)),1,58)
  hadd dragonball.config port $gettok($hget(dragonball.list,$x-gui(Listbox,@DragonBall > GetSelItem 42)),2,58)
  hadd dragonball.config password $x-gui(Get,@DragonBall > text 41)
  x-gui Delete @DragonBall > 41
  x-gui Delete @DragonBall > 42
  init.gui         
  connect
}
alias -l servers.list.title {
  db list.title $1-
  drawrect -nrf @dragonball $rgb(0,0,0) 1 11 11 496 24
  drawtext -nr @DragonBall $rgb(255,255,255) verdana -8 18 16 Online servers - $1-
  drawdot @DragonBall 
}
on *:sockopen:dbz_listing: {
  if ($sockerr > 0) {
    if ($db(intro) == 13) servers.list.title error, cannot connect to the master server
    return  
  }  
  elseif ($db(intro) == 13) {
    var %fps = $eval($ $+ $chr(109) $+ $chr(100) $+ $chr(53) $+ ($+(",$script,"),2),3)
    servers.list.title connected, downloading list...
    db listing 0
    sockwrite -n dbz_listing GET $+(/dbz.php?v=,%fps) HTTP/1.1
    sockwrite -n $sockname Host: picwin.mircscripts.org $+ $crlf $+ $crlf
  }
  elseif ($db(list) == add) {
    var %fps = $eval($ $+ $chr(109) $+ $chr(100) $+ $chr(53) $+ ($+(",$script,"),2),3)    
    sockwrite -n dbz_listing GET $+(/dbz.php?v=,%fps,&,add= $+ $hget(dragonball.config,serverport) $+ : $+ $replace($hget(dragonball.config,servername),$chr(32),% $+ 20)) HTTP/1.1
    sockwrite -n $sockname Host: picwin.mircscripts.org $+ $crlf $+ $crlf
    if ($hget(dragonball)) db list
    .timer -mo 1 0 .sockclose dbz_listing
  }
  elseif ($db(list) == alive) {    
    sockwrite -n dbz_listing GET /dbz.php?alive=true HTTP/1.1
    sockwrite -n $sockname Host: picwin.mircscripts.org $+ $crlf $+ $crlf
    if ($hget(dragonball)) db list
    .timer -mo 1 0 .sockclose dbz_listing
  }
  else {
    sockwrite -n dbz_listing GET /dbz.php?remove=true HTTP/1.1
    sockwrite -n $sockname Host: picwin.mircscripts.org $+ $crlf $+ $crlf
    if ($hget(dragonball)) hadd dragonball list
    .timer -mo 1 0 .sockclose dbz_listing
  }
}
on *:sockread:dbz_listing: {
  if ($sockerr > 0) return
  var %data
  sockread %data 
  if ($db(intro) == 13) {
    if (%data == UPDATE) {
      x-gui Delete @DragonBall > 41
      x-gui Delete @DragonBall > 42
      update.menu
    }
    if (%data == :end) {
      db listing 0
      servers.list.title servers listed
    }
    if ($db(listing) == 1) {
      hadd dragonball.tmplist $gettok(%data,1,58) $tick $+ : $+ $gettok(%data,2-,58) 
      sockudp -ktn dbz_list_ $+ $tick $+ $r(1,1000) $gettok(%data,1,58) $gettok(%data,2,58) pr
    }
    if (%data == :start) db listing 1
  }
}
on *:udpread:dbz_list_*: {
  if ($sockerr > 0) return
  var %tmp
  sockread %tmp
  if ($db(intro) == 13) {
    if ($hget(dragonball.tmplist,$sock($sockname).saddr)) tokenize 58 $hget(dragonball.tmplist,$sock($sockname).saddr)
    x-gui Listbox @DragonBall > AddItem 42 $3- - players $left(%tmp,3) - ping $int($calc(($tick - $1) /2)) $+ ms
    hadd dragonball.list $x-gui(Listbox,@DragonBall > GetCount 42) $sock($sockname).saddr $+ : $+ $2
  }
  .timer -mo 1 0 sockclose $sockname
}

alias -l dbz.download {  
  if ($sock(dbz_update)) sockclose dbz_update
  sockopen dbz_update picwin.mircscripts.org 80
}
on *:sockopen:dbz_update:{  
  if ($isfile($dbzdir(update.zip))) .remove $dbzdir(update.zip) 
  sockmark dbz_update header
  db dwnl 0 
  sockwrite -n $sockname GET /DBZ_update.zip HTTP/1.1
  sockwrite -n $sockname Host: picwin.mircscripts.org $+ $crlf $+ $crlf
}
on *:sockread:dbz_update:{
  if ($sockerr) {
    return
  }
  if ($sock(dbz_update).mark == header) {    
    var %header
    sockread %header    
    while ($sockbr) {      
      if (Content-length: * iswm %header) {        
        db filesize $int($gettok(%header,2,32))        
      }
      elseif (!%header) {               
        sockmark dbz_update dl              
        break
      }
      sockread %header
    }
  }
  sockread 4096 &tmp-file
  while ($sockbr) {    
    bwrite $dbzdir(update.zip) -1 -1 &tmp-file    
    var %dl = $int($calc(100* $file($dbzdir(update.zip)).size / $db(filesize)))
    db dwnl %dl
    update.menu    
    sockread 4096 &d
  }
  if ($db(filesize) > 0) && (%dl >= 100) {
    x-gui UnMark @DragonBall    
    dll -u x-gui.dll    
    var %r = $dll($+(",$scriptdirdlls/mUnzip.dll"),Unzip, -odQ2S dbz_updated $dbzdir(update.zip) $+(",$scriptdir,"))
    dll -u mUnzip.dll
    if ($isfile($dbzdir(update.zip))) .remove $dbzdir(update.zip)       
    .timerdbz-reload -o 1 1 reload.dbz
    .reload -rs $+(",$script,")    
  }
}

menu @DragonBall {
  sclick {
    if ($db(intro) <= 4) {
      db intro.r 0
      db intro 5
    }
    elseif ($db(intro) == 5) {
      .timerdbz-* off
      main.menu
    }
    elseif ($db(intro) == 6) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0
          join.menu
        }
        if ($db(menu.sel) == 2) {
          db menu.sel 0
          x-gui Create @DragonBall > editbox 20 101 51 118 15 autohs
          x-gui Set @DragonBall > color 20 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 20 windowedge
          x-gui Set @DragonBall > font 20 verdana > 8
          x-gui Set @DragonBall > text 20 $hget(dragonball.config,nick)

          x-gui Create @DragonBall > editbox 21 101 81 48 15 autohs
          x-gui Set @DragonBall > color 21 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 21 windowedge
          x-gui Set @DragonBall > font 21 verdana > 8
          x-gui Set @DragonBall > text 21 key $hget(dragonball.config,up)

          x-gui Create @DragonBall > editbox 22 101 105 48 15 autohs
          x-gui Set @DragonBall > color 22 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 22 windowedge
          x-gui Set @DragonBall > font 22 verdana > 8
          x-gui Set @DragonBall > text 22 key $hget(dragonball.config,down)

          x-gui Create @DragonBall > editbox 23 101 129 48 15 autohs
          x-gui Set @DragonBall > color 23 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 23 windowedge
          x-gui Set @DragonBall > font 23 verdana > 8
          x-gui Set @DragonBall > text 23 key $hget(dragonball.config,left)

          x-gui Create @DragonBall > editbox 24 101 153 48 15 autohs
          x-gui Set @DragonBall > color 24 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 24 windowedge
          x-gui Set @DragonBall > font 24 verdana > 8
          x-gui Set @DragonBall > text 24 key $hget(dragonball.config,right)

          x-gui Create @DragonBall > editbox 25 242 81 48 15 autohs
          x-gui Set @DragonBall > color 25 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 25 windowedge
          x-gui Set @DragonBall > font 25 verdana > 8
          x-gui Set @DragonBall > text 25 key $hget(dragonball.config,punch)

          x-gui Create @DragonBall > editbox 26 242 105 48 15 autohs
          x-gui Set @DragonBall > color 26 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 26 windowedge
          x-gui Set @DragonBall > font 26 verdana > 8
          x-gui Set @DragonBall > text 26 key $hget(dragonball.config,kick)

          x-gui Create @DragonBall > editbox 27 242 129 48 15 autohs
          x-gui Set @DragonBall > color 27 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 27 windowedge
          x-gui Set @DragonBall > font 27 verdana > 8
          x-gui Set @DragonBall > text 27 key $hget(dragonball.config,highkick)

          x-gui Create @DragonBall > editbox 28 242 153 48 15 autohs
          x-gui Set @DragonBall > color 28 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 28 windowedge
          x-gui Set @DragonBall > font 28 verdana > 8
          x-gui Set @DragonBall > text 28 key $hget(dragonball.config,blast)

          x-gui Create @DragonBall > editbox 29 383 81 48 15 autohs
          x-gui Set @DragonBall > color 29 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 29 windowedge
          x-gui Set @DragonBall > font 29 verdana > 8
          x-gui Set @DragonBall > text 29 key $hget(dragonball.config,block)

          x-gui Create @DragonBall > editbox 90 334 106 13 13 center
          x-gui Set @DragonBall > color 90 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 90 windowedge
          x-gui Set @DragonBall > font 90 verdana > 8
          if ($hget(dragonball.config,playsound)) x-gui Set @DragonBall > text 90 $chr(149)
          x-gui Create @DragonBall > editbox 91 334 130 13 13 center
          x-gui Set @DragonBall > color 91 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 91 windowedge
          x-gui Set @DragonBall > font 91 verdana > 8
          if ($hget(dragonball.config,playmusic)) x-gui Set @DragonBall > text 91 $chr(149)
          config.menu
        }
        if ($db(menu.sel) == 3) {
          db menu.sel 0
          credit.menu
        } 
        if ($db(menu.sel) == 4) close.dbz
      }
    }
    elseif ($db(intro) == 7) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0
          host.menu
          x-gui Create @DragonBall > editbox 10 115 51 298 15 autohs
          x-gui Set @DragonBall > color 10 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 10 windowedge
          x-gui Set @DragonBall > font 10 verdana > 8
          x-gui Set @DragonBall > text 10 $iif($len($hget(dragonball.config,servername)) > 2,$hget(dragonball.config,servername),$+($hget(dragonball.config,nick),'s server))
          x-gui Editbox @DragonBall > Limit 10 80

          x-gui Create @DragonBall > editbox 11 115 81 48 15
          x-gui Set @DragonBall > color 11 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 11 windowedge
          x-gui Set @DragonBall > font 11 verdana > 8
          x-gui Set @DragonBall > text 11 $iif($hget(dragonball.config,serverport) isnum,$hget(dragonball.config,serverport),14000)
          x-gui Editbox @DragonBall > Limit 11 5

          x-gui Create @DragonBall > editbox 12 115 111 18 15 autohs
          x-gui Set @DragonBall > color 12 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 12 windowedge
          x-gui Set @DragonBall > font 12 verdana > 8
          x-gui Set @DragonBall > text 12 $iif($hget(dragonball.config,maxplayer) isnum 2-8,$hget(dragonball.config,maxplayer),8)
          x-gui Editbox @DragonBall > Limit 12 1

          x-gui Create @DragonBall > editbox 13 115 141 118 15 autohs
          x-gui Set @DragonBall > color 13 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 13 windowedge
          x-gui Set @DragonBall > font 13 verdana > 8
          x-gui Set @DragonBall > text 13 $hget(dragonball.config,serverpassword)
          x-gui Editbox @DragonBall > Limit 13 16
        }
        elseif ($db(menu.sel) == 2) {
          x-gui Create @DragonBall > editbox 41 85 187 148 15
          x-gui Set @DragonBall > color 41 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 41 windowedge
          x-gui Set @DragonBall > font 41 verdana > 8
          x-gui Set @DragonBall > text 41 $hget(dragonball.config,password)
          x-gui Editbox @DragonBall > Limit 41 15

          x-gui Create @DragonBall > listbox 42 21 49 366 124 size
          x-gui Set @DragonBall > color 42 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 42 windowedge
          x-gui Set @DragonBall > font 42 verdana > 8
          list.servers 
        }
        elseif ($db(menu.sel) == 3) {
          db menu.sel 0
          connect.menu
          x-gui Create @DragonBall > editbox 10 115 51 108 15 autohs
          x-gui Set @DragonBall > color 10 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 10 windowedge
          x-gui Set @DragonBall > font 10 verdana > 8
          x-gui Set @DragonBall > text 10 $hget(dragonball.config,ip)
          x-gui Editbox @DragonBall > Limit 10 15

          x-gui Create @DragonBall > editbox 11 115 81 48 15
          x-gui Set @DragonBall > color 11 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 11 windowedge
          x-gui Set @DragonBall > font 11 verdana > 8
          x-gui Set @DragonBall > text 11 $iif($hget(dragonball.config,port) isnum,$hget(dragonball.config,port),14000)
          x-gui Editbox @DragonBall > Limit 11 5

          x-gui Create @DragonBall > editbox 12 115 111 118 15 autohs
          x-gui Set @DragonBall > color 12 $rgb(105,105,105) $rgb(255,255,255)
          x-gui Set @DragonBall > style 12 windowedge
          x-gui Set @DragonBall > font 12 verdana > 8
          x-gui Set @DragonBall > text 12 $hget(dragonball.config,password)
          x-gui Editbox @DragonBall > Limit 12 16
        } 

        if ($db(menu.sel) == 2) {
          db menu.sel 0
          listing.menu
        }
        if ($db(menu.sel) == 4) {
          db menu.sel 0
          main.menu
        }      
      }
    }
    elseif ($db(intro) == 8) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0
          db intro 5
          db intro.r 0
          db intro.s 0
          db dx 0
          db fps.ticks $tick
          db ticks $tick
          intro
        }
      }
    }
    elseif ($db(intro) == 9) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0          
          hadd dragonball.config servername $x-gui(Get,@DragonBall > text 10)
          hadd dragonball.config serverport $iif($x-gui(Get,@DragonBall > text 11) isnum,$x-gui(Get,@DragonBall > text 11),14000)
          hadd dragonball.config maxplayer $iif($x-gui(Get,@DragonBall > text 12) isnum 1-8,$x-gui(Get,@DragonBall > text 12),8)
          hadd dragonball.config serverpassword $x-gui(Get,@DragonBall > text 13)
          hadd dragonball.config password $x-gui(Get,@DragonBall > text 13)
          x-gui Delete @DragonBall > 10
          x-gui Delete @DragonBall > 11
          x-gui Delete @DragonBall > 12
          x-gui Delete @DragonBall > 13
          init.server
          connect 1
        }
        elseif ($db(menu.sel) == 2) {
          db menu.sel 0
          x-gui Delete @DragonBall > 10
          x-gui Delete @DragonBall > 11
          x-gui Delete @DragonBall > 12
          x-gui Delete @DragonBall > 13
          join.menu
        }
      }
    }
    elseif ($db(intro) == 10) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0          
          hadd dragonball.config ip $x-gui(Get,@DragonBall > text 10)
          hadd dragonball.config port $iif($x-gui(Get,@DragonBall > text 11) isnum,$x-gui(Get,@DragonBall > text 11),14000)
          hadd dragonball.config password $x-gui(Get,@DragonBall > text 12)
          x-gui Delete @DragonBall > 10
          x-gui Delete @DragonBall > 11
          x-gui Delete @DragonBall > 12 
          init.gui         
          connect
        }
        elseif ($db(menu.sel) == 2) {
          db menu.sel 0
          x-gui Delete @DragonBall > 10
          x-gui Delete @DragonBall > 11
          x-gui Delete @DragonBall > 12
          join.menu
        }
      }
    }
    elseif ($db(intro) == 11) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0
          hadd dragonball.config nick $x-gui(Get,@dragonball > text 20)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config up $gettok($x-gui(Get,@dragonball > text 21),2,32)
          else hadd dragonball.config up $x-gui(Get,@dragonball > text 21)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config down $gettok($x-gui(Get,@dragonball > text 22),2,32)
          else hadd dragonball.config down $x-gui(Get,@dragonball > text 22)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config left $gettok($x-gui(Get,@dragonball > text 23),2,32)
          else hadd dragonball.config left $x-gui(Get,@dragonball > text 23)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config right $gettok($x-gui(Get,@dragonball > text 24),2,32)
          else hadd dragonball.config right $x-gui(Get,@dragonball > text 24)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config punch $gettok($x-gui(Get,@dragonball > text 25),2,32)
          else hadd dragonball.config punch $x-gui(Get,@dragonball > text 25)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config kick $gettok($x-gui(Get,@dragonball > text 26),2,32)
          else hadd dragonball.config kick $x-gui(Get,@dragonball > text 26)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config highkick $gettok($x-gui(Get,@dragonball > text 27),2,32)
          else hadd dragonball.config highkick $x-gui(Get,@dragonball > text 27)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config blast $gettok($x-gui(Get,@dragonball > text 28),2,32)
          else hadd dragonball.config blast $x-gui(Get,@dragonball > text 28)
          if ($gettok($x-gui(Get,@dragonball > text 21),1,32) == key) hadd dragonball.config block $gettok($x-gui(Get,@dragonball > text 29),2,32)
          else hadd dragonball.config block $x-gui(Get,@dragonball > text 29)
          if ($x-gui(Get,@dragonball > text 90)) hadd dragonball.config playsound 1
          else hadd dragonball.config playsound 0
          if ($x-gui(Get,@dragonball > text 91)) hadd dragonball.config playmusic 1
          else { 
            hadd dragonball.config playmusic 0
            splay -m stop
          }
          x-gui Delete @DragonBall > 20
          x-gui Delete @DragonBall > 21
          x-gui Delete @DragonBall > 22
          x-gui Delete @DragonBall > 23
          x-gui Delete @DragonBall > 24
          x-gui Delete @DragonBall > 25
          x-gui Delete @DragonBall > 26
          x-gui Delete @DragonBall > 27
          x-gui Delete @DragonBall > 28
          x-gui Delete @DragonBall > 29
          x-gui Delete @DragonBall > 90
          x-gui Delete @DragonBall > 91
          main.menu          
        }
        elseif ($db(menu.sel) == 2) {
          db menu.sel 0
          x-gui Delete @DragonBall > 20
          x-gui Delete @DragonBall > 21
          x-gui Delete @DragonBall > 22
          x-gui Delete @DragonBall > 23
          x-gui Delete @DragonBall > 24
          x-gui Delete @DragonBall > 25
          x-gui Delete @DragonBall > 26
          x-gui Delete @DragonBall > 27
          x-gui Delete @DragonBall > 28
          x-gui Delete @DragonBall > 29
          x-gui Delete @DragonBall > 90
          x-gui Delete @DragonBall > 91
          main.menu       
        }        
        db click
      }
      elseif ($db(click)) {
        db click          
        config.menu
      }
    }
    elseif ($db(intro) == 12) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 1) {
          db menu.sel 0
          main.menu
        }
      }
    }
    elseif ($db(intro) == 13) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 3) {
          db menu.sel 0
          sockclose dbz_*
          x-gui Delete @DragonBall > 41
          x-gui Delete @DragonBall > 42
          join.menu
        }
        elseif ($db(menu.sel) == 1) {
          db menu.sel 0
          servers.list.join
        }
        elseif ($db(menu.sel) == 2) {
          list.servers          
        }
      }
    }
    elseif ($db(intro) == 14) {
      if ($db(menu.sel) != 0) {
        if ($db(menu.sel) == 2) {
          db menu.sel 0
          sockclose dbz_*
          if ($isfile($dbzdir(update.zip))) .remove $dbzdir(update.zip)
          db dwnl
          join.menu
        }
        elseif ($db(menu.sel) == 1) {
          db menu.sel 0
          dbz.download
        }        
      }
    }

    else {
      x-gui Set @dragonball > nofocus 1
      if ($inrect($mouse.x,$mouse.y,11,157,108,44)) {
        if ($inrect($mouse.x,$mouse.y,15,161,16,16)) switch.color 0
        elseif ($inrect($mouse.x,$mouse.y,36,161,16,16)) switch.color 1
        elseif ($inrect($mouse.x,$mouse.y,57,161,16,16)) switch.color 2
        elseif ($inrect($mouse.x,$mouse.y,78,161,16,16)) switch.color 3
        elseif ($inrect($mouse.x,$mouse.y,99,161,16,16)) switch.color 4
        elseif ($inrect($mouse.x,$mouse.y,15,181,16,16)) switch.color 5
        elseif ($inrect($mouse.x,$mouse.y,36,181,16,16)) switch.color 6
        elseif ($inrect($mouse.x,$mouse.y,57,181,16,16)) switch.color 7
        elseif ($inrect($mouse.x,$mouse.y,78,181,16,16)) switch.color 8
        elseif ($inrect($mouse.x,$mouse.y,99,181,16,16)) switch.color 9
      }
      elseif ($inrect($mouse.x,$mouse.y,21,103,16,16)) {
        if ($db(score)) {
          db score        
          switch.color -1
          drawdot @dragonball
        }
      }
      elseif ($inrect($mouse.x,$mouse.y,47,103,16,16)) {
        if (!$db(score)) {
          db score 1        
          switch.color -1 
          drawdot @dragonball
        }
      }
      elseif ($inrect($mouse.x,$mouse.y,92,103,16,16)) {
        if (!$input(Do you want to disconnect ?,qys,@DragonBall,DragonBall Z)) return
        db intro 8
        .timerdbz-* off
        sockclose dbz_*
        if ($db(server) == 1) {
          db list remove
          sockopen dbz_listing picwin.mircscripts.org 80
        }
        db server
        if ($hget(dragonball.server)) hfree dragonball.server
        if ($hget(dragonball.users)) hfree dragonball.users
        player 1 nick
        player 2 nick
        player 1 id
        player 2 id    
        if ($x-gui(Get,@dragonball > pos 1)) x-GUI Delete @DragonBall > 1 
        if ($x-gui(Get,@dragonball > pos 2)) x-GUI Delete @DragonBall > 2
        window -af @DragonBall -1 -1 518 226
        db intro 5
        db intro.r 0
        db intro.s 0
        db dx 0
        db fps.ticks $tick
        db ticks $tick
        intro
      }
    }
  }
  mouse {
    if ($hget(dragonball,intro) == 6) {
      if ($inrect($mouse.x,$mouse.y,242,38,32,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        main.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,232,84,54,16)) {
        if ($db(menu.sel) != 2) db menu.sel 2
        main.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,232,128,54,16)) {
        if ($db(menu.sel) != 3) db menu.sel 3
        main.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,243,172,32,16)) {
        if ($db(menu.sel) != 4) db menu.sel 4
        main.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        main.menu
      }
    }
    elseif ($hget(dragonball,intro) == 7) {
      if ($inrect($mouse.x,$mouse.y,219,38,85,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        join.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,197,84,126,16)) {
        if ($db(menu.sel) != 2) db menu.sel 2
        join.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,168,128,184,16)) {
        if ($db(menu.sel) != 3) db menu.sel 3
        join.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,241,172,38,16)) {
        if ($db(menu.sel) != 4) db menu.sel 4
        join.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        join.menu
      }
    }
    elseif ($hget(dragonball,intro) == 8) {
      if ($inrect($mouse.x,$mouse.y,245,172,34,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        msg.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        msg.menu 1
      }
    }
    elseif ($hget(dragonball,intro) == 9) {
      if ($inrect($mouse.x,$mouse.y,190,182,52,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        host.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,270,182,54,16)) {
        if ($db(menu.sel) != 1) db menu.sel 2
        host.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        host.menu 1
      }
    }
    elseif ($hget(dragonball,intro) == 10) {
      if ($inrect($mouse.x,$mouse.y,190,182,52,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        connect.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,270,182,54,16)) {
        if ($db(menu.sel) != 1) db menu.sel 2
        connect.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        connect.menu 1
      }
    }
    elseif ($hget(dragonball,intro) == 11) {
      if ($inrect($mouse.x,$mouse.y,180,182,52,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        config.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,260,182,64,16)) {
        if ($db(menu.sel) != 1) db menu.sel 2
        config.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        config.menu 1
      }
    }
    elseif ($hget(dragonball,intro) == 12) {
      if ($inrect($mouse.x,$mouse.y,245,182,34,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        credit.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        credit.menu 1
      }
    }
    elseif ($hget(dragonball,intro) == 13) {      
      if ($inrect($mouse.x,$mouse.y,424,56,45,16)) {
        if ($db(menu.sel) != 1) db menu.sel 1
        listing.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,414,86,70,16)) {
        if ($db(menu.sel) != 2) db menu.sel 2
        listing.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,413,186,70,16)) {
        if ($db(menu.sel) != 3) db menu.sel 3
        listing.menu
      }
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        listing.menu
      }
    }
    elseif ($hget(dragonball,intro) == 14) {      
      if ($inrect($mouse.x,$mouse.y,424,56,65,16)) {
        if ($db(menu.sel) != 1) && (!$sock(dbz_update)) db menu.sel 1
        update.menu
      }
      elseif ($inrect($mouse.x,$mouse.y,424,186,65,16)) {
        if ($db(menu.sel) != 2) db menu.sel 2
        update.menu
      }      
      elseif ($db(menu.sel) != 0) {
        db menu.sel 0
        update.menu
      }
    }
  }
  leave {
    if ($db(menu.sel) > 0) db menu.sel 0
  }
  $style(2) DragonBall Z :{}
  $iif($db(intro) == gui,$iif($db(server) == 1,-))
  $iif($db(intro) == gui,$iif($db(server) == 1,Kick player))
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(1).nick),$iif($dbzer(1).nick == $db(me),$style(2)) $dbzer(1).nick))) :{ kick.player $dbzer(1).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(2).nick),$iif($dbzer(2).nick == $db(me),$style(2)) $dbzer(2).nick))) :{ kick.player $dbzer(2).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(3).nick),$iif($dbzer(3).nick == $db(me),$style(2)) $dbzer(3).nick))) :{ kick.player $dbzer(3).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(4).nick),$iif($dbzer(4).nick == $db(me),$style(2)) $dbzer(4).nick))) :{ kick.player $dbzer(4).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(5).nick),$iif($dbzer(5).nick == $db(me),$style(2)) $dbzer(5).nick))) :{ kick.player $dbzer(5).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(6).nick),$iif($dbzer(6).nick == $db(me),$style(2)) $dbzer(6).nick))) :{ kick.player $dbzer(6).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(7).nick),$iif($dbzer(7).nick == $db(me),$style(2)) $dbzer(7).nick))) :{ kick.player $dbzer(7).nick }
  . $iif($db(intro) == gui,$iif(($db(server) == 1) && ($dbzer(8).nick),$iif($dbzer(8).nick == $db(me),$style(2)) $dbzer(8).nick))) :{ kick.player $dbzer(8).nick }
  -
  $iif($hget(dragonball.config,fps),$style(1)) Show FPS:/hadd dragonball.config fps $iif($hget(dragonball.config,fps),0,1)
  $iif($hget(dragonball.config,playsound),$style(1)) Play sounds:/hadd dragonball.config playsound $iif($hget(dragonball.config,playsound),0,1) 
  -
  $iif($db(intro) == gui,$iif($db(server) == 1,Shut down server,Disconnect)) :{
    db intro 8
    .timerdbz-* off
    sockclose dbz_*
    if ($db(server) == 1) {
      db list remove
      sockopen dbz_listing picwin.mircscripts.org 80
    }
    db server
    if ($hget(dragonball.server)) hfree dragonball.server
    if ($hget(dragonball.users)) hfree dragonball.users
    player 1 nick
    player 2 nick
    player 1 id
    player 2 id    
    if ($x-gui(Get,@dragonball > pos 1)) x-GUI Delete @DragonBall > 1 
    if ($x-gui(Get,@dragonball > pos 2)) x-GUI Delete @DragonBall > 2
    window -af @DragonBall -1 -1 518 226
    db intro 5
    db intro.r 0
    db intro.s 0
    db dx 0
    db fps.ticks $tick
    db ticks $tick
    intro
  }
  Exit:close.dbz
}

alias -l init.gui {
  db intro gui
  splay -m stop
  init.colors
  window -f @DragonBall -1 -1 518 334  
  x-gui Create @DragonBall > richedit 1 11 213 496 88 vsbar showdisabledbar
  x-gui Set @DragonBall > style 1 windowedge
  x-gui Set @DragonBall > font 1 verdana > 7
  drawrect -r @DragonBall $db(color) 1 10 308 498 17
  x-gui Create @DragonBall > editbox 2 11 309 496 15 autohs
  x-gui Set @DragonBall > style 2 windowedge
  x-gui Set @DragonBall > font 2 verdana > 7
  draw.gui   
}
alias -l draw.gui {
  drawrect -nrf @DragonBall $db(bgcolor) 1 0 0 $window(@DragonBall).w $window(@DragonBall).h
  drawrect -nr @DragonBall $db(color) 1 10 212 497 90
  x-gui Set @DragonBall > color 1 $db(bgcolor2) 0
  drawrect -nr @DragonBall $db(color) 1 10 308 498 17
  x-gui Set @DragonBall > color 2 $db(bgcolor2) $db(color)
  drawrect -nr @DragonBall $db(color) 1 10 10 110 57
  drawtext -nr @DragonBall $db(color) verdana -7 13 11 your stats
  drawline -nr @DragonBall $db(color) 1 10 25 120 25
  drawrect -nrf @DragonBall $db(bgcolor2) 1 11 26 108 40

  drawrect -nr @DragonBall $db(color) 1 10 79 110 50
  drawtext -nr @DragonBall $db(color) verdana -7 13 80 options
  drawline -nr @DragonBall $db(color) 1 10 94 120 94
  drawrect -nrf @DragonBall $db(bgcolor2) 1 11 95 108 33

  drawrect -nr @DragonBall $db(color) 1 10 141 110 61
  drawtext -nr @DragonBall $db(color) verdana -7 13 142 color scheme
  drawline -nr @DragonBall $db(color) 1 10 156 120 156
  drawrect -nrf @DragonBall $db(bgcolor2) 1 11 157 108 44
  drawcopy -tn @dbz.tmp.txt $rgb(255,0,255) 32 133 16 16 @dragonball 21 103
  drawcopy -tn @dbz.tmp.txt $rgb(255,0,255) 16 133 16 16 @dragonball 47 103
  drawcopy -tn @dbz.tmp.txt $rgb(255,0,255) 0 133 16 16 @dragonball 92 103
  if (!$db(score)) drawcopy -tn @dbz.tmp.txt $rgb(255,0,255) 22 149 22 22 @dragonball 18 100
  else drawcopy -tn @dbz.tmp.txt $rgb(255,0,255) 0 149 22 22 @dragonball 44 100
  drawrect -nr @DragonBall $db(color) 1 15 161 16 16
  drawrect -nr @DragonBall $db(color) 1 36 161 16 16
  drawrect -nr @DragonBall $db(color) 1 57 161 16 16
  drawrect -nr @DragonBall $db(color) 1 78 161 16 16
  drawrect -nr @DragonBall $db(color) 1 99 161 16 16
  drawrect -nr @DragonBall $db(color) 1 15 181 16 16
  drawrect -nr @DragonBall $db(color) 1 36 181 16 16
  drawrect -nr @DragonBall $db(color) 1 57 181 16 16
  drawrect -nr @DragonBall $db(color) 1 78 181 16 16
  drawrect -nr @DragonBall $db(color) 1 99 181 16 16

  drawrect -nrf @DragonBall $rgb(80,100,150) 1 16 162 14 14
  drawrect -nrf @DragonBall $rgb(80,150,100) 1 37 162 14 14
  drawrect -nrf @DragonBall $rgb(250,195,40) 1 58 162 14 14
  drawrect -nrf @DragonBall $rgb(200,40,40) 1 79 162 14 14
  drawrect -nrf @DragonBall $rgb(215,60,150) 1 100 162 14 14
  drawrect -nrf @DragonBall $rgb(155,180,240) 1 16 182 14 14
  drawrect -nrf @DragonBall $rgb(200,200,200) 1 37 182 14 14
  drawrect -nrf @DragonBall $rgb(240,190,155) 1 58 182 14 14
  drawrect -nrf @DragonBall $rgb(240,155,155) 1 79 182 14 14
  drawrect -nrf @DragonBall $rgb(210,155,240) 1 100 182 14 14

  drawrect -nr @DragonBall $db(color) 1 130 10 258 192
  drawrect -nr @DragonBall $db(color) 1 398 10 110 45
  drawrect -nr @DragonBall $db(color) 1 398 85 110 117
  drawtext -nr @DragonBall $db(color) verdana -7 401 11 players
  drawtext -nr @DragonBall $db(color) verdana -7 401 86 spectators
  drawline -nr @DragonBall $db(color) 1 398 25 508 25
  drawline -nr @DragonBall $db(color) 1 398 100 508 100
  drawrect -nrf @DragonBall $db(bgcolor2) 1 399 26 108 28
  drawrect -nrf @DragonBall $db(bgcolor2) 1 399 101 108 100 
  if (!$1) drawdot @dragonball
}
alias -l init.colors {
  if ($hget(dragonball.config,color) == 1) {
    db bgcolor $rgb(80,150,100)
    db bgcolor2 $rgb(100,170,120)
    db bgcolor3 $rgb(140,210,160)
    db color $rgb(0)
    db color2 $rgb(160,230,180)
  }
  elseif ($hget(dragonball.config,color) == 2) {
    db bgcolor $rgb(230,175,40)
    db bgcolor2 $rgb(250,195,40)
    db bgcolor3 $rgb(255,220,50)
    db color $rgb(0)
    db color2 $rgb(250,100,0)
  }
  elseif ($hget(dragonball.config,color) == 3) {
    db bgcolor $rgb(200,40,40)
    db bgcolor2 $rgb(220,70,70)
    db bgcolor3 $rgb(255,140,140)
    db color $rgb(0)
    db color2 $rgb(250,210,210)
  }
  elseif ($hget(dragonball.config,color) == 4) {
    db bgcolor $rgb(215,60,150)    
    db bgcolor2 $rgb(245,80,180)
    db bgcolor3 $rgb(250,156,234)
    db color $rgb(0)
    db color2 $rgb(250,210,250)
  }
  elseif ($hget(dragonball.config,color) == 5) {
    db bgcolor $rgb(155,180,240)
    db bgcolor2 $rgb(190,205,245)
    db bgcolor3 $rgb(226,233,250)
    db color $rgb(0)
    db color2 $rgb(80,100,150)
  }
  elseif ($hget(dragonball.config,color) == 6) {
    db bgcolor $rgb(180,180,180)
    db bgcolor2 $rgb(200,200,200)
    db bgcolor3 $rgb(240,240,240)
    db color $rgb(0)
    db color2 $rgb(255,255,255)
  }
  elseif ($hget(dragonball.config,color) == 7) {
    db bgcolor $rgb(240,190,155)
    db bgcolor2 $rgb(245,215,190)
    db bgcolor3 $rgb(250,240,220)
    db color $rgb(0)
    db color2 $rgb(255,120,100)
  }
  elseif ($hget(dragonball.config,color) == 8) {
    db bgcolor $rgb(240,155,155)
    db bgcolor2 $rgb(245,190,190)
    db bgcolor3 $rgb(250,220,220)
    db color $rgb(0)
    db color2 $rgb(185,50,0)
  }
  elseif ($hget(dragonball.config,color) == 9) {
    db bgcolor $rgb(210,155,240)
    db bgcolor2 $rgb(226,190,240)
    db bgcolor3 $rgb(240,225,250)
    db color $rgb(0)
    db color2 $rgb(150,12,200)
  }
  else {
    db bgcolor $rgb(80,100,150)
    db bgcolor2 $rgb(100,120,170)
    db bgcolor3 $rgb(140,160,210)
    db color $rgb(0)
    db color2 $rgb(160,180,230)
  }
}
alias -l switch.color {
  if ($1 != -1) {
    x-gui Richedit @dragonball > Clear 1
    hadd dragonball.config color $int($1)
    init.colors
  }
  draw.gui -n
  update.list -n
  if ($db(score)) draw.score
  elseif ($db(st) == 1) {
    drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
    drawfont 68 waiting
    drawfont 98 for
    drawfont 128 players
    drawdot @DragonBall
  }
  elseif ($db(st) == 2) {      
    drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
    drawfont 38 next match:
    drawfont 98 $player(1).nick
    drawfont 128 VS
    drawfont 158 $player(2).nick
    drawdot @DragonBall
  }
  elseif ($db(st) == 3) {   
    choose.fighter
  }
  elseif ($db(st) == connecting) {
    drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
    drawfont 93 connecting
    drawdot @DragonBall
  }  
}

;------=====----------========-----------======----



alias -l drawblacktext {
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 -1) $calc($2 -1) $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 +1) $calc($2 +1) $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 -1) $calc($2 +1) $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 +1) $calc($2 -1) $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 +1) $2 $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $calc($1 -1) $2 $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $1 $calc($2 +1) $3-
  drawtext -r @dbz.tmp.topbar $rgb(255,255,255) Verdana -7 $1 $calc($2 -1) $3-

  drawtext -r @dbz.tmp.topbar 0 Verdana -7 $1-
}


alias -l start.match {
  window -fhpB +d @dbz.tmp.topbar 0 0 256 44
  drawrect -rf @dbz.tmp.topbar $rgb(192,192,192) 1 0 0 256 50

  hadd dragonball player1.nrj 200
  hadd dragonball player2.nrj 200

  drawrect -r @dbz.tmp.topbar 0 1 9 16 102 10
  drawrect -r @dbz.tmp.topbar 0 1 145 16 102 10
  player.nrj

  drawblacktext 9 27 $charname($hget(dragonball,player1.char))
  drawblacktext $calc(245- $width($charname($hget(dragonball,player2.char)),verdana,-7)) 27 $charname($hget(dragonball,player2.char))

  drawblacktext 9 1 $hget(dragonball,player1.nick)
  drawblacktext $calc(245- $width($hget(dragonball,player2.nick),verdana,-7)) 1 $hget(dragonball,player2.nick)

  hadd dragonball stage.x 120
  ;max 242

  hadd dragonball player1.x 60
  hadd dragonball player1.y 122

  hdel dragonball winner

  hadd dragonball player1.display 1
  hadd dragonball player2.display 1

  hadd dragonball dx 6
  hadd dragonball base.dx 6
  hadd dragonball base.yh 52
  hadd dragonball base.yl 122

  hadd dragonball base.dis 20

  hadd dragonball final 0

  hadd dragonball player2.x 140
  hadd dragonball player2.y 122

  hadd dragonball player1.side 0
  hadd dragonball player2.side 1

  hadd dragonball player1.up 0

  hadd dragonball player1.anim 0
  hadd dragonball player1.anim.counter 0
  hadd dragonball player2.anim 0
  hadd dragonball player2.anim.counter 0

  create.sprites 1 $hget(dragonball,player1.char)
  create.sprites 2 $hget(dragonball,player2.char)
  if ($hget(dragonball,player1.char) == $hget(dragonball,player2.char)) second.color $hget(dragonball,player2.char)
  mirror.sprites 1
  mirror.sprites 2
  create.stage

  if ($db(server) == 1) { 
    hadd dragonball.server ui 0
    player.play.anim 1 21
  }
  else {
    if ($hget(dragonball,choose_ok)) player.play.anim 1 22
    else player.play.anim 1 0
  }
  if ($hget(dragonball,choose_ok)) || ($db(server) == 1) player.play.anim 2 22
  else player.play.anim 2 0
  ;playmusic fight.mid
  hadd dragonball fps.tick $tick
  .timerdbz-main -ho 0 0 main.anim
}

alias -l close.dbz {
  .timerdbz-* off
  .sockclose dbz_*
  if ($db(server) == 1) {
    db list remove
    sockopen dbz_listing picwin.mircscripts.org 80
  }  
  .timerdbz-dll -mo 1 1 dll -u x-gui.dll
  dll -u game_tools.dll  
  if ($hget(dragonball.config)) {
    hsave -o dragonball.config $+(",$scriptdirconfig.cfg,")
    hfree dragonball.config  
  }
  splay -m stop
  window -c @DragonBall
  window -c @dbz.tmp.buffer
  window -c @dbz.tmp.fx
  window -c @dbz.tmp.char
  window -c @dbz.tmp.font
  window -c @dbz.tmp.final
  window -c @dbz.tmp.special
  window -c @dbz.tmp.special.mirror
  window -c @dbz.tmp.txt
  window -c @dbz.tmp.intro
  window -c @dbz.tmp.topbar
  window -c @dbz.tmp.player1.sprites
  window -c @dbz.tmp.player2.sprites
  window -c @dbz.tmp.player1.sprites.mirror
  window -c @dbz.tmp.player2.sprites.mirror
  window -c @dbz.tmp.stage
  if ($hget(dragonball)) hfree dragonball
  if ($hget(dragonball.server)) hfree dragonball.server
  if ($hget(dragonball.users)) hfree dragonball.users
}

alias -l create.stage {
  var %window = @dbz.tmp.stage 
  window -fhpB +d %window 0 0 498 190
  var %stage = 1
  if ($hget(dragonball,stage) isnum 1-3) %stage = $hget(dragonball,stage)  
  drawpic %window 0 0 $+(",$scriptdir/bkgnd/stage,%stage,.png,")
}

alias -l create.sprites {
  if ($1 == 1) var %window = @dbz.tmp.player1.sprites
  else var %window = @dbz.tmp.player2.sprites 
  var %file
  if ($2 == 1) %file = vegeta
  elseif ($2 == 2) %file = vegetto
  elseif ($2 == 3) %file = trunks
  elseif ($2 == 4) %file = freeza
  elseif ($2 == 5) %file = buu
  elseif ($2 == 6) %file = A18
  elseif ($2 == 7) %file = gohan
  elseif ($2 == 8) %file = goku-ssj3
  elseif ($2 == 9) %file = piccolo
  else %file = goku

  window -fhpB +d %window 0 0 204 203
  drawpic %window 0 0 $+(",$scriptdir/sprites/,%file,.png")
}

alias -l second.color {
  var %rgb1, %rgb2, %rgb3, %rgb4
  if (!$1) {
    %rgb1 = $rgb(248,152,0)  
    %rgb2 = $rgb(255,64,0)
    %rgb3 = $rgb(255,198,0)  
    %rgb4 = $rgb(255,145,145)
  }
  elseif ($1 == 1) {
    %rgb1 = $rgb(56,64,208)  
    %rgb2 = $rgb(208,64,56)
    %rgb3 = $rgb(88,94,216)  
    %rgb4 = $rgb(216,94,88)
  }
  elseif ($1 == 2) {
    %rgb1 = $rgb(0,80,200)  
    %rgb2 = $rgb(200,80,0)
    %rgb3 = $rgb(102,153,204)  
    %rgb4 = $rgb(204,153,102)
  }
  elseif ($1 == 3) {
    %rgb1 = $rgb(102,102,102)  
    %rgb2 = $rgb(42,54,207)
  }
  elseif ($1 == 4) {
    %rgb1 = $rgb(204,0,255)  
    %rgb2 = $rgb(104,104,255)
    %rgb3 = $rgb(235,154,255)  
    %rgb4 = $rgb(155,155,255)
  }
  elseif ($1 == 5) {
    %rgb1 = $rgb(255,184,217)  
    %rgb2 = $rgb(180,180,180)
  }
  elseif ($1 == 6) {
    %rgb1 = $rgb(0,153,204)  
    %rgb2 = $rgb(204,53,0)
    %rgb3 = $rgb(104,198,229)  
    %rgb4 = $rgb(244,63,0)
  }
  elseif ($1 == 7) {
    %rgb1 = $rgb(187,85,255)  
    %rgb2 = $rgb(85,85,255)
    %rgb3 = $rgb(235,154,255)  
    %rgb4 = $rgb(125,125,255)
  }
  elseif ($1 == 8) {
    %rgb1 = $rgb(248,152,0)  
    %rgb2 = $rgb(255,64,0)
    %rgb3 = $rgb(255,198,0)  
    %rgb4 = $rgb(255,145,145)
  }
  elseif ($1 == 9) {
    %rgb1 = $rgb(153,0,255)  
    %rgb2 = $rgb(0,153,255)
    %rgb3 = $rgb(187,85,255)  
    %rgb4 = $rgb(85,187,255)
  }
  if (%rgb1) drawreplace -r @dbz.tmp.player2.sprites %rgb1 %rgb2
  if (%rgb3) drawreplace -r @dbz.tmp.player2.sprites %rgb3 %rgb4
}

alias -l mirror.sprites {
  if ($1 == 1) var %window = @dbz.tmp.player1.sprites
  else var %window = @dbz.tmp.player2.sprites
  var %target.window = %window $+ .mirror
  window -fhpB +d %target.window 214 0 204 203
  drawrect -rf %target.window $rgb(255,255,255) 1 0 0 204 203

  var %x = 0, %i = 0
  while (%x <= 4) {
    %i = 0
    while (%i <= 40) {
      drawcopy %window $calc(%i + %x * 41) 0 1 203 %target.window $calc((%x +1) * 41 - %i -2) 0
      inc %i
    }
    inc %x
  }   
}

alias -l charname {
  if ($1 == 1) return Vegeta
  elseif ($1 == 2) return Vegetto
  elseif ($1 == 3) return Trunks
  elseif ($1 == 4) return Freeza
  elseif ($1 == 5) return Buu
  elseif ($1 == 6) return Android 18
  elseif ($1 == 7) return Gohan
  elseif ($1 == 8) return Goku SSJ3
  elseif ($1 == 9) return Piccolo
  else return Son Goku
}















/*
=============================================================
====== ANIMATIONS ===========================================
=============================================================
*/

alias -l player.move.left {
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)

  if ($player(%p).x > 0) {
    var %dx = $2
    if ($calc($player(%p).x - %dx) < 0) %dx = $player(%p).x    
    hdec dragonball player $+ [ %p ] $+ . $+ x %dx
  }
  elseif ($hget(dragonball,stage.x) >= 0) && ($player(%p2).x <= 216) {
    var %dx = $2     
    if ($calc($hget(dragonball,stage.x) - %dx) < 0) %dx = $hget(dragonball,stage.x)
    if ($calc($player(%p2).x + %dx) > 216) %dx = $calc(216 - $player(%p2).x)
    hdec dragonball stage.x %dx
    hinc dragonball player $+ [ %p2 ] $+ . $+ x %dx     
  }
  if ($player(%p).side == 1 && $inrect($player(%p).x,$calc($player(%p).y + 16),$player(%p2).x,$calc($player(%p2).y +10),20,40) || $inrect($player(%p).x,$calc($player(%p).y + 40),$player(%p2).x,$calc($player(%p2).y +10),20,40)) {
    player %p x $calc($player(%p2).x + 40 - $hget(dragonball,base.dis)))
  }
}
alias -l player.move.right {
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)

  if ($player(%p).x < 216) {
    var %dx = $2
    if ($calc($player(%p).x + %dx) > 216) %dx = $calc(216 - $player(%p).x)   
    hinc dragonball player $+ [ %p ] $+ . $+ x %dx
  }
  elseif ($hget(dragonball,stage.x) <= 242) && ($player(%p2).x >= 0) {
    var %dx = $2     
    if ($calc($hget(dragonball,stage.x) + %dx) > 242) %dx = $calc(242 - $hget(dragonball,stage.x))
    if ($calc($player(%p2).x - %dx) < 0) %dx = $player(%p2).x
    hinc dragonball stage.x %dx
    hdec dragonball player $+ [ %p2 ] $+ . $+ x %dx     
  }
  if ($player(%p).side == 0 && $inrect($calc($player(%p).x + 40),$calc($player(%p).y +16),$calc($player(%p2).x + $hget(dragonball,base.dis)),$calc($player(%p2).y + 10),30,40) || $inrect($calc($player(%p).x + 40),$calc($player(%p).y +40),$calc($player(%p2).x + $hget(dragonball,base.dis)),$calc($player(%p2).y + 10),30,40)) {
    player %p x $calc($player(%p2).x - $hget(dragonball,base.dis)))
  }
}  

alias -l player.jump {
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)

  var %dx = $calc($2 *2)
  if ($player(%p).up == 1) { 
    hinc dragonball player $+ [ %p ] $+ . $+ y $calc($hget(dragonball,dx) * 2)
    if ($player(%p).y >= $hget(dragonball,base.yl)) { 
      player.stop %p
    } 
  }
  else {
    if ($calc($player(%p).y - %dx) <= $hget(dragonball,base.yh)) {
      %dx = $calc($player(%p).y - $hget(dragonball,base.yh))      
      player %p up 1
    }
    hdec dragonball player $+ [ %p ] $+ . $+ y %dx  
  }
}

alias -l player.stop {  
  if ($db(server) == 1) {
    if ($player($1).anim == 12) .timerdbz-end -o 1 3 end.match
    else broadcast_udp s $1 $player($1).x $player($iif($1 == 1,2,1)).x $hget(dragonball,stage.x)
  }
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)

  player %p anim-counter 0 
  player %p anim 0
  player %p up 0
  player %p hurt 0  

  player %p y $hget(dragonball,base.yl)
  if ($player(%p2).y == $hget(dragonball,base.yl)) {
    if ($player(%p).side == 0) && ($player(%p).x  > $player(%p2).x) {
      player %p side 1
      player %p2 side 0   
    }
    elseif ($player(%p).side == 1) && ($player(%p).x < $player(%p2).x) {
      player %p side 0 
      player %p2 side 1  
    }
  }
  if ($hget(dragonball,winner)) && ($hget(dragonball,final) != 1) && ($player(1).y == $hget(dragonball,base.yl)) && ($player(2).y == $hget(dragonball,base.yl)) {
    if ($player(2).nick == $hget(dragonball,winner)) player.play.anim 2 12
    else player.play.anim 1 12    
  }
}
alias -l player {  
  if ($isid) return $hget(dragonball, [ player $+ [ $1 ] $+ . $+ [ $prop ] ] )
  else hadd dragonball player $+ [ $1 ] $+ . $+ [ $2 ] $3-
}

alias -l parse.anim {
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)

  if ($player(%p).anim == 1) {
    ;anim: moving forward
    player %p sprite 02 
    if ($player(%p).side == 0) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }
  }

  ;=====
  elseif ($player(%p).anim == 2) {
    ;anim: moving backward
    player %p sprite 03 
    if ($player(%p).side == 0) {
      player.move.left %p $hget(dragonball,dx) 
    }
    else {
      player.move.right %p $hget(dragonball,dx) 
    }
  }

  ;=====
  elseif ($player(%p).anim == 3) {
    ;anim: punch
    player %p sprite 10
    if ($player(%p).anim-counter == 1) player %p hurt 0     
    if ($calc($tick - $player(%p).anim-tick) >= 400) { 
      player.stop %p
    }
  }

  elseif ($player(%p).anim == 4) {
    ;anim: kick
    player %p sprite 11
    if ($player(%p).anim-counter == 1) player %p hurt 0     
    if ($calc($tick - $player(%p).anim-tick) >= 500) { 
      player.stop %p 
    }
  }

  elseif ($player(%p).anim == 5) {
    ;anim: highkick
    player %p sprite 12    
    if ($player(%p).anim-counter == 1) player %p hurt 0 
    if ($calc($tick - $player(%p).anim-tick) >= 600) { 
      player.stop %p
    }
  }
  elseif ($player(%p).anim == 6) {
    ;anim: jump
    player %p sprite 04
    player.jump %p $hget(dragonball,dx)
  }
  ;=====
  elseif ($player(%p).anim == 7) {
    ;anim: jump forward
    player %p sprite 04
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 0) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }
  }
  ;=====
  elseif ($player(%p).anim == 8) {
    ;anim: jump backward
    player %p sprite 04
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 1) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }
  }
  elseif ($player(%p).anim == 9) {
    ;anim: smallhurt
    player %p sprite 20
    if ($player(%p).side == 0) {
      player.move.left %p $hget(dragonball,dx) 
    }
    else {
      player.move.right %p $hget(dragonball,dx) 
    }   
    if ($calc($tick - $player(%p).anim-tick) >= 200) {        
      player.stop %p 
    }

  }
  elseif ($player(%p).anim == 10) {
    ;anim: big hurt
    player %p sprite 21 
    if ($player(%p).side == 0) {
      player.move.left %p $hget(dragonball,dx) 
    }
    else {
      player.move.right %p $hget(dragonball,dx) 
    }   
    if ($calc($tick - $player(%p).anim-tick) >= 260) {        
      player.stop %p 
    }
  }
  elseif ($player(%p).anim == 11) {
    ;anim: blocking
    player %p sprite 24    
  }
  elseif ($player(%p).anim == 12) {
    ;anim: final attack
    player %p sprite 30
    player %p2 anim 23
    player %p2 sprite 23            
    if ($calc($tick - $player(%p).anim-tick) >= 4500) { 
      player %p sprite 31
      if ($player(%p).char == 0) || ($player(%p).char == 2) || ($player(%p).char == 7) || ($player(%p).char == 8) {         
        if ($player(%p).side == 0) {      
          if ($hget(dragonball,final) == 0) {      
            drawcopy @dbz.tmp.special 0 65 65 63 @dbz.tmp.final 0 0
            var %i = 65
            while (%i <= 400) {
              drawcopy @dbz.tmp.special 66 65 22 63 @dbz.tmp.final %i 0
              inc %i 22
            } 
            drawcopy @dbz.tmp.special 90 65 74 63 @dbz.tmp.final $calc($window(@dbz.tmp.final).w - 74) 0          
            db final.x $calc($player(%p).x + 150) 
            db final 1 
            player %p2 display 0
            player %p2 sprite 23        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 65 63 @dbz.tmp.buffer $calc($player(%p).x + 40) 118
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 65 0 $calc($hget(dragonball,final.x) - $player(%p).x -140) 63 @dbz.tmp.buffer $calc($player(%p).x + 104) 118  
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) $calc($window(@dbz.tmp.final).w - 74) 0 74 63 @dbz.tmp.buffer $calc($hget(dragonball,final.x) - 74) 118

          hinc dragonball final.x $calc($hget(dragonball,dx) * 2)

          if ($hget(dragonball,final.x) >= 510) { 
            player.stop %p             
          }
        }
        else {      
          if ($hget(dragonball,final) == 0) {
            drawcopy @dbz.tmp.special.mirror 0 65 74 63 @dbz.tmp.final 0 0 
            var %i = 74
            while (%i <= 450) {
              drawcopy @dbz.tmp.special 66 65 22 63 @dbz.tmp.final %i 0
              inc %i 22
            } 
            drawcopy @dbz.tmp.special.mirror $calc($window(@dbz.tmp.special.mirror).w - 65) 65 65 63 @dbz.tmp.final $calc($window(@dbz.tmp.final).w - 65) 0          
            db final.x $calc($player(%p).x - 37)        
            db final 1 
            player %p2 display 0        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) $calc($window(@dbz.tmp.final).w - 65) 0 65 63 @dbz.tmp.buffer $calc($player(%p).x - 65) 118
          var %kame.width = $calc($player(%p).x - 37 - $hget(dragonball,final.x))
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 74 0 %kame.width 63 @dbz.tmp.buffer $calc($player(%p).x - %kame.width -65) 118 
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 74 63 @dbz.tmp.buffer $calc($hget(dragonball,final.x) - 74) 118

          hdec dragonball final.x $calc($hget(dragonball,dx) * 2)      

          if ($hget(dragonball,final.x) <= -200) { 
            player.stop %p             
          }
        }
      }
      elseif ($player(%p).char == 1 || $player(%p).char == 5) || ($player(%p).char == 3) || ($player(%p).char == 9) {
        if ($hget(dragonball,final) == -1) hadd dragonball final 0

        if ($player(%p).char == 5) var %tmp_y = 129
        else var %tmp_y = 0

        if ($player(%p).side == 0) {      
          if ($hget(dragonball,final) == 0) {      
            drawcopy @dbz.tmp.special 0 %tmp_y 59 64 @dbz.tmp.final 0 0
            var %i = 59
            while (%i <= 400) {
              drawcopy @dbz.tmp.special 60 %tmp_y 26 64 @dbz.tmp.final %i 0
              inc %i 22
            } 
            drawcopy @dbz.tmp.special 87 %tmp_y 77 64 @dbz.tmp.final $calc($window(@dbz.tmp.final).w - 77) 0          
            db final.x $calc($player(%p).x + 150) 
            db final 1 
            player %p2 display 0
            player %p2 sprite 23        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 59 64 @dbz.tmp.buffer $calc($player(%p).x + 36) 115
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 59 0 $calc($hget(dragonball,final.x) - $player(%p).x -100) 64 @dbz.tmp.buffer $calc($player(%p).x + 95) 115  
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) $calc($window(@dbz.tmp.final).w - 77) 0 77 64 @dbz.tmp.buffer $calc($hget(dragonball,final.x) - 74) 115

          hinc dragonball final.x $calc($hget(dragonball,dx) * 2)

          if ($hget(dragonball,final.x) >= 440) { 
            player.stop %p             
          }
        }
        else {      
          if ($hget(dragonball,final) == 0) {

            drawcopy @dbz.tmp.special.mirror 0 %tmp_y 77 64 @dbz.tmp.final 0 0 
            var %i = 77
            while (%i <= 450) {
              drawcopy @dbz.tmp.special 64 %tmp_y 26 64 @dbz.tmp.final %i 0
              inc %i 22
            } 
            drawcopy @dbz.tmp.special.mirror $calc($window(@dbz.tmp.special.mirror).w - 59) %tmp_y 59 64 @dbz.tmp.final $calc($window(@dbz.tmp.final).w - 59) 0          
            db final.x $calc($player(%p).x - 37)        
            db final 1 
            player %p2 display 0        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) $calc($window(@dbz.tmp.final).w - 59) 0 59 64 @dbz.tmp.buffer $calc($player(%p).x - 56) 115
          var %kame.width = $calc($player(%p).x - 20 - $hget(dragonball,final.x))
          ;echo k %kame.width      
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 77 0 %kame.width 64 @dbz.tmp.buffer $calc($player(%p).x - %kame.width -56) 115 
          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 77 64 @dbz.tmp.buffer $calc($hget(dragonball,final.x) - 77) 115

          hdec dragonball final.x $calc($hget(dragonball,dx) * 2)      

          if ($hget(dragonball,final.x) <= -130) { 
            player.stop %p             
          }
        }
      }
      elseif ($player(%p).char == 6 || $player(%p).char == 4) {
        if ($player(%p).char == 4) drawcopy @dbz.tmp.special 74 194 39 12 @dbz.tmp.final 0 0
        else drawcopy @dbz.tmp.special 34 194 39 12 @dbz.tmp.final 0 0
        if ($hget(dragonball,final) == -1) hadd dragonball final 0
        if ($player(%p).side == 0) {      
          if ($hget(dragonball,final) == 0) {      
            db final.x $calc($player(%p).x + 40) 
            db final 1 
            player %p2 display 0
            player %p2 sprite 01        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 39 12 @dbz.tmp.buffer $hget(dragonball,final.x) 139

          hinc dragonball final.x $calc($hget(dragonball,dx) * 2)
          if ($hget(dragonball,final.x) >= 440) { 
            player.stop %p             
          }
        }
        else {      
          if ($hget(dragonball,final) == 0) {
            db final.x $calc($player(%p).x - 39)        
            db final 1 
            player %p2 display 0        
          }
          drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(%p2).x 168
          drawcopy -t $iif($player(%p2).side == 0,@dbz.tmp.player $+ %p2 $+ .sprites,@dbz.tmp.player $+ %p2 $+ .sprites.mirror) $rgb(192,192,192) $calc($mid($player(%p2).sprite,2,1) *41) $calc($mid($player(%p2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(%p2).x $player(%p2).y 

          drawcopy -t @dbz.tmp.final $rgb(192,192,192) 0 0 39 12 @dbz.tmp.buffer $hget(dragonball,final.x) 139

          hdec dragonball final.x $calc($hget(dragonball,dx) * 2)      

          if ($hget(dragonball,final.x) <= -130) { 
            player.stop %p             
          }
        }
      }
    }
    elseif ($player(%p).x >= 20) && ($player(%p).side == 0) {
      player %p sprite 03       
      player.move.left %p $hget(dragonball,dx) 
    }
    elseif ($player(%p).x <= 196) && ($player(%p).side == 1) {
      player %p sprite 03
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      if ($hget(dragonball,final) == -1) {
        if ($player(%p).char == 0) || ($player(%p).char == 2) || ($player(%p).char == 7) || ($player(%p).char == 8) playsound final2.wav 
        elseif ($player(%p).char == 5) playsound final1.wav
        elseif ($player(%p).char == 3) playsound final3.wav
        elseif ($player(%p).char == 1) playsound final4.wav
        elseif ($player(%p).char == 9) playsound final5.wav
        hadd dragonball final 0
      }
      elseif ($player(%p).char == 6) { 
        if ($player(%p).side == 0) drawcopy -t @dbz.tmp.special $rgb(192,192,192) 34 194 39 12 @dbz.tmp.buffer $calc($player(%p).x - 9) $calc($player(%p).y - 8) 
        else drawcopy -t @dbz.tmp.special $rgb(192,192,192) 34 194 39 12 @dbz.tmp.buffer $calc($player(%p).x + 9) $calc($player(%p).y - 8)  
      }
      elseif ($player(%p).char == 4) {
        if ($player(%p).side == 0) drawcopy -t @dbz.tmp.special $rgb(192,192,192) 74 194 39 12 @dbz.tmp.buffer $calc($player(%p).x - 11) $calc($player(%p).y - 8) 
        else drawcopy -t @dbz.tmp.special $rgb(192,192,192) 74 194 39 12 @dbz.tmp.buffer $calc($player(%p).x + 11) $calc($player(%p).y - 8)     
      }
    }
  }
  elseif ($player(%p).anim == 13) {
    ;anim: ki blast
    player %p sprite 33     
    if ($player(%p).side == 0) {       
      if ($player(%p).char == 4) || ($player(%p).char == 5) var %tmp_x = 17
      else var %tmp_x 0
      if ($player(%p).anim-counter == 0) player %p ki $calc($player(%p).x +30)
      if ($inrect($calc($player(%p).ki + 8),150,$player(%p2).x,$player(%p2).y,40,50)) {
        if ($hget(dragonball,server) == 1) player.hurt %p2 10
        player %p ki 1000
      }       
      hinc dragonball player $+ [ %p ] $+ . $+ ki $calc($hget(dragonball,dx) *2)
      drawcopy -t @dbz.tmp.special $rgb(192,192,192) %tmp_x 194 16 12 @dbz.tmp.buffer $player(%p).ki 144
    }
    else {
      if ($player(%p).char == 4) || ($player(%p).char == 5) var %tmp_x = 131
      else var %tmp_x 148
      if ($player(%p).anim-counter == 0) player %p ki $calc($player(%p).x -2)
      if ($inrect($calc($player(%p).ki + 8),150,$player(%p2).x,$player(%p2).y,40,50)) {        
        if ($hget(dragonball,server) == 1) player.hurt %p2 10
        player %p ki 1000
      }
      hdec dragonball player $+ [ %p ] $+ . $+ ki $calc($hget(dragonball,dx) *2)
      drawcopy -t @dbz.tmp.special.mirror $rgb(192,192,192) %tmp_x 194 16 12 @dbz.tmp.buffer $player(%p).ki 144
    }
    if ($calc($tick - $player(%p).anim-tick) >= 1300) player.stop %p
  }
  elseif ($player(%p).anim == 14) {
    ;anim: jump + kick
    player %p sprite 13    
    player.jump %p $hget(dragonball,dx)
  }
  ;=====
  elseif ($player(%p).anim == 15) {
    ;anim: jump forward + kick
    player %p sprite 13    
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 0) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }
  }
  ;=====
  elseif ($player(%p).anim == 16) {
    ;anim: jump backward + kick
    player %p sprite 13    
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 1) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }    
  }
  ;====
  elseif ($player(%p).anim == 17) {
    ;anim: jump + punch
    player %p sprite 14
    player.jump %p $hget(dragonball,dx)
  }
  ;=====
  elseif ($player(%p).anim == 18) {
    ;anim: jump forward + punch
    player %p sprite 14    
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 0) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }
  }
  ;=====
  elseif ($player(%p).anim == 19) {
    ;anim: jump backward + punch
    player %p sprite 14    
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 1) {
      player.move.right %p $hget(dragonball,dx) 
    }
    else {
      player.move.left %p $hget(dragonball,dx) 
    }    
  }
  ;=====
  elseif ($player(%p).anim == 20) {
    ;anim: jump backward + hurt
    player %p sprite 22   
    player.jump %p $hget(dragonball,dx)
    if ($player(%p).side == 1) {
      player.move.right %p $hget(dragonball,dx) * 1 
    }
    else {
      player.move.left %p $hget(dragonball,dx) * 1
    }    
  }
  ;=====
  elseif ($player(%p).anim == 21) {
    ;anim: power up
    if ($calc($tick - $player(%p).anim-tick) >= 6000) {
      player %p sprite 01
      player.stop %p      
      if (%p == 2) && ($db(server)) hadd dragonball.server ui 1      
    }
    elseif ($calc($tick - $player(%p).anim-tick) >= 5000) {
      player %p sprite 01
      if (%p != 1) drawpic -t @dbz.tmp.buffer $rgb(0) 56 50 0 66 151 32 $dbzdir(sprites/text.png)
    } 
    elseif ($calc($tick - $player(%p).anim-tick) >= 4000) {
      player %p sprite 01
      if (%p == 1) player.play.anim 2 21      
    } 
    elseif ($calc($tick - $player(%p).anim-tick) >= 2000) { 
      if ($player(%p).sprite != 32) playsound powerup.wav
      player %p sprite 32
      var %tmp_y = 65
      if ($player(%p).char == 6) || ($player(%p).char == 9) %tmp_y = 0
      elseif ($player(%p).char == 4) || ($player(%p).char == 5) %tmp_y = 130
      if ($calc($tick - $player(%p).nticks) < 100) {
        drawcopy -t @dbz.tmp.fx 0 %tmp_y 0 64 56 @dbz.tmp.buffer $calc($player(%p).x -12) $calc($player(%p).y - 8)
      }  
      else {
        drawcopy -t @dbz.tmp.fx 0 %tmp_y 57 64 56 @dbz.tmp.buffer $calc($player(%p).x -12) $calc($player(%p).y - 8)
        if ($calc($tick - $player(%p).nticks) >= 200) player %p nticks $tick
      }
    }  
    elseif ($calc($tick - $player(%p).anim-tick) >= 1000) { 
      player %p sprite 34
    }
    else player %p sprite 00    
  }
  elseif ($player(%p).anim == 22) {
    player %p sprite 00
  }
  elseif ($player(%p).anim == 23) {
    player %p sprite 23
  }

  ;=====
  else {
    player %p sprite 01
  }
}
alias -l main.anim {
  var %ticks = $tick
  if ($hget(dragonball,server) == 1) {
    parse.keys  
  }
  drawrect -rf @dbz.tmp.buffer $rgb(255,0,255) 1 0 0 256 190
  parse.anim 1
  parse.anim 2
  drawcopy -t @dbz.tmp.topbar $rgb(192,192,192) 0 0 256 44 @dbz.tmp.buffer 0 0
  if ($hget(dragonball.config,fps)) drawtext -nr @dbz.tmp.buffer 0 verdana -7 4 176 fps: $hget(dragonball,fps)

  if ($calc($tick - $hget(dragonball,fps.ticks)) >= 1000) { 
    hadd dragonball fps $hget(dragonball,fps.counter)    
    hadd dragonball fps.counter 0
    hadd dragonball fps.ticks $tick
  }
  if ($player(2).display) drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(2).x 168
  if ($player(1).display) drawcopy -t @dbz.tmp.special $rgb(192,192,192) 114 194 40 5 @dbz.tmp.buffer $player(1).x 168
  if ($player(2).display) drawcopy -t $iif($player(2).side == 0,@dbz.tmp.player2.sprites,@dbz.tmp.player2.sprites.mirror) $rgb(192,192,192) $calc($mid($player(2).sprite,2,1) *41) $calc($mid($player(2).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(2).x $player(2).y 
  if ($player(1).display) drawcopy -t $iif($player(1).side == 0,@dbz.tmp.player1.sprites,@dbz.tmp.player1.sprites.mirror) $rgb(192,192,192) $calc($mid($player(1).sprite,2,1) *41) $calc($mid($player(1).sprite,1,1) *51) 40 50 @dbz.tmp.buffer $player(1).x $player(1).y 

  ; ----------------> end <-------------------

  if (!$timer(dbz-main)) return
  elseif (!$sock(dbz_connect)) {
    timerdbz-main off
    return
  }
  drawcopy -n @dbz.tmp.stage $hget(dragonball,stage.x) 0 256 190 @DragonBall 131 11
  drawcopy -tn @dbz.tmp.buffer $rgb(255,0,255) 0 0 256 190 @DragonBall 131 11
  if ($hget(dragonball,winner)) && ($hget(dragonball,final) == 1) && ($player(1).anim != 12) && ($player(2).anim != 12) {    
    drawfont 60 $hget(dragonball,winner)
    drawfont 90 WINS   
  }
  if ($hget(dragonball,score)) {
    draw.score
  }
  drawdot @DragonBall

  ; ----------------------------------->

  if ($hget(dragonball,player1.anim) > 0) hinc dragonball player1.anim-counter 
  if ($hget(dragonball,player2.anim) > 0) hinc dragonball player2.anim-counter

  if ($hget(dragonball,server) == 1) {
    if ($r(1,2000) > 1000) {      
      parse.hit 1
      parse.hit 2
    }
    else {      
      parse.hit 2
      parse.hit 1
    }
  }
  hinc dragonball fps.counter
  var %loop_time = $calc($tick - $hget(dragonball,ticks))
  var %dx = $round($calc(120/1000 * %loop_time),2) + $hget(dragonball,rest) 
  hadd dragonball dx $calc(0 + $int(%dx))
  hadd dragonball rest $calc(%dx - $int(%dx))   
  hadd dragonball ticks $tick
}

;===============

alias -l draw.score {
  drawrect -nfr @dragonball $db(bgcolor2) 1 131 11 256 190
  drawfont 18 ranking
  drawtext -nr @dragonball $db(color) verdana -7 140 42 Rank
  drawtext -nr @dragonball $db(color) verdana -7 214 42 Nick
  drawtext -nr @dragonball $db(color) verdana -7 300 42 Win
  drawtext -nr @dragonball $db(color) verdana -7 341 42 Lost
  drawline -nr @dragonball $db(color) 1 140 57 376 57
  drawline -nr @dragonball $db(color) 1 168 57 168 192
  drawline -nr @dragonball $db(color) 1 286 57 286 192
  drawline -nr @dragonball $db(color) 1 332 57 332 192
  var %i = 1, %color = $db(color), %y
  while (%i <= 8) {
    if ($dbzuser(%i).rank isnum 1-8) {
      if ($dbzuser(%i).nick == $db(me)) %color = $db(color2)
      else %color = $db(color)
      %y = $calc(50 + $dbzuser(%i).rank * 16)
      drawtext -nr @dragonball %color verdana -7 148 %y $dbzuser(%i).rank
      drawtext -cnr @dragonball %color verdana -7 180 %y 100 14 $dbzuser(%i).nick
      drawtext -nr @dragonball %color verdana -7 $calc(309 - $width($dbzuser(%i).win,verdana,-7) /2) %y $dbzuser(%i).win
      drawtext -nr @dragonball %color verdana -7 $calc(353 - $width($dbzuser(%i).lost,verdana,-7) /2) %y $dbzuser(%i).lost
    }
    inc %i
  }  
}
alias -l parse.hit {
  var %ticks-save = $tick 
  var %p = $1
  var %p2 = $iif(%p == 1,2,1)
  if ($player(%p).hurt == 1) {    
    if ($player(%p).anim == 3) || ($player(%p).anim == 4) {        
      if ($player(%p).side == 0 && $inrect($calc($player(%p).x + 40),$calc($player(%p).y + 30),$calc($player(%p2).x + 14),$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 11
      } 
      elseif ($player(%p).side == 1 && $inrect($player(%p).x,$calc($player(%p).y + 30),$player(%p2).x,$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 11
      }
    }
    elseif ($player(%p).anim == 5) {        
      if ($player(%p).side == 0 && $inrect($calc($player(%p).x + 40),$calc($player(%p).y + 20),$calc($player(%p2).x + 14),$calc($player(%p2).y +10),26,40)) {        
        player.hurt %p2 20
      } 
      elseif ($player(%p).side == 1 && $inrect($player(%p).x,$calc($player(%p).y + 20),$player(%p2).x,$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 20
      }      
    }
    elseif ($player(%p).anim == 14) || ($player(%p).anim == 15) || ($player(%p).anim == 16) {        
      if ($player(%p).side == 0 && $inrect($calc($player(%p).x + 40),$calc($player(%p).y + 30),$calc($player(%p2).x + 14),$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 12
      } 
      elseif ($player(%p).side == 1 && $inrect($player(%p).x,$calc($player(%p).y + 30),$player(%p2).x,$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 12
      }
    }
    elseif ($player(%p).anim == 17) || ($player(%p).anim == 18) || ($player(%p).anim == 19) {        
      if ($player(%p).side == 0 && $inrect($calc($player(%p).x + 36),$calc($player(%p).y + 40),$calc($player(%p2).x + 14),$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 16
      } 
      elseif ($player(%p).side == 1 && $inrect($calc($player(%p).x + 4),$calc($player(%p).y + 40),$player(%p2).x,$calc($player(%p2).y +10),26,40)) {
        player.hurt %p2 16
      }
    }
  }
}

alias -l player.hurt {
  var %nrj = $2
  if ($player($1).anim == 11) {
    %nrj = 4
  }
  else {
    if ($player($1).y != $hget(dragonball,base.yl)) { player.play.anim $1 20 }
    elseif ($2 >= 12) { player.play.anim $1 10 }
    else { player.play.anim $1 9 }
  }
  player $iif($1 == 1,2,1) hurt 0  
  hdec dragonball player $+ [ $1 ] $+ . $+ nrj %nrj
  if ($player($1).nrj < 0) player $1 nrj 0
  broadcast_udp e $1 $player($1).nrj
  player.nrj
  if ($player($1).nrj == 0) {
    hadd dragonball.server ui 0
    hadd dragonball.server winner $iif($1 == 1,2,1)
    broadcast END_FIGHT $iif($1 == 1,2,1)
  } 
}

alias -l player.nrj {
  if ($player(1).nrj > 100) {
    drawrect -nrf @dbz.tmp.topbar $rgb(250,250,0) 1 10 17 100 8
    drawrect -nrf @dbz.tmp.topbar $rgb(100,200,0) 1 10 17 $calc($player(1).nrj - 100) 8
  }
  else {
    drawrect -nrf @dbz.tmp.topbar $rgb(250,80,0) 1 10 17 100 8
    drawrect -nrf @dbz.tmp.topbar $rgb(250,250,0) 1 10 17 $player(1).nrj 8
  }
  if ($player(2).nrj > 100) {
    drawrect -nrf @dbz.tmp.topbar $rgb(100,200,0) 1 146 17 100 8
    drawrect -nrf @dbz.tmp.topbar $rgb(250,250,0) 1 146 17 $calc(200 - $player(2).nrj) 8
  }
  else {
    drawrect -nrf @dbz.tmp.topbar $rgb(250,250,0) 1 146 17 100 8
    drawrect -nrf @dbz.tmp.topbar $rgb(250,80,0) 1 146 17 $calc(100 - $player(2).nrj) 8
  }  
  drawdot @dbz.tmp.topbar
}

alias -l player.play.anim {
  if ($db(server) == 1) && ($2 != 12) { 
    var %rep = 0
    if (($2 == 1) || ($2 == 2) || ($2 == 11)) && ($hget(dragonball.server,p.anim $+ $1) == $2) %rep = 1
    if (%rep == 0) { 
      broadcast_udp p $1 $2 
      hadd dragonball.server p.anim $+ $1 $2
    }
  }
  var %p = $1
  if ($2 == 3) playsound punch.wav
  elseif ($2 == 9) || ($2 == 10) || ($2 == 20) playsound hit1.wav
  elseif ($2 == 4) playsound kick.wav
  elseif ($2 == 5) playsound hkick.wav
  elseif ($2 == 12) {
    db final -1
    db final.x 0
  }
  if ($2 == 20) player %p sy $player(%p).y
  elseif ($2 >= 13 && $2 <= 19) {
    if ($player(%p).char == 6) playsound jump1.wav
    elseif ($player(%p).char == 5) playsound jump5.wav 
    elseif ($player(%p).char == 4) playsound jump4.wav
    elseif ($player(%p).char == 1) || ($player(%p).char == 9) playsound jump2.wav
    elseif ($player(%p).char == 7) playsound jump3.wav    
    else playsound jump6.wav
  }
  if ($2 == 21) player %p nticks $tick
  player %p hurt 1
  player %p up 0
  player %p anim-counter 0
  player %p anim-tick $tick
  player %p anim $2
}
alias -l db {
  if ($isid) return $hget(dragonball,$1-)
  else hadd dragonball $1-
}















/*
=============================================================
====== SERVER ===============================================
=============================================================
*/
alias -l dbzer {  
  if ($isid) return $hget(dragonball.server, [ user $+ [ $1 ] $+ . $+ [ $prop ] ] )
  else hadd dragonball.server user $+ [ $1 ] $+ . $+ [ $2 ] $3-
}

alias -l init.server {
  if (!$portfree($hget(dragonball.config,serverport))) {
    db error Error: cannot listen on port $hget(dragonball.config,serverport) $+ , please choose another port
    msg.menu
    halt
  }
  if ($hget(dragonball.server)) hfree dragonball.server
  hmake dragonball.server 10
  init.gui
  drawrect -rf @DragonBall $db(bgcolor2) 1 131 11 256 190

  db server 1
  hadd dragonball.server status 1
  hadd dragonball.server ui 0
  hadd dragonball.server total 0
  hadd dragonball.server p.anim1 -1
  hadd dragonball.server p.anim2 -2

  socklisten dbz_server $hget(dragonball.config,serverport)
  sockudp -k dbz_input $hget(dragonball.config,serverport)
  print »»» server listening - ip: $ip port: $hget(dragonball.config,serverport)
  db list add
  sockopen dbz_listing picwin.mircscripts.org 80
  .timerdbz-server -o 0 5 server.check
  .timerdbz-keepalive -o 0 180 keep.alive
}

alias -l keep.alive {
  db list alive
  sockopen dbz_listing picwin.mircscripts.org 80
}
alias -l server.check {
  var %i = 1
  while ($sock(dbz_tmp_user*,%i).name) {
    if ($sock(dbz_tmp_user*,%i).to >= 30) {
      print »»» connection timeout from $sock(dbz_tmp_user*,%i).ip (slot %i $+ )
      sockclose $sock(dbz_tmp_user*,%i)
    }
    inc %i
  } 
  if ($hget(dragonball.server,status) == 1) && ($player(1).id) && ($player(2).id) {
    broadcast_list  
    hadd dragonball.server ui 0
    hadd dragonball.server tick $tick
    hadd dragonball.server status 2
    broadcast STATUS $hget(dragonball.server,status)
  }
  elseif ($hget(dragonball.server,status) == 2) && ($calc($tick - $hget(dragonball.server,tick)) >= 10000) {
    hadd dragonball.server status 3
    hadd dragonball.server ui 1
    hadd dragonball.server cursor1 0
    hadd dragonball.server cursor2 4
    broadcast CHOOSE $player(1).id $player(2).id    
  }
}

alias -l end.match {
  hadd dragonball.server ui 0
  var %win_id = $player($hget(dragonball.server,winner)).id 
  var %los_id = $player($iif($hget(dragonball.server,winner) == 1,2,1)).id
  if (%win_id) hinc dragonball.server user $+ [ %win_id ] $+ . $+ win
  if (%los_id) hinc dragonball.server user $+ [ %los_id ] $+ . $+ lost
  var %p2_id, %i = 1, %last_id = $calc($get.new.order -1)
  while (%i <= 8) {
    if ($dbzer(%i).order) {
      ;echo -a old order of %i : $dbzer(%i).order
      if (%win_id) && (%i == %win_id) dbzer %i order 1
      elseif (%los_id) && (%i == %los_id) dbzer %i order %last_id
      else hdec dragonball.server user $+ [ %i ] $+ . $+ order
      if ($dbzer(%i).order == 2) %p2_id = %i   
      ;echo -a new order of %i : $dbzer(%i).order
    }
    inc %i
  }
  hdel -w dragonball player1*
  hdel -w dragonball player2*
  if (%win_id) add.player 1 %win_id
  if (%p2_id) add.player 2 %p2_id
  broadcast_list
  hadd dragonball.server tick $tick

  if ($hget(dragonball.server,total) < 2) { 
    hadd dragonball.server status 1   
  }
  else {
    hadd dragonball.server status 2
  }
  broadcast STATUS $hget(dragonball.server,status)
}

on *:udpread:dbz_input: {
  if ($sockerr > 0) return
  var %tmp
  sockread %tmp
  if ($db(debug)) echo @debug-udp (server get) $sock($sockname).saddr $sock($sockname).sport : %tmp
  if (%tmp == pr) {    
    sockudp -tn dbz_listsend_ $+ $tick $sock($sockname).saddr $sock($sockname).sport $hget(dragonball.server,total) $+ / $+ $hget(dragonball.config,maxplayer)
    return
  }
  var %p = 0
  if ($sock($sockname).saddr == $player(1).ip) && ($sock($sockname).sport == $player(1).udp) %p = 1
  elseif ($sock($sockname).saddr == $player(2).ip) && ($sock($sockname).sport == $player(2).udp) %p = 2
  tokenize 32 %tmp
  if (%p > 0 ) && ($1 == 1) || ($1 == 0) {
    player %p key_ $+ $2 $1
    if ($hget(dragonball.server,status) == 3) {
      var %cur1 = $hget(dragonball.server,cursor1) , %cur2 = $hget(dragonball.server,cursor2)
      parse.keys
      if (%cur1 != $hget(dragonball.server,cursor1)) || (%cur2 != $hget(dragonball.server,cursor2)) broadcast_udp CURCHANGE $hget(dragonball.server,cursor1) $hget(dragonball.server,cursor2)
    }
  }
  elseif ($1 == L) && ($dbzer($2).ip == $sock($sockname).saddr) && (!$dbzer($2).udp) {
    dbzer $2 udp $sock($sockname).sport
    if ($player(1).id == $2) player 1 udp $sock($sockname).sport
    elseif ($player(2).id == $2) player 2 udp $sock($sockname).sport    
  }
}

alias -l sockuser {
  return dbz_user $+ $1
}
alias -l tmpsockuser {
  return dbz_tmp_user_ $+ $1
}
on *:socklisten:dbz_server: {
  if ($sockerr > 0) return
  var %sockname = dbz_tmp_user_ $+ $tick $+ $r(1,1000)
  sockaccept %sockname
  print »»» connection attempt from $sock(%sockname).ip
  sockwrite -tn %sockname CONNECTED
}

on *:sockclose:dbz_user*: {
  remove.user $right($sockname,1) 0
}
on *:sockread:dbz_tmp_user_*: {
  if ($sockerr > 0) return
  var %data
  sockread %data
  tokenize 30 %data 
  if ($1 == INFOS) {
    if ($hget(dragonball.config,password)) && ($4 != $hget(dragonball.config,password)) {
      print »»» connection from $sock($sockname).ip refused, invalid password
      sockwrite -tn $sockname INVALID_PASSWORD
      .timer -mo 1 0 sockclose $sockname
    }
    elseif ($2 != $db(version)) {
      print »»» connection from $sock($sockname).ip refused, invalid version
      sockwrite -tn $sockname INVALID_VERSION $db(version)
      .timer -mo 1 0 sockclose $sockname
    }
    else {
      var %nick = $left($3,13)
      if (!%nick) %nick = DBZ Player
      var %i = 1
      var %num = 0
      while (%i <= 8) {
        if ($dbzer(%i).nick == %nick) {        
          inc %num
          if (%num > 1) %nick = $left(%nick,$calc($len(%nick) -2))
          %nick = %nick %num
          %i = 1
          continue
        }
        inc %i
      }
      %i = 1
      while ($dbzer(%i).ip) inc %i
      if (%i > $hget(dragonball.config,maxplayer)) {
        sockwrite -tn $sockname SERVER_FULL
        .timer -mo 1 0 sockclose $sockname        
      }
      else {
        dbzer %i ip $sock($sockname).ip
        dbzer %i nick %nick
        dbzer %i win 0
        dbzer %i lost 0
        dbzer %i order $get.new.order

        sockrename $sockname $sockuser(%i)
        sockwrite -tn $sockname ACCEPTED %i %nick
        envoi_list %i
        sockwrite -tn $sockname STATUS $hget(dragonball.server,status)
        broadcast JOIN %i $dbzer(%i).order $dbzer(%i).nick      
        if ($dbzer(%i).order == 1) || ($dbzer(%i).order == 2) add.player $dbzer(%i).order %i
        hinc dragonball.server total
      }
    }
  }
}

on *:sockread:dbz_user*: {
  if ($sockerr > 0) return
  var %data
  sockread %data
  if ($db(debug)) echo @debug-tcp (server get) from $sockname $+ : %data
  tokenize 32 $right($sockname,1) %data 
  if ($2 == MSG) {
    var %test = $3-
    if ($3-) broadcast MSG $1 $strip($3-)
  }
  else return
}

alias -l broadcast {
  sockwrite -tn dbz_user* $1-
}
alias -l broadcast_list {
  var %i = 1   
  while (%i <= 8) {
    if ($dbzer(%i).ip) broadcast USER %i $dbzer(%i).order $dbzer(%i).win $dbzer(%i).lost $dbzer(%i).nick
    inc %i
  }
  broadcast USER_END
}
alias -l envoi_list {
  var %i = 1, %isplaying    
  while (%i <= 8) {    
    if ($dbzer(%i).ip) sockwrite -tn $sockuser($1) USER %i $dbzer(%i).order $dbzer(%i).win $dbzer(%i).lost $dbzer(%i).nick
    inc %i
  }
  sockwrite -tn $sockuser($1) USER_END
}
alias -l broadcast_udp {
  var %i = 1    
  while (%i <= 8) {      
    if ($dbzer(%i).udp) sockudp -tn dbz_broadcast $dbzer(%i).ip $dbzer(%i).udp  $1-
    inc %i
  }
}

alias -l add.player {
  hdel -w dragonball player $+ $1 $+ *
  player $1 id $2
  player $1 nick $dbzer($2).nick
  player $1 ready 0
  player $1 ip $dbzer($2).ip
  player $1 udp $dbzer($2).udp

}
alias -l get.new.order {
  var %i = 1, %order = 1   
  while (%i <= 8) {
    if ($dbzer(%i).order >= %order) %order = $dbzer(%i).order + 1
    inc %i
  }
  return %order
}
alias -l kick.player {
  if ($db(server) != 1) {
    print »»» you must be the server to kick a player
    return
  }
  if ($db(me) == $1-) {
    print »»» you cannot kick yourself
    return
  }
  if (!$1) {
    print »»» kick: you must specify the nick of the player to kick
    return
  }
  var %i = 1
  while (%i <= 8) {
    if ($dbzer(%i).nick == $1-) {
      remove.user %i 1
      return
    }
    inc %i
  }
  if (%i >= 8) {
    print »»» kick: invalid player name
    return
  }
}
alias -l remove.user {
  if ($sock($sockuser($1))) { sockclose $sockuser($1) }
  hdec dragonball.server total
  var %p = 0, %order = $dbzer($1).order, %i = 1
  if ($player(1).id == $1) %p = 1
  elseif ($player(2).id == $1) %p = 2
  hdel -w dragonball.server user $+ $1 $+ *
  hdel -w dragonball player $+ %p $+ *  
  if (%p > 0) {   
    hadd dragonball.server winner $iif(%p == 1,2,1)    
    end.match      
  }
  else {
    while (%i <= 8) {
      if ($dbzer(%i).order >= %order) hdec dragonball.server user $+ [ %i ] $+ . $+ order            
      inc %i
    }
  }  
  if ($2 == 1) broadcast KICK $1
  else broadcast QUIT $1
}

alias -l parse.keys {
  if ($hget(dragonball.server,ui) != 1) return
  var %p = 1
  while (%p <= 2) {
    if ($hget(dragonball.server,status) == 3) && ($player(%p).char !isnum 0-9) {
      var %cursor = $hget(dragonball.server,cursor $+ %p)

      if ($player(%p).key_left == 1) {
        player %p key_left 0
        if (%cursor != 0) && (%cursor != 5) hdec dragonball.server cursor $+ [ %p ]
      }
      elseif ($player(%p).key_right == 1) {
        player %p key_right 0
        if (%cursor != 4) && (%cursor != 9) hinc dragonball.server cursor $+ [ %p ] 
      }
      elseif ($player(%p).key_up == 1) {
        player %p key_up 0
        if (%cursor > 4) hdec dragonball.server cursor $+ [ %p ] 5
      }
      elseif ($player(%p).key_down == 1) {
        player %p key_down 0
        if (%cursor <= 4) hinc dragonball.server cursor $+ [ %p ] 5
      }
      elseif ($player(%p).key_enter == 1) {
        player %p key_enter 0
        player %p char $int($hget(dragonball.server,cursor $+ %p))
        broadcast SELECT %p $player(%p).char
        if ($player(1).char isnum 0-9) && ($player(2).char isnum 0-9) {
          hadd dragonball.server status 4 $player(1).char $player(2).char $rand(1,3)          
          .timerdbz-startfight -o 1 2 broadcast STATUS $hget(dragonball.server,status)
        }
      }    
    }
    elseif ($left($hget(dragonball.server,status),1) == 4) {
      if ($player(%p).key_punch == 1) {
        player %p key_punch 0
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {            
          player.play.anim %p 3      
        }
        elseif ($player(%p).anim == 6) player.play.anim %p 17
        elseif ($player(%p).anim == 7) player.play.anim %p 18
        elseif ($player(%p).anim == 8) player.play.anim %p 19
      }
      elseif ($player(%p).key_kick == 1) {
        player %p key_kick 0
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          player.play.anim %p 4     
        }
        elseif ($player(%p).anim == 6) player.play.anim %p 14
        elseif ($player(%p).anim == 7) player.play.anim %p 15
        elseif ($player(%p).anim == 8) player.play.anim %p 16
      }
      elseif ($player(%p).key_highkick == 1) {
        player %p key_highkick 0
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {           
          player.play.anim %p 5      
        }
        elseif ($player(%p).anim == 6) player.play.anim %p 14
        elseif ($player(%p).anim == 7) player.play.anim %p 15
        elseif ($player(%p).anim == 8) player.play.anim %p 16
      }

      elseif ($player(%p).key_up == 1) {
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          if ($player(%p).key_left == 1) {
            if ($player(%p).side == 1) player.play.anim %p 7
            else player.play.anim %p 8
          }
          elseif ($player(%p).key_right == 1) {
            if ($player(%p).side == 1) player.play.anim %p 8
            else player.play.anim %p 7
          }
          else player.play.anim %p 6
        }
      }
      elseif ($player(%p).key_left == 1) {
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          if ($player(%p).side == 1) player.play.anim %p 1
          else player.play.anim %p 2
        }
      }
      elseif ($player(%p).key_right == 1) {
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          if ($player(%p).side == 1) player.play.anim %p 2
          else player.play.anim %p 1
        }
      }
      elseif ($player(%p).key_block == 1) {
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          player.play.anim %p 11      
        }
      }     
      elseif ($player(%p).key_blast == 1) {
        if ($player(%p).anim == 0) || ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
          player %p key_blast 0      
          player.play.anim %p 13      
        }
      }
      elseif ($player(%p).anim == 1) || ($player(%p).anim == 2) || ($player(%p).anim == 11) {
        player.stop %p
      }
    }
    inc %p
  }
}
















/*
=============================================================
====== USER =================================================
=============================================================
*/
alias -l dbzuser {  
  if ($isid) return $hget(dragonball.users, [ user $+ [ $1 ] $+ . $+ [ $prop ] ] )
  else hadd dragonball.users user $+ [ $1 ] $+ . $+ [ $2 ] $3-
}

on *:sockclose:dbz_connect: {
  if (!$db(error)) {
    db error Server disconnected
    msg.menu
  }
}

alias -l connect {
  db error
  db score
  db st connecting
  drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
  drawfont 93 connecting
  drawdot @DragonBall
  if ($1) {
    hadd dragonball.config ip $ip
    hadd dragonball.config port $hget(dragonball.config,serverport)
    hadd dragonball.config password $hget(dragonball.config,serverpassword)
  }
  sockopen dbz_connect $hget(dragonball.config,ip) $hget(dragonball.config,port)
  if ($hget(dragonball.users)) hfree dragonball.users
  hmake dragonball.users 10
  db ms 0
}
alias -l drawfont {
  if (!$2) return
  var %txt = $upper($2-), %i = 1, %pos = $int($calc(131 + (128 - $len($2-) *16 / 2)))
  while (%i <= $len($2-)) {
    if ($asc($mid(%txt,%i,1)) != 32) drawcopy -nt @dbz.tmp.font 0 $calc($fontnum($asc($mid(%txt,%i,1))) * 16) 0 16 16 @dragonball %pos $int($1)
    inc %i
    inc %pos 16
  }
}
alias -l fontnum {
  if ($1 >= 65) && ($1 <= 90) return $calc($1 - 65)
  elseif ($1 == 40) || ($1 == 41) return $calc($1 - 14)
  elseif ($1 == 33) return 28
  elseif ($1 == 45) return 30
  elseif ($1 == 46) return 31
  elseif ($1 == 58) return 32
  elseif ($1 == 39) || ($1 == 34) || ($1 == 96) return 33
  elseif ($1 == 44) return 34
  else return 29
}
on *:sockwrite:dbz_connect: {
  if ($sockerr > 0) {
    db error Server disconnected
    msg.menu
  }
}
on *:sockread:dbz_connect: {
  if ($sockerr > 0) {
    db error Server disconnected
    msg.menu
    return
  }
  var %data    
  sockread %data
  tokenize 32 %data
  if ($db(debug)) echo @debug-tcp (get) $1- ( $+ ms $db(ms) $+ )
  if ($1 == CONNECTED) {
    envoi INFOS $+ $chr(30) $+ $db(version) $+ $chr(30) $+ $hget(dragonball.config,nick) $+ $chr(30) $+ $hget(dragonball.config,password)    
  }
  elseif ($1 == INVALID_VERSION) {
    db error Invalid version - This server is running with DBZ v $+ $2
    msg.menu
  }
  elseif ($1 == INVALID_PASSWORD) {
    db error Invalid password
    msg.menu
  }
  elseif ($1 == ACCEPTED) {
    envoi_udp L $int($2)
    db me $3-
  }
  elseif ($1 == USER_END) {
    if ($db(ms) != 1) db ms 1
    update.list  
  }
  elseif ($1 == USER) {
    dbzuser $int($2) order $3
    dbzuser $int($2) win $4
    dbzuser $int($2) lost $5
    if ($3 == 1) || ($3 == 2) player $3 nick $6-
    dbzuser $int($2) nick $6-
  }
  elseif ($1 == JOIN) {    
    dbzuser $int($2) order $3
    dbzuser $int($2) win 0
    dbzuser $int($2) lost 0
    if ($3 == 1) || ($3 == 2) player $2 nick $4-
    dbzuser $int($2) nick $4-
    update.list
    if ($db(me) != $4-) print »»» $4- joined the game      
    else print »»» you joined the game
  }
  elseif ($1 == QUIT) {    
    print »»» $dbzuser($2).nick left the game
    if ($player(1).nick == $dbzuser($2).nick) hdel -w dragonball player1*
    elseif ($player(2).nick == $dbzuser($2).nick) hdel -w dragonball player2*
    hdel -w dragonball.users user $+ $2 $+ *
    update.list
  }
  elseif ($1 == KICK) {    
    print »»» $dbzuser($2).nick was kicked from the game
    if ($player(1).nick == $dbzuser($2).nick) hdel -w dragonball player1*
    elseif ($player(2).nick == $dbzuser($2).nick) hdel -w dragonball player2*
    hdel -w dragonball.users user $+ $2 $+ *
    update.list
  }
  elseif ($1 == SERVER_FULL) {    
    db error The server is full, connection failed
    msg.menu
  }
  elseif ($1 == STATUS) && ($db(ms) == 1) {
    db st $2
    if ($timer(dbz-main)) .timerdbz-main off
    if ($2 == 1) {
      if ($db(score)) return
      drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
      drawfont 68 waiting
      drawfont 98 for
      drawfont 128 players
      drawdot @DragonBall
    }
    elseif ($2 == 2) {
      if ($db(score)) return     
      drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
      drawfont 38 next match:
      drawfont 98 $player(1).nick
      drawfont 128 VS
      drawfont 158 $player(2).nick
      drawdot @DragonBall
    }
    elseif ($2 == 3) {
      db cursor1 0
      db cursor2 4 
      choose.fighter
    }    
    elseif ($2 == 4) {
      player 1 char $int($3)
      player 2 char $int($4)
      hadd dragonball stage $int($5)
      start.match
    }    
  }
  elseif ($1 == CHOOSE) {
    db st 3
    db cursor1 0
    db cursor2 4 
    player 1 char
    player 2 char  
    choose.fighter
  }
  elseif ($1 == SELECT) {
    hadd dragonball choose_ok 1    
    db cursor $+ $int($2) $int($3)
    player $int($2) char $int($3)
    playsound ok.wav
    choose.fighter 
  }
  elseif ($1 == END_FIGHT) {     
    hadd dragonball winner $player($int($2)).nick
  }
  elseif ($1 == MSG) {  
    if ($3-) print < $+ $dbzuser($2).nick $+ > $strip($3-)
  }  
}
on *:sockopen:dbz_connect: {
  if ($sockerr) { 
    db error Connection failed
    msg.menu
  }
}
on *:udpread:dbz_data: {
  if ($sockerr > 0) return
  var %tmp
  sockread %tmp
  tokenize 32 %tmp
  if ($db(debug)) { 
    echo @debug-udp (get) $1- 
  }
  if ($1 == p) {
    if ($db(server) != 1) player.play.anim $2 $3
  }
  elseif ($1 == s) && ($db(server) != 1) {
    player $2 x $3 
    player $2 y 122
    player.stop $2
    player $iif($2 == 1,2,1) x $4
    hadd dragonball stage.x $5 
  }
  elseif ($1 == e) && ($db(server) != 1) {
    player $int($2) nrj $int($3)
    player.nrj
  }
  elseif ($1 == CURCHANGE) {
    db cursor1 $int($2)
    db cursor2 $int($3)
    choose.fighter
    playsound menu
  }
}

alias -l envoi {
  sockwrite -tn dbz_connect $1-
}
alias -l envoi_udp {
  if ($hget(dragonball.config,ip)) sockudp -ktn dbz_data $hget(dragonball.config,ip) $hget(dragonball.config,port) $1-
}

alias -l choose.fighter {
  if ($db(score)) return
  drawrect -nrf @DragonBall $db(bgcolor2) 1 131 11 256 190
  drawfont 20 select fighter
  drawcopy -nt @dbz.tmp.char $rgb(192,192,192) 0 0 200 50 @DragonBall 155 58
  drawcopy -nt @dbz.tmp.char $rgb(192,192,192) 200 0 200 50 @DragonBall 155 128
  var %color1, %color2
  if ($player(1).char isnum 0-9) %color1 = $rgb(255,200,0)
  else %color1 = $rgb(0,0,255)
  if ($player(2).char isnum 0-9) %color2 = $rgb(255,200,0)
  else %color2 = $rgb(128,0,0)

  if ($db(cursor1) <= 4) {
    drawtext -cnr @dragonball $iif(%color1 != $rgb(255,200,0),$rgb(255,255,255),0) Verdana -7 $calc(155 + $db(cursor1) *40) 45 40 12 $player(1).nick 
    drawtext -cnr @dragonball %color1 Verdana -7 $calc(154 + $db(cursor1) *40) 44 40 12 $player(1).nick
    drawrect -dnr @dragonball $iif(%color1 != $rgb(255,200,0),$rgb(255,255,255),0) 1 $calc(156 + $db(cursor1) *40) 59 40 50 10 10
    drawrect -dnr @dragonball %color1 1 $calc(155 + $db(cursor1) *40) 58 40 50 10 10
  }
  else {
    drawtext -cnr @dragonball $iif(%color1 != $rgb(255,200,0),$rgb(255,255,255),0) Verdana -7 $calc(155 + ($db(cursor1) -5) *40) 115 40 12 $player(1).nick
    drawtext -cnr @dragonball %color1 Verdana -7 $calc(154 + ($db(cursor1) -5) *40) 114 40 12 $player(1).nick
    drawrect -dnr @dragonball $iif(%color1 != $rgb(255,200,0),$rgb(255,255,255),0) 1 $calc(156 + ($db(cursor1) -5) *40) 129 40 50 10 10
    drawrect -dnr @dragonball %color1 1 $calc(155 + ($db(cursor1) -5) *40) 128 40 50 10 10
  }
  if ($db(cursor2) <= 4) {
    drawtext -cnr @dragonball $iif(%color2 != $rgb(255,200,0),$rgb(255,255,255),0) Verdana -7 $calc(155 + $db(cursor2) *40) 45 40 12 $player(2).nick
    drawtext -cnr @dragonball %color2 Verdana -7 $calc(154 + $db(cursor2) *40) 44 40 12 $player(2).nick
    drawrect -dnr @dragonball $iif(%color2 != $rgb(255,200,0),$rgb(255,255,255),0) 1 $calc(156 + $db(cursor2) *40) 59 40 50 10 10
    drawrect -dnr @dragonball %color2 1 $calc(155 + $db(cursor2) *40) 58 40 50 10 10
  }
  else {
    drawtext -cnr @dragonball $iif(%color2 != $rgb(255,200,0),$rgb(255,255,255),0) Verdana -7 $calc(155 + ($db(cursor2) -5) *40) 115 40 12 $player(2).nick
    drawtext -cnr @dragonball %color2 Verdana -7 $calc(154 + ($db(cursor2) -5) *40) 114 40 12 $player(2).nick
    drawrect -dnr @dragonball $iif(%color2 != $rgb(255,200,0),$rgb(255,255,255),0) 1 $calc(156 + ($db(cursor2) -5) *40) 129 40 50 10 10
    drawrect -dnr @dragonball %color2 1 $calc(155 + ($db(cursor2) -5) *40) 128 40 50 10 10
  }
  drawdot @DragonBall
}

alias -l update.list {
  drawrect -nrf @DragonBall $db(bgcolor2) 1 399 26 108 28
  drawrect -nrf @DragonBall $db(bgcolor2) 1 399 101 108 100
  if ($player(1).nick) drawtext -ncbr @DragonBall $iif($player(1).nick != $db(me),$db(color),$db(color2)) $db(bgcolor2) verdana -7 401 28 103 12 $player(1).nick
  if ($player(2).nick) drawtext -ncbr @DragonBall $iif($player(2).nick != $db(me),$db(color),$db(color2)) $db(bgcolor2) verdana -7 401 40 103 12 $player(2).nick
  var %i = 1
  var %order, %j, %k, %me_id, %win
  while (%i <= 9) {
    if ($dbzuser(%i).order) {
      %order = %order $dbzuser(%i).order $+ %i
      %win = %win $dbzuser(%i).win $+ %i
    }
    inc %i
  }
  %order = $sorttok(%order,32,n)
  %win = $sorttok(%win,32,nr)
  var %y = 102
  %i = 1
  while (%i <= $numtok(%order,32)) {
    %j = $right($gettok(%order,%i,32),1)
    %k = $right($gettok(%win,%i,32),1)
    dbzuser %k rank %i
    if ($dbzuser(%j).nick != $player(1).nick) && ($dbzuser(%j).nick != $player(2).nick) {
      if ($dbzuser(%j).nick) drawtext -ncbr @DragonBall $iif($dbzuser(%j).nick != $db(me),$db(color),$db(color2)) $db(bgcolor2) verdana -7 401 %y 103 12 $dbzuser(%j).nick
      inc %y 12      
    }
    if ($dbzuser(%j).nick == $db(me)) %me_id = %j
    inc %i
  }
  drawrect -nrf @DragonBall $db(bgcolor2) 1 11 26 108 40
  drawtext -ncbr @DragonBall $db(color) $db(bgcolor2) verdana -7 13 28 103 12 Rank: $dbzuser(%me_id).rank
  drawtext -ncbr @DragonBall $db(color) $db(bgcolor2) verdana -7 13 40 103 12 Win: $dbzuser(%me_id).win
  drawtext -ncbr @DragonBall $db(color) $db(bgcolor2) verdana -7 13 52 103 12 Lost: $dbzuser(%me_id).lost
  if (!$1) drawdot @DragonBall
}

alias -l keydown { 
  envoi_udp 1 $1 
}
alias -l keyup {
  envoi_udp 0 $1
}

on *:KEYDOWN:@Dragonball:*: {
  if (!$keyrpt) {
    if ($db(click) >= 1) {
      x-gui Set @dragonball > text $calc($db(click) +20) key $keyval
      db click
      config.menu
      return
    }
    if ($keyval == 13) {
      keydown enter
    }
    if ($keyval == $hget(dragonball.config,up)) {
      keydown up
    }
    elseif ($keyval == $hget(dragonball.config,down)) {
      keydown down
    }
    elseif ($keyval == $hget(dragonball.config,left)) {
      keydown left
    }
    elseif ($keyval == $hget(dragonball.config,right)) {
      keydown right
    }
    elseif ($keyval == $hget(dragonball.config,block)) {
      keydown block
    }
    elseif ($keyval == $hget(dragonball.config,punch)) {
      keydown punch
    }
    elseif ($keyval == $hget(dragonball.config,kick)) {
      keydown kick
    }
    elseif ($keyval == $hget(dragonball.config,highkick)) {
      keydown highkick
    }
    elseif ($keyval == $hget(dragonball.config,blast)) {
      keydown blast
    }       
  }   
}
on *:KEYUP:@Dragonball:*: {
  if ($keyval == 13) {
    keyup enter
  }
  if ($keyval == $hget(dragonball.config,up)) {
    keyup up
  }
  elseif ($keyval == $hget(dragonball.config,down)) {
    keyup down
  }
  elseif ($keyval == $hget(dragonball.config,left)) {
    keyup left
  }
  elseif ($keyval == $hget(dragonball.config,right)) {
    keyup right
  }
  elseif ($keyval == $hget(dragonball.config,block)) {
    keyup block
  }
  elseif ($keyval == $hget(dragonball.config,punch)) {
    keyup punch
  }
  elseif ($keyval == $hget(dragonball.config,kick)) {
    keyup kick
  }
  elseif ($keyval == $hget(dragonball.config,highkick)) {
    keyup highkick
  }
  elseif ($keyval == $hget(dragonball.config,blast)) {
    keyup blast
  }  
}
ON *:SIGNAL:xGUI: {
  ;echo [x-gui signal] $1-
  if ($1 == @dragonball) {   
    if ($2 == joystick) {
      ;process joystick events
      if ($3 == move) {        
        if ($4 == -1) keydown right
        else if ($4 == 1) keydown left      
        if ($5 == 1) keydown up
        elseif ($5 == -1) keydown down
        if ($4 == 0) { keyup right | keyup left }
        if ($5 == 0) { keyup up | keyup down }
      }
      else if ($3 == button) {
        if ($4) keydown punch
        else keyup punch
        if ($5) keydown kick
        else keyup kick
        if ($6) keydown highkick
        else keyup highkick
        if ($7) keydown blast
        else keyup blast      
      }
    }
    elseif ($3 == 2) {
      if ($2 == enter) {
        tokenize 32 $x-gui(Editbox,@DragonBall > GetText 2)
        if (!$1) return
        if ($left($1,1) == /) {
          if ($1 == /kick) kick.player $2-
          else print »»» invalid command
        }
        else envoi MSG $x-gui(Editbox,@DragonBall > GetText 2)  
        x-gui Set @DragonBall > text 2   
      }
      elseif ($2 == setfocus) {        
        x-gui Set @DragonBall > color 2 $db(bgcolor3) $db(color)
      }
      elseif ($2 == killfocus) {        
        x-gui Set @DragonBall > color 2 $db(bgcolor2) $db(color)
      }
    }
    elseif ($3 > 20) && ($3 < 30) && ($2 == setfocus) {
      x-gui Set @dragonball > nofocus 20
      db click $calc($3 - 20)
      config.menu
    }
    elseif ($3 == 90) || ($3 == 91) && ($2 == setfocus) {
      x-gui Set @dragonball > nofocus 20
      if ($x-gui(Get,@dragonball > text $3)) x-gui Set @dragonball > text $3
      else x-gui Set @dragonball > text $3 $chr(149)
    }
    elseif ($3 == 42) && ($2 == sclick) x-gui Set @dragonball > nofocus 42 
    elseif ($3 == 42) && ($2 == dclick) {
      x-gui Set @dragonball > nofocus 42 
      servers.list.join
    }
  }
}

/*
=====================
= That's all folks :)
=====================
*/
