Advertisement
dalvorsn

getCreaturesInRetangleArea(pos_init, pos_final)

Mar 16th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. function getCreaturesInRetangleArea(pos_init, pos_final)
  2.     --[[
  3.     getCreaturesInRetangleArea(pos_init, pos_final)
  4.  
  5.     * pos_init(1) = ponta esquerda superior da area
  6.     * pos_final(2) = ponta direita inferior da area
  7.     @return table -> lista de uids das criaturas na area
  8.     ]]
  9.  
  10.     -- private
  11.     local function isPosition(pos)
  12.         return (type(pos) == "table" and type(pos.x) == "number" and type(pos.y) == "number" and type(pos.z) == "number")
  13.     end
  14.  
  15.  
  16.     local creatureList = {}
  17.     local error = nil
  18.  
  19.     if not (isPosition(pos_init) and isPosition(pos_final)) then
  20.         error = debug.traceback("Invalid parameters, expected positions")
  21.     end
  22.  
  23.     if not (pos_init.x <= pos_final.x and pos_init.y <= pos_init.y and pos_init.z == pos_final.z) then
  24.         error = debug.traceback("can't form a retangle with positions")
  25.     end
  26.  
  27.     if error then
  28.         print(error)
  29.         return false
  30.     end
  31.  
  32.     for x = pos_init.x, pos_final.x do
  33.         for y = pos_init.y, pos_final.y do
  34.             local pos = {x = x, y = y, z = pos_init.z}
  35.             for stack = 0, getTileInfo(pos).things do
  36.                 pos.stackpos = stack
  37.                 local thing = getThingfromPos(pos)
  38.                 if isCreature(thing.uid) then
  39.                     table.insert(creatureList, thing.uid)
  40.                 end
  41.             end
  42.         end
  43.     end
  44.  
  45.     return creatureList
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement