-- occompass -- Optionally places a compass and/or a bearing device on the screen -- Descended from a mod by David_G (kestral246@gmail.com) -- This version is by Robert Kiraly (OldCoder) local get_png = function (basepng) return basepng end occompass.param_compass = { hud_elem_type="image", text="", position = { x = 1 , y = 0 } , scale = { x = 1 , y = 1 } , alignment = { x = -1 , y = 1 } , offset = { x = -8 , y = 4 } , } ocbearing.param_bearing = { hud_elem_type="image", text="", position = { x = 1 , y = 0 } , scale = { x = 1 , y = 1 } , alignment = { x = -1 , y = 1 } , offset = { x = -8 , y = 4 } , } if occompass.enable_compass and occompass.enable_bearing then ocbearing.param_bearing.position.y = 0.15 end minetest.register_on_joinplayer (function(player) local pname = player:get_player_name() if occompass.enable_bearing then ocbearing [pname] = { id_bearing = player:hud_add (ocbearing.param_bearing) , last_image = -1 , state = 1 , counter = 0 , } end if occompass.enable_compass then occompass [pname] = { id_compass = player:hud_add (occompass.param_compass) , last_image = -1 , state = 1 , counter = 0 , } end end) minetest.register_on_leaveplayer (function (player) if player == nil then return end local pname = player:get_player_name() if pname == nil then return end if ocbearing [pname] then ocbearing [pname] = nil end if occompass [pname] then occompass [pname] = nil end end) local bearing_cache = {} local compass_cache = {} 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 if pname ~= nil then dir = player:get_look_horizontal() end if pname ~= nil and ocbearing [pname] ~= nil then local angle_relative = 360 - math.deg (dir) if angle_relative < 0 then angle_relative = 0 end if angle_relative >= 360 then angle_relative = 0 end local image_bearing = math.floor ((angle_relative / 22.5) + 0.5) % 16 if ocbearing [pname].last_image ~= image_bearing then local png = bearing_cache [image_bearing] if png == nil then png = get_png ("ocbearing_bg.png^ocbearing_" .. image_bearing .. ".png") bearing_cache [image_bearing] = png end local rc = player:hud_change (ocbearing [pname].id_bearing, "text", png) if rc == 1 then ocbearing [pname].last_image = image_bearing end end end if pname ~= nil and occompass [pname] ~= nil then local angle_relative = math.deg (dir) if angle_relative < 0 then angle_relative = 0 end if angle_relative >= 360 then angle_relative = 0 end local image_compass = math.floor ((angle_relative / 22.5) + 0.5) % 16 if occompass [pname].last_image ~= image_compass then local png = compass_cache [image_compass] if png == nil then png = get_png ("occompass_" .. image_compass .. ".png") compass_cache [image_compass] = png end local rc = player:hud_change (occompass [pname].id_compass, "text", png) if rc == 1 then occompass [pname].last_image = image_compass end end end end end)