Rotate yaw clockwise (like real compasses)
[minetest_orienteering.git] / init.lua
bloba862fa35be552e3c96bc2b91cc14d5ef6c8fc1fa
1 local S = minetest.get_translator("orienteering")
2 local mod_map = minetest.get_modpath("map")
4 local orienteering = {}
5 orienteering.playerhuds = {}
6 orienteering.settings = {}
7 orienteering.settings.speed_unit = S("m/s")
8 orienteering.settings.length_unit = S("m")
9 orienteering.settings.hud_pos = { x = 0.5, y = 0 }
10 orienteering.settings.hud_offset = { x = 0, y = 15 }
11 orienteering.settings.hud_alignment = { x = 0, y = 0 }
13 local set = tonumber(minetest.settings:get("orienteering_hud_pos_x"))
14 if set then orienteering.settings.hud_pos.x = set end
15 set = tonumber(minetest.settings:get("orienteering_hud_pos_y"))
16 if set then orienteering.settings.hud_pos.y = set end
17 set = tonumber(minetest.settings:get("orienteering_hud_offset_x"))
18 if set then orienteering.settings.hud_offset.x = set end
19 set = tonumber(minetest.settings:get("orienteering_hud_offset_y"))
20 if set then orienteering.settings.hud_offset.y = set end
21 set = minetest.settings:get("orienteering_hud_alignment")
22 if set == "left" then
23 orienteering.settings.hud_alignment.x = 1
24 elseif set == "center" then
25 orienteering.settings.hud_alignment.x = 0
26 elseif set == "right" then
27 orienteering.settings.hud_alignment.x = -1
28 end
30 local o_lines = 4 -- Number of lines in HUD
32 -- Helper function to switch between 12h and 24 mode for the time
33 function orienteering.toggle_time_mode(itemstack, user, pointed_thing)
34 --[[ Player attribute “orienteering:twelve”:
35 * "true": Use 12h mode for time
36 * "false" or unset: Use 24h mode for time ]]
37 if user:get_attribute("orienteering:twelve") == "true" then
38 user:set_attribute("orienteering:twelve", "false")
39 else
40 user:set_attribute("orienteering:twelve", "true")
41 end
42 orienteering.update_hud_displays(user)
43 end
45 local use = S("Put this tool in your hotbar to see the data it provides.")
46 local use_time = S("Put this tool in your hotbar to make use of its functionality. Punch to toggle between 24-hour and 12-hour display for the time feature.")
48 -- Displays height (Y)
49 minetest.register_tool("orienteering:altimeter", {
50 description = S("Altimeter"),
51 _doc_items_longdesc = S("It shows you your current elevation (Y)."),
52 _doc_items_usagehelp = use,
53 wield_image = "orienteering_altimeter.png",
54 inventory_image = "orienteering_altimeter.png",
55 groups = { disable_repair = 1 },
58 -- Displays X and Z coordinates
59 minetest.register_tool("orienteering:triangulator", {
60 description = S("Triangulator"),
61 _doc_items_longdesc = S("It shows you the coordinates of your current position in the horizontal plane (X and Z)."),
62 _doc_items_usagehelp = use,
63 wield_image = "orienteering_triangulator.png",
64 inventory_image = "orienteering_triangulator.png",
65 groups = { disable_repair = 1 },
68 -- Displays player yaw
69 -- TODO: calculate yaw difference between 2 points
70 minetest.register_tool("orienteering:compass", {
71 description = S("Compass"),
72 _doc_items_longdesc = S("It shows you your yaw (horizontal viewing angle) in degrees."),
73 _doc_items_usagehelp = use,
74 wield_image = "orienteering_compass_wield.png",
75 inventory_image = "orienteering_compass_inv.png",
76 groups = { disable_repair = 1 },
79 -- Displays player pitch
80 -- TODO: calculate pitch difference between 2 points
81 minetest.register_tool("orienteering:sextant", {
82 description = S("Sextant"),
83 _doc_items_longdesc = S("It shows you your pitch (vertical viewing angle) in degrees."),
84 _doc_items_usagehelp = use,
85 wield_image = "orienteering_sextant_wield.png",
86 inventory_image = "orienteering_sextant_inv.png",
87 groups = { disable_repair = 1 },
90 -- Ultimate orienteering tool: Displays X,Y,Z, yaw, pitch, time, speed and enables the minimap
91 minetest.register_tool("orienteering:quadcorder", {
92 description = S("Quadcorder"),
93 _doc_items_longdesc = S("This is the ultimate orientieering tool. It shows you your coordinates (X, Y and Z), shows your yaw and pitch (horizontal and vertical viewing angles), the current time, your current speed and it enables you to access the minimap."),
94 wield_image = "orienteering_quadcorder.png",
95 _doc_items_usagehelp = use_time,
96 wield_scale = { x=1, y=1, z=3.5 },
97 inventory_image = "orienteering_quadcorder.png",
98 groups = { disable_repair = 1 },
99 on_use = orienteering.toggle_time_mode,
102 -- Displays game time
103 minetest.register_tool("orienteering:watch", {
104 description = S("Watch"),
105 _doc_items_longdesc = S("It shows you the current time."),
106 _doc_items_usagehelp = S("Put the watch in your hotbar to see the time. Punch to toggle between the 24-hour and 12-hour display."),
107 wield_image = "orienteering_watch.png",
108 inventory_image = "orienteering_watch.png",
109 groups = { disable_repair = 1 },
110 on_use = orienteering.toggle_time_mode,
113 -- Displays speed
114 minetest.register_tool("orienteering:speedometer", {
115 description = S("Speedometer"),
116 _doc_items_longdesc = S("It shows you your current horizontal (“hor.”) and vertical (“ver.”) speed in meters per second, where one meter is the side length of a single cube."),
117 _doc_items_usagehelp = use,
118 wield_image = "orienteering_speedometer_wield.png",
119 inventory_image = "orienteering_speedometer_inv.png",
120 groups = { disable_repair = 1 },
123 if not mod_map then
124 -- Enables minimap (surface)
125 minetest.register_tool("orienteering:map", {
126 description = S("Map"),
127 _doc_items_longdesc = S("The map allows you to view a minimap of the area around you."),
128 _doc_items_usagehelp = S("If you put a map in your hotbar, you will be able to access the minimap (only surface mode). By default, the minimap can be opened with [F7]."),
129 wield_image = "orienteering_map.png",
130 wield_scale = { x=1.5, y=1.5, z=0.15 },
131 inventory_image = "orienteering_map.png",
132 groups = { disable_repair = 1 },
136 -- Enables minimap (radar)
137 minetest.register_tool("orienteering:automapper", {
138 description = S("Radar Mapper"),
139 _doc_items_longdesc = S("The radar mapper is a device that combines a map with a radar. It unlocks both the surface mode and radar mode of the minimap."),
140 _doc_items_usagehelp = S("If you put a radar mapper in your hotbar, you will be able to access the minimap. By default, the minimap can be opened with [F7]."),
141 wield_image = "orienteering_automapper_wield.png",
142 wield_scale = { x=1, y=1, z=2 },
143 inventory_image = "orienteering_automapper_inv.png",
144 groups = { disable_repair = 1 },
147 -- Displays X,Y,Z coordinates, yaw and game time
148 minetest.register_tool("orienteering:gps", {
149 description = S("GPS device"),
150 _doc_items_longdesc = S("The GPS device shows you your coordinates (X, Y and Z), your yaw (horizontal viewing angle) and the time."),
151 _doc_items_usagehelp = use_time,
152 wield_image = "orienteering_gps_wield.png",
153 wield_scale = { x=1, y=1, z=2 },
154 inventory_image = "orienteering_gps_inv.png",
155 groups = { disable_repair = 1 },
156 on_use = orienteering.toggle_time_mode,
159 if minetest.get_modpath("default") ~= nil then
160 -- Register crafts
161 minetest.register_craft({
162 output = "orienteering:altimeter",
163 recipe = {
164 {"default:glass"},
165 {"default:steel_ingot"},
166 {"default:steel_ingot"},
169 minetest.register_craft({
170 output = "orienteering:triangulator",
171 recipe = {
172 {"", "default:bronze_ingot", ""},
173 {"default:bronze_ingot", "", "default:bronze_ingot"},
176 minetest.register_craft({
177 output = "orienteering:sextant",
178 recipe = {
179 {"", "default:gold_ingot", ""},
180 {"default:gold_ingot", "default:gold_ingot", "default:gold_ingot"},
183 minetest.register_craft({
184 output = "orienteering:compass",
185 recipe = {
186 {"", "default:tin_ingot", ""},
187 {"default:tin_ingot", "group:stick", "default:tin_ingot"},
188 {"", "default:tin_ingot", ""},
191 minetest.register_craft({
192 output = "orienteering:speedometer",
193 recipe = {
194 {"", "default:gold_ingot", ""},
195 {"default:steel_ingot", "group:stick", "default:steel_ingot"},
196 {"", "default:steel_ingot", ""},
199 minetest.register_craft({
200 output = "orienteering:automapper",
201 recipe = {
202 {"default:gold_ingot", "default:gold_ingot", "default:gold_ingot"},
203 {"default:mese_crystal", "default:obsidian_shard", "default:mese_crystal"},
204 {"default:gold_ingot", "default:gold_ingot", "default:gold_ingot"}
207 minetest.register_craft({
208 output = "orienteering:gps",
209 recipe = {
210 { "default:gold_ingot", "orienteering:triangulator", "default:gold_ingot" },
211 { "orienteering:compass", "default:bronze_ingot", "orienteering:watch" },
212 { "default:tin_ingot", "orienteering:altimeter", "default:tin_ingot" }
215 minetest.register_craft({
216 output = "orienteering:quadcorder",
217 recipe = {
218 { "default:gold_ingot", "default:gold_ingot", "default:gold_ingot" },
219 { "orienteering:speedometer", "default:diamond", "orienteering:automapper", },
220 { "orienteering:sextant", "default:diamond", "orienteering:gps" }
223 minetest.register_craft({
224 output = "orienteering:watch",
225 recipe = {
226 { "default:copper_ingot" },
227 { "default:glass" },
228 { "default:copper_ingot" }
232 if (not mod_map) and minetest.get_modpath("dye") then
233 minetest.register_craft({
234 output = "orienteering:map",
235 recipe = {
236 { "default:paper", "default:paper", "default:paper" },
237 { "default:paper", "dye:black", "default:paper" },
238 { "default:paper", "default:paper", "default:paper" },
245 function orienteering.update_automapper(player)
246 if mod_map then
247 if orienteering.tool_active(player, "orienteering:automapper") or orienteering.tool_active(player, "orienteering:quadcorder") or minetest.settings:get_bool("creative_mode") then
248 player:hud_set_flags({minimap_radar = true})
249 else
250 player:hud_set_flags({minimap_radar = false})
252 else
253 if orienteering.tool_active(player, "orienteering:automapper") or orienteering.tool_active(player, "orienteering:quadcorder") or minetest.settings:get_bool("creative_mode") then
254 player:hud_set_flags({minimap = true, minimap_radar = true})
255 elseif orienteering.tool_active(player, "orienteering:map") then
256 player:hud_set_flags({minimap = true, minimap_radar = false})
257 else
258 player:hud_set_flags({minimap = false, minimap_radar = false})
263 -- Checks whether a certain orienteering tool is “active” and ready for use
264 function orienteering.tool_active(player, item)
265 -- Requirement: player carries the tool in the hotbar
266 local inv = player:get_inventory()
267 local hotbar = player:hud_get_hotbar_itemcount()
268 for i=1, hotbar do
269 if inv:get_stack("main", i):get_name() == item then
270 return true
273 return false
276 function orienteering.init_hud(player)
277 orienteering.update_automapper(player)
278 local name = player:get_player_name()
279 orienteering.playerhuds[name] = {}
280 for i=1, o_lines do
281 orienteering.playerhuds[name]["o_line"..i] = player:hud_add({
282 hud_elem_type = "text",
283 text = "",
284 position = orienteering.settings.hud_pos,
285 offset = { x = orienteering.settings.hud_offset.x, y = orienteering.settings.hud_offset.y + 20*(i-1) },
286 alignment = orienteering.settings.hud_alignment,
287 number = 0xFFFFFF,
288 scale= { x = 100, y = 20 },
293 function orienteering.update_hud_displays(player)
294 local toDegrees=180/math.pi
295 local name = player:get_player_name()
296 local gps, altimeter, triangulator, compass, sextant, watch, speedometer, quadcorder
298 if orienteering.tool_active(player, "orienteering:gps") then
299 gps = true
301 if orienteering.tool_active(player, "orienteering:altimeter") then
302 altimeter = true
304 if orienteering.tool_active(player, "orienteering:triangulator") then
305 triangulator = true
307 if orienteering.tool_active(player, "orienteering:compass") then
308 compass = true
310 if orienteering.tool_active(player, "orienteering:sextant") then
311 sextant = true
313 if orienteering.tool_active(player, "orienteering:watch") then
314 watch = true
316 if orienteering.tool_active(player, "orienteering:speedometer") then
317 speedometer = true
319 if orienteering.tool_active(player, "orienteering:quadcorder") then
320 quadcorder = true
323 local str_pos, str_angles, str_time, str_speed
324 local pos = vector.round(player:get_pos())
325 if (altimeter and triangulator) or gps or quadcorder then
326 str_pos = S("Coordinates: X=@1, Y=@2, Z=@3", pos.x, pos.y, pos.z)
327 elseif altimeter then
328 str_pos = S("Height: Y=@1", pos.y)
329 elseif triangulator then
330 str_pos = S("Coordinates: X=@1, Z=@2", pos.x, pos.z)
331 else
332 str_pos = ""
335 -- Yaw in Minetest goes counter-clockwise, which is opposite of how compasses work
336 local yaw = 360-player:get_look_horizontal()*toDegrees
337 local pitch = player:get_look_vertical()*toDegrees
338 if ((compass or gps) and sextant) or quadcorder then
339 str_angles = S("Yaw: @1°, pitch: @2°", string.format("%.1f", yaw), string.format("%.1f", pitch))
340 elseif compass or gps then
341 str_angles = S("Yaw: @1°", string.format("%.1f", yaw))
342 elseif sextant then
343 str_angles = S("Pitch: @1°", string.format("%.1f", pitch))
344 else
345 str_angles = ""
348 local time = minetest.get_timeofday()
349 if watch or gps or quadcorder then
350 local totalminutes = time * 1440
351 local minutes = totalminutes % 60
352 local hours = math.floor((totalminutes - minutes) / 60)
353 minutes = math.floor(minutes)
354 local twelve = player:get_attribute("orienteering:twelve") == "true"
355 if twelve then
356 if hours == 12 and minutes == 0 then
357 str_time = S("Time: noon")
358 elseif hours == 0 and minutes == 0 then
359 str_time = S("Time: midnight")
360 else
361 local hours12 = math.fmod(hours, 12)
362 if hours12 == 0 then hours12 = 12 end
363 if hours >= 12 then
364 str_time = S("Time: @1:@2 p.m.", string.format("%i", hours12), string.format("%02i", minutes))
365 else
366 str_time = S("Time: @1:@2 a.m.", string.format("%i", hours12), string.format("%02i", minutes))
369 else
370 str_time = S("Time: @1:@2", string.format("%02i", hours), string.format("%02i", minutes))
372 else
373 str_time = ""
376 if speedometer or quadcorder then
377 local speed_hor, speed_ver
378 local v
379 local attach = player:get_attach()
380 if attach == nil then
381 v = player:get_player_velocity()
382 else
383 v = attach:get_velocity()
384 if not v then
385 v = player:get_player_velocity()
388 speed_ver = v.y
389 v.y = 0
390 speed_hor = vector.length(v)
392 local u = orienteering.settings.speed_unit
393 str_speed = S("Speed: hor.: @1 @2, vert.: @3 @4", string.format("%.1f", speed_hor), u, string.format("%.1f", speed_ver), u)
394 else
395 str_speed = ""
398 local strs = { str_pos, str_angles, str_time, str_speed }
399 local line = 1
400 for i=1, o_lines do
401 if strs[i] ~= "" then
402 player:hud_change(orienteering.playerhuds[name]["o_line"..line], "text", strs[i])
403 line = line + 1
406 for l=line, o_lines do
407 player:hud_change(orienteering.playerhuds[name]["o_line"..l], "text", "")
411 minetest.register_on_newplayer(orienteering.init_hud)
412 minetest.register_on_joinplayer(orienteering.init_hud)
414 minetest.register_on_leaveplayer(function(player)
415 orienteering.playerhuds[player:get_player_name()] = nil
416 end)
418 local updatetimer = 0
419 minetest.register_globalstep(function(dtime)
420 updatetimer = updatetimer + dtime
421 if updatetimer > 0.1 then
422 local players = minetest.get_connected_players()
423 for i=1, #players do
424 orienteering.update_automapper(players[i])
425 orienteering.update_hud_displays(players[i])
427 updatetimer = updatetimer - dtime
429 end)
431 if minetest.get_modpath("awards") ~= nil and minetest.get_modpath("default") ~= nil then
432 awards.register_achievement("orienteering_quadcorder", {
433 title = S("Master of Orienteering"),
434 description = S("Craft a quadcorder."),
435 icon = "orienteering_quadcorder.png",
436 trigger = {
437 type = "craft",
438 item = "orienteering:quadcorder",
439 target = 1