1
0

init.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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! " .. core.colorize("green", "Green") ..
  11. core.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. core.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(core.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. core.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 = core.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. core.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. core.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. core.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 = core.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. core.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. core.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. core.register_on_joinplayer(function(player)
  176. player:set_properties({zoom_fov = 15})
  177. end)
  178. core.register_chatcommand("zoomfov", {
  179. params = "[<FOV>]",
  180. description = "Set or display your zoom_fov",
  181. func = function(name, param)
  182. local player = core.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. -- Hotbars
  200. local hud_hotbar_defs = {
  201. {
  202. type = "hotbar",
  203. position = {x=0.2, y=0.5},
  204. direction = 0,
  205. alignment = {x=1, y=-1},
  206. },
  207. {
  208. type = "hotbar",
  209. position = {x=0.2, y=0.5},
  210. direction = 2,
  211. alignment = {x=1, y=1},
  212. },
  213. {
  214. type = "hotbar",
  215. position = {x=0.7, y=0.5},
  216. direction = 0,
  217. offset = {x=140, y=20},
  218. alignment = {x=-1, y=-1},
  219. },
  220. {
  221. type = "hotbar",
  222. position = {x=0.7, y=0.5},
  223. direction = 2,
  224. offset = {x=140, y=20},
  225. alignment = {x=-1, y=1},
  226. },
  227. }
  228. local player_hud_hotbars= {}
  229. core.register_chatcommand("hudhotbars", {
  230. description = "Shows some test Lua HUD elements of type hotbar. " ..
  231. "(add: Adds elements (default). remove: Removes elements)",
  232. params = "[ add | remove ]",
  233. func = function(name, params)
  234. local player = core.get_player_by_name(name)
  235. if not player then
  236. return false, "No player."
  237. end
  238. local id_table = player_hud_hotbars[name]
  239. if not id_table then
  240. id_table = {}
  241. player_hud_hotbars[name] = id_table
  242. end
  243. if params == "remove" then
  244. for _, id in ipairs(id_table) do
  245. player:hud_remove(id)
  246. end
  247. return true, "Hotbars removed."
  248. end
  249. -- params == "add" or default
  250. for _, def in ipairs(hud_hotbar_defs) do
  251. table.insert(id_table, player:hud_add(def))
  252. end
  253. return true, #hud_hotbar_defs .." Hotbars added."
  254. end
  255. })
  256. -- Inventories
  257. local hud_inventory_defs = {
  258. {
  259. type = "inventory",
  260. position = {x=0.2, y=0.5},
  261. direction = 0,
  262. text = "main",
  263. number = 4,
  264. item = 2,
  265. },
  266. {
  267. type = "inventory",
  268. position = {x=0.2, y=0.5},
  269. direction = 2,
  270. text = "main",
  271. number = 4,
  272. item = 2,
  273. },
  274. {
  275. type = "inventory",
  276. position = {x=0.7, y=0.5},
  277. direction = 1,
  278. text = "main",
  279. number = 4,
  280. item = 2,
  281. },
  282. {
  283. type = "inventory",
  284. position = {x=0.7, y=0.5},
  285. direction = 3,
  286. text = "main",
  287. number = 4,
  288. item = 2,
  289. },
  290. }
  291. local player_hud_inventories= {}
  292. core.register_chatcommand("hudinventories", {
  293. description = "Shows some test Lua HUD elements of type inventory. (add: Adds elements (default). remove: Removes elements)",
  294. params = "[ add | remove ]",
  295. func = function(name, params)
  296. local player = core.get_player_by_name(name)
  297. if not player then
  298. return false, "No player."
  299. end
  300. local id_table = player_hud_inventories[name]
  301. if not id_table then
  302. id_table = {}
  303. player_hud_inventories[name] = id_table
  304. end
  305. if params == "remove" then
  306. for _, id in ipairs(id_table) do
  307. player:hud_remove(id)
  308. end
  309. return true, "HUD Inventories removed."
  310. end
  311. -- params == "add" or default
  312. for _, def in ipairs(hud_inventory_defs) do
  313. table.insert(id_table, player:hud_add(def))
  314. end
  315. return true, #hud_inventory_defs .." HUD Inventories added."
  316. end
  317. })
  318. core.register_on_leaveplayer(function(player)
  319. local playername = player:get_player_name()
  320. player_font_huds[playername] = nil
  321. player_waypoints[playername] = nil
  322. player_hud_hotbars[playername] = nil
  323. player_hud_inventories[playername] = nil
  324. end)
  325. core.register_chatcommand("hudprint", {
  326. description = "Writes all used Lua HUD elements into chat.",
  327. func = function(name, params)
  328. local player = core.get_player_by_name(name)
  329. if not player then
  330. return false, "No player."
  331. end
  332. local s = "HUD elements:"
  333. for k, elem in pairs(player:hud_get_all()) do
  334. local ename = dump(elem.name)
  335. local etype = dump(elem.type)
  336. local epos = "{x="..elem.position.x..", y="..elem.position.y.."}"
  337. s = s.."\n["..k.."] type = "..etype.." | name = "..ename.." | pos = ".. epos
  338. end
  339. return true, s
  340. end
  341. })
  342. local hud_flags = {"hotbar", "healthbar", "crosshair", "wielditem", "breathbar",
  343. "minimap", "minimap_radar", "basic_debug", "chat"}
  344. core.register_chatcommand("hudtoggleflag", {
  345. description = "Toggles a hud flag.",
  346. params = "[ ".. table.concat(hud_flags, " | ") .." ]",
  347. func = function(name, params)
  348. local player = core.get_player_by_name(name)
  349. if not player then
  350. return false, "No player."
  351. end
  352. local flags = player:hud_get_flags()
  353. if flags[params] == nil then
  354. return false, "Unknown hud flag."
  355. end
  356. flags[params] = not flags[params]
  357. player:hud_set_flags(flags)
  358. return true, "Flag \"".. params .."\" set to ".. tostring(flags[params]) .. "."
  359. end
  360. })