diff --git a/battlefield.lua b/battlefield.lua --- a/battlefield.lua +++ b/battlefield.lua @@ -3,12 +3,10 @@ local open = { {p = {{x1, y1}}, l = 0} }; - - --print("call", x2, y2); - + while #open > 0 do local item = table.remove (open, 1); - + local node = item.p[#item.p]; local inClosed = false; for i = 1, #closed do @@ -25,12 +23,12 @@ table.insert(closed, node); --print("closed", #closed); --print("open", #open); - + local pos = 1; while (#open >= pos and open[pos].l <= item.l) do pos = pos + 1; end; - + if s:isPassable(node[1], node[2] - 1) or (node[1] == x2 and node[2] - 1 == y2) then local p = {}; @@ -43,7 +41,7 @@ l = item.l + 1 }); end; - + if s:isPassable(node[1], node[2] + 1) or (node[1] == x2 and node[2] + 1 == y2) then local p = {}; @@ -56,8 +54,7 @@ l = item.l + 1 }); end; - - + if s:isPassable(node[1] - 1, node[2]) or (node[1] - 1 == x2 and node[2] == y2) then local p = {}; @@ -70,8 +67,7 @@ l = item.l + 1 }); end; - - + if s:isPassable(node[1] + 1, node[2]) or (node[1] + 1 == x2 and node[2] == y2) then local p = {}; @@ -86,7 +82,7 @@ end; end; end; - + return nil; end; @@ -131,8 +127,9 @@ battlefield = function(tab) return false; end; - for i = 1, #s.obj do - if (s.obj[i].obst and s.obj[i].obst > 0) then + for i = 1, #s.obj do + local obst = EngineUtils.getValue(s.obj[i].obst, s.obj[i]); + if (obst and obst > 0) then if (s.obj[i].x == x and s.obj[i].y == y) then return false; end; @@ -147,8 +144,9 @@ battlefield = function(tab) return false; end; - for i = 1, #s.obj do - if (s.obj[i].obst and s.obj[i].obst > 1) then + for i = 1, #s.obj do + local obst = EngineUtils.getValue(s.obj[i].obst, s.obj[i]); + if (obst and obst > 1) then if (s.obj[i].x == x and s.obj[i].y == y) then return false; end; @@ -193,7 +191,6 @@ battlefield = function(tab) end; else --vertical line - print("2--2"); local x = 0; for y = 0, dy do print(x, y); @@ -479,18 +476,17 @@ combatant = function(tab) end; tab.canthrust = function(s) - local dist = here().getDistance(here().plX, here().plY, s.x, s.y); - return dist <= 1 and not tab.ally; + return math.abs(s.x - here().plX) + math.abs(s.y - here().plY) <= 2 and not s.ally; end; tab.canhit = function(s) - local dist = here().getDistance(here().plX, here().plY, s.x, s.y); - return dist == 0 and not tab.ally; + return (math.abs(s.x - here().plX) == 1 and s.y == here().plY or + s.x == here().plX and math.abs(s.y - here().plY) == 1) and not tab.ally; end; tab.onhit = function(s) - local dist = here().getDistance(here().plX, here().plY, s.x, s.y); - if (dist == 0) then + if (math.abs(s.x - here().plX) == 1 and s.y == here().plY or + s.x == here().plX and math.abs(s.y - here().plY) == 1) then tab.hp = tab.hp - 2; if (tab.hp > 0) then p(tab.wpnHit); @@ -509,17 +505,16 @@ combatant = function(tab) if tab.ally then return tab.nohit; end; - - local dist = here().getDistance(here().plX, here().plY, s.x, s.y); - - if (dist == 0) then + + if (math.abs(s.x - here().plX) == 1 and s.y == here().plY or + s.x == here().plX and math.abs(s.y - here().plY) == 1) then tab.hp = tab.hp - 1; if (tab.hp > 0) then p(tab.handHit); else p(tab.handKill); end; - + here():make_turn(); else p(tab.handFar); @@ -531,7 +526,13 @@ combatant = function(tab) end; end; if (not tab.obst) then - tab.obst = 2; + tab.obst = function(s) + if (s.hp > 0) then + return 2; + else + return 0; + end; + end; end; return obj(tab);