Skip to content
Snippets Groups Projects
Commit 21dcc8cf authored by DarkGod's avatar DarkGod
Browse files

Merge branch 'game_act_fix' into 'master'

Occasional turn loss fix.

Game.tick now creates a copy of entitites' list to iterate over since iterating over a table while modifying it is undefined.

tickLevel now adjusts indices accordingly when entity is added or removed.
parents bfdacbd6 875e330f
No related branches found
No related tags found
No related merge requests found
......@@ -72,9 +72,9 @@ function _M:tick()
self.level = mainlev
end
local arr = self.entities
local arr = table.clone(self.entities)
for i, e in pairs(arr) do
e = arr[i]
e = self.entities[i]
if e and e.act and e.energy then
if e.energy.value < self.energy_to_act then
e.energy.value = (e.energy.value or 0) + self.energy_per_tick * (e.energy.mod or 1) * (e.global_speed or 1)
......@@ -97,19 +97,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
......@@ -131,12 +131,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
......
......@@ -107,6 +107,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
......@@ -126,12 +129,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