changed rounding logic to round instead of trunc

master
Kilarin 2014-05-29 21:28:55 -05:00
parent baaf79a964
commit ea8e16007c
2 changed files with 20 additions and 10 deletions

6
README
View File

@ -1,4 +1,4 @@
compassgps
compassgps 1.2
Echo created a compass mod back in 2012: https://forum.minetest.net/viewtopic.php?id=3785
PilzAdams made a modification of it, which I can not find the source to, I don't know how much of PilzAdams changes made it into the later versions of Echo's mod.
@ -40,11 +40,11 @@ Down at the lower right of the screen, I'm certain you noticed the "Teleport to
The bookmark list is saved any time a user changes it. All of your other settings, the currently selected bookmark, sort order, distance function, and hud position, are saved whenever a user leaves the game, and on game shutdown. so if you move the hud down to the lower right hand corner of the screen, and then quit, the hud will still be in the place you put it when you restart the game later.
I also fixed a few bugs while I was working on this. There was a problem in the mod that caused compass to jump around in inventory if there were empty slots above it, that is fixed now. And there was also a problem with the bookmark list not being saved after you removed a bookmark if you didn't add a new bookmark afterwards. Now the bookmark list is modified whenever you change it, either adding or removing.
I also fixed a few bugs while I was working on this. There was a problem in the mod that caused compass to jump around in inventory if there were empty slots above it, that is fixed now. And there was also a problem with the bookmark list not being saved after you removed a bookmark if you didn't add a new bookmark afterwards. Now the bookmark list is saved whenever you change it, either adding or removing.
I tried to follow Echo and TeTpaAka's examples of how to properly code for multiplayer games, and I think all of the new settings should work just fine in a multiplayer game. If anyone tests this on a server, please let me know how it works.
The code is kinda a mess, because I was learning a lot of new things while working on it. I hope to do a clean up on it sometime in the near future, but I wanted to release it now so some people could start testing it. Please do not hesitate to offer critiques, criticism, or coding advice. I'm new to lua and minetest and could use the help.
The code is kinda a mess, because I was learning a lot of new things while working on it. I hope to do a clean up on it sometime in the near future, but I wanted to release it now so some people could start testing it. Please do not hesitate to offer critiques, criticism, or coding advice. I'm new to lua and minetest and could use the help.
[b]Credits:[/b]
Original mod is by Echo and TeTpaAka, and probably PilzAdam. Cactuz_pl clockmod showed me how to write the hud to the screen. My son offered a lot of advice and suggested several changes. I got an example of how to sort lists in lua from Michal Kottman on StackOverflow

View File

@ -1,3 +1,5 @@
--compassgps 1.2
--fixed bug that caused compass to jump around in inventory
--fixed bug causing removed bookmarks not to be saved
--expanded bookmark list from dropdown to textlist
@ -287,11 +289,7 @@ function compassgps.set_bookmark(name, param)
end
local pos = player:getpos()
--we are marking a NODE, no need to keep all those fractions
pos=compassgps.trunc_pos(pos)
--pos.x=compassgps.trunc(pos.x)
--pos.y=compassgps.trunc(pos.y+0.5)
----for y, height 20.5 should return 21, height -20.5 should return 20
--pos.z=compassgps.trunc(pos.z)
pos=compassgps.round_pos(pos)
--remove dangerous characters that will mess up the bookmark
--the file can handle these fine, but the LIST for the textlist
@ -330,6 +328,7 @@ minetest.register_chatcommand("set_bookmark", {
})
--[
--truncates a number
function compassgps.trunc(num)
if num >= 0 then return math.floor(num)
@ -346,6 +345,17 @@ function compassgps.trunc_pos(pos)
pos.z=compassgps.trunc(pos.z)
return pos
end --trunc_pos
--]
--returns a pos that is rounded special case. round 0 digits for X and Z,
--round 1 digit for Y
function compassgps.round_pos(pos)
pos.x=compassgps.round_digits(pos.x,0)
pos.y=compassgps.round_digits(pos.y,1)
pos.z=compassgps.round_digits(pos.z,0)
return pos
end --round_pos
function compassgps.round_digits(num,digits)
@ -361,12 +371,12 @@ end --round_digits_vector
--because built in pos_to_string doesn't handle nil, and commas mess up textlist
--this truncates same rules as for setting bookmark or teleporting
--this rounds same rules as for setting bookmark or teleporting
--that way what you see in the hud matches where you teleport or bookmark
function compassgps.pos_to_string(pos)
if pos==nil then return "(nil)"
else
pos=compassgps.trunc_pos(pos)
pos=compassgps.round_pos(pos)
return "("..pos.x.." "..pos.y.." "..pos.z..")"
end --pos==nill
end --pos_to_string