SuperTuxKart
arena_ai.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2016 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_ARENA_AI_HPP
20 #define HEADER_ARENA_AI_HPP
21 
22 #include "karts/controller/ai_base_controller.hpp"
23 #include "race/race_manager.hpp"
24 
25 #undef AI_DEBUG
26 #ifdef AI_DEBUG
27 #include "graphics/irr_driver.hpp"
28 #endif
29 
30 class ArenaGraph;
31 
32 namespace irr
33 {
34  namespace scene { class ISceneNode; }
35 }
36 
40 class ArenaAI : public AIBaseController
41 {
42 protected:
45 
48 
51 
54 
57 
60  irr::scene::ISceneNode *m_debug_sphere;
61 
64  irr::scene::ISceneNode *m_debug_sphere_next;
65 
68 
71 
75 
76  // ------------------------------------------------------------------------
77  void tryCollectItem(Vec3* aim_point, int* target_node) const;
78  // ------------------------------------------------------------------------
82  virtual void findClosestKart(bool consider_difficulty, bool find_sta) = 0;
83 
84 private:
87 
91 
94  bool m_is_stuck;
95 
98  bool m_is_uturn;
99 
102  std::set<int> m_on_node;
103 
106 
109 
112 
115 
118 
121 
124 
129 
132 
133  void configSpeed();
134  // ------------------------------------------------------------------------
135  void configSteering();
136  // ------------------------------------------------------------------------
137  void checkIfStuck(const float dt);
138  // ------------------------------------------------------------------------
139  void determinePath(int forward, std::vector<int>* path);
140  // ------------------------------------------------------------------------
141  void doSkiddingTest();
142  // ------------------------------------------------------------------------
143  void doUTurn(const float dt);
144  // ------------------------------------------------------------------------
145  bool gettingUnstuck(int ticks);
146  // ------------------------------------------------------------------------
147  bool updateAimingPosition(Vec3* target_point);
148  // ------------------------------------------------------------------------
149  void useItems(const float dt);
150  // ------------------------------------------------------------------------
151  virtual bool canSkid(float steer_fraction) OVERRIDE
152  { return m_mini_skid; }
153  // ------------------------------------------------------------------------
155  virtual void findTarget() = 0;
156  // ------------------------------------------------------------------------
158  virtual bool forceBraking() { return false; }
159  // ------------------------------------------------------------------------
161  virtual int getCurrentNode() const = 0;
162  // ------------------------------------------------------------------------
165  virtual float getKartDistance(const AbstractKart* kart) const = 0;
166  // ------------------------------------------------------------------------
168  virtual bool ignorePathFinding() { return false; }
169  // ------------------------------------------------------------------------
171  virtual bool isWaiting() const = 0;
172  // ------------------------------------------------------------------------
175  virtual bool isKartOnRoad() const = 0;
176  // ------------------------------------------------------------------------
179  virtual void resetAfterStop() {}
180 
181 public:
182  ArenaAI(AbstractKart *kart);
183  // ------------------------------------------------------------------------
184  virtual ~ArenaAI() {}
185  // ------------------------------------------------------------------------
186  virtual void update(int ticks) OVERRIDE;
187  // ------------------------------------------------------------------------
188  virtual void reset() OVERRIDE;
189  // ------------------------------------------------------------------------
190  virtual void newLap(int lap) OVERRIDE {}
191 
192 };
193 
194 #endif
void determinePath(int forward, std::vector< int > *path)
Determine if the path to target needs to be changed to avoid bad items, it will also set the turn rad...
Definition: arena_ai.cpp:655
A base class for all AI karts.
Definition: ai_base_controller.hpp:33
Difficulty
Game difficulty.
Definition: race_manager.hpp:224
float m_time_since_driving
This is a timer that counts down when the kart is starting to drive.
Definition: arena_ai.hpp:111
virtual int getCurrentNode() const =0
Return the current ArenaNode the AI located on.
void configSteering()
This function config the steering (m_steering_angle) of AI.
Definition: arena_ai.cpp:244
virtual bool isWaiting() const =0
If true, AI will stop moving.
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
void doUTurn(const float dt)
Make AI reverse so that it faces in front of the last target point.
Definition: arena_ai.cpp:365
float m_time_since_last_shot
Time an item has been collected and not used.
Definition: arena_ai.hpp:105
Vec3 m_target_point_lc
Local coordinates of current target point.
Definition: arena_ai.hpp:86
Vec3 m_target_point
The coordinates of target point.
Definition: arena_ai.hpp:70
void useItems(const float dt)
Determine how AI should use its item, different m_cur_difficulty will have a corresponding strategy.
Definition: arena_ai.cpp:417
void configSpeed()
Configure a suitable speed depends on current turn radius.
Definition: arena_ai.cpp:339
virtual float getKartDistance(const AbstractKart *kart) const =0
Return the distance based on graph distance matrix to any kart.
Vec3 m_reverse_point
Save the last target point before reversing, so AI will end reversing until facing in front of it.
Definition: arena_ai.hpp:90
AbstractKart * m_closest_kart
Pointer to the closest kart around this kart.
Definition: arena_ai.hpp:47
void doSkiddingTest()
Determine if AI should skid: When it's close to target, but not straight ahead, in front of it,...
Definition: arena_ai.cpp:633
virtual void reset() OVERRIDE
Resets the AI when a race is restarted.
Definition: arena_ai.cpp:45
void tryCollectItem(Vec3 *aim_point, int *target_node) const
Try to collect item in arena, if no suitable item is found, like they are swapped,...
Definition: arena_ai.cpp:579
float m_turn_radius
Used to determine braking and nitro usage.
Definition: arena_ai.hpp:120
bool updateAimingPosition(Vec3 *target_point)
Update aiming position, use path finding if necessary.
Definition: arena_ai.cpp:171
Vec3 m_closest_kart_point
The closest kart location.
Definition: arena_ai.hpp:53
A graph made from navmesh.
Definition: arena_graph.hpp:34
std::set< int > m_on_node
Holds the unique node ai has driven through, useful to tell if AI is stuck by determine the size of t...
Definition: arena_ai.hpp:102
virtual void resetAfterStop()
Overridden if any action is needed to be done when AI stopped moving or changed driving direction.
Definition: arena_ai.hpp:179
virtual bool forceBraking()
If true, AI will always try to brake for this frame.
Definition: arena_ai.hpp:158
virtual void findTarget()=0
Find a suitable target for this frame, implemented by sub-class.
irr::scene::ISceneNode * m_debug_sphere_next
For debugging purpose: a sphere indicating where the first turning corner is located.
Definition: arena_ai.hpp:64
int m_ticks_since_off_road
This is a timer that counts when the kart start going off road.
Definition: arena_ai.hpp:117
ArenaGraph * m_graph
Pointer to the ArenaGraph.
Definition: arena_ai.hpp:44
float m_steering_angle
Used to determine if skidding can be done.
Definition: arena_ai.hpp:123
RaceManager::Difficulty m_cur_difficulty
Holds the current difficulty.
Definition: arena_ai.hpp:56
bool m_is_stuck
Indicates that the kart is currently stuck, and m_ticks_since_reversing is counting down.
Definition: arena_ai.hpp:94
void checkIfStuck(const float dt)
Determine whether AI is stuck, by checking if it stays on the same node for a long period of time (se...
Definition: arena_ai.cpp:303
virtual bool ignorePathFinding()
If true, AI will drive directly to target without path finding.
Definition: arena_ai.hpp:168
int m_closest_kart_node
The ArenaNode at which the closest kart located on.
Definition: arena_ai.hpp:50
virtual void update(int ticks) OVERRIDE
This is the main entry point for the AI.
Definition: arena_ai.cpp:78
virtual bool canSkid(float steer_fraction) OVERRIDE
Return true if AI can skid now.
Definition: arena_ai.hpp:151
float m_time_since_uturn
This is a timer that counts down when the kart is doing u-turn.
Definition: arena_ai.hpp:114
virtual void findClosestKart(bool consider_difficulty, bool find_sta)=0
Find the closest kart around this AI, implemented by sub-class.
bool m_is_uturn
Indicates that the kart need a uturn to reach a node behind, and m_time_since_uturn is counting down.
Definition: arena_ai.hpp:98
virtual bool isKartOnRoad() const =0
If true, AI stays on the ArenaNode correctly, otherwise RescueAnimation will be done after sometime.
float m_ticks_since_reversing
This is a timer that counts down when the kart is reversing to get unstuck.
Definition: arena_ai.hpp:108
int m_target_node
The ArenaNode at which the target point located on.
Definition: arena_ai.hpp:67
bool m_mini_skid
True if AI can skid, currently only do when close to target, see doSkiddingTest().
Definition: arena_ai.hpp:74
bool gettingUnstuck(int ticks)
Function to let AI get unstuck.
Definition: arena_ai.cpp:391
Vec3 m_current_forward_point
The point in front of the AI which distance is m_kart_length, used to compensate the time difference ...
Definition: arena_ai.hpp:128
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
irr::scene::ISceneNode * m_debug_sphere
For debugging purpose: a sphere indicating where the AI is targeting at.
Definition: arena_ai.hpp:60
int m_current_forward_node
The ArenaNode at which the forward point located on.
Definition: arena_ai.hpp:131
A base class for AI that use navmesh to work.
Definition: arena_ai.hpp:40
virtual void newLap(int lap) OVERRIDE
Callback whenever a new lap is triggered.
Definition: arena_ai.hpp:190