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/core/gameinterface.cc | 77 +++++++++++++++++++++++++++++++++++------------ src/core/gameinterface.h | 3 ++ src/game/base/game.cc | 4 ++- src/game/base/npc.cc | 37 ++++++++++++++++++----- src/game/base/npc.h | 21 +++++++++++-- 5 files changed, 111 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/core/gameinterface.cc b/src/core/gameinterface.cc index aa4ef4c..8ecd697 100644 --- a/src/core/gameinterface.cc +++ b/src/core/gameinterface.cc @@ -26,9 +26,11 @@ namespace core void func_list_players(std::string const &args) { - if (args.size()) { + if (args.size()) + { Player *player = game()->find_player(args); - if (!player) { + if (!player) + { con_print << "^BPlayer '" + args + "^B' not found"; return; } else { @@ -44,9 +46,11 @@ Player GameInterface::game_localplayer(0); EntityControlable *localcontrol() { if (game()->localplayer()) + { return game()->localplayer()->control(); - else + } else { return 0; + } } Player *localplayer() @@ -59,7 +63,8 @@ GameInterface::GameInterface() clear(); game_localplayer.clear(); - if (Cvar::sv_dedicated->value()) { + if (Cvar::sv_dedicated->value()) + { game_localplayer.player_name.assign("Console"); } else { game_localplayer.player_name.assign("Player"); @@ -94,7 +99,8 @@ GameInterface::~GameInterface() clear(); // delete vertex array - if (game_vertexarray) { + if (game_vertexarray) + { delete game_vertexarray; game_vertexarray = 0; } @@ -119,9 +125,11 @@ void GameInterface::clear() model::Material::clear(); // clear player list - for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) { + for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) + { Player *player = (*it); - if (player != localplayer()) { + if (player != localplayer()) + { delete player; } } @@ -129,8 +137,10 @@ void GameInterface::clear() game_playerlist_timestamp = 0; // remove all game functions - for (Func::Registry::iterator it = Func::registry().begin(); it != Func::registry().end();) { - if (((*it).second->flags() & Func::Game) == Func::Game) { + for (Func::Registry::iterator it = Func::registry().begin(); it != Func::registry().end();) + { + if (((*it).second->flags() & Func::Game) == Func::Game) + { delete(*it).second; Func::registry().erase(it++); } else { @@ -139,8 +149,10 @@ void GameInterface::clear() } // remove all game cvars - for (Cvar::Registry::iterator it = Cvar::registry().begin(); it != Cvar::registry().end();) { - if (((*it).second->flags() & Cvar::Game) == Cvar::Game) { + for (Cvar::Registry::iterator it = Cvar::registry().begin(); it != Cvar::registry().end();) + { + if (((*it).second->flags() & Cvar::Game) == Cvar::Game) + { delete(*it).second; Cvar::registry().erase(it++); } else { @@ -172,40 +184,64 @@ void GameInterface::set_playerlist_timestamp(const unsigned long timestamp) game_playerlist_timestamp = ( timestamp > 0 ? timestamp : 1); } -void GameInterface::abort() { +void GameInterface::abort() +{ game_running = false; } Player *GameInterface::find_player(const int playerid) { - if (playerid == 0) { + if (playerid == 0) + { return 0; } - for (std::list:: iterator it = game_players.begin(); it != game_players.end(); it++) { - if ((*it)->id() == playerid) { + for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) + { + if ((*it)->id() == playerid) + { return (*it); } } return 0; } +Player *GameInterface::find_player(const Player *player) +{ + if (player) + { + for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) + { + if ((*it) == player) + { + return (*it); + } + } + } + return 0; +} + Player *GameInterface::find_player(const std::string &search) { std::istringstream searchstr(search); int id = 0; if (searchstr >> id) { - for (std::list:: iterator it = game_players.begin(); it != game_players.end(); it++) { - if ((*it)->id() == id) { + for (Players:: iterator it = game_players.begin(); it != game_players.end(); it++) + { + if ((*it)->id() == id) + { return (*it); } } } if (search.size() < 3) + { return 0; + } - for (std::list:: iterator it = game_players.begin(); it != game_players.end(); it++) { + for (Players:: iterator it = game_players.begin(); it != game_players.end(); it++) + { if (aux::text_strip_lowercase((*it)->name()).find(aux::lowercase(search)) != std::string::npos) return (*it); } @@ -217,7 +253,8 @@ void GameInterface::list_players() { int count = 0; - for (Players::iterator it = game_players.begin(); it != game_players.end(); it++) { + for (Players::const_iterator it = game_players.begin(); it != game_players.end(); it++) + { const Player *player = (*it); con_print << " " << std::setfill(' ') << aux::pad_left(player->name(), 24) << "^N " @@ -235,4 +272,4 @@ void GameInterface::list_players() con_print << count << " connected " << aux::plural("player", count) << std::endl; } -} +} // namespace core diff --git a/src/core/gameinterface.h b/src/core/gameinterface.h index f118ad3..f204c04 100644 --- a/src/core/gameinterface.h +++ b/src/core/gameinterface.h @@ -52,6 +52,9 @@ public: /// find a player id Player *find_player(const int playerid); + + /// find a player + Player *find_player(const Player *player); /// show a list of connected players void list_players(); 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