hud.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. --[[
  2. Register function to easily register new builtin hud elements
  3. `def` is a table and contains the following fields:
  4. elem_def the HUD element definition which can be changed with hud_replace_builtin
  5. events (optional) additional event names on which the element will be updated
  6. ("hud_changed" will always be used.)
  7. show_elem(player, flags, id)
  8. (optional) a function to decide if the element should be shown to a player
  9. It is called before the element gets updated.
  10. update_def(player, elem_def)
  11. (optional) a function to change the elem_def before it will be used.
  12. (elem_def can be changed, since the table which got set by using
  13. hud_replace_builtin isn't exposed to the API.)
  14. update_elem(player, id)
  15. (optional) a function to change the element after it has been updated
  16. (Is not called when the element is first set or recreated.)
  17. ]]--
  18. local registered_elements = {}
  19. local update_events = {}
  20. local function register_builtin_hud_element(name, def)
  21. registered_elements[name] = def
  22. for _, event in ipairs(def.events or {}) do
  23. update_events[event] = update_events[event] or {}
  24. table.insert(update_events[event], name)
  25. end
  26. end
  27. -- Stores HUD ids for all players
  28. local hud_ids = {}
  29. -- Updates one element
  30. -- In case the element is already added, it only calls the update_elem function from
  31. -- registered_elements. (To recreate the element remove it first.)
  32. local function update_element(player, player_hud_ids, elem_name, flags)
  33. local def = registered_elements[elem_name]
  34. local id = player_hud_ids[elem_name]
  35. if def.show_elem and not def.show_elem(player, flags, id) then
  36. if id then
  37. player:hud_remove(id)
  38. player_hud_ids[elem_name] = nil
  39. end
  40. return
  41. end
  42. if not id then
  43. if def.update_def then
  44. def.update_def(player, def.elem_def)
  45. end
  46. id = player:hud_add(def.elem_def)
  47. player_hud_ids[elem_name] = id
  48. return
  49. end
  50. if def.update_elem then
  51. def.update_elem(player, id)
  52. end
  53. end
  54. -- Updates all elements
  55. -- If to_update is specified it will only update those elements.
  56. local function update_hud(player, to_update)
  57. local flags = player:hud_get_flags()
  58. local playername = player:get_player_name()
  59. hud_ids[playername] = hud_ids[playername] or {}
  60. local player_hud_ids = hud_ids[playername]
  61. if to_update then
  62. for _, elem_name in ipairs(to_update) do
  63. update_element(player, player_hud_ids, elem_name, flags)
  64. end
  65. else
  66. for elem_name, _ in pairs(registered_elements) do
  67. update_element(player, player_hud_ids, elem_name, flags)
  68. end
  69. end
  70. end
  71. local function player_event_handler(player, eventname)
  72. assert(player:is_player())
  73. if eventname == "hud_changed" then
  74. update_hud(player)
  75. return
  76. end
  77. -- Custom events
  78. local to_update = update_events[eventname]
  79. if to_update then
  80. update_hud(player, to_update)
  81. end
  82. end
  83. -- Returns true if successful, otherwise false,
  84. -- but currently the return value is not documented in the Lua API.
  85. function core.hud_replace_builtin(elem_name, elem_def)
  86. assert(type(elem_def) == "table")
  87. local registered = registered_elements[elem_name]
  88. if not registered then
  89. return false
  90. end
  91. registered.elem_def = table.copy(elem_def)
  92. for playername, player_hud_ids in pairs(hud_ids) do
  93. local player = core.get_player_by_name(playername)
  94. local id = player_hud_ids[elem_name]
  95. if player and id then
  96. player:hud_remove(id)
  97. player_hud_ids[elem_name] = nil
  98. update_element(player, player_hud_ids, elem_name, player:hud_get_flags())
  99. end
  100. end
  101. return true
  102. end
  103. local function cleanup_builtin_hud(player)
  104. hud_ids[player:get_player_name()] = nil
  105. end
  106. -- Append "update_hud" as late as possible
  107. -- This ensures that the HUD is hidden when the flags are updated in this callback
  108. core.register_on_mods_loaded(function()
  109. core.register_on_joinplayer(function(player)
  110. update_hud(player)
  111. end)
  112. end)
  113. core.register_on_leaveplayer(cleanup_builtin_hud)
  114. core.register_playerevent(player_event_handler)
  115. ---- Builtin HUD Elements
  116. --- Healthbar
  117. -- Cache setting
  118. local enable_damage = core.settings:get_bool("enable_damage")
  119. local function scale_to_hud_max(player, field)
  120. -- Scale "hp" or "breath" to the hud maximum dimensions
  121. local current = player["get_" .. field](player)
  122. local nominal
  123. if field == "hp" then -- HUD is called health but field is hp
  124. nominal = registered_elements.health.elem_def.item
  125. else
  126. nominal = registered_elements[field].elem_def.item
  127. end
  128. local max_display = math.max(player:get_properties()[field .. "_max"], current)
  129. return math.ceil(current / max_display * nominal)
  130. end
  131. register_builtin_hud_element("health", {
  132. elem_def = {
  133. type = "statbar",
  134. position = {x = 0.5, y = 1},
  135. text = "heart.png",
  136. text2 = "heart_gone.png",
  137. number = core.PLAYER_MAX_HP_DEFAULT,
  138. item = core.PLAYER_MAX_HP_DEFAULT,
  139. direction = 0,
  140. size = {x = 24, y = 24},
  141. offset = {x = (-10 * 24) - 25, y = -(48 + 24 + 16)},
  142. },
  143. events = {"properties_changed", "health_changed"},
  144. show_elem = function(player, flags)
  145. return flags.healthbar and enable_damage and
  146. player:get_armor_groups().immortal ~= 1
  147. end,
  148. update_def = function(player, elem_def)
  149. elem_def.item = elem_def.item or elem_def.number or core.PLAYER_MAX_HP_DEFAULT
  150. elem_def.number = scale_to_hud_max(player, "hp")
  151. end,
  152. update_elem = function(player, id)
  153. player:hud_change(id, "number", scale_to_hud_max(player, "hp"))
  154. end,
  155. })
  156. --- Breathbar
  157. -- Stores core.after calls for every player
  158. local breathbar_removal_jobs = {}
  159. register_builtin_hud_element("breath", {
  160. elem_def = {
  161. type = "statbar",
  162. position = {x = 0.5, y = 1},
  163. text = "bubble.png",
  164. text2 = "bubble_gone.png",
  165. number = core.PLAYER_MAX_BREATH_DEFAULT * 2,
  166. item = core.PLAYER_MAX_BREATH_DEFAULT * 2,
  167. direction = 0,
  168. size = {x = 24, y = 24},
  169. offset = {x = 25, y= -(48 + 24 + 16)},
  170. },
  171. events = {"properties_changed", "breath_changed"},
  172. show_elem = function(player, flags, id)
  173. local show_breathbar = flags.breathbar and enable_damage and
  174. player:get_armor_groups().immortal ~= 1
  175. if id then
  176. -- The element will not prematurely be removed by update_element
  177. -- (but may still be instantly removed if the flag changed)
  178. return show_breathbar
  179. end
  180. -- Don't add the element if the breath is full
  181. local breath_relevant = player:get_breath() < player:get_properties().breath_max
  182. return show_breathbar and breath_relevant
  183. end,
  184. update_def = function(player, elem_def)
  185. elem_def.item = elem_def.item or elem_def.number or core.PLAYER_MAX_BREATH_DEFAULT
  186. elem_def.number = scale_to_hud_max(player, "breath")
  187. end,
  188. update_elem = function(player, id)
  189. player:hud_change(id, "number", scale_to_hud_max(player, "breath"))
  190. local player_name = player:get_player_name()
  191. local breath_relevant = player:get_breath() < player:get_properties().breath_max
  192. if not breath_relevant then
  193. if not breathbar_removal_jobs[player_name] then
  194. -- The breathbar stays for some time and then gets removed.
  195. breathbar_removal_jobs[player_name] = core.after(1, function()
  196. local player = core.get_player_by_name(player_name)
  197. local player_hud_ids = hud_ids[player_name]
  198. if player and player_hud_ids and player_hud_ids.breath then
  199. player:hud_remove(player_hud_ids.breath)
  200. player_hud_ids.breath = nil
  201. end
  202. breathbar_removal_jobs[player_name] = nil
  203. end)
  204. end
  205. else
  206. -- Cancel removal
  207. local job = breathbar_removal_jobs[player_name]
  208. if job then
  209. job:cancel()
  210. breathbar_removal_jobs[player_name] = nil
  211. end
  212. end
  213. end,
  214. })
  215. --- Minimap
  216. register_builtin_hud_element("minimap", {
  217. elem_def = {
  218. type = "minimap",
  219. position = {x = 1, y = 0},
  220. alignment = {x = -1, y = 1},
  221. offset = {x = -10, y = 10},
  222. size = {x = 0, y = -25},
  223. },
  224. show_elem = function(player, flags)
  225. local proto_ver = core.get_player_information(player:get_player_name()).protocol_version
  226. -- Don't add a minimap for clients which already have it hardcoded in C++.
  227. return flags.minimap and proto_ver >= 44
  228. end,
  229. update_def = function(player, elem_def)
  230. local proto_ver = core.get_player_information(player:get_player_name()).protocol_version
  231. -- Only use percentage when the client supports it.
  232. elem_def.size = proto_ver >= 45 and {x = 0, y = -25} or {x = 256, y = 256}
  233. end,
  234. })
  235. --- Hotbar
  236. register_builtin_hud_element("hotbar", {
  237. elem_def = {
  238. type = "hotbar",
  239. position = {x = 0.5, y = 1},
  240. direction = 0,
  241. alignment = {x = 0, y = -1},
  242. offset = {x = 0, y = -4}, -- Extra padding below.
  243. },
  244. show_elem = function(player, flags)
  245. return flags.hotbar
  246. end,
  247. })