the pit class
Author: Bernhard Wymann
Keywords: torcs, berniw, bernhard wymann, pit, pit stop, class

# 8.3 The Pit Class ### The Header File In this section we prepare the header file for the pit class. It contains the definition of the functionality to decide if we want to pit (strategy), to compute the pit path (offset) and some utility functions. Put the following code into a new file named pit.h.
    /***************************************************************************

        file                 : pit.h
        created              : Thu Mai 15 2:41:00 CET 2003
        copyright            : (C) 2003 by Bernhard Wymann
        email                : berniw@bluewin.ch

     ***************************************************************************/

    /***************************************************************************
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 2 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     ***************************************************************************/

    #ifndef _PIT_H_
    #define _PIT_H_

    #include "driver.h"
    #include "spline.h"

    #define NPOINTS 7
First we define the number of points for the pit path spline.
    class Driver;

    class Pit {
        public:
            Pit(tSituation *s, Driver *driver);
            ~Pit();
The constructor and destructor. We have already implemented them in the last section.
            void setPitstop(bool pitstop);
            bool getPitstop() { return pitstop; }
Setter and getter for the pitstop variable.
            void setInPit(bool inpitlane) { this->inpitlane = inpitlane; }
            bool getInPit() { return inpitlane; }
Setter and getter for the inpitlane variable.
            float getPitOffset(float offset, float fromstart);
Computes for a given distance from the start line the offset of the path.
            bool isBetween(float fromstart);
Checks if the given distance from start is between pitentry and pitexit. Such a method is handy because the value wraps-around at the start line.
            float getNPitStart() { return p[1].x; }
            float getNPitLoc() { return p[3].x; }
            float getNPitEnd() { return p[5].x; }
Methods to get the spline "x" coordinates of the points 1,3 and 5. We need them to control the speed limit in the pit lane and to stop in the pit.
            float toSplineCoord(float x);
Converts the track (distance from start) "x" coordinates of the spline into spline "x" coordinates, starting from zero with no wrap-around in the "x" value.
            float getSpeedlimitSqr() { return speedlimitsqr; }
            float getSpeedlimit() { return speedlimit; }
Methods to get the speed limit in the pits. The returned value already includes the security margin.
            void update();
            int getRepair();
            float getFuel();
The update() method does the housekeeping of the data used for the strategy and decides if we need to pit. The getRepair() and getFuel() methods decides how much damage gets repaired and how much fuel we get at the pit stop.
        private:
            tTrack *track;
            tCarElt *car;
            tTrackOwnPit *mypit;    /* pointer to my pit */
            tTrackPitInfo *pitinfo; /* general pit info */
Pointers to the track, car, the pit and the pitinfo structure. I use them to avoid too much indirection.
            SplinePoint p[NPOINTS]; /* spline points */
            Spline *spline;         /* spline */
The spline point data and the spline instance.
            bool pitstop;           /* pitstop planned */
The pitstop variable indicates if we plan a pit stop. It will become set to false right after the stop, because we do not need to stop anymore.
            bool inpitlane;         /* we are still in the pit lane */
The inpitlane variable indicates if we are in the pit lane. It will become set to true if pitstop is true and we are in the pit lane (pitentry). It will become set to false if we passed pitexit. The variable is necessary to finish the pit stop correctly.
            float pitentry;         /* distance to start line of the pit entry */
            float pitexit;          /* distance to the start line of the pit exit */
            float speedlimitsqr;    /* pit speed limit squared */
            float speedlimit;       /* pit speed limit */
Some static data needed by several functions.
            bool fuelchecked;       /* fuel statistics updated */
            float lastfuel;         /* the fuel available when we cross the start lane */
            float lastpitfuel;      /* amount refueled, special case when we refuel */
            float fuelperlap;       /* the maximum amount of fuel we needed for a lap */
The data necessary to decide if we need to refuel or not. The fuelperlap variable contains the maximum amount of fuel we needed so far for a lap. The amount of fuel we request on the pit stop will be computed based on that value.
            static const float SPEED_LIMIT_MARGIN;
            static const int PIT_DAMMAGE;
    };

    #endif // _PIT_H_
Finally the declaration of the constants. You already know them from the last section. ### Summary You have implemented the header file pit.h.