Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. function line_of_sight(self, pos1, pos2, stepsize)
  2.  
  3.     stepsize = stepsize or 1
  4.  
  5.     local s, pos = minetest.line_of_sight(pos1, pos2, stepsize)
  6.  
  7.     -- normal walking and flying mobs can see you through air
  8.     if s == true then
  9.         return true
  10.     end
  11.  
  12.     -- New pos1 to be analyzed
  13.     local npos1 = {x = pos1.x, y = pos1.y, z = pos1.z}
  14.  
  15.     local r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  16.  
  17.     -- Checks the return
  18.     if r == true then return true end
  19.  
  20.     -- Nodename found
  21.     local nn = minetest.get_node(pos).name
  22.  
  23.     -- Target Distance (td) to travel
  24.     local td = get_distance(pos1, pos2)
  25.  
  26.     -- Actual Distance (ad) traveled
  27.     local ad = 0
  28.  
  29.     -- It continues to advance in the line of sight in search of a real obstruction.
  30.     while minetest.registered_nodes[nn] and (minetest.registered_nodes[nn].walkable == false or nn == "default:snow") do
  31.  
  32.         -- Check if you can still move forward
  33.         if td < ad + stepsize then
  34.             return true -- Reached the target
  35.         end
  36.  
  37.         -- Moves the analyzed pos
  38.         local d = get_distance(pos1, pos2)
  39.         npos1.x = ((pos2.x - pos1.x) / d * stepsize) + pos1.x
  40.         npos1.y = ((pos2.y - pos1.y) / d * stepsize) + pos1.y
  41.         npos1.z = ((pos2.z - pos1.z) / d * stepsize) + pos1.z
  42.  
  43.         ad = ad + stepsize
  44.  
  45.         -- scan again
  46.         r, pos = minetest.line_of_sight(npos1, pos2, stepsize)
  47.  
  48.         if r == true then return true end
  49.  
  50.         -- New Nodename found
  51.         nn = minetest.get_node(pos).name
  52.  
  53.     end
  54.  
  55.     return false
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement