heli_utilities.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. helicopter.gravity = tonumber(minetest.settings:get("movement_gravity")) or 9.8
  2. helicopter.vector_up = vector.new(0, 1, 0)
  3. function helicopter.get_hipotenuse_value(point1, point2)
  4. return math.sqrt((point1.x - point2.x) ^ 2 + (point1.y - point2.y) ^ 2 + (point1.z - point2.z) ^ 2)
  5. end
  6. --painting
  7. function helicopter.paint(self, colstr)
  8. if colstr then
  9. self.color = colstr
  10. local l_textures = self.initial_properties.textures
  11. for _, texture in ipairs(l_textures) do
  12. local i,indx = texture:find('helicopter_painting.png')
  13. if indx then
  14. l_textures[_] = "helicopter_painting.png^[multiply:".. colstr
  15. end
  16. local i,indx = texture:find('helicopter_colective.png')
  17. if indx then
  18. l_textures[_] = "helicopter_colective.png^[multiply:".. colstr
  19. end
  20. end
  21. self.object:set_properties({textures=l_textures})
  22. end
  23. end
  24. function helicopter.setText(self)
  25. local properties = self.object:get_properties()
  26. local formatted = string.format(
  27. "%.2f", self.hp_max
  28. )
  29. if properties then
  30. properties.infotext = "Nice helicopter of " .. self.owner .. ". Current hp: " .. formatted
  31. self.object:set_properties(properties)
  32. end
  33. end
  34. -- attach player
  35. function helicopter.attach(self, player)
  36. local name = player:get_player_name()
  37. self.driver_name = name
  38. -- sound and animation
  39. self.sound_handle = minetest.sound_play({name = "helicopter_motor"},
  40. {object = self.object, gain = 2.0, max_hear_distance = 32, loop = true,})
  41. self.object:set_animation_frame_speed(60)
  42. -- attach the driver
  43. player:set_attach(self.pilot_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  44. player:set_eye_offset({x = 0, y = -4, z = 1}, {x = 0, y = 8, z = -30})
  45. player_api.player_attached[name] = true
  46. -- make the driver sit
  47. minetest.after(0.2, function()
  48. local player = minetest.get_player_by_name(name)
  49. if player then
  50. player_api.set_animation(player, "sit")
  51. update_heli_hud(player)
  52. end
  53. end)
  54. -- disable gravity
  55. self.object:set_acceleration(vector.new())
  56. end
  57. -- dettach player
  58. function helicopter.dettach(self, player)
  59. local name = self.driver_name
  60. helicopter.setText(self)
  61. -- driver clicked the object => driver gets off the vehicle
  62. self.driver_name = nil
  63. -- sound and animation
  64. if self.sound_handle then
  65. minetest.sound_stop(self.sound_handle)
  66. self.sound_handle = nil
  67. end
  68. self.object:set_animation_frame_speed(0)
  69. -- detach the player
  70. player:set_detach()
  71. player_api.player_attached[name] = nil
  72. player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
  73. player_api.set_animation(player, "stand")
  74. self.object:set_acceleration(vector.multiply(helicopter.vector_up, -helicopter.gravity))
  75. --remove hud
  76. if player then remove_heli_hud(player) end
  77. end
  78. -- attach passenger
  79. function helicopter.attach_pax(self, player)
  80. local name = player:get_player_name()
  81. self._passenger = name
  82. -- attach the passenger
  83. player:set_attach(self.passenger_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  84. player:set_eye_offset({x = 0, y = -4, z = 1}, {x = 0, y = 8, z = -5})
  85. player_api.player_attached[name] = true
  86. -- make the driver sit
  87. minetest.after(0.2, function()
  88. local player = minetest.get_player_by_name(name)
  89. if player then
  90. player_api.set_animation(player, "sit")
  91. end
  92. end)
  93. end
  94. -- dettach passenger
  95. function helicopter.dettach_pax(self, player)
  96. local name = self._passenger
  97. -- passenger clicked the object => driver gets off the vehicle
  98. self._passenger = nil
  99. -- detach the player
  100. player:set_detach()
  101. player_api.player_attached[name] = nil
  102. player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
  103. player_api.set_animation(player, "stand")
  104. end
  105. -- destroy the helicopter
  106. function helicopter.destroy(self, puncher)
  107. if self.sound_handle then
  108. minetest.sound_stop(self.sound_handle)
  109. self.sound_handle = nil
  110. end
  111. if self.driver_name then
  112. -- detach the driver first (puncher must be driver)
  113. puncher:set_detach()
  114. puncher:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  115. player_api.player_attached[name] = nil
  116. -- player should stand again
  117. player_api.set_animation(puncher, "stand")
  118. self.driver_name = nil
  119. end
  120. local pos = self.object:get_pos()
  121. if self.pointer then self.pointer:remove() end
  122. if self.pilot_seat_base then self.pilot_seat_base:remove() end
  123. if self.passenger_seat_base then self.passenger_seat_base:remove() end
  124. self.object:remove()
  125. pos.y=pos.y+2
  126. for i=1,2 do
  127. if math.random(0,1) == 1 then
  128. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'basic_materials:gear_steel')
  129. end
  130. end
  131. for i=1,4 do
  132. if math.random(0,1) == 1 then
  133. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'basic_materials:steel_bar')
  134. end
  135. end
  136. for i=1,12 do
  137. if math.random(0,1) == 1 then
  138. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:steel_ingot')
  139. end
  140. end
  141. for i=1,6 do
  142. if math.random(0,1) == 1 then
  143. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:copper_ingot')
  144. end
  145. end
  146. for i=1,4 do
  147. if math.random(0,1) == 1 then
  148. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'basic_materials:plastic_sheet')
  149. end
  150. end
  151. for i=1,5 do
  152. if math.random(0,1) == 1 then
  153. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:wood')
  154. end
  155. end
  156. for i=1,1 do
  157. if math.random(0,1) == 1 then
  158. minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:glass')
  159. end
  160. end
  161. end