functions.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. local function xplayer(player)
  2. if not player:is_player() then
  3. return
  4. end
  5. local name = player:get_player_name()
  6. return christmas.players[name]
  7. end
  8. function christmas.get_present_formspec(pos)--Taken from default chest
  9. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  10. local formspec =
  11. "size[8,9]" ..
  12. "list[nodemeta:" .. spos .. ";main;3.5,2.5;1,1;]" ..
  13. "list[current_player;main;0,4.85;8,1;]" ..
  14. "list[current_player;main;0,6.08;8,3;8]" ..
  15. "listring[nodemeta:" .. spos .. ";main]" ..
  16. "listring[current_player;main]"
  17. return formspec
  18. end
  19. function christmas.to_time(time)
  20. local remaining = time % 86400
  21. remaining = remaining % 3600
  22. local minutes = math.floor(remaining/60)
  23. remaining = remaining % 60
  24. local seconds = remaining
  25. if (minutes < 10) then
  26. minutes = "0" .. tostring(minutes)
  27. end
  28. if (seconds < 10) then
  29. seconds = "0" .. tostring(seconds)
  30. end
  31. answer = minutes..':'..seconds
  32. return answer
  33. end
  34. function christmas.register_reward(item, quantity, rarity)
  35. table.insert(christmas.rewards, {item=item, quantity=quantity, rarity=rarity})
  36. end
  37. function christmas.random_reward()--Adapted from Dungeontest room selection "dungeon_rooms.random_roomdata"
  38. local pool = christmas.rewards
  39. local candidates = {}
  40. local raresum = 0
  41. for i=1, #pool do
  42. local reward = pool[i]
  43. table.insert(candidates, pool)
  44. raresum = raresum + reward.rarity
  45. end
  46. local rarepick = math.random() * raresum
  47. local rarecount = 0
  48. for c=1, #candidates do
  49. local rewards = candidates[c]
  50. rarecount = rarecount + christmas.rewards[c].rarity
  51. local q = christmas.rewards[c].quantity
  52. local quantity = math.random(q.min, q.max)
  53. if rarecount >= rarepick then
  54. --return christmas.rewards[c].item
  55. return ItemStack(christmas.rewards[c].item.." "..quantity)
  56. end
  57. end
  58. end
  59. function christmas.eat_candy(hp_change, replace_with_item)
  60. return function(itemstack, user, pointed_thing)
  61. local name = user:get_player_name()
  62. local p = xplayer(user)
  63. christmas.players[name].candy = p.candy +1
  64. if p.candy == 8 then
  65. p.time = 60
  66. p.hud.ui = user:hud_add({
  67. hud_elem_type = "image",
  68. position = {x = 0.2, y = 0.2},
  69. offset = {x = 0, y = 0},
  70. text = "christmas_powerup_ui.png",
  71. scale = { x = 10, y = 10},
  72. alignment = { x = 0, y = 0 },
  73. })
  74. p.hud.icon = user:hud_add({
  75. hud_elem_type = "image",
  76. position = {x = 0.2, y = 0.2},
  77. offset = {x = 0, y = 0},
  78. text = "christmas_candy_cane.png",
  79. scale = { x = 10, y = 10},
  80. alignment = { x = 0, y = 0 },
  81. })
  82. p.hud.time = user:hud_add({
  83. hud_elem_type = "text",
  84. position = {x = 0.2, y = 0.2},
  85. offset = {x = 0, y = 0},
  86. text = "SUGAR RUSH!!\n"..christmas.to_time (p.time),
  87. number = 0xffffff,
  88. scale = { x = 10, y = 10},
  89. alignment = { x = 0, y = 0 },
  90. })
  91. end
  92. if p.time > 0 then
  93. p.time = p.time + 3
  94. end
  95. return minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  96. end
  97. end
  98. minetest.register_on_joinplayer(function(player)
  99. local name = player:get_player_name()
  100. christmas.players[name] = {}
  101. christmas.players[name].candy = 0
  102. christmas.players[name].hud = {}
  103. christmas.players[name].time = 0
  104. end)
  105. local t = 0
  106. minetest.register_globalstep (function(dtime)
  107. t = t + dtime
  108. if t > 1 then
  109. t = 0
  110. end
  111. for _, player in ipairs(minetest.get_connected_players()) do
  112. local p = xplayer(player)
  113. if p.time > 0 and t > 1-dtime then
  114. p.time = p.time - 1
  115. player:hud_change(p.hud.time, "text", "SUGAR RUSH!!\n~~"..christmas.to_time(p.time).."~~")
  116. elseif math.floor(p.time) == 1 then
  117. p.candy = 0
  118. end
  119. if p.time > 0 then
  120. player:set_physics_override({
  121. speed = 2.5,
  122. })
  123. end
  124. --minetest.chat_send_all(p.candy)
  125. if p.time == 0 then
  126. player:set_physics_override({
  127. speed = 1,
  128. })
  129. player:hud_remove(p.hud.ui)
  130. player:hud_remove(p.hud.icon)
  131. player:hud_remove(p.hud.time)
  132. end
  133. end
  134. end)