English Check if tile is water

9 replies
Goto Page
To the start Previous 1 Next To the start
04.06.19 06:39:30 pm
Up
Goo
User
Offline Off
How does one check if a tile is wet (water)?
I know of
tile(tx, ty, 'property') == 14
but that doesn't work properly when using dynamic floors.
This only checks for the tile below the dynamic floor.

So how do I check if it's water by either a tile or a dynamic floor?
04.06.19 07:29:32 pm
Up
Yunu
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
local tiles = {
     [14] = true
}

function isWater(x, y)
     if entity(x, y, "exists") and entity(x, y, "type") == 71 then
          if tiles[entity(x, y, "int0")] then
               return true
          end
     end
     return false
end


Specify water tile IDs in tiles table it works only for entities you still need use
tile(tx, ty, 'property')
for normal tiles with water property.
04.06.19 07:52:15 pm
Up
Goo
User
Offline Off
It doesn't exactly work as I expected, I'm going to try something and edit this...
04.06.19 08:14:21 pm
Up
DC
Admin
Offline Off
What user Yunu wrote looked quite right. Are you sure that it isn't working?
Make sure not to just put 14 into the array. You have to add the tile FRAMES (!) of water tiles there. So the 14 is very misleading in that sample code.

It's a bit stupid that you manually have to define water tiles. I will add the Lua command cs2d lua cmd tileproperty which allows you to get the property value of a tile frame (instead of a tile position).
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
04.06.19 08:27:57 pm
Up
Goo
User
Offline Off
IMG:https://i.imgur.com/nTSi1Dg.png



Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- List of tiles that are... Wet...?
hc.water_tiles = {
     [42] = true,
     [52] = true,
     [53] = true
}

function hc.util.is_water(tx, ty)
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
                    
               return true
          end
     end
      
     return false
end


This returns 'false' on both water and the road, I'm trying to make a bridge.

The bridge is made out of dynamic walls.
04.06.19 08:35:19 pm
Up
DC
Admin
Offline Off
Like user Yunu said you STILL also have to do your
tile(tx, ty, 'property') == 14
-check! In fact you should do your check first and only if that returns false you should try the entity/dynamic object checks.

If the dynamic object part isn't working I would try to print the values to console/chat/wherever to see if anything returns weird/unexpected things.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
04.06.19 08:57:44 pm
Up
Goo
User
Offline Off
Using:

Code:
1
2
3
4
5
6
7
8
9
10
function hc.util.is_water(tx, ty)
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (tile(tx, ty, 'property') == 14) or (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
                    
               return true
          end
     end
      
     return false
end


or

Code:
1
2
3
4
5
6
7
8
9
10
function hc.util.is_water(tx, ty)
     if (tile(tx, ty, 'property') == 14) or entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
                    
               return true
          end
     end
      
     return false
end


Still false everywhere.
04.06.19 09:08:46 pm
Up
DC
Admin
Offline Off
The nesting is bad and hard to understand and read. You always do entity checks. Even for tiles which are simply water tiles without an entity.

Simple code with more IFs and less and/or is always better (easier to understand, less error prone)

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function hc.util.is_water(tx, ty)
     -- water! no need to check entities!
     if (tile(tx, ty, 'property') == 14) then
          return true
     end

     -- dynamic object check
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          if (not entity(tx, ty, 'state') and hc.water_tiles[entity(tx, ty, 'int0')]) then
               return true
          end
     end
      
     return false
end


to debug however you should try something like

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function hc.util.is_water(tx, ty)
     -- water! no need to check entities!
     if (tile(tx, ty, 'property') == 14) then
          return true
     end

     -- dynamic object check
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 then
          msg("tile is dynamic object")
          if (not entity(tx, ty, 'state') then
               msg("dyn obj state is false")
          end
          if hc.water_tiles[entity(tx, ty, 'int0')]) then
               msg("dyn obj tile is water")
               return true
          end
     end
      
     return false
end


by checking the messages you get you can see where it fails.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
05.06.19 01:24:47 am
Up
Goo
User
Offline Off
More >


I've made it work:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function hc.util.is_water(tx, ty)
     -- Dynamic Tile
     if entity(tx, ty, 'exists') and entity(tx, ty, 'type') == 71 and (not entity(tx, ty, 'state')) then
          if hc.water_tiles[entity(tx, ty, 'int0')] then
               return true
          else
               return false
          end
     end
     
     -- Actual Tile
     if tile(tx, ty, 'property') == 14 then
          return true
     end
     
     return false
end


Thanks.
edited 1×, last 05.06.19 01:47:34 am
05.06.19 07:05:43 pm
Up
DC
Admin
Offline Off
Ah, sorry, my bad! Didn't consider that the tile is still a water tile if it's covered by the entity. Of course my suggestion to check that first and return true in that case was nonsense then!

Glad to see that you managed to solve it! √
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 Next To the start