;-------------------------------- -------------- - -----------------------
; Sokoban 1.2 by praetorian 
; praetorian@mircscripts.org
; http://praetorian.mircscripts.org
;---------------- --- -   - - 
; Sokoban is a simple puzzle game created in Japan in 1980 and copyrighted by Thinking Rabbit, INC. in 1982.
;
; The objective of the game is to move all of the squares (cardboard boxes) into the "slots" (shaded/grayed boxes)
; until all of the boxes have been placed on a square.
;
; See README.txt for installation instructions and general information about this addon.

on 1:load:{
  if ($version < 5.8) { echo $color(info) -ae * Sokoban requires mIRC 5.8 or higher to function.  You can try to use it anyway, but it probably isn't going to work. }
  else {
    echo $color(info) -ae * Sokoban 1.2 by praetorian loaded.
    echo $color(info) -ae * To play, click on "Play Sokoban" in the menubar or status window popup, or type /sokoban.
  }
}
dialog sokoabout {
  title "Sokoban - About"
  size -1 -1 150 30
  option dbu
  icon $scriptdirimages\box.ico
  text "Sokoban v1.2 by praetorian", 1, 20 2 127 8
  button "Button", 3, 0 0 0 0, ok
  icon 5, 1 5 15 15, $scriptdirImages\box.ico, 0, noborder
  text "Email: ", 6, 20 10 16 8
  link "praetorian@mircscripts.org", 7, 36 10 112 9
  text "Homepage:", 8, 20 18 27 8
  link "http://www.detox.xs3.com", 9, 50 18 98 8
  text "", 11, 2 28 146 8,
  text "", 12, 2 36 146 8,
  text "", 13, 2 44 146 8,
  text "", 14, 2 52 146 8,
  text "", 15, 2 60 146 8,
  text "", 16, 2 68 146 8,
  text "", 17, 2 76 146 8,
  text "", 18, 2 84 146 8,
  text "", 19, 2 92 146 8,
  text "", 20, 2 100 146 8,
  text "", 21, 2 108 146 8,
  text "", 22, 2 116 146 8,
  text "", 23, 2 124 146 8,
  text "", 24, 2 132 146 8,
  text "", 25, 2 140 146 8,
}
dialog sokoerror {
  title Sokoban - Error
  option dbu
  size -1 -1 80 26
  icon $scriptdirimages\box.ico
  text "No such level.",1,2 2 76 8,center
  button "Ok",2,25 12 30 10,ok,result
}
dialog sokoseriesend {
  title Sokoban - Error
  option dbu
  size -1 -1 100 34
  icon $scriptdirimages\box.ico
  text "There are no more levels in this series.",1,2 2 96 8,center
  text "",2,2 10 96 8,center
  button "&Yes",3,20 20 30 10,ok,result
  button "&No",4,50 20 30 10,cancel
}
dialog sokogoto {
  title Sokoban - Go to level
  option dbu
  size -1 -1 80 30
  icon $scriptdirimages\box.ico
  text "Go to level..",1,2 4 32 8,
  combo 2,34 2 44 100,drop,edit,right,result
  button "&Ok",3,10 16 30 10,ok
  button "&Cancel",4,40 16 30 10,cancel
}
alias sokoban { 
  window -ak0pdhf @Sokoban -1 -1 440 372 
  window @sokoban iconfile $scriptdirimages\box.ico 
  drawpic -clsn @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,")
  if ($hget(sokoban)) { hfree sokoban } 
  hmake sokoban 100  
  if ($hget(sokolevel)) { hfree sokolevel } 
  hmake sokolevel 100 
  drawrect -nfr @sokoban $rgb(face) 1 0 352 440 20 
  drawline -nr @sokoban $rgb(3dlight) 1 0 352 440 352 
  drawline -nr @sokoban $rgb(hilight) 1 0 353 440 353 
  drawline -nr @sokoban $rgb(shadow) 1 57 356 0 356 0 372 
  drawline -nr @sokoban $rgb(3dlight) 1 1 371 57 371 57 356 
  drawline -nr @sokoban $rgb(3dlight) 1 439 356 439 371 320 371 | drawline -nr @sokoban $rgb(shadow) 1 320 371 320 356 440 356 
  drawline -nr @sokoban $rgb(3dlight) 1 316 356 316 371 196 371 | drawline -nr @sokoban $rgb(shadow) 1 197 371 197 356 317 356 | drawline -nr @sokoban $rgb(shadow) 1 193 356 61 356 61 372 | drawline -nr @sokoban $rgb(3dlight) 1 62 371 193 371 193 356 | hadd sokoban series 1 | hadd sokoban time 0 | .timer 1 0 sokoload 1-1 | hadd sokoban edit off 
  sokovercheck
}
alias -l sokosave { 
  hadd sokolevel pusher $hget(sokoban,pusher) 
  hadd sokolevel grid $hget(sokoban,grid) 
  hadd sokolevel level $hget(sokoban,series) $+ - $+ $hget(sokoban,level) 
  hadd sokolevel moves $hget(sokoban,moves) 
  hadd sokolevel pushes $hget(sokoban,pushes) 
  hadd sokolevel time $hget(sokoban,time) 
  var %i = 1,%g = $hget(sokoban,grid) 
  while ($hmatch(sokolevel,*,%i)) { 
    var %t = $ifmatch,%tg = $calc($gettok(%t,1,46) + 1) $+ . $+ $calc($gettok(%t,2,46) + 1) 
    if ($hget(sokolevel,%t) == $!scriptdirimages\pusher.bmp) { 
      hadd sokolevel $hmatch(sokolevel,*,%i) $!scriptdirimages\light.bmp 
    } 
    if ($istok(%g,%t,33)) && ($hget(sokolevel,%tg) != $!scriptdirimages\grid.bmp) { 
      hadd sokolevel %tg $iif($hget(sokolevel,%tg) == $!scriptdirimages\light.bmp,$!scriptdirimages\grid.bmp,$instok($hget(sokolevel,%tg),$!scriptdirimages\grid.bmp,1,33)) 
    } 
    inc %i 
  }
  var %pt = $instok($calc($gettok($hget(sokoban,pusher),1,32) + 1),$calc($gettok($hget(sokoban,pusher),2,32) + 1),2,46) 
  var %tt = $calc($gettok(%pt,1,46) - 1) $+ . $+ $calc($gettok(%pt,2,46) - 1),%ift = $istok(%g,%tt,33) 
  hadd sokolevel %pt $iif(%ift,$!scriptdirimages\grid.bmp $+ ! $+ $!scriptdirimages\pusher.bmp,$!scriptdirimages\pusher.bmp) 
  hsave -o sokolevel $+(",$1-,$iif($right($1-,4) != .sav,.sav),")
}
alias sokoload { 
  var %l = $iif($exists($scriptdirlevels\ $+ $gettok($1,1,45) $+ .info),y) 
  if (%l != y) { hadd sokoban type nt | hadd sokoban nt $1 } 
  else { hadd sokoban type reg } 
  if (!$1) { return } 
  if (!$exists($scriptdirlevels\ $+ $1 $+ .sok)) { var %e = $dialog(sokoerror,sokoerror,@sokoban) | return } 
  hfree sokolevel 
  hmake sokolevel 100 
  hdel sokoban grid 
  hadd sokoban time 0 
  hload sokolevel $+(",$scriptdirlevels\ $+ $1 $+ .sok,")
  hadd sokoban series $gettok($1,1,45) | hadd sokoban level $gettok($1,2,45) 
  sokotxt Level Level $gettok($1,2,45) 
  if (%l == y) { 
    titlebar @sokoban - $read($scriptdirlevels\ $+ $gettok($1,1,45) $+ .info,1) - Level $gettok($1,2,45) of $findfile($scriptdirlevels\,$gettok($1,1,45) $+ *-*.sok,0) 
  } 
  else { titlebar @sokoban - $1 $+ .sok }
  hdel sokoban pushes 
  hdel sokoban pusher 
  hdel sokoban moves 
  var %r = $readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)) 
  var %t = $gettok(%r,1,33) 
  sokotxt pushes 0 Pushes (Best: $iif(%t,%t,N\A) $+ ) 
  var %t = $gettok(%r,2,33) 
  sokotxt moves 0 Moves (Best: $iif(%t,%t,N\A) $+ ) 
  var %t = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)),3,33) 
  sokotxt time $sokohms($hget(sokoban,time)) (Best: $iif(%t,$sokohms(%t),N\A) $+ ) 
  .timercount -o 0 1 sokotime 
  drawpic -csl @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,") 
  drawdot @sokoban 
  sokodrawlevel 
}
alias -l sokodrawlevel {
  var %i = 1,%g = 1 
  drawpic -sl @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,") 
  while ($hmatch(sokolevel,*,%i)) { 
    var %p = $ifmatch 
    if ($gettok($hget(sokolevel,%p),1,33) == $!scriptdirimages\pusher.bmp) || ($gettok($hget(sokolevel,%p),1,33) == $!scriptdirimages\box.bmp) { 
      drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%p,1,46) - 1),$calc($gettok(%p,2,46) - 1)).draw $+(",$scriptdirimages\light.bmp,") 
    } 
    drawpic -ctr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%p,1,46) - 1),$calc($gettok(%p,2,46) - 1)).draw $+(",$eval($gettok($hget(sokolevel,%p),1,33),2),") 
    if ($gettok($hget(sokolevel,%p),1,33) == $!scriptdirimages\pusher.bmp) { 
      hadd sokoban pusher $calc($gettok(%p,1,46) - 1) $calc($gettok(%p,2,46) - 1) 
    }
    if ($gettok($hget(sokolevel,%p),1,33) == $!scriptdirimages\grid.bmp) { 
      var %t = $instok($calc($gettok(%p,1,46) - 1),$calc($gettok(%p,2,46) - 1),2,46) 
      hadd sokoban grid $addtok($hget(sokoban,grid),%t,33) 
      if ($gettok($hget(sokolevel,%p),2,33) == $!scriptdirimages\pusher.bmp) { 
        hadd sokoban pusher $calc($gettok(%p,1,46) - 1) $calc($gettok(%p,2,46) - 1) 
      } 
      if ($gettok($hget(sokolevel,%p),2,33)) { 
        drawpic -ctr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%p,1,46) - 1),$calc($gettok(%p,2,46) - 1)).draw $+(",$eval($gettok($hget(sokolevel,%p),2,33),2),") 
      } 
      if ($gettok($hget(sokolevel,%p),2,33) == $!scriptdirimages\box.bmp) { 
        hadd sokolevel %p $!scriptdirimages\box.bmp 
      } 
    } 
    inc %i 
  } 
  if ($hget(sokoundo)) { hfree sokoundo } 
}
alias -l sokomove {
  if ($hget(sokoban,edit) = on) { return }
  var %grid = $hget(sokoban,grid) 
  if ($hget(sokolevel,$instok($calc($3 + 1),$calc($4 + 1),2,46)) == $!scriptdirimages\border.bmp) { return } \
  if ($hget(sokolevel,$instok($calc($3 + 1),$calc($4 + 1),2,46)) == $!scriptdirimages\box.bmp) { 
    if ($hget(sokolevel,$instok($calc($3 - ($1 - $3) + 1),$calc($4 - ($2 - $4) + 1),2,46)) == $!scriptdirimages\box.bmp) || ($hget(sokolevel,$instok($calc($3 - ($1 - $3) + 1),$calc($4 - ($2 - $4) + 1),2,46)) == $!scriptdirimages\border.bmp) { return }
    var %t = $instok($3,$4,2,46) 
    drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($calc($3 - ($1 - $3)),$calc($4 - ($2 - $4))).draw $+(",$scriptdirimages\box.bmp,")
    if ($istok(%grid,%t,33)) { drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($3,$4).draw $+(",$scriptdirimages\grid.bmp,") } 
    else { drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($3,$4).draw $+(",$scriptdirimages\light.bmp,") } 
    hadd sokolevel $instok($calc($3 + 1),$calc($4 + 1),2,46) $!scriptdirimages\light.bmp 
    hadd sokolevel $instok($calc($3 - ($1 - $3) + 1),$calc($4 - ($2 - $4) + 1),2,46) $!scriptdirimages\box.bmp 
    var %ub = $instok($calc($3 + 1),$calc($4 + 1),2,46),%nb = $instok($calc($3 - ($1 - $3) + 1),$calc($4 - ($2 - $4) + 1),2,46) 
    hadd sokoban pushes $calc($hget(sokoban,pushes) + 1) 
    var %incd = 1 
    var %pt = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)),1,33) 
    sokotxt pushes $hget(sokoban,pushes) Pushes (Best $iif(%pt,%pt,N\A) $+ )
    if ($istok(%grid,$instok($calc($3 - ($1 - $3) - 0),$calc($4 - ($2 - $4) - 0),2,46),33)) && ($sokoboxes(torf)) { 
      var %mt = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,level)),2,33),%r = $readini(mirc.ini,sokoban,$hget(sokoban,level)) 
      if (%pt > $hget(sokoban,pushes)) || (!%pt) { var %pf = $hget(sokoban,pushes) } 
      else { var %pf = %pt } 
      if (%mt > $hget(sokoban,moves)) || (!%mt) { var %mf = $hget(sokoban,moves) } 
      else { var %mf = %mt } 
      var %st = $readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)) 
      if ($gettok(%st,3,33) > $hget(sokoban,time)) || (!$gettok(%st,3,33)) { var %tf = $hget(sokoban,time) } 
      else { var %tf = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)),3,33) } 
      writeini mirc.ini sokoban $hget(sokoban,series) $+ - $+ $hget(sokoban,level) $instok($instok(%pf,%mf,2,33),%tf,3,33) 
      .timer -m 1 10 sokobeat 
    }
  }
  var %t = $instok($1,$2,2,46) | if ($istok(%grid,%t,33)) { drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($calc($1),$calc($2)).draw $+(",$scriptdirimages\grid.bmp,") } | else { drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($calc($1),$calc($2)).draw $+(",$scriptdirimages\light.bmp,") } | drawpic -ctnr @sokoban $rgb(252,2,252) $sokogrid($3,$4).draw $+(",$scriptdirimages\pusher.bmp,") | hadd sokoban pusher $3 $4 | if (!%incd) { hadd sokoban moves $calc($hget(sokoban,moves) + 1) | var %mt = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)),2,33) | sokotxt moves $hget(sokoban,moves) Moves (Best: $iif(%mt,%mt,N\A) $+ ) } | sokoundos $1 $2 %ub %nb
  drawdot @sokoban
}
alias -l sokoundo {
  if (!$hget(sokoundo)) || (!$hmatch(sokoundo,*,0)) { return } | var %t = $gettok($hget(sokoundo,$hmatch(sokoundo,*,0)),1,33),%b = $gettok($hget(sokoundo,$hmatch(sokoundo,*,0)),3,33),%lb = $gettok($hget(sokoundo,$hmatch(sokoundo,*,0)),2,33),%tm = $hget(sokoban,pusher),%tmm = $gettok(%tm,1,32) $+ . $+ $gettok(%tm,2,32)
  drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($gettok(%t,1,46),$gettok(%t,2,46)).draw $+(",$scriptdirimages\pusher.bmp,") | if ($istok($hget(sokoban,grid),%tmm,33)) { drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($gettok(%tm,1,32),$gettok(%tm,2,32)).draw $+(",$scriptdirimages\grid.bmp,") } | else { drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($gettok(%tm,1,32),$gettok(%tm,2,32)).draw $+(",$scriptdirimages\light.bmp,") } | hadd sokoban pusher $gettok(%t,1,46) $gettok(%t,2,46) | hdel sokoundo $hmatch(sokoundo,*,0)
  if ($hget(sokolevel,%b)) { var %tb = $instok($calc($gettok(%b,1,46) - 1),$calc($gettok(%b,2,46) - 1),2,46) | drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%lb,1,46) - 1),$calc($gettok(%lb,2,46) - 1)).draw $+(",$scriptdirimages\box.bmp,") | if ($istok($hget(sokoban,grid),%tb,33)) { drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%b,1,46) - 1),$calc($gettok(%b,2,46) - 1)).draw $+(",$scriptdirimages\grid.bmp,") } | else { drawpic -tnr @sokoban $rgb(252,2,252) $sokogrid($calc($gettok(%b,1,46) - 1),$calc($gettok(%b,2,46) - 1)).draw $+(",$scriptdirimages\light.bmp,") } | hadd sokolevel %lb $!scriptdirimages\box.bmp | hadd sokolevel %b $!scriptdirimages\light.bmp } | drawdot @sokoban 
}
alias -l sokogrid { if ($prop == mouse) { return $calc($int($calc($1 / 22)) + 1) $calc($int($calc($2 / 22)) + 1) } | if ($prop == window) { return $calc($int($calc($1 / 22)) * 22) $calc($int($calc($2 / 22)) * 22) } | if ($prop == draw) { return $calc($1 * 22) $calc($2 * 22) } }
alias -l sokonext { if ($1) { if ($sokoext($calc($hget(sokoban,series) + 1) $+ -1)) { return $true } | else { return $false } }  | else { hadd sokoban series $calc($hget(sokoban,series) + 1) | hadd sokoban level 1 | sokoload $hget(sokoban,series) $+ - $+ $hget(sokoban,level) } }
alias -l sokoprev { if ($1) { if ($sokoext($calc($hget(sokoban,series) - 1) $+ -1)) { return $true } | else { return $false } }  | else { hadd sokoban series $calc($hget(sokoban,series) - 1) | hadd sokoban level 1 | sokoload $hget(sokoban,series) $+ - $+ $hget(sokoban,level) } }
alias -l sokoloadsave { 
  hfree sokolevel
  hmake sokolevel 100
  hload sokolevel $+(",$1-,") 
  hadd sokoban moves $hget(sokolevel,moves) 
  hadd sokoban pushes $hget(sokolevel,pushes) 
  hadd sokoban grid $hget(sokolevel,grid) 
  hadd sokoban level $gettok($hget(sokolevel,level),2,$asc(-)) 
  hadd sokoban pusher $hget(sokolevel,pusher) 
  hadd sokoban time $hget(sokolevel,time) 
  hadd sokoban series $gettok($hget(sokolevel,level),1,$asc(-))
  hdel sokolevel moves 
  hdel sokolevel pushes 
  hdel sokolevel grid 
  hdel sokolevel level 
  hdel sokolevel pusher 
  hdel sokolevel time 

  var %s = $hget(sokoban,series)
  var %sn = $read($+(",$scriptdirlevels\,%s,.info,"),1)

  titlebar @sokoban - %sn - Level $hget(sokoban,level) of $findfile($scriptdirlevels\,$hget(sokoban,series) $+ *-*.sok,0)

  drawpic -csl @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,") 
  var %t = $readini(mirc.ini,sokoban,$hget(sokoban,level)) 
  sokotxt pushes $hget(sokoban,pushes) Pushes (Best: $iif($gettok(%t,1,33),$ifmatch,N\A) $+ ) 
  sokotxt moves $hget(sokoban,moves) Moves (Best: $iif($gettok(%t,2,33),$ifmatch,N\A) $+ ) 
  sokodrawlevel 
}
alias -l sokotxt { if ($1 == level) { drawrect -nfr @sokoban $rgb(face) 1 1 357 56 14 | drawtext -nr @sokoban $rgb(text) "arial" -8 $calc((56 / 2) - ($width($2-,arial,-8) / 2)) 357 $2- } | if ($1 == pushes) { drawrect -nfr @sokoban $rgb(face) 1 322 357 117 14 | drawtext -nr @sokoban $rgb(text) "arial" -8 $calc((117 / 2) - ($width($2-,arial,-8) / 2) + 322) 357 $2- } | if ($1 == moves) { drawrect -nfr @sokoban $rgb(face) 1 199 357 117 14 | drawtext -nr @sokoban $rgb(text) "arial" -8 $calc((117 / 2) - ($width($2-,arial,-8) / 2) + 199) 357 $2- } | if ($1 == time) { drawrect -nfr @sokoban $rgb(face) 1 62 357 130 14 | drawtext -nr @sokoban $rgb(text) "arial" -8 $calc((132 / 2) - ($width($2-,arial,-8) / 2) + 62) 357 $2- } }
alias -l sokotime { hadd sokoban time $calc($hget(sokoban,time) + 1) | var %t = $gettok($readini(mirc.ini,sokoban,$hget(sokoban,series) $+ - $+ $hget(sokoban,level)),3,33) | sokotxt time $sokohms($hget(sokoban,time)) (Best: $iif(%t,$sokohms(%t),N\A) $+ ) | drawdot @sokoban }
alias -l sokoext { if ($exists($scriptdirlevels\ $+ $1 $+ .sok)) { return $true } | else return $false }
alias -l sokobeat { if ($hget(sokoban,type) == nt) { return } | if ($sokoext($hget(sokoban,series) $+ - $+ $calc($hget(sokoban,level) + 1))) { sokoload $sokoup } | elseif ($sokoext($calc($hget(sokoban,series) + 1) $+ -1)) { var %e = $dialog(sokoseriesend,sokoseriesend) | if (%e) { sokoload $calc($hget(sokoban,series) + 1) $+ -1 } } | else { var %e = $dialog(sokoend,sokoseriesend) | if (%e) { sokoload 1-1 } } }
alias -l sokoundos { if (!$hget(sokoundo)) { hmake sokoundo 200 } | hadd sokoundo $calc($hmatch(sokoundo,*,0) + 1) $instok($1,$2,2,46) $+ $iif($3,!) $+ $iif($3,$3) $+ $iif($4,!) $+ $iif($4,$4) }
alias -l sokoup { 
  var %f = $hget(sokoban,series) $+ - $+ $calc($hget(sokoban,level) + 1) 
  if ($sokoext(%f)) { 
    hadd sokoban level $calc($hget(sokoban,level) + 1) 
    return %f 
  } 
  else { return } 
}
alias -l sokodown { 
  var %f = $hget(sokoban,series) $+ - $+ $calc($hget(sokoban,level) - 1) 
  if (%f == 1-0) || ($gettok(%f,2,45) == 0) { return } 
  if ($sokoext(%f)) { 
    hadd sokoban level $calc($hget(sokoban,level) - 1) 
    return %f 
  } 
  else { return } 
}
alias -l sokopos { return $hget(sokoban,pusher) }
alias -l sokoboxes { var %i = 1,%f | while ($gettok($hget(sokoban,grid),%i,33)) { tokenize 46 $ifmatch | var %t = $calc($1 + 1) $+ . $+ $calc($2 + 1) | if ($hget(sokolevel,%t) == $!scriptdirimages\box.bmp) || ($gettok($hget(sokolevel,%t),2,33) == $!scriptdirimages\box.bmp) { inc %f } | inc %i } | if (%f == $numtok($hget(sokoban,grid),33)) { return $true } | else { return $false } }
alias -l sokohms { if ($1 == $null) return 00:00:00 | if ($1 == 0) return 00:00:00 | var %s = $calc($1 % 60) | var %h = $int($calc($1 / 60)) | var %m = $calc(%h % 60) | var %h = $int($calc(%h / 60)) | if (%h < 10) var %h = 0 $+ %h | if (%m < 10) var %m = 0 $+ %m  | if (%s < 10) var %s = 0 $+ %s | return %h $+ : $+ %m $+ : $+ %s }
on 1:dialog:sokoabout:init:0:{ var %i = 1,%id = 11 | while ($exists($scriptdirlevels\ $+ %i $+ .info)) { did -ra sokoabout %id $read($scriptdirlevels\ $+ %i $+ .info,1) levels by $read($scriptdirlevels\ $+ %i $+ .info,2) | inc %i | inc %id } | dialog -s sokoabout $dialog(sokoabout).x $dialog(sokoabout).y $dialog(sokoabout).w $calc(%i * 16 + 44) | dialog -r sokoabout }
on 1:dialog:sokoabout:sclick:7:{ run mailto:praetorian@detox.xs3.com }
on 1:dialog:sokoabout:sclick:9:{ run http://www.detox.xs3.com }
on 1:dialog:sokoseriesend:init:0:{ did -a $dname 2 Go to the next series? }
on 1:dialog:sokoend:init:0:{ did -a $dname 2 Restart from the beginning? }
on 1:dialog:sokogoto:init:0:{ var %i = 1 | while ($exists($scriptdirlevels\ $+ $hget(sokoban,series) $+ - $+ %i $+ .sok)) { did -a sokogoto 2 %i | inc %i } | did -c sokogoto 2 $hget(sokoban,level) }
on 1:keydown:@sokoban:*:{ 
  if ($keyval == 13) { sokosave $$file="Save game" $scriptdirsaves\*.sav }
  if ($keyval == 16) { sokoloadsave $$file="Choose game" $scriptdirsaves\*.sav }
  if ($keyval == 17) { sokoload $nopath($remove($$file="Load level.." $scriptdirlevels\*.sok,.sok)) } 
  if ($keyval == 38) { tokenize 32 $sokopos | sokomove $1 $2 $1 $calc($2 - 1) | return } 
  if ($keyval == 40) { tokenize 32 $sokopos | sokomove $1 $2 $1 $calc($2 + 1) | return } 
  if ($keyval == 37) { tokenize 32 $sokopos | sokomove $1 $2 $calc($1 - 1) $2 | return } 
  if ($keyval == 39) { tokenize 32 $sokopos | sokomove $1 $2 $calc($1 + 1) $2 | return } 
  if ($keyval == 36) && (!$keyrpt) { 
    if ($hget(sokoban,type) == nt) { sokoload $hget(sokoban,nt) | return } 
    sokoload $hget(sokoban,series) $+ - $+ $hget(sokoban,level) 
  } 
  if ($keyval == 33) && (!$keyrpt) { sokoload $sokoup } 
  if ($keyval == 34) && (!$keyrpt) { sokoload $sokodown } 
  if ($keyval == 8) { sokoundo } 
  if ($keyval == 46) && ($sokoprev(k)) { sokoprev } 
  if ($keyval == 45) && ($sokonext(k)) { sokonext } 
  if ($keyval == 35) { var %l = $dialog(sokogoto,sokogoto,@sokoban) 
    if (%l) { sokoload $hget(sokoban,series) $+ - $+ %l } 
  } 
}
on 1:close:@sokoban:{ .timercount off | hfree sokolevel | if ($hget(sokoverinfo)) { hfree sokoverinfo } | hfree sokoban }
menu @sokoban {
  mouse:{ if (!$hget(sokoban,draw)) { return } | if ($mouse.key & 1) && ($hget(sokoban,edit) == on) && ($mouse.y < 350) { drawpic -tr @sokoban $rgb(252,2,252) $sokogrid($mouse.x,$mouse.y).window $+(",$hget(sokoban,draw),") | var %t = $hget(sokolevel,$instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46)) | if (%t) && (%t == $scriptdirimages\grid.bmp) && ($istok($scriptdirimages\pusher.bmp $+ ! $+ $scriptdirimages\box.bmp,$hget(sokoban,draw),33)) { hadd sokolevel $instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46) $scriptdirimages\grid.bmp $+ ! $+ $hget(sokoban,draw) } | else { hadd sokolevel $instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46) $hget(sokoban,draw) } } }
  sclick:{ if (!$hget(sokoban,draw)) { return } | if ($mouse.key & 1) && ($hget(sokoban,edit) == on) && ($mouse.y < 350) { drawpic -tr @sokoban $rgb(252,2,252) $sokogrid($mouse.x,$mouse.y).window $+(",$hget(sokoban,draw),") | var %t = $hget(sokolevel,$instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46)) | if (%t) && (%t == $scriptdirimages\grid.bmp) && ($istok($scriptdirimages\pusher.bmp $+ ! $+ $scriptdirimages\box.bmp,$hget(sokoban,draw),33)) { hadd sokolevel $instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46) $scriptdirimages\grid.bmp $+ ! $+ $hget(sokoban,draw) } | else { hadd sokolevel $instok($gettok($sokogrid($mouse.x,$mouse.y).mouse,1,32),$gettok($sokogrid($mouse.x,$mouse.y).mouse,2,32),2,46) $hget(sokoban,draw) } } }
  $iif($hget(sokoban,edit) == off,Undo (Backspace)):sokoundo
  -
  $iif($hget(sokoban,edit) == off,Restart level (Home)):if ($hget(sokoban,type) == nt) { sokoload $hget(sokoban,nt) | return } | sokoload $hget(sokoban,series) $+ - $+ $hget(sokoban,level)
  $iif($hget(sokoban,edit) == off,$iif(!$sokoext($hget(sokoban,series) $+ - $+ $calc($hget(sokoban,level) + 1)),$style(2)) Next level (Page up)):sokoload $sokoup
  $iif($hget(sokoban,edit) == off,$iif(!$sokoext($hget(sokoban,series) $+ - $+ $calc($hget(sokoban,level) - 1)),$style(2)) Previous level (Page down)):sokoload $sokodown
  $iif($hget(sokoban,edit) == off,$iif($hget(sokoban,type) == nt,$style(2)) Go to level.. (End)):var %l = $dialog(sokogoto,sokogoto,@sokoban) | if (%l) { sokoload $hget(sokoban,series) $+ - $+ %l }
  $iif($hget(sokoban,edit) == off,Load level.. (Ctrl)):sokoload $nopath($remove($$file="Load level.." $scriptdirlevels\*.sok,.sok)) 
  -
  $iif($hget(sokoban,edit) == off,$iif(!$sokonext(p),$style(2)) Next series (Insert)):sokonext
  $iif($hget(sokoban,edit) == off,$iif(!$sokoprev(p),$style(2)) Previous series (Delete)):sokoprev
  $iif($hget(sokoban,edit) == off,Download more levels):sokocheck
  -
  $iif($hget(sokoban,edit) == off,Save game (Enter)):sokosave $$file="Save game" $scriptdirsaves\*.sav
  $iif($hget(sokoban,edit) == off,Load game (Shift)):sokoloadsave $$file="Choose game" $scriptdirsaves\*.sav
  -
  $iif($hget(sokoban,edit) == on,Exit edit mode,Switch to edit mode):if ($hget(sokoban,edit) == on) { .timer 1 0 sokoload 1-1 | hadd sokoban edit off | if ($dialog(sokoedit)) { dialog -x sokoedit } | if ($hget(sokoban,filename)) { hdel sokoban filename } | return } | hadd sokoban edit on | .timercount off | sokotxt level Level N\A | sokotxt moves 0 Moves (Best: N\A) | sokotxt pushes 0 Pushes (Best: N\A) | sokotxt time 00:00:00 (Best: N\A) | titlebar @sokoban - Editing level - $hget(sokoban,series) $+ - $+ $hget(sokoban,level) $+ .sok | drawdot @sokoban | sokoedit
  -
  $iif($hget(sokoban,edit) == off,About):dialog -mar sokoabout sokoabout
}

;------------------------------------------------------------------------------------------------------------------
; level updating dialogs and routines
;------------------------------------------------------------------------------------------------------------------

alias -l sokocheck { sockclose sokoban | if (!$dialog(sokocheck)) { dialog -ma sokocheck sokocheck } | sockopen sokoban www.interl.net 80  | sockmark sokoban http://www.interl.net/~jpdillin/mirc_stuff/sokoban/sokoban_levels.txt | did -ra sokocheck 1 Connecting.. | .timersokotimeout 1 60 sokotimeout }
alias -l sokotimeout { sockclose sokoban | did -ra sokocheck 1 Connection timed out. }
alias -l sokostartdl { if ($exists($scriptdirlevels\ $+ $1 $+ .info)) { %sokocur = $1 | var %e = $dialog(sokodupdl,sokodupdl,@sokoban) | if (!%e) { return } } | sokodl $1 }
alias -l sokodl { sockclose sokodl | sockopen sokodl www.interl.net 80 | sockmark sokodl http://www.interl.net/~jpdillin/mirc_stuff/sokoban/ $+ $1 $+ .cab | if (!$dialog(sokodl)) dialog -am sokodl sokodl | if ($exists($scriptdirlevels\ $+ $1 $+ .cab)) { .remove $scriptdirlevels\ $+ $1 $+ .cab } | .timersokotimeout 1 60 sokotimeoutdl }
alias -l sokotimeoutdl { sockclose sokodl | if ($dialog(sokodl)) { did -ra sokodl 1 Connection timed out.. } }
alias -l sokoinstall { if ($os == 95) || ($os == 98) { run -n command.com /c extract $1 /e /l $scriptdirlevels\ } | else { run -n command.com /c expand $1 -F:* $scriptdirlevels\ } | .timer -m 1 1500 did -ra sokodl 1 Finished. | .timer -m 1 2000 did -era sokodl 3 Close | .timer 1 3 dialog -x sokodl }

dialog sokocheck {
  title Sokoban - Download more levels
  size -1 -1 134 89
  option dbu
  icon $scriptdirimages\box.ico
  edit "",1,2 64 130 10,read
  list 2,2 2 130 70,disable
  button "Download selected levels",3,2 76 70 10,disable
  button "Refresh",4,72 76 30 10
  button "Cancel",5,102 76 30 10,cancel
}
dialog sokodupdl {
  title Sokoban - Error
  option dbu
  size -1 -1 130 34
  icon $scriptdirimages\box.ico
  text "",1,2 2 126 8,center
  text "Do you want to overwrite them?",2,2 10 126 8,center
  button "&Yes",3,35 20 30 10,ok,result
  button "&No",4,65 20 30 10,cancel
}
dialog sokodl {
  title Sokoban - Downloading..
  option dbu
  size -1 -1 120 34
  icon $scriptdirimages\box.ico
  button "ok",100,0 0 0 0,ok
  text "",1,2 10 116 8,center
  text "",2,2 2 116 8,center
  button "Cancel",3,45 20 30 10,cancel
}
on 1:dialog:sokodupdl:init:0:{ did -ra sokodupdl 1 $read($scriptdirlevels\ $+ %sokocur $+ .info,1) levels are already installed. }
on 1:dialog:sokocheck:sclick:2:{ did -b sokocheck 3 }
on 1:dialog:sokocheck:sclick:5:{ if ($sock(sokoban)) { sockclose sokoban } | .timersokotimeout off }
on 1:dialog:sokocheck:sclick:4:{ did -rb sokocheck 2 | did -b sokocheck 3 | sokocheck 1 | did -ra sokocheck 5 Cancel }
on 1:dialog:sokocheck:sclick:3:{ sokostartdl $did(sokocheck,2,1).sel }
on 1:dialog:sokodl:init:0:{ did -ra sokodl 1 Connecting.. | var %t = $gettok($gettok($sock(sokodl).mark,5,47),1-,46) | did -ra sokodl 2 $gettok($did(sokocheck,2,%t),1,40) }
on 1:dialog:sokodl:sclick:3:{ sockclose sokodl | unset %sokoheader }
on 1:sockopen:sokoban:{ 
  if ($sockerr) { did -ra sokocheck 1 Error.. $sock($sockname).wsmsg } 
  else { 
    .timersokotimeout off 
    did -ra sokocheck 1 Connected, waiting for data.. 
    sockwrite -n sokoban GET $sock($sockname).mark HTTP/1.1
    sockwrite -n sokoban Accept: */*  
    sockwrite -n sokoban Accept-Encoding: deflate 
    sockwrite -n sokoban User-Agent: mIRC $version 
    sockwrite -n sokoban connection: close
    sockwrite -n sokoban Host: $host 
    sockwrite -n sokoban $crlf
    .timersokotimeout 1 30 sokotimeout 
    %sokoread = $false 
  } 
}

on 1:sockclose:sokoban:{ }
on 1:sockread:sokoban:{ 
  if ($sockerr) { 
    did -ra sokocheck 1 Error.. $sock($sockname).wsmsg 
  } 
  .timersokotimeout off 
  :read 
  var %tmp 
  sockread %tmp 
  tokenize 32 %tmp 
  if ($1 == levels) { %sokoread = ok | did -ra sokocheck 1 Receiving data.. | return } 
  if ($1 == /levels) { unset %sokoread | did -ra sokocheck 1 Finished. | did -e sokocheck 2 | did -ra sokocheck 5 Close | sockclose sokoban | return }
  if ($1) && (%sokoread) { did -a sokocheck 2 $gettok($1-,2,61) ( $+ $gettok($1-,4,61) levels) } 
  if ($sockbr) goto read 
} 
on 1:sockopen:sokodl:{ if ($sockerr) { did -ra sokodl 1 Error.. $sock($sockname).wsmsg } | else { .timersokotimeout off | did -ra sokodl 1 Connected, waiting for data.. | sockwrite -n sokodl GET $sock($sockname).mark HTTP/1.1  | sockwrite -n sokodl Accept: */* | sockwrite -n sokodl Accept-Encoding: deflate | sockwrite -n sokodl User-Agent: mIRC $version | sockwrite -n sokodl Host: $host | sockwrite -n sokodl $lf | .timersokotimeout 1 25 sokotimeoutdl | %sokoheader = ok } }
on 1:sockread:sokodl:{ 
  if (!$dialog(sokodl)) { sockclose $sockname | return } 
  if ($sockerr) { did -ra sokodl 1 Error.. $sock($sockname).wsmsg } 
  .timersokotimeout off 
  :read 
  if (%sokoheader) { var %tmp | sockread %tmp | tokenize 32 %tmp | if ($1-) { if ($1 == Content-Length:) { %sokosize = $2- } | if ($sockbr) { goto read } } 
  else { unset %sokoheader | %sokogot = $sock($sockname).rcvd } } 
  else { sockread 4096 &bvar | bwrite $scriptdirlevels\ $+ $gettok($sock($sockname).mark,3,47) -1 -1 &bvar | did -ra sokodl 1 Downloading.. $lower($bytes($calc($sock($sockname).rcvd - %sokogot),k).suf) of $lower($bytes(%sokosize,k).suf) ( $+ $round($calc((($sock($sockname).rcvd - %sokogot) / %sokosize) * 100),0) $+ $chr(37) $+ ) | if ($calc($sock($sockname).rcvd - %sokogot) == %sokosize) { unset %sokosize | unset %sokogot | unset %sokoheader | unset %sokocur | .timer -m 1 1000 did -ra sokodl 1 Installing.. | did -b sokodl 3 | .timer -m 1 2000 sokoinstall $scriptdirlevels\ $+ $gettok($sock($sockname).mark,3,47) | sockclose $sockname } | if ($sockbr) { goto read } } 
}
;------------------------------------------------------------------------------------------------------------------
; level editor dialogs and routines
;------------------------------------------------------------------------------------------------------------------

dialog sokoedit {
  title Sokoban - Level editor
  option dbu
  size $calc($window(@sokoban).dx + ($window(@sokoban).w - 213)) $calc($window(@sokoban).dy + ($window(@sokoban).h - $window(@sokoban).dh)) 100 11
  icon $scriptdirimages\box.ico
  button "ok",100,0 0 0 0,ok
  button "cancel",101,0 0 0 0,cancel
  icon 1,0 -1 12 12,$scriptdirimages\new-dialog.bmp,noborder
  icon 2,10 -1 12 12,$scriptdirimages\open-dialog.bmp,noborder
  icon 3,20 -1 12 12,$scriptdirimages\save-dialog.bmp,noborder
  icon 4,34 -1 6 12,$scriptdirimages\spcr.bmp,noborder
  icon 5,36 -1 12 12,$scriptdirimages\tpusher-dialog.bmp,noborder
  icon 6,46 -1 12 12,$scriptdirimages\tbox-dialog.bmp,noborder
  icon 7,56 -1 12 12,$scriptdirimages\tgrid-dialog.bmp,noborder
  icon 8,66 -1 12 12,$scriptdirimages\tlight-dialog.bmp,noborder
  icon 9,76 -1 12 12,$scriptdirimages\tborder-dialog.bmp,noborder
  icon 10,86 -1 12 12,$scriptdirimages\tdark-dialog.bmp,noborder
  icon 11,36 -1 12 12,$scriptdirimages\tpusher-dialog-pressed.bmp,noborder,hide
  icon 12,46 -1 12 12,$scriptdirimages\tbox-dialog-pressed.bmp,noborder,hide
  icon 13,56 -1 12 12,$scriptdirimages\tgrid-dialog-pressed.bmp,noborder,hide
  icon 14,66 -1 12 12,$scriptdirimages\tlight-dialog-pressed.bmp,noborder,hide
  icon 15,76 -1 12 12,$scriptdirimages\tborder-dialog-pressed.bmp,noborder,hide
  icon 16,86 -1 12 12,$scriptdirimages\tdark-dialog-pressed.bmp,noborder,hide
}
dialog sokosavec {
  title Sokoban - Level editor
  option dbu
  size -1 -1 80 26
  icon $scriptdirimages\box.ico
  text "",1,2 2 76 8,center
  button "Ok",2,25 12 30 10,ok,result
}
dialog sokocomperr {
  title Sokoban - Error
  option dbu
  size -1 -1 100 42
  icon $scriptdirimages\box.ico
  text "Error compiling level:",1,2 2 96 8,center
  text "",2,2 10 96 8,center
  text "",3,2 18 96 8,center
  button "Ok",4,35 28 30 10,ok,result
}
dialog sokosaveex {
  title Sokoban - Error
  option dbu
  size -1 -1 100 34
  icon $scriptdirimages\box.ico
  text "",1,2 2 96 8,center
  text "Do you want to overwrite it?",2,2 10 96 8,center
  button "&Yes",3,20 20 30 10,ok,result
  button "&No",4,50 20 30 10,cancel
}
dialog sokoloaderr {
  title Sokoban - Error
  option dbu
  size -1 -1 80 26
  icon $scriptdirimages\box.ico
  text "",1,2 2 76 8,center
  button "Ok",2,25 12 30 10,ok,result
}
alias -l sokosaveedit { 
  :start 
  var %fn = $$file="Save level as.." $scriptdirlevels\*.sok 
  if ($gettok($nopath(%fn),0,46) < 2) { var %fn = %fn $+ .sok } 
  if ($nofile(%fn) != $scriptdirlevels\) { 
    hadd sokoban errormsg Invalid directory. 
    var %e = $dialog(sokoloaderr,sokoloaderr,@sokoban) 
    return 
  } 
  if ($gettok($nopath(%fn),-1,46) != sok) { 
    hadd sokoban errormsg Invalid file extention. 
    var %e = $dialog(sokoloaderr,sokoloaderr,@sokoban) 
    return 
  } 
  if ($exists(%fn)) { 
    hadd sokoban errormsg The file ' $+ $nopath(%fn) $+ ' already exists. 
    var %e = $dialog(sokosaveex,sokosaveex,@sokoban) 
    if (!%e) { goto start } 
  } 
  if ($sokocompile == $true) { 
    hsave -o sokolevel $+(",%fn,") 
    .timer 1 0 sokofix %fn 
    titlebar @sokoban - Editing level - $nopath(%fn) 
    hadd sokoban filename $nopath(%fn) 
    hadd sokoban errormsg Level successfully saved. 
  dialog -am sokosavec sokosavec } 
  if ($sokocompile != $true) { 
    hadd sokoban errormsg $ifmatch 
    dialog -am sokocomperr sokocomperr 
    return 
  }
}
alias -l sokodeselall { var %i = 5 | while (%i < 11) { if ($did(sokoedit,$calc(%i + 6)).visible) { did -h sokoedit $calc(%i + 6) | did -v sokoedit %i } | inc %i } }
alias -l sokomakespcr { 
  window -ak0ph +d @sokospcr -1 -1 6 20 
  drawfill -r @sokospcr $rgb(face) $rgb(face) 0 0 6 20 
  drawline -r @sokospcr $rgb(shadow) 1 0 1 0 18 
  drawline -r @sokospcr $rgb(3dlight) 1 1 1 1 19 
  drawdot -r @sokospcr $rgb(3dlight) 1 0 18 
  drawsave @sokospcr $+(",$scriptdirimages\spcr.bmp,") 
  window -c @sokospcr 
}
alias -l sokomakebtn { 
  window -ak0ph +d @sokobtn -1 -1 20 20 
  drawfill -r @sokobtn $rgb(face) $rgb(face) 0 0 20 20 
  drawpic -tr @sokobtn $rgb(255,0,255) 0 0 $+(",$scriptdirimages\ $+ $1,") 
  var %fn = -dialog.bmp 
  if ($2 == pressed) { 
    drawline -r @sokobtn $rgb(shadow) 1 20 0 0 0 0 20 
    drawline -r @sokobtn $rgb(3dlight) 1 19 1 19 19 1 19 
    var %fn = -dialog-pressed.bmp 
  } 
  drawsave @sokobtn $+(",$scriptdirimages\ $+ $gettok($1,1,46) $+ %fn,")
  window -c @sokobtn 
} 
alias -l sokoedit { sokomakespcr | sokomakebtn new.bmp | sokomakebtn open.bmp | sokomakebtn save.bmp | sokomakebtn tpusher.bmp | sokomakebtn tpusher.bmp pressed | sokomakebtn tbox.bmp | sokomakebtn tbox.bmp pressed | sokomakebtn tgrid.bmp | sokomakebtn tgrid.bmp pressed | sokomakebtn tlight.bmp | sokomakebtn tlight.bmp pressed | sokomakebtn tborder.bmp | sokomakebtn tborder.bmp pressed | sokomakebtn tdark.bmp | sokomakebtn tdark.bmp pressed | dialog -ma sokoedit sokoedit }
alias -l sokoloadedit { 
  hfree sokolevel 
  hmake sokolevel 100 
  hload sokolevel $+(",$scriptdirlevels\ $+ $1,") 
  titlebar @sokoban - Editing level - $1 
  sokodrawlevel 
}
alias -l sokocompile { var %i = 1,%p,%b,%g | while ($hmatch(sokolevel,*,%i)) { var %t = $hget(sokolevel,$ifmatch) | if (pusher isin %t) { inc %p } | if (grid isin %t) { inc %g } | if (box isin %t) { inc %b } | inc %i } | if (%p > 1) { return More than one pusher. } | if (%g > %b) { return Not enough boxes. !( $+ %b boxes, %g grid squares). } | if (%g < %b) { return Not enough grid squares. !( $+ %g grid squares, %b boxes). } | else { return $true } }
alias -l sokofix { 
  if (!$hget(temp)) { hmake temp 100 } 
  if ($hget(temp)) { hfree temp | hmake temp 100 } 
  hload temp $+(",$1-,") 
  var %i = 1 
  while ($hmatch(temp,*,%i)) { 
    var %t = $ifmatch 
    hadd temp %t $replace($hget(temp,%t),$scriptdir,$!scriptdir) 
    inc %i 
  } 
  hsave -o temp $+(",$1-,") 
}
on 1:dialog:sokoedit:init:0:{ did -vh sokoedit 8,14 | hadd sokoban draw $scriptdirimages\light.bmp }
on 1:dialog:sokoedit:sclick:1:{ hfree sokolevel | hmake sokolevel 100 | .timercount off | sokotxt level Level N\A | sokotxt moves 0 Moves (Best: N\A) | sokotxt pushes 0 Pushes (Best: N\A) | sokotxt time 00:00:00 (Best: N\A) | drawpic -sl @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,") | titlebar @sokoban - Editing level - Untitled.sok | drawdot @sokoban }
on 1:dialog:sokoedit:sclick:2:{ var %t = $$file="Select level" $mircdirsokoban\levels\*.sok | if ($gettok(%t,-1,46) != sok) { hadd sokoban errormsg Invalid filename. | dialog -ma sokoloaderr sokoloaderr | return } | if ($nofile(%t) != $scriptdirlevels\) { hadd sokoban errormsg Invalid directory. | dialog -ma sokoloaderr sokoloaderr | return } | drawpic -sl @sokoban 0 0 440 352 $+(",$scriptdirimages\dark.bmp,") | .timer 1 0 sokoloadedit $nopath(%t) }
on 1:dialog:sokoedit:sclick:3:{ sokosaveedit }
on 1:dialog:sokoedit:sclick:5:{ if (pusher isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 5,11 | hadd sokoban draw $scriptdirimages\pusher.bmp }
on 1:dialog:sokoedit:sclick:6:{ if (box isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 6,12 | hadd sokoban draw $scriptdirimages\box.bmp }
on 1:dialog:sokoedit:sclick:7:{ if (grid isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 7,13 | hadd sokoban draw $scriptdirimages\grid.bmp }
on 1:dialog:sokoedit:sclick:8:{ if (light isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 8,14 | hadd sokoban draw $scriptdirimages\light.bmp }
on 1:dialog:sokoedit:sclick:9:{ if (border isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 9,15 | hadd sokoban draw $scriptdirimages\border.bmp }
on 1:dialog:sokoedit:sclick:10:{ if (dark isin $hget(sokoban,draw)) { return } | sokodeselall | did -vh sokoedit 10,16 | hadd sokoban draw $scriptdirimages\dark.bmp }
on 1:dialog:sokoedit:*:101:{ .timer 1 0 sokoload 1-1 | hadd sokoban edit off | if ($hget(sokoban,filename)) { hdel sokoban filename } }
on 1:dialog:sokosavec:init:0:{ did -ra sokosavec 1 $hget(sokoban,errormsg) }
on 1:dialog:sokocomperr:sclick:4:{ hadd sokoban errormsg Level was not saved. | .timer 1 0 dialog -am sokosavec sokosavec }
on 1:dialog:sokocomperr:init:0:{ did -ra sokocomperr 2 $gettok($hget(sokoban,errormsg),1,33) | if ($gettok($hget(sokoban,errormsg),2,33)) { did -ra sokocomperr 3 $ifmatch } }
on 1:dialog:sokosaveex:init:0:{ did -ra sokosaveex 1 $hget(sokoban,errormsg) }
on 1:dialog:sokoloaderr:init:0:{ did -ra sokoloaderr 1 $hget(sokoban,errormsg) }

;------------------------------------------------------------------------------------------------------------------
; script updating dialogs and routines
;------------------------------------------------------------------------------------------------------------------

alias sokoversion return 1.1
alias sokovercheck {
  if ($sock(sokovercheck)) { sockclose sokovercheck }
  sockopen sokovercheck www.interl.net 80
}
on 1:sockopen:sokovercheck:{
  if ($sockerr) { return } 
  sockwrite -n sokovercheck GET http://www.interl.net/~jpdillin/mirc_stuff/sokoban/sokoban.txt HTTP/1.0 
  sockwrite -n sokovercheck Accept: */*  
  sockwrite -n sokovercheck Accept-Encoding: deflate 
  sockwrite -n sokovercheck User-Agent: mIRC $version 
  sockwrite -n sokovercheck Connection: close
  sockwrite -n sokovercheck Host: $host 
  sockwrite -n sokovercheck $crlf 
}
on 1:sockread:sokovercheck:{
  if ($sockerr) { return }
  :read
  var %tmp
  sockread %tmp
  tokenize 32 %tmp
  if ($1 == version:) { 
    if ($2 > $sokoversion) { 
      if ($hget(sokoverinfo)) { hfree sokoverinfo } 
      hmake sokoverinfo 5 
      hadd sokoverinfo version $2 
      hadd sokoverinfo line 1 
    } 
    else { sockclose sokovercheck | return } 
  }
  if ($1 = -) && ($hget(sokoverinfo)) { 
    hadd sokoverinfo $addtok(line,$hget(sokoverinfo,line),46) $1- 
    hinc sokoverinfo line 
  }
  if ($1 = EOF) { sockclose sokovercheck | sokonewversion | return }
  if ($sockbr) { goto read }
}
alias -l sokonewversion {
  if ($dialog(sokonewversion,sokonewversion,@sokoban)) { sokodlnewversion }
  else { return }
}
dialog sokonewversion {
  title "Sokoban - New version available"
  size -1 -1 150 103
  option dbu
  icon $scriptdirimages\box.ico,0
  text "Your version:",1,2 2 73 8,center
  text "New version:",2,73 2 74 8,center
  text "Would you like to download the new version now?",3,2 80 146 8,center
  box "New features and bugfixes",4,2 12 146 66
  button "Yes",5,43 90 30 10,result ok
  button "No",6,76 90 30 10,cancel
  text "", 7,4 20 140 8
  text "", 8,4 28 140 8
  text "", 9,4 36 140 8
  text "", 10,4 44 140 8
  text "", 11,4 52 140 8
  text "", 12,4 60 140 8
  text "", 13,4 68 140 8
}
on 1:dialog:sokonewversion:init:0:{
  did -ra sokonewversion 2 New version: $hget(sokoverinfo,version)
  did -ra sokonewversion 1 Your version: $sokoversion
  var %i = 1
  while ($hmatch(sokoverinfo,$+(line.,%i),1)) {
    did -a sokonewversion $calc(%i + 6) $hget(sokoverinfo,$ifmatch) $lf
    inc %i
  }
}
on 1:dialog:sokonewversion:sclick:5:{ dialog -x sokonewversion | run http://www.interl.net/~jpdillin/mirc_stuff/sokoban/sokoban.zip }
