SuperTuxKart
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
AIBaseLapController Class Reference

A base class for all AI karts. More...

#include <ai_base_lap_controller.hpp>

Inheritance diagram for AIBaseLapController:
Inheritance graph
[legend]

Public Member Functions

 AIBaseLapController (AbstractKart *kart)
 This is the base class for all AIs. More...
 
virtual void reset ()
 
- Public Member Functions inherited from AIBaseController
 AIBaseController (AbstractKart *kart)
 
virtual bool disableSlipstreamBonus () const OVERRIDE
 Certain AI levels will not receive a slipstream bonus in order to be not as hard.
 
virtual void crashed (const Material *m) OVERRIDE
 This is called when the kart crashed with the terrain. More...
 
virtual void crashed (const AbstractKart *k) OVERRIDE
 
virtual void handleZipper (bool play_sound) OVERRIDE
 
virtual void finishedRace (float time) OVERRIDE
 Called whan this controller's kart finishes the last lap. More...
 
virtual void collectedItem (const ItemState &item, float previous_energy=0) OVERRIDE
 
virtual void setPosition (int p) OVERRIDE
 
virtual bool isPlayerController () const OVERRIDE
 This function checks if this player is not an AI, i.e. More...
 
virtual bool isLocalPlayerController () const OVERRIDE
 This function checks if this is a local player. More...
 
virtual bool action (PlayerAction action, int value, bool dry_run=false) OVERRIDE
 Default: ignore actions. More...
 
virtual void skidBonusTriggered () OVERRIDE
 
virtual bool saveState (BareNetworkString *buffer) const OVERRIDE
 
virtual void rewindTo (BareNetworkString *buffer) OVERRIDE
 
void setNetworkAI (bool val)
 
- Public Member Functions inherited from Controller
 Controller (AbstractKart *kart)
 Constructor, saves the kart pointer and a pointer to the KartControl of the kart.
 
const std::string & getControllerName () const
 Returns the name of this controller. More...
 
virtual KartControlgetControls ()
 Get a pointer on the kart controls. More...
 
void setControls (KartControl *kc)
 
virtual bool canGetAchievements () const
 Only local players can get achievements. More...
 
virtual core::stringw getName (bool include_handicap_string=true) const
 Display name of the controller. More...
 
AbstractKartgetKart () const
 Returns the kart controlled by this controller. More...
 

Protected Member Functions

virtual void update (int ticks)
 Updates the ai base controller each time step. More...
 
virtual unsigned int getNextSector (unsigned int index)
 Returns the next sector of the given sector index. More...
 
virtual void newLap (int lap)
 Triggers a recomputation of the path to use, so that the AI does not always use the same way.
 
float steerToAngle (const unsigned int sector, const float angle)
 This function steers towards a given angle. More...
 
void computePath ()
 Computes a path for the AI to follow. More...
 
virtual void raceFinished ()
 Nothing special to do when the race is finished. More...
 
- Protected Member Functions inherited from AIBaseController
void setControllerName (const std::string &name) OVERRIDE
 In debug mode when the user specified –ai-debug on the command line set the name of the controller as on-screen text, so that the different AI controllers can be distinguished. More...
 
float steerToPoint (const Vec3 &point)
 Computes the steering angle to reach a certain point. More...
 
float normalizeAngle (float angle)
 Normalises an angle to be between -pi and _ pi. More...
 
bool isStuck () const
 This can be called to detect if the kart is stuck (i.e. More...
 
void determineTurnRadius (const Vec3 &end, Vec3 *center, float *radius) const
 Determine the center point and radius of a circle given two points on the circle and the tangent at the first point. More...
 
virtual void setSteering (float angle, float dt)
 Converts the steering angle to a lr steering in the range of -1 to 1. More...
 
virtual bool canSkid (float steer_fraction)=0
 Return true if AI can skid now. More...
 

Protected Attributes

int m_track_node
 The current node the kart is on. More...
 
LinearWorldm_world
 Keep a pointer to world. More...
 
std::vector< int > m_successor_index
 Which of the successors of a node was selected by the AI. More...
 
std::vector< int > m_next_node_index
 For each node in the graph this list contains the chosen next node. More...
 
std::vector< std::vector< int > > m_all_look_aheads
 For each graph node this list contains a list of the next X graph nodes. More...
 
- Protected Attributes inherited from AIBaseController
bool m_enabled_network_ai
 
float m_kart_length
 Length of the kart, storing it here saves many function calls. More...
 
float m_kart_width
 Cache width of kart. More...
 
Trackm_track
 Keep a pointer to the track to reduce calls.
 
const AIPropertiesm_ai_properties
 A pointer to the AI properties for this kart. More...
 
- Protected Attributes inherited from Controller
AbstractKartm_kart
 Pointer to the kart that is controlled by this controller. More...
 
KartControlm_controls
 A pointer to the main controller, from which the kart takes it commands. More...
 
std::string m_controller_name
 The name of the controller, mainly used for debugging purposes. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from AIBaseController
static void enableDebug ()
 
static void setTestAI (int n)
 
static int getTestAI ()
 
- Static Protected Attributes inherited from AIBaseController
static bool m_ai_debug = false
 
static int m_test_ai = 0
 Stores the '–test-ai=n' command line parameter: It indicates which fraction of the AIs are going to be the test AI: 1 means only to use the TestAI, 2 means every second AI will be test etc. More...
 

Detailed Description

A base class for all AI karts.

This class basically provides some common low level functions.

Constructor & Destructor Documentation

◆ AIBaseLapController()

AIBaseLapController::AIBaseLapController ( AbstractKart kart)

This is the base class for all AIs.

At this stage there are two similar AIs: one is the SkiddingAI, which is the AI used in lap based races (including follow-the-leader mode), the other one is the end controller, I.e. the controller that takes over from a player (or AI) when the race is finished.

This base class defines some basic operations:

  • It takes care on which part of the DriveGraph the AI currently is.
  • It determines which path the AI should take (in case of shortcuts or forks in the road).

At race start and every time a new lap is started, the AI will compute the path the kart is taking this lap (computePath). At this stage the decision which road in case of shortcut to take is purely random. It stores the information in two arrays: m_successor_index[i] stores which successor to take from node i. The successor is a number between 0 and number_of_successors - 1. m_next_node_index[i] stores the actual index of the graph node that follows after node i. Depending on operation one of the other data is more useful, so this class stores both information to avoid looking it up over and over. Once this is done (still in computePath), the array m_all_look_aheads is computed. This array stores for each quad a list of the next (atm) 10 quads. This is used when the AI is selecting where to drive next, and it will just pass the list of next quads to findRoadSector.

Note that the quad graph information is stored for every quad in the quad graph, even if the quad is not on the path chosen. This is necessary since it can happen that a kart ends up on a path not choses (e.g. perhaps it was pushed on that part, or couldn't get a sharp corner).

In update(), which gets called one per frame per AI, this object will determine the quad the kart is currently on (which is then used to determine where the kart will be driving to). This uses the m_all_look_aheads to speed up this process (since the kart is likely to be either on the same quad as it was before, or the next quad in the m_all_look_aheads list).

It will also check if the kart is stuck: this is done by maintaining a list of times when the kart hits the track. If (atm) more than 3 collisions happen in 1.5 seconds, the kart is considered stuck and will trigger a rescue (due to the pushback from the track it will take some time if a kart is really stuck before it will hit the track again).

This base class also contains some convenience functions which are useful in all AIs, e.g.:

  • steerToPoint: determine the steering angle to use depending on the current location and the point the kart is driving to.
  • normalizeAngle: To normalise the steering angle to be in [-PI,PI].
  • setSteering: Converts the steering angle into a steering fraction in [-1,1].

Member Function Documentation

◆ computePath()

void AIBaseLapController::computePath ( )
protected

Computes a path for the AI to follow.

This function is called at race start and every time a new lap is started. Recomputing the path every time will mean that the kart will not always take the same path, but (potentially) vary from lap to lap. At this stage the decision is done randomly. The AI could be improved by collecting more information about each branch of a track, and selecting the 'appropriate' one (e.g. if the AI is far ahead, chose a longer/slower path).

◆ getNextSector()

unsigned int AIBaseLapController::getNextSector ( unsigned int  index)
protectedvirtual

Returns the next sector of the given sector index.

This is used for branches in the quad graph to select which way the AI kart should go. This is a very simple implementation that always returns the first successor, but it can be overridden to allow a better selection.

Parameters
indexIndex of the graph node for which the successor is searched.
Returns
Returns the successor of this graph node.

Reimplemented in SkiddingAI, and TestAI.

◆ raceFinished()

virtual void AIBaseLapController::raceFinished ( )
inlineprotectedvirtual

Nothing special to do when the race is finished.

◆ steerToAngle()

float AIBaseLapController::steerToAngle ( const unsigned int  sector,
const float  add_angle 
)
protected

This function steers towards a given angle.

It also takes a plunger attached to this kart into account by modifying the actual steer angle somewhat to simulate driving without seeing.

◆ update()

void AIBaseLapController::update ( int  ticks)
protectedvirtual

Updates the ai base controller each time step.

Note that any calls to isStuck() must be done before update is called, since update will call AIBaseController::update() which will reset the isStuck flag!

Parameters
ticksNumber of physics time steps - should be 1.

Reimplemented from AIBaseController.

Reimplemented in SkiddingAI, TestAI, and EndController.

Member Data Documentation

◆ m_all_look_aheads

std::vector<std::vector<int> > AIBaseLapController::m_all_look_aheads
protected

For each graph node this list contains a list of the next X graph nodes.

◆ m_next_node_index

std::vector<int> AIBaseLapController::m_next_node_index
protected

For each node in the graph this list contains the chosen next node.

For normal lap track without branches we always have m_next_node_index[i] = (i+1) % size; but if a branch is possible, the AI will select one option here. If the node is not used, m_next_node_index will be -1.

◆ m_successor_index

std::vector<int> AIBaseLapController::m_successor_index
protected

Which of the successors of a node was selected by the AI.

◆ m_track_node

int AIBaseLapController::m_track_node
protected

The current node the kart is on.

This can be different from the value in LinearWorld, since it takes the chosen path of the AI into account (e.g. the closest point in LinearWorld might be on a branch not chosen by the AI).

◆ m_world

LinearWorld* AIBaseLapController::m_world
protected

Keep a pointer to world.


The documentation for this class was generated from the following files: