From ce62661012a167d48bd6117940a551355eb6773b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 15 Oct 2013 19:57:27 +0000 Subject: Added ship autopilot, removed entity_controlflags, added goto ui button. --- src/game/base/game.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/game/base/game.h | 2 ++ src/game/base/ship.cc | 49 +++++++++++++++++++++++++++++++++------------- src/game/base/ship.h | 30 +++++++++++++++++++++++++++- 4 files changed, 121 insertions(+), 14 deletions(-) (limited to 'src/game') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index aa7fd07..72a684b 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -142,12 +142,60 @@ void Game::func_impulse(core::Player *player, std::string const &args) { if (!player->control()) return; + if (player->control()->moduletype() != ship_enttype) return; + Ship * ship = static_cast(player->control()); ship->func_impulse(); } +// autopilot free flight +void Game::func_freeflight(core::Player *player, std::string const &args) +{ + if (!player->control()) + return; + + if (player->control()->moduletype() != ship_enttype) + return; + + Ship * ship = static_cast(player->control()); + + ship->set_autopilot_target(0); + ship->unset_autopilot_flag(Ship::AutoPilotEnabled); + player->send("Autopilot disabled"); +} + +// autopilot goto +void Game::func_target_goto(core::Player *player, core::Entity *entity) +{ + if (!player->control()) + return; + + if (player->control() == entity) + return; + + if (player->control()->zone() != entity->zone()) + return; + + if (player->control()->moduletype() != ship_enttype) + return; + + Ship * ship = static_cast(player->control()); + + if (ship->has_autopilot_flag(Ship::AutoPilotEnabled) && (entity == ship->autopilot_target())) { + ship->set_autopilot_target(0); + ship->unset_autopilot_flag(Ship::AutoPilotEnabled); + player->send("Autopilot disabled"); + } else { + ship->set_autopilot_target(entity); + ship->set_autopilot_flag(Ship::AutoPilotEnabled); + ship->unset_autopilot_flag(Ship::AutoPilotDock); + ship->unset_autopilot_flag(Ship::AutoPilotFormation); + player->send("Autopilot set to " + entity->name()); + } +} + // a player sends a docking request void Game::func_target_dock(core::Player *player, core::Entity *entity) { @@ -1631,6 +1679,9 @@ Game::Game() : core::Module("Project::OSiRiON", true) func = core::Func::add("impulse", Game::func_impulse); func->set_info("toggle kinetic impulse drive"); + + func = core::Func::add("freeflight", Game::func_freeflight); + func->set_info("disable autopilot, enable free flight"); func = core::Func::add("launch", Game::func_launch); func->set_info("launch to space when docked"); @@ -1641,6 +1692,9 @@ Game::Game() : core::Module("Project::OSiRiON", true) func = core::Func::add("goto", Game::func_goto); func->set_info("[string] goto to an entity within the zone"); + func = core::Func::add("@goto", Game::func_target_goto); + func->set_info("set autopilot to target"); + func = core::Func::add("@dock", Game::func_target_dock); func->set_info("send a docking request to target"); diff --git a/src/game/base/game.h b/src/game/base/game.h index 3872303..645d573 100644 --- a/src/game/base/game.h +++ b/src/game/base/game.h @@ -125,6 +125,7 @@ private: static void func_impulse(core::Player *player, std::string const &args); static void func_launch(core::Player *player, std::string const &args); static void func_respawn(core::Player *player, std::string const &args); + static void func_freeflight(core::Player *player, std::string const &args); static void func_goto(core::Player *player, const std::string &args); static void func_buy(core::Player *player, std::string const &args); static void func_sell(core::Player *player, const std::string &args); @@ -139,6 +140,7 @@ private: /* ---- target functions ----------------------------------- */ + static void func_target_goto(core::Player *player, core::Entity *entity); static void func_target_dock(core::Player *player, core::Entity *entity); static void func_target_hail(core::Player *player, core::Entity *entity); static void func_target_trade(core::Player *player, core::Entity *entity); diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index 23baa5d..d816943 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -38,6 +38,9 @@ Ship::Ship(core::Player *owner, const ShipModel *shipmodel) : core::EntityContro ship_jumpdrive_timer = 0; ship_jumpdepart = 0; + ship_autopilot_target = 0; + ship_autopilot_flags = AutoPilotDisabled; + ship_dock = 0; ship_spawn = 0; @@ -344,6 +347,11 @@ void Ship::set_zone(core::Zone *zone) owner()->set_zone(zone); } +void Ship::set_autopilot_target(core::Entity *target) +{ + ship_autopilot_target = target; +} + void Ship::set_dock(core::Entity *dock) { if (!dock) { @@ -623,13 +631,30 @@ void Ship::frame(const unsigned long elapsed) math::Vector3f n; // normal of a plane math::Axis target_axis(axis()); // target axis - /* -- update state ----------------------------------------- */ // speed might be set to 0 on this update if (entity_speed != 0.0f) { set_dirty(); } + /* -- autopilot -------------------------------------------- */ + + if (has_autopilot_flag(Ship::AutoPilotEnabled) && !has_target_controlflag(core::EntityControlable::ControlFlagOverride)) { + + if (!ship_autopilot_target) { + unset_autopilot_flag(Ship::AutoPilotEnabled); + + } else if (!zone()->find_entity(ship_autopilot_target)) { + ship_autopilot_target = 0; + + } else { + frame_autopilot_goto(elapsed, ship_autopilot_target); + + } + } + + /* -- update state ----------------------------------------- */ + if (entity_state == core::Entity::Docked) { target_direction = 0; @@ -637,13 +662,13 @@ void Ship::frame(const unsigned long elapsed) target_roll = 0; target_strafe = 0.0f; target_vstrafe = 0.0f; + target_controlflags = 0; target_afterburner = 0.0f; target_thrust = 0; entity_thrust = 0; entity_speed = 0.0f; - entity_controlflags = 0; } else if (entity_state == core::Entity::JumpInitiate) { @@ -693,8 +718,8 @@ void Ship::frame(const unsigned long elapsed) target_afterburner = 0.0f; target_thrust = 0.1f; - entity_controlflags = 0; - + target_controlflags = 0; + } else if (entity_state == core::Entity::Jump) { // control is disabled while the jumpdrive is activated @@ -707,8 +732,8 @@ void Ship::frame(const unsigned long elapsed) target_afterburner = 0.0f; target_thrust = 0.0f; - entity_controlflags = 0; - + target_controlflags = 0; + // apply jump drive cooldown if (ship_jumpdrive_timer + 1.0f <= core::server()->time()) { entity_timer -= 1.0f; @@ -758,7 +783,7 @@ void Ship::frame(const unsigned long elapsed) 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 @@ -777,7 +802,7 @@ void Ship::frame(const unsigned long elapsed) //target_afterburner = 0.0f; //target_thrust = 0.0f; } - + } else if (entity_state == core::Entity::Normal) { // clamp input values @@ -787,9 +812,6 @@ void Ship::frame(const unsigned long elapsed) math::clamp(target_direction, -1.0f, 1.0f); math::clamp(target_afterburner, -1.0f, 1.0f); - entity_controlflags = target_controlflags; - - } else if (entity_state == core::Entity::Destroyed) { target_direction = 0; @@ -800,9 +822,10 @@ void Ship::frame(const unsigned long elapsed) target_afterburner = 0.0f; target_thrust = 0; + + target_controlflags = 0; entity_thrust = 0; - entity_controlflags = 0; } // current health @@ -919,7 +942,7 @@ void Ship::frame(const unsigned long elapsed) EntityControlable::frame(elapsed); // fire weapons - if (model() && slots() && (state() == core::Entity::Normal) && has_controlflag(core::EntityControlable::ControlFlagFire)) { + if (model() && slots() && (state() == core::Entity::Normal) && has_target_controlflag(core::EntityControlable::ControlFlagFire)) { const float modelscale = radius() / model()->radius(); diff --git a/src/game/base/ship.h b/src/game/base/ship.h index ad2c71b..7b25f18 100644 --- a/src/game/base/ship.h +++ b/src/game/base/ship.h @@ -24,6 +24,8 @@ const float MIN_DELTA = 0.000001f; class Ship : public core::EntityControlable { public: + enum AutoPilotFlags { AutoPilotDisabled = 0, AutoPilotEnabled = 1, AutoPilotDock = 2, AutoPilotFormation = 4 }; + Ship(core::Player *owner, const ShipModel *shipmodel); ~Ship(); @@ -72,11 +74,21 @@ public: } /// (dockable) entity where the ship will respawn if destroyed - core::Entity *spawn() + inline core::Entity *spawn() { return ship_spawn; } + /// current autopilot target + inline core::Entity *autopilot_target() + { + return ship_autopilot_target; + } + + inline bool has_autopilot_flag(const AutoPilotFlags flag) { + return ( (ship_autopilot_flags & flag) == flag); + } + /// maximum amount of armor inline const float maxarmor() const { @@ -177,6 +189,19 @@ public: */ void set_dock(core::Entity *dock); + /** + * @brief set the autopilot target + * */ + void set_autopilot_target(core::Entity *target); + + inline void set_autopilot_flag(const AutoPilotFlags flag) { + ship_autopilot_flags = ship_autopilot_flags | flag; + } + + inline void unset_autopilot_flag(const AutoPilotFlags flag) { + ship_autopilot_flags = ship_autopilot_flags & ~flag; + } + void launch(); void set_spawn(core::Entity *spawn); @@ -227,6 +252,9 @@ private: core::Entity *ship_dock; core::Entity *ship_spawn; + + int ship_autopilot_flags; + core::Entity *ship_autopilot_target; }; } -- cgit v1.2.3