imk0tter

Untitled

Sep 22nd, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.17 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; ;;
  3. ;; Connect 4: By Imk0tter & Kylar ;;
  4. ;; ;;
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6.  
  7. menu Nicklist {
  8. Connect4:Connect4::SendRequest $1
  9. }
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11. ; Open Handeling ;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ctcp 1:CONNECT4.REQUEST*:*: {
  14. .timer 1 0 Connect4::ReceiveRequest $nick $ctime
  15. }
  16. ctcp 1:CONNECT4.REJECT*:*: {
  17. .timer 1 0 Connect4::RequestFailed $nick
  18. .timerCONNECT4.REQUEST. $+ $nick off
  19. }
  20. ctcp 1:CONNECT4.ACCEPT*:*: {
  21. if ($timer(CONNECT4.REQUEST. $+ $nick)) {
  22. .timerCONNECT4.REQUEST. $+ $nick off
  23. var %id $Connect4::UnusedID
  24. sockopen %id $2 $3
  25. }
  26. }
  27. alias Connect4::SendRequest {
  28. ctcp $1 CONNECT4.REQUEST
  29. .timerCONNECT4.REQUEST. $+ $1 1 10 Connect4::RequestFailed $1
  30. }
  31. alias Connect4::RequestFailed {
  32. var %x $input($1 denied your Connect4 request,o)
  33. }
  34. alias Connect4::ReceiveRequest {
  35. var %request $input(Would you like to start a Connect4 game with $1?,y)
  36. if (%request && $calc($ctime - $2) < 10) {
  37. .timerRequestTimeout $+ $1 off
  38. if ($sock(CONNECT4.LISTEN. $+ $1)) sockclose CONNECT4.LISTEN. $+ $nick
  39. socklisten Connect4.LISTEN. $+ $1
  40. ctcp $1 CONNECT4.ACCEPT $ip $sock(Connect4.LISTEN. $+ $1).port
  41. .timerConnect4LISTEN 1 10 Connect4::AcceptFailed $1
  42. }
  43. elseif (%request) {
  44. var %x $input(Request has expired,o)
  45. }
  46. else {
  47. ctcp $1 CONNECT4.REJECT
  48. }
  49. }
  50. alias Connect4::AcceptFailed {
  51. sockclose Connect4.LISTEN. $+ $nick
  52. Connect4::SendRequest $1
  53. }
  54. on 1:socklisten:CONNECT4.LISTEN*: {
  55. .timerConnect4LISTEN off
  56. var %id $Connect4::UnusedID
  57. sockaccept %id
  58. Connect4::InitClient 1 %id
  59. sockclose $sockname
  60. }
  61.  
  62. on 1:sockopen:CONNECT4.*: {
  63. Connect4::InitClient 2 $sockname
  64. }
  65.  
  66. alias Connect4::InitClient {
  67. ;; Params: Server/client, client ID
  68. ;; Initiates the GUI and base variables
  69. var %Client $1,%id $2
  70. hadd -m %id Turn 1
  71. hadd -m %id Client %Client
  72. Connect4::Init %id
  73. ;; Send opponent name
  74. sockwrite -n %id 2 $+ $nick
  75. .timer $+ %id $+ KEEPALIVE 0 60 sockwrite -n %id 4
  76. }
  77. alias Connect4::UnusedID {
  78. ;parameters: none
  79. ;Finds an ID that can be used as a hash table and window name.
  80. var %id Connect4. $+ $ticks
  81. while (($hget(%id)) || ($window(@ $+ %id)) || ($sock(%id))) {
  82. var %id Connect4. $+ $ticks
  83. }
  84. return %id
  85. }
  86.  
  87. alias Connect4::Init {
  88. ;parameters: <ID>
  89. ;Initialises all values for the board, and GUI.
  90.  
  91. hadd $1 Pieces $str(0,42)
  92.  
  93. hadd $1 Wins 0
  94. hadd $1 Losses 0
  95.  
  96. Connect4::InitGUI $1
  97.  
  98. }
  99.  
  100. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  101. ; Data Handeling ;
  102. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  103. on 1:sockread:CONNECT4.*: {
  104. var %data,%id $sockname
  105. sockread %data
  106. goto $left(%data,1)
  107.  
  108. ;;Place Piece
  109. :1
  110. var %x $mid(%data,2)
  111. if (!$Connect4::TrySetPiece(%id,%x,2)) .timer 1 0 Connect4::Unverify %id
  112. return
  113.  
  114. ;;Player sends name
  115. :2
  116. hadd -m %id Opponent $mid(%data,2)
  117. Connect4::DrawNames %id
  118. return
  119.  
  120. ;;ResetBoard Packet
  121. :3
  122. Connect4::ResetBoard $sockname 1
  123. return
  124. :4
  125. return
  126. }
  127.  
  128. alias Connect4::TrySetPiece {
  129. var %id $1, %x $2,%y 0,%pieces $hget(%id, pieces),%piece $3
  130. while (%y < 8 && $mid(%pieces,$calc(%y * 7 + %x + 1),1)) {
  131. inc %y
  132. }
  133. if (%y > 5) || (%piece == 2 && $hget(%id,Client) == $hget(%id,Turn)) {
  134. return $false
  135. }
  136. var %pos $calc(%y * 7 + %x + 1)
  137. hadd -m %id pieces $left(%pieces,$calc(%pos - 1)) $+ %piece $+ $mid(%pieces,$calc(%pos + 1))
  138. Connect4::DrawPiece -n %id %x %y 20
  139. if (%piece == 2) hadd -m %id Turn $hget(%id,Client)
  140. else hadd -m %id Turn $iif($hget(%id,Client) == 1,2,1)
  141. Connect4::DrawNames %id
  142. if ($Connect4::CheckForWin(%id)) {
  143. var %winState $token($v1,1,32)
  144. var %winPos $token($v1,2-,32)
  145. if (%winState == 1) Connect4::RegisterWin %id %winPos
  146. if (%winState == 2) Connect4::RegisterLoss %id %winPos
  147. if (%winState == 3) Connect4::RegisterDraw %id
  148. }
  149. return $true
  150. }
  151.  
  152.  
  153. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  154. ; GUI
  155. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  156.  
  157.  
  158. alias Connect4::InitGUI {
  159. ;parameters: <ID>
  160. ;Calculates the default values for the GUI,
  161. ;opens the window, then draws it.
  162.  
  163. var %windowPadding 5
  164.  
  165. var %squareSize 40
  166.  
  167. hadd $1 squareSize %squareSize
  168.  
  169. var %board.x %windowPadding
  170. var %board.y %windowPadding
  171. var %board.w $calc(%squareSize * 7)
  172. var %board.h $calc(%squareSize * 6)
  173.  
  174. hadd $1 board.x %board.x
  175. hadd $1 board.y %board.y
  176. hadd $1 board.w %board.w
  177. hadd $1 board.h %board.h
  178.  
  179. var %sideBar.x $calc(%board.x + %board.w + %windowPadding)
  180. var %sideBar.y %windowPadding
  181. var %sideBar.w 200
  182. var %sideBar.h %board.h
  183.  
  184. hadd $1 sideBar.x %sideBar.x
  185. hadd $1 sideBar.y %sideBar.y
  186. hadd $1 sideBar.w %sideBar.w
  187. hadd $1 sideBar.h %sideBar.h
  188.  
  189. var %closeButton.w 80
  190. var %closeButton.h 21
  191. var %closeButton.x $calc(%sideBar.x + (%sideBar.w - %closeButton.w) / 2)
  192. var %closeButton.y $calc(%sideBar.y + %sideBar.h - %closeButton.h)
  193.  
  194. hadd $1 closeButton.x %closeButton.x
  195. hadd $1 closeButton.y %closeButton.y
  196. hadd $1 closeButton.w %closeButton.w
  197. hadd $1 closeButton.h %closeButton.h
  198.  
  199. var %window.w $calc(%sidebar.x + %sidebar.w + %windowPadding)
  200. var %window.h $calc(%board.y + %board.h + %windowPadding)
  201.  
  202. hadd $1 window.w %window.w
  203. hadd $1 window.h %window.h
  204.  
  205. window -aBdfp @ $+ $1 -1 -1 %window.w %window.h
  206.  
  207. Connect4::Draw $1
  208.  
  209. }
  210.  
  211.  
  212.  
  213. alias Connect4::Draw {
  214. ;parameters: [-n] <ID>
  215. ;Draws the whole Connect 4 Window.
  216.  
  217. var %n
  218. if ($1 == -n) {
  219. var %n 1
  220. tokenize 32 $2-
  221. }
  222.  
  223. var %white $rgb(255, 255, 255)
  224.  
  225. ;Clear the background
  226. drawrect -nrf @ $+ $1 %white 1 0 0 $hget($1,window.w) $hget($1,window.h)
  227.  
  228. Connect4::DrawBoard -n $1
  229.  
  230. Connect4::DrawSideBarStatus -n $1 -1 Connecting...
  231. Connect4::DrawScore -n $1
  232. Connect4::DrawCloseButton -n $1
  233.  
  234. if (!%n) {
  235. drawdot @ $+ $1
  236. }
  237.  
  238. }
  239.  
  240. alias Connect4::DrawBoard {
  241. ;parameters: <ID> <ALPHA>
  242.  
  243. var %n
  244. if ($1 == -n) {
  245. var %n 1
  246. tokenize 32 $2-
  247. }
  248.  
  249. var %x $hget($1,board.x)
  250. var %y $hget($1,board.y)
  251. var %w $hget($1,board.w)
  252. var %h $hget($1,board.h)
  253.  
  254.  
  255. ;Draw board
  256. drawrect -nrf @ $+ $1 $rgb(240,240,240) 1 %x %y %w %h
  257.  
  258. ;Draw board border
  259. drawrect -nr @ $+ $1 $rgb(200,200,200) 1 %x %y %w %h
  260.  
  261. var %column 0
  262.  
  263. while (%column < 7) {
  264.  
  265. var %row 0
  266.  
  267. while (%row < 6) {
  268.  
  269. Connect4::DrawPiece -n $1 %column %row $2
  270.  
  271. inc %row
  272.  
  273. }
  274.  
  275. inc %column
  276.  
  277. }
  278.  
  279. if (!%n) {
  280. drawdot @ $+ $1
  281. }
  282. }
  283.  
  284. alias Connect4::DrawPiece {
  285. ;parameters: [-n] <ID> <Column> <Row> <Alpha>
  286.  
  287. var %n
  288. if ($1 == -n) {
  289. var %n 1
  290. tokenize 32 $2-
  291. }
  292.  
  293. var %alpha $4
  294. var %pieces $hget($1,pieces)
  295.  
  296. var %piece $mid(%pieces,$calc($3 * 7 + $2 + 1),1)
  297.  
  298. var %x $hget($1,board.x)
  299. var %y $hget($1,board.y)
  300.  
  301. var %squareSize $hget($1,squareSize)
  302.  
  303. inc %x $calc(%squareSize * $2)
  304. inc %y $calc(%squareSize * (5 - $3))
  305. inc %alpha 20
  306. if (%piece == 0) {
  307.  
  308. var %colour $rgb(255, 255, 255)
  309.  
  310. }
  311.  
  312. elseif (%piece == 1) {
  313.  
  314. var %colour $rgb($calc(0 + %alpha), $calc(0 + %alpha), $calc(0 + %alpha))
  315.  
  316. }
  317.  
  318. elseif (%piece == 2) {
  319.  
  320. var %colour $rgb(255, $calc(0 + %alpha), $calc(0 + %alpha))
  321.  
  322. }
  323.  
  324. inc %x 4
  325. inc %y 4
  326. dec %squareSize 8
  327.  
  328. ;Draw piece
  329. drawrect -nrfe @ $+ $1 %colour 1 %x %y %squareSize %squareSize
  330.  
  331. ;Draw border around piece
  332. drawrect -nre @ $+ $1 $iif(%alpha == 40,65535,$rgb(200,200,200)) 1 %x %y %squareSize %squareSize
  333. ; DrawBorder normal around last piece
  334. if ($hget($1,lastMove)) {
  335. drawrect -nre @ $+ $1 $rgb(200,200,200) 1 $token($v1,1,32) $token($v1,2,32) %squareSize %squareSize
  336. }
  337. hadd -m $1 lastMove %x %y
  338. if (!%n) {
  339. drawdot @ $+ $1
  340. }
  341. }
  342.  
  343. alias Connect4::DrawCloseButton {
  344. ;parameters: <ID>
  345.  
  346. var %n
  347. if ($1 == -n) {
  348. var %n 1
  349. tokenize 32 $2-
  350. }
  351.  
  352. var %x $hget($1,closeButton.x)
  353. var %y $hget($1,closeButton.y)
  354. var %w $hget($1,closeButton.w)
  355. var %h $hget($1,closeButton.h)
  356.  
  357. drawrect -nrd @ $+ $1 $rgb(0,0,0) 1 %x %y %w %h 3 3
  358.  
  359. var %text Close
  360. var %font Tahoma
  361. var %fontSize 13
  362. var %colour $rgb(0,0,0)
  363.  
  364. var %x $int($calc(%x + (%w - $width(%text,%font,%fontSize)) / 2))
  365. var %y $int($calc(%y + (%h - $height(%text,%font,%fontSize)) / 2))
  366.  
  367. drawtext -nr @ $+ $1 %colour %font %fontSize %x %y %text
  368.  
  369. if (!%n) {
  370. drawdot @ $+ $1
  371. }
  372.  
  373. }
  374.  
  375. alias Connect4::DrawNames {
  376. ;parameters: <ID>
  377.  
  378. var %n
  379. if ($1 == -n) {
  380. var %n 1
  381. tokenize 32 $2-
  382. }
  383. var %id $1
  384. var %turn $hget($1,Turn)
  385. var %x $hget($1,sideBar.x)
  386. var %y $hget($1,sideBar.y)
  387. var %w $hget($1,sideBar.w)
  388. var %h 100
  389. var %center.x $calc(%x + %w / 2)
  390. var %center.y $calc(%y + %h / 2)
  391. var %colour $rgb(200,200,200)
  392. var %colour2 $rgb(160,160,240)
  393. var %colour3 $rgb(130,130,130)
  394. var %colour4 $rgb(240,160,160)
  395. var %size 20
  396. var %font "Tahoma"
  397. drawrect -nrf @ $+ %id %Colour 1 %x %y %w %h
  398. drawrect -nr @ $+ %id %colour3 1 %x %y %w %h
  399. drawrect -nr @ $+ %id %colour3 1 %x %y %w $calc(%h / 2)
  400. inc %y 13
  401. var %text $me
  402. var %x $calc(%center.x - $width(%text,Tahoma,%size) / 2 - 5)
  403. drawtext -nrbo @ $+ %id %colour3 $iif($hget(%id,Turn) == $hget(%id,Client),%colour2,%colour) %font %size %x %y %text
  404. inc %y 50
  405. var %text $hget(%id,Opponent)
  406. var %x $calc(%center.x - $width(%text,Tahoma,%size) / 2 - 5)
  407. drawtext -nrbo @ $+ %id %colour3 $iif($v1 == $v2,%colour,%colour4) %font %size %x %y %text
  408. if (!%n) {
  409. drawdot @ $+ $1
  410. }
  411. }
  412.  
  413. alias Connect4::DrawSideBarStatus {
  414. ;Params: [-n] <ID> <COLOR> <TEXT>
  415. if ($1 == -n) {
  416. var %n 1
  417. tokenize 32 $2-
  418. }
  419. var %id $1
  420. var %turn $hget(%id,Turn)
  421. var %x $hget(%id,sideBar.x)
  422. var %y $hget(%id,sideBar.y)
  423. var %w $hget(%id,sideBar.w)
  424. var %h 100
  425. var %center.x $calc(%x + %w / 2)
  426. var %center.y $calc(%y + %h / 2)
  427. var %colour $rgb(200,200,200)
  428. var %colour2 $iif($2 < 0,$rgb(130,130,130),$2)
  429. var %size 20
  430. var %font "Tahoma"
  431. var %text $3-
  432. drawrect -nrf @ $+ %id %colour 1 %x %y %w %h
  433. drawrect -nr @ $+ %id %colour2 1 %x %y %w %h
  434. var %x $calc(%center.x - $width(%text,Tahoma,%size) / 2 - 5)
  435. var %y $calc(%center.y / 2 + 12)
  436. drawtext -nrbo @ $+ %id %colour2 %colour %font %size %x %y %text
  437. if (!%n) drawdot @ $+ %id
  438. }
  439. alias Connect4::DrawScore {
  440. var %n
  441. if ($1 == -n) {
  442. var %n 1
  443. tokenize 32 $2-
  444. }
  445. var %id $1
  446. var %turn $hget($1,Turn)
  447. var %x $hget($1,sideBar.x)
  448. var %y $hget($1,sideBar.y) + 105
  449. var %w $hget($1,sideBar.w)
  450. var %h 100
  451. var %center.x $calc(%x + %w / 2)
  452. var %center.y $calc(%y + %h / 2)
  453. var %colour $rgb(200,200,200)
  454. var %colour3 $rgb(130,130,130)
  455. var %size 15
  456. var %font "Tahoma"
  457. drawrect -nrf @ $+ %id %Colour 1 %x %y %w %h
  458. drawrect -nr @ $+ %id %colour3 1 %x %y %w %h
  459. drawrect -nr @ $+ %id %colour3 1 %x %y %w $calc(%h / 2)
  460. var %wins $hget(%id,Wins)
  461. var %losses $hget(%id,Losses)
  462. inc %y 13
  463. var %text Wins: $hget(%id,Wins)
  464. var %x $calc(%center.x - $width(%text,Tahoma,%size) / 2 - 5)
  465. drawtext -nrbo @ $+ %id %colour3 %colour %font %size %x %y %text
  466. inc %y 50
  467. var %text Losses: $hget(%id,Losses)
  468. var %x $calc(%center.x - $width(%text,Tahoma,%size) / 2 - 5)
  469. drawtext -nrbo @ $+ %id %colour3 %Colour %font %size %x %y %text
  470. if (!%n) {
  471. drawdot @ $+ $1
  472. }
  473. }
  474.  
  475. alias Connect4::DrawEndGameText {
  476. ;;Params: <ID> <Win|Lose|Draw>
  477.  
  478. var %n
  479. if ($1 == -n) {
  480. var %n 1
  481. tokenize 32 $2-
  482. }
  483.  
  484. var %id $1
  485. var %x $hget($1,board.x)
  486. var %y $hget($1,board.y)
  487. var %w $hget($1,board.w)
  488. var %h $hget($1,board.h)
  489.  
  490. if ($2 == Win) {
  491. var %text You Win!
  492. var %colour $rgb(0,0,255)
  493. }
  494. elseif ($2 == Lose) {
  495. var %text You Lose!
  496. var %colour $rgb(255,0,0)
  497. }
  498. elseif ($2 == Draw) {
  499. var %text Draw!
  500. var %colour $rgb(0,0,0)
  501. }
  502. else {
  503. return
  504. }
  505.  
  506. var %font Tahoma
  507. var %size 50
  508. var %x $calc(%x + (%w - $width(%text,%font,%size)) / 2)
  509. var %y $calc(%y + (%h - $height(%text,%font,%size)) / 2)
  510. drawtext -nr @ $+ $1 %colour %font %size %x %y %text
  511. Connect4::DrawScore $1
  512.  
  513. if (!%n) {
  514. drawdot @ $+ $1
  515. }
  516.  
  517. }
  518. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  519. ; Register Win ;
  520. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  521. alias Connect4::CheckForWin {
  522. ;parameters: <ID>
  523. var %pieces $hget($1,Pieces)
  524.  
  525. ;Add spaces between each row
  526. var %rows $regsubex(%pieces,/(.{7})/g,\1 $+ $chr(32))
  527.  
  528. ;Horizontal
  529. if ($len($token($remove($regsubex(%rows,/(1|2)\1{3}/,\1 $+ $chr(2)),$chr(32)),1,2)) < 42) {
  530. var %pos $v1,%pos2 %pos + 1, %pos3 %pos2 + 1, %pos4 %pos3 + 1
  531. return $regml(1) %pos %pos2 %pos3 %pos4
  532. }
  533.  
  534. ;Vertical
  535. elseif ($len($token($remove($regsubex(%rows,/(1|2)(.{7}\1){3}/,\1 $+ $chr(2)),$chr(32)),1,2)) < 42) {
  536. var %pos $v1,%pos2 %pos + 7, %pos3 %pos2 + 7, %pos4 %pos3 + 7
  537. return $regml(1) %pos %pos2 %pos3 %pos4
  538. }
  539.  
  540. ;Diagonal, bottom left to top right
  541. elseif ($len($token($remove($regsubex(%rows,/(1|2)(.{8}\1){3}/,\1 $+ $chr(2)),$chr(32)),1,2)) < 42) {
  542. var %pos $v1,%pos2 %pos + 8, %pos3 %pos2 + 8, %pos4 %pos3 + 8
  543. return $regml(1) $v1 %pos %pos2 %pos3 %pos4
  544. }
  545.  
  546. ;Diagonal, up left
  547. elseif ($len($token($remove($regsubex(%rows,/(1|2)(.{6}\1){3}/,\1 $+ $chr(2)),$chr(32)),1,2)) < 42) {
  548. var %pos $v1,%pos2 %pos + 6, %pos3 %pos2 + 6, %pos4 %pos3 + 6
  549. return $regml(1) $v1 %pos %pos2 %pos3 %pos4
  550. }
  551.  
  552. ;Board is full
  553. elseif (!$regex(%pieces,/0/)) {
  554. return 3
  555. }
  556.  
  557. }
  558.  
  559. alias Connect4::RegisterWin {
  560. ;parameters <ID> <WINPOS>
  561. var %id $1
  562. hinc -m %id Wins
  563. var %l $hget(%id,lastMove),%size $hget(%id,squareSize) - 8
  564. Connect4::DrawBoard -n %id 160
  565. Connect4::DrawSideBarStatus -n %id $rgb(50,50,255) Winner!
  566. var %a 1
  567. ;echo @Debug WINN:::: $2-
  568. while ($token($2-,%a,32)) {
  569. var %y $int($calc(($v1 - 1) / 7))
  570. var %x $int($calc(($v1 - 1) % 7))
  571. Connect4::DrawPiece -n %id %x %y 40
  572. inc %a
  573. }
  574. drawRect -nre @ $+ %id 65535 1 $token(%l,1,32) $token(%l,2,32) %size %size
  575. Connect4::DrawEndGameText %id Win
  576. hadd -m %id Turn 0
  577. }
  578. alias Connect4::RegisterLoss {
  579. ;paramters <ID> <WINPOS>
  580. var %id $1
  581. hinc -m %id Losses
  582. var %l $hget(%id,lastMove),%size $hget(%id,squareSize) - 8
  583. Connect4::DrawBoard -n %id 160
  584. Connect4::DrawSideBarStatus -n %id $rgb(255,50,50) Loser!
  585. var %a 1
  586. while ($token($2-,%a,32)) {
  587. var %y $int($calc(($v1 - 1) / 7))
  588. var %x $int($calc(($v1 - 1) % 7))
  589. Connect4::DrawPiece -n %id %x %y 40
  590. inc %a
  591. }
  592. drawRect -nre @ $+ %id 65535 1 $token(%l,1,32) $token(%l,2,32) %size %size
  593. Connect4::DrawEndGameText %id Lose
  594. .timer $+ %id $+ RESET -m 1 7500 Connect4::ResetBoard %id 0
  595. hadd -m %id Turn 0
  596. }
  597. alias Connect4::RegisterDraw {
  598. ;paramters <ID>
  599. var %id $1
  600. var %firstMove $iif($hget(%id,Turn) == $hget(%id,Client),0,1)
  601. Connect4::DrawEndGameText -n %id Draw
  602. Connect4::DrawBoard -n %id 160
  603. Connect4::DrawSideBarStatus %id $rgb(240,240,240) Tie!
  604. if (!%firstMove) {
  605. .timer $+ %id $+ RESET 1 5 Connect4::ResetBoard %id 0
  606. }
  607. hadd -m %id Turn 0
  608. }
  609. alias Connect4::ResetBoard {
  610. ;;Params: <ID> <WIN/LOSE>
  611. var %id $1
  612. if ($window(@ $+ %id)) {
  613. hadd -m %id Pieces $str(0,42)
  614. Connect4::DrawBoard -n %id 0
  615. if ($2) hadd -m %id Turn $hget($1,Client)
  616. else {
  617. hadd -m %id Turn $iif($hget($1,Client) == 1,2,1)
  618. sockwrite -n %id 3
  619. }
  620. Connect4::DrawNames %id
  621. }
  622. }
  623.  
  624. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  625. ; Mouse events ;
  626. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  627.  
  628.  
  629. menu @Connect4.* {
  630.  
  631. sclick: if ($hget($mid($active,2))) Connect4::Click $mid($active,2)
  632. dclick: if ($hget($mid($active,2))) Connect4::Click $mid($active,2)
  633.  
  634. }
  635.  
  636. alias Connect4::Click {
  637. var %id $1
  638. ;parameters: <ID>
  639. ;Called when mouse is clicked in a Connect 4 window.
  640.  
  641. var %board.x $hget($1,board.x)
  642. var %board.y $hget($1,board.y)
  643. var %board.w $hget($1,board.w)
  644. var %board.h $hget($1,board.h)
  645.  
  646. var %squareSize $hget($1,squaresize)
  647.  
  648. if ($inrect($mouse.x, $mouse.y, %board.x, %board.y, %board.w, %board.h)) {
  649.  
  650. var %column $int($calc(($mouse.x - %board.x) / %squareSize))
  651. var %row $calc(5 - $int($calc(($mouse.y - %board.y) / %squareSize)))
  652. ;; Imk0tter code :P
  653. if ($hget(%id,Turn) == $hget(%id,Client)) {
  654. if ($Connect4::TrySetPiece(%id,%column,1)) {
  655. sockwrite -n %id 1 $+ %column
  656. }
  657. }
  658. ;; End Imk0tter
  659. }
  660. elseif ($inroundrect($mouse.x,$mouse.y,$hget($1,closeButton.x),$hget($1,closeButton.y),$hget($1,closeButton.w),$hget($1,closeButton.h),3,3)) {
  661. Connect4::DisconnectClient %id
  662. }
  663.  
  664. }
  665.  
  666. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  667. ; Closing events
  668. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  669.  
  670.  
  671. on 1:sockclose:CONNECT4.*: {
  672. Connect4::DisconnectClient $sockname
  673. }
  674.  
  675. alias Connect4::DisconnectClient {
  676. ;Parameters <ID> <WINDOWCLOSE>
  677. var %id $1,%closedWindow $2
  678. if ($sock(%id)) sockclose %id
  679. if (!%closedwindow) {
  680. Connect4::DrawSidebarStatus -n %id $rgb(255,0,0) Disconnected...
  681. Connect4::DrawBoard %id 160
  682. }
  683. if ($hget(%id)) hfree %id
  684. if ($timer(%id $+ RESET)) .timer $+ %id $+ RESET off
  685. if ($timer(%id $+ KEEPALIVE)) .timer $+ %id $+ KEEPALIVE off
  686. ;Draw "Disconnected..." text
  687. }
  688. on 1:close:@: {
  689.  
  690. if (@Connect4.* iswm $target) {
  691. var %id $mid($target,2)
  692. Connect4::DisconnectClient %id 1
  693. }
  694. }
  695.  
  696. alias Connect4::Unverify {
  697. ;; Parameters: ID
  698. Connect4::DisconnectClient %id
  699. var %id $1, %x $input($hget(%id,Opponent) has sent a false move.. Disconnecting client.,o)
  700. }
Add Comment
Please, Sign In to add comment