Enhanced minetest.line_of_sight()

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Enhanced minetest.line_of_sight()

by stu » Wed Nov 27, 2013 20:34

Looking recently at kaeza's kutils.find_pointed_thing() code just
gave me the idea that it could be very helpful for some mods if minetest.line_of_sight()
would return the actual blocking node position instead of just false.

From what I can make of ServerEnvironment::line_of_sight() it should not cost any additional
overhead to do so.

I would submit a pull request but I am not familiar with how this routine integrates with
the lua api. I also know that this has just recently been fixed 'again' so I wouldn't want
to mess things up.
Last edited by stu on Wed Nov 27, 2013 20:44, edited 1 time in total.
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

by rubenwardy » Thu Nov 28, 2013 08:50

It would have to be a separate api function, as returning an object would equate to true, which will break old mods.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Thu Nov 28, 2013 16:54

rubenwardy wrote:It would have to be a separate api function, as returning an object would equate to true, which will break old mods.

We could also return false, blocking_node
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

by stu » Thu Nov 28, 2013 18:23

rubenwardy wrote:It would have to be a separate api function, as returning an object would equate to true, which will break old mods.

How long has this method been available? I was thinking it was a fairly recent addition but could well be mistaken.
If it has been around a while then I do see your point as it was (until very recently) returning nil.
PilzAdam wrote:We could also return false, blocking_node

But wouldn't returning a table still be interpreted as not-nil? It's a real pity as i can think of a number of potential uses for
such a silmple modification.
Last edited by stu on Thu Nov 28, 2013 18:40, edited 1 time in total.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Thu Nov 28, 2013 18:44

stu wrote:
PilzAdam wrote:We could also return false, blocking_node

But wouldn't returning a table still be interpreted as not-nil? It's a real pity as i can think of a number of potential uses for
such a silmple modification.

Not a table, in Lua functions can have more than one return value.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

by stu » Thu Nov 28, 2013 19:01

PilzAdam wrote:Not a table, in Lua functions can have more than one return value.

Oh, I was not even aware you could do that, maybe it's time I properly read the lua docs.

So does that mean that the following code would still work assuming a false condition?
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
if not minetest.line_of_sight(p1, p2) then
    --code here
end

btw, I think returning the actual position of the node would be more useful than the node
itself as it would be possible to determine the node from the position but not vice-versa.

Edit: This seems to explain things quite well http://www.lua.org/pil/5.1.html
I guess it should be ok in that case.
Last edited by stu on Thu Nov 28, 2013 19:20, edited 1 time in total.
 

User avatar
rubenwardy
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

by rubenwardy » Thu Nov 28, 2013 19:35

I used it in capture the flag 3 months ago.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

by stu » Thu Nov 28, 2013 20:04

rubenwardy wrote:I used it in capture the flag 3 months ago.

I just checked, minetest.line_of_sight() was introduced in the 0.4.7 release so there are probably
not that many 'old' mods that use it.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

by stu » Mon Dec 09, 2013 21:27

Ok, this appears to work https://gist.github.com/stujones11/7881082

I am sure there are other ways it could have been done but this seemed like the least intrusive to me.
I would be interested to hear any comments on the style before I waste my time submitting a pull request.
Last edited by stu on Mon Dec 09, 2013 21:28, edited 1 time in total.
 

celeron55
Member
 
Posts: 430
Joined: Tue Apr 19, 2011 10:10

by celeron55 » Tue Dec 10, 2013 20:04

Make it a pull request; it seems fine and that way it gets more attention.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

by stu » Wed Dec 11, 2013 00:35

celeron55, If you are ok with the code then I will certainly do that

cheers!
 


Return to Minetest Features

Who is online

Users browsing this forum: No registered users and 11 guests

cron