gui.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. local MT = minetest
  2. local vMod = vm_lighting_wand
  3. local players = {}
  4. math.randomseed(os.time())
  5. local get_random = function()
  6. return math.random(math.random(1000, 14999), math.random(15000, 999999))
  7. end
  8. local get_formname = function(player)
  9. return vMod.tool .. ":" .. vMod.get_gui_player(player)
  10. end
  11. local bar = {
  12. bg = vMod.modname .. "_bar_bg.png^[lowpart:",
  13. yellow = vMod.modname .. "_bar_yellow.png",
  14. green = vMod.modname .. "_bar_green.png"
  15. }
  16. local progress_bar = function(fsb, x, y, w, h, percent, text, color)
  17. fsb:image(fsb:slot(x), fsb:slot(y), w, h, bar.bg .. (percent) .. ":" .. bar[color])
  18. fsb:tooltip(fsb:slot(x), fsb:slot(y), w, h, text .. "\n\n" .. MT.colorize("goldenrod", percent .. "%"))
  19. end
  20. local add_stat = function(fsb, x, y, name, value, info)
  21. info = info and " " .. MT.colorize("darkslategrey", info) or ""
  22. fsb:label(fsb:slot(x) + fsb.padding, fsb:slot(y) + fsb.padding, MT.colorize("grey", name))
  23. fsb:label(fsb:slot(x + 1.5), fsb:slot(y) + fsb.padding, MT.colorize("darkcyan", value) .. info)
  24. end
  25. local function commas(n) -- credit http://richard.warburton.it
  26. local left, num, right = string.match(n, "^([^%d]*%d)(%d*)(.-)$")
  27. return left .. (num:reverse():gsub("(%d%d%d)", "%1,"):reverse()) .. right
  28. end
  29. -- Wands formspec
  30. vMod.open_gui = function(player, id)
  31. --
  32. local fsb = vMod.fsbuilder:new({height = 11.65, width = 10.5})
  33. -- If a id was given then check it
  34. if not player or id and vMod.get_gui_player(player) ~= id then
  35. vMod.close_gui(player)
  36. return
  37. end
  38. local player_name = player:get_player_name()
  39. local detached_name = vMod.detached_name(player_name)
  40. -- now set a id to this player
  41. players[player_name] = get_random()
  42. local is_creative_mode = vMod.is_creative_mode(player)
  43. local level = vMod.get_level(player)
  44. local levels = vMod.get_all_levels()
  45. local power = vMod.get_power(player)
  46. local max_power = vMod.get_max_power()
  47. local next_level = levels[level.id + 1] and levels[level.id + 1].amount_start or max_power
  48. local max_percent = vMod.round((power / max_power) * 100, 2)
  49. local level_percent = vMod.round(((power - level.amount_start) / (next_level - level.amount_start)) * 100, 2)
  50. -- Title
  51. fsb:label(
  52. fsb:slot(0.125),
  53. fsb:slot(0.15),
  54. fsb.esc(
  55. MT.colorize("limegreen", "Lighting Wand") ..
  56. (is_creative_mode and " :: " .. MT.colorize("deeppink", "creative mode") or "")
  57. )
  58. )
  59. -- Level
  60. fsb:label(fsb:slot(0.25), fsb:slot(0.75), MT.colorize("goldenrod", level.name))
  61. -- Power
  62. add_stat(fsb, 0.25, 1.50, "Power", commas(tostring(power)))
  63. -- Range
  64. add_stat(fsb, 0.25, 2.00, "Range", level.max_range)
  65. -- Efficiency
  66. add_stat(fsb, 0.25, 2.50, "Efficiency", (level.fuel_efficiency * 100) .. "%")
  67. -- Cost per use
  68. add_stat(fsb, 0.25, 3.00, "Cost", level.cost_per_use, "per use")
  69. -- Cost per node
  70. add_stat(fsb, 0.25, 3.50, "", level.cost_per_node, "per node travelled")
  71. -- Show power % needed to level up/max
  72. progress_bar(fsb, 7.1, 0.5, 0.3, 4.75, level_percent, "Level Progress", "yellow")
  73. -- Show max power % bar
  74. progress_bar(fsb, 7.7, 0.5, 0.3, 4.75, max_percent, "Total Power", "green")
  75. -- detached fuel slot
  76. fsb:list("detached:" .. detached_name, vMod.detached_list, fsb:slot(5.6), fsb:slot(3), 1, 1)
  77. -- slot tooltip
  78. fsb:tooltip(fsb:slot(5.6), fsb:slot(3), 1, 1, "Fuel Input")
  79. -- Players inventory
  80. fsb:player_list(fsb.padding, 4.75)
  81. fsb:listring("current_player", "main")
  82. fsb:listring("detached:" .. detached_name, vMod.detached_list)
  83. --Show the formspec
  84. MT.show_formspec(player_name, get_formname(player), fsb:get_form_string())
  85. end
  86. vMod.get_gui_player = function(player)
  87. if not player then
  88. return nil
  89. end
  90. return players[player:get_player_name()] or nil
  91. end
  92. vMod.close_gui = function(player)
  93. if not player then
  94. return
  95. end
  96. local player_name = player:get_player_name()
  97. MT.close_formspec(player_name, get_formname(player))
  98. players[player_name] = nil
  99. return
  100. end
  101. --[[
  102. local string_starts = function(s, prefix)
  103. return string.sub(s, 1, string.len(prefix)) == prefix
  104. end
  105. MT.register_on_player_receive_fields(
  106. function(player, formname, fields)
  107. -- nothing intresting skip it
  108. if not player or not string_starts(formname, vMod.tool) then
  109. return
  110. end
  111. -- its just a quit?
  112. if fields.quit ~= nil then
  113. vMod.close_gui(player)
  114. return
  115. end
  116. -- Take a closer look and ensure the formname matches the expected name
  117. if get_formname(player) == formname then
  118. vMod.close_gui(player)
  119. return
  120. end
  121. --todo: toggle sound/particles on/off
  122. end
  123. )
  124. ]]