SuperTuxKart
test_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_TEST_AI_HPP
23 #define HEADER_TEST_AI_HPP
24 
25 
26 // Some debugging features for the AI. For example you can visualise the
27 // point the AI is aiming at, or visualise the curve the AI is predicting.
28 // It works best with just 1 AI kart, so set the number of karts
29 // to 2 in main.cpp with quickstart and run supertuxkart with the arg -N.
30 // Or use --profile-laps=99 and run just one AI. Using the debug camera
31 // (top view) is useful, too
32 
33 #ifdef DEBUG
34  // Enable AI graphical debugging
35 # undef AI_DEBUG
36  // Shows left and right lines when using new findNonCrashing function
37 # undef AI_DEBUG_NEW_FIND_NON_CRASHING
38  // Show the predicted turn circles
39 # undef AI_DEBUG_CIRCLES
40  // Show the heading of the kart
41 # undef AI_DEBUG_KART_HEADING
42  // Shows line from kart to its aim point
43 # undef AI_DEBUG_KART_AIM
44 #endif
45 
46 
47 #include "karts/controller/ai_base_lap_controller.hpp"
48 #include "race/race_manager.hpp"
49 #include "tracks/drive_node.hpp"
50 #include "utils/random_generator.hpp"
51 
52 #include <line3d.h>
53 
54 class ItemState;
55 
56 #ifdef AI_DEBUG
57  class ShowCurve;
58 
59 namespace irr
60 {
61  namespace scene
62  {
63  class ISceneNode;
64  }
65 }
66 #endif
67 
75 {
76 private:
77 
78  class CrashTypes
79  {
80  public:
81 
82  bool m_road; //true if we are going to 'crash' with the bounds of the road
83  int m_kart; //-1 if no crash, pos numbers are the kart it crashes with
84  CrashTypes() : m_road(false), m_kart(-1) {};
85  void clear() {m_road = false; m_kart = -1;}
86  } m_crashes;
87 
88  RaceManager::AISuperPower m_superpower;
89 
90  /*General purpose variables*/
91 
95 
98 
102 
105 
108 
111 
112  float m_time_since_stuck;
113 
116 
119 
123 
127 
131  unsigned int m_last_direction_node;
132 
135 
139 
142 
145 
154  enum {SKID_PROBAB_NOT_YET, SKID_PROBAB_NO_SKID, SKID_PROBAB_SKID}
156 
160 
163 
166 
180  enum {PSA_DEFAULT, PSA_FIXED, PSA_NEW}
182 
183 #ifdef AI_DEBUG
184 
185  ShowCurve **m_curve;
186 
189  irr::scene::ISceneNode *m_debug_sphere[4];
190 
193  irr::scene::ISceneNode *m_item_sphere;
194 #endif
195 
196 
197  /*Functions called directly from update(). They all represent an action
198  *that can be done, and end up setting their respective m_controls
199  *variable, except handle_race_start() that isn't associated with any
200  *specific action (more like, associated with inaction).
201  */
202  void handleRaceStart();
203  void handleAcceleration(int ticks);
204  void handleSteering(float dt);
205  void handleItems(const float dt);
206  void handleRescue(const float dt);
207  void handleBraking();
208  void handleNitroAndZipper();
209  void computeNearestKarts();
210  void handleItemCollectionAndAvoidance(Vec3 *aim_point,
211  int last_node);
212  bool handleSelectedItem(Vec3 kart_aim_direction, Vec3 *aim_point);
213  bool steerToAvoid(const std::vector<const ItemState *> &items_to_avoid,
214  const core::line3df &line_to_target,
215  Vec3 *aim_point);
216  bool hitBadItemWhenAimAt(const ItemState *item,
217  const std::vector<const ItemState *> &items_to_avoid);
218  void evaluateItems(const ItemState *item, Vec3 kart_aim_direction,
219  std::vector<const ItemState *> *items_to_avoid,
220  std::vector<const ItemState *> *items_to_collect);
221 
222  void checkCrashes(const Vec3& pos);
223  void findNonCrashingPointFixed(Vec3 *result, int *last_node);
224  void findNonCrashingPointNew(Vec3 *result, int *last_node);
225  void findNonCrashingPoint(Vec3 *result, int *last_node);
226 
227  void determineTrackDirection();
228  virtual bool canSkid(float steer_fraction);
229  virtual void setSteering(float angle, float dt);
230  void handleCurve();
231 
232 protected:
233  virtual unsigned int getNextSector(unsigned int index);
234 
235 public:
236  TestAI(AbstractKart *kart);
237  ~TestAI();
238  virtual void update (int ticks);
239  virtual void reset ();
240  virtual const irr::core::stringw& getNamePostfix() const;
241 };
242 
243 #endif
244 
245 /* EOF */
A random number generator.
Definition: random_generator.hpp:33
enum TestAI::@12 m_point_selection_algorithm
Determines the algorithm to use to select the point-to-aim-for There are three different Point Select...
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: test_ai.hpp:131
float m_distance_ahead
Distance to the kart ahead.
Definition: test_ai.hpp:97
int m_start_kart_crash_direction
Direction of crash: -1 = left, 1 = right, 0 = no crash.
Definition: test_ai.hpp:115
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
bool m_really_collect_item
True if m_last_item_random was randomly selected to be collected.
Definition: test_ai.hpp:162
DirectionType
To indiciate in which direction the track is going: straight, left, right.
Definition: drive_node.hpp:37
float m_time_since_last_shot
Time an item has been collected and not used.
Definition: test_ai.hpp:110
DriveNode::DirectionType m_current_track_direction
The direction of the track where the kart is on atm.
Definition: test_ai.hpp:118
AbstractKart * m_kart_ahead
Pointer to the closest kart ahead of this kart.
Definition: test_ai.hpp:94
virtual void update(int ticks)
Updates the ai base controller each time step.
int m_start_delay
The actual start delay used in ticks.
Definition: test_ai.hpp:107
bool m_avoid_item_close
True if items to avoid are close by.
Definition: test_ai.hpp:138
This is a test version of the AI, which can be used to create new AIs, and compare them with the curr...
Definition: test_ai.hpp:74
float m_distance_behind
Distance to the kard behind.
Definition: test_ai.hpp:104
AbstractKart * m_kart_behind
Pointer to the closest kart behind this kart.
Definition: test_ai.hpp:101
This class is used for debugging.
Definition: show_curve.hpp:45
virtual unsigned int getNextSector(unsigned int index)
Returns the next sector of the given sector index.
float m_distance_to_player
Distance to the player, used for rubber-banding.
Definition: test_ai.hpp:141
virtual void setSteering(float angle, float dt)
Converts the steering angle to a lr steering in the range of -1 to 1.
RandomGenerator m_random_skid
A random number generator to decide if the AI should skid or not.
Definition: test_ai.hpp:144
Vec3 m_curve_center
Stores the center of the curve (if the kart is in a curve, otherwise undefined).
Definition: test_ai.hpp:126
AISuperPower
True if the AI should have additional abbilities, e.g.
Definition: race_manager.hpp:130
const ItemState * m_item_to_collect
If set an item that the AI should aim for.
Definition: test_ai.hpp:134
enum TestAI::@11 m_skid_probability_state
This implements a simple finite state machine: it starts in NOT_YET.
RandomGenerator m_random_collect_item
A random number generator for collecting items.
Definition: test_ai.hpp:165
A base class for all AI karts.
Definition: ai_base_lap_controller.hpp:32
Definition: test_ai.hpp:78
virtual bool canSkid(float steer_fraction)
Return true if AI can skid now.
const ItemState * m_last_item_random
The last item selected for collection, for which a probability was determined.
Definition: test_ai.hpp:159
float m_current_curve_radius
The radius of the curve the kart is currently driving.
Definition: test_ai.hpp:122
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