Forum

> > CS2D > Scripts > Is this function correct?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Is this function correct?

5 replies
To the start Previous 1 Next To the start

old Is this function correct?

Mami Tomoe
User Off Offline

Quote
Hello!
I have made a function but because the script is big I'm not sure if it's working as intended.
The script is RPG and the function gets a monster's TILEX and TILEY and compares it with all players' TILEX and TILEY to see if they're in range.
The range is CONFIG.SPAWNDISTANCE which is static and equal to 128.

It will basically return true if the monster is within 128 X,Y tiles of any player or false if not.

This is an image I used if you want to take a look and the function is below it.

IMG:https://i.imgur.com/NeoevRS.png

P > Player
M > Monster
X > TileX
Y > TileY
-+ > X smaller and Y bigger (and so on for the rest)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function InPlayerRange(tilex, tiley)
	local XDist, YDist = 0, 0
	for _,id in pairs(player(0,"tableliving")) do
		if (tilex > player(id,"tilex")) and (tiley > player(id,"tiley")) then -- ++
			XDist = tilex - player(id,"tilex")
			YDist = tiley - player(id,"tiley")
		elseif (tilex < player(id,"tilex")) and (tiley < player(id,"tiley")) then -- --
			XDist = player(id,"tilex") - tilex
			YDist = player(id,"tiley") - tiley
		elseif (tilex > player(id,"tilex")) and (tiley < player(id,"tiley")) then -- +-
			XDist = tilex - player(id,"tilex")
			YDist = player(id,"tiley") - tiley
		elseif (tilex < player(id,"tilex")) and (tiley > player(id,"tiley")) then -- -+
			XDist = player(id,"tilex") - tilex
			YDist = tiley - player(id,"tiley")
		end
		if XDist <= CONFIG.SPAWNDISTANCE and YDist <= CONFIG.SPAWNDISTANCE then
			return true
		end
	end
	return false
end

old Re: Is this function correct?

Bowlinghead
User Off Offline

Quote
Why dont you use
1
a² + b² = c²

Then you look wether c is > 100 or not.
On this way you got a circle which is probably what you want.
So:
1
2
3
4
5
6
7
deltaX = math.abs(player(id,"tilex") - monsterX) -- math.abs deletes all "-". So wether its 5 -3 or 3-5 its always +2!
deltaY = math.abs(player(id,"tiley") - monsterY)

deltaX = deltaX* deltaX -- ^2
deltaY = deltaY* deltaY

return math.sqrt(deltaX + deltaY) -- return c

IMG:https://i.imgur.com/6Uv5Vj8.png

old Re: Is this function correct?

Bowlinghead
User Off Offline

Quote
c is the distance between the player and the monster.

So if the number in return math.sqrt(deltaX+ deltaY) is <= 128 then its within the spawn distance, else its not.

old Re: Is this function correct?

Cure Pikachu
User Off Offline

Quote
@user Bowlinghead: Squaring a negative number will give you a positive number so you don't even need
math.abs()
(You only need it if you for example, also want the scalar distance per axis). So I can basically do this:
local x, y, dis
x = (player(id,"tilex")-tilex)^2
y = (player(id,"tiley")-tiley)^2
return math.sqrt(x+y)
edited 1×, last 23.11.18 01:39:56 pm

old Re: Is this function correct?

Mami Tomoe
User Off Offline

Quote
I ended up using this and it works just like mine but for less code, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
function InPlayerRange(tilex, tiley)
	for _,id in pairs(player(0,"tableliving")) do
		local x, y, dist
		x = (player(id,"tilex")-tilex)^2 
		y = (player(id,"tiley")-tiley)^2
		dist = math.sqrt(x+y)
		if dist <= CONFIG.SPAWNDISTANCE then
			return true
		end
	end
	return false
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview