A new unified function ThreeStrikesBattle::updateKartNodes() that localizes all karts (AI and player) on the polygon map.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/battleAI@14618 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
nixt 2013-12-03 16:43:07 +00:00
parent 41fa68f948
commit c6cbf487c9
6 changed files with 158 additions and 21 deletions

View File

@ -56,8 +56,6 @@ BattleAI::BattleAI(AbstractKart *kart,
m_track = NULL;
}
updateCurrentNode();
// Don't call our own setControllerName, since this will add a
// billboard showing 'AIBaseController' to the kar.
Controller::setControllerName("BattleAI");
@ -66,9 +64,9 @@ BattleAI::BattleAI(AbstractKart *kart,
void BattleAI::update(float dt)
{
m_controls->m_accel = 0.65f;
m_controls->m_accel = 0.45f;
// m_controls->m_steer = 0;
updateCurrentNode();
// updateCurrentNode();
handleSteering(dt);
}
@ -79,22 +77,13 @@ void BattleAI::reset()
m_current_node = BattleGraph::UNKNOWN_POLY;
}
/*
void BattleAI::updateCurrentNode()
{
//std::cout<<"Current Node \t"<< m_current_node << std::endl;
std::cout<<"Current Node \t"<< m_current_node << std::endl;
// if unknown location, search everywhere
if(m_current_node == BattleGraph::UNKNOWN_POLY)
{
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(m_kart->getXYZ()))
m_current_node = i;
}
return;
}
if(m_current_node != BattleGraph::UNKNOWN_POLY)
{
@ -118,10 +107,28 @@ void BattleAI::updateCurrentNode()
BattleGraph::get()->getPolyOfNode(adjacents[i]);
if(p_temp.pointInPoly(m_kart->getXYZ())) m_current_node = adjacents[i];
}
return;
}
}
if(m_current_node == BattleGraph::UNKNOWN_POLY)
{
int max_count = BattleGraph::get()->getNumNodes();
//float min_dist = 9999.99f;
for(unsigned int i =0; i<max_count; i++)
{
const NavPoly& p = BattleGraph::get()->getPolyOfNode(i);
if((p.pointInPoly(m_kart->getXYZ())))
{
m_current_node = i;
//min_dist = (p.getCenter() - m_kart->getXYZ()).length_2d();
}
}
}
return;
}
*/
void BattleAI::handleSteering(const float dt)
{
@ -134,7 +141,7 @@ void BattleAI::handleSteering(const float dt)
if(p.pointInPoly(kart->getXYZ()))
player_node = i;
}
std::cout<<"PLayer node " << player_node<<" This cpu kart node" << m_current_node<<std::endl;
if(player_node == BattleGraph::UNKNOWN_POLY || m_current_node == BattleGraph::UNKNOWN_POLY) return;
int next_node = BattleGraph::get()->getNextShortestPathPoly(m_current_node, player_node);
if(next_node == -1) return;

View File

@ -48,7 +48,7 @@ private:
int m_current_node;
void updateCurrentNode();
//void updateCurrentNode();
void handleAcceleration(const float dt) {};
void handleSteering(const float dt);
void handleBraking() {};
@ -65,7 +65,8 @@ public:
BattleAI(AbstractKart *kart,
StateManager::ActivePlayer *player=NULL);
//~BattleAI();
unsigned int getCurrentNode() const { return m_current_node; }
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

@ -37,6 +37,7 @@
#include "modes/world.hpp"
#include "race/history.hpp"
#include "states_screens/race_gui_base.hpp"
#include "tracks/battle_graph.hpp"
#include "utils/constants.hpp"
#include "utils/log.hpp"
#include "utils/translation.hpp"
@ -93,6 +94,7 @@ void PlayerController::reset()
m_prev_accel = 0;
m_prev_nitro = false;
m_penalty_time = 0;
m_current_node = BattleGraph::UNKNOWN_POLY;
} // reset
// ----------------------------------------------------------------------------

View File

@ -43,6 +43,9 @@ 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;
@ -64,6 +67,8 @@ 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,6 +26,8 @@
#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/kart_model.hpp"
#include "karts/kart_properties.hpp"
#include "physics/physics.hpp"
@ -298,6 +300,8 @@ void ThreeStrikesBattle::update(float dt)
WorldWithRank::update(dt);
WorldWithRank::updateTrack(dt);
updateKartNodes();
core::vector3df tire_offset;
std::string tire;
float scale = 0.5f;
@ -432,6 +436,121 @@ bool ThreeStrikesBattle::isRaceOver()
return getCurrentNumKarts()==1 || getCurrentNumPlayers()==0;
} // isRaceOver
//-----------------------------------------------------------------------------
/** Updates the m_current_node value of each kart controller to localize it
* on the navigation mesh.
*/
void ThreeStrikesBattle::updateKartNodes()
{
const unsigned int n = getNumKarts();
for(unsigned int i=0; i<n; i++)
{
if(m_karts[i]->isEliminated()) continue;
const AbstractKart* kart = m_karts[i];
if(!kart->getController()->isPlayerController())
{
BattleAI* controller = (BattleAI*)(kart->getController());
if(controller->getCurrentNode()!= BattleGraph::UNKNOWN_POLY)
{
//check if the kart is still on the same node
const NavPoly& p = BattleGraph::get()->getPolyOfNode(controller->getCurrentNode());
if(p.pointInPoly(kart->getXYZ())) return;
//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[i]);
if(p_temp.pointInPoly(kart->getXYZ()))
controller->setCurrentNode(adjacents[i]);
}
}
if(controller->getCurrentNode() == BattleGraph::UNKNOWN_POLY)
{
unsigned int max_count = BattleGraph::get()->getNumNodes();
//float min_dist = 9999.99f;
for(unsigned int i =0; i<max_count; i++)
{
const NavPoly& p = BattleGraph::get()->getPolyOfNode(i);
if((p.pointInPoly(kart->getXYZ())))
{
controller->setCurrentNode(i);
//m_current_node = i;
//min_dist = (p.getCenter() - m_kart->getXYZ()).length_2d();
}
}
}
}
else
{
PlayerController* controller = (PlayerController*)(kart->getController());
if(controller->getCurrentNode()!= BattleGraph::UNKNOWN_POLY)
{
//check if the kart is still on the same node
const NavPoly& p = BattleGraph::get()->getPolyOfNode(controller->getCurrentNode());
if(p.pointInPoly(kart->getXYZ())) return;
//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[i]);
if(p_temp.pointInPoly(kart->getXYZ()))
controller->setCurrentNode(adjacents[i]);
}
}
if(controller->getCurrentNode() == BattleGraph::UNKNOWN_POLY)
{
unsigned int max_count = BattleGraph::get()->getNumNodes();
//float min_dist = 9999.99f;
for(unsigned int i =0; i<max_count; i++)
{
const NavPoly& p = BattleGraph::get()->getPolyOfNode(i);
if((p.pointInPoly(kart->getXYZ())))
{
controller->setCurrentNode(i);
//m_current_node = i;
//min_dist = (p.getCenter() - m_kart->getXYZ()).length_2d();
}
}
}
}
}
}
//-----------------------------------------------------------------------------
/** 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,

View File

@ -70,6 +70,9 @@ private:
PtrVector<TrackObject, REF> m_tires;
/** Function to udpate the locations of all karts on the polygon map */
void updateKartNodes();
public:
/** Used to show a nice graph when battle is over */