diff --git a/world.lua b/world.lua index 421489f..4e6df71 100644 --- a/world.lua +++ b/world.lua @@ -1,4 +1,5 @@ ---- Procedural generation methods +--- Procedural generation methods, and other aspects of generating the +--- environment function init_world() -- Metadata for different biomes @@ -156,14 +157,8 @@ end --- ---- mod buffer functions - these handle parts of the world map that have ---- changed from their 'default' position ---- ---- todo: the mod buffer eventually needs to be a bit more elaborate. ---- we need to make it possible to cull only old and unimportant changes, ---- so each entry needs a 'critical' flag and something to indicate order ---- added. We could keep 2 buffers for the latter, one that's just keys ---- in an array... expensive though. +--- mod buffer functions - these handle locations on the world map that have +--- changed from their 'default' state --- -- x and y are global coords @@ -173,24 +168,34 @@ end -- x and y are map-local coords function write_map_change(new_sprite, x, y, perm) - if #mod_buffer >= 8192 then - cull_mod_buffer() - end local key = get_mod_key(x, y) mod_buffer[key] = new_sprite -- the queue gives us a time-ordered list of items to delete. -- anything that should persist is simply not added to the queue, - -- making it un-deletable. + -- making it un-deletable. It also doesn't count against the maximum + -- size of the mod queue before deletion, meaning it permanently inflates + -- the size of ram. -- obviously if we end up with a *very large number* of persistent -- objects we can run into trouble, but this is functionally a design - -- limitation. + -- limitation. We also only save 32 of these on exit, further limiting + -- the possible size of the 'memory leak' pool, as it were. if not perm then add(mod_queue, key) end + + if #mod_queue >= 8192 then + cull_mod_buffer() + end end function cull_mod_buffer() -- we cull 512 entries at a time. - -- stub: implement me! + local count = 0 + for i=1,511 do + local key = mod_queue[1] + if (not key) return -- check that we're not out of items for some reason + mod_buffer[key] = nil + del(mod_queue, key) + end end