init.lua 4.7 KB

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