From 6b985286b8f7fbcfe9c6f46e6bfada1a76d9447c Mon Sep 17 00:00:00 2001 From: Alexsandro Percy Date: Sun, 2 Jul 2023 15:16:07 -0300 Subject: [PATCH] new flap code --- lib_planes/control.lua | 10 +++- lib_planes/entities.lua | 26 ++++++++- lib_planes/utilities.lua | 118 +++++++++++++++++++++++++++++++++++---- 3 files changed, 139 insertions(+), 15 deletions(-) diff --git a/lib_planes/control.lua b/lib_planes/control.lua index f8f797c..3e71228 100755 --- a/lib_planes/control.lua +++ b/lib_planes/control.lua @@ -43,10 +43,16 @@ function airutils.control(self, dtime, hull_direction, longit_speed, longit_drag if player then ctrl = player:get_player_control() - --engine and power control if ctrl.aux1 and self._last_time_command > 0.5 then self._last_time_command = 0 - + + end + ---------------------------------- + -- flap operation + ---------------------------------- + if ctrl.aux1 and ctrl.sneak and self._last_time_command >= 0.3 and self._wing_angle_extra_flaps then + self._last_time_command = 0 + airutils.flap_operate(self, player) end self._acceleration = 0 diff --git a/lib_planes/entities.lua b/lib_planes/entities.lua index 29ebef6..657c64a 100644 --- a/lib_planes/entities.lua +++ b/lib_planes/entities.lua @@ -17,6 +17,8 @@ function airutils.get_staticdata(self) -- unloaded/unloads ... is now saved stored_last_accell = self._last_accell, stored_engine_running = self._engine_running, stored_inv_id = self._inv_id, + stored_flap = self._flap, + stored_passengers = self._passengers, stored_vehicle_custom_data = self._vehicle_custom_data }) end @@ -27,6 +29,7 @@ end function airutils.on_activate(self, staticdata, dtime_s) airutils.actfunc(self, staticdata, dtime_s) + if staticdata ~= "" and staticdata ~= nil then local data = minetest.deserialize(staticdata) or {} self._energy = data.stored_energy @@ -39,6 +42,8 @@ function airutils.on_activate(self, staticdata, dtime_s) self._last_accell = data.stored_last_accell self._engine_running = data.stored_engine_running self._inv_id = data.stored_inv_id + self._flap = data.stored_flap + self._passengers = data.stored_passengers or {} local custom_data = data.stored_vehicle_custom_data if custom_data then self._vehicle_custom_data = custom_data @@ -51,7 +56,7 @@ function airutils.on_activate(self, staticdata, dtime_s) self._last_applied_power = -1 --signal to start end end - + if self._register_parts_method then self._register_parts_method(self) end @@ -352,9 +357,11 @@ function airutils.logic(self) accel = airutils.autopilot(self, self.dtime, hull_direction, longit_speed, accel, curr_pos) end end - --end accell + --get disconnected players + airutils.rescueConnectionFailedPassengers(self) + if accel == nil then accel = {x=0,y=0,z=0} end --lift calculation @@ -485,6 +492,21 @@ function airutils.logic(self) self.object:set_rotation({x=newpitch,y=newyaw,z=newroll}) --end + if self._wing_configuration == self._wing_angle_of_attack and self._flap then + airutils.flap_on(self) + end + if self._wing_configuration ~= self._wing_angle_of_attack and self._flap == false then + airutils.flap_off(self) + end + + if longit_speed > self._max_speed and self._flap == true then + if is_attached and self.driver_name then + minetest.chat_send_player(self.driver_name, core.colorize('#ff0000', " >>> Flaps retracted due for overspeed")) + end + self._flap = false + end + + --adjust elevator pitch (3d model) self.object:set_bone_position("elevator", self._elevator_pos, {x=-self._elevator_angle*2 - 90, y=0, z=0}) --adjust rudder diff --git a/lib_planes/utilities.lua b/lib_planes/utilities.lua index d893b8a..27e3f56 100644 --- a/lib_planes/utilities.lua +++ b/lib_planes/utilities.lua @@ -683,19 +683,21 @@ function airutils.seats_create(self) if self.object then local pos = self.object:get_pos() self._passengers_base = {} - local max_seats = table.getn(self._seats) - for i=1, max_seats do - self._passengers_base[i] = minetest.add_entity(pos,'airutils:seat_base') - if not self._seats_rot then - self._passengers_base[i]:set_attach(self.object,'',self._seats[i],{x=0,y=0,z=0}) - else - self._passengers_base[i]:set_attach(self.object,'',self._seats[i],{x=0,y=self._seats_rot[i],z=0}) + if self._seats then + local max_seats = table.getn(self._seats) + for i=1, max_seats do + self._passengers_base[i] = minetest.add_entity(pos,'airutils:seat_base') + if not self._seats_rot then + self._passengers_base[i]:set_attach(self.object,'',self._seats[i],{x=0,y=0,z=0}) + else + self._passengers_base[i]:set_attach(self.object,'',self._seats[i],{x=0,y=self._seats_rot[i],z=0}) + end end - end - self.pilot_seat_base = self._passengers_base[1] --sets pilot seat reference - if self._have_copilot and self.pilot_seat_base[2] then - self.co_pilot_seat_base = self.pilot_seat_base[2] --sets copilot seat reference + self.pilot_seat_base = self._passengers_base[1] --sets pilot seat reference + if self._have_copilot and self.pilot_seat_base[2] then + self.co_pilot_seat_base = self.pilot_seat_base[2] --sets copilot seat reference + end end end end @@ -706,3 +708,97 @@ function airutils.seats_destroy(self) if self._passengers_base[i] then self._passengers_base[i]:remove() end end end + +function airutils.flap_on(self) + if self._wing_angle_extra_flaps == nil then self._wing_angle_extra_flaps = 0 end --if not, just keep the same as normal angle of attack + local flap_limit = 15 + if self._flap_limit then flap_limit = self._flap_limit end + self._wing_configuration = self._wing_angle_of_attack + self._wing_angle_extra_flaps + self.object:set_bone_position("flap.l", {x=0, y=0, z=0}, {x=-flap_limit, y=0, z=0}) + self.object:set_bone_position("flap.r", {x=0, y=0, z=0}, {x=-flap_limit, y=0, z=0}) +end + +function airutils.flap_off(self) + self._wing_configuration = self._wing_angle_of_attack + self.object:set_bone_position("flap.l", {x=0, y=0, z=0}, {x=0, y=0, z=0}) + self.object:set_bone_position("flap.r", {x=0, y=0, z=0}, {x=0, y=0, z=0}) +end + +function airutils.flap_operate(self, player) + if self._flap == false then + minetest.chat_send_player(player:get_player_name(), ">>> Flap down") + self._flap = true + airutils.flap_on(self) + minetest.sound_play("airutils_collision", { + object = self.object, + max_hear_distance = 15, + gain = 1.0, + fade = 0.0, + pitch = 0.5, + }, true) + else + minetest.chat_send_player(player:get_player_name(), ">>> Flap up") + self._flap = false + airutils.flap_off(self) + minetest.sound_play("airutils_collision", { + object = self.object, + max_hear_distance = 15, + gain = 1.0, + fade = 0.0, + pitch = 0.7, + }, true) + end +end + +local function do_attach(self, player, slot) + if slot == 0 then return end + if self._passengers[slot] == nil then + local name = player:get_player_name() + --minetest.chat_send_all(self.driver_name) + self._passengers[slot] = name + player:set_attach(self._passengers_base[slot], "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}) + player_api.player_attached[name] = true + + local eye_y = -4 + if airutils.detect_player_api(player) == 1 then + eye_y = 2.5 + end + player:set_eye_offset({x = 0, y = eye_y, z = 2}, {x = 0, y = 3, z = -30}) + + player_api.set_animation(player, "sit") + -- make the driver sit + minetest.after(1, function() + player = minetest.get_player_by_name(name) + if player then + airutils.sit(player) + --apply_physics_override(player, {speed=0,gravity=0,jump=0}) + end + end) + end +end + +--this method checks for a disconected player who comes back +function airutils.rescueConnectionFailedPassengers(self) + if self._disconnection_check_time == nil then self._disconnection_check_time = 1 end + self._disconnection_check_time = self._disconnection_check_time + self.dtime + local max_seats = table.getn(self._passengers_base) + if self._disconnection_check_time > 1 then + --minetest.chat_send_all(dump(self._passengers)) + self._disconnection_check_time = 0 + for i = max_seats,1,-1 + do + if self._passengers[i] then + local player = minetest.get_player_by_name(self._passengers[i]) + if player then --we have a player! + if player_api.player_attached[self._passengers[i]] == nil then --but isn't attached? + --minetest.chat_send_all("okay") + if player:get_hp() > 0 then + self._passengers[i] = nil --clear the slot first + do_attach(self, player, i) --attach + end + end + end + end + end + end +end