init.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. -- Hangglider mod for Minetest
  2. -- Original code by Piezo_ (orderofthefourthwall@gmail.com)
  3. -- 2018-11-14
  4. -- Modifications by David G (kestral246@gmail.com)
  5. -- 2018-11-24
  6. -- For Minetest 5.x, glider's set_attach needs to be offset by 1 node
  7. -- Switch to alternate commented line below with correct offset.
  8. -- Additional tuning of parameters.
  9. -- Commented out debug hud display code, prefixed with "--debug:".
  10. -- 2018-11-22
  11. -- Give visual indication that hangglider is equiped.
  12. -- Display simple overlay with blurred struts when equiped.
  13. -- Issue: don't know how to disable overlay in third person view.
  14. -- Also Unequip hangglider when landing on water.
  15. -- Attempt to linearize parabolic flight path.
  16. -- Start gravity stronger, but gradually reduce it as descent velocity increases.
  17. -- Don't use airstopper when equipped from the ground (descent velocity is low).
  18. -- Slightly increase flight speed to 1.25.
  19. -- Unequip/equip cycling mid-flight should not fly farther than continuous flight.
  20. -- When equipping mid-air (descent velocity higher), use airstopper but increase descent slope afterwards.
  21. -- Create airbreak flag so all equips mid-flight use faster descent.
  22. -- Reset airbreak flag only when land (canExist goes false).
  23. -- Issue: it wouldn't reset if land in water, use fly, and launch from air, before I added test for water,
  24. -- not sure if there are other such cases.
  25. -- Temporarily add hud debug display to show descent velocity, gravity override, and airbreak flag.
  26. -- Still in process of tuning all the parameters.
  27. -- Modifications by Piezo_
  28. -- 2018-11-25
  29. -- hud overlay and debug can be enabled/disabled
  30. -- Added blender-rendered overlay for struts using the actual model.
  31. -- Reduced airbreak penalty severity
  32. -- gave glider limited durability.
  33. -- Improved gravity adjustment function.
  34. -- Changed airbreaking process
  35. -- Removed airbreak penalty, as any 'advantage' seems minimal after new adjustments
  36. -- Removed airbreak until minetest devs are smart enough to implement better serverside players.
  37. -- Simplified liquid check.
  38. -- Modifications by gpcf
  39. -- 2018-12-09
  40. -- get shot down while flying over protected areas marked as no-fly-zones (flak, from German Flugabwehrkanone)
  41. -- set these areas with the /area_flak command
  42. -- Modifications by SpaghettiToastBook
  43. -- 2018-12-29
  44. -- Physics overrides use player_monoids mod if available
  45. local HUD_Overlay = true --show glider struts as overlay on HUD
  46. local debug = false --show debug info in top-center of hud
  47. hangglider = {} --Make this global, so other mods can tell if hangglider exists.
  48. hangglider.use = {}
  49. hangglider.players = {}
  50. if HUD_Overlay then
  51. hangglider.id = {} -- hud id for displaying overlay with struts
  52. end
  53. if debug then
  54. hangglider.debug = {} -- hud id for debug data
  55. end
  56. local physics_attrs = {"jump", "speed", "gravity"}
  57. local function apply_physics_override(player, overrides)
  58. player:set_physics_override(overrides)
  59. end
  60. local function remove_physics_override(player, overrides)
  61. for _, attr in pairs(physics_attrs) do
  62. if overrides[attr] then
  63. player:set_physics_override({[attr] = 1})
  64. end
  65. end
  66. end
  67. local step_v
  68. minetest.register_entity("hangglider:glider", {
  69. visual = "mesh",
  70. visual_size = {x = 12, y = 12},
  71. collisionbox = {0,0,0,0,0,0},
  72. mesh = "glider.obj",
  73. immortal = true,
  74. static_save = false,
  75. textures = {"wool.png","default_wood.png"},
  76. on_step = function(self, dtime)
  77. local canExist = false
  78. if self.object:get_attach() then
  79. local player = self.object:get_attach("parent")
  80. if player then
  81. local pos = player:get_pos()
  82. local pname = player:get_player_name()
  83. if hangglider.use[pname] then
  84. local mrn_name = minetest.registered_nodes[minetest.get_node(vector.new(pos.x, pos.y-0.5, pos.z)).name]
  85. if mrn_name then
  86. if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then
  87. canExist = true
  88. step_v = player:get_velocity().y
  89. if step_v < 0 and step_v > -3 then
  90. apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75})
  91. elseif step_v <= -3 then --Cap our gliding movement speed.
  92. apply_physics_override(player, {speed=2.25})
  93. else
  94. remove_physics_override(player, {speed=1})
  95. end
  96. if debug then player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
  97. apply_physics_override(player, {gravity=((step_v + 3)/20)})
  98. end
  99. --[[local vel = player:get_velocity()
  100. if debug then player:hud_change(hangglider.debug[pname].id, "text", vel.y..', '..grav..', '..tostring(hangglider.airbreak[pname])) end
  101. player:set_physics_override({gravity = (vel.y + 2.0)/20})
  102. ]]
  103. end
  104. end
  105. if not canExist then --Landed, reset player physics and stuff.
  106. hangglider.use[pname] = false
  107. armor:set_player_armor(player)
  108. if HUD_Overlay then
  109. player:hud_change(hangglider.id[pname], "text", "blank.png")
  110. end
  111. --hangglider.airbreak[pname] = false
  112. end
  113. end
  114. end
  115. if not canExist then
  116. self.object:set_detach()
  117. self.object:remove()
  118. end
  119. end
  120. })
  121. minetest.register_on_dieplayer(function(player)
  122. remove_physics_override(player, {
  123. gravity = 1,
  124. jump = 1,
  125. })
  126. hangglider.use[player:get_player_name()] = false
  127. end)
  128. minetest.register_on_joinplayer(function(player)
  129. local pname = player:get_player_name()
  130. remove_physics_override(player, {
  131. gravity = 1,
  132. jump = 1,
  133. })
  134. hangglider.use[pname] = false
  135. if HUD_Overlay then
  136. hangglider.id[pname] = player:hud_add({
  137. hud_elem_type = "image",
  138. text = "blank.png",
  139. position = {x=0, y=0},
  140. scale = {x=-100, y=-100},
  141. alignment = {x=1, y=1},
  142. offset = {x=0, y=0}
  143. }) end
  144. if debug then
  145. hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text",
  146. position = {x=0.5, y=0.1},
  147. text = "-",
  148. number = 0xFF0000}), -- red text
  149. -- ht = {50,50,50},
  150. }
  151. end
  152. --hangglider.airbreak[pname] = false
  153. end)
  154. minetest.register_on_leaveplayer(function(player)
  155. local pname = player:get_player_name()
  156. hangglider.use[pname] = nil
  157. if HUD_Overlay then hangglider.id[pname] = nil end
  158. if debug then hangglider.debug[pname] = nil end
  159. --hangglider.airbreak[pname] = nil
  160. end)
  161. minetest.register_tool("hangglider:hangglider", {
  162. description = "Glider",
  163. inventory_image = "glider_item.png",
  164. stack_max=1,
  165. on_use = function(itemstack, player, pointed_thing)
  166. if not player then
  167. return
  168. end
  169. local pos = player:get_pos()
  170. local pname = player:get_player_name()
  171. hangglider.players[pname] = player:get_physics_override()
  172. if not hangglider.use[pname] then --Equip
  173. minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0})
  174. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
  175. local airbreak = false
  176. local vel = player:get_velocity().y
  177. --[[if vel < -1.5 then -- engage mid-air, falling fast, so stop but ramp velocity more quickly
  178. --hangglider.airbreak[pname] = true
  179. airbreak = true
  180. local stopper = minetest.add_entity(pos, "hangglider:airstopper")
  181. minetest.after(0, function(stopper, player) --"Extreme Measures"
  182. stopper:set_pos(player:get_pos())
  183. stopper:get_luaentity().attach = player
  184. player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  185. end, stopper, player)
  186. end]]
  187. if not airbreak then
  188. minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  189. end
  190. hangglider.use[pname] = true
  191. apply_physics_override(player, {jump = 0})
  192. itemstack:set_wear(itemstack:get_wear() + 255)
  193. return itemstack
  194. elseif hangglider.use[pname] then --Unequip
  195. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end
  196. hangglider.use[pname] = false
  197. end
  198. end,
  199. sound = {breaks = "default_tool_breaks"},
  200. })