SuperTuxKart
Classes | Public Member Functions | Protected Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
TestAI Class Reference

This is a test version of the AI, which can be used to create new AIs, and compare them with the current AI. More...

#include <test_ai.hpp>

Inheritance diagram for TestAI:
Inheritance graph
[legend]

Classes

class  CrashTypes
 

Public Member Functions

 TestAI (AbstractKart *kart)
 
virtual void update (int ticks)
 Updates the ai base controller each time step. More...
 
virtual void reset ()
 
virtual const irr::core::stringw & getNamePostfix () const
 
- Public Member Functions inherited from AIBaseLapController
 AIBaseLapController (AbstractKart *kart)
 This is the base class for all AIs. More...
 
- 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 unsigned int getNextSector (unsigned int index)
 Returns the next sector of the given sector index. More...
 
- Protected Member Functions inherited from AIBaseLapController
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...
 

Private Types

enum  { SKID_PROBAB_NOT_YET, SKID_PROBAB_NO_SKID, SKID_PROBAB_SKID }
 This implements a simple finite state machine: it starts in NOT_YET. More...
 
enum  { PSA_DEFAULT, PSA_FIXED, PSA_NEW }
 Determines the algorithm to use to select the point-to-aim-for There are three different Point Selection Algorithms: More...
 

Private Member Functions

void handleRaceStart ()
 
void handleAcceleration (int ticks)
 
void handleSteering (float dt)
 
void handleItems (const float dt)
 
void handleRescue (const float dt)
 
void handleBraking ()
 
void handleNitroAndZipper ()
 
void computeNearestKarts ()
 
void handleItemCollectionAndAvoidance (Vec3 *aim_point, int last_node)
 
bool handleSelectedItem (Vec3 kart_aim_direction, Vec3 *aim_point)
 
bool steerToAvoid (const std::vector< const ItemState * > &items_to_avoid, const core::line3df &line_to_target, Vec3 *aim_point)
 
bool hitBadItemWhenAimAt (const ItemState *item, const std::vector< const ItemState * > &items_to_avoid)
 
void evaluateItems (const ItemState *item, Vec3 kart_aim_direction, std::vector< const ItemState * > *items_to_avoid, std::vector< const ItemState * > *items_to_collect)
 
void checkCrashes (const Vec3 &pos)
 
void findNonCrashingPointFixed (Vec3 *result, int *last_node)
 
void findNonCrashingPointNew (Vec3 *result, int *last_node)
 
void findNonCrashingPoint (Vec3 *result, int *last_node)
 
void determineTrackDirection ()
 
virtual bool canSkid (float steer_fraction)
 Return true if AI can skid now. More...
 
virtual void setSteering (float angle, float dt)
 Converts the steering angle to a lr steering in the range of -1 to 1. More...
 
void handleCurve ()
 

Private Attributes

class TestAI::CrashTypes m_crashes
 
RaceManager::AISuperPower m_superpower
 
AbstractKartm_kart_ahead
 Pointer to the closest kart ahead of this kart. More...
 
float m_distance_ahead
 Distance to the kart ahead. More...
 
AbstractKartm_kart_behind
 Pointer to the closest kart behind this kart. More...
 
float m_distance_behind
 Distance to the kard behind. More...
 
int m_start_delay
 The actual start delay used in ticks. More...
 
float m_time_since_last_shot
 Time an item has been collected and not used. More...
 
float m_time_since_stuck
 
int m_start_kart_crash_direction
 Direction of crash: -1 = left, 1 = right, 0 = no crash. More...
 
DriveNode::DirectionType m_current_track_direction
 The direction of the track where the kart is on atm. More...
 
float m_current_curve_radius
 The radius of the curve the kart is currently driving. More...
 
Vec3 m_curve_center
 Stores the center of the curve (if the kart is in a curve, otherwise undefined). More...
 
unsigned int m_last_direction_node
 The index of the last node with the same direction as the current node the kart is on. More...
 
const ItemStatem_item_to_collect
 If set an item that the AI should aim for. More...
 
bool m_avoid_item_close
 True if items to avoid are close by. More...
 
float m_distance_to_player
 Distance to the player, used for rubber-banding. More...
 
RandomGenerator m_random_skid
 A random number generator to decide if the AI should skid or not. More...
 
enum TestAI:: { ... }  m_skid_probability_state
 This implements a simple finite state machine: it starts in NOT_YET. More...
 
const ItemStatem_last_item_random
 The last item selected for collection, for which a probability was determined. More...
 
bool m_really_collect_item
 True if m_last_item_random was randomly selected to be collected. More...
 
RandomGenerator m_random_collect_item
 A random number generator for collecting items. More...
 
enum TestAI:: { ... }  m_point_selection_algorithm
 Determines the algorithm to use to select the point-to-aim-for There are three different Point Selection Algorithms: More...
 

Additional Inherited Members

- Static Public Member Functions inherited from AIBaseController
static void enableDebug ()
 
static void setTestAI (int n)
 
static int getTestAI ()
 
- Protected Attributes inherited from AIBaseLapController
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...
 
- 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

This is a test version of the AI, which can be used to create new AIs, and compare them with the current AI.

It is otherwise (at this stage) identical to the Skidding AI.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private

This implements a simple finite state machine: it starts in NOT_YET.

The first time the AI decides to skid, the state is changed randomly (depending on skid probability) to NO_SKID or SKID. As long as the AI keeps on deciding to skid, the state remains unchanged (so no new random decision is made) till it decides not to skid. In which case the state is set to NOT_YET again. This guarantees that for each 'skidable' section of the track the random decision is only done once.

◆ anonymous enum

anonymous enum
private

Determines the algorithm to use to select the point-to-aim-for There are three different Point Selection Algorithms:

  1. findNonCrashingPoint() is the default (which is actually slightly buggy, but so far best one after handling of 90 degree turns was added).
  2. findNonCrashingPointFixed() which fixes the bugs of the default algorithm.
  3. findNonCrashingPointNew() A newly designed algorithm, which is faster than the standard one, but does not give as good results as the 'buggy' one.

So far the default one has by far the best performance, even though it has bugs.

Member Function Documentation

◆ canSkid()

virtual bool TestAI::canSkid ( float  steer_fraction)
privatevirtual

Return true if AI can skid now.

Implements AIBaseController.

◆ getNextSector()

virtual unsigned int TestAI::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 from AIBaseLapController.

◆ setSteering()

virtual void TestAI::setSteering ( float  angle,
float  dt 
)
privatevirtual

Converts the steering angle to a lr steering in the range of -1 to 1.

If the steering angle is too great, it will also trigger skidding. This function uses a 'time till full steer' value specifying the time it takes for the wheel to reach full left/right steering similar to player karts when using a digital input device. The parameter is defined in the kart properties and helps somewhat to make AI karts more 'pushable' (since otherwise the karts counter-steer to fast). It also takes the effect of a plunger into account by restricting the actual steer angle to 50% of the maximum.

Parameters
angleSteering angle.
dtTime step.

Reimplemented from AIBaseController.

◆ update()

virtual void TestAI::update ( int  ticks)
virtual

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 AIBaseLapController.

Member Data Documentation

◆ m_avoid_item_close

bool TestAI::m_avoid_item_close
private

True if items to avoid are close by.

Used to avoid using zippers (which would make it more difficult to avoid items).

◆ m_current_curve_radius

float TestAI::m_current_curve_radius
private

The radius of the curve the kart is currently driving.

Undefined when being on a straigt section.

◆ m_current_track_direction

DriveNode::DirectionType TestAI::m_current_track_direction
private

The direction of the track where the kart is on atm.

◆ m_curve_center

Vec3 TestAI::m_curve_center
private

Stores the center of the curve (if the kart is in a curve, otherwise undefined).

◆ m_distance_ahead

float TestAI::m_distance_ahead
private

Distance to the kart ahead.

◆ m_distance_behind

float TestAI::m_distance_behind
private

Distance to the kard behind.

◆ m_distance_to_player

float TestAI::m_distance_to_player
private

Distance to the player, used for rubber-banding.

◆ m_item_to_collect

const ItemState* TestAI::m_item_to_collect
private

If set an item that the AI should aim for.

◆ m_kart_ahead

AbstractKart* TestAI::m_kart_ahead
private

Pointer to the closest kart ahead of this kart.

NULL if this kart is first.

◆ m_kart_behind

AbstractKart* TestAI::m_kart_behind
private

Pointer to the closest kart behind this kart.

NULL if this kart is last.

◆ m_last_direction_node

unsigned int TestAI::m_last_direction_node
private

The index of the last node with the same direction as the current node the kart is on.

If kart is in a left turn, this will be the last node that is still turning left etc.

◆ m_last_item_random

const ItemState* TestAI::m_last_item_random
private

The last item selected for collection, for which a probability was determined.

◆ m_point_selection_algorithm

enum { ... } TestAI::m_point_selection_algorithm

Determines the algorithm to use to select the point-to-aim-for There are three different Point Selection Algorithms:

  1. findNonCrashingPoint() is the default (which is actually slightly buggy, but so far best one after handling of 90 degree turns was added).
  2. findNonCrashingPointFixed() which fixes the bugs of the default algorithm.
  3. findNonCrashingPointNew() A newly designed algorithm, which is faster than the standard one, but does not give as good results as the 'buggy' one.

So far the default one has by far the best performance, even though it has bugs.

◆ m_random_collect_item

RandomGenerator TestAI::m_random_collect_item
private

A random number generator for collecting items.

◆ m_random_skid

RandomGenerator TestAI::m_random_skid
private

A random number generator to decide if the AI should skid or not.

◆ m_really_collect_item

bool TestAI::m_really_collect_item
private

True if m_last_item_random was randomly selected to be collected.

◆ m_skid_probability_state

enum { ... } TestAI::m_skid_probability_state

This implements a simple finite state machine: it starts in NOT_YET.

The first time the AI decides to skid, the state is changed randomly (depending on skid probability) to NO_SKID or SKID. As long as the AI keeps on deciding to skid, the state remains unchanged (so no new random decision is made) till it decides not to skid. In which case the state is set to NOT_YET again. This guarantees that for each 'skidable' section of the track the random decision is only done once.

◆ m_start_delay

int TestAI::m_start_delay
private

The actual start delay used in ticks.

◆ m_start_kart_crash_direction

int TestAI::m_start_kart_crash_direction
private

Direction of crash: -1 = left, 1 = right, 0 = no crash.

◆ m_time_since_last_shot

float TestAI::m_time_since_last_shot
private

Time an item has been collected and not used.


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