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 ItemState;
54 class LinearWorld;
55 class Track;
56 
57 namespace irr
58 {
59  namespace scene
60  {
61  class ISceneNode;
62  }
63 }
64 
116 {
117 private:
118 
120  {
121  public:
122 
123  bool m_road; //true if we are going to 'crash' with the bounds of the road
124  int m_kart; //-1 if no crash, pos numbers are the kart it crashes with
125  CrashTypes() : m_road(false), m_kart(-1) {};
126  void clear() {m_road = false; m_kart = -1;}
127  } m_crashes;
128 
129  RaceManager::AISuperPower m_superpower;
130 
131  /*General purpose variables*/
132 
136 
139 
143 
146 
151 
152 
155 
158 
159  float m_time_since_stuck;
160 
163 
166 
170 
174 
178  unsigned int m_last_direction_node;
179 
182 
186 
189 
192 
194  bool m_burster;
195 
198 
207  enum {SKID_PROBAB_NOT_YET, SKID_PROBAB_NO_SKID, SKID_PROBAB_SKID}
209 
211  enum SkillType {ITEM_SKILL, NITRO_SKILL};
212 
216 
219 
222 
234  enum {PSA_DEFAULT, PSA_NEW}
236 
237 #ifdef AI_DEBUG
238 
239  ShowCurve **m_curve;
240 
243  irr::scene::ISceneNode *m_debug_sphere[4];
244 
247  irr::scene::ISceneNode *m_item_sphere;
248 #endif
249 
250 
251  /*Functions called directly from update(). They all represent an action
252  *that can be done, and end up setting their respective m_controls
253  *variable, except handle_race_start() that isn't associated with any
254  *specific action (more like, associated with inaction).
255  */
256  void handleRaceStart();
257  void handleAccelerationAndBraking(int ticks);
258  void handleSteering(float dt);
259  int computeSkill(SkillType type);
260  void handleItems(const float dt, const Vec3 *aim_point,
261  int last_node, int item_skill);
262  void handleBubblegum(int item_skill,
263  const std::vector<const ItemState *> &items_to_collect,
264  const std::vector<const ItemState *> &items_to_avoid);
265  void handleCake(int item_skill);
266  void handleBowling(int item_skill);
267  void handleSwatter(int item_skill);
268  void handleSwitch(int item_skill,
269  const std::vector<const ItemState *> &items_to_collect,
270  const std::vector<const ItemState *> &items_to_avoid);
271  void handleRescue(const float dt);
272  void handleBraking(float max_turn_speed, float min_speed);
273  void handleNitroAndZipper(float max_safe_speed);
274  void computeNearestKarts();
275  void handleItemCollectionAndAvoidance(Vec3 *aim_point,
276  int last_node);
277  bool handleSelectedItem(Vec3 kart_aim_direction, Vec3 *aim_point);
278  bool steerToAvoid(const std::vector<const ItemState *> &items_to_avoid,
279  const core::line3df &line_to_target,
280  Vec3 *aim_point);
281  bool hitBadItemWhenAimAt(const ItemState *item,
282  const std::vector<const ItemState *> &items_to_avoid);
283  void evaluateItems(const ItemState *item, Vec3 kart_aim_direction,
284  std::vector<const ItemState *> *items_to_avoid,
285  std::vector<const ItemState *> *items_to_collect);
286 
287  void checkCrashes(const Vec3& pos);
288  void findNonCrashingPointNew(Vec3 *result, int *last_node);
289  void findNonCrashingPoint(Vec3 *result, int *last_node);
290 
292  virtual bool canSkid(float steer_fraction);
293  virtual void setSteering(float angle, float dt);
294  void handleCurve();
295 
296 protected:
297  virtual unsigned int getNextSector(unsigned int index);
298 
299 public:
300  SkiddingAI(AbstractKart *kart);
301  ~SkiddingAI();
302  virtual void update (int ticks);
303  virtual void reset ();
304  virtual const irr::core::stringw& getNamePostfix() const;
305 };
306 
307 #endif
308 
309 /* EOF */
AbstractKart * m_kart_behind
Pointer to the closest kart behind this kart.
Definition: skidding_ai.hpp:142
A random number generator.
Definition: random_generator.hpp:33
bool m_avoid_item_close
True if items to avoid are close by.
Definition: skidding_ai.hpp:185
void handleNitroAndZipper(float max_safe_speed)
Decides wether to use nitro and zipper or not.
Definition: skidding_ai.cpp:2181
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
void handleAccelerationAndBraking(int ticks)
Determines if the AI should accelerate or not, and if not if it should brake.
Definition: skidding_ai.cpp:1977
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:1594
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:150
DirectionType
To indiciate in which direction the track is going: straight, left, right.
Definition: drive_node.hpp:37
virtual unsigned int getNextSector(unsigned int index)
Returns the pre-computed successor of a graph node.
Definition: skidding_ai.cpp:212
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:1736
bool m_really_collect_item
True if m_last_item_random was randomly selected to be collected.
Definition: skidding_ai.hpp:218
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:758
enum SkiddingAI::@9 m_skid_probability_state
This implements a simple finite state machine: it starts in NOT_YET.
RandomGenerator m_random_skid
A random number generator to decide if the AI should skid or not.
Definition: skidding_ai.hpp:197
virtual void reset()
Resets the AI when a race is restarted.
Definition: skidding_ai.cpp:159
virtual const irr::core::stringw & getNamePostfix() const
Returns a name for the AI.
Definition: skidding_ai.cpp:200
DriveNode::DirectionType m_current_track_direction
The direction of the track where the kart is on atm.
Definition: skidding_ai.hpp:165
const ItemState * m_last_item_random
The last item selected for collection, for which a probability was determined.
Definition: skidding_ai.hpp:215
float m_distance_ahead
Distance to the kart ahead.
Definition: skidding_ai.hpp:138
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:971
Definition: skidding_ai.hpp:119
void determineTrackDirection()
Determines the direction of the track ahead of the kart: 0 indicates straight, +1 right turn,...
Definition: skidding_ai.cpp:2764
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:783
void handleCurve()
If the kart is at/in a curve, determine the turn radius.
Definition: skidding_ai.cpp:2821
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:1078
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:1675
void handleSteering(float dt)
Decides in which direction to steer.
Definition: skidding_ai.cpp:367
int m_start_delay
The actual start delay used in ticks.
Definition: skidding_ai.hpp:154
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:2568
Vec3 m_curve_center
Stores the center of the curve (if the kart is in a curve, otherwise undefined).
Definition: skidding_ai.hpp:173
float m_distance_to_player
Distance to the player, used for rubber-banding.
Definition: skidding_ai.hpp:188
const ItemState * m_item_to_collect
If set an item that the AI should aim for.
Definition: skidding_ai.hpp:181
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:178
This class is used for debugging.
Definition: show_curve.hpp:45
SkillType
This is used by computeSkill to know what skill is used.
Definition: skidding_ai.hpp:211
bool m_burster
This bool allows to make the AI use nitro by series of two bursts.
Definition: skidding_ai.hpp:194
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:551
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:3013
int m_start_kart_crash_direction
Direction of crash: -1 = left, 1 = right, 0 = no crash.
Definition: skidding_ai.hpp:162
void computeNearestKarts()
Determines the closest karts just behind and in front of this kart.
Definition: skidding_ai.cpp:1863
AbstractKart * m_kart_ahead
Pointer to the closest kart ahead of this kart.
Definition: skidding_ai.hpp:135
float m_time_since_last_shot
Time an item has been collected and not used.
Definition: skidding_ai.hpp:157
void handleBraking(float max_turn_speed, float min_speed)
This function decides if the AI should brake.
Definition: skidding_ai.cpp:2057
virtual bool canSkid(float steer_fraction)
Determines if the kart should skid.
Definition: skidding_ai.cpp:2883
~SkiddingAI()
Destructor, mostly to clean up debug data structures.
Definition: skidding_ai.cpp:142
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:1456
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:818
AISuperPower
True if the AI should have additional abbilities, e.g.
Definition: race_manager.hpp:130
A base class for all AI karts.
Definition: ai_base_lap_controller.hpp:32
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:1293
enum SkiddingAI::@10 m_point_selection_algorithm
Determines the algorithm to use to select the point-to-aim-for There are two different Point Selectio...
virtual void update(int ticks)
This is the main entry point for the AI.
Definition: skidding_ai.cpp:222
float m_current_curve_radius
The radius of the curve the kart is currently driving.
Definition: skidding_ai.hpp:169
Definition: linear_world.hpp:35
int m_num_players_ahead
Number of players ahead, used for rubber-banding.
Definition: skidding_ai.hpp:191
void findNonCrashingPoint(Vec3 *result, int *last_node)
This is basically the original AI algorithm.
Definition: skidding_ai.cpp:2679
Contains the state information of an item, i.e.
Definition: item.hpp:52
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
Definition: track.hpp:96
float m_distance_behind
Distance to the kard behind.
Definition: skidding_ai.hpp:145
int computeSkill(SkillType type)
Returns the AI skill value used by the kart.
Definition: skidding_ai.cpp:2382
RandomGenerator m_random_collect_item
A random number generator for collecting items.
Definition: skidding_ai.hpp:221
This is the actual racing AI.
Definition: skidding_ai.hpp:115