init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. local player_font_huds = {}
  2. local font_states = {
  3. {0, "Normal font"},
  4. {1, "Bold font"},
  5. {2, "Italic font"},
  6. {3, "Bold and italic font"},
  7. {4, "Monospace font"},
  8. {5, "Bold and monospace font"},
  9. {7, "ZOMG all the font styles"},
  10. }
  11. local font_default_def = {
  12. hud_elem_type = "text",
  13. position = {x = 0.5, y = 0.5},
  14. scale = {x = 2, y = 2},
  15. alignment = { x = 0, y = 0 },
  16. number = 0xFFFFFF,
  17. }
  18. local function add_font_hud(player, state)
  19. local def = table.copy(font_default_def)
  20. local statetbl = font_states[state]
  21. def.offset = {x = 0, y = 32 * state}
  22. def.style = statetbl[1]
  23. def.text = statetbl[2]
  24. return player:hud_add(def)
  25. end
  26. local font_etime = 0
  27. local font_state = 0
  28. minetest.register_globalstep(function(dtime)
  29. font_etime = font_etime + dtime
  30. if font_etime < 1 then
  31. return
  32. end
  33. font_etime = 0
  34. for _, player in ipairs(minetest.get_connected_players()) do
  35. local huds = player_font_huds[player:get_player_name()]
  36. if huds then
  37. for i, hud_id in ipairs(huds) do
  38. local statetbl = font_states[(font_state + i) % #font_states + 1]
  39. player:hud_change(hud_id, "style", statetbl[1])
  40. player:hud_change(hud_id, "text", statetbl[2])
  41. end
  42. end
  43. end
  44. font_state = font_state + 1
  45. end)
  46. minetest.register_chatcommand("hudfonts", {
  47. params = "[<HUD elements>]",
  48. description = "Show/Hide some text on the HUD with various font options",
  49. func = function(name, param)
  50. local player = minetest.get_player_by_name(name)
  51. local param = tonumber(param) or 0
  52. param = math.min(math.max(param, 1), #font_states)
  53. if player_font_huds[name] == nil then
  54. player_font_huds[name] = {}
  55. for i = 1, param do
  56. table.insert(player_font_huds[name], add_font_hud(player, i))
  57. end
  58. minetest.chat_send_player(name, ("%d text HUD element(s) added."):format(param))
  59. else
  60. local huds = player_font_huds[name]
  61. if huds then
  62. for _, hud_id in ipairs(huds) do
  63. player:hud_remove(hud_id)
  64. end
  65. minetest.chat_send_player(name, "All text HUD elements removed.")
  66. end
  67. player_font_huds[name] = nil
  68. end
  69. return true
  70. end,
  71. })
  72. -- Testing waypoint capabilities
  73. local player_waypoints = {}
  74. minetest.register_chatcommand("hudwaypoints", {
  75. params = "[ add | add_change | remove ]",
  76. description = "Create HUD waypoints at your position for testing (add: Add waypoints and change them after 0.5s (default). add_change: Add waypoints and change immediately. remove: Remove all waypoints)",
  77. func = function(name, params)
  78. local player = minetest.get_player_by_name(name)
  79. if not player then
  80. return false, "No player."
  81. end
  82. if params == "remove" then
  83. if player_waypoints[name] then
  84. for i=1, #player_waypoints[name] do
  85. player:hud_remove(player_waypoints[name][i])
  86. end
  87. player_waypoints[name] = {}
  88. end
  89. return true, "All waypoint HUD elements removed."
  90. end
  91. if not (params == "add_change" or params == "add" or params == "") then
  92. -- Incorrect syntax
  93. return false
  94. end
  95. local regular = player:hud_add {
  96. hud_elem_type = "waypoint",
  97. name = "regular waypoint",
  98. text = "m",
  99. number = 0xFFFFFF,
  100. world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0})
  101. }
  102. local reduced_precision = player:hud_add {
  103. hud_elem_type = "waypoint",
  104. name = "imprecise waypoint",
  105. text = "m (0.1 steps, precision = 10)",
  106. precision = 10,
  107. number = 0xFFFFFF,
  108. world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0})
  109. }
  110. local hidden_distance = player:hud_add {
  111. hud_elem_type = "waypoint",
  112. name = "waypoint with hidden distance",
  113. text = "this text is hidden as well (precision = 0)",
  114. precision = 0,
  115. number = 0xFFFFFF,
  116. world_pos = vector.add(player:get_pos(), {x = 0, y = 0.5, z = 0})
  117. }
  118. local function change(chplayer)
  119. if not (chplayer and chplayer:is_player()) then
  120. return
  121. end
  122. if regular then
  123. chplayer:hud_change(regular, "world_pos", vector.add(player:get_pos(), {x = 0, y = 3, z = 0}))
  124. chplayer:hud_change(regular, "number", 0xFF0000)
  125. end
  126. if reduced_precision then
  127. chplayer:hud_change(reduced_precision, "precision", 2)
  128. chplayer:hud_change(reduced_precision, "text", "m (0.5 steps, precision = 2)")
  129. chplayer:hud_change(reduced_precision, "number", 0xFFFF00)
  130. end
  131. if hidden_distance then
  132. chplayer:hud_change(hidden_distance, "number", 0x0000FF)
  133. end
  134. minetest.chat_send_player(chplayer:get_player_name(), "Waypoints changed.")
  135. end
  136. if params == "add_change" then
  137. -- change immediate
  138. change(player)
  139. else
  140. minetest.after(0.5, change, player)
  141. end
  142. local image_waypoint = player:hud_add {
  143. hud_elem_type = "image_waypoint",
  144. text = "testhud_waypoint.png",
  145. world_pos = player:get_pos(),
  146. scale = {x = 3, y = 3},
  147. offset = {x = 0, y = -32}
  148. }
  149. if not player_waypoints[name] then
  150. player_waypoints[name] = {}
  151. end
  152. if regular then
  153. table.insert(player_waypoints[name], regular)
  154. end
  155. if reduced_precision then
  156. table.insert(player_waypoints[name], reduced_precision)
  157. end
  158. if hidden_distance then
  159. table.insert(player_waypoints[name], hidden_distance)
  160. end
  161. if image_waypoint then
  162. table.insert(player_waypoints[name], image_waypoint)
  163. end
  164. regular = regular or "error"
  165. reduced_precision = reduced_precision or "error"
  166. hidden_distance = hidden_distance or "error"
  167. image_waypoint = image_waypoint or "error"
  168. return true, "Waypoints added. IDs: regular: " .. regular .. ", reduced precision: " .. reduced_precision ..
  169. ", hidden distance: " .. hidden_distance .. ", image waypoint: " .. image_waypoint
  170. end
  171. })
  172. minetest.register_on_joinplayer(function(player)
  173. player:set_properties({zoom_fov = 15})
  174. end)
  175. minetest.register_chatcommand("zoomfov", {
  176. params = "[<FOV>]",
  177. description = "Set or display your zoom_fov",
  178. func = function(name, param)
  179. local player = minetest.get_player_by_name(name)
  180. if not player then
  181. return false, "No player."
  182. end
  183. if param == "" then
  184. local fov = player:get_properties().zoom_fov
  185. return true, "zoom_fov = "..tostring(fov)
  186. end
  187. local fov = tonumber(param)
  188. if not fov then
  189. return false, "Missing or incorrect zoom_fov parameter!"
  190. end
  191. player:set_properties({zoom_fov = fov})
  192. fov = player:get_properties().zoom_fov
  193. return true, "zoom_fov = "..tostring(fov)
  194. end,
  195. })
  196. minetest.register_on_leaveplayer(function(player)
  197. player_font_huds[player:get_player_name()] = nil
  198. player_waypoints[player:get_player_name()] = nil
  199. end)