raze/polymer/eduke32/source/lunatic/test/maputil.lua
helixhorned 6458c0ba34 a.m32: add state 'for_sprites_near_picnum'.
The state quries the user for four values: ref. picnum, target picnum, max.
ldist and a lotag.  Then, for all sprites i (of ref. picnum), for all sprites j
(of target picnum), if ldist(i,j)<=maxldist, sprite j's lotag is changed to the
provided one.

Also add lunatic/test/maputil.lua, containing a similar function but allowing
to customize the selection predicates of the ref. and target sprites, as well
as the action to carry out.

git-svn-id: https://svn.eduke32.com/eduke32@4414 1a8010ca-5511-0410-912e-c29ae57300e0
2014-04-05 11:28:08 +00:00

63 lines
2 KiB
Lua

local xmath = require("xmath")
local sprite = sprite
--
local maputil = {}
-- csfunc = maputil.CreateCrossSpriteFunc(use_sprite_i_func, use_sprite_j_func)
--
-- use_sprite_i_func: function(spri, ud), where
-- <spri> is a reference to sprite i
-- use_sprite_j_func: function(sprj, spri, ud), where
-- <spri> is a reference to sprite i
-- <sprj> is a reference to sprite j
--
-- csfunc: function(userdata, process_sprite_j_func), where
-- <userdata> is passed as <ud> to the functions described above
-- <process_sprite_j_func>: function(sprj, spri, ud), the user-provided
-- function that processes sprite j in some way
function maputil.CreateCrossSpriteFunc(use_sprite_i_func, use_sprite_j_func)
return function(userdata, process_sprite_j_func)
for i in sprite.all() do
if (use_sprite_i_func(sprite[i], userdata)) then
for j in sprite.all() do
if (use_sprite_j_func(sprite[j], sprite[i], userdata)) then
process_sprite_j_func(sprite[j], sprite[i], userdata)
end
end
end
end
end
end
-- Functions for "for all sprites Y of tile X that are closer than D, ..."
-- Passed userdefs: { picnumi, picnumj, maxldist }
local function snearpic_usei(spr, ud)
return spr.picnum == ud[1]
end
local function snearpic_usej(sprj, spri, ud)
return sprj.picnum == ud[2] and xmath.ldist(spri, sprj) <= ud[3]
end
local snearpic_func = maputil.CreateCrossSpriteFunc(snearpic_usei, snearpic_usej)
-- maputil.for_sprites_near_picnum(picnumi, picnumj, dist, process_sprite_func)
--
-- Runs the following loop:
--
-- for all sprites i with tile <picnumi>,
-- for all sprites j of tile <picnumj> that are closer to sprite i than <dist> [*],
-- call process_sprite_func(sprite[j], sprite[i])
--
-- [*] using xmath.ldist()
function maputil.for_sprites_near_picnum(picnumi, picnumj, dist, process_sprite_func)
snearpic_func({picnumi, picnumj, dist}, process_sprite_func)
end
return maputil