Create can_see.lua

This commit is contained in:
taxicomics 2024-05-27 15:58:59 +02:00 committed by GitHub
parent 553e09e977
commit 3573330dc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 32 additions and 0 deletions

32
lua_scripts/can_see.lua Normal file
View File

@ -0,0 +1,32 @@
--This is a basic raycast function.
--It checks whether two points on a map can "see" each other based on the map data. It assumes flag 0 to be the collision layer.
--It returns TRUE if they can see each other or FALSE if they can't.
function can_see(x1,y1,x2,y2,length)
--x1 and y1 are the point of view,length is the max distance of the two points
local max_length=length or 1000
local xvec=0
local yvec=0
local len=0
local ret=true
local tx=x1
local ty=y1
xvec=x2-x1
yvec=y2-y1
len=sqrt(xvec^2+yvec^2)
if len==0 or len>max_length then
ret=false
end
xvec=(xvec/len)
yvec=(yvec/len)
if ret then
for i=1,len do
tx+=xvec
ty+=yvec
if fget(mget(flr(tx/8),flr(ty/8)),0) then
ret=false
break
end
end
end
return ret
end