init.lua 13 KB

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