From 347c608867175686ba6eb30b6bb52c600cde1041 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 5 Jan 2016 13:15:50 +0800 Subject: [PATCH] Final clean, put every kart location in three strikes battle world --- src/karts/controller/battle_ai.cpp | 19 +++++----- src/karts/controller/controller.cpp | 7 ++-- src/karts/controller/controller.hpp | 9 ----- src/karts/controller/player_controller.cpp | 1 - src/modes/three_strikes_battle.cpp | 44 ++++++++++++---------- src/modes/three_strikes_battle.hpp | 4 +- 6 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/karts/controller/battle_ai.cpp b/src/karts/controller/battle_ai.cpp index ab334809d..cc189186e 100644 --- a/src/karts/controller/battle_ai.cpp +++ b/src/karts/controller/battle_ai.cpp @@ -95,7 +95,6 @@ BattleAI::~BattleAI() */ void BattleAI::reset() { - m_current_node = BattleGraph::UNKNOWN_POLY; m_target_node = BattleGraph::UNKNOWN_POLY; m_adjusting_side = false; m_closest_kart = NULL; @@ -201,7 +200,7 @@ void BattleAI::checkIfStuck(const float dt) m_time_since_driving = 0.0f; } - m_on_node.insert(m_current_node); + m_on_node.insert(m_world->getKartNode(m_kart->getWorldKartId())); m_time_since_driving += dt; if ((m_time_since_driving >= @@ -294,8 +293,7 @@ void BattleAI::findClosestKart(bool difficulty) } const AbstractKart* closest_kart = m_world->getKart(closest_kart_num); - Controller* controller = (Controller*)(closest_kart->getController()); - m_closest_kart_node = controller->getCurrentNode(); + m_closest_kart_node = m_world->getKartNode(closest_kart_num); m_closest_kart_point = closest_kart->getXYZ(); if (!difficulty) @@ -373,7 +371,9 @@ void BattleAI::handleUTurn(const float dt) */ void BattleAI::handleSteering(const float dt) { - if (m_current_node == BattleGraph::UNKNOWN_POLY || + const int current_node = m_world->getKartNode(m_kart->getWorldKartId()); + + if (current_node == BattleGraph::UNKNOWN_POLY || m_target_node == BattleGraph::UNKNOWN_POLY) return; if (m_is_steering_overridden) @@ -393,7 +393,7 @@ void BattleAI::handleSteering(const float dt) return; } - if (m_target_node == m_current_node) + if (m_target_node == current_node) { // Very close to the item, steer directly checkPosition(m_target_point, &m_cur_kart_pos_data); @@ -413,9 +413,9 @@ void BattleAI::handleSteering(const float dt) return; } - else if (m_target_node != m_current_node) + else if (m_target_node != current_node) { - findPortals(m_current_node, m_target_node); + findPortals(current_node, m_target_node); stringPull(m_kart->getXYZ(), m_target_point); if (m_path_corners.size() > 0) m_target_point = m_path_corners[0]; @@ -632,7 +632,8 @@ void BattleAI::handleBraking() { m_controls->m_brake = false; - if (m_current_node == BattleGraph::UNKNOWN_POLY || + if (m_world->getKartNode(m_kart->getWorldKartId()) + == BattleGraph::UNKNOWN_POLY || m_target_node == BattleGraph::UNKNOWN_POLY || m_is_steering_overridden) return; diff --git a/src/karts/controller/controller.cpp b/src/karts/controller/controller.cpp index 005302679..1f9d8f71f 100644 --- a/src/karts/controller/controller.cpp +++ b/src/karts/controller/controller.cpp @@ -29,10 +29,9 @@ */ Controller::Controller(AbstractKart *kart, StateManager::ActivePlayer *player) { - m_controls = &(kart->getControls()); - m_kart = kart; - m_player = player; - m_current_node = -1; + m_controls = &(kart->getControls()); + m_kart = kart; + m_player = player; setControllerName("Controller"); } // Controller diff --git a/src/karts/controller/controller.hpp b/src/karts/controller/controller.hpp index 34b4e3777..fe1b29f3d 100644 --- a/src/karts/controller/controller.hpp +++ b/src/karts/controller/controller.hpp @@ -58,9 +58,6 @@ protected: /** The name of the controller, mainly used for debugging purposes. */ std::string m_controller_name; - /** This variable is required for used in arena **/ - int m_current_node; - public: Controller (AbstractKart *kart, StateManager::ActivePlayer *player=NULL); @@ -108,12 +105,6 @@ public: /** Get a pointer on the kart controls. */ virtual KartControl* getControls() { return m_controls; } // ------------------------------------------------------------------------ - /** For arena only, get the current node in arena. */ - int getCurrentNode() const { return m_current_node; } - // ------------------------------------------------------------------------ - /** For arena only, set the current node in arena. */ - void setCurrentNode(int i) { m_current_node = i; } - // ------------------------------------------------------------------------ }; // Controller #endif diff --git a/src/karts/controller/player_controller.cpp b/src/karts/controller/player_controller.cpp index 8272f4cc0..8ea4f3c6f 100644 --- a/src/karts/controller/player_controller.cpp +++ b/src/karts/controller/player_controller.cpp @@ -95,7 +95,6 @@ void PlayerController::reset() m_prev_nitro = false; m_sound_schedule = false; m_penalty_time = 0; - m_current_node = BattleGraph::UNKNOWN_POLY; } // reset // ---------------------------------------------------------------------------- diff --git a/src/modes/three_strikes_battle.cpp b/src/modes/three_strikes_battle.cpp index f2b0f0525..c0a0ff434 100644 --- a/src/modes/three_strikes_battle.cpp +++ b/src/modes/three_strikes_battle.cpp @@ -26,7 +26,6 @@ #include "graphics/irr_driver.hpp" #include "io/file_manager.hpp" #include "karts/abstract_kart.hpp" -#include "karts/controller/controller.hpp" #include "karts/kart_model.hpp" #include "karts/kart_properties.hpp" #include "physics/physics.hpp" @@ -86,7 +85,8 @@ void ThreeStrikesBattle::reset() for(unsigned int n=0; nsetPosition(-1); @@ -441,7 +441,7 @@ bool ThreeStrikesBattle::isRaceOver() } // isRaceOver //----------------------------------------------------------------------------- -/** Updates the m_current_node value of each kart controller to localize it +/** Updates the m_on_node value of each kart to localize it * on the navigation mesh. */ void ThreeStrikesBattle::updateKartNodes() @@ -453,24 +453,22 @@ void ThreeStrikesBattle::updateKartNodes() { if (m_karts[i]->isEliminated()) continue; - const AbstractKart* kart = m_karts[i]; - Controller* controller = (Controller*)kart->getController(); - const int saved_current_node = controller->getCurrentNode(); + const int saved_current_node = m_kart_info[i].m_on_node; if (saved_current_node == BattleGraph::UNKNOWN_POLY) { // Try all nodes in the battle graph bool found = false; - unsigned int num = 0; - while (!found && num < BattleGraph::get()->getNumNodes()) + unsigned int node = 0; + while (!found && node < BattleGraph::get()->getNumNodes()) { - const NavPoly& p_all = BattleGraph::get()->getPolyOfNode(num); - if ((p_all.pointInPoly(kart->getXYZ()))) + const NavPoly& p_all = BattleGraph::get()->getPolyOfNode(node); + if ((p_all.pointInPoly(m_karts[i]->getXYZ()))) { - controller->setCurrentNode(num); + m_kart_info[i].m_on_node = node; found = true; } - num++; + node++; } } else @@ -478,7 +476,7 @@ void ThreeStrikesBattle::updateKartNodes() // Check if the kart is still on the same node const NavPoly& p_cur = BattleGraph::get() ->getPolyOfNode(saved_current_node); - if (p_cur.pointInPoly(kart->getXYZ())) continue; + if (p_cur.pointInPoly(m_karts[i]->getXYZ())) continue; // If not then check all adjacent polys const std::vector& adjacents = NavMesh::get() @@ -488,7 +486,7 @@ void ThreeStrikesBattle::updateKartNodes() // we look everywhere the next time updateKartNodes is called. // This is useful in cases when you are "teleported" // to some other polygons, ex. rescue - controller->setCurrentNode(BattleGraph::UNKNOWN_POLY); + m_kart_info[i].m_on_node = BattleGraph::UNKNOWN_POLY; bool found = false; unsigned int num = 0; @@ -496,31 +494,39 @@ void ThreeStrikesBattle::updateKartNodes() { const NavPoly& p_temp = BattleGraph::get()->getPolyOfNode(adjacents[num]); - if (p_temp.pointInPoly(kart->getXYZ())) + if (p_temp.pointInPoly(m_karts[i]->getXYZ())) { - controller->setCurrentNode(adjacents[num]); + m_kart_info[i].m_on_node = adjacents[num]; found = true; } num++; } // Current node is still unkown - if (controller->getCurrentNode() == BattleGraph::UNKNOWN_POLY) + if (m_kart_info[i].m_on_node == BattleGraph::UNKNOWN_POLY) { // Calculated distance from saved node to current position, // if it's close enough than use the saved node anyway, it // may happen when the kart stays on the edge of obstacles const NavPoly& p = BattleGraph::get() ->getPolyOfNode(saved_current_node); - const float dist = (p.getCenter() - kart->getXYZ()).length_2d(); + const float dist = (p.getCenter() - m_karts[i]->getXYZ()).length_2d(); if (dist < 3.0f) - controller->setCurrentNode(saved_current_node); + m_kart_info[i].m_on_node = saved_current_node; } } } } +//----------------------------------------------------------------------------- +/** Get the which node the kart located in navigation mesh. + */ +int ThreeStrikesBattle::getKartNode(unsigned int kart_id) const +{ + return m_kart_info[kart_id].m_on_node; +} // getKartNode + //----------------------------------------------------------------------------- /** Called when the race finishes, i.e. after playing (if necessary) an * end of race animation. It updates the time for all karts still racing, diff --git a/src/modes/three_strikes_battle.hpp b/src/modes/three_strikes_battle.hpp index c82d48a27..04e6ced8e 100644 --- a/src/modes/three_strikes_battle.hpp +++ b/src/modes/three_strikes_battle.hpp @@ -39,7 +39,8 @@ class ThreeStrikesBattle : public WorldWithRank private: struct BattleInfo { - int m_lives; + int m_lives; + int m_on_node; }; /** This vector contains an 'BattleInfo' struct for every kart in the race. @@ -108,6 +109,7 @@ public: virtual void kartAdded(AbstractKart* kart, scene::ISceneNode* node); + int getKartNode(unsigned int kart_id) const; void updateKartRanks(); }; // ThreeStrikesBattles