diff --git a/init.lua b/init.lua index 87ce33a..145d915 100644 --- a/init.lua +++ b/init.lua @@ -37,6 +37,7 @@ dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "airutils_wind.lua") dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "inventory_management.lua") dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "light.lua") dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "physics_lib.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "init.lua") if player_api and not minetest.settings:get_bool('airutils.disable_uniforms') then dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "pilot_skin_manager.lua") @@ -269,7 +270,9 @@ function airutils.getLiftAccel(self, velocity, accel, longit_speed, roll, curr_p if self._wing_configuration then wing_config = self._wing_configuration end --flaps! local retval = accel - if longit_speed > 1 then + local min_speed = 1; + if self._min_speed then min_speed = self._min_speed end + if longit_speed > min_speed then local angle_of_attack = math.rad(self._angle_of_attack + wing_config) --local acc = 0.8 local daoa = deg(angle_of_attack) @@ -374,8 +377,11 @@ function airutils.set_paint(self, puncher, itmstck, texture_name) --minetest.chat_send_all(color ..' '.. dump(colstr)) if colstr then airutils.paint(self, colstr, texture_name) + if self._alternate_painting_texture and self._mask_painting_texture then + airutils.paint_with_mask(self, colstr, self._alternate_painting_texture, self._mask_painting_texture) + end itmstck:set_count(itmstck:get_count()-1) - puncher:set_wielded_item(itmstck) + if puncher ~= nil then puncher:set_wielded_item(itmstck) end return true end -- end painting @@ -386,6 +392,7 @@ end --painting function airutils.paint(self, colstr, texture_name) + if not self then return end if colstr then self._color = colstr local l_textures = self.initial_properties.textures @@ -399,6 +406,22 @@ function airutils.paint(self, colstr, texture_name) end end +function airutils.paint_with_mask(self, colstr, target_texture, mask_texture) + if colstr then + self._color = colstr + self._det_color = mask_colstr + local l_textures = self.initial_properties.textures + for _, texture in ipairs(l_textures) do + local indx = texture:find(target_texture) + if indx then + --"("..target_texture.."^[mask:"..mask_texture..")" + l_textures[_] = "("..target_texture.."^[multiply:".. colstr..")^("..target_texture.."^[mask:"..mask_texture..")" + end + end + self.object:set_properties({textures=l_textures}) + end +end + function airutils.getAngleFromPositions(origin, destiny) local angle_north = math.deg(math.atan2(destiny.x - origin.x, destiny.z - origin.z)) if angle_north < 0 then angle_north = angle_north + 360 end diff --git a/lib_planes/control.lua b/lib_planes/control.lua new file mode 100755 index 0000000..0e3f99a --- /dev/null +++ b/lib_planes/control.lua @@ -0,0 +1,252 @@ +--global constants +airutils.ideal_step = 0.02 + +--[[airutils.rudder_limit = 30 +airutils.elevator_limit = 40]]-- + +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "utilities.lua") + +function airutils.powerAdjust(self,dtime,factor,dir,max_power) + local max = max_power or 100 + local add_factor = factor/2 + add_factor = add_factor * (dtime/airutils.ideal_step) --adjusting the command speed by dtime + local power_index = self._power_lever + + if dir == 1 then + if self._power_lever < max then + self._power_lever = self._power_lever + add_factor + end + if self._power_lever > max then + self._power_lever = max + end + end + if dir == -1 then + if self._power_lever > 0 then + self._power_lever = self._power_lever - add_factor + if self._power_lever < 0 then self._power_lever = 0 end + end + if self._power_lever <= 0 then + self._power_lever = 0 + end + end +end + +function airutils.control(self, dtime, hull_direction, longit_speed, longit_drag, + later_speed, later_drag, accel, player, is_flying) + --if self.driver_name == nil then return end + local retval_accel = accel + + local stop = false + local ctrl = nil + + -- player control + 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 + if self._engine_running then + self._engine_running = false + self._autopilot = false + self._power_lever = 0 --zero power + self._last_applied_power = 0 --zero engine + elseif self._engine_running == false and self._energy > 0 then + self._engine_running = true + self._last_applied_power = -1 --send signal to start + end + end + + self._acceleration = 0 + if self._engine_running then + --engine acceleration calc + local engineacc = (self._power_lever * self._max_engine_acc) / 100; + + local factor = 1 + + --increase power lever + if ctrl.jump then + airutils.powerAdjust(self, dtime, factor, 1) + end + --decrease power lever + if ctrl.sneak then + airutils.powerAdjust(self, dtime, factor, -1) + if self._power_lever <= 0 and is_flying == false then + --break + if longit_speed > 0 then + engineacc = -1 + if (longit_speed + engineacc) < 0 then + engineacc = longit_speed * -1 + end + end + if longit_speed < 0 then + engineacc = 1 + if (longit_speed + engineacc) > 0 then + engineacc = longit_speed * -1 + end + end + if abs(longit_speed) < 0.2 then + stop = true + end + end + end + --do not exceed + local max_speed = 6 + if longit_speed > max_speed then + engineacc = engineacc - (longit_speed-max_speed) + if engineacc < 0 then engineacc = 0 end + end + self._acceleration = engineacc + else + local paddleacc = 0 + if longit_speed < 1.0 then + if ctrl.jump then paddleacc = 0.5 end + end + if longit_speed > -1.0 then + if ctrl.sneak then paddleacc = -0.5 end + end + self._acceleration = paddleacc + end + + local hull_acc = vector.multiply(hull_direction,self._acceleration) + retval_accel=vector.add(retval_accel,hull_acc) + + --pitch + local pitch_cmd = 0 + if ctrl.up then pitch_cmd = 1 elseif ctrl.down then pitch_cmd = -1 end + airutils.set_pitch(self, pitch_cmd, dtime) + + -- yaw + local yaw_cmd = 0 + if ctrl.right then yaw_cmd = 1 elseif ctrl.left then yaw_cmd = -1 end + airutils.set_yaw(self, yaw_cmd, dtime) + + --I'm desperate, center all! + if ctrl.right and ctrl.left then + self._elevator_angle = 0 + self._rudder_angle = 0 + end + end + + if longit_speed > 0 then + if ctrl then + if ctrl.right or ctrl.left then + else + airutils.rudder_auto_correction(self, longit_speed, dtime) + end + else + airutils.rudder_auto_correction(self, longit_speed, dtime) + end + if airutils.elevator_auto_correction then + self._elevator_angle = airutils.elevator_auto_correction(self, longit_speed, self.dtime, self._max_speed, self._elevator_angle, self._elevator_limit, airutils.ideal_step, 100) + end + end + + return retval_accel, stop +end + +function airutils.set_pitch(self, dir, dtime) + local pitch_factor = 12 + local multiplier = pitch_factor*dtime + if dir == -1 then + --minetest.chat_send_all("cabrando") + if self._elevator_angle > 0 then pitch_factor = pitch_factor * 2 end + self._elevator_angle = math.max(self._elevator_angle-multiplier,-self._elevator_limit) + elseif dir == 1 then + --minetest.chat_send_all("picando") + if self._angle_of_attack < 0 then pitch_factor = 1 end --lets reduce the command power to avoid accidents + self._elevator_angle = math.min(self._elevator_angle+multiplier,self._elevator_limit) + end +end + +function airutils.set_yaw(self, dir, dtime) + local yaw_factor = 25 + if dir == 1 then + self._rudder_angle = math.max(self._rudder_angle-(yaw_factor*dtime),-self._rudder_limit) + elseif dir == -1 then + self._rudder_angle = math.min(self._rudder_angle+(yaw_factor*dtime),self._rudder_limit) + end +end + +function airutils.rudder_auto_correction(self, longit_speed, dtime) + local factor = 1 + if self._rudder_angle > 0 then factor = -1 end + local correction = (self._rudder_limit*(longit_speed/1000)) * factor * (dtime/airutils.ideal_step) + local before_correction = self._rudder_angle + local new_rudder_angle = self._rudder_angle + correction + if math.sign(before_correction) ~= math.sign(new_rudder_angle) then + self._rudder_angle = 0 + else + self._rudder_angle = new_rudder_angle + end +end + +--obsolete, will be removed +function getAdjustFactor(curr_y, desired_y) + local max_difference = 0.1 + local adjust_factor = 0.5 + local difference = math.abs(curr_y - desired_y) + if difference > max_difference then difference = max_difference end + return (difference * adjust_factor) / max_difference +end + +function airutils.autopilot(self, dtime, hull_direction, longit_speed, accel, curr_pos) + + local retval_accel = accel + + local max_autopilot_power = 85 + + --climb + local velocity = self.object:get_velocity() + local climb_rate = velocity.y * 1.5 + if climb_rate > 5 then climb_rate = 5 end + if climb_rate < -5 then + climb_rate = -5 + end + + self._acceleration = 0 + if self._engine_running then + --engine acceleration calc + local engineacc = (self._power_lever * airutils.max_engine_acc) / 100; + --self.engine:set_animation_frame_speed(60 + self._power_lever) + + local factor = math.abs(climb_rate * 0.1) --getAdjustFactor(curr_pos.y, self._auto_pilot_altitude) + --increase power lever + if climb_rate > 0.2 then + airutils.powerAdjust(self, dtime, factor, -1) + end + --decrease power lever + if climb_rate < 0 then + airutils.powerAdjust(self, dtime, factor, 1, max_autopilot_power) + end + --do not exceed + local max_speed = airutils.max_speed + if longit_speed > max_speed then + engineacc = engineacc - (longit_speed-max_speed) + if engineacc < 0 then engineacc = 0 end + end + self._acceleration = engineacc + end + + local hull_acc = vector.multiply(hull_direction,self._acceleration) + retval_accel=vector.add(retval_accel,hull_acc) + + --pitch + if self._angle_of_attack > self._max_attack_angle then + airutils.set_pitch(self, 1, dtime) + elseif self._angle_of_attack < self._max_attack_angle then + airutils.set_pitch(self, -1, dtime) + end + + -- yaw + airutils.set_yaw(self, 0, dtime) + + if longit_speed > 0 then + airutils.rudder_auto_correction(self, longit_speed, dtime) + if airutils.elevator_auto_correction then + self._elevator_angle = airutils.elevator_auto_correction(self, longit_speed, self.dtime, airutils.max_speed, self._elevator_angle, airutils.elevator_limit, airutils.ideal_step, 100) + end + end + + return retval_accel +end diff --git a/lib_planes/custom_physics.lua b/lib_planes/custom_physics.lua new file mode 100755 index 0000000..d20763e --- /dev/null +++ b/lib_planes/custom_physics.lua @@ -0,0 +1,43 @@ + +local min = math.min +local abs = math.abs +--local deg = math.deg + +function airutils.physics(self) + local friction = 0.99 + local vel=self.object:get_velocity() + -- dumb friction + if self.isonground and not self.isinliquid then + vel = {x=vel.x*friction, + y=vel.y, + z=vel.z*friction} + self.object:set_velocity(vel) + end + + -- bounciness + if self.springiness and self.springiness > 0 then + local vnew = vector.new(vel) + + if not self.collided then -- ugly workaround for inconsistent collisions + for _,k in ipairs({'y','z','x'}) do + if vel[k]==0 and abs(self.lastvelocity[k])> 0.1 then + vnew[k]=-self.lastvelocity[k]*self.springiness + end + end + end + + if not vector.equals(vel,vnew) then + self.collided = true + else + if self.collided then + vnew = vector.new(self.lastvelocity) + end + self.collided = false + end + + self.object:set_velocity(vnew) + end + + self.object:set_acceleration({x=0,y=airutils.gravity,z=0}) + +end diff --git a/lib_planes/entities.lua b/lib_planes/entities.lua new file mode 100644 index 0000000..f94dd0f --- /dev/null +++ b/lib_planes/entities.lua @@ -0,0 +1,617 @@ +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "global_definitions.lua") + +function lib_change_color(self, colstr) + airutils.paint(self, colstr, self._painting_texture) +end + +function airutils.get_staticdata(self) -- unloaded/unloads ... is now saved + return minetest.serialize({ + --stored_sound_handle = self.sound_handle, + stored_energy = self._energy, + stored_owner = self.owner, + stored_hp = self.hp_max, + stored_color = self._color, + stored_power_lever = self._power_lever, + stored_driver_name = self.driver_name, + stored_last_accell = self._last_accell, + stored_engine_running = self._engine_running, + stored_inv_id = self._inv_id, + }) +end + +function airutils.on_deactivate(self) + airutils.save_inventory(self) +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 + self.owner = data.stored_owner + self.hp_max = data.stored_hp + self._color = data.stored_color + self._power_lever = data.stored_power_lever + self.driver_name = data.stored_driver_name + self._last_accell = data.stored_last_accell + self._engine_running = data.stored_engine_running + self._inv_id = data.stored_inv_id + --self.sound_handle = data.stored_sound_handle + --minetest.debug("loaded: ", self._energy) + if self._engine_running then + self._last_applied_power = -1 --signal to start + end + end + + if self._register_parts_method then + self._register_parts_method(self) + end + + airutils.paint(self, self._color, self._painting_texture) + if self._alternate_painting_texture and self._mask_painting_texture then + airutils.paint_with_mask(self, self._color, self._alternate_painting_texture, self._mask_painting_texture) + end + + self.object:set_armor_groups({immortal=1}) + + self.object:set_animation({x = 1, y = self._anim_frames}, 0, 0, true) + if self.wheels then + self.wheels:set_animation({x = 1, y = self._anim_frames}, 0, 0, true) + end + + local inv = minetest.get_inventory({type = "detached", name = self._inv_id}) + -- if the game was closed the inventories have to be made anew, instead of just reattached + if not inv then + airutils.create_inventory(self, self._trunk_slots) + else + self.inv = inv + end +end + +function airutils.on_step(self,dtime,colinfo) + self.dtime = math.min(dtime,0.2) + self.colinfo = colinfo + self.height = airutils.get_box_height(self) + +-- physics comes first + local vel = self.object:get_velocity() + + if colinfo then + self.isonground = colinfo.touching_ground + else + if self.lastvelocity.y==0 and vel.y==0 then + self.isonground = true + else + self.isonground = false + end + end + + self:physics() + + if self.logic then + self:logic() + end + + self.lastvelocity = self.object:get_velocity() + self.time_total=self.time_total+self.dtime +end + +function airutils.logic(self) + local velocity = self.object:get_velocity() + local curr_pos = self.object:get_pos() + + self._last_time_command = self._last_time_command + self.dtime + + if self._last_time_command > 1 then self._last_time_command = 1 end + + local player = nil + if self.driver_name then player = minetest.get_player_by_name(self.driver_name) end + local passenger = nil + if self._passenger then passenger = minetest.get_player_by_name(self._passenger) end + + if player then + local ctrl = player:get_player_control() + --------------------- + -- change the driver + --------------------- + if passenger and self._last_time_command >= 1 and self._instruction_mode == true then + if self._command_is_given == true then + if ctrl.sneak or ctrl.jump or ctrl.up or ctrl.down or ctrl.right or ctrl.left then + self._last_time_command = 0 + --take the control + airutils.transfer_control(self, false) + end + else + if ctrl.sneak == true and ctrl.jump == true then + self._last_time_command = 0 + --trasnfer the control to student + airutils.transfer_control(self, true) + end + end + end + ----------- + --autopilot + ----------- + if self._instruction_mode == false and self._last_time_command >= 1 then + if self._autopilot == true then + if ctrl.sneak or ctrl.jump or ctrl.up or ctrl.down or ctrl.right or ctrl.left then + self._last_time_command = 0 + self._autopilot = false + minetest.chat_send_player(self.driver_name," >>> Autopilot deactivated") + end + else + if ctrl.sneak == true and ctrl.jump == true then + self._last_time_command = 0 + self._autopilot = true + self._auto_pilot_altitude = curr_pos.y + minetest.chat_send_player(self.driver_name,core.colorize('#00ff00', " >>> Autopilot on")) + end + end + end + ---------------------------------- + -- shows the hud for the player + ---------------------------------- + if ctrl.up == true and ctrl.down == true and self._last_time_command >= 1 then + self._last_time_command = 0 + if self._show_hud == true then + self._show_hud = false + else + self._show_hud = true + end + end + end + + local accel_y = self.object:get_acceleration().y + local rotation = self.object:get_rotation() + local yaw = rotation.y + local newyaw=yaw + local pitch = rotation.x + local roll = rotation.z + local newroll=roll + newroll = math.floor(newroll/360) + newroll = newroll * 360 + + local hull_direction = airutils.rot_to_dir(rotation) --minetest.yaw_to_dir(yaw) + local nhdir = {x=hull_direction.z,y=0,z=-hull_direction.x} -- lateral unit vector + + local longit_speed = vector.dot(velocity,hull_direction) + self._longit_speed = longit_speed + local longit_drag = vector.multiply(hull_direction,longit_speed* + longit_speed*self._longit_drag_factor*-1*airutils.sign(longit_speed)) + local later_speed = airutils.dot(velocity,nhdir) + --minetest.chat_send_all('later_speed: '.. later_speed) + local later_drag = vector.multiply(nhdir,later_speed*later_speed* + self._later_drag_factor*-1*airutils.sign(later_speed)) + local accel = vector.add(longit_drag,later_drag) + local stop = false + + local node_bellow = airutils.nodeatpos(airutils.pos_shift(curr_pos,{y=-1.3})) + local is_flying = true + if self.colinfo then + is_flying = not self.colinfo.touching_ground + end + --if is_flying then minetest.chat_send_all('is flying') end + + local is_attached = airutils.checkAttach(self, player) + + if not is_attached then + -- for some engine error the player can be detached from the machine, so lets set him attached again + airutils.checkattachBug(self) + end + + if longit_speed == 0 and is_flying == false and is_attached == false and self._engine_running == false then + return + end + + --ajustar angulo de ataque + if longit_speed then + local percentage = math.abs(((longit_speed * 100)/(self._min_speed + 5))/100) + if percentage > 1.5 then percentage = 1.5 end + self._angle_of_attack = self._wing_angle_of_attack - ((self._elevator_angle / 20)*percentage) + if self._angle_of_attack < -0.5 then + self._angle_of_attack = -0.1 + self._elevator_angle = self._elevator_angle - 0.1 + end --limiting the negative angle]]-- + if self._angle_of_attack > 20 then + self._angle_of_attack = 20 + self._elevator_angle = self._elevator_angle + 0.1 + end --limiting the very high climb angle due to strange behavior]]-- + + --set the plane on level + if airutils.adjust_attack_angle_by_speed then + self._angle_of_attack = airutils.adjust_attack_angle_by_speed(self._angle_of_attack, 1, 6, 40, longit_speed, airutils.ideal_step, self.dtime) + end + end + + --minetest.chat_send_all(self._angle_of_attack) + + -- pitch + local newpitch = math.rad(0) + if airutils.get_plane_pitch then + newpitch = airutils.get_plane_pitch(velocity, longit_speed, self._min_speed, self._angle_of_attack) + end + + + -- adjust pitch at ground + local tail_lift_min_speed = 4 + local tail_lift_max_speed = 8 + if math.abs(longit_speed) > tail_lift_min_speed then + if math.abs(longit_speed) < tail_lift_max_speed then + --minetest.chat_send_all(math.abs(longit_speed)) + local speed_range = tail_lift_max_speed - tail_lift_min_speed + percentage = 1-((math.abs(longit_speed) - tail_lift_min_speed)/speed_range) + if percentage > 1 then percentage = 1 end + if percentage < 0 then percentage = 0 end + local angle = self._tail_angle * percentage + local calculated_newpitch = math.rad(angle) + if newpitch < calculated_newpitch then newpitch = calculated_newpitch end --ja aproveita o pitch atual se ja estiver cerrto + if newpitch > math.rad(self._tail_angle) then newpitch = math.rad(self._tail_angle) end --não queremos arrastar o cauda no chão + end + else + if math.abs(longit_speed) < tail_lift_min_speed then + newpitch = math.rad(self._tail_angle) + end + end + + -- new yaw + if math.abs(self._rudder_angle)>1.5 then + local turn_rate = math.rad(14) + local yaw_turn = self.dtime * math.rad(self._rudder_angle) * turn_rate * + airutils.sign(longit_speed) * math.abs(longit_speed/2) + newyaw = yaw + yaw_turn + end + + --roll adjust + --------------------------------- + local delta = 0.002 + if is_flying then + local roll_reference = newyaw + local sdir = minetest.yaw_to_dir(roll_reference) + local snormal = {x=sdir.z,y=0,z=-sdir.x} -- rightside, dot is negative + local prsr = airutils.dot(snormal,nhdir) + local rollfactor = -90 + local roll_rate = math.rad(10) + newroll = (prsr*math.rad(rollfactor)) * (later_speed * roll_rate) * airutils.sign(longit_speed) + + --[[local rollRotation = -self._rudder_angle * 0.1 + newroll = rollRotation]]-- + + --minetest.chat_send_all('newroll: '.. newroll) + else + delta = 0.2 + if roll > 0 then + newroll = roll - delta + if newroll < 0 then newroll = 0 end + end + if roll < 0 then + newroll = roll + delta + if newroll > 0 then newroll = 0 end + end + end + + --------------------------------- + -- end roll + + local pilot = player + if self._command_is_given and passenger then + pilot = passenger + else + self._command_is_given = false + end + + ------------------------------------------------------ + --accell calculation block + ------------------------------------------------------ + if is_attached or passenger then + if self._autopilot ~= true then + accel, stop = airutils.control(self, self.dtime, hull_direction, + longit_speed, longit_drag, later_speed, later_drag, accel, pilot, is_flying) + else + accel = airutils.autopilot(self, self.dtime, hull_direction, longit_speed, accel, curr_pos) + end + end + + --end accell + + if accel == nil then accel = {x=0,y=0,z=0} end + + --lift calculation + accel.y = accel_y + + --lets apply some bob in water + if self.isinliquid then + self._engine_running = false + local bob = airutils.minmax(airutils.dot(accel,hull_direction),0.2) -- vertical bobbing + accel.y = accel.y + bob + local max_pitch = 6 + local h_vel_compensation = (((longit_speed * 2) * 100)/max_pitch)/100 + if h_vel_compensation < 0 then h_vel_compensation = 0 end + if h_vel_compensation > max_pitch then h_vel_compensation = max_pitch end + newpitch = newpitch + (velocity.y * math.rad(max_pitch - h_vel_compensation)) + end + + local new_accel = accel + if longit_speed > 1.5 then + new_accel = airutils.getLiftAccel(self, velocity, new_accel, longit_speed, roll, curr_pos, self._lift, 15000) + end + -- end lift + + --wind effects + if longit_speed > 1.5 and airutils.wind then + local wind = airutils.get_wind(curr_pos, 0.1) + new_accel = vector.add(new_accel, wind) + end + + if stop ~= true then --maybe == nil + self._last_accell = new_accel + self.object:move_to(curr_pos) + --self.object:set_velocity(velocity) + --[[if player then + airutils.attach(self, player, self._instruction_mode) + end]]-- + airutils.set_acceleration(self.object, new_accel) + else + if stop == true then + self.object:set_acceleration({x=0,y=0,z=0}) + self.object:set_velocity({x=0,y=0,z=0}) + end + end + + if self.wheels then + if is_flying == false then --isn't flying? + --animate wheels + if math.abs(longit_speed) > 0.1 then + self.wheels:set_animation_frame_speed(longit_speed * 10) + else + self.wheels:set_animation_frame_speed(0) + end + else + --stop wheels + self.wheels:set_animation_frame_speed(0) + end + end + + + + ------------------------------------------------------ + -- end accell + ------------------------------------------------------ + + ------------------------------------------------------ + -- sound and animation + ------------------------------------------------------ + airutils.engine_set_sound_and_animation(self) + ------------------------------------------------------ + + --self.object:get_luaentity() --hack way to fix jitter on climb + + --adjust climb indicator + local climb_rate = velocity.y + if climb_rate > 5 then climb_rate = 5 end + if climb_rate < -5 then + climb_rate = -5 + end + + --is an stall, force a recover + if longit_speed < (self._min_speed / 2) and climb_rate < -3.5 and is_flying then + self._elevator_angle = 0 + self._angle_of_attack = -4 + newpitch = math.rad(self._angle_of_attack) + end + + --minetest.chat_send_all('rate '.. climb_rate) + local climb_angle = airutils.get_gauge_angle(climb_rate) + --self.climb_gauge:set_attach(self.object,'',ALBATROS_D5_GAUGE_CLIMBER_POSITION,{x=0,y=0,z=climb_angle}) + + local indicated_speed = longit_speed * 0.9 + if indicated_speed < 0 then indicated_speed = 0 end + local speed_angle = airutils.get_gauge_angle(indicated_speed, -45) + --self.speed_gauge:set_attach(self.object,'',ALBATROS_D5_GAUGE_SPEED_POSITION,{x=0,y=0,z=speed_angle}) + + if is_attached then + if self._show_hud then + airutils.update_hud(player, climb_angle, speed_angle) + else + airutils.remove_hud(player) + end + end + + --adjust power indicator + local power_indicator_angle = airutils.get_gauge_angle(self._power_lever/10) + --self.power_gauge:set_attach(self.object,'',ALBATROS_D5_GAUGE_POWER_POSITION,{x=0,y=0,z=power_indicator_angle}) + + if is_flying == false then + -- new yaw + local turn_rate = math.rad(30) + local yaw_turn = self.dtime * math.rad(self._rudder_angle) * turn_rate * + airutils.sign(longit_speed) * math.abs(longit_speed/2) + newyaw = yaw + yaw_turn + end + + --apply rotations + self.object:set_rotation({x=newpitch,y=newyaw,z=newroll}) + --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 + self.object:set_bone_position("rudder", self._rudder_pos, {x=0,y=self._rudder_angle,z=0}) + --adjust ailerons + if self._aileron_r_pos and self._aileron_l_pos then + self.object:set_bone_position("aileron.r", self._aileron_r_pos, {x=-self._rudder_angle - 90,y=0,z=0}) + self.object:set_bone_position("aileron.l", self._aileron_l_pos, {x=self._rudder_angle - 90,y=0,z=0}) + end + --set stick position + if self.stick then + self.stick:set_attach(self.object,'',self._stick_pos,{x=self._elevator_angle/2,y=0,z=self._rudder_angle}) + end + + -- calculate energy consumption -- + airutils.consumptionCalc(self, accel) + + --test collision + airutils.testImpact(self, velocity, curr_pos) + + --saves last velocity for collision detection (abrupt stop) + self._last_vel = self.object:get_velocity() +end + +function airutils.on_punch(self, puncher, ttime, toolcaps, dir, damage) + if not puncher or not puncher:is_player() then + return + end + + local is_admin = false + is_admin = minetest.check_player_privs(puncher, {server=true}) + local name = puncher:get_player_name() + if self.owner and self.owner ~= name and self.owner ~= "" then + if is_admin == false then return end + 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 is_attached = false + if puncher:get_attach() == self.object then is_attached = true end + + local itmstck=puncher:get_wielded_item() + local item_name = "" + if itmstck then item_name = itmstck:get_name() end + + if is_attached == false then + if airutils.loadFuel(self, puncher:get_player_name()) then + return + end + + --repair + if (item_name == "airutils:repair_tool") + and self._engine_running == false then + if self.hp_max < 50 then + local inventory_item = "default:steel_ingot" + local inv = puncher:get_inventory() + if inv:contains_item("main", inventory_item) then + local stack = ItemStack(inventory_item .. " 1") + inv:remove_item("main", stack) + self.hp_max = self.hp_max + 10 + if self.hp_max > 50 then self.hp_max = 50 end + airutils.setText(self, self.infotext) + else + minetest.chat_send_player(puncher:get_player_name(), "You need steel ingots in your inventory to perform this repair.") + end + end + return + end + + -- deal with painting or destroying + if itmstck then + + if airutils.set_paint(self, puncher, itmstck, self._painting_texture) == false then + if not self.driver and toolcaps and toolcaps.damage_groups + and toolcaps.damage_groups.fleshy and item_name ~= airutils.fuel then + --airutils.hurt(self,toolcaps.damage_groups.fleshy - 1) + --airutils.make_sound(self,'hit') + self.hp_max = self.hp_max - 10 + minetest.sound_play(self._collision_sound, { + object = self.object, + max_hear_distance = 5, + gain = 1.0, + fade = 0.0, + pitch = 1.0, + }) + airutils.setText(self, self.infotext) + end + end + end + + if self.hp_max <= 0 then + airutils.destroy(self) + end + + end + +end + +function airutils.on_rightclick(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 + + local passenger_name = nil + if self._passenger then + passenger_name = self._passenger + end + + local touching_ground, liquid_below = airutils.check_node_below(self.object, 1.3) + local is_on_ground = self.isinliquid or touching_ground or liquid_below + local is_under_water = airutils.check_is_under_water(self.object) + + --minetest.chat_send_all('name '.. dump(name) .. ' - pilot: ' .. dump(self.driver_name) .. ' - pax: ' .. dump(passenger_name)) + --========================= + -- detach pilot + --========================= + if name == self.driver_name then + airutils.pilot_formspec(name) + --========================= + -- detach passenger + --========================= + elseif name == passenger_name then + if is_on_ground or clicker:get_player_control().sneak then + airutils.dettach_pax(self, clicker) + else + minetest.chat_send_player(name, "Hold sneak and right-click to disembark while flying") + end + + --========================= + -- attach pilot + --========================= + elseif not self.driver_name then + if self.owner == name or minetest.check_player_privs(clicker, {protection_bypass=true}) then + if clicker:get_player_control().aux1 == true then --lets see the inventory + airutils.show_vehicle_trunk_formspec(self, clicker, airutils.trunk_slots) + else + if is_under_water then return end + --remove pax to prevent bug + if self._passenger then + local pax_obj = minetest.get_player_by_name(self._passenger) + airutils.dettach_pax(self, pax_obj) + end + + --attach player + if clicker:get_player_control().sneak == true then + -- flight instructor mode + self._instruction_mode = true + airutils.attach(self, clicker, true) + else + -- no driver => clicker is new driver + self._instruction_mode = false + airutils.attach(self, clicker) + end + self._elevator_angle = 0 + self._rudder_angle = 0 + self._command_is_given = false + end + else + minetest.chat_send_player(name, core.colorize('#ff0000', " >>> You aren't the owner of this machine.")) + end + + --========================= + -- attach passenger + --========================= + elseif self.driver_name and not self._passenger then + airutils.attach_pax(self, clicker) + + else + minetest.chat_send_player(name, core.colorize('#ff0000', " >>> Can't enter airplane.")) + end +end diff --git a/lib_planes/forms.lua b/lib_planes/forms.lua new file mode 100644 index 0000000..7f1d547 --- /dev/null +++ b/lib_planes/forms.lua @@ -0,0 +1,72 @@ +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "global_definitions.lua") + +-------------- +-- Manual -- +-------------- + +function airutils.getPlaneFromPlayer(player) + local seat = player:get_attach() + if seat then + local plane = seat:get_attach() + return plane + end + return nil +end + +function airutils.pilot_formspec(name) + local basic_form = table.concat({ + "formspec_version[3]", + "size[6,6]", + }, "") + + basic_form = basic_form.."button[1,1.0;4,1;go_out;Go Offboard]" + basic_form = basic_form.."button[1,2.5;4,1;hud;Show/Hide Gauges]" + + minetest.show_formspec(name, "lib_planes:pilot_main", basic_form) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == "lib_planes:pilot_main" then + local name = player:get_player_name() + local plane_obj = airutils.getPlaneFromPlayer(player) + if plane_obj then + local ent = plane_obj:get_luaentity() + if fields.hud then + if ent._show_hud == true then + ent._show_hud = false + else + ent._show_hud = true + end + end + if fields.go_out then + local touching_ground, liquid_below = airutils.check_node_below(plane_obj, 1.3) + local is_on_ground = ent.isinliquid or touching_ground or liquid_below + + if is_on_ground then --or clicker:get_player_control().sneak then + if ent._passenger then --any pax? + local pax_obj = minetest.get_player_by_name(ent._passenger) + airutils.dettach_pax(ent, pax_obj) + end + ent._instruction_mode = false + --[[ sound and animation + if ent.sound_handle then + minetest.sound_stop(ent.sound_handle) + ent.sound_handle = nil + end + ent.engine:set_animation_frame_speed(0)]]-- + else + -- not on ground + if ent._passenger then + --give the control to the pax + ent._autopilot = false + airutils.transfer_control(ent, true) + ent._command_is_given = true + ent._instruction_mode = true + end + end + airutils.dettachPlayer(ent, player) + end + end + minetest.close_formspec(name, "lib_planes:pilot_main") + end +end) diff --git a/lib_planes/fuel_management.lua b/lib_planes/fuel_management.lua new file mode 100755 index 0000000..78d5f4c --- /dev/null +++ b/lib_planes/fuel_management.lua @@ -0,0 +1,57 @@ +function airutils.contains(table, val) + for k,v in pairs(table) do + if k == val then + return v + end + end + return false +end + +function airutils.loadFuel(self, player_name) + local player = minetest.get_player_by_name(player_name) + local inv = player:get_inventory() + + local itmstck=player:get_wielded_item() + local item_name = "" + if itmstck then item_name = itmstck:get_name() end + + local fuel = airutils.contains(airutils.fuel, item_name) + if fuel then + local stack = ItemStack(item_name .. " 1") + + if self._energy < 10 then + inv:remove_item("main", stack) + self._energy = self._energy + fuel + if self._energy > 10 then self._energy = 10 end + + local energy_indicator_angle = airutils.get_gauge_angle(self._energy) + --self.fuel_gauge:set_attach(self.object,'',self._gauge_fuel_position,{x=0,y=0,z=energy_indicator_angle}) + end + + return true + end + + return false +end + +function airutils.consumptionCalc(self, accel) + if accel == nil then return end + if self._energy > 0 and self._engine_running and accel ~= nil then + local consumed_power = self._power_lever/700000 + --minetest.chat_send_all('consumed: '.. consumed_power) + self._energy = self._energy - consumed_power; + + local energy_indicator_angle = airutils.get_gauge_angle(self._energy) + if self.fuel_gauge then + if self.fuel_gauge:get_luaentity() then + self.fuel_gauge:set_attach(self.object,'',self._gauge_fuel_position,{x=0,y=0,z=energy_indicator_angle}) + end + end + end + if self._energy <= 0 and self._engine_running and accel ~= nil then + self._engine_running = false + self._autopilot = false + if self.sound_handle then minetest.sound_stop(self.sound_handle) end + self.object:set_animation_frame_speed(0) + end +end diff --git a/lib_planes/global_definitions.lua b/lib_planes/global_definitions.lua new file mode 100755 index 0000000..ecf81a9 --- /dev/null +++ b/lib_planes/global_definitions.lua @@ -0,0 +1,6 @@ +-- +-- constants +-- +airutils.vector_up = vector.new(0, 1, 0) + + diff --git a/lib_planes/hud.lua b/lib_planes/hud.lua new file mode 100755 index 0000000..dc38053 --- /dev/null +++ b/lib_planes/hud.lua @@ -0,0 +1,216 @@ +airutils.hud_list = {} + +function airutils.animate_gauge(player, ids, prefix, x, y, angle) + local angle_in_rad = math.rad(angle + 180) + local dim = 10 + local pos_x = math.sin(angle_in_rad) * dim + local pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "2"], "offset", {x = pos_x + x, y = pos_y + y}) + dim = 20 + pos_x = math.sin(angle_in_rad) * dim + pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "3"], "offset", {x = pos_x + x, y = pos_y + y}) + dim = 30 + pos_x = math.sin(angle_in_rad) * dim + pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "4"], "offset", {x = pos_x + x, y = pos_y + y}) + dim = 40 + pos_x = math.sin(angle_in_rad) * dim + pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "5"], "offset", {x = pos_x + x, y = pos_y + y}) + dim = 50 + pos_x = math.sin(angle_in_rad) * dim + pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "6"], "offset", {x = pos_x + x, y = pos_y + y}) + dim = 60 + pos_x = math.sin(angle_in_rad) * dim + pos_y = math.cos(angle_in_rad) * dim + player:hud_change(ids[prefix .. "7"], "offset", {x = pos_x + x, y = pos_y + y}) +end + +function airutils.update_hud(player, climb, speed) + local player_name = player:get_player_name() + + local screen_pos_y = -150 + local screen_pos_x = 10 + + local clb_gauge_x = screen_pos_x + 80 + local clb_gauge_y = screen_pos_y + 5 + local sp_gauge_x = screen_pos_x + 180 + local sp_gauge_y = clb_gauge_y + + local ids = airutils.hud_list[player_name] + if ids then + airutils.animate_gauge(player, ids, "clb_pt_", clb_gauge_x, clb_gauge_y, climb) + airutils.animate_gauge(player, ids, "sp_pt_", sp_gauge_x, sp_gauge_y, speed) + else + ids = {} + + ids["title"] = player:hud_add({ + hud_elem_type = "text", + position = {x = 0, y = 1}, + offset = {x = screen_pos_x +140, y = screen_pos_y -100}, + text = "Flight Information", + alignment = 0, + scale = { x = 100, y = 30}, + number = 0xFFFFFF, + }) + + ids["bg"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = screen_pos_x, y = screen_pos_y}, + text = "airutils_hud_panel.png", + scale = { x = 0.5, y = 0.5}, + alignment = { x = 1, y = 0 }, + }) + + ids["clb_pt_1"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + + ids["clb_pt_2"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["clb_pt_3"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["clb_pt_4"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["clb_pt_5"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["clb_pt_6"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["clb_pt_7"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = clb_gauge_x, y = clb_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + + ids["sp_pt_1"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_2"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_3"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_4"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_5"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_6"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + ids["sp_pt_7"] = player:hud_add({ + hud_elem_type = "image", + position = {x = 0, y = 1}, + offset = {x = sp_gauge_x, y = sp_gauge_y}, + text = "airutils_ind_box.png", + scale = { x = 6, y = 6}, + alignment = { x = 1, y = 0 }, + }) + + airutils.hud_list[player_name] = ids + end +end + + +function airutils.remove_hud(player) + if player then + local player_name = player:get_player_name() + --minetest.chat_send_all(player_name) + local ids = airutils.hud_list[player_name] + if ids then + --player:hud_remove(ids["altitude"]) + --player:hud_remove(ids["time"]) + player:hud_remove(ids["title"]) + player:hud_remove(ids["bg"]) + player:hud_remove(ids["clb_pt_7"]) + player:hud_remove(ids["clb_pt_6"]) + player:hud_remove(ids["clb_pt_5"]) + player:hud_remove(ids["clb_pt_4"]) + player:hud_remove(ids["clb_pt_3"]) + player:hud_remove(ids["clb_pt_2"]) + player:hud_remove(ids["clb_pt_1"]) + player:hud_remove(ids["sp_pt_7"]) + player:hud_remove(ids["sp_pt_6"]) + player:hud_remove(ids["sp_pt_5"]) + player:hud_remove(ids["sp_pt_4"]) + player:hud_remove(ids["sp_pt_3"]) + player:hud_remove(ids["sp_pt_2"]) + player:hud_remove(ids["sp_pt_1"]) + end + airutils.hud_list[player_name] = nil + end + +end diff --git a/lib_planes/init.lua b/lib_planes/init.lua new file mode 100755 index 0000000..e7847c2 --- /dev/null +++ b/lib_planes/init.lua @@ -0,0 +1,25 @@ + + +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "global_definitions.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "control.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "fuel_management.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "custom_physics.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "utilities.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "entities.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "forms.lua") + +-- +-- helpers and co. +-- + + +-- +-- items +-- + +settings = Settings(minetest.get_worldpath() .. "/settings.conf") +local function fetch_setting(name) + local sname = name + return settings and settings:get(sname) or minetest.settings:get(sname) +end + diff --git a/lib_planes/utilities.lua b/lib_planes/utilities.lua new file mode 100644 index 0000000..d027d8b --- /dev/null +++ b/lib_planes/utilities.lua @@ -0,0 +1,332 @@ +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "global_definitions.lua") +dofile(minetest.get_modpath("airutils") .. DIR_DELIM .. "lib_planes" .. DIR_DELIM .. "hud.lua") + +function airutils.properties_copy(origin_table) + local tablecopy = {} + for k, v in pairs(origin_table) do + tablecopy[k] = v + end + return tablecopy +end + +function airutils.get_hipotenuse_value(point1, point2) + return math.sqrt((point1.x - point2.x) ^ 2 + (point1.y - point2.y) ^ 2 + (point1.z - point2.z) ^ 2) +end + +function airutils.dot(v1,v2) + return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z +end + +function airutils.sign(n) + return n>=0 and 1 or -1 +end + +function airutils.minmax(v,m) + return math.min(math.abs(v),m)*airutils.sign(v) +end + +function airutils.get_gauge_angle(value, initial_angle) + initial_angle = initial_angle or 90 + local angle = value * 18 + angle = angle - initial_angle + angle = angle * -1 + return angle +end + +-- attach player +function airutils.attach(self, player, instructor_mode) + instructor_mode = instructor_mode or false + local name = player:get_player_name() + self.driver_name = name + + -- attach the driver + local eye_y = 0 + if instructor_mode == true then + eye_y = -2.5 + player:set_attach(self.passenger_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}) + else + eye_y = -4 + player:set_attach(self.pilot_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}) + end + if airutils.detect_player_api(player) == 1 then + eye_y = eye_y + 6.5 + end + + player:set_eye_offset({x = 0, y = eye_y, z = 2}, {x = 0, y = 1, z = -30}) + player_api.player_attached[name] = true + player_api.set_animation(player, "sit") + -- make the driver sit + minetest.after(1, function() + if player then + --minetest.chat_send_all("okay") + airutils.sit(player) + --apply_physics_override(player, {speed=0,gravity=0,jump=0}) + end + end) +end + +-- attach passenger +function airutils.attach_pax(self, player) + local name = player:get_player_name() + self._passenger = name + + -- attach the driver + local eye_y = 0 + if self._instruction_mode == true then + eye_y = -4 + player:set_attach(self.pilot_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}) + else + eye_y = -2.5 + player:set_attach(self.passenger_seat_base, "", {x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0}) + end + if airutils.detect_player_api(player) == 1 then + eye_y = eye_y + 6.5 + end + + player:set_eye_offset({x = 0, y = eye_y, z = 2}, {x = 0, y = 1, z = -30}) + player_api.player_attached[name] = true + 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 + +function airutils.dettachPlayer(self, player) + local name = self.driver_name + airutils.setText(self, self.infotext) + + airutils.remove_hud(player) + + --self._engine_running = false + + -- driver clicked the object => driver gets off the vehicle + self.driver_name = nil + + -- detach the player + --player:set_physics_override({speed = 1, jump = 1, gravity = 1, sneak = true}) + player:set_detach() + player_api.player_attached[name] = nil + player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0}) + player_api.set_animation(player, "stand") + self.driver = nil + --remove_physics_override(player, {speed=1,gravity=1,jump=1}) +end + +function airutils.dettach_pax(self, player) + local name = self._passenger + + -- passenger clicked the object => driver gets off the vehicle + self._passenger = nil + + -- detach the player + --player:set_physics_override({speed = 1, jump = 1, gravity = 1, sneak = true}) + if player then + player:set_detach() + player_api.player_attached[name] = nil + player_api.set_animation(player, "stand") + player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0}) + --remove_physics_override(player, {speed=1,gravity=1,jump=1}) + end +end + +function airutils.checkAttach(self, player) + if player then + local player_attach = player:get_attach() + if player_attach then + if player_attach == self.pilot_seat_base or player_attach == self.passenger_seat_base then + return true + end + end + end + return false +end + +-- destroy the boat +function airutils.destroy(self) + if self.sound_handle then + minetest.sound_stop(self.sound_handle) + self.sound_handle = nil + end + + if self._passenger then + -- detach the passenger + local passenger = minetest.get_player_by_name(self._passenger) + if passenger then + airutils.dettach_pax(self, passenger) + end + end + + if self.driver_name then + -- detach the driver + local player = minetest.get_player_by_name(self.driver_name) + airutils.dettachPlayer(self, player) + end + + local pos = self.object:get_pos() + if self.fuel_gauge then self.fuel_gauge:remove() end + if self.power_gauge then self.power_gauge:remove() end + if self.climb_gauge then self.climb_gauge:remove() end + if self.speed_gauge then self.speed_gauge:remove() end + if self.engine then self.engine: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 + + if self.stick then self.stick:remove() end + + airutils.destroy_inventory(self) + self.object:remove() + + --[[pos.y=pos.y+2 + minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'hidroplane:wings') + + for i=1,6 do + minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:steel_ingot') + end + + for i=1,2 do + minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'wool:white') + end + + for i=1,6 do + minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:mese_crystal') + minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'default:diamond') + end]]-- + + --minetest.add_item({x=pos.x+math.random()-0.5,y=pos.y,z=pos.z+math.random()-0.5},'hidroplane:hidro') +end + +function airutils.testImpact(self, velocity, position) + local p = position --self.object:get_pos() + local collision = false + if self._last_vel == nil then return end + --lets calculate the vertical speed, to avoid the bug on colliding on floor with hard lag + if abs(velocity.y - self._last_vel.y) > 2 then + local noded = airutils.nodeatpos(airutils.pos_shift(p,{y=-2.8})) + if (noded and noded.drawtype ~= 'airlike') then + collision = true + else + self.object:set_velocity(self._last_vel) + --self.object:set_acceleration(self._last_accell) + self.object:set_velocity(vector.add(velocity, vector.multiply(self._last_accell, self.dtime/8))) + end + end + local impact = abs(airutils.get_hipotenuse_value(velocity, self._last_vel)) + --minetest.chat_send_all('impact: '.. impact .. ' - hp: ' .. self.hp_max) + if impact > 2 then + --minetest.chat_send_all('impact: '.. impact .. ' - hp: ' .. self.hp_max) + if self.colinfo then + collision = self.colinfo.collides + end + end + + if impact > 1.2 and self._longit_speed > 2 then + local noded = airutils.nodeatpos(airutils.pos_shift(p,{y=-2.8})) + if (noded and noded.drawtype ~= 'airlike') then + minetest.sound_play("airutils_touch", { + --to_player = self.driver_name, + object = self.object, + max_hear_distance = 15, + gain = 1.0, + fade = 0.0, + pitch = 1.0, + }, true) + end + end + + if collision then + --self.object:set_velocity({x=0,y=0,z=0}) + local damage = impact / 2 + self.hp_max = self.hp_max - damage --subtract the impact value directly to hp meter + minetest.sound_play(self._collision_sound, { + --to_player = self.driver_name, + object = self.object, + max_hear_distance = 15, + gain = 1.0, + fade = 0.0, + pitch = 1.0, + }, true) + + if self.driver_name then + local player_name = self.driver_name + airutils.setText(self, self.infotext) + + --minetest.chat_send_all('damage: '.. damage .. ' - hp: ' .. self.hp_max) + if self.hp_max < 0 then --if acumulated damage is greater than 50, adieu + airutils.destroy(self) + end + + local player = minetest.get_player_by_name(player_name) + if player then + if player:get_hp() > 0 then + player:set_hp(player:get_hp()-(damage/2)) + end + end + if self._passenger ~= nil then + local passenger = minetest.get_player_by_name(self._passenger) + if passenger then + if passenger:get_hp() > 0 then + passenger:set_hp(passenger:get_hp()-(damage/2)) + end + end + end + end + + end +end + +function airutils.checkattachBug(self) + -- for some engine error the player can be detached from the submarine, so lets set him attached again + if self.owner and self.driver_name then + -- attach the driver again + local player = minetest.get_player_by_name(self.owner) + if player then + if player:get_hp() > 0 then + airutils.attach(self, player, self._instruction_mode) + else + airutils.dettachPlayer(self, player) + end + else + if self._passenger ~= nil and self._command_is_given == false then + self._autopilot = false + airutils.transfer_control(self, true) + end + end + end +end + +function airutils.engineSoundPlay(self) + --sound + if self.sound_handle then minetest.sound_stop(self.sound_handle) end + if self.object then + self.sound_handle = minetest.sound_play({name = self._engine_sound}, + {object = self.object, gain = 2.0, + pitch = 0.5 + ((self._power_lever/100)/2), + max_hear_distance = 15, + loop = true,}) + end +end + +function airutils.engine_set_sound_and_animation(self) + --minetest.chat_send_all('test1 ' .. dump(self._engine_running) ) + if self._engine_running then + if self._last_applied_power ~= self._power_lever then + --minetest.chat_send_all('test2') + self._last_applied_power = self._power_lever + self.object:set_animation_frame_speed(60 + self._power_lever) + airutils.engineSoundPlay(self) + end + else + if self.sound_handle then + minetest.sound_stop(self.sound_handle) + self.sound_handle = nil + self.object:set_animation_frame_speed(0) + end + end +end + + diff --git a/models/airutils_seat_base.b3d b/models/airutils_seat_base.b3d new file mode 100755 index 0000000..ccf1db3 Binary files /dev/null and b/models/airutils_seat_base.b3d differ diff --git a/sounds/airutils_collision.ogg b/sounds/airutils_collision.ogg new file mode 100755 index 0000000..b15fbb1 Binary files /dev/null and b/sounds/airutils_collision.ogg differ diff --git a/sounds/airutils_touch.ogg b/sounds/airutils_touch.ogg new file mode 100644 index 0000000..90d35c4 Binary files /dev/null and b/sounds/airutils_touch.ogg differ diff --git a/textures/airutils_black2.png b/textures/airutils_black2.png new file mode 100755 index 0000000..4652883 Binary files /dev/null and b/textures/airutils_black2.png differ diff --git a/textures/airutils_brown.png b/textures/airutils_brown.png new file mode 100644 index 0000000..ff14583 Binary files /dev/null and b/textures/airutils_brown.png differ diff --git a/textures/airutils_grey.png b/textures/airutils_grey.png new file mode 100755 index 0000000..af9db27 Binary files /dev/null and b/textures/airutils_grey.png differ diff --git a/textures/airutils_hud_panel.png b/textures/airutils_hud_panel.png new file mode 100755 index 0000000..01e249f Binary files /dev/null and b/textures/airutils_hud_panel.png differ diff --git a/textures/airutils_ind_box.png b/textures/airutils_ind_box.png new file mode 100755 index 0000000..dabada9 Binary files /dev/null and b/textures/airutils_ind_box.png differ diff --git a/textures/airutils_painting.png b/textures/airutils_painting.png new file mode 100755 index 0000000..0c02eaf Binary files /dev/null and b/textures/airutils_painting.png differ