init.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. -- Localize for performance.
  2. local math_floor = math.floor
  3. -- Code API.
  4. hud_clock = hud_clock or {}
  5. local player_hud = {}
  6. local timer = 0
  7. local positionx = 1.0
  8. local positiony = 1.0 --0.02
  9. --local positionx = 0.30; --horz
  10. --local positiony = 0.90; --vert
  11. local last_time = os.time()
  12. local function floormod ( x, y )
  13. return (math_floor(x) % y);
  14. end
  15. local function get_digxp(pname)
  16. return string.format("%.2f", xp.get_xp(pname, "digxp"))
  17. end
  18. function hud_clock.get_time()
  19. local secs = (60*60*24*minetest.get_timeofday())
  20. local s = floormod(secs, 60)
  21. local m = floormod(secs/60, 60)
  22. local h = floormod(secs/3600, 60)
  23. local a = "Noctis"
  24. if secs >= 60*60*5 and secs <= 60*60*19 then
  25. a = "Dies"
  26. end
  27. if h > 12 then
  28. h = h - 12
  29. end
  30. if h < 1 then
  31. h = h + 12
  32. end
  33. return ("%02d:%02d %s"):format(h, m, a);
  34. end
  35. minetest.register_globalstep(function ( dtime )
  36. timer = timer + dtime;
  37. if os.time() >= last_time then
  38. last_time = os.time() + 1
  39. if (timer >= 10.0) then
  40. timer = 0
  41. for _,player in ipairs(minetest.get_connected_players()) do
  42. local name = player:get_player_name();
  43. if player_hud[name] then
  44. local h = player_hud[name].clock
  45. player:hud_change(h, "text", hud_clock.get_time())
  46. end
  47. end
  48. end
  49. end
  50. end)
  51. function hud_clock.update_xp(pname)
  52. local player = minetest.get_player_by_name(pname)
  53. if player and player_hud[pname] then
  54. local x = player_hud[pname].digxp
  55. player:hud_change(x, "text", ("Mineral XP: " .. get_digxp(pname)))
  56. end
  57. end
  58. minetest.register_on_joinplayer(function(player)
  59. local pname = player:get_player_name()
  60. if player_hud[pname] then
  61. player_hud[pname] = nil
  62. end
  63. player_hud[pname] = {}
  64. local offy = -130
  65. local h = player:hud_add({
  66. hud_elem_type = "text",
  67. position = {x=positionx, y=positiony},
  68. alignment = {x=-1, y=1},
  69. offset = {x=-16, y=offy},
  70. text = hud_clock.get_time(),
  71. number = 0xFFFFFF,
  72. })
  73. local c = player:hud_add({
  74. hud_elem_type = "image",
  75. position = {x=positionx, y=positiony},
  76. alignment = {x=-1, y=1},
  77. offset = {x=-108, y=offy},
  78. scale = {x=1, y=1},
  79. text = "mthudclock.png",
  80. })
  81. local x = player:hud_add({
  82. hud_elem_type = "text",
  83. position = {x=positionx, y=positiony},
  84. offset = {x=-16, y=offy + 18},
  85. alignment = {x=-1, y=1},
  86. text = ("Mineral XP: " .. get_digxp(pname)),
  87. number = 0xFFFFFF,
  88. })
  89. player_hud[pname].clock = h
  90. player_hud[pname].icon = c
  91. player_hud[pname].digxp = x
  92. end)
  93. -- Do NOT change formatting, code relies on this!
  94. function hud_clock.get_datetime(days)
  95. local season = snow.get_day()
  96. local r1 = math_floor(days/(34*12)) -- Years.
  97. local r2 = math_floor(days%(34*12)) -- Year remainder, in days.
  98. local m1 = math_floor(r2/34) -- Months.
  99. local m2 = math_floor(r2%34) -- Month remainder, in days.
  100. local d1 = m2 -- Days.
  101. return season .. "\nSince Epoch: " .. r1+1 .. "/" .. m1+1 .. "/" .. d1+1
  102. end
  103. -- Do NOT change formatting, code relies on this!
  104. function hud_clock.get_date_string()
  105. local time = os.time()
  106. local epoch = os.time({year=2016, month=10, day=1})
  107. time = time - epoch
  108. local days = math_floor(((time/60)/60)/24)
  109. return hud_clock.get_datetime(days)
  110. end
  111. function hud_clock.get_calendar_infotext()
  112. return hud_clock.get_date_string() ..
  113. "\nSpawn: " .. randspawn.get_spawn_name() ..
  114. "\nOutback Winds: " .. math_floor(serveressentials.get_outback_timeout() / (60*60*24)) .. " Days" ..
  115. "\nOutback Gate: " .. math_floor(randspawn.get_spawn_reset_timeout() / (60*60*24)) .. " Days"
  116. end
  117. minetest.register_node("clock:calendar", {
  118. description = "Snowmelt Calendar",
  119. tiles = {"calendar.png"},
  120. wield_image = "calendar.png",
  121. inventory_image = "calendar.png",
  122. sounds = default.node_sound_leaves_defaults(),
  123. groups = utility.dig_groups("bigitem"),
  124. paramtype = 'light',
  125. paramtype2 = "wallmounted",
  126. drawtype = "nodebox",
  127. sunlight_propagates = true,
  128. walkable = false,
  129. node_box = {
  130. type = "wallmounted",
  131. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  132. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  133. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  134. },
  135. selection_box = {type = "wallmounted"},
  136. on_construct = function(pos)
  137. local meta = minetest.get_meta(pos)
  138. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  139. minetest.get_node_timer(pos):start(60*60)
  140. end,
  141. on_timer = function(pos, elapsed)
  142. local meta = minetest.get_meta(pos)
  143. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  144. minetest.get_node_timer(pos):start(60*60)
  145. end,
  146. on_punch = function(pos, node, puncher, pt)
  147. if not puncher or not puncher:is_player() then
  148. return
  149. end
  150. local meta = minetest.get_meta(pos)
  151. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  152. end,
  153. })
  154. minetest.register_craft({
  155. output = "clock:calendar",
  156. recipe = {
  157. {'', 'default:paper', ''},
  158. {'', 'default:stick', ''},
  159. {'', 'default:paper', ''},
  160. },
  161. })