init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. --localize functions for better performance
  2. local string_byte = string.byte
  3. local string_sub = string.sub
  4. local get_item_group = minetest.get_item_group
  5. local add_particlespawner = minetest.add_particlespawner
  6. local add_item = minetest.add_item
  7. local get_node = minetest.get_node
  8. local add_entity = minetest.add_entity
  9. local modpath = minetest.get_modpath("hot_air_balloons")
  10. local set_rescue, mark_for_deletion_if_piloted = dofile(modpath .. "/absent_ballooner_rescuing.lua")
  11. local handle_movement = dofile(modpath .. "/movement.lua")
  12. local is_in_creative = function(name)
  13. return creative and creative.is_enabled_for
  14. and creative.is_enabled_for(name)
  15. end
  16. local get_fire_particle = function (pos)
  17. pos.y = pos.y + 3
  18. return {
  19. amount = 3,
  20. time = 1,
  21. minpos = pos,
  22. maxpos = pos,
  23. minvel = {x = 0, y = 1, z = 0},
  24. maxvel = {x = 0, y = 1, z = 0},
  25. minexptime = 1,
  26. maxexptime = 1,
  27. minsize = 10,
  28. maxsize = 5,
  29. collisiondetection = false,
  30. vertical = false,
  31. texture = "hot_air_balloons_flame.png"
  32. }
  33. end
  34. local add_heat = function(self, player)
  35. local item_stack = player:get_wielded_item()
  36. local item_name = item_stack:get_name()
  37. local group_coal = get_item_group(item_name, "coal")
  38. if group_coal == 0
  39. then
  40. return false
  41. end
  42. local heat = self.heat
  43. heat = heat + 1200 * group_coal --1 min until heat is back to original
  44. if heat < 12000 --cap heat at 12000 (10 min)
  45. then
  46. self.heat = heat
  47. --adding particle effect
  48. local pos = self.object:get_pos()
  49. add_particlespawner(get_fire_particle(pos))
  50. if not is_in_creative(player:get_player_name())
  51. then
  52. item_stack:take_item()
  53. player:set_wielded_item(item_stack)
  54. end
  55. end
  56. return true
  57. end
  58. --global table, has fields get_entity_def and get_item_def
  59. --custom balloons right now turn into normal ones when the pilot leaves
  60. hot_air_balloons = {}
  61. hot_air_balloons.get_entity = function(name, mesh_name, texture_name)
  62. return
  63. name,
  64. {
  65. initial_properties =
  66. {
  67. hp_max = 1,
  68. physical = true,
  69. weight = 5,
  70. collisionbox = {-0.65, -0.01, -0.65, 0.65, 1.11, 0.65},
  71. visual = "mesh",
  72. mesh = "hot_air_balloons_balloon.obj",
  73. textures = {"hot_air_balloons_balloon_model.png"},
  74. is_visible = true,
  75. makes_footstep_sound = false,
  76. automatic_rotate = false,
  77. backface_culling = false,
  78. },
  79. heat = 0,
  80. balloon_type = name,
  81. on_step = function(self, dtime)
  82. --decrease heat, move
  83. if self.heat > 0
  84. then
  85. self.heat = self.heat - 1
  86. end
  87. handle_movement(self)
  88. end,
  89. on_rightclick = function (self, clicker)
  90. --if hoding coal, increase heat, else mount/dismount
  91. if not clicker or not clicker:is_player()
  92. then
  93. return
  94. end
  95. --checking if clicker is holding coal
  96. --heating balloon and returning if yes
  97. if add_heat(self, clicker)
  98. then
  99. return
  100. end
  101. --if not holding coal:
  102. local playername = clicker:get_player_name()
  103. if self.pilot and self.pilot == playername
  104. then
  105. self.pilot = nil
  106. clicker:set_detach()
  107. elseif not self.pilot
  108. then
  109. --attach
  110. self.pilot = playername
  111. clicker:set_attach(self.object, "",
  112. {x = 0, y = 1, z = 0}, {x = 0, y = 0, z = 0})
  113. end
  114. end,
  115. --if pilot leaves start sinking and prepare for next pilot
  116. on_detach_child = function(self, child)
  117. self.heat = 0
  118. self.object:setvelocity({x = 0, y = 0, z = 0})
  119. end,
  120. on_activate = function(self, staticdata, dtime_s)
  121. self.object:set_armor_groups({punch_operable = 1})
  122. --so balloons don't get lost
  123. self.object:setvelocity({x = 0, y = 0, z = 0})
  124. --checking if balloon was spawned from item or unloaded without pilot
  125. if staticdata == ""
  126. then
  127. return
  128. end
  129. --checking if balloon should despawn when pilot logged off
  130. local first_char = string_byte(staticdata)
  131. --ballooner logged off, balloon will respawn when ballooner logs back in
  132. if first_char == 82 --chr 82 = R
  133. then
  134. self.object:remove()
  135. return
  136. --absent ballooner logged back in
  137. elseif first_char == 80 --chr 80 = P
  138. then
  139. set_rescue(self, string_sub(staticdata, 2))
  140. end
  141. end,
  142. get_staticdata = mark_for_deletion_if_piloted,
  143. on_punch = function(self, puncher) --drop balloon item
  144. if self.pilot
  145. then
  146. return
  147. elseif not (puncher and puncher:is_player())
  148. then
  149. return
  150. else
  151. self.object:remove()
  152. local inv = puncher:get_inventory()
  153. if not is_in_creative(puncher:get_player_name())
  154. or not inv:contains_item("main", "hot_air_balloons:item")
  155. then
  156. local leftover = inv:add_item("main", "hot_air_balloons:item")
  157. if not leftover:is_empty()
  158. then
  159. add_item(self.object:get_pos(), leftover)
  160. end
  161. end
  162. end
  163. end,
  164. }
  165. end
  166. hot_air_balloons.get_item = function(name, description, texture, object_name)
  167. return
  168. name,
  169. {
  170. description = description,
  171. inventory_image = texture,
  172. stack_max = 1,
  173. liquids_pointable = true,
  174. groups = {flammable = 2},
  175. on_place =
  176. function (itemstack, placer, pointed_thing)
  177. --places balloon if the clicked thing is a node and the above node is air
  178. if pointed_thing.type == "node"
  179. and get_node (pointed_thing.above).name == "air"
  180. then
  181. if not is_in_creative(placer:get_player_name())
  182. then
  183. itemstack:take_item()
  184. end
  185. local pos_to_place = pointed_thing.above
  186. pos_to_place.y = pos_to_place.y - 0.5 --subtracting 0.5 to place on ground
  187. add_entity(pointed_thing.above, object_name)
  188. end
  189. --add remaining items to inventory
  190. return itemstack
  191. end
  192. }
  193. end
  194. --registering the balloon entity, item and recepies
  195. minetest.register_entity(hot_air_balloons.get_entity(
  196. "hot_air_balloons:balloon",
  197. "hot_air_balloons_balloon.obj",
  198. "hot_air_balloons_balloon_model.png"))
  199. minetest.register_craftitem(hot_air_balloons.get_item(
  200. "hot_air_balloons:item",
  201. minetest.translate("hot_air_balloons", "Hot Air Balloon"),
  202. "hot_air_balloons_balloon.png",
  203. "hot_air_balloons:balloon"))
  204. minetest.register_craft(
  205. {
  206. output = "hot_air_balloons:item",
  207. recipe = {
  208. {"default:paper", "default:paper", "default:paper"},
  209. {"default:paper", "bucket:bucket_lava", "default:paper"},
  210. {"", "group:wood", "" },
  211. },
  212. })
  213. minetest.register_craft(
  214. {
  215. type = "fuel",
  216. recipe = "hot_air_balloons:item",
  217. burntime = 20,
  218. })
  219. --[[
  220. minetest.register_craft(
  221. {
  222. type = "aircraft"
  223. }
  224. ]]