Forum

> > CS2D > Scripts > Is a player in a zone check
Forums overviewCS2D overview Scripts overviewLog in to reply

English Is a player in a zone check

3 replies
To the start Previous 1 Next To the start

old Is a player in a zone check

EngiN33R
Moderator Off Offline

Quote
So I was making my mod and I stumbled upon this noobish problem - how do I check whether a player is in zone1 and return true if there are >1 zones, so, for example, I have this table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
zones = {
	[1] = {
		mnx = 1,
		mxx = 5,
		mny = 1,
		mxy = 5,
	},
	[2] = {
		mnx = 6,
		mxx = 11,
		mny = 6,
		mxy = 11,
	},
	[3] = {
		mnx = 12,
		mxx = 17,
		mny = 12,
		mxy = 17,
	},
}
And I have this function:
1
2
3
4
5
6
7
function checkInZone(id,z)
	if (player(id,"tilex")>=zones[1].mnx and player(id,"tilex")<=zones[1].mxx and player(id,"tiley")>=zones[1].mny and player(id,"tiley")<=zones[1].mxy) then
		return true
	else
		return false
	end
end
And finally, I have this hook:
1
2
3
4
5
6
7
8
9
10
addhook("movetile","isinzone")
function isinzone(id)
	for z=1,#zones do
		if (checkInZone(id,z)) then
			msg("Lolwut it worked")
		else
			msg("Lolno it didn't work")
		end
	end
end
So obviously, it returns 2 "Lolno"s and 1 "Lolwut" whenever I'm walking in a zone. I realize that may be a dumb so
lution to what I want to make, but how do I fix it?

Thanks in advance,
Engy.

P.S. I may have made some mistakes in the code, fix me if you see any.

old Re: Is a player in a zone check

Yasday
User Off Offline

Quote
So, try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
zones = {n = 0,}

function zones.check(pl,id,x,y)
	if not x then x = player(pl,"tilex") end
	if not y then y = player(pl,"tiley") end
	if x >= zones[id].x[1] and x <= zones[id].x[2] and y >= zones[id].y[1] and y <= zones[id].y[2] then
		return true
	else
		return false
	end
end

function zones.create(x,y,name)
	if not x then x = {} end
	if not y then y = {} end
	if not name then name = "Zone" end
	zones.n = zones.n + 1
	local id = zones.n
	zones[id] = {
		x = x,
		y = y,
		name = name,
		showed = {},
	}
	for pl = 1,32,1 do
		zones[id].showed[pl] = false
	end
end

zones.create({1,5},{1,5},"Mega-Zone")

addhook([[movetile]],[[mv]])
function mv(pl,x,y)
	for id = 1,zones.n,1 do
		if zones[id] then
			if zones.check(pl,id,x,y) then
				if zones[id].showed[pl] == false then
					msg2(pl,string.char(0169).."255255255You are in Zone: "..zones[id].name)
					zones[id].showed[pl] = true
				end
			else
				zones[id].showed[pl] = false
			end
		end
	end
end
edited 4×, last 25.01.11 08:36:42 pm

old Re: Is a player in a zone check

EngiN33R
Moderator Off Offline

Quote
This doesn't work, tried stuff but still these errors:
LUA ERROR: sys/lua/space_rpg/zones.lua:30: 'for' limit must be a number
LUA ERROR: attempt to call a nil value

old Re: Is a player in a zone check

Yasday
User Off Offline

Quote
1
2
3
4
5
zones = {}
zones.n = 0
...
for id = 1,zones.n,1 do
...
It should've worked. zones.n was initialised(zones = {n = 0})
At least it should work like this..(even if it's the same thing, zones.n IS a number)

Edit:
Somehow, this works perfectly for me.
Spoiler >
edited 1×, last 25.01.11 08:37:56 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview