HC/spatialhash.lua

153 lines
4.3 KiB
Lua
Raw Normal View History

2011-01-13 13:38:36 +00:00
--[[
Copyright (c) 2011 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
2011-02-07 18:03:19 +00:00
local floor = math.floor
local min, max = math.min, math.max
local _PACKAGE = (...):match("^(.+)%.[^%.]+")
if not (common and common.class and common.instance) then
class_commons = true
require(_PACKAGE .. '.class')
end
local vector = require(_PACKAGE .. '.vector')
2011-01-13 13:38:36 +00:00
-- special cell accesor metamethods, so vectors are converted
-- to a string before using as keys
local cell_meta = {}
function cell_meta.__newindex(tbl, key, val)
2011-02-02 20:04:00 +00:00
return rawset(tbl, key.x..","..key.y, val)
2011-01-13 13:38:36 +00:00
end
function cell_meta.__index(tbl, key)
2011-02-02 20:04:00 +00:00
local key = key.x..","..key.y
2011-01-13 13:38:36 +00:00
local ret = rawget(tbl, key)
if not ret then
ret = setmetatable({}, {__mode = "kv"})
rawset(tbl, key, ret)
end
return ret
end
local Spatialhash = {}
function Spatialhash:init(cell_size)
2011-01-13 13:38:36 +00:00
self.cell_size = cell_size or 100
self.cells = setmetatable({}, cell_meta)
end
2011-01-13 13:38:36 +00:00
function Spatialhash:cellCoords(v)
2011-02-07 18:03:19 +00:00
return {x=floor(v.x / self.cell_size), y=floor(v.y / self.cell_size)}
2011-01-13 13:38:36 +00:00
end
function Spatialhash:cell(v)
return self.cells[ self:cellCoords(v) ]
end
function Spatialhash:insert(obj, ul, lr)
local ul = self:cellCoords(ul)
local lr = self:cellCoords(lr)
for i = ul.x,lr.x do
for k = ul.y,lr.y do
2011-02-07 18:03:19 +00:00
rawset(self.cells[ {x=i,y=k} ], obj, obj)
2011-01-13 13:38:36 +00:00
end
end
end
function Spatialhash:remove(obj, ul, lr)
-- no bbox given. => must check all cells
if not ul or not lr then
for _,cell in pairs(self.cells) do
2011-02-07 18:03:19 +00:00
rawset(cell, obj, nil)
2011-01-13 13:38:36 +00:00
end
return
end
local ul = self:cellCoords(ul)
local lr = self:cellCoords(lr)
2011-02-07 18:03:19 +00:00
-- else: remove only from bbox
2011-01-13 13:38:36 +00:00
for i = ul.x,lr.x do
for k = ul.y,lr.y do
2011-02-07 18:03:19 +00:00
rawset(self.cells[{x=i,y=k}], obj, nil)
2011-01-13 13:38:36 +00:00
end
end
end
-- update an objects position
function Spatialhash:update(obj, ul_old, lr_old, ul_new, lr_new)
2011-01-13 15:15:35 +00:00
local ul_old, lr_old = self:cellCoords(ul_old), self:cellCoords(lr_old)
local ul_new, lr_new = self:cellCoords(ul_new), self:cellCoords(lr_new)
if ul_old.x == ul_new.x and ul_old.y == ul_new.y and
lr_old.x == lr_new.x and lr_old.y == lr_new.y then
return
end
2011-01-13 13:38:36 +00:00
for i = ul_old.x,lr_old.x do
for k = ul_old.y,lr_old.y do
rawset(self.cells[{x=i,y=k}], obj, nil)
end
end
for i = ul_new.x,lr_new.x do
for k = ul_new.y,lr_new.y do
rawset(self.cells[{x=i,y=k}], obj, obj)
2011-01-13 13:38:36 +00:00
end
end
end
function Spatialhash:getNeighbors(obj, ul, lr)
local ul = self:cellCoords(ul)
local lr = self:cellCoords(lr)
2011-02-26 16:27:04 +00:00
local set = {}
2011-01-13 13:38:36 +00:00
for i = ul.x,lr.x do
for k = ul.y,lr.y do
2011-02-02 20:04:00 +00:00
local cell = self.cells[{x=i,y=k}] or {}
2011-01-13 13:38:36 +00:00
for other,_ in pairs(cell) do
rawset(set, other, other)
2011-01-13 13:38:36 +00:00
end
end
end
rawset(set, obj, nil)
return set
2011-01-13 13:38:36 +00:00
end
2011-01-18 17:55:51 +00:00
function Spatialhash:draw(how, show_empty, print_key)
if show_empty == nil then show_empty = true end
for k,cell in pairs(self.cells) do
local empty = true
(function() for _ in pairs(cell) do empty = false; return end end)()
if show_empty or not empty then
local x,y = k:match("([^,]+),([^,]+)")
x = x * self.cell_size
y = y * self.cell_size
love.graphics.rectangle(how, x,y, self.cell_size, self.cell_size)
if print_key then
love.graphics.print(k, x+3,y+3)
end
end
end
end
return common.class('Spatialhash', Spatialhash)