Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] How to determine if map tile is blocked?

kds71

Member

Hello,

I'm newbie to RGSS (and Ruby), but I consider myself as good programmer.
I've read all the FAQ and searched forum, but I couldn't find answer to my question.

I'm creating my own game system (ABS / CMS / HUD) from scratch. Everything is going fine (as I've found many solutions on this great forum). But now I encountered problem, which is unsolvable for me.

I need to determine if tile on map blocks line of sight (LoS):
- tile with other character blocks LoS
- tile with something on middle layer (like tree) blocks LoS
- tile with something blocking movement doesn't need to block LoS (for example: water)
- tile with event doesn't need to block LoS (events without graphics, events which doesn't block movement)

All I need is to write method which takes two parameters (coordinates of tile) and returns true (when tile blocks LoS) or false (when it doesn't).

Any help will be appreciated :)

Edit:
I solved it (partially):
Code:
  def tile_is_blocked(x, y)
    tile_id = $game_map.data[x, y, 1]
    if  tile_id > 0
      pass = $tileset.passages[tile_id]
      return true if ((pass & 1) == 1) and @dir = 0
      return true if ((pass & 2) == 2) and @dir = 1
      return true if ((pass & 4) == 4) and @dir = 2
      return true if ((pass & 8) == 8) and @dir = 3
    end
    return false
  end
Now I have problem with events. I think I need to view game map events array and check every event coordinates. But it shall return true only if through parameter is false. But I have no idea how to check value of this parameter...

(sorry if my English is poor, it isn't my native language)
 
kds71;157324 said:
Edit:
I solved it (partially):
Code:
  def tile_is_blocked(x, y)
    tile_id = $game_map.data[x, y, 1]
    if  tile_id > 0
      pass = $tileset.passages[tile_id]
      return true if ((pass & 1) == 1) and @dir = 0
      return true if ((pass & 2) == 2) and @dir = 1
      return true if ((pass & 4) == 4) and @dir = 2
      return true if ((pass & 8) == 8) and @dir = 3
    end
    return false
  end

I think this method is what you are looking for.
Code:
$Game_Map.passable?(x, y, d, self_event = nil)
Code:
  #--------------------------------------------------------------------------
  # * Determine if Passable
  #     x          : x-coordinate
  #     y          : y-coordinate
  #     d          : direction (0,2,4,6,8,10)
  #                  *  0,10 = determine if all directions are impassable
  #     self_event : Self (If event is determined passable)
  #--------------------------------------------------------------------------
  def passable?(x, y, d, self_event = nil)
    # If coordinates given are outside of the map
    unless valid?(x, y)
      # impassable
      return false
    end
    # Change direction (0,2,4,6,8,10) to obstacle bit (0,1,2,4,8,0)
    bit = (1 << (d / 2 - 1)) & 0x0f
    # Loop in all events
    for event in events.values
      # If tiles other than self are consistent with coordinates
      if event.tile_id >= 0 and event != self_event and
         event.x == x and event.y == y and not event.through
        # If obstacle bit is set
        if @passages[event.tile_id] & bit != 0
          # impassable
          return false
        # If obstacle bit is set in all directions
        elsif @passages[event.tile_id] & 0x0f == 0x0f
          # impassable
          return false
        # If priorities other than that are 0
        elsif @priorities[event.tile_id] == 0
          # passable
          return true
        end
      end
    end
    # Loop searches in order from top of layer
    for i in [2, 1, 0]
      # Get tile ID
      tile_id = data[x, y, i]
      # Tile ID acquistion failure
      if tile_id == nil
        # impassable
        return false
      # If obstacle bit is set
      elsif @passages[tile_id] & bit != 0
        # impassable
        return false
      # If obstacle bit is set in all directions
      elsif @passages[tile_id] & 0x0f == 0x0f
        # impassable
        return false
      # If priorities other than that are 0
      elsif @priorities[tile_id] == 0
        # passable
        return true
      end
    end
    # passable
    return true
  end
Then again I could be wrong...
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top