Advertisement
crabb

keiki

Jul 4th, 2020
1,373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. --[[
  2. ***************************************************************
  3.     * > keiki object pooler
  4.     *
  5.     * > can pool up to 65536 objects at once.
  6.     * > designed to be used with callbacks, so you can
  7.         *   customize pools to your heart's content.
  8.     *
  9.     * > by suwacrab 2020:28:06
  10. ***************************************************************
  11. --]]
  12.  
  13. local keiki = {}
  14. keiki.__index = keiki
  15.  
  16.  
  17. --|==== function reference ===|
  18. --[[
  19.     * > function list:
  20.     * keiki.new(len,def): creates new list w/ length `len` and optional callback `def`
  21.     * keiki:add(mode,f) : adds new object w/ mode `mode` and optional callback `f`
  22.     * keiki:del(id,f)   : deletes object [id] and uses optional callback `f`
  23.     * keiki:iter(f)     : iterates through pool with callback `f`
  24.     *
  25.     * > structure info:
  26.     * every pool has 4 attributes:
  27.         ・objs: the (0-indexed!) array of objects
  28.         ・len: the length of objects
  29.         ・last: a pointer to the last object in the
  30.             pool. this is used for adding and removing
  31.             objects from the pool's free list
  32.         ・alive: the number of alive objects
  33.     * every object has 4 attributes, but you're free to add more to them:
  34.         ・mode: the object's "type". you can use this
  35.             for using different types of objects inside one pool.
  36.         ・dead: whether or not the object's alive.
  37.         ・id: the object's index in the array.
  38.         ・next: a pointer to the next object. used for the pool's free
  39.             list.
  40. ]]
  41.  
  42.  
  43. --|==== functions =============|
  44. -- * > creates a new object pool
  45. -- * ・len: length of army, must be under 0x10000.
  46. -- * ・def: callback on every object. 1st is the list,2nd is index.
  47. keiki.new = function(len,def)
  48.     local may = { objs = {},len = len,last = 0,alive = 0 }
  49.     for i = 0,len-1 do
  50.         may.objs[i] = { mode=0,dead=true,id=i,next=i+1 }
  51.         if def then def(may,i,may.objs[i]) end
  52.     end
  53.     may.objs[len-1].next = 0xFFFF
  54.    
  55.     return setmetatable(may,keiki)
  56. end
  57.  
  58. --> adds a new object to pool.
  59. -- > may: the list to add to
  60. -- > mode: the type of object
  61. -- > f: the callback on the new object. works the same as keiki.new's callback.
  62. keiki.add = function(may,mode,f)
  63.     -- if there's no space, return nil.
  64.     if may.alive == may.len then return end
  65.     -- find the nearest object, reset it's attributes
  66.     local hani = may.objs[may.last]
  67.     hani.dead = false
  68.     hani.mode = mode or 0;
  69.     if f then f(may,hani.id,hani) end
  70.     -- make sure this object will be the next free object once it's dead
  71.     may.last = hani.next
  72.     -- increment the alive count, then return the new object.
  73.     may.alive = may.alive+1
  74.     return hani;
  75. end
  76.  
  77. --> deletes object [id] from the pool.
  78. -- > may: the list to remove from
  79. -- > id: the id of the object to remove.
  80. -- > f: callback on the deleted object. works the same as keiki.new's callback.
  81. keiki.del = function(may,id,f)
  82.     -- if the deleted object doesn't exist, return nil.
  83.     local hani = may.objs[id]
  84.     if(hani==nil) then return end
  85.     -- reset variables.
  86.     hani.dead = true
  87.     if f then f(may,hani.id,hani) end
  88.     -- hani's next object should be the last free object
  89.     -- the new free object should be hani
  90.     hani.next = may.last
  91.     may.last = id
  92.     -- decrement alive counter
  93.     may.alive = may.alive-1
  94.     return hani
  95. end
  96.  
  97. --> iterates through pool with the callback f.
  98. -- > may: the pool to iterate through
  99. -- > f: the callback to use.
  100. -- >> 1st argument is the list, 2nd is the id, 3rd is the current object.
  101. keiki.iter = function(may,f)
  102.     for i = 0,may.len-1 do
  103.         f(may,i,may.objs[i])
  104.     end
  105. end
  106.  
  107. return keiki
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement