Cleanup controller, plus better logic in updateKartNodes

This commit is contained in:
Benau 2016-01-04 14:28:06 +08:00
parent 0393eef825
commit 87df259f48
7 changed files with 65 additions and 119 deletions

View File

@ -23,10 +23,6 @@
#include "states_screens/state_manager.hpp"
class AIProperties;
class LinearWorld;
class ThreeStrikesBattle;
class QuadGraph;
class BattleGraph;
class Track;
class Vec3;

View File

@ -33,8 +33,7 @@
#include "karts/rescue_animation.hpp"
#include "karts/skidding.hpp"
#include "modes/three_strikes_battle.hpp"
#include "tracks/nav_poly.hpp"
#include "tracks/navmesh.hpp"
#include "tracks/battle_graph.hpp"
#include "utils/log.hpp"
#ifdef AI_DEBUG
@ -295,19 +294,9 @@ void BattleAI::findClosestKart(bool difficulty)
}
const AbstractKart* closest_kart = m_world->getKart(closest_kart_num);
if (!closest_kart->getController()->isPlayerController())
{
BattleAI* controller = (BattleAI*)(closest_kart->getController());
m_closest_kart_node = controller->getCurrentNode();
m_closest_kart_point = closest_kart->getXYZ();
}
else if (closest_kart->getController()->isPlayerController())
{
PlayerController* controller = (PlayerController*)(closest_kart->getController());
m_closest_kart_node = controller->getCurrentNode();
m_closest_kart_point = closest_kart->getXYZ();
}
Controller* controller = (Controller*)(closest_kart->getController());
m_closest_kart_node = controller->getCurrentNode();
m_closest_kart_point = closest_kart->getXYZ();
if (!difficulty)
{

View File

@ -28,13 +28,9 @@
#include "karts/controller/ai_base_controller.hpp"
#include "race/race_manager.hpp"
#include "tracks/battle_graph.hpp"
#include "utils/random_generator.hpp"
class AIProperties;
class ThreeStrikesBattle;
class BattleGraph;
class Track;
class Vec3;
class Item;
@ -56,11 +52,6 @@ private:
* turning when steering is overridden. */
bool m_adjusting_side;
/** Holds the current position of the AI on the battle graph. Sets to
* BattleGraph::UNKNOWN_POLY if the location is unknown. This variable is
* updated in ThreeStrikesBattle::updateKartNodes(). */
int m_current_node;
int m_closest_kart_node;
Vec3 m_closest_kart_point;
@ -149,8 +140,6 @@ public:
BattleAI(AbstractKart *kart,
StateManager::ActivePlayer *player=NULL);
~BattleAI();
unsigned int getCurrentNode() const { return m_current_node; }
void setCurrentNode(int i) { m_current_node = i; }
virtual void update (float delta);
virtual void reset ();

View File

@ -29,9 +29,10 @@
*/
Controller::Controller(AbstractKart *kart, StateManager::ActivePlayer *player)
{
m_controls = &(kart->getControls());
m_kart = kart;
m_player = player;
m_controls = &(kart->getControls());
m_kart = kart;
m_player = player;
m_current_node = -1;
setControllerName("Controller");
} // Controller

View File

@ -57,6 +57,10 @@ 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);
@ -104,6 +108,12 @@ 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

View File

@ -44,9 +44,6 @@ private:
float m_penalty_time;
/** This variable is required for battle mode **/
int m_current_node;
/** The camera attached to the kart for this controller. The camera
* object is managed in the Camera class, so no need to free it. */
Camera *m_camera;
@ -68,8 +65,6 @@ public:
void handleZipper (bool play_sound);
void collectedItem (const Item &item, int add_info=-1,
float previous_energy=0);
unsigned int getCurrentNode() const { return m_current_node; }
void setCurrentNode(int i) { m_current_node = i; }
virtual void skidBonusTriggered();
virtual void setPosition (int p);
virtual bool isPlayerController() const {return true;}

View File

@ -26,8 +26,7 @@
#include "graphics/irr_driver.hpp"
#include "io/file_manager.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/controller/battle_ai.hpp"
#include "karts/controller/player_controller.hpp"
#include "karts/controller/controller.hpp"
#include "karts/kart_model.hpp"
#include "karts/kart_properties.hpp"
#include "physics/physics.hpp"
@ -450,106 +449,73 @@ void ThreeStrikesBattle::updateKartNodes()
if (isRaceOver()) return;
const unsigned int n = getNumKarts();
for(unsigned int i=0; i<n; i++)
for (unsigned int i = 0; i < n; i++)
{
if(m_karts[i]->isEliminated()) continue;
if (m_karts[i]->isEliminated()) continue;
const AbstractKart* kart = m_karts[i];
Controller* controller = (Controller*)kart->getController();
const int saved_current_node = controller->getCurrentNode();
if(!kart->getController()->isPlayerController())
if (saved_current_node == BattleGraph::UNKNOWN_POLY)
{
BattleAI* controller = (BattleAI*)(kart->getController());
int saved_current_node = controller->getCurrentNode();
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())
{
//check if the kart is still on the same node
const NavPoly& p = BattleGraph::get()->getPolyOfNode(controller->getCurrentNode());
if(p.pointInPoly(kart->getXYZ())) continue;
//if not then check all adjacent polys
const std::vector<int>& adjacents =
NavMesh::get()->getAdjacentPolys(controller->getCurrentNode());
// Set m_current_node to unknown so that if no adjacent poly checks true
// we look everywhere the next time updateCurrentNode is called. This is
// useful in cases when you are "teleported" to some other poly, ex. rescue
controller->setCurrentNode(BattleGraph::UNKNOWN_POLY);
for(unsigned int i=0; i<adjacents.size(); i++)
const NavPoly& p_all = BattleGraph::get()->getPolyOfNode(num);
if ((p_all.pointInPoly(kart->getXYZ())))
{
const NavPoly& p_temp =
BattleGraph::get()->getPolyOfNode(adjacents[i]);
if(p_temp.pointInPoly(kart->getXYZ()))
controller->setCurrentNode(adjacents[i]);
controller->setCurrentNode(num);
found = true;
}
num++;
}
//Current node is still unkown
if (saved_current_node == BattleGraph::UNKNOWN_POLY)
{
bool flag = 0;
unsigned int max_count = BattleGraph::get()->getNumNodes();
for(unsigned int i=0; i<max_count; i++)
{
const NavPoly& p = BattleGraph::get()->getPolyOfNode(i);
if((p.pointInPoly(kart->getXYZ())))
{
controller->setCurrentNode(i);
flag = 1;
}
}
if(flag == 0) controller->setCurrentNode(saved_current_node);
}
}
else
{
PlayerController* controller = (PlayerController*)(kart->getController());
// 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;
int saved_current_node = controller->getCurrentNode();
// If not then check all adjacent polys
const std::vector<int>& adjacents = NavMesh::get()
->getAdjacentPolys(saved_current_node);
if (saved_current_node != BattleGraph::UNKNOWN_POLY)
// Set current node to unknown so that if no adjacent polygons,
// 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);
bool found = false;
unsigned int num = 0;
while (!found && num < adjacents.size())
{
//check if the kart is still on the same node
const NavPoly& p = BattleGraph::get()->getPolyOfNode(controller->getCurrentNode());
if(p.pointInPoly(kart->getXYZ())) continue;
//if not then check all adjacent polys
const std::vector<int>& adjacents =
NavMesh::get()->getAdjacentPolys(controller->getCurrentNode());
// Set m_current_node to unknown so that if no adjacent poly checks true
// we look everywhere the next time updateCurrentNode is called. This is
// useful in cases when you are "teleported" to some other poly, ex. rescue
controller->setCurrentNode(BattleGraph::UNKNOWN_POLY);
for(unsigned int i=0; i<adjacents.size(); i++)
const NavPoly& p_temp =
BattleGraph::get()->getPolyOfNode(adjacents[num]);
if (p_temp.pointInPoly(kart->getXYZ()))
{
const NavPoly& p_temp =
BattleGraph::get()->getPolyOfNode(adjacents[i]);
if(p_temp.pointInPoly(kart->getXYZ()))
controller->setCurrentNode(adjacents[i]);
controller->setCurrentNode(adjacents[num]);
found = true;
}
num++;
}
if (saved_current_node == BattleGraph::UNKNOWN_POLY)
// Current node is still unkown
if (controller->getCurrentNode() == BattleGraph::UNKNOWN_POLY)
{
bool flag = 0;
unsigned int max_count = BattleGraph::get()->getNumNodes();
for(unsigned int i =0; i<max_count; i++)
{
const NavPoly& p = BattleGraph::get()->getPolyOfNode(i);
if((p.pointInPoly(kart->getXYZ())))
{
controller->setCurrentNode(i);
flag = 1;
}
}
// 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();
if(flag == 0) controller->setCurrentNode(saved_current_node);
if (dist < 3.0f)
controller->setCurrentNode(saved_current_node);
}
}
}