From decbe4831c78f3d78a9f00b42179a608fb9c128c Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Mon, 11 Nov 2024 13:57:15 +0200 Subject: [PATCH] lua: add simple spatial search for edicts --- Misc/qs_pak/scripts/edicts.lua | 48 +++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Misc/qs_pak/scripts/edicts.lua b/Misc/qs_pak/scripts/edicts.lua index 07c44498..dcbc826a 100644 --- a/Misc/qs_pak/scripts/edicts.lua +++ b/Misc/qs_pak/scripts/edicts.lua @@ -168,9 +168,10 @@ local mods = progs.mods local localize = text.localize -local vec3origin = vec3.new() -local vec3one = vec3.new(1, 1, 1) -local vec3minusone = vec3.new(-1, -1, -1) +local vec3new = vec3.new +local vec3origin = vec3new() +local vec3one = vec3new(1, 1, 1) +local vec3minusone = vec3new(-1, -1, -1) local vec3mid = vec3.mid local FL_MONSTER = edicts.flags.FL_MONSTER @@ -600,6 +601,47 @@ function edicts.massacre() end +function edicts.boxsearch(halfedge, origin) + if not halfedge then + halfedge = 256 + end + + local edictcount = #edicts + + if edictcount == 0 then + return + end + + if not origin then + origin = edicts[2].origin + end + + local halfedgevec = vec3new(halfedge, halfedge, halfedge) + local minpos = origin - halfedgevec + local maxpos = origin + halfedgevec + local minx, miny, minz = minpos.x, minpos.y, minpos.z + local maxx, maxy, maxz = maxpos.x, maxpos.y, maxpos.z + local result = {} + + for i = 3, edictcount do -- skip worldspawn and player + local edict = edicts[i] + local absmin = edict.absmin + local absmax = edict.absmax + + if not isfree(edict) then + local hasintersection = minx <= absmax.x and miny <= absmax.y and minz <= absmax.z + and maxx >= absmin.x and maxy >= absmin.y and maxz >= absmin.z + + if hasintersection then + insert(result, edict) + end + end + end + + return result +end + + --- --- Host helpers ---