init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if debug then hangglider.debug = {} end -- hud id for debug data
  57. --hangglider.airbreak = {} -- true if falling fast when equip
  58. --[[
  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 = true
  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.can_fly = function (pname, pos)
  135. -- Checks if the player will get shot down at the position
  136. if wardzones then
  137. local zone = wardzones.getZone(pos)
  138. if zone then
  139. return (minetest.check_player_privs(pname, {protection_bypass=true}) or wardzones.checkPlayerZoneAccess(pname, zone) or not zone["data"]["no_fly"])
  140. end
  141. end
  142. if areas and minetest.is_protected(vector.round(pos), pname) then
  143. if hangglider.flak then
  144. for id, area in pairs(areas:getAreasAtPos(pos)) do
  145. if area.flak then
  146. return false
  147. end
  148. end
  149. end
  150. end
  151. return true
  152. end
  153. hangglider.shot_sound = function (pos)
  154. minetest.sound_play("hangglider_flak_shot", {
  155. pos = pos,
  156. max_hear_distance = 30,
  157. gain = 10.0,
  158. })
  159. end
  160. local physics_attrs = {"jump", "speed", "gravity"}
  161. local function apply_physics_override(player, overrides)
  162. if player_monoids then
  163. for _, attr in pairs(physics_attrs) do
  164. if overrides[attr] then
  165. player_monoids[attr]:add_change(player, overrides[attr], "hangglider:glider")
  166. end
  167. end
  168. else
  169. player:set_physics_override(overrides)
  170. end
  171. end
  172. local function remove_physics_override(player, overrides)
  173. for _, attr in pairs(physics_attrs) do
  174. if overrides[attr] then
  175. if core.global_exists("player_monoids") then
  176. player_monoids[attr]:del_change(player, "hangglider:glider")
  177. else
  178. player:set_physics_override({[attr] = 1})
  179. end
  180. end
  181. end
  182. end
  183. local step_v
  184. minetest.register_entity("hangglider:glider", {
  185. visual = "mesh",
  186. visual_size = {x = 12, y = 12},
  187. collisionbox = {0,0,0,0,0,0},
  188. mesh = "glider.obj",
  189. immortal = true,
  190. static_save = false,
  191. textures = {"wool_white.png","default_wood.png"},
  192. on_step = function(self, dtime)
  193. local canExist = false
  194. if self.object:get_attach() then
  195. local player = self.object:get_attach("parent")
  196. if player then
  197. local pos = player:getpos()
  198. local pname = player:get_player_name()
  199. if hangglider.use[pname] then
  200. local mrn_name = minetest.registered_nodes[minetest.get_node(vector.new(pos.x, pos.y-0.5, pos.z)).name]
  201. if mrn_name then
  202. if not (mrn_name.walkable or mrn_name.liquidtype ~= "none") then
  203. canExist = true
  204. if not minetestd then
  205. step_v = player:get_player_velocity().y
  206. if step_v < 0 and step_v > -3 then
  207. apply_physics_override(player, {speed=math.abs(step_v/2) + 0.75})
  208. elseif step_v <= -3 then --Cap our gliding movement speed.
  209. apply_physics_override(player, {speed=2.25})
  210. else
  211. remove_physics_override(player, {speed=1})
  212. end
  213. if debug then player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
  214. apply_physics_override(player, {gravity=((step_v + 3)/20)})
  215. end
  216. --[[local vel = player:get_player_velocity()
  217. if debug then player:hud_change(hangglider.debug[pname].id, "text", vel.y..', '..grav..', '..tostring(hangglider.airbreak[pname])) end
  218. player:set_physics_override({gravity = (vel.y + 2.0)/20})
  219. ]]end
  220. end
  221. end
  222. if not hangglider.can_fly(pname,pos) then
  223. if not self.warned then -- warning shot
  224. self.warned = 0
  225. hangglider.shot_sound(pos)
  226. minetest.chat_send_player(pname, "Protected area! You will be shot down in two seconds by anti-aircraft guns!")
  227. end
  228. self.warned = self.warned + dtime
  229. if self.warned > 2 then -- shoot down
  230. player:set_hp(1)
  231. player:get_inventory():remove_item("main", ItemStack("hangglider:hangglider"))
  232. hangglider.shot_sound(pos)
  233. canExist = false
  234. end
  235. end
  236. if not canExist then
  237. if not minetestd then
  238. remove_physics_override(player, {
  239. gravity=1,
  240. jump = 1,
  241. speed = 1,})
  242. end
  243. hangglider.use[pname] = false
  244. if HUD_Overlay then
  245. player:hud_change(hangglider.id[pname], "text", "blank.png")
  246. end
  247. --hangglider.airbreak[pname] = false
  248. end
  249. end
  250. end
  251. if not canExist then
  252. self.object:set_detach()
  253. self.object:remove()
  254. end
  255. end
  256. })
  257. minetest.register_on_dieplayer(function(player)
  258. remove_physics_override(player, {
  259. gravity = 1,
  260. jump = 1,
  261. })
  262. hangglider.use[player:get_player_name()] = false
  263. end)
  264. minetest.register_on_joinplayer(function(player)
  265. local pname = player:get_player_name()
  266. remove_physics_override(player, {
  267. gravity = 1,
  268. jump = 1,
  269. })
  270. hangglider.use[pname] = false
  271. if HUD_Overlay then
  272. hangglider.id[pname] = player:hud_add({
  273. hud_elem_type = "image",
  274. text = "blank.png",
  275. position = {x=0, y=0},
  276. scale = {x=-100, y=-100},
  277. alignment = {x=1, y=1},
  278. offset = {x=0, y=0}
  279. }) end
  280. if debug then
  281. hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text",
  282. position = {x=0.5, y=0.1},
  283. text = "-",
  284. number = 0xFF0000}), -- red text
  285. -- ht = {50,50,50},
  286. }
  287. end
  288. --hangglider.airbreak[pname] = false
  289. end)
  290. minetest.register_on_leaveplayer(function(player)
  291. local pname = player:get_player_name()
  292. hangglider.use[pname] = nil
  293. if HUD_Overlay then hangglider.id[pname] = nil end
  294. if debug then hangglider.debug[pname] = nil end
  295. --hangglider.airbreak[pname] = nil
  296. end)
  297. minetest.register_tool("hangglider:hangglider", {
  298. description = "Glider",
  299. inventory_image = "glider_item.png",
  300. stack_max=1,
  301. on_use = function(itemstack, player, pointed_thing)
  302. if not player then
  303. return
  304. end
  305. local pos = player:get_pos()
  306. local pname = player:get_player_name()
  307. if not hangglider.use[pname] then --Equip
  308. minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0})
  309. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
  310. local airbreak = false
  311. local vel = player:get_player_velocity().y
  312. --[[if vel < -1.5 then -- engage mid-air, falling fast, so stop but ramp velocity more quickly
  313. --hangglider.airbreak[pname] = true
  314. airbreak = true
  315. local stopper = minetest.add_entity(pos, "hangglider:airstopper")
  316. minetest.after(0, function(stopper, player) --"Extreme Measures"
  317. stopper:set_pos(player:get_pos())
  318. stopper:get_luaentity().attach = player
  319. player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  320. end, stopper, player)
  321. end]]
  322. if not airbreak then
  323. if moveModelUp then
  324. minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  325. else
  326. minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
  327. end
  328. end
  329. hangglider.use[pname] = true
  330. apply_physics_override(player, {jump = 0})
  331. -- if minetest 0.4.x use this:
  332. -- if minetest 5.x use this:
  333. -- minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
  334. itemstack:set_wear(itemstack:get_wear() + 255)
  335. return itemstack
  336. elseif hangglider.use[pname] then --Unequip
  337. if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end
  338. hangglider.use[pname] = false
  339. end
  340. end,
  341. sound = {breaks = "default_tool_breaks"},
  342. })
  343. minetest.register_craft({
  344. type = "shapeless",
  345. output = "hangglider:hangglider",
  346. recipe = {"default:paper", "default:paper", "default:paper", "default:paper", "hangglider:hangglider", "default:paper", "default:paper", "default:paper", "default:paper"},
  347. })
  348. minetest.register_craft({
  349. output = "hangglider:hangglider",
  350. recipe = {
  351. {"wool:white", "wool:white", "wool:white"},
  352. {"default:stick", "", "default:stick"},
  353. {"", "default:stick", ""},
  354. }
  355. })