commit ce64a96b6010b69d6bf416a82ff7a291b4b0b409 Author: rainer Date: Thu Feb 12 13:34:27 2026 +0100 init diff --git a/README.md b/README.md new file mode 100644 index 0000000..474c3bd --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +HUD Compass [hud_compass] +------------------------- + +A Minetest mod to optionally place a HUD compass and a 24-hour clock on the screen. + +By David G (kestral246) + +![HUD Compass Screenshot](screenshot.png "hud_compass") + +How to enable +------------- + +This mod defaults to not displaying a compass or a clock. To enable, use the chat command: + + "/compass" -> By default this places just a compass in the bottom right corner of the screen. + +Repeated use of this command will toggle the display of the compass off and on. + +When given with an argument, the user can select whether to display just a compass, or a compass and a clock, and which corner of the screen to place them in. This is particularly useful with Android clients, where the bottom right corner of the screen has the jump button. + + "/compass 1" -> compass only, top right corner + "/compass 2" -> compass only, bottom right corner + "/compass 3" -> compass only, bottom left corner + "/compass 4" -> compass only, top left corner + + "/compass 5" -> compass and clock, top right corner + "/compass 6" -> compass and clock, bottom right corner + "/compass 7" -> compass and clock, bottom left corner + "/compass 8" -> compass and clock, top left corner + +In addition: + + "/compass 0" -> forces compass and clock off. + +**Note:** The clock is a 24-hour clock, with only one hand that displays the hours. Noon is at the top and midnight is at the bottom. + +**New:** The default initial state can now be set in minetest.conf using "compass\_default\_corner = n", where "n" can be one of the eight corner numbers above. If it's positive, the compass (or compass and clock) will be enabled at start, while if it's negative, they will start disabled. + +Local mod storage is used to maintain the state and position of hud_compass display between sessions, per user. + + +Special Thanks +-------------- +Special thanks go out to MoNTE48, for pointing out that the image files could be significantly reduced with zopflipng (much better than with optipng), in this mod's first Pull Request. + + +Licenses +-------- +Source code + +> The MIT License (MIT) + +Media (textures) + +> Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) + +> (Compass textures were copied from my realcompass mod, which were originally based on the textures created by tacotexmex for the ccompass mod. Clock textures were derived from these same textures.) + + + + + + + diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..7733721 --- /dev/null +++ b/init.lua @@ -0,0 +1,145 @@ +-- hud_compass +-- Optionally place a compass and 24-hour clock on the screen. +-- A HUD version of my realcompass mod. +-- By David_G (kestral246@gmail.com) +-- 2019-12-31 + +local hud_compass = {} +local storage = minetest.get_mod_storage() + +-- State of hud_compass +-- 1 = NE, 2 = SE, 3 = SW, 4 = NW (just compass) +-- 5 = NE, 6 = SE, 7 = SW, 8 = NW (both compass and clock) +-- positive == enabled, negative == disabled + +-- Define default (if not overridden): SE corner, compass only, off by default +local default_corner = tonumber(minetest.settings:get("compass_default_corner") or -2) + +local lookup_compass = { + {type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-8,y=4}}, + {type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-8,y=-4}}, + {type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=8,y=-4}}, + {type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=8,y=4}}, + {type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-76,y=4}}, + {type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-76,y=-4}}, + {type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=76,y=-4}}, + {type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=76,y=4}} +} +local lookup_clock = { + {type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-8,y=4}}, + {type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-8,y=-4}}, + {type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=8,y=-4}}, + {type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=8,y=4}}, + {type="image", text="", position={x=1,y=0}, scale={x=4,y=4}, alignment={x=-1,y=1}, offset={x=-8,y=4}}, + {type="image", text="", position={x=1,y=1}, scale={x=4,y=4}, alignment={x=-1,y=-1}, offset={x=-8,y=-4}}, + {type="image", text="", position={x=0,y=1}, scale={x=4,y=4}, alignment={x=1,y=-1}, offset={x=8,y=-4}}, + {type="image", text="", position={x=0,y=0}, scale={x=4,y=4}, alignment={x=1,y=1}, offset={x=8,y=4}} +} + +-- backward compatibility (core version < 5.9.0) +if not core.has_feature("hud_def_type_field") then + for _, t in pairs({lookup_compass, lookup_clock}) do + for _, def in pairs(t) do + def.hud_elem_type = def.type + def.type = nil + end + end +end + +minetest.register_on_joinplayer(function(player) + local pname = player:get_player_name() + local corner = default_corner + if storage:get(pname) and tonumber(storage:get(pname)) then -- validate mod storage value + local temp = math.floor(tonumber(storage:get(pname))) + if temp ~= nil and temp ~= 0 and temp >= -8 and temp <= 8 then + corner = temp + end + end + hud_compass[pname] = { + id_compass = player:hud_add(lookup_compass[math.abs(corner)]), + last_image_compass = -1, + id_clock = player:hud_add(lookup_clock[math.abs(corner)]), + last_image_clock = -1, + state = corner, + } +end) + +minetest.register_chatcommand("compass", { + params = "[]", + description = "Change display of HUD Compass.", + privs = {}, + func = function(pname, params) + local player = minetest.get_player_by_name(pname) + if params and string.len(params) > 0 then -- includes corner parameter + local corner = tonumber(string.match(params, "^%d$")) + if corner and corner == 0 then -- disable compass and clock + player:hud_change(hud_compass[pname].id_compass, "text", "") -- blank hud compass + hud_compass[pname].last_image_compass = -1 + player:hud_change(hud_compass[pname].id_clock, "text", "") -- blank hud clock + hud_compass[pname].last_image_clock = -1 + hud_compass[pname].state = -1 * math.abs(hud_compass[pname].state) + storage:set_string(pname, hud_compass[pname].state) + elseif corner and corner > 0 and corner <= 4 then -- enable compass only to given corner + player:hud_remove(hud_compass[pname].id_compass) -- remove old hud compass + hud_compass[pname].last_image_compass = -1 + player:hud_remove(hud_compass[pname].id_clock) -- remove old hud clock + hud_compass[pname].last_image_clock = -1 + hud_compass[pname].id_compass = player:hud_add(lookup_compass[corner]) -- place new hud compass at requested corner + hud_compass[pname].state = corner + storage:set_string(pname, corner) + elseif corner and corner >= 5 and corner <= 8 then -- enable compass and clock to given corner + player:hud_remove(hud_compass[pname].id_compass) -- remove old hud compass + hud_compass[pname].last_image_compass = -1 + player:hud_remove(hud_compass[pname].id_clock) -- remove old hud clock + hud_compass[pname].last_image_clock = -1 + hud_compass[pname].id_compass = player:hud_add(lookup_compass[corner]) -- place new hud compass at requested corner + hud_compass[pname].id_clock = player:hud_add(lookup_clock[corner]) -- place new hud clock at requested corner + hud_compass[pname].state = corner + storage:set_string(pname, corner) + end + else -- just toggle hud + if hud_compass[pname].state > 0 then -- is enabled + player:hud_change(hud_compass[pname].id_compass, "text", "") -- blank hud compass + hud_compass[pname].last_image_compass = -1 + player:hud_change(hud_compass[pname].id_clock, "text", "") -- blank hud clock + hud_compass[pname].last_image_clock = -1 + end + hud_compass[pname].state = -1 * hud_compass[pname].state -- toggle state + storage:set_string(pname, hud_compass[pname].state) + end + end, +}) + +minetest.register_on_leaveplayer(function(player) + local pname = player:get_player_name() + if hud_compass[pname] then + hud_compass[pname] = nil + end +end) + +minetest.register_globalstep(function(dtime) + local players = minetest.get_connected_players() + for i,player in ipairs(players) do + local pname = player:get_player_name() + local dir = player:get_look_horizontal() + -- Calculate image indexes for compass and clock. + local angle_relative = math.deg(dir) + local image_compass = math.floor((angle_relative/22.5) + 0.5)%16 + local image_clock = math.floor(24 * minetest.get_timeofday()) + + if hud_compass[pname].state > 0 and image_compass ~= hud_compass[pname].last_image_compass then + local rc = player:hud_change(hud_compass[pname].id_compass, "text", "realcompass_"..image_compass..".png") + -- Check return code, seems to fix occasional startup glitch. + if rc == 1 then + hud_compass[pname].last_image_compass = image_compass + end + end + if hud_compass[pname].state >= 5 and image_clock ~= hud_compass[pname].last_image_clock then + local rc = player:hud_change(hud_compass[pname].id_clock, "text", "hud_24hr_clock_"..image_clock..".png") + -- Check return code, seems to fix occasional startup glitch. + if rc == 1 then + hud_compass[pname].last_image_clock = image_clock + end + end + end +end) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..1317ca8 --- /dev/null +++ b/mod.conf @@ -0,0 +1,5 @@ +name = hud_compass +description = Optionally place a HUD compass and 24-hour clock on the screen. +release = 29223 +author = kestral +title = HUD Compass diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..34f1760 Binary files /dev/null and b/screenshot.png differ diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..07ff95f --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,8 @@ +# This file contains settings for wand_of_illumination that can be changed in +# minetest.conf + +# Define default to place compass and whether to use clock. +# -1 to -4 = compass only (disabled), -5 to -8 = compass + clock (disabled) +# 1 to 4 = compass only (enabled), 5 to 8 = compass + clock (enabled) +# (default = -2) +compass_default_corner (Default corner to place compass) enum -2 -8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8 diff --git a/textures/hud_24hr_clock_0.png b/textures/hud_24hr_clock_0.png new file mode 100644 index 0000000..cffc830 Binary files /dev/null and b/textures/hud_24hr_clock_0.png differ diff --git a/textures/hud_24hr_clock_1.png b/textures/hud_24hr_clock_1.png new file mode 100644 index 0000000..0187e08 Binary files /dev/null and b/textures/hud_24hr_clock_1.png differ diff --git a/textures/hud_24hr_clock_10.png b/textures/hud_24hr_clock_10.png new file mode 100644 index 0000000..2ddbf8a Binary files /dev/null and b/textures/hud_24hr_clock_10.png differ diff --git a/textures/hud_24hr_clock_11.png b/textures/hud_24hr_clock_11.png new file mode 100644 index 0000000..2f2a212 Binary files /dev/null and b/textures/hud_24hr_clock_11.png differ diff --git a/textures/hud_24hr_clock_12.png b/textures/hud_24hr_clock_12.png new file mode 100644 index 0000000..6ea67b2 Binary files /dev/null and b/textures/hud_24hr_clock_12.png differ diff --git a/textures/hud_24hr_clock_13.png b/textures/hud_24hr_clock_13.png new file mode 100644 index 0000000..6611a78 Binary files /dev/null and b/textures/hud_24hr_clock_13.png differ diff --git a/textures/hud_24hr_clock_14.png b/textures/hud_24hr_clock_14.png new file mode 100644 index 0000000..6a0a037 Binary files /dev/null and b/textures/hud_24hr_clock_14.png differ diff --git a/textures/hud_24hr_clock_15.png b/textures/hud_24hr_clock_15.png new file mode 100644 index 0000000..5d8c4f2 Binary files /dev/null and b/textures/hud_24hr_clock_15.png differ diff --git a/textures/hud_24hr_clock_16.png b/textures/hud_24hr_clock_16.png new file mode 100644 index 0000000..38d981a Binary files /dev/null and b/textures/hud_24hr_clock_16.png differ diff --git a/textures/hud_24hr_clock_17.png b/textures/hud_24hr_clock_17.png new file mode 100644 index 0000000..0ac013e Binary files /dev/null and b/textures/hud_24hr_clock_17.png differ diff --git a/textures/hud_24hr_clock_18.png b/textures/hud_24hr_clock_18.png new file mode 100644 index 0000000..47d70a0 Binary files /dev/null and b/textures/hud_24hr_clock_18.png differ diff --git a/textures/hud_24hr_clock_19.png b/textures/hud_24hr_clock_19.png new file mode 100644 index 0000000..7fa91f4 Binary files /dev/null and b/textures/hud_24hr_clock_19.png differ diff --git a/textures/hud_24hr_clock_2.png b/textures/hud_24hr_clock_2.png new file mode 100644 index 0000000..2a8dda4 Binary files /dev/null and b/textures/hud_24hr_clock_2.png differ diff --git a/textures/hud_24hr_clock_20.png b/textures/hud_24hr_clock_20.png new file mode 100644 index 0000000..0739d16 Binary files /dev/null and b/textures/hud_24hr_clock_20.png differ diff --git a/textures/hud_24hr_clock_21.png b/textures/hud_24hr_clock_21.png new file mode 100644 index 0000000..53f3cff Binary files /dev/null and b/textures/hud_24hr_clock_21.png differ diff --git a/textures/hud_24hr_clock_22.png b/textures/hud_24hr_clock_22.png new file mode 100644 index 0000000..e8e32ad Binary files /dev/null and b/textures/hud_24hr_clock_22.png differ diff --git a/textures/hud_24hr_clock_23.png b/textures/hud_24hr_clock_23.png new file mode 100644 index 0000000..4158a77 Binary files /dev/null and b/textures/hud_24hr_clock_23.png differ diff --git a/textures/hud_24hr_clock_3.png b/textures/hud_24hr_clock_3.png new file mode 100644 index 0000000..06761c8 Binary files /dev/null and b/textures/hud_24hr_clock_3.png differ diff --git a/textures/hud_24hr_clock_4.png b/textures/hud_24hr_clock_4.png new file mode 100644 index 0000000..66b66e4 Binary files /dev/null and b/textures/hud_24hr_clock_4.png differ diff --git a/textures/hud_24hr_clock_5.png b/textures/hud_24hr_clock_5.png new file mode 100644 index 0000000..41ec7bf Binary files /dev/null and b/textures/hud_24hr_clock_5.png differ diff --git a/textures/hud_24hr_clock_6.png b/textures/hud_24hr_clock_6.png new file mode 100644 index 0000000..c23f6ae Binary files /dev/null and b/textures/hud_24hr_clock_6.png differ diff --git a/textures/hud_24hr_clock_7.png b/textures/hud_24hr_clock_7.png new file mode 100644 index 0000000..782dadd Binary files /dev/null and b/textures/hud_24hr_clock_7.png differ diff --git a/textures/hud_24hr_clock_8.png b/textures/hud_24hr_clock_8.png new file mode 100644 index 0000000..e514ac1 Binary files /dev/null and b/textures/hud_24hr_clock_8.png differ diff --git a/textures/hud_24hr_clock_9.png b/textures/hud_24hr_clock_9.png new file mode 100644 index 0000000..6c800a3 Binary files /dev/null and b/textures/hud_24hr_clock_9.png differ diff --git a/textures/realcompass_0.png b/textures/realcompass_0.png new file mode 100644 index 0000000..cff66cb Binary files /dev/null and b/textures/realcompass_0.png differ diff --git a/textures/realcompass_1.png b/textures/realcompass_1.png new file mode 100644 index 0000000..dfe027e Binary files /dev/null and b/textures/realcompass_1.png differ diff --git a/textures/realcompass_10.png b/textures/realcompass_10.png new file mode 100644 index 0000000..42285d4 Binary files /dev/null and b/textures/realcompass_10.png differ diff --git a/textures/realcompass_11.png b/textures/realcompass_11.png new file mode 100644 index 0000000..d5dba54 Binary files /dev/null and b/textures/realcompass_11.png differ diff --git a/textures/realcompass_12.png b/textures/realcompass_12.png new file mode 100644 index 0000000..ebb4bcd Binary files /dev/null and b/textures/realcompass_12.png differ diff --git a/textures/realcompass_13.png b/textures/realcompass_13.png new file mode 100644 index 0000000..b02c770 Binary files /dev/null and b/textures/realcompass_13.png differ diff --git a/textures/realcompass_14.png b/textures/realcompass_14.png new file mode 100644 index 0000000..948dd1f Binary files /dev/null and b/textures/realcompass_14.png differ diff --git a/textures/realcompass_15.png b/textures/realcompass_15.png new file mode 100644 index 0000000..ac8005d Binary files /dev/null and b/textures/realcompass_15.png differ diff --git a/textures/realcompass_2.png b/textures/realcompass_2.png new file mode 100644 index 0000000..6d5af28 Binary files /dev/null and b/textures/realcompass_2.png differ diff --git a/textures/realcompass_3.png b/textures/realcompass_3.png new file mode 100644 index 0000000..59944df Binary files /dev/null and b/textures/realcompass_3.png differ diff --git a/textures/realcompass_4.png b/textures/realcompass_4.png new file mode 100644 index 0000000..9a0dc9a Binary files /dev/null and b/textures/realcompass_4.png differ diff --git a/textures/realcompass_5.png b/textures/realcompass_5.png new file mode 100644 index 0000000..688b117 Binary files /dev/null and b/textures/realcompass_5.png differ diff --git a/textures/realcompass_6.png b/textures/realcompass_6.png new file mode 100644 index 0000000..ebcf95f Binary files /dev/null and b/textures/realcompass_6.png differ diff --git a/textures/realcompass_7.png b/textures/realcompass_7.png new file mode 100644 index 0000000..c89a6a0 Binary files /dev/null and b/textures/realcompass_7.png differ diff --git a/textures/realcompass_8.png b/textures/realcompass_8.png new file mode 100644 index 0000000..d9c3a59 Binary files /dev/null and b/textures/realcompass_8.png differ diff --git a/textures/realcompass_9.png b/textures/realcompass_9.png new file mode 100644 index 0000000..caec9d7 Binary files /dev/null and b/textures/realcompass_9.png differ