Back to index

supertuxkart  0.5+dfsg1
default_robot.hpp
Go to the documentation of this file.
00001 //  SuperTuxKart - a fun racing game with go-kart
00002 //  Copyright (C) 2004-2005 Steve Baker <sjbaker1@airmail.net>
00003 //  Copyright (C) 2006-2007 Eduardo Hernandez Munoz
00004 //
00005 //  This program is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU General Public License
00007 //  as published by the Free Software Foundation; either version 2
00008 //  of the License, or (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 #ifndef HEADER_DEFAULT_H
00020 #define HEADER_DEFAULT_H
00021 
00022 #include "auto_kart.hpp"
00023 
00024 class DefaultRobot : public AutoKart
00025 {
00026 private:
00027     enum FallbackTactic
00028     {
00029         FT_AVOID_TRACK_CRASH, //Only steer to avoid getting out of the road,
00030                               //otherwise, don't steer at all
00031         FT_PARALLEL,    //Stay parallel to the road
00032         FT_FAREST_POINT //Drive towards the farest non-crashing point that
00033                         //the kart can drive to in a straight line without
00034                         //crashing with the track.
00035     };
00036 
00037     enum ItemTactic
00038     {
00039         IT_TEN_SECONDS, //Fire after 10 seconds have passed, since the item
00040                         //was grabbed.
00041         IT_CALCULATE //Aim carefully, check for enough space for boosters,
00042                      //and that other conditions are meet before firing.
00043     };
00044 
00045     class CrashTypes
00046     {
00047         public:
00048 
00049         bool m_road; //true if we are going to 'crash' with the bounds of the road
00050         int m_kart; //-1 if no crash, pos numbers are the kart it crashes with
00051         CrashTypes() : m_road(false), m_kart(-1) {};
00052         void clear() {m_road = false; m_kart = -1;}
00053     } m_crashes;
00054 
00055     /*Difficulty handling variables*/
00056     float m_max_start_delay; //Delay before accelerating at the start of each
00057                              //race
00058     int m_min_steps; //Minimum number of steps to check. If 0, the AI doesn't
00059                      //even has check around the kart, if 1, it checks around
00060                      //the kart always, and more than that will check the
00061                      //remaining number of steps in front of the kart, always
00062     bool  m_wait_for_players; //If true, the acceleration is decreased when
00063                               //the AI is in a better position than all the
00064                               //human players.
00065     float m_max_handicap_accel; //The allowed maximum speed, in percentage,
00066                                 //from 0.0 to 1.0. Used only when
00067                                 //m_wait_for_players == true.
00068     FallbackTactic m_fallback_tactic; //General steering procedure. Used
00069                                       //mostly on straight lines and on curves
00070                                       //that re too small to need special
00071                                       //handling.
00072     bool m_use_wheelies; //Is the AI allowed to use wheelies?
00073     float m_wheelie_check_dist; //How far to check for the space needed for
00074                                 //wheelies, in percentage. Used only when
00075                                 //m_use_wheelies == true.
00076     ItemTactic m_item_tactic; //How are items going to be used?
00077 
00078     /*General purpose variables*/
00079     //The crash percentage is how much of the time the AI has been crashing,
00080     //if the AI has been crashing for some time, use the rescue.
00081     float m_crash_time;
00082 
00083     float m_time_since_last_shot;
00084     int   m_future_sector;
00085     sgVec2 m_future_location;
00086 
00087     float m_time_till_start; //Used to simulate a delay at the start of the
00088                              //race, since human players don't accelerate
00089                              //at the same time and rarely repeat the a
00090                              //previous timing.
00091 
00092     int m_inner_curve;//-1 left, 1 = right, 0 = center
00093     float m_curve_target_speed;
00094     float m_curve_angle;
00095 
00096     float m_time_since_stuck;
00097 
00098     int m_start_kart_crash_direction; //-1 = left, 1 = right, 0 = no crash.
00099 
00100     /*Functions called directly from update(). They all represent an action
00101      *that can be done, and end up setting their respective m_controls
00102      *variable, except handle_race_start() that isn't associated with any
00103      *specific action (more like, associated with inaction).
00104      */
00105     void handle_race_start();
00106     void handle_acceleration(const float DELTA);
00107     void handle_steering();
00108     void handle_items(const float DELTA, const int STEPS);
00109     void handle_rescue(const float DELTA);
00110     void handle_braking();
00111     void handle_wheelie(const int STEPS);
00112 
00113     /*Lower level functions not called directly from update()*/
00114     float steer_to_angle(const size_t SECTOR, const float ANGLE);
00115     float steer_to_point(const sgVec2 POINT);
00116 
00117     bool do_wheelie(const int STEPS);
00118     void check_crashes(const int STEPS, sgVec3 const pos);
00119     void find_non_crashing_point(sgVec2 result);
00120 
00121     float normalize_angle (float angle);
00122     int calc_steps();
00123 
00124     float angle_to_control(float angle) const;
00125     float get_approx_radius(const int START, const int END) const;
00126     void find_curve();
00127     int m_sector;
00128 
00129 public:
00130     DefaultRobot(const std::string& kart_name, int position,
00131              sgCoord init_pos);
00132 
00133     void update      (float delta) ;
00134     void reset       ();
00135     int  isPlayerKart() const {return 0;}
00136 };
00137 
00138 #endif
00139 
00140 /* EOF */