2
0

init.lua 6.9 KB

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