on *:unload: { unset %to.* }
}
alias to.start { $iif($dialog(to.table),dialog -x to.table,dialog -m to.table to.table) }

dialog to.table {
title "                     ..::: DiabLo :::.."
size -1 -1 327 300
icon C:\Windows\System32\shell32.dll,200
box "Твои очки:",1,10 10 80 90
button %to.g.m,2,18 30 65 60,disable
box "Кредиты:",3,200 10 120 50
box "CPU:",4,110 10 80 90
button %to.g.c,5,118 30 65 60,disable
edit "",6,210 30 100 20,right
box "Добавить:",7,200 60 120 40
button "+",8,230 78 15 17
button "-",9,210 78 15 17
button "+",10,300 78 15 17
button "-",11,280 78 15 17
text "5 | 50",12,250 80 28 14
button "Раздать",13,10 170 105 27
button "Еще",14,120 170 95 27
button "Оставить",15,220 170 95 27
box "Ставка:",16,10 110 310 50
edit %to.bet,17,20 130 75 20,right
button "0",18,100 125 15 27
button "1",19,120 125 15 27
button "2",20,140 125 15 27
button "3",21,160 125 15 27
button "4",22,180 125 15 27
button "5",23,200 125 15 27
button "6",24,220 125 15 27
button "7",25,240 125 15 27
button "8",26,260 125 15 27
button "9",27,280 125 15 27
button "С",28,300 125 15 27
edit "",29,10 210 305 50,read multi
button "Reset",30,160 267 75 27
button "Выход",31,240 267 75 27
text "Mirc-TeaM",32,10 266 140 14
text "cOrporetinon 2007",33,10 280 140 14
}

on *:dialog:to.table:*:*: {
if ($devent == init) {
  if (%to.a) { did -e $dname 2 }
  if (%to.ingame) { did -b $dname 13 }
  else {
    did -b $dname 14,15
    did -f $dname 13
  }
  if (%to.crd) { did -o $dname 6 1 %to.crd }
  else {
    set %to.crd 100
    did -o $dname 6 1 %to.crd
  }
  if (%to.bet) { did -o $dname 17 1 %to.bet }
  else {
    set %to.bet 5
    did -o $dname 17 1 %to.bet
  }
}
if ($devent == sclick) {
  if ($did == 2) { to.core ace }
  if ($did == 8) { did -o $dname 6 1 $calc(%to.crd + 5) }
  if ($did == 9) {
    var %to.x = $calc(%to.crd - 5)
    if (%to.x > 0) { did -o $dname 6 1 %to.x }
    else { to.hint Кредит не может быть отрицательным. }
  }
  if ($did == 10) { did -o $dname 6 1 $calc(%to.crd + 50) }
  if ($did == 11) {
    var %to.x = $calc(%to.crd - 50)
    if (%to.x > 0) { did -o $dname 6 1 %to.x }
    else { to.hint Кредит не может быть отрицательным. }
  }
  if ($did == 13) { to.core start }
  if ($did == 14) { to.core hit }
  if ($did == 15) { to.core stand }
  if ($did isnum 18-27) {
    if (%to.ingame) { to.hint Нельзя менять ставку во время игры. }
    else {
      var %to.x = %to.bet $+ $did($did)
      if (%to.x isnum 1-) {
        if (%to.x <= $did(6)) { did -o $dname 17 1 %to.x }
        else {
          did -o $dname 17 1 $did(6)
          to.hint Ставка не может превышать кредит.
        }
      }
    }
  }
  if ($did == 28) {
    if (%to.ingame) { to.hint Нельзя менять ставку во время игры. }
    else { did -r $dname 17 }
  }
  if ($did isnum 8-11) || ($did isnum 18-28) {
    set %to.crd $did($dname,6)
    set %to.bet $did($dname,17)
  }
  if ($did == 30) {
    unset %to.*
    dialog -x $dname
    ;      unload $script
  }
  if ($did == 31) { dialog -x $dname }
}
if ($devent == edit) {
  if ($did == 6) {
    if ($did($did) !isnum 1-) || ($did($did) !iswm $int($did($did))) {
      did -o $dname $did 1 %to.crd
      to.hint Кредит может быть только положительным целым числом.
    }
    set %to.crd $did($did)
  }
  if ($did == 17) {
    if (%to.ingame) {
      did -o $dname $did 1 %to.bet
      to.hint Нельзя менять ставку во время игры.
    }
    else {
      if ($did($did) isnum 1-) && ($did($did) iswm $int($did($did))) {
        if ($did($did) > $did(6) ) {
          did -o $dname $did 1 $did(6)
          to.hint Ставка не может превышать кредит.
        }
      }
      else {
        did -o $dname $did 1 %to.bet
        to.hint Ставка может быть только положительным целым числом.
      }
      set %to.bet $did($did)
    }
  }
}
if ($devent == mouse) {
  if ($did == 1) { to.hint Мои карты: $+ $crlf $+ %to.m }
  if ($did == 4) { to.hint Cpu карты: $+ $crlf $+ %to.c }
  if ($did == 29) { to.hint %to.result }
}
}
alias to.hint {
if (!%to.ingame) && (!%to.result) { set %to.result $1- }
did -r to.table 29
did -a to.table 29 $1-
}

alias to.card {
if (%to.pack) {
  var %r = $rand(1,$numtok(%to.pack,044)), %t = $gettok(%to.pack,%r,044), %c = $gettok(%t,1,058), %q = $gettok(%t,2,058), %m = $calc(%q - 1), %d = $+(%c,$chr(058),%m)
  set %to.pack $iif(%m,$reptok(%to.pack,%t,%d,1,044),$deltok(%to.pack,%r,044))
  return %c
}
else { return $to.pack }
}
alias to.pack {
if (%to.out) { var %def = %to.out | unset %to.out }
else { var %def = 2:24,3:24,4:24,5:24,6:24,7:24,8:24,9:24,10:24,j:24,q:24,k:24,a:24 }
while (%def) { var %r = $rand(1,$numtok(%def,044)), %t = $gettok(%def,%r,044), %def = $deltok(%def,%r,044), %s = $rand(0,$calc($numtok(%shuf,044) + 1)), %shuf = $instok(%shuf,%t,%s,044) }
set %to.pack %shuf
return $to.card
}
alias to.sort {
var %c = $to.card, %d = $+(%c,$chr(058),1), %w = $+(%c,$chr(058),$chr(042))
if ($wildtok(%to.game,%w,1,044)) {
  var %m = $v1, %a = $gettok(%m,1,058), %b = $gettok(%m,2,058), %b = $calc(%b + 1), %n = $+(%a,$chr(058),%b)
  set %to.game $reptok(%to.game,%m,%n,044)
}
else { set %to.game $instok(%to.game,%d,$rand(0,$calc($numtok(%to.game,044) + 1)),044) }
if ($1 == m) { set %to.m $iif(%to.m,$instok(%to.m,%c,$calc($numtok(%to.m,032) + 1),032),%c) }
if ($1 == c) { set %to.c $iif(%to.c,$instok(%to.c,%c,$calc($numtok(%to.c,032) + 1),032),%c) }
if (%c !isnum) && (%c != a) { var %s = 10 }
elseif (%c !isnum) && (%c == a) { var %s = 11 }
else { var %s = %c }
if ($1 == m) && (%c == a) { inc %to.a }
if ($1 == c) && (%c == a) { inc %to.b }
return %s
}

alias to.core {
if ($1 == start) {
  if (%to.ingame) { to.hint Игра уже идёт. }
  elseif (!%to.crd) { to.hint Пополните ваш кредит. | did -f to.table 6 }
  elseif (!%to.bet) { to.hint Не указана ставка. | did -f to.table 17 }
  else {
    to.hint
    set %to.ingame $true
    did -b to.table 13
    did -e to.table 14,15
    unset %to.g.* %to.m %to.c %to.a %to.b %to.result
    set %to.g.m $calc($to.sort(m) + $to.sort(m))
    set %to.g.c $to.sort(c)
    did -o to.table 2 1 %to.g.m
    did -o to.table 5 1 %to.g.c
    if (%to.g.m == 21) { to.end win.21 }
    elseif (%to.a) { did -e to.table 2 }
  }
}
if ($1 == hit) {
  inc %to.g.m $to.sort(m)
  did -o to.table 2 1 %to.g.m
  if ($calc(%to.g.m - %to.a * 10) > 21) { to.end lost }
  if (%to.g.m == 21) { set %to.21 $true }
  elseif (%to.a) { did -e to.table 2 }
}
if ($1 == ace) {
  dec %to.g.m 10
  did -o to.table 2 1 %to.g.m
  dec %to.a
  if (!%to.a) { did -b to.table 2 }
}
if ($1 == stand) {
  did -b to.table 2,14,15
  did -e to.table 13
  if (%to.21) { inc %to.g.c $to.sort(c) }
  else {
    while (%to.g.c < 17) {
      inc %to.g.c $to.sort(c)
      if (%to.g.c > 21) && (%to.b) { dec %to.g.c 10 | dec %to.b }
    }
  }
  did -o to.table 5 1 %to.g.c
  if (%to.g.m > 21) { to.end lost }
  elseif (%to.g.c > 21) { to.end win }
  elseif (%to.g.c == %to.g.m) { to.end draw }
  elseif (%to.g.c == 21) { to.end lost }
  elseif (%to.g.m == 21) { to.end win.21 }
  elseif (%to.g.c < %to.g.m) { to.end win }
  elseif (%to.g.c > %to.g.m) { to.end lost }
}
if ($1 == end) { to.end }
}
alias to.end {
if (%to.ingame) {
  set %to.ingame $false
  did -b to.table 2,14,15
  did -e to.table 13
  did -f to.table 13
  if (!$1) { to.hint Игра завершена. }
  elseif ($1 == win) {
    inc %to.crd %to.bet
    did -o to.table 6 1 %to.crd
    to.hint Поздравляю, вы выйграли %to.bet кредитов.
  }
  elseif ($1 == lost) {
    dec %to.crd %to.bet
    did -o to.table 6 1 %to.crd
    to.hint Поздравляю, вы проиграли %to.bet кредитов. :)
  }
  elseif ($1 == win.21) {
    inc %to.crd $round($calc(%to.bet * 1.5),0)
    did -o to.table 6 1 %to.crd
    to.hint Поздравляю, вы выйграли $round($calc(%to.bet * 1.5),0) кредитов.
  }
  elseif ($1 == draw) { to.hint Ничья. }
  if (%to.game) {
    var %out = %to.out
    while (%to.game) {
      var %r = $rand(1,$numtok(%to.game,044)), %t = $gettok(%to.game,%r,044), %a = $gettok(%t,1,058), %b = $gettok(%t,2,058), %w = $+(%a,$chr(058),$chr(042))
      if ($wildtok(%out,%w,1,044)) { var %m = $v1, %x = $gettok(%m,1,058), %y = $gettok(%m,2,058), %z = $+(%x,$chr(058),$calc(%b + %y)), %out = $reptok(%out,%m,%z,1,044) }
      else { var %out = $instok(%out,%t,$rand(0,$calc($numtok(%out,044) + 1)),044) }
      set %to.game $deltok(%to.game,%r,044)
    }
    set %to.out %out
    unset %to.game %to.21 %to.a %to.b
  }
}
}
