init.lua 14 KB

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