/* base/ship.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include #include #include "auxiliary/functions.h" #include "core/gameserver.h" #include "core/entity.h" #include "base/game.h" #include "base/ship.h" #include "math/functions.h" using math::degrees360f; using math::degrees180f; namespace game { const float MIN_DELTA = 0.000001f; Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable() { entity_moduletypeid = ship_enttype; set_modelname(shipmodel->modelname()); set_name(shipmodel->name()); set_info(shipmodel); ship_shipmodel = shipmodel; ship_jumpdrive = shipmodel->jumpdrive(); ship_impulsedrive_timer = 0; ship_jumpdrive_timer = 0; ship_jumpdepart = 0; if (owner) { // this ship is owned by a player set_owner(owner); get_color().assign(owner->color()); get_color_second().assign(owner->color_second()); std::string str(aux::text_strip(owner->name())); aux::to_label(str); set_label(str); // add an inventory set_inventory(new core::Inventory()); inventory()->set_capacity(shipmodel->maxcargo()); } else { set_name(shipmodel->name()); set_label(shipmodel->label()); } if (shipmodel->dock()) { using core::MenuDescription; using core::ButtonDescription; MenuDescription *menu_main = new MenuDescription(); menu_main->set_label("main"); menu_main->set_text("Launch area"); add_menu(menu_main); ButtonDescription *button = new ButtonDescription(); button->set_text("Launch"); button->set_command("launch", ButtonDescription::CommandGame); button->set_alignment(ButtonDescription::Center); menu_main->add_button(button); set_flag(core::Entity::Dockable); } set_mass(shipmodel->mass()); set_impulse_force(shipmodel->impulse_force()); set_thrust_force(shipmodel->thrust_force()); set_strafe_force(shipmodel->strafe_force()); set_torque_force(shipmodel->torque_force()); reset(); const float linear_damp = 0.8f; const float angular_damp = 0.8f; body()->setDamping(linear_damp, angular_damp); } Ship::~Ship() { } void Ship::reset() { current_target_direction = 0.0f; current_target_pitch = 0.0f;; current_target_roll = 0.0f; current_target_strafe = 0.0f; current_target_vstrafe = 0.0f; current_target_afterburner = 0.0f; EntityControlable::reset(); } void Ship::func_impulse() { if (entity_state == core::Entity::Impulse) { entity_state = core::Entity::Normal; target_thrust = 1.0f; entity_thrust = 0.0f; set_dirty(); return; } if (entity_state == core::Entity::ImpulseInitiate) { entity_state = core::Entity::Normal; set_dirty(); return; } if (entity_state == core::Entity::JumpInitiate) { entity_state = core::Entity::Normal; ship_jumpdrive_timer = 0; entity_timer = 0; set_dirty(); } if (entity_state != core::Entity::Normal) return; entity_state = core::Entity::ImpulseInitiate; entity_timer = impulse_timer_delay; ship_impulsedrive_timer = core::server()->time(); } void Ship::initiate_jump(JumpPoint *depart) { ship_jumpdepart = 0; if (!depart) return; if (!depart->target()) return; ship_jumpdepart = depart; entity_state = core::Entity::JumpInitiate; entity_timer = jump_timer_delay; ship_jumpdrive_timer = core::server()->time(); set_dirty(); } void Ship::func_jump(std::string const &args) { if (entity_state == core::Entity::Docked) { return; } // devel mode provides instant jump access to arbitrary systems if (Game::g_devel->value() && (args.size())) { core::Zone *jumptargetzone = core::Zone::search(args); if (!jumptargetzone) { std::string helpstr; for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) { if (helpstr.size()) helpstr.append("^N|^B"); helpstr.append((*it).second->label()); } owner()->send("Usage: jump [^B" + helpstr + "^N]"); return; } if (jumptargetzone == zone()) { owner()->send("Already in the " + jumptargetzone->name()); return; } owner()->send("Jumping to the " + jumptargetzone->name()); set_zone(jumptargetzone); ship_jumpdrive_timer = 0; entity_timer = 0; set_state(core::Entity::Jump); set_dirty(); return; } else { if (!jumpdrive() && !Game::g_devel->value()) { owner()->send("This ship is not equiped with a hyperspace drive!"); return; } else if (entity_state == core::Entity::Jump) { return; } else if (entity_state == core::Entity::JumpInitiate) { owner()->send("Jump aborted, hyperspace drive deactivated"); ship_jumpdrive_timer = 0; entity_timer = 0; entity_state = core::Entity::Normal; return; } initiate_jump(find_closest_jumppoint()); } } JumpPoint * Ship::find_closest_jumppoint() { // find closest jumpgate or jumppoint float d = -1; JumpPoint *jumppoint = 0; for (core::Zone::Content::iterator it = zone()->content().begin(); it != zone()->content().end(); it++) { core::Entity *entity = (*it); if ((entity->moduletype() == jumppoint_enttype) || (entity->moduletype() == jumpgate_enttype)) { JumpPoint *te = static_cast(entity); float d1 = math::distance(location(), te->location()); if ((d < 0) || (d1 < d)) { d = d1; jumppoint = te; } } } if (jumppoint && jumppoint->target()) { if (Game::g_jumppointrange->value() < d) { owner()->send("Jumppoint out of range!"); return 0; } else { owner()->send("Jumping to the " + jumppoint->target()->zone()->name()); return jumppoint; } } else { owner()->send("No jumppoints found!"); return 0; } return 0; } void Ship::explode() { set_state(core::Entity::Destroyed); target_direction = 0; target_pitch = 0; target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; target_afterburner = 0.0f; target_thrust = 0; entity_thrust = 0; if (owner()) { if (owner()->control() == this) owner()->set_view(this); } }; void Ship::set_zone(core::Zone *zone) { core::EntityControlable::set_zone(zone); if (owner() && (owner()->control() == (EntityControlable*) this)) owner()->set_zone(zone); } void Ship::action (btScalar seconds) { float maxspeed = 0; float engine_force = 0; btTransform t; switch (entity_state) { case core::Entity::Normal: engine_force = ship_thrust_force * entity_thrust; maxspeed = ship_shipmodel->maxspeed() * entity_thrust; break; case core::Entity::ImpulseInitiate: case core::Entity::Impulse: engine_force = ship_impulse_force; maxspeed = (Game::g_impulsespeed ? Game::g_impulsespeed->value() * 0.01f : 0.0f); break; case core::Entity::JumpInitiate: case core::Entity::Jump: maxspeed = 0.0f; engine_force = 0.0f; break; case core::Entity::Docked: maxspeed = 0.0f; engine_force = 0.0f; t.setIdentity(); t.setOrigin(to_btVector3(location())); t.setBasis(to_btMatrix3x3(axis())); body()->setWorldTransform(t); zone()->physics()->synchronizeSingleMotionState(entity_body); return; break; default: maxspeed = 0.0f; engine_force = 0.0f; break; } t.setIdentity(); entity_motionstate->getWorldTransform(t); get_location().assign(t.getOrigin()); get_axis().assign(t.getBasis()); const float torque_scale = 0.001f; const float roll_scale = 0.25f; // apply strafe body()->applyCentralImpulse(math::to_btVector3(axis().up() * current_target_vstrafe * ship_strafe_force)); body()->applyCentralImpulse(math::to_btVector3(axis().left() * current_target_strafe * ship_strafe_force)); // apply afterburner body()->applyCentralImpulse(math::to_btVector3(axis().forward() * current_target_afterburner * ship_strafe_force)); // apply main thruster if (current_target_afterburner >= 0) { body()->applyCentralImpulse(math::to_btVector3(axis().forward() * engine_force)); } // apply direction body()->applyTorqueImpulse(math::to_btVector3(axis().up() * current_target_direction * ship_torque_force * torque_scale)); // apply pitch body()->applyTorqueImpulse(math::to_btVector3(axis().left() * -current_target_pitch * ship_torque_force * torque_scale)); // apply roll body()->applyTorqueImpulse(math::to_btVector3(axis().forward() * -current_target_roll * ship_torque_force * roll_scale * torque_scale)); // limit speed entity_speed = (float) entity_body->getLinearVelocity().length(); if (entity_speed > Game::g_impulsespeed->value() * 0.01f) { entity_speed = Game::g_impulsespeed->value() * 0.01f; body()->setLinearVelocity(math::to_btVector3(axis().forward() * entity_speed)); } } void Ship::frame(float seconds) { const float direction_reaction = 2.0f; // directional control reaction time const float thrust_reaction = 0.5f; // thrust control reaction time const float strafe_reaction = 1.5f; // strafe control reaction time const float afterburner_reaction = 1.0f; // afterburner control reaction time math::Vector3f n; // normal of a plane math::Axis target_axis(axis()); // target axis entity_movement = 0; /* -- update state ----------------------------------------- */ // speed might be set to 0 on this update if (entity_speed != 0.0f) set_dirty(); if (entity_state == core::Entity::Docked) { target_direction = 0; target_pitch = 0; target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; target_afterburner = 0.0f; target_thrust = 0; entity_thrust = 0; entity_speed = 0.0f; } else if (entity_state == core::Entity::JumpInitiate) { if (ship_jumpdrive_timer + 1.0f <= core::server()->time()) { entity_timer -= 1.0f; if (entity_timer <= 0) { if (ship_jumpdepart && ship_jumpdepart->target()) { set_state(core::Entity::Jump); entity_speed = Game::g_impulsespeed->value() * 0.01f; if (ship_jumpdepart->moduletype() == jumpgate_enttype) { get_axis().assign(ship_jumpdepart->target()->axis()); get_location().assign(ship_jumpdepart->target()->location()); get_location() += axis().forward() * (radius() + ship_jumpdepart->target()->radius()); } else { get_location().assign(ship_jumpdepart->target()->location() + location() - ship_jumpdepart->location()); } set_zone(ship_jumpdepart->target()->zone()); if (owner() && owner()->view() && (owner()->view()->zone() != ship_jumpdepart->target()->zone())) owner()->set_view(0); owner()->send("^BJumping to the " + ship_jumpdepart->target()->zone()->name()); } else { set_state(core::Entity::Normal); } ship_jumpdrive_timer = 0; entity_timer = 0; set_dirty(); return; } else { ship_jumpdrive_timer = core::server()->time(); set_dirty(); } } // control is disabled while the jumpdrive is activated target_direction = 0; target_pitch = 0; target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; target_afterburner = 0.0f; target_thrust = 0.1f; } else if (entity_state == core::Entity::Jump) { // control is disabled while the jumpdrive is activated target_direction = 0; target_pitch = 0; target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; target_afterburner = 0.0f; target_thrust = 0.0f; // FIXME 5 second cooldown entity_state = core::Entity::Normal; set_dirty(); if (owner() && owner()->view() && owner()->control() == (EntityControlable*) this) owner()->set_view(0); return; } else if (entity_state == core::Entity::ImpulseInitiate) { // cancel impulse drive if afterburner goes reverse if (target_afterburner < 0.0f) { set_state(core::Entity::Normal); set_dirty(); entity_timer = 0; } else { if (ship_impulsedrive_timer + 1.0f <= core::server()->time()) { entity_timer -= 1.0f; if (entity_timer <= 0) { entity_state = core::Entity::Impulse; entity_timer = 0; set_dirty(); } else { ship_impulsedrive_timer = core::server()->time(); set_dirty(); } } // clamp input values target_thrust = 0.0f; target_afterburner = 0.0f; math::clamp(target_pitch, -1.0f, 1.0f); math::clamp(target_roll, -1.0f, 1.0f); math::clamp(target_direction, -1.0f, 1.0f); } } else if (entity_state == core::Entity::Impulse) { // clamp input values math::clamp(target_pitch, -1.0f, 1.0f); math::clamp(target_roll, -1.0f, 1.0f); math::clamp(target_direction, -1.0f, 1.0f); // cancel impulse drive if afterburner goes reverse if (target_afterburner < 0.0f) { set_state(core::Entity::Normal); set_dirty(); entity_timer = 0; target_thrust = 1.0f; entity_thrust = 1.0f; } else { target_afterburner = 0.0f; target_thrust = 0.0f; } } else if (entity_state == core::Entity::Normal) { // clamp input values math::clamp(target_thrust, 0.0f, 1.0f); math::clamp(target_pitch, -1.0f, 1.0f); math::clamp(target_roll, -1.0f, 1.0f); math::clamp(target_direction, -1.0f, 1.0f); math::clamp(target_afterburner, -1.0f, 1.0f); } else if (entity_state == core::Entity::Destroyed) { target_direction = 0; target_pitch = 0; target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; target_afterburner = 0.0f; target_thrust = 0; entity_thrust = 0; } // update afterburner control target if (current_target_afterburner < target_afterburner) { current_target_afterburner += afterburner_reaction * seconds; if (current_target_afterburner > target_afterburner) current_target_afterburner = target_afterburner; } else if (current_target_afterburner > target_afterburner) { current_target_afterburner -= afterburner_reaction * seconds; if (current_target_afterburner < target_afterburner) current_target_afterburner = target_afterburner; } // update thrust control target if (current_target_afterburner < 0.0f) { target_thrust = 0; } if (entity_thrust < target_thrust) { entity_thrust += thrust_reaction * seconds; if (entity_thrust > target_thrust) entity_thrust = target_thrust; } else if (entity_thrust > target_thrust) { entity_thrust -= thrust_reaction * seconds; if (entity_thrust < target_thrust) entity_thrust = target_thrust; } math::clamp(entity_thrust, 0.0f, 1.0f); // update strafe control target if (current_target_strafe < target_strafe) { current_target_strafe += strafe_reaction * seconds; if (current_target_strafe > target_strafe) current_target_strafe = target_strafe; } else if (current_target_strafe > target_strafe) { current_target_strafe -= strafe_reaction * seconds; if (current_target_strafe < target_strafe) current_target_strafe = target_strafe; } // update vstrafe control target if (current_target_vstrafe < target_vstrafe) { current_target_vstrafe += strafe_reaction * seconds; if (current_target_vstrafe > target_vstrafe) current_target_vstrafe = target_vstrafe; } else if (current_target_vstrafe > target_vstrafe) { current_target_vstrafe -= strafe_reaction * seconds; if (current_target_vstrafe < target_vstrafe) current_target_vstrafe = target_vstrafe; } // update direction control target if (current_target_direction < target_direction) { current_target_direction += direction_reaction * seconds; if (current_target_direction > target_direction) { current_target_direction = target_direction; } } else if (current_target_direction > target_direction) { current_target_direction -= direction_reaction * seconds; if (current_target_direction < target_direction) { current_target_direction = target_direction; } } // update pitch control target if (current_target_pitch < target_pitch) { current_target_pitch += direction_reaction * seconds; if (current_target_pitch > target_pitch) current_target_pitch = target_pitch; } else if (current_target_pitch > target_pitch) { current_target_pitch -= direction_reaction * seconds; if (current_target_pitch < target_pitch) current_target_pitch = target_pitch; } // update roll control target if (current_target_roll < target_roll) { current_target_roll += direction_reaction * seconds; if (current_target_roll > target_roll) current_target_roll = target_roll; } else if (current_target_roll > target_roll) { current_target_roll -= direction_reaction * seconds; if (current_target_roll < target_roll) current_target_roll = target_roll; } /* // -- BULLET // apply thrust body()->applyCentralForce(math::to_btVector3(axis().forward() * (actual_thrust * actual_acceleration))); // apply strafe body()->applyCentralForce(math::to_btVector3(axis().left() * (current_target_strafe * 0.15f * actual_acceleration))); body()->applyCentralForce(math::to_btVector3(axis().up() * (current_target_vstrafe * 0.15f * actual_acceleration))); // FIXME get movement state from linear/angular velocity entity_movement = target_thrust; entity_movement = math::max(entity_movement, fabs(current_target_pitch)); entity_movement = math::max(entity_movement, fabs(current_target_direction)); entity_movement = math::max(entity_movement, fabs(current_target_roll)); entity_movement = math::max(entity_movement, fabs(current_target_afterburner)); entity_movement = math::max(entity_movement, fabs(current_target_strafe)); entity_movement = math::max(entity_movement, fabs(current_target_vstrafe)); if (entity_movement > 0) { set_dirty(); } EntityDynamic::frame(seconds); // apply direction rotation to target axis if (fabs(current_target_direction) > MIN_DELTA) { math::clamp(current_target_direction, -1.0f, 1.0f); target_axis.change_direction(actual_turnspeed * current_target_direction); } else { current_target_direction = 0.0f; } // apply pitch rotation to target axis if (fabs(current_target_pitch) > MIN_DELTA) { math::clamp(current_target_pitch, -1.0f, 1.0f); target_axis.change_pitch(actual_turnspeed * current_target_pitch); } else { current_target_pitch = 0.0f; } // apply roll rotation to axis if (fabs(current_target_roll) > MIN_DELTA) { math::clamp(current_target_roll, -1.0f, 1.0f); get_axis().change_roll(actual_turnspeed * current_target_roll * seconds); } else { current_target_roll = 0.0f; } // update axis float cosangle; // cosine of an angle float angle; // angle in radians n.assign(math::crossproduct(axis().forward(), target_axis.forward())); if (!(n.length() < MIN_DELTA)) { n.normalize(); cosangle = math::dotproduct(axis().forward(), target_axis.forward()); angle = acos(cosangle) * seconds; // * 180.0f / M_PI; if (angle > MIN_DELTA) get_axis().rotate(n, -angle); } // update speed if ((entity_state == core::Entity::ImpulseInitiate) || (entity_state == core::Entity::Impulse)) { actual_thrust = 1.0f; } else { actual_thrust = entity_thrust + current_target_afterburner * 0.15f; } const float max = actual_thrust * actual_maxspeed; if (entity_speed < max) { entity_speed += actual_acceleration * seconds; if (entity_speed > max) { entity_speed = max; } } else if (entity_speed > max) { entity_speed -= actual_acceleration * seconds; if (entity_speed < max) { entity_speed = max; } } // apply strafe to location if (fabs(current_target_strafe) > MIN_DELTA) { get_location() += axis().left() * (current_target_strafe * 0.15f * actual_maxspeed) * seconds; } // apply vstrafe to location if (fabs(current_target_vstrafe) > MIN_DELTA) { get_location() += axis().up() * (current_target_vstrafe * 0.15f * actual_maxspeed) * seconds; } // apply speed to location if (fabs(speed()) > MIN_DELTA) { get_location() += axis().forward() * speed() * seconds; } entity_movement = target_thrust; entity_movement = math::max(entity_movement, fabs(current_target_pitch)); entity_movement = math::max(entity_movement, fabs(current_target_direction)); entity_movement = math::max(entity_movement, fabs(current_target_roll)); entity_movement = math::max(entity_movement, fabs(current_target_afterburner)); entity_movement = math::max(entity_movement, fabs(current_target_strafe)); entity_movement = math::max(entity_movement, fabs(current_target_vstrafe)); if ((entity_movement > 0) || (entity_speed > 0)) { set_dirty(); } // transfer entity location to motion state btTransform t; t.setIdentity(); t.setOrigin(math::to_btVector3(location())); t.setBasis(math::to_btMatrix3x3(axis())); entity_body->setWorldTransform(t); if (zone()) zone()->physics()->synchronizeSingleMotionState(entity_body); */ EntityControlable::frame(seconds); } } // namespace game