From e7061d6fed8bdfc1828315eb8745cd919cb96bbc Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 22 Dec 2014 16:57:20 +0000 Subject: Prevent wingmen from getting destroyed when you buy a new ship or use the 'give ship' engine command. --- src/game/base/game.cc | 4 +++- src/game/base/npc.cc | 37 ++++++++++++++++++++++++++++++------- src/game/base/npc.h | 21 ++++++++++++++++++--- 3 files changed, 51 insertions(+), 11 deletions(-) (limited to 'src/game') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 62ceef8..2ec3c62 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -686,7 +686,7 @@ void Game::func_wingmen(core::Player *player, const std::string &args) return; } - assert(player->control()->moduletype() == ship_enttype); + assert(player->control()->moduletype() == ship_enttype); Ship * ship = static_cast(player->control()); std::istringstream is(args); @@ -714,6 +714,8 @@ void Game::func_wingmen(core::Player *player, const std::string &args) for (int count = 0; count < amount; ++count) { NPC *npc = NPC::add_wingman(ship); if (npc) { + npc->set_commander(player); + if (faction) { faction->apply(npc); player->send(faction->name() + " wingman standing by!"); diff --git a/src/game/base/npc.cc b/src/game/base/npc.cc index 9aff6b2..0dd33c8 100644 --- a/src/game/base/npc.cc +++ b/src/game/base/npc.cc @@ -80,6 +80,7 @@ NPC::NPC(const ShipModel *shipmodel) : Ship(0, shipmodel) npc_patrol = 0; npc_leader = 0; + npc_commander = 0; npc_weapon_range = 0.0f; } @@ -138,6 +139,11 @@ void NPC::set_patrol(Patrol *patrol) npc_patrol = patrol; } +void NPC::set_commander(core::Player *commander) +{ + npc_commander = commander; +} + Ship *NPC::target_closest_enemy() { // scan for enemies @@ -217,17 +223,34 @@ void NPC::frame(const unsigned long elapsed) } } else { - // TODO pilot magic and mood witchcraft - if (leader()) { - + // TODO pilot magic and mood witchcraft + + if (leader()) + { // verify leader still exists - if (!core::Entity::find(leader())) { - + if (!core::Entity::find(leader())) + { set_leader(0); + + // verify the commander hasn't left the game + if (commander()) + { + set_commander(core::game()->find_player(commander())); + + if (commander() && commander()->control() && (commander()->control()->moduletype() == ship_enttype)) + { + set_leader(static_cast(commander()->control())); + } + } + } + + // if the leader has dissapeared, the NPC explodes + if (!leader()) + { explode(); npc_destroyed_timestamp = core::game()->time(); - - } else if (leader()->zone() == zone()) { + + } else if (leader()->zone() == zone()) { // leader is in this zone if (leader()->state() == Docked) { diff --git a/src/game/base/npc.h b/src/game/base/npc.h index 39395c0..411f807 100644 --- a/src/game/base/npc.h +++ b/src/game/base/npc.h @@ -31,7 +31,7 @@ public: /* ---- inspectors ----------------------------------------- */ /** - * @brief returns the general moode of the NPC + * @brief returns the general mood of the NPC * */ inline const Mood mood() const { @@ -39,7 +39,7 @@ public: } /** - * @brief returns this NPC's leader. + * @brief returns this NPC's leader * */ inline Ship *leader() { @@ -47,13 +47,21 @@ public: } /** - * @brief returns this NPC's patrol. + * @brief returns this NPC's patrol * */ inline Patrol *patrol() { return npc_patrol; } + /** + * @brief return this NPC;s fleet commander + * */ + inline core::Player *commander() + { + return npc_commander; + } + /* ---- mutators ------------------------------------------- */ /** @@ -71,6 +79,11 @@ public: * */ void set_patrol(Patrol *patrol); + /** + * @brief set the NPC's fleet commander + * */ + void set_commander(core::Player *player); + /** * @brief game frame * */ @@ -103,6 +116,8 @@ private: float npc_weapon_range; + core::Player *npc_commander; + }; // class NPC } // namespace game -- cgit v1.2.3