Tutorial: Line of sight

Here's the first part of my line of sight implementation.

I say tutorial, but it's basically me writing it as I go.

First, I want to be able to visualize my line of sight. Tilemap natively supports tile flashing, so I make use of that.

lineOfSight1.jpg


This is the code I use to draw my highlights.

It is updated every frame.

Spoiler
class Scene_Map def update_highlight(tiles) @spriteset.reserve_highlights(tiles) if @spriteset endendclass Spriteset_Map alias :th_event_trigger_range_create_tilemap :create_tilemap def create_tilemap th_event_trigger_range_create_tilemap @reserved_tiles = [] clear_highlights end alias :th_trigger_range_highlight_update_tilemap :update_tilemap def update_tilemap th_trigger_range_highlight_update_tilemap update_highlights end def update_highlights clear_highlights @reserved_tiles.each do |pos| @tilemap.flash_data[pos[0], pos[1]] = 0x8f2 end @reserved_tiles.clear end def reserve_highlights(tiles) @reserved_tiles.concat(tiles) end def clear_highlights @tilemap.flash_data = Table.new($game_map.width, $game_map.height) endend
And here's some code that shows the player's line of sight

Spoiler
class Game_Player @@sight = 12 alias :th_los_update :update def update th_los_update update_line_of_sight end def update_line_of_sight arr = [] @@sight.times {|i| arr << [@x + normalize_dir_x * i, @y + normalize_dir_y * i] } SceneManager.scene.update_highlight(arr) end def normalize_dir_x case @direction when 4 return -1 when 6 return 1 else return 0 end end def normalize_dir_y case @direction when 2 return 1 when 8 return -1 else return 0 end endend
It currently does not implement obstacle checking, but that's the next step after being able to visualize it.

It also assumes 4-dir line of sight.

Feel free to point out anything that can be improved.
You can also get the line itself using Bresenham's Algorithm. For example, if the sprite or actor is in a certain place, you can do:

Spoiler
def get_line(x0,y0,x1,y1) original_x0, original_y0 = x0, y0 points = [] steep = ((y1-y0).abs) > ((x1-x0).abs) if steep x0,y0 = y0,x0 x1,y1 = y1,x1 end if x0 > x1 x0,x1 = x1,x0 y0,y1 = y1,y0 end deltax = x1-x0 deltay = (y1-y0).abs error = (deltax / 2).to_i y = y0 ystep = nil if y0 < y1 ystep = 1 else ystep = -1 end for x in x0..x1 if steep points << {:x => y, :y => x} else points << {:x => x, :y => y} end error -= deltay if error < 0 y += ystep error += deltax end end if original_x0 != points[0][:x] or original_y0 != points[0][:y] points.reverse! end return points endend 
Note that this is just an example of how to do it via script.

It is also interesting that when you duplicate this functions or change these:

when ROUTE_END; process_route_end when ROUTE_MOVE_DOWN; move_straight(2) when ROUTE_MOVE_LEFT; move_straight(4) when ROUTE_MOVE_RIGHT; move_straight(6) when ROUTE_MOVE_UP; move_straight(8) when ROUTE_MOVE_LOWER_L; move_diagonal(4, 2) when ROUTE_MOVE_LOWER_R; move_diagonal(6, 2) when ROUTE_MOVE_UPPER_L; move_diagonal(4, 8) when ROUTE_MOVE_UPPER_R; move_diagonal(6, 8)You can make a nifty check of an event that if the event or your character is in the line of sight, the event would move towards you.

Spoiler
#-------------------------------------------------------------------------- # * Move Toward Position #-------------------------------------------------------------------------- def move_toward_position(x,y) sx = @x - x sy = @y - y if sx != 0 or sy != 0 if sx.abs > sy.abs sx > 0 ? move_left : move_right if @move_failed and sy != 0 sy > 0 ? move_up : move_down end else sy > 0 ? move_up : move_down if @move_failed and sx != 0 sx > 0 ? move_left : move_right end end end end 
Well I hope this helps. It is only an idea, it might work. 

Edit:

This was based on changing this:

Spoiler
Code:
  #--------------------------------------------------------------------------  # * Move Type : Approach  #--------------------------------------------------------------------------  def move_type_toward_player    if near_the_player?      case rand(6)      when 0..3;  move_toward_player      when 4;     move_random      when 5;     move_forward      end    else      move_random    end  end 
Bresenham's line algorithm is nice.

I'm thinking of something like

Code:
Extend a line segment in the direction of the player, with length equal to the player's rangeCheck if there are any obstacles in the wayIf there are obstacles  use the closest obstacle as the other pointelse  use the end point of the line segment as the other pointReturn an array of points (using that algo)
Have to figure out how to determine whether there are any obstacles in the way.
I thought of a way how to but its roughly an idea and may or not work. First is to check the passable tiles the event is in (or for example, let's say a guard is walking and the guard detects a passable or impassable tile - as an obstacle)

if not $game_map.passable?(x, y) @fovPassability[[x,y]] = false return true endone possible way is to create a field of view for these chasing events and check an obstacle. 

direction = event.direction case direction when 2 y -= 1 for i in (x-1)..(x+1) if @checkedTiles.index([i,y]) != nil if not @fovPassability[[i,y]] @fovPassability[[originalX,originalY]] = false return true end end endit's somehow getting to which direction and checking the field of view for each using the tiles. I haven't defined most of those, as to I am getting only to the point where it may work :\ 

when 2 by the way means the moving down of an event. if its left...something like:

when 4 x += 1 for i in (y-1)..(y+1) if @checkedTiles.index([x,i]) != nil if not @fovPassability[[x,i]] @fovPassability[[originalX,originalY]] = false return true end end endjust to check for the obstacles as well.

Original thread: https://forums.rpgmakerweb.com/threads/tutorial-line-of-sight.25402/