init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. local moveModelUp = false
  48. if tonumber(string.sub(minetest.get_version().string, 1, 1)) and tonumber(string.sub(minetest.get_version().string, 1, 1)) > 4 then
  49. moveModelUp = true
  50. end
  51. hangglider = {} --Make this global, so other mods can tell if hangglider exists.
  52. hangglider.use = {}
  53. if HUD_Overlay then
  54. hangglider.id = {} -- hud id for displaying overlay with struts
  55. end
  56. hangglider.players = {}
  57. if debug then hangglider.debug = {} end -- hud id for debug data
  58. --hangglider.airbreak = {} -- true if falling fast when equip
  59. --[[minetest.register_entity("hangglider:airstopper", { --A one-instant entity that catches the player and stops them.
  60. is_visible = false,
  61. physical = false,
  62. immortal = true,
  63. attach = nil,
  64. on_step = function(self, _)
  65. local canExist = false
  66. if self.attach then
  67. local player = self.attach
  68. if player:is_player() then
  69. local pname = player:get_player_name()
  70. canExist = true
  71. if player:get_player_velocity().y < 0.5 and player:get_player_velocity().y > -0.5 then
  72. --Let go when the player actually stops, as that's the whole point.
  73. if hangglider.use[pname] then
  74. if moveModelUp then
  75. minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  76. else
  77. minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  78. end
  79. end
  80. canExist = false
  81. end
  82. end
  83. if not canExist then
  84. player:set_detach()
  85. end
  86. end
  87. if not canExist then
  88. self.object:remove()
  89. end
  90. end
  91. })
  92. if core.global_exists("areas") then
  93. hangglider.flak = false
  94. -- chat command definition essentially copied from areas mod.
  95. minetest.register_chatcommand("area_flak",{
  96. params = "<ID>",
  97. description = "Toggle airspace restrictions for area <ID>",
  98. func = function(name, param)
  99. local id = tonumber(param)
  100. if not id then
  101. return false, "Invalid usage, see /help area_flak."
  102. end
  103. if not areas:isAreaOwner(id, name) then
  104. return false, "Area "..id.." does not exist"
  105. .." or is not owned by you."
  106. end
  107. local open = not areas.areas[id].flak
  108. -- Save false as nil to avoid inflating the DB.
  109. areas.areas[id].flak = open or nil
  110. areas:save()
  111. return true, ("Area's airspace %s."):format(open and "closed" or "opened")
  112. end
  113. })
  114. end
  115. if core.global_exists("minetestd") and minetestd.services.physicsctl.enabled then
  116. minetestd.physicsctl.register_physics_effect("hangglider",
  117. function(player) -- check
  118. return hangglider.use[player:get_player_name()]
  119. end,
  120. function(phys, player) -- blend
  121. local vel_y = player:get_player_velocity().y
  122. if debug then player:hud_change(hangglider.debug[pname].id, "text", vel_y..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
  123. phys.gravity = phys.gravity*((vel_y + 3)/20)
  124. if vel_y < 0 and vel_y > -3 then
  125. phys.speed = (math.abs(vel_y/2) + 0.75)
  126. elseif vel_y <= -3 then --Cap our gliding movement speed.
  127. phys.speed = 2.25
  128. end
  129. phys.jump = 0
  130. end,
  131. 7 -- effect order
  132. )
  133. end
  134. hangglider.shot_sound = function (pos)
  135. minetest.sound_play("hangglider_flak_shot", {
  136. pos = pos,
  137. max_hear_distance = 30,
  138. gain = 10.0,
  139. })
  140. end
  141. ]]
  142. local physics_attrs = {"jump", "speed", "gravity"}
  143. local function apply_physics_override(player, overrides)
  144. if player_monoids then
  145. for _, attr in pairs(physics_attrs) do
  146. if overrides[attr] then
  147. player_monoids[attr]:add_change(player, overrides[attr], "hangglider:glider")
  148. end
  149. end
  150. else
  151. player:set_physics_override(overrides)
  152. end
  153. end
  154. local function remove_physics_override(player, overrides)
  155. for _, attr in pairs(physics_attrs) do
  156. if overrides[attr] then
  157. if core.global_exists("player_monoids") then
  158. player_monoids[attr]:del_change(player, "hangglider:glider")
  159. else
  160. player:set_physics_override({[attr] = 1})
  161. end
  162. end
  163. end
  164. end
  165. local step_v
  166. minetest.register_entity("hangglider:glider", {
  167. visual = "mesh",
  168. visual_size = {x = 12, y = 12},
  169. collisionbox = {0,0,0,0,0,0},
  170. mesh = "glider.obj",
  171. immortal = true,
  172. static_save = false,
  173. textures = {"wool.png","default_wood.png"},
  174. on_step = function(self, dtime)
  175. local canExist = false
  176. if self.object:get_attach() then
  177. local player = self.object:get_attach("parent")
  178. if player then
  179. local pos = player:getpos()
  180. local pname = player:get_player_name()
  181. if hangglider.use[pname] then
  182. local mrn_name = minetest.registered_nodes[minetest.get_node(vector.new(pos.x, pos.y-0.5, pos.z)).name]
  183. if mrn_name then
  184. if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then
  185. canExist = true
  186. if not minetestd then
  187. step_v = player:get_player_velocity().y
  188. if step_v < 0 and step_v > -3 then
  189. apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75})
  190. elseif step_v <= -3 then --Cap our gliding movement speed.
  191. apply_physics_override(player, {speed=2.25})
  192. else
  193. remove_physics_override(player, {speed=1})
  194. end
  195. if debug then player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
  196. apply_physics_override(player, {gravity=((step_v + 3)/20)})
  197. end
  198. --[[local vel = player:get_player_velocity()
  199. if debug then player:hud_change(hangglider.debug[pname].id, "text", vel.y..', '..grav..', '..tostring(hangglider.airbreak[pname])) end
  200. player:set_physics_override({gravity = (vel.y + 2.0)/20})
  201. ]]end
  202. end
  203. end
  204. if not canExist then
  205. if not minetestd then
  206. local player_physics = hangglider.players[pname]
  207. local player_gravity = player_physics.gravity
  208. local player_jump = player_physics.jump
  209. local player_speed = player_physics.speed
  210. if player_gravity < .9 then
  211. player:set_physics_override({gravity=1})
  212. end
  213. if player_jump < .8 then
  214. player:set_physics_override({jump=1})
  215. end
  216. if player_speed < .9 or player_speed > 2 then
  217. player:set_physics_override({speed=1})
  218. end
  219. if player_jump > .7 and player_gravity > .8 then
  220. player:set_physics_override(hangglider.players[pname])
  221. end
  222. end
  223. hangglider.use[pname] = false
  224. if HUD_Overlay then
  225. player:hud_change(hangglider.id[pname], "text", "blank.png")
  226. end
  227. --hangglider.airbreak[pname] = false
  228. end
  229. end
  230. end
  231. if not canExist then
  232. self.object:set_detach()
  233. self.object:remove()
  234. end
  235. end
  236. })
  237. minetest.register_on_dieplayer(function(player)
  238. remove_physics_override(player, {
  239. gravity = 1,
  240. jump = 1,
  241. })
  242. hangglider.use[player:get_player_name()] = false
  243. end)
  244. minetest.register_on_joinplayer(function(player)
  245. local pname = player:get_player_name()
  246. remove_physics_override(player, {
  247. gravity = 1,
  248. jump = 1,
  249. })
  250. hangglider.use[pname] = false
  251. if HUD_Overlay then
  252. hangglider.id[pname] = player:hud_add({
  253. hud_elem_type = "image",
  254. text = "blank.png",
  255. position = {x=0, y=0},
  256. scale = {x=-100, y=-100},
  257. alignment = {x=1, y=1},
  258. offset = {x=0, y=0}
  259. }) end
  260. if debug then
  261. hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text",
  262. position = {x=0.5, y=0.1},
  263. text = "-",
  264. number = 0xFF0000}), -- red text
  265. -- ht = {50,50,50},
  266. }
  267. end
  268. --hangglider.airbreak[pname] = false
  269. end)
  270. minetest.register_on_leaveplayer(function(player)
  271. local pname = player:get_player_name()
  272. hangglider.use[pname] = nil
  273. if HUD_Overlay then hangglider.id[pname] = nil end
  274. if debug then hangglider.debug[pname] = nil end
  275. --hangglider.airbreak[pname] = nil
  276. end)
  277. minetest.register_tool("hangglider:hangglider", {
  278. description = "Glider",
  279. inventory_image = "glider_item.png",
  280. stack_max=1,
  281. on_use = function(itemstack, player, pointed_thing)
  282. if not player then
  283. return
  284. end
  285. local pos = player:get_pos()
  286. local pname = player:get_player_name()
  287. hangglider.players[pname] = player:get_physics_override()
  288. if not hangglider.use[pname] then --Equip
  289. minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0})
  290. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
  291. local airbreak = false
  292. local vel = player:get_player_velocity().y
  293. --[[if vel < -1.5 then -- engage mid-air, falling fast, so stop but ramp velocity more quickly
  294. --hangglider.airbreak[pname] = true
  295. airbreak = true
  296. local stopper = minetest.add_entity(pos, "hangglider:airstopper")
  297. minetest.after(0, function(stopper, player) --"Extreme Measures"
  298. stopper:set_pos(player:get_pos())
  299. stopper:get_luaentity().attach = player
  300. player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  301. end, stopper, player)
  302. end]]
  303. if not airbreak then
  304. if moveModelUp then
  305. minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  306. else
  307. minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  308. end
  309. end
  310. hangglider.use[pname] = true
  311. apply_physics_override(player, {jump = 0})
  312. -- if minetest 0.4.x use this:
  313. -- if minetest 5.x use this:
  314. -- minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  315. itemstack:set_wear(itemstack:get_wear() + 255)
  316. return itemstack
  317. elseif hangglider.use[pname] then --Unequip
  318. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end
  319. hangglider.use[pname] = false
  320. end
  321. end,
  322. sound = {breaks = "default_tool_breaks"},
  323. })