]> git.lizzy.rs Git - hangglider.git/blob - init.lua
Removed airstopper until minetest devs are smart enough to implement serverside players
[hangglider.git] / init.lua
1 -- Hangglider mod for Minetest
2 -- Original code by Piezo_ (orderofthefourthwall@gmail.com)
3 -- 2018-11-14
4
5 -- Modifications by David G (kestral246@gmail.com)
6 -- 2018-11-24
7 -- For Minetest 5.x, glider's set_attach needs to be offset by 1 node
8 --     Switch to alternate commented line below with correct offset.
9 -- Additional tuning of parameters.
10 -- Commented out debug hud display code, prefixed with "--debug:".
11
12 -- 2018-11-22
13 -- Give visual indication that hangglider is equiped.
14 --     Display simple overlay with blurred struts when equiped.
15 --     Issue: don't know how to disable overlay in third person view.
16 -- Also Unequip hangglider when landing on water.
17 -- Attempt to linearize parabolic flight path.
18 --     Start gravity stronger, but gradually reduce it as descent velocity increases.
19 --     Don't use airstopper when equipped from the ground (descent velocity is low).
20 --     Slightly increase flight speed to 1.25.
21 -- Unequip/equip cycling mid-flight should not fly farther than continuous flight.
22 --     When equipping mid-air (descent velocity higher), use airstopper but increase descent slope afterwards.
23 --     Create airbreak flag so all equips mid-flight use faster descent.
24 --     Reset airbreak flag only when land (canExist goes false).
25 --     Issue: it wouldn't reset if land in water, use fly, and launch from air, before I added test for water,
26 --            not sure if there are other such cases.
27 -- Temporarily add hud debug display to show descent velocity, gravity override, and airbreak flag.
28 --     Still in process of tuning all the parameters.
29
30
31 -- Modifications by Piezo_
32 -- 2018-11-25
33 -- hud overlay and debug can be enabled/disabled
34 -- Added blender-rendered overlay for struts using the actual model.
35 -- Reduced airbreak penalty severity
36 -- gave glider limited durability.
37 -- Improved gravity adjustment function.
38 -- Changed airbreaking process 
39 -- Removed airbreak penalty, as any 'advantage' seems minimal after new adjustments
40
41
42 local HUD_Overlay = true --show glider struts as overlay on HUD
43 local debug = false --show debug info in top-center of hud
44 local moveModelUp = false
45 if tonumber(string.sub(minetest.get_version().string, 1, 1)) and tonumber(string.sub(minetest.get_version().string, 1, 1)) > 4 then
46         moveModelUp = true
47 end
48 hangglider = {} --Make this global, so other mods can tell if hangglider exists.
49 hangglider.use = {}
50 if HUD_Overlay then
51 hangglider.id = {}  -- hud id for displaying overlay with struts
52 end
53 if debug then  hangglider.debug = {} end -- hud id for debug data
54 --hangglider.airbreak = {}  -- true if falling fast when equip
55 --[[
56 minetest.register_entity("hangglider:airstopper", { --A one-instant entity that catches the player and stops them.
57         is_visible = false,
58         physical = false,
59         immortal = true,
60         attach = nil,
61         on_step = function(self, _)
62                 local canExist = false
63                 if self.attach then
64                         local player = self.attach
65                         if player:is_player() then
66                                 local pname = player:get_player_name()
67                                 canExist = true
68                                 if player:get_player_velocity().y < 0.5 and player:get_player_velocity().y > -0.5 then 
69                                         --Let go when the player actually stops, as that's the whole point.
70                                         if hangglider.use[pname] then
71                                                 if moveModelUp then
72                                                         minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
73                                                 else
74                                                         minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
75                                                 end
76                                         end
77                                         canExist = false
78                                 end
79                         end
80                         if not canExist then
81                                 player:set_detach()
82                         end
83                 end
84                 if not canExist then
85                         self.object:remove()
86                 end
87         end
88 })]]
89
90 if minetestd and minetestd.services.gravityctl.enabled then
91 minetestd.gravityctl.register_gravity_effect("hangglider", 
92         function(player) 
93                 return hangglider.use[player:get_player_name()] 
94         end,
95         function(gravity, player) 
96                 local vel = player:get_player_velocity()
97                 if debug then player:hud_change(hangglider.debug[pname].id, "text", vel.y..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
98                 return ((vel.y + 3)/20)
99         end,
100         7,
101         1000
102 )
103 end
104
105 local step_v
106 minetest.register_entity("hangglider:glider", {
107         visual = "mesh",
108         visual_size = {x = 12, y = 12},
109         mesh = "glider.obj",
110         immortal = true,
111         static_save = false,
112         textures = {"wool_white.png","default_wood.png"},
113         on_step = function(self, _)
114                 local canExist = false
115                 if self.object:get_attach() then
116                         local player = self.object:get_attach("parent")
117                         if player then
118                                 local pos = player:getpos()
119                                 local pname = player:get_player_name()
120                                 if hangglider.use[pname] then
121                                         local mrn_name = minetest.registered_nodes[minetest.get_node(vector.new(pos.x, pos.y-0.5, pos.z)).name]
122                                         if mrn_name then
123                                                 if not (mrn_name.walkable or (mrn_name.drowning and mrn_name.drowning == 1)) then
124                                                         canExist = true
125                                                         step_v = player:get_player_velocity().y
126                                                         if step_v < 0 and step_v > -3 then
127                                                                 player:set_physics_override({speed=math.abs(step_v/2) + 0.75})
128                                                         elseif step_v <= -3 then --Cap our fall speed.
129                                                                 player:set_physics_override({speed=2.25})
130                                                         else
131                                                                 player:set_physics_override({speed=1})
132                                                         end
133                                                         if not minetestd then
134                                                                 if debug then player:hud_change(hangglider.debug[pname].id, "text", step_v..', '..player:get_physics_override().gravity..', '..tostring(hangglider.airbreak[pname])) end
135                                                                 player:set_physics_override({gravity=((step_v + 3)/20)})
136                                                         end
137                                                         --[[local vel = player:get_player_velocity()
138                                                         if debug then player:hud_change(hangglider.debug[pname].id, "text", vel.y..', '..grav..', '..tostring(hangglider.airbreak[pname])) end
139                                                         
140                                                         player:set_physics_override({gravity = (vel.y + 2.0)/20})
141                                                 ]]end
142                                         end
143                                 end
144                                 if not canExist then
145                                         player:set_physics_override({
146                                                 jump = 1,
147                                                 speed = 1,
148                                         })
149                                         if not minetestd then
150                                                 player:set_physics_override({gravity=1})
151                                         end
152                                         hangglider.use[pname] = false
153                                         if HUD_Overlay then
154                                         player:hud_change(hangglider.id[pname], "text", "blank.png")
155                                         end
156                                         --hangglider.airbreak[pname] = false
157                                 end
158                         end
159                 end
160                 if not canExist then 
161                         self.object:set_detach()
162                         self.object:remove() 
163                 end
164         end
165 })
166
167 minetest.register_on_dieplayer(function(player)
168         player:set_physics_override({
169                 gravity = 1,
170                 jump = 1,
171         })
172         hangglider.use[player:get_player_name()] = false
173 end)
174
175
176 minetest.register_on_joinplayer(function(player)
177         local pname = player:get_player_name()
178         player:set_physics_override({
179                 gravity = 1,
180                 jump = 1,
181         })
182         hangglider.use[pname] = false
183         if HUD_Overlay then
184         hangglider.id[pname] = player:hud_add({
185                 hud_elem_type = "image",
186                 text = "blank.png",
187                 position = {x=0, y=0},
188                 scale = {x=-100, y=-100},
189                 alignment = {x=1, y=1},
190                 offset = {x=0, y=0}
191         }) end
192         if debug then 
193                 hangglider.debug[pname] = {id = player:hud_add({hud_elem_type = "text",
194                         position = {x=0.5, y=0.1},
195                         text = "-",
196                         number = 0xFF0000}),  -- red text
197                         -- ht = {50,50,50},
198                 }
199         end
200         --hangglider.airbreak[pname] = false
201 end)
202
203 minetest.register_on_leaveplayer(function(player)
204         local pname = player:get_player_name()
205         hangglider.use[pname] = nil
206         if HUD_Overlay then hangglider.id[pname] = nil end
207         if debug then hangglider.debug[pname] = nil end
208         --hangglider.airbreak[pname] = nil
209 end)
210
211 minetest.register_tool("hangglider:hangglider", {
212         description = "Glider",
213         inventory_image = "glider_item.png",
214         stack_max=1,
215         on_use = function(itemstack, player, pointed_thing)
216                 if not player then
217                         return
218                 end
219                 local pos = player:get_pos()
220                 local pname = player:get_player_name()
221                 if minetest.get_node(pos).name == "air" and not hangglider.use[pname] then --Equip
222                         minetest.sound_play("bedsheet", {pos=pos, max_hear_distance = 8, gain = 1.0})
223                         if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "glider_struts.png") end
224                         local airbreak = false
225                         local vel = player:get_player_velocity().y
226                         --[[if vel < -1.5 then  -- engage mid-air, falling fast, so stop but ramp velocity more quickly
227                                 --hangglider.airbreak[pname] = true
228                                 airbreak = true
229                                 local stopper = minetest.add_entity(pos, "hangglider:airstopper")
230                                 minetest.after(0, function(stopper, player) --"Extreme Measures"
231                                         stopper:set_pos(player:get_pos())
232                                         stopper:get_luaentity().attach = player
233                                         player:set_attach( stopper, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
234                                 end, stopper, player)
235                         end]]
236                         if not airbreak then 
237                                 if moveModelUp then
238                                         minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
239                                 else
240                                         minetest.add_entity(pos, "hangglider:glider"):set_attach(player, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})
241                                 end
242                         end
243                         hangglider.use[pname] = true
244                         player:set_physics_override({jump = 0})
245                         -- if minetest 0.4.x use this:
246                         
247                         -- if minetest 5.x use this:
248                         -- minetest.add_entity(player:get_pos(), "hangglider:glider"):set_attach(player, "", {x=0,y=10,z=0}, {x=0,y=0,z=0})
249                         itemstack:set_wear(itemstack:get_wear() + 255)
250                         return itemstack
251                 elseif hangglider.use[pname] then --Unequip
252                         if HUD_Overlay then player:hud_change(hangglider.id[pname], "text", "default_wood.png^[colorize:#0000:255") end
253                         hangglider.use[pname] = false
254                 end
255         end,
256         sound = {breaks = "default_tool_breaks"},
257 })
258
259 minetest.register_craft({
260         type = "shapeless",
261         output = "hangglider:hangglider",
262         recipe = {"default:paper", "default:paper", "default:paper", "default:paper", "hangglider:hangglider", "default:paper", "default:paper", "default:paper", "default:paper"},
263 })
264
265 minetest.register_craft({
266         output = "hangglider:hangglider",
267         recipe = {
268                 {"wool:white", "wool:white", "wool:white"},
269                 {"default:stick", "", "default:stick"},
270                 {"", "default:stick", ""},
271         }
272 })