# Forum  CS2D Scripts Check ID of tiles around player

# Check ID of tiles around player

2 replies
Goto Page  1  Goo
User
Offline Hello! I'm here for a small function that I need, it will check for the 8 tiles around a player (and the tile below the player making it 9 tiles) and check if their ID is equal to a specific ID.

I (think that I) can make it but I need it to be efficient because my script is huge.

This is a small example of the tiles it's supposed to check and the tile that the player is standing on. This is an example of the function:
Code:
1
2
3
4
5
6
7
function NearTile(playerid, tileid)
if (neartile) then
return true
else
return false
end
end

Vehk
User
Offline Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
function checktiles(x, y, tileid)
local results = {}

x = x - 1
y = y - 1
for j = 0, 2 do
for i = 0, 2 do
local match = tile(x + i, y + j, "frame") == tileid
results[j * 3 + i + 1] = match
end
end
return results
end

This function will compare the tiles around the specified position and return the results as a table.

There are 9 tiles checked, a 3 by 3 area. The table indexes are mapped like this:

1 2 3
4 5 6
7 8 9

Where 5 is the index for the result at position X, Y.

If you want it to be more efficient you could unwrap the two loops
Goo
User
Offline Thanks but someone already PM'd me a working function.

Code:
function NearTile(playerid, tileid)
for x = player(playerid,"tilex") - 1, player(playerid,"tiley") + 1 do
for y = player(playerid,"tiley") - 1, player(playerid,"tiley") + 1 do
if ( x > -1 and y > -1 and tile(x, y, "frame") == tonumber(tileid) ) and ( x < map("sizex") and y < map("sizey") ) then
return true
end
end
end
return false
end  1  ﻿