init.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. -- Localize for performance.
  2. local math_floor = math.floor
  3. -- Code API.
  4. if not minetest.global_exists("hud_clock") then hud_clock = {} end
  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 >= 1.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. local days1 = math_floor(serveressentials.get_outback_timeout() / (60*60*24))
  113. local days2 = math_floor(randspawn.get_spawn_reset_timeout() / (60*60*24))
  114. local days3 = math_floor(serveressentials.get_midfeld_timeout() / (60*60*24))
  115. days1 = math.max(days1, 0)
  116. days2 = math.max(days2, 0)
  117. days3 = math.max(days3, 0)
  118. return hud_clock.get_date_string() ..
  119. "\nSpawn: " .. randspawn.get_spawn_name() ..
  120. "\nOutback Winds: " .. days1 .. " Days" ..
  121. "\nOutback Gate: " .. days2 .. " Days" ..
  122. "\nMidfeld Fog: " .. days3 .. " Days"
  123. end
  124. minetest.register_node("clock:calendar", {
  125. description = "Calendar of Enyekala",
  126. tiles = {"calendar.png"},
  127. wield_image = "calendar.png",
  128. inventory_image = "calendar.png",
  129. sounds = default.node_sound_leaves_defaults(),
  130. groups = utility.dig_groups("bigitem", {flammable = 1}),
  131. paramtype = 'light',
  132. paramtype2 = "wallmounted",
  133. drawtype = "nodebox",
  134. sunlight_propagates = true,
  135. walkable = false,
  136. node_box = {
  137. type = "wallmounted",
  138. wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
  139. wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
  140. wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
  141. },
  142. selection_box = {type = "wallmounted"},
  143. on_construct = function(pos)
  144. local meta = minetest.get_meta(pos)
  145. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  146. minetest.get_node_timer(pos):start(60*60)
  147. end,
  148. on_timer = function(pos, elapsed)
  149. local meta = minetest.get_meta(pos)
  150. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  151. minetest.get_node_timer(pos):start(60*60)
  152. end,
  153. on_punch = function(pos, node, puncher, pt)
  154. if not puncher or not puncher:is_player() then
  155. return
  156. end
  157. local meta = minetest.get_meta(pos)
  158. meta:set_string("infotext", hud_clock.get_calendar_infotext())
  159. end,
  160. })
  161. minetest.register_craft({
  162. output = "clock:calendar",
  163. recipe = {
  164. {'', 'default:paper', ''},
  165. {'', 'default:stick', ''},
  166. {'', 'default:paper', ''},
  167. },
  168. })