Skip to content
Snippets Groups Projects
Commit 71f8c033 authored by Alex Ksandra's avatar Alex Ksandra
Browse files

Made the game being gentle about e_array modifications.

parent fb397b15
No related branches found
No related tags found
1 merge request!120Occasional turn loss fix.
......@@ -94,19 +94,19 @@ function _M:tickLevel(level)
local arr = level.e_array
if level.last_iteration then
i = nil
for ii = 1, #arr do if arr[ii] == level.last_iteration.e then i = ii + 1 break end end
if not i then i = level.last_iteration.i + 1 end
i = level.last_iteration.i + 1
if i > #arr then i = 1 end
level.last_iteration = nil
-- print("=====LEVEL", level.level, level.sublevel_id, "resuming tick loop at ", i, arr[i].name)
end
for i = i, #arr do
level.last_iteration = {} -- mark as iterating
local iter = level.last_iteration
local finished = true
while i <= #arr do
e = arr[i]
iter.e, iter.i = e, i
if e and e.act and e.energy then
if e.actBase and e.energyBase then
if e.energyBase < self.energy_to_act then
......@@ -128,12 +128,14 @@ function _M:tickLevel(level)
-- print(">ENERGY", e.name, e.uid, "::", e.energy.value, self.paused, "::", e.player)
if self.can_pause and self.paused then
level.last_iteration = {i=i, e=e}
finished = false
-- print("====LEVEL", level.level, level.sublevel_id, "pausing tick loop at ", i, e.name)
break
end
end
i = iter.i + 1
end
if finished then level.last_iteration = nil end
end
--- Called every game turns
......
......@@ -106,6 +106,9 @@ function _M:addEntity(e, after, no_error)
end
if pos then
table.insert(self.e_array, pos+1, e)
if self.last_iteration and self.last_iteration.i >= pos then
self.last_iteration.i = self.last_iteration.i + 1
end
else
table.insert(self.e_array, e)
end
......@@ -125,12 +128,19 @@ function _M:removeEntity(e, force)
if not self.entities[e.uid] and not force then error("Entity "..e.uid.."("..(e.name or "???")..") not present on the level") end
self.entities[e.uid] = nil
local pos = nil
for i = 1, #self.e_array do
if self.e_array[i] == e then
table.remove(self.e_array, i)
pos = i
break
end
end
if pos then
if self.last_iteration and self.last_iteration.i >= pos then
self.last_iteration.i = self.last_iteration.i - 1
end
table.remove(self.e_array, pos)
end
game:removeEntity(e)
-- Tells it to delete itself if needed
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment