summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2017-06-25 13:27:03 -0500
committerDavid McMackins II <contact@mcmackins.org>2017-06-25 13:27:03 -0500
commit123fef1939abe022dacc81395e349e66fa0868ad (patch)
treec7d90be28979a5746986cd003d628ed36262c32d
parent80309291968164501a42e36a20b459a399390cc4 (diff)
Add basic TRYMOVE, needs improvement for other move functions
-rw-r--r--serverthread.lua64
1 files changed, 63 insertions, 1 deletions
diff --git a/serverthread.lua b/serverthread.lua
index 474a9bf..32fe88c 100644
--- a/serverthread.lua
+++ b/serverthread.lua
@@ -17,6 +17,7 @@
require 'board'
require 'class'
+require 'geom'
require 'piece'
require 'util'
@@ -81,7 +82,6 @@ function Server:_resetgame()
self._turn = 1
self._selected = nil
self._targetspace = nil
- self._wantsplit = false
self._winner = nil
end
@@ -93,6 +93,50 @@ function Server:_toggleturn()
end
end
+function Server:_trymove(quad, distance)
+ if distance == 2 and not self:_trymove(quad, 1) then
+ return nil
+ end
+
+ local x, y
+
+ if quad == 1 then
+ x = self._selected.x + distance
+ y = self._selected.y + distance
+ elseif quad == 2 then
+ x = self._selected.x - distance
+ y = self._selected.y + distance
+ elseif quad == 3 then
+ x = self._selected.x - distance
+ y = self._selected.y - distance
+ else
+ x = self._selected.x + distance
+ y = self._selected.y - distance
+ end
+
+ if not self._selected:canmove(x, y) then
+ return nil
+ end
+
+ return {x=x, y=y}
+end
+
+function Server:_getmove(dx, dy, wantsplit)
+ local quad = getquadrant(dx, dy)
+ if not quad then
+ return nil
+ end
+
+ if wantsplit then
+ local move = self:_trymove(quad, 2)
+ if move then
+ return move
+ end
+ end
+
+ return self:_trymove(quad, 1)
+end
+
function Server:_sendall(msg)
for _,sock in ipairs(self._socks) do
sock:send(msg)
@@ -185,6 +229,24 @@ function Server:_process()
self._selected = nil
self._socks[this]:send(AFFIRMATIVE)
end
+ elseif line:startswith('TRYMOVE') then
+ line = line:split(' ')
+ if self._turn ~= this or not self._selected then
+ self._socks[this]:send(NEGATIVE)
+ elseif #line ~= 3 then
+ self._socks[this]:send(ERR_ARGNUM)
+ else
+ local dx = tonumber(line[2])
+ local dy = tonumber(line[3])
+
+ if not dx or not dy then
+ self._socks[this]:send(ERR_SYNTAX)
+ else
+ local move = self:_getmove(dx, dy, false)
+ self._socks[this]:send(
+ table.concat({'Y', move.x, move.y, 'END'}, '\n'))
+ end
+ end
elseif line == 'FORFEIT' then
self._sendall('FORFEIT ' .. this .. '\nSHUTDOWN\nEND\n')
self._done = true