SuperTuxKart
skidding_ai.hpp
1 
2 //
3 // SuperTuxKart - a fun racing game with go-kart
4 // Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net>
5 // Copyright (C) 2006-2015 Eduardo Hernandez Munoz
6 // Copyright (C) 2010-2015 Joerg Henrichs
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 3
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 #ifndef HEADER_SKIDDING_AI_HPP
23 #define HEADER_SKIDDING_AI_HPP
24 
25 // Some debugging features for the AI. For example you can visualise the
26 // point the AI is aiming at, or visualise the curve the AI is predicting.
27 // It works best with just 1 AI kart, so set the number of karts
28 // to 2 in main.cpp with quickstart and run supertuxkart with the arg -N.
29 // Or use --profile-laps=99 and run just one AI. Using the debug camera
30 // (top view) is useful, too
31 
32 #ifdef DEBUG
33  // Enable AI graphical debugging
34 # undef AI_DEBUG
35  // Shows left and right lines when using new findNonCrashing function
36 # undef AI_DEBUG_NEW_FIND_NON_CRASHING
37  // Show the predicted turn circles
38 # undef AI_DEBUG_CIRCLES
39  // Show the heading of the kart
40 # undef AI_DEBUG_KART_HEADING
41  // Shows line from kart to its aim point
42 # undef AI_DEBUG_KART_AIM
43 #endif
44 
45 
46 #include "karts/controller/ai_base_lap_controller.hpp"
47 #include "race/race_manager.hpp"
48 #include "tracks/drive_node.hpp"
49 #include "utils/random_generator.hpp"
50 
51 #include <line3d.h>
52 
53 class ItemManager;
54 class ItemState;
55 class LinearWorld;
56 class Track;
57 
58 namespace irr
59 {
60  namespace scene
61  {
62  class ISceneNode;
63  }
64 }
65 
117 {
118 private:
119 
121  {
122  public:
123 
124  bool m_road; //true if we are going to 'crash' with the bounds of the road
125  int m_kart; //-1 if no crash, pos numbers are the kart it crashes with
126  CrashTypes() : m_road(false), m_kart(-1) {};
127  void clear() {m_road = false; m_kart = -1;}
128  } m_crashes;
129 
130  RaceManager::AISuperPower m_superpower;
131 
132  /*General purpose variables*/
133 
137 
140 
144 
147 
152 
153 
156 
159 
160  float m_time_since_stuck;
161 
164 
167 
171 
175 
179  unsigned int m_last_direction_node;
180 
183 
187 
190 
193 
195  bool m_burster;
196 
199 
208  enum {SKID_PROBAB_NOT_YET, SKID_PROBAB_NO_SKID, SKID_PROBAB_SKID}
210 
212  enum SkillType {ITEM_SKILL, NITRO_SKILL};
213 
217 
220 
223 
235  enum {PSA_DEFAULT, PSA_NEW}
237 
238  ItemManager* m_item_manager;
239 #ifdef AI_DEBUG
241  ShowCurve **m_curve;
242 
245  irr::scene::ISceneNode *m_debug_sphere[4];
246 
249  irr::scene::ISceneNode *m_item_sphere;
250 #endif
251 
252 
253  /*Functions called directly from update(). They all represent an action
254  *that can be done, and end up setting their respective m_controls
255  *variable, except handle_race_start() that isn't associated with any
256  *specific action (more like, associated with inaction).
257  */
258  void handleRaceStart();
259  void handleAccelerationAndBraking(int ticks);
260  void handleSteering(float dt);
261  int computeSkill(SkillType type);
262  void handleItems(const float dt, const Vec3 *aim_point,
263  int last_node, int item_skill);
264  void handleBubblegum(int item_skill,
265  const std::vector<const ItemState *> &items_to_collect,
266  const std::vector<const ItemState *> &items_to_avoid);
267  void handleCake(int item_skill);
268  void handleBowling(int item_skill);
269  void handleSwatter(int item_skill);
270  void handleSwitch(int item_skill,
271  const std::vector<const ItemState *> &items_to_collect,
272  const std::vector<const ItemState *> &items_to_avoid);
273  void handleRescue(const float dt);
274  void handleBraking(float max_turn_speed, float min_speed);
275  void handleNitroAndZipper(float max_safe_speed);
276  void computeNearestKarts();
277  void handleItemCollectionAndAvoidance(Vec3 *aim_point,
278  int last_node);
279  bool handleSelectedItem(Vec3 kart_aim_direction, Vec3 *aim_point);
280  bool steerToAvoid(const std::vector<const ItemState *> &items_to_avoid,
281  const core::line3df &line_to_target,
282  Vec3 *aim_point);
283  bool hitBadItemWhenAimAt(const ItemState *item,
284  const std::vector<const ItemState *> &items_to_avoid);
285  void evaluateItems(const ItemState *item, Vec3 kart_aim_direction,
286  std::vector<const ItemState *> *items_to_avoid,
287  std::vector<const ItemState *> *items_to_collect);
288 
289  void checkCrashes(const Vec3& pos);
290  void findNonCrashingPointNew(Vec3 *result, int *last_node);
291  void findNonCrashingPoint(Vec3 *result, int *last_node);
292 
294  virtual bool canSkid(float steer_fraction);
295  virtual void setSteering(float angle, float dt);
296  void handleCurve();
297 
298 protected:
299  virtual unsigned int getNextSector(unsigned int index);
300 
301 public:
302  SkiddingAI(AbstractKart *kart);
303  ~SkiddingAI();
304  virtual void update (int ticks);
305  virtual void reset ();
306  virtual const irr::core::stringw& getNamePostfix() const;
307 };
308 
309 #endif
310 
311 /* EOF */
A base class for all AI karts.
Definition: ai_base_lap_controller.hpp:33
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:62
DirectionType
To indiciate in which direction the track is going: straight, left, right.
Definition: drive_node.hpp:37
Definition: item_manager.hpp:47
Contains the state information of an item, i.e.
Definition: item.hpp:53
Definition: linear_world.hpp:36
AISuperPower
True if the AI should have additional abbilities, e.g.
Definition: race_manager.hpp:135
A random number generator.
Definition: random_generator.hpp:33
This class is used for debugging.
Definition: show_curve.hpp:46
Definition: skidding_ai.hpp:121
This is the actual racing AI.
Definition: skidding_ai.hpp:117
RandomGenerator m_random_collect_item
A random number generator for collecting items.
Definition: skidding_ai.hpp:222
const ItemState * m_item_to_collect
If set an item that the AI should aim for.
Definition: skidding_ai.hpp:182
void determineTrackDirection()
Determines the direction of the track ahead of the kart: 0 indicates straight, +1 right turn,...
Definition: skidding_ai.cpp:2780
void handleSteering(float dt)
Decides in which direction to steer.
Definition: skidding_ai.cpp:370
void handleAccelerationAndBraking(int ticks)
Determines if the AI should accelerate or not, and if not if it should brake.
Definition: skidding_ai.cpp:1989
void evaluateItems(const ItemState *item, Vec3 kart_aim_direction, std::vector< const ItemState * > *items_to_avoid, std::vector< const ItemState * > *items_to_collect)
This subroutine decides if the specified item should be collected, avoided, or ignored.
Definition: skidding_ai.cpp:974
bool handleSelectedItem(Vec3 kart_aim_direction, Vec3 *aim_point)
This function is called when the AI is trying to hit an item that is pre-selected to be collected.
Definition: skidding_ai.cpp:786
bool m_really_collect_item
True if m_last_item_random was randomly selected to be collected.
Definition: skidding_ai.hpp:219
virtual bool canSkid(float steer_fraction)
Determines if the kart should skid.
Definition: skidding_ai.cpp:2899
unsigned int m_last_direction_node
The index of the last node with the same direction as the current node the kart is on.
Definition: skidding_ai.hpp:179
DriveNode::DirectionType m_current_track_direction
The direction of the track where the kart is on atm.
Definition: skidding_ai.hpp:166
float m_distance_leader
Distance to the leader kart (used only in FTL) If this kart is leader, contains a high value to avoid...
Definition: skidding_ai.hpp:151
virtual void setSteering(float angle, float dt)
Converts the steering angle to a lr steering in the range of -1 to 1.
Definition: skidding_ai.cpp:3029
void handleBraking(float max_turn_speed, float min_speed)
This function decides if the AI should brake.
Definition: skidding_ai.cpp:2069
void handleCurve()
If the kart is at/in a curve, determine the turn radius.
Definition: skidding_ai.cpp:2837
AbstractKart * m_kart_ahead
Pointer to the closest kart ahead of this kart.
Definition: skidding_ai.hpp:136
virtual void reset()
Resets the AI when a race is restarted.
Definition: skidding_ai.cpp:162
void findNonCrashingPointNew(Vec3 *result, int *last_node)
This is a new version of findNonCrashingPoint, which at this stage is slightly inferior (though faste...
Definition: skidding_ai.cpp:2584
void findNonCrashingPoint(Vec3 *result, int *last_node)
This is basically the original AI algorithm.
Definition: skidding_ai.cpp:2695
void handleSwatter(int item_skill)
Handle the swatter depending on the chosen strategy Level 2 : Use the swatter immediately after a wai...
Definition: skidding_ai.cpp:1681
void handleSwitch(int item_skill, const std::vector< const ItemState * > &items_to_collect, const std::vector< const ItemState * > &items_to_avoid)
Handle switch depending on the chosen strategy Level 2 : Use the switch after a wait time Level 3 : S...
Definition: skidding_ai.cpp:1742
void handleBubblegum(int item_skill, const std::vector< const ItemState * > &items_to_collect, const std::vector< const ItemState * > &items_to_avoid)
Handle bubblegum depending on the chosen strategy Level 2 : Use the shield immediately after a wait t...
Definition: skidding_ai.cpp:1299
enum SkiddingAI::@8 m_skid_probability_state
This implements a simple finite state machine: it starts in NOT_YET.
~SkiddingAI()
Destructor, mostly to clean up debug data structures.
Definition: skidding_ai.cpp:145
int computeSkill(SkillType type)
Returns the AI skill value used by the kart.
Definition: skidding_ai.cpp:2394
AbstractKart * m_kart_behind
Pointer to the closest kart behind this kart.
Definition: skidding_ai.hpp:143
Vec3 m_curve_center
Stores the center of the curve (if the kart is in a curve, otherwise undefined).
Definition: skidding_ai.hpp:174
float m_time_since_last_shot
Time an item has been collected and not used.
Definition: skidding_ai.hpp:158
float m_distance_to_player
Distance to the player, used for rubber-banding.
Definition: skidding_ai.hpp:189
bool steerToAvoid(const std::vector< const ItemState * > &items_to_avoid, const core::line3df &line_to_target, Vec3 *aim_point)
Decides if steering is necessary to avoid bad items.
Definition: skidding_ai.cpp:821
void handleNitroAndZipper(float max_safe_speed)
Decides wether to use nitro and zipper or not.
Definition: skidding_ai.cpp:2193
bool m_avoid_item_close
True if items to avoid are close by.
Definition: skidding_ai.hpp:186
void computeNearestKarts()
Determines the closest karts just behind and in front of this kart.
Definition: skidding_ai.cpp:1869
void handleItemCollectionAndAvoidance(Vec3 *aim_point, int last_node)
Decides if the currently selected aim at point (as determined by handleSteering) should be changed in...
Definition: skidding_ai.cpp:554
void handleBowling(int item_skill)
Handle the bowling ball depending on the chosen strategy Level 2 : Use the bowling ball against enemi...
Definition: skidding_ai.cpp:1600
int m_start_kart_crash_direction
Direction of crash: -1 = left, 1 = right, 0 = no crash.
Definition: skidding_ai.hpp:163
float m_current_curve_radius
The radius of the curve the kart is currently driving.
Definition: skidding_ai.hpp:170
virtual void update(int ticks)
This is the main entry point for the AI.
Definition: skidding_ai.cpp:225
int m_start_delay
The actual start delay used in ticks.
Definition: skidding_ai.hpp:155
int m_num_players_ahead
Number of players ahead, used for rubber-banding.
Definition: skidding_ai.hpp:192
enum SkiddingAI::@9 m_point_selection_algorithm
Determines the algorithm to use to select the point-to-aim-for There are two different Point Selectio...
RandomGenerator m_random_skid
A random number generator to decide if the AI should skid or not.
Definition: skidding_ai.hpp:198
float m_distance_behind
Distance to the kard behind.
Definition: skidding_ai.hpp:146
virtual unsigned int getNextSector(unsigned int index)
Returns the pre-computed successor of a graph node.
Definition: skidding_ai.cpp:215
void handleItems(const float dt, const Vec3 *aim_point, int last_node, int item_skill)
Handle all items depending on the chosen strategy.
Definition: skidding_ai.cpp:1081
const ItemState * m_last_item_random
The last item selected for collection, for which a probability was determined.
Definition: skidding_ai.hpp:216
void handleCake(int item_skill)
Handle cake depending on the chosen strategy Level 2 : Use the cake against any close vulnerable enem...
Definition: skidding_ai.cpp:1462
bool m_burster
This bool allows to make the AI use nitro by series of two bursts.
Definition: skidding_ai.hpp:195
float m_distance_ahead
Distance to the kart ahead.
Definition: skidding_ai.hpp:139
virtual const irr::core::stringw & getNamePostfix() const
Returns a name for the AI.
Definition: skidding_ai.cpp:203
SkillType
This is used by computeSkill to know what skill is used.
Definition: skidding_ai.hpp:212
bool hitBadItemWhenAimAt(const ItemState *item, const std::vector< const ItemState * > &items_to_avoid)
Returns true if the AI would hit any of the listed bad items when trying to drive towards the specifi...
Definition: skidding_ai.cpp:761
Definition: track.hpp:115
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35