alias mm {
  if ($1 == 2P) { mm.multi.start | return }
  if (($1) && (($1 <= 0) || ($1 !isnum))) return
  if ($window(@MasterMind_Multi)) window -c $ifmatch
  if ($sock(mm.*)) .sockclose mm.*
  if ($timer(mm.follow)) .timermm.follow off
  mm.start
  if (!$window(@MasterMind)) { window -pdhk0 +t @MasterMind 50 50 247 250 } | else { window -a @MasterMind }
  if (!$window(@MasterMind_db)) { window -ph @MasterMind_db | drawpic @MasterMind_db 1 1 $+(",$scriptdirmm_img.bmp,") }
  drawfill -rs @MasterMind 12632256 $color($color(b)) 1 1
  var %mdh $window(@MasterMind).dh
  drawrect -rf @MasterMind 12632256 1 10 $calc((%mdh /3)+30) 221 $calc((%mdh /3)+30)
  mm.drawrows $iif($1,$1,15)
  drawcopy @MasterMind_db 1 55 93 65 @MasterMind 145 5
  drawrect -r @MasterMind 0 1 151 85 80 130
  drawline -r @MasterMind 0 1 151 100 230 100
  drawfill -r @MasterMind 8421504 0 152 86
  drawtext -r @MasterMind 16777215 Tahoma 10 157 86 Current Colour
  drawcopy @MasterMind_db 1 120 43 50 @MasterMind 170 115
  mm.pin Red
  mm.change.status Single Player
}
alias -l mm.pin {
  var %mm.x = $mouse.x, %mm.y = $mouse.y,%mm.dot = $getdot(@MasterMind,%mm.x,%mm.y),%mmaincol = $pegcolor($hget(masterm,lastpeg)).main,%pegid = $pegcolor(%mmaincol).id,%mcalcthing = $calc(24*$hget(masterm,numrows)), %inrectthing = $calc(%mcalcthing -8),%mwon
  if ($inrect(%mm.x,%mm.y,17,15,93,$calc((24*$hget(masterm,numrows))-8))) {
    if (%mmaincol) {
      if ((%mm.dot == 14671839) || ($pegcolor(%mm.dot).f)) {
        drawfill -r @MasterMind %mmaincol 0 %mm.x $calc(%mcalcthing -(24*$hget(masterm,currrow))))
        if ($inrect(%mm.x,%mm.y,18,15,15,%inrectthing)) hadd -m masterm col1 %pegid
        if ($inrect(%mm.x,%mm.y,43,15,15,%inrectthing)) hadd -m masterm col2 %pegid
        if ($inrect(%mm.x,%mm.y,68,15,15,%inrectthing)) hadd -m masterm col3 %pegid
        if ($inrect(%mm.x,%mm.y,94,15,15,%inrectthing)) hadd -m masterm col4 %pegid
        if ($hfind(masterm,col?,0,w) == 4) && ($hget(masterm,multicolours)) { .sockwrite -nt $iif($hget(masterm,multitype) == $yes,mm.player,mm.play) GUESS %mcalcthing $hget(masterm,col1) $hget(masterm,col2) $hget(masterm,col3) $hget(masterm,col4) | return }
        if (!$hget(masterm,multicolours)) mm.check %mcalcthing
      }
    }
  }
  elseif ($1) && (!$hget(masterm,ended)) {
    var %pegcol = $1, %lastpeg = $iif($hget(masterm,lastpeg),$ifmatch,Gray),%pegtit = $calc(31 -($width(%pegcol,Courier New,16) /2))
    var %lastpegmain = $iif($hget(masterm,lastpegmain),$ifmatch,5197647), %lastpeghl = $iif($hget(masterm,lastpeghl),$ifmatch,15000804)
    var %pegcolmain = $pegcolor(%pegcol).main,%pegcolhl = $pegcolor(%pegcol).hl
    drawreplace -nr @MasterMind $pegcolor(%lastpeg).main %pegcolmain 170 115 43 50
    drawreplace -r @MasterMind $pegcolor(%lastpeg).hl %pegcolhl 170 115 43 50
    drawrect -fr @MasterMind 12632256 1 160 170 65 20
    drawtext -or @MasterMind 0 "Courier New" 16 $calc(161 + %pegtit) 171 $1
    hadd -m masterm lastpeg %pegcol
    hadd -m masterm lastpegmain %pegcolmain
    hadd -m masterm lastpeghl %pegcolhl
  }
}
alias -l WorB return $iif($1,0,$iif($2,16777215,14671839))
;Thanks to myndzi for speeding up my pegcolor alias *^_^*
alias -l pegcolor {
  var %n = Gray     Red     Blue     Purple   Yellow  Green   Pink
  var %c = 5197647  255     16711680 8388736  4378365 32768   9511679
  var %h = 15000804 8421631 16744576 10705345 65535   3650878 13144575
  var %t = $findtok(%n,$1,1,32)
  if ($prop == f) {
    if ($findtok(%c,$1,1,32)) return $gettok(%n,$ifmatch,32)
    if ($findtok(%h,$1,1,32)) return $gettok(%n,$ifmatch,32)
  }
  elseif ($prop == id) {
    if ($findtok(%c,$1,1,32)) return $ifmatch
  }
  elseif ($prop == idname) {
    if ($gettok(%n,$1,32)) return $ifmatch
  }
  else return $gettok($iif($prop == hl,%h,%c),%t,32)
}
alias -l mm.drawrows {
  var %mwin $+(@MasterMind,$iif($2,_Multi))
  if ($window(%mwin)) {
    hadd -m masterm numrows $1
    var %numrows = $1,%rowh = 12
    window %mwin $window(%mwin).x $window(%mwin).y 247 $iif($calc((24*%numrows) +100) > 250,$ifmatch,250)
    drawfill -rs %mwin 12632256 $color($color(b)) $calc($window(%mwin).dw -3) $calc($window(%mwin).dh -3)
    drawcopy @MasterMind_db 1 25 146 12 %mwin 1 1
    while (%numrows) {
      drawcopy @MasterMind_db 1 1 143 24 %mwin 1 %rowh
      dec %numrows
      inc %rowh 24
    }
    drawcopy @MasterMind_db 1 37 145 18 %mwin 1 %rowh
    drawrect -rf %mwin 12632256 1  1 $calc(16 +%rowh) 145 300
  }
}
alias -l mm.start {
  if ($hget(masterm)) .hfree masterm
  hadd -m masterm currrow 0
  hadd -m masterm result $r(2,7) $r(2,7) $r(2,7) $r(2,7)
  hadd -m masterm started $ticks
}
alias -l mm.end.msg {
  if ($window(@MasterMind)) {
    var %mdh = $window(@MasterMind).dh,%mdhc = $calc((%mdh /3)+30),%mdw = $window(@MasterMind).dw
    drawrect -rf @MasterMind 12632256 1 10 %mdhc 221 %mdhc
    drawrect -r @MasterMind 0 1 10 %mdhc 221 %mdhc
    if ($1) {
      drawtext -rob @MasterMind 0 12632256 Tahoma 16 56 $calc(2+%mdhc) Congratulations!
      drawtext -rb @MasterMind 0 12632256 Tahoma 12 52 $calc(21+%mdhc) You beat the MasterMind!
    }
    else {
      drawtext -rob @MasterMind 0 12632256 Tahoma 16 80 $calc(2+%mdhc) Bad Luck!
      drawtext -rb @MasterMind 0 12632256 Tahoma 12 62 $calc(21+%mdhc) You ran out of rows!
    }
    var %mrows = Rows: $$hget(masterm,currrow), %mtime = Time: $duration($calc(($ticks - $hget(masterm,started))/1000))
    drawtext -rbp @MasterMind 0 12632256 Tahoma 14 $calc((%mdw /2)-($width(%mrows,Tahoma,14) /2)) $calc(35+%mdhc) %mrows
    drawtext -rbp @MasterMind 0 12632256 Tahoma 14 $calc((%mdw /2)-($width(%mtime,Tahoma,14) /2)) $calc(53+%mdhc) %mtime
    var %mkount = 1,%mpegs = $hget(masterm,result),%mkount2 = 65,%mcurr
    while $gettok(%mpegs,%mkount,32) {
      %mcurr = $pegcolor($ifmatch).idname
      drawrect -efr @MasterMind $pegcolor(%mcurr) 1 %mkount2 $calc(76+%mdhc) 20 20
      drawrect -er @MasterMind 0 1 %mkount2 $calc(76+%mdhc) 20 20
      drawdot -r @MasterMind $pegcolor(%mcurr).hl 7 $calc(10+%mkount2) $calc(86+%mdhc)
      drawdot -r @MasterMind 16777215 3 $calc(8+%mkount2) $calc(84+%mdhc)
      inc %mkount
      inc %mkount2 30
    }
    mm.end
  }
}
alias -l mm.end .hadd -m masterm ended 1
alias -l mm.check {
  if ($window(@MasterMind)) {
    if ($hfind(masterm,col?,0,w) == 4) || ($hget(masterm,multicolours)) {
      var %mkount = 4,%mcalcthing = $1,%mresult,%mwhite,%mblack,%mwon
      if (!$2) {
        while %mkount {
          %mresult = $hget(masterm,$+(col,%mkount)) %mresult 
          dec %mkount
        }
      }
      else %mresult = $2-
      var %realresult = $hget(masterm,result),%mkount = 1,%mcurr
      while $gettok(%mresult,%mkount,32) {
        %mcurr = $ifmatch
        if ($gettok(%realresult,%mkount,32) == %mcurr) { inc %mblack | %mresult = $reptok(%mresult,%mcurr,8,1,32) | %realresult = $reptok(%realresult,%mcurr,8,1,32) }
        inc %mkount
      }
      if (%mblack == 4) %mwon = 1
      %mkount = 1
      var %mc1,%mc2,%mresult2 = %mresult
      while $gettok(%mresult,%mkount,32) {
        %mcurr = $ifmatch
        %mc1 = $count(%realresult,%mcurr)
        %mc2 = $count(%mresult2,%mcurr)
        if ($istok(%realresult,%mcurr,32)) && (%mcurr != 8) { inc %mwhite $iif(%mc1 > %mc2,%mc2,%mc1) | %mresult2 = $replace(%mresult2,%mcurr,8) }
        dec %mkount
      }
      var %mdots $calc((%mcalcthing -(24*$hget(masterm,currrow)))+6)
      if ($hget(masterm,multicolours)) { echo -a wut wut | .sockwrite -nt $iif($hget(masterm,multitype) == $yes,mm.player,mm.play) RESULT $calc(%mblack) $calc(%mwhite) | return }
      if (!%mwhite) && (!%mblack) { drawline -r @MasterMind 255 2 127 $calc(%mdots -9) 134 $calc(%mdots -2) | drawline -r @MasterMind 255 2 134 $calc(%mdots -9) 127 $calc(%mdots -2) | goto skip }
      drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 128 $calc(%mdots -10) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
      drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 136 $calc(%mdots -10) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
      drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 128 $calc(%mdots -2) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
      drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 136 $calc(%mdots -2) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
      :skip
      hinc -m masterm currrow
      if (!$hget(masterm,multicolors)) {
        if (%mwon) {
          mm.end.msg 1
        }
        if ($hget(masterm,currrow) == $hget(masterm,numrows)) && (!%mwon) {
          mm.end.msg
        }
      }
      hdel -w masterm col?
      if ($hget(masterm,procols)) .hdel masterm procols
    }
  }
}
alias mm.change.status {
  .hadd -m masterm status $1-
  if ($window(@MasterMind)) || ($1 == 1) {
    var %mwin = @MasterMind,%mwindh = $calc($window(%mwin).dh -18)
    drawrect -rf %mwin 12632256 1 3 %mwindh 140 20
    drawtext -rbp %mwin 0 12632256 Arial 12 3 %mwindh Status: $hget(masterm,status)
  }
}
menu @MasterMind {
  ;sclick:echo -a $mouse.x - $mouse.y - mm.pin $pegcolor($getdot(@MasterMind,$mouse.x,$mouse.y)).f
  sclick:if (!$hget(masterm,ended)) mm.pin $pegcolor($getdot(@MasterMind,$mouse.x,$mouse.y)).f
  .MasterMind by zack^:var %m.
  -
  Red:mm.pin Red
  Blue:mm.pin Blue
  Purple:mm.pin Purple
  Yellow:mm.pin Yellow
  Green:mm.pin Green
  Pink:mm.pin Pink
  -
  .New Game
  ..Single Player:mm $hget(masterm,numrows)
  ..$style(2) Multi Player:echo -a it's disabled foo
  -
  .Level (Rows)
  ..$iif($hget(masterm,numrows) == 1,$style(1)) Impossible (1):mm 1
  ..$iif($hget(masterm,numrows) == 4,$style(1)) Really Hard (4):mm 4
  ..$iif($hget(masterm,numrows) == 6,$style(1)) Hard (6):mm 6
  ..$iif($hget(masterm,numrows) == 10,$style(1)) Medium (10):mm 10
  ..$iif($hget(masterm,numrows) == 15,$style(1)) Easy (15):mm 15
  ..$iif($hget(masterm,numrows) == 20,$style(1)) Really Easy (20):mm 20
  ..$iif($hget(masterm,numrows) == 25,$style(1)) You're Kidding Me Right? (25):mm 25
  ..$iif(!$istok(1.4.6.10.15.20.25,$hget(masterm,numrows),46),$style(1)) Custom (?):var %mtemp $$input(How many rows?,e,Rows) | mm %mtemp
}

on *:CLOSE:@MasterMind*:{ if ($active != @Mastermind_Multi) { close -@ @MasterMind* | if ($hget(masterm)) hfree masterm } | elseif ($active == @Mastermind_Multi) { mm.change.status Canceled } | .timermm.follow off }
on *:ACTIVE:@MasterMind:{ if (!$hget(masterm,activepro)) && ($window(@MasterMind_Multi)) { window -a @MasterMind_Multi | window -a @MasterMind | hadd -mu1 masterm activepro 1 } }

;;;;Multiplayer Options;;;;

alias -l mm.multi.start {
  var %mwin @MasterMind_Multi
  hadd -m masterm multistart 1
  hadd -m masterm ended 1
  if ($hget(masterm,result)) .hdel masterm result
  if ($sock(mm.multi)) .sockclose $ifmatch
  if (!$window(%mwin)) { window -pdhk0 +t %mwin $calc(10+$window(@MasterMind).x +$window(@MasterMind).w) $window(@MasterMind).y 247 $window(@MasterMind).h } | else { window -a %mwin }
  .timermm.follow -m 0 20 mm.multi.follow
  drawfill -rs %mwin 12632256 $color($color(b)) 1 1
  mm.drawrows $hget(masterm,numrows) 1
  mm.change.status Waiting...
  hadd -m masterm multitype $input(Will you be hosting the game?,yv,MasterMind Multiplayer - Type)
  if ($hget(masterm,multitype) == $no) {
    hadd -m masterm multidest $input(Destination IP:Port,e,MasterMind Multiplayer - Destination IP & Port)
    hadd -m masterm multiIP $gettok($hget(masterm,multidest),1,58)
    hadd -m masterm multiCONNPORT $gettok($hget(masterm,multidest),2,58)
    if (!$longip($hget(masterm,multiIP))) { mm.multi.error | return }
    mm.change.status Connecting...
    .sockopen mm.play $hget(masterm,multiIP) $hget(masterm,multiCONNPORT)
  }
  else {
    :port
    hadd -m masterm multiport $input(Port for friend to connect to?,e,MasterMind Multiplayer - Port)
    if (!$hget(masterm,multiport)) { mm.multi.error | return }
    if (!$portfree($hget(masterm,multiport))) { var %mtemp $input(Sorry that port currently cannot be used. Please select another.,o,MasterMind Multiplayer - Port) | goto port }
    mm.change.status Waiting...
    socklisten mm.multi $hget(masterm,multiport)
  }
}
alias -l mm.multi.follow window @MasterMind_Multi $calc(10+$window(@MasterMind).x +$window(@MasterMind).w) $window(@MasterMind).y 247 $window(@MasterMind).h
alias -l mm.multi.error { mm.change.status Invalid Data | .timermm.follow off | window -c @MasterMind_Multi | hdel masterm multistart }
alias mm.multi.choose {
  if (!$window(@MasterMind_Choose)) { window -pdhk0 +t @MasterMind_Choose 50 50 175 150 } | else { window -a @MasterMind_Choose }
  drawfill -rs @MasterMind_Choose 12632256 $color($color(b)) 1 1
  drawcopy @MasterMind_db 1 55 93 65 @MasterMind_Choose 40 1
  drawtext -r @MasterMind_Choose 0 Arial 12 5 65 Select Colours For Opponent
  var %mkount = 4,%mkount2 = 10
  while %mkount {
    drawrect -re @MasterMind_Choose 0 1 %mkount2 90 30 30
    dec %mkount
    inc %mkount2 40
  }
}
alias -l mm.multi.choose.peg {
  var %mpeg = $pegcolor($pegcolor($1).f),%mid = $pegcolor(%mpeg).id
  if (%mpeg) { 
    hinc -m masterm multipeg 
    drawfill -r @MasterMind_Choose %mpeg 0 $calc(35*$hget(masterm,multipeg)) 108 
    hadd -m masterm result $hget(masterm,result) %mid
    if ($hget(masterm,multipeg) == 4) {
      .sockwrite -nt $iif($hget(masterm,multitype) == $yes,mm.player,mm.play) COLOURS
      .hdel masterm multipeg
      .window -c @MasterMind_Choose
      mm.change.status Waiting...
    }
  }
}
alias -l mm.multi.result {
  var %mblack = $1,%mwhite = $2,%mwon
  if (%mblack == 4) %mwon = 1
  if (!%mwhite) && (!%mblack) { drawline -r @MasterMind 255 2 127 $calc(%mdots -9) 134 $calc(%mdots -2) | drawline -r @MasterMind 255 2 134 $calc(%mdots -9) 127 $calc(%mdots -2) | goto skip }
  drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 128 $calc(%mdots -10) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
  drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 136 $calc(%mdots -10) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
  drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 128 $calc(%mdots -2) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
  drawdot -r @MasterMind $worb(%mblack,%mwhite) 3 136 $calc(%mdots -2) | if (%mblack) dec %mblack | elseif (%mwhite) dec %mwhite
  :skip
  hinc -m masterm currrow
  if (%mwon) {
    mm.multi.end.msg 1
  }
  if ($hget(masterm,currrow) == $hget(masterm,numrows)) && (!%mwon) {
    mm.multi.end.msg
  }
  hdel -w masterm col?
  if ($hget(masterm,procols)) .hdel masterm procols
}
menu @MasterMind_Choose {
  sclick:if ($inrect($mouse.x,$mouse.y,40,1,93,65)) mm.multi.choose.peg $getdot(@MasterMind_Choose,$mouse.x,$mouse.y)
}
;Server - mm.player
on *:SOCKLISTEN:mm.multi:{
  .sockaccept mm.player
  mm.change.status Connected!
  .sockwrite -nt mm.player OK $hget(masterm,numrows)
}
on *:SOCKREAD:mm.player:{
  .sockread %mm.player
  tokenize 32 %mm.player
  echo -a Server: $1-
  var %mcmd = $1
  goto %mcmd

  :CONNOK
  .sockwrite -nt $sockname CONNOK
  mm.change.status Verified!
  mm.multi.choose
  return
  :COLOURS
  if ($numtok($hget(masterm,result),32) == 4) { sockwrite -nt $sockname START | hadd -m masterm multicolours 1 }
  return
  :START
  if (!$hget(masterm,multicolours)) { sockwrite -nt $sockname START | .hadd -m masterm multicolours 1 } | mm.change.status Playing! | if ($hget(masterm,ended)) .hdel masterm ended
  return
  :GUESS
  mm.check $2-
  return
  :RESULT
  mm.multi.result $2- 
  return
  :%mcmd
  return
}
on *:SOCKCLOSE:mm.player:mm.change.status Player Left
;Client - mm.play
on *:SOCKOPEN:mm.play:{
  .hadd -m masterm multiconn 1
}
on *:SOCKREAD:mm.play:{
  .sockread %mm.play
  tokenize 32 %mm.play
  echo -a Client: $1-
  var %mcmd = $1
  goto %mcmd
  :OK
  .sockwrite -nt $sockname CONNOK
  mm.drawrows $2
  mm.drawrows $2 1
  return
  :CONNOK 
  mm.change.status Verified!
  mm.multi.choose
  return
  :COLOURS
  if ($numtok($hget(masterm,result),32) == 4) { sockwrite -nt $sockname START | hadd -m masterm multicolours 1 }
  return
  :START
  if (!$hget(masterm,multicolours)) { sockwrite -nt $sockname START | .hadd -m masterm multicolors 1 } | mm.change.status Playing!
  return
  :GUESS
  mm.check $2-
  return
  :RESULT
  mm.multi.result $2- 
  return
  :%mcmd
  return

}
on *:SOCKCLOSE:mm.play:mm.change.status Player Left
