
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Worm War ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias -l wormwarversion var %v = 0.1 | if ($1) return %v | return Worm War v $+ %v
alias -l line drawline -nr @Worm War $1 1 $2-
alias -l dot drawdot -nr @Worm War $1 1 $2-
alias -l text drawtext -nrb @Worm War %wormwar.text 0 %wormwar.font $1-
alias -l rect drawrect -nr @Worm War $1 1 $2-
alias -l frect drawrect -nrf @Worm War $1 1 $2-
alias -l cls frect 0 0 0 $window(@Worm War).w $window(@Worm War).h
alias -l x if ($isid) return $line(@wormwar,$calc(-7+8*$1)) | rline @wormwar $calc(-7+8*$1) $2
alias -l y if ($isid) return $line(@wormwar,$calc(-6+8*$1)) | rline @wormwar $calc(-6+8*$1) $2
alias -l xp if ($isid) return $line(@wormwar,$calc(-5+8*$1)) | rline @wormwar $calc(-5+8*$1) $2
alias -l yp if ($isid) return $line(@wormwar,$calc(-4+8*$1)) | rline @wormwar $calc(-4+8*$1) $2
alias -l xe if ($isid) return $line(@wormwar,$calc(-3+8*$1)) | rline @wormwar $calc(-3+8*$1) $2
alias -l ye if ($isid) return $line(@wormwar,$calc(-2+8*$1)) | rline @wormwar $calc(-2+8*$1) $2
alias -l d if ($isid) return $line(@wormwar,$calc(-1+8*$1)) | rline @wormwar $calc(-1+8*$1) $2
alias -l c if ($isid) return $line(@wormwar,$calc(8*$1)) | rline @wormwar $calc(8*$1) $2
alias -l i return echo -s $1-
alias -l setfont {
  if ($1 == 2) %wormwar.font = fixedsys 20
  elseif ($1 == 1) %wormwar.font = fixedsys 15
  elseif ($1 == 0) %wormwar.font = "lucida console" 10
  elseif ($1 == -1) %wormwar.font = "lucida console" 8
}
alias wormwar {
  if ($1-) { if ($isid) return $ [ $+ [ $1- ] ] | $1- | return }
  ;window -a "status window"
  ;clear -s
  i wormwar $1-
  var %xy = -1 -1
  if ($window(@Worm War)) %xy = $window($ifmatch).x $window($ifmatch).y
  end
  %wormwar.size = 5
  window -opfk0 +nf @Worm War %xy $calc(32*%wormwar.size +100) $calc(32*%wormwar.size +10)
  window -lh @wormwar $calc($window(-1).w -200) 100 200 -1
  %wormwar.k = 0
  %wormwar.lives = 3
  %wormwar.cheat = 0
  %wormwar.score = 0
  if (* !iswm %wormwar.high) %wormwar.high = 0
  %wormwar.gameover = 0
  %wormwar.wall = $rgb(0,255,0)
  %wormwar.text = $rgb(255,255,255)
  %wormwar.rgb = $rgb(255,255,170) $rgb(255,218,85) $rgb(255,218,255) $rgb(255,218,170) $rgb(255,255,85) $rgb(255,182,255) $rgb(255,182,170)
  setlevel 1 1
}
alias -l end close -@ @Worm War @wormwar
alias -l open {
  if ($getdot(@Worm War,$1,$2)) return
  if ($getdot(@Worm War,$calc(1+$1),$calc(1+$2))) return
  return 1
}
alias -l disp {
  i disp $1-
  setfont 0
  var %x = %wormwar.size * 32
  text $calc(8+%x) 20 Level: %wormwar.levela $+ . $+ %wormwar.levelb
  text $calc(8+%x) 30 Score: %wormwar.score
  text $calc(8+%x) 40 Lives: %wormwar.lives
  text $calc(50+%x) 60 %wormwar.high
  setfont -1
  text $calc(18+%x) 53 High
  text $calc(18+%x) 61 Score:
}
alias -l lines {
  i lines $1-
  var %c = line 0, %s = %wormwar.size, %n = 31, %x1 = $1, %y1 = $2, %x2 = $3, %y2 = $4, %0x1 = %n - %x1, %0y1 = %n - %y1, %0x2 = %n - %x2, %0y2 = %n - %y2
  if ($5 != 0) %c = line %wormwar.wall
  %c %x1 %y1 %x2 %y2
  %c %y1 %x1 %y2 %x2
  %c %0x1 %y1 %0x2 %y2
  %c %0y1 %x1 %0y2 %x2
  %c %0x1 %0y1 %0x2 %0y2
  %c %0y1 %0x1 %0y2 %0x2
  %c %x1 %0y1 %x2 %0y2
  %c %y1 %0x1 %y2 %0x2
}
alias -l dispversion {
  i dispversion $1-
  setfont 2
  var %x = %wormwar.size * 32
  text $calc(8+%x) 3 Worm War
  setfont -1
  ;text $calc(46+%x) 95 v $+ $wormwarversion(1)
  text $calc(76+%x) 9 v $+ $wormwarversion(1)
}
alias -l setlevel {
  .timerwormwar off
  i setlevel $1-
  %wormwar.k = 0
  if ($1 isnum) %wormwar.levela = $1
  if ($2 isnum) %wormwar.levelb = $2
  if (%wormwar.levelb > 5) {
    inc %wormwar.levela
    %wormwar.levelb = 1
  }
  if (%wormwar.levela > 5) %wormwar.levela = 1
  var %n, %a = %wormwar.levela, %b = %wormwar.levelb, %s = %wormwar.size, %x, %y, %w = 1 + $replace(%b,5,6)
  cls
  dispversion
  disp
  rect %wormwar.wall 0 0 32 32
  if (%a == 2) {
    %x = 0 | while (%x < 30) {
      %y = 0 | while (%y < 30) {
        dot %wormwar.wall $calc(3+%x) $calc(3+%y)
        inc %y 5
      }
      inc %x 5
    }
  }
  elseif (%a == 3) {
    lines 2 2 15 2
    lines 5 2 5 3 0
    lines 11 29 11 28 0
  }
  elseif (%a == 4) {
    lines 4 2 6 2
    lines 10 2 11 2
    lines 15 2 16 2
    lines 8 3 8 6
    lines 13 3 13 5
  }
  elseif (%a == 5) lines 11 1 11 6
  if (%a == 1) %a = 2
  while ($line(@wormwar,0)) { dline @wormwar 1 }
  while ($line(@wormwar,0) < $calc(8*%w)) { aline @wormwar 0 }
  %n = 1 | while (%n <= %w) { c %n $gettok(%wormwar.rgb,%n,32) | inc %n }
  x 1 1 | y 1 1
  var %y = 2 $gettok(1.1 3.3 4 1.3 4 1 2.1 1 1 1 1 1,%b,46) | %x = $numtok(%y,32) | while (%x) { d %x $gettok(%y,%x,32) | dec %x }
  if (%b < 4) {
    %y = 1 $gettok(16.17 15.30 30 1,%b,46) | %x = %b + 1 | while (%x) { x %x $gettok(%y,%x,32) | dec %x }
  }
  if (%b < 3) {
    %x = $line(@wormwar,0) / 8 | while (%x > 1) { y %x $x(%x) | dec %x }
  }
  if (%b == 3) {
    %y = 1 1 30 30 | %x = 4 | while (%x) { y %x $gettok(%y,%x,32) | dec %x }
  }
  if (%b == 4) {
    %y = 1 24 30 7 1 | %x = 5 | while (%x) { x %x $gettok(%y,%x,32) | dec %x }
    %y = 1 1 24 30 7 | %x = 5 | while (%x) { y %x $gettok(%y,%x,32) | dec %x }
  }
  if (%b == 5) {
    %y = 1 $gettok(22 30 9 1.1 6 11 16 21 26.1 5 11 20 26 30.1 7 12 19 24 30.1 8 10 21 24 30,%a,46)
    %x = $numtok(%y,32) | while (%x) { x %x $gettok(%y,%x,32) | dec %x }
    %x = $line(@wormwar,0) / 8 | while (%x > 1) { y %x 30 | dec %x }
  }
  %n = $line(@wormwar,0) / 8
  while (%n) {
    %x = $x(%n) * %s
    %y = $y(%n) * %s
    inc %x
    inc %y
    x %n %x
    y %n %y
    xp %n %x
    yp %n %y
    xe %n %x
    ye %n %y
    dec %n
  }
  %x = 31 | while (%x >= 0) {
    %y = 31 | while (%y >= 0) {
      frect $iif($getdot(@Worm War,%x,%y),%wormwar.wall,0) $calc(%x *%s) $calc(%y *%s) %s %s
      dec %y
    }
    dec %x
  }
  game
}
alias -l move {
  var %xx, %yy, %x = $x($1), %y = $y($1), %xp = $xp($1), %yp = $yp($1), %xe = $xe($1), %ye = $ye($1), %d = $d($1), %c = $c($1), %s = %wormwar.size, %dd = %d
  if (%d !isnum 1-4) return 1
  :next
  %xx = %x
  %yy = %y
  if (%d == 1) %yy = %y - %s
  elseif (%d == 2) %xx = %x + %s
  elseif (%d == 3) %yy = %y + %s
  elseif (%d == 4) %xx = %x - %s
  if ($open(%xx,%yy)) {
    frect 0 $calc(%x -1) $calc(%y -1) %s %s
    line %c %xp %yp %x %y
    line %c %x %y %xx %yy
    %s = %c $calc(%xx -1) $calc(%yy -1) %s %s
    if ($1 > 1) rect %s
    else frect %s
    xp $1 %x
    yp $1 %y
    x $1 %xx
    y $1 %yy
    return 1
  }
  elseif ($1 > 1) {
    dec %d
    if (%d < 1) %d = 4
    d $1 %d
    if (%dd != %d) goto next
  }
  if ($getdot(@worm war,%xe,$calc(%ye -1))) {
    line 0 %xe %ye %xe $calc(%ye -%s)
    dec %ye %s
    ye $1 %ye
  }
  elseif ($getdot(@worm war,%xe,$calc(%ye +1))) {
    line 0 %xe %ye %xe $calc(%ye +%s)
    inc %ye %s
    ye $1 %ye
  }
  elseif ($getdot(@worm war,$calc(%xe -1),%ye)) {
    line 0 %xe %ye $calc(%xe -%s) %ye
    dec %xe %s
    xe $1 %xe
  }
  elseif ($getdot(@worm war,$calc(%xe +1),%ye)) {
    line 0 %xe %ye $calc(%xe +%s) %ye
    inc %xe %s
    xe $1 %xe
  }
  if (%xe == %x) && (%ye == %y) {
    if ($1 > 1) {
      if (%wormwar.cheat == 0) {
        inc %wormwar.score
        if (%wormwar.score > %wormwar.high) %wormwar.high = %wormwar.score
        if ($calc(%wormwar.score % 20) == 0) && (%wormwar.lives < 6) inc %wormwar.lives
      }
      var %n = 8 | while (%n) { dline @wormwar $calc(8*$1 -7) | dec %n }
    }
    else d $1 0
    disp
    frect 0 $calc(%x -1) $calc(%y -1) %s %s
  }
  else return 1
}
alias -l game {
  drawpic @Worm War
  .timerwormwar $iif(%wormwar.timer,-moch,-moh) 0 50 if ($window(@Worm War)) && ($script(wormwar.mrc)) wormwartimer $chr(124) else .timerwormwar off
}
alias -l wormwartimer {
  var %n, %l, %k = %wormwar.k, %s = %wormwar.size
  %wormwar.k = 0
  if (%wormwar.gameover) goto gameover
  if (%k == 32) {
    %wormwar.k = 32
    return
  }
  elseif (%k == 27) goto escape
  if (%k isnum 37-40) var %x = $x(1), %y = $y(1)
  if (%k == 38) {
    if ($open(%x,$calc(%y -%s))) d 1 1
    else %wormwar.k = %k
  }
  elseif (%k == 39) {
    if ($open($calc(%x +%s),%y)) d 1 2
    else %wormwar.k = %k
  }
  elseif (%k == 40) {
    if ($open(%x,$calc(%y +%s))) d 1 3
    else %wormwar.k = %k
  }
  elseif (%k == 37) {
    if ($open($calc(%x -%s),%y)) d 1 4
    else %wormwar.k = %k
  }
  elseif (%k == 76) {
    %wormwar.levelb = 6
    %wormwar.cheat = 1
    setlevel
    return
  }
  elseif (%k == 83) {
    inc %wormwar.levelb
    %wormwar.cheat = 1
    setlevel
    return
  }
  %n = $line(@wormwar,0) / 8
  %l = 1
  while (%l <= %n) {
    if ($move(%l)) inc %l
  }
  if ($line(@wormwar,0) < 16) {
    inc %wormwar.levelb
    setlevel
    return
  }
  drawpic @Worm War
  if ($d(1)) return
  dec %wormwar.lives
  i lives = %wormwar.lives
  if (%wormwar.lives) {
    setlevel
    return
  }
  :escape
  i escape
  setfont 1
  text $calc(11+%s *31) 75 Game Over
  disp
  :gameover
  i gameover
  if (%wormwar.gameover != 1) {
    %wormwar.gameover = 1
    %wormwar.k = 0
    setfont -1
    text 0 $calc(1+%s *32) ENTER=Restart, SPACE=Info, ESC=Exit
  }
  drawpic @Worm War
  if (%k == 13) wormwar
  if (%k == 32) {
    cls
    setfont 0
    text 8 56 by Andy Dufilie
    text 8 72 pages.cthome.net/pdufilie
    dispversion
  }
  if (%k == 27) .timer -om 1 300 end
}
on *:keydown:@Worm War:*:%wormwar.k = $keyval
on *:close:@Worm War:end
on *:close:@wormwar:end
alias -l movewin {
  if ($window(@Worm War)) {
    %wormwar.k = 32
    var %x = $mouse.dx - $1, %y = $mouse.dy - $2, %w = $window(-1).w - $window(@Worm War).w, %h = $window(-1).h - $window(@Worm War).h
    if (%x > %w) %x = %w
    if (%y > %h) %y = %h
    if (%x < 0) %x = 0
    if (%y < 0) %y = 0
    window @Worm War %x %y
    if ($mouse.key & 1) .timer $+ @Worm War -o 1 0 movewin $1-
    else %wormwar.k = 0
  }
}
menu @Worm War {
  sclick:movewin $calc($mouse.dx -$window(@Worm War).x) $calc($mouse.dy -$window(@Worm War).y)
  dclick:end
  $iif(%wormwar.timer,[×],[  ])  Use timer -c (catch up):%wormwar.timer = $iif(%wormwar.timer,0,1) | game
  -
  Visit website:run http://pages.cthome.net/pdufilie/
  -
  Close:end
}
