forked from Desour/helicopter
-
Notifications
You must be signed in to change notification settings - Fork 6
/
heli_entities.lua
381 lines (332 loc) · 13.6 KB
/
heli_entities.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
--
-- entity
--
local S = helicopter.S
-- supported colors
local color_def = {
["#2b2b2b"] = "Black",
["#ffffff"] = "White",
["#9f9f9f"] = "Grey",
["#6d6d6d"] = "Dark Grey",
["#dc1818"] = "Red",
["#07b6bc"] = "Cyan",
["#8c5922"] = "Brown",
["#4ee34c"] = "Green",
["#567a42"] = "Dark Green",
["#ff62c6"] = "Pink",
["#ff8b0e"] = "Orange",
["#a437ff"] = "Violet",
["#ffe400"] = "Yellow",
["#ff0098"] = "Magenta",
}
minetest.register_entity("nss_helicopter:seat_base",{
initial_properties = {
physical = false,
collide_with_objects=false,
pointable=false,
visual = "mesh",
mesh = "seat_base.b3d",
textures = {"nss_helicopter_black.png",},
},
on_activate = function(self,std)
self.sdata = minetest.deserialize(std) or {}
if self.sdata.remove then self.object:remove() end
end,
get_staticdata=function(self)
self.sdata.remove=true
return minetest.serialize(self.sdata)
end,
})
minetest.register_entity("nss_helicopter:heli", {
initial_properties = {
physical = true,
collide_with_objects = true,
collisionbox = {-1,0,-1, 1,0.3,1},
selectionbox = {-1,0,-1, 1,0.3,1},
visual = "mesh",
mesh = "nss_helicopter_heli.b3d",
backface_culling = false,
textures = {"nss_helicopter_interior_black.png", "nss_helicopter_metal.png", "nss_helicopter_strips.png",
"nss_helicopter_painting.png", "nss_helicopter_black.png", "nss_helicopter_aluminum.png", "nss_helicopter_glass.png",
"nss_helicopter_interior.png", "nss_helicopter_panel.png", "nss_helicopter_colective.png", "nss_helicopter_painting.png",
"nss_helicopter_rotors.png", "nss_helicopter_interior_black.png",},
},
driver_name = nil,
shaded = true,
show_on_minimap = true,
sound_handle = nil,
tilting = vector.new(),
energy = 0.001,
owner = "",
static_save = true,
infotext = S("A nice helicopter"),
last_vel = vector.new(),
hp_max = 50,
color = "#0063b0",
_passenger = nil,
_by_mouse = true, --helicopter.turn_player_look,
get_staticdata = function(self) -- unloaded/unloads ... is now saved
return minetest.serialize({
stored_energy = self.energy,
stored_owner = self.owner,
stored_hp = self.hp_max,
stored_color = self.color,
stored_driver_name = self.driver_name,
})
end,
on_activate = function(self, staticdata, dtime_s)
if staticdata ~= "" and staticdata ~= nil then
local data = minetest.deserialize(staticdata) or {}
self.energy = data.stored_energy
self.owner = data.stored_owner
self.hp_max = data.stored_hp
self.color = data.stored_color
self.driver_name = data.stored_driver_name
--minetest.debug("loaded: ", self.energy)
local properties = self.object:get_properties()
properties.infotext = data.stored_owner .. S(" nice helicopter")
self.object:set_properties(properties)
end
helicopter.paint(self, self.color)
local pos = self.object:get_pos()
local pointer=minetest.add_entity(pos,'nss_helicopter:pointer')
local energy_indicator_angle = ((self.energy * 18) - 90) * -1
pointer:set_attach(self.object,'',{x=0,y=11.26,z=9.37},{x=0,y=0,z=energy_indicator_angle})
self.pointer = pointer
local pilot_seat_base=minetest.add_entity(pos,'nss_helicopter:seat_base')
pilot_seat_base:set_attach(self.object,'',{x=4.2,y=10,z=2},{x=0,y=0,z=0})
self.pilot_seat_base = pilot_seat_base
local passenger_seat_base=minetest.add_entity(pos,'nss_helicopter:seat_base')
passenger_seat_base:set_attach(self.object,'',{x=-4.2,y=10,z=2},{x=0,y=0,z=0})
self.passenger_seat_base = passenger_seat_base
-- set the animation once and later only change the speed
self.object:set_animation({x = 0, y = 11}, 0, 0, true)
self.object:set_armor_groups({immortal=1})
local vector_up = vector.new(0, 1, 0)
self.object:set_acceleration(vector.multiply(vector_up, -helicopter.gravity))
end,
on_step = function(self, dtime)
helicopter.helicopter_last_time_command = helicopter.helicopter_last_time_command + dtime
if helicopter.helicopter_last_time_command > 1 then helicopter.helicopter_last_time_command = 1 end
local touching_ground, liquid_below
local vel = self.object:get_velocity()
touching_ground, liquid_below = helicopter.check_node_below(self.object)
vel = helicopter.heli_control(self, dtime, touching_ground, liquid_below, vel) or vel
helicopter.sound_and_animation_manager(self)
if vel.x == 0 and vel.y == 0 and vel.z == 0 then
return
end
if touching_ground == nil then
touching_ground, liquid_below = helicopter.check_node_below(self.object)
end
-- quadratic and constant deceleration
local speedsq = helicopter.vector_length_sq(vel)
local fq, fc
if touching_ground then
fq, fc = helicopter.friction_land_quadratic, helicopter.friction_land_constant
elseif liquid_below then
fq, fc = helicopter.friction_water_quadratic, helicopter.friction_water_constant
else
fq, fc = helicopter.friction_air_quadratic, helicopter.friction_air_constant
end
vel = vector.apply(vel, function(a)
local s = math.sign(a)
a = math.abs(a)
a = math.max(0, a - fq * dtime * speedsq - fc * dtime)
return a * s
end)
--[[
collision detection
using velocity vector as virtually a point on space, we compare
if last velocity has a great distance difference (virtually 5) from current velocity
using some trigonometry (get_hipotenuse_value). If yes, we have an abrupt collision
]]--
local is_attached = false
if self.owner then
local player = minetest.get_player_by_name(self.owner)
if player then
local player_attach = player:get_attach()
if player_attach then
--XXXXXXXX
if player_attach == self.pilot_seat_base then is_attached = true end
--XXXXXXXX
end
end
end
if is_attached then
local impact = helicopter.get_hipotenuse_value(vel, self.last_vel)
if impact > 5 then
--self.damage = self.damage + impact --sum the impact value directly to damage meter
local curr_pos = self.object:get_pos()
minetest.sound_play("nssh_collision", {
to_player = self.driver_name,
--pos = curr_pos,
--max_hear_distance = 5,
gain = 1.0,
fade = 0.0,
pitch = 1.0,
})
--[[if self.damage > 100 then --if acumulated damage is greater than 100, adieu
helicopter.destroy(self, player)
end]]--
end
--update hud
local player = minetest.get_player_by_name(self.driver_name)
if helicopter.helicopter_last_time_command > 0.3 then
helicopter.helicopter_last_time_command = 0
update_heli_hud(player)
end
else
-- for some error the player can be detached from the helicopter, so lets set him attached again
local can_stop = true
if self.owner and self.driver_name and touching_ground == false then
-- attach the driver again
local player = minetest.get_player_by_name(self.owner)
if player then
helicopter.attach(self, player)
can_stop = false
end
end
if can_stop then
--detach player
if self.sound_handle ~= nil then
helicopter.sound_and_animation_manager(self)
--why its here? cause if the sound is attached, player must so
local player_owner = minetest.get_player_by_name(self.owner)
if player_owner then remove_heli_hud(player_owner) end
end
end
end
self.last_vel = vel --saves velocity for collision comparation
-- end collision detection
self.object:set_velocity(vel)
end,
on_punch = function(self, puncher, ttime, toolcaps, dir, damage)
if not puncher or not puncher:is_player() then
return
end
local name = puncher:get_player_name()
if self.owner and self.owner ~= name and self.owner ~= "" then return end
if self.owner == nil then
self.owner = name
end
if self.driver_name and self.driver_name ~= name then
-- do not allow other players to remove the object while there is a driver
return
end
local touching_ground, liquid_below = helicopter.check_node_below(self.object)
--XXXXXXXX
local is_attached = false
if puncher:get_attach() == self.pilot_seat_base then is_attached = true end
--XXXXXXXX
local itmstck=puncher:get_wielded_item()
local item_name = ""
if itmstck then item_name = itmstck:get_name() end
if is_attached == false then
if touching_ground then
if helicopter.loadFuel(self, self.owner) then return end
end
-- deal with painting or destroying
if itmstck then
local _,indx = item_name:find('dye:')
if indx then
--lets paint!!!!
local color = item_name:sub(indx+1)
local colstr = helicopter.colors[color]
--minetest.chat_send_all(color ..' '.. dump(colstr))
if colstr then
helicopter.paint(self, colstr)
itmstck:set_count(itmstck:get_count()-1)
puncher:set_wielded_item(itmstck)
end
-- end painting
else -- deal damage
if not self.driver and toolcaps and toolcaps.damage_groups and toolcaps.damage_groups.fleshy then
--mobkit.hurt(self,toolcaps.damage_groups.fleshy - 1)
--mobkit.make_sound(self,'hit')
self.hp_max = self.hp_max - 10
minetest.sound_play("nssh_collision", {
object = self.object,
max_hear_distance = 5,
gain = 1.0,
fade = 0.0,
pitch = 1.0,
})
end
end
end
if self.hp_max <= 0 then
if helicopter.punch_inv then
local pinv = puncher:get_inventory()
local stack = ItemStack("nss_helicopter:heli")
local imeta = stack:get_meta()
-- store fuel level & color
imeta:set_float("fuel", self.energy)
if self.color and self.color ~= "#0063b0" then -- don't store defult "blue"
imeta:set_string("color", self.color)
end
local color_name = color_def[self.color:lower()]
if color_name then
imeta:set_string("description", color_name .. S(" Helicopter"))
end
if not pinv:room_for_item("main", stack) then
minetest.chat_send_player(puncher:get_player_name(),
S("You do not have room in your inventory"))
else
if self.pointer then self.pointer:remove() end
if self.pilot_seat_base then self.pilot_seat_base:remove() end
if self.passenger_seat_base then self.passenger_seat_base:remove() end
self.object:remove()
if not helicopter.creative or not pinv:contains_item("main", stack, true) then
pinv:add_item("main", stack)
end
end
else
helicopter.destroy(self, puncher)
end
end
end
end,
on_rightclick = function(self, clicker)
if not clicker or not clicker:is_player() then
return
end
local name = clicker:get_player_name()
if self.owner == "" then
self.owner = name
end
if self.owner == name or minetest.check_player_privs(clicker, {protection_bypass=true}) then
if name == self.driver_name then
-- driver clicked the object => driver gets off the vehicle
helicopter.dettach(self, clicker)
if self._passenger then
local passenger = minetest.get_player_by_name(self._passenger)
if passenger then
helicopter.dettach_pax(self, passenger)
end
end
elseif not self.driver_name then
local is_under_water = helicopter.check_is_under_water(self.object)
if is_under_water then return end
-- temporary------
self.hp_max = 50 -- why? cause I can desist from destroy
------------------
helicopter.attach(self, clicker)
end
else
--passenger section
--only can enter when the pilot is inside
if self.driver_name then
if self._passenger == nil then
helicopter.attach_pax(self, clicker)
else
helicopter.dettach_pax(self, clicker)
end
else
if self._passenger then
helicopter.dettach_pax(self, clicker)
end
end
end
end,
})