1 /***************************************************************************
2 
3     file                 : mycar.h
4     created              : Mon Oct 10 13:51:00 CET 2001
5     copyright            : (C) 2001-2006 by Bernhard Wymann
6     email                : berniw@bluewin.ch
7     version              : $Id: mycar.h,v 1.1.2.2 2008/11/09 17:50:20 berniw Exp $
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version.                                   *
17  *                                                                         *
18  ***************************************************************************/
19 
20 /*
21 	this class holds some properties of the car
22 */
23 
24 #ifndef _MYCAR_H_
25 #define _MYCAR_H_
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <cstring>
30 
31 #include <tgf.h>
32 #include <track.h>
33 #include <car.h>
34 #include <raceman.h>
35 #include <robot.h>
36 #include <robottools.h>
37 #include <math.h>
38 #include <tmath/linalg_t.h>
39 
40 #include "trackdesc.h"
41 #include "pathfinder.h"
42 
43 
44 
45 class Pathfinder;
46 class PathSeg;
47 
48 class AbstractCar
49 {
50 	public:
AbstractCar()51 		AbstractCar() {};
~AbstractCar()52 		~AbstractCar() {};
getCarPtr()53 		inline tCarElt* getCarPtr() { return me; }
getCurrentPos()54 		inline vec2d* getCurrentPos() { return &currentpos; }
getDir()55 		inline vec2d* getDir() { return &dir; }
getSpeedSqr()56 		inline double getSpeedSqr() { return speedsqr; }
getSpeed()57 		inline double getSpeed() { return speed; }
getCurrentSegId()58 		inline int getCurrentSegId() { return currentsegid; }
59 
60 	protected:
setCarPtr(tCarElt * car)61 		inline void setCarPtr(tCarElt* car) { me = car; }
updateDir()62 		inline void updateDir() { dir.x = cos(me->_yaw); dir.y = sin(me->_yaw); }
updatePos()63 		inline void updatePos() { currentpos.x = me->_pos_X; currentpos.y = me->_pos_Y; }
updateSpeedSqr()64 		inline void updateSpeedSqr() { speedsqr = (me->_speed_x)*(me->_speed_x) + (me->_speed_y)*(me->_speed_y) + (me->_speed_z)*(me->_speed_z); }
updateSpeed()65 		inline void updateSpeed() { speed = sqrt(speedsqr); }
initCGh()66 		inline void initCGh() { cgh = GfParmGetNum(me->_carHandle, SECT_CAR, PRM_GCHEIGHT, NULL, 0.0); }
67 
68 		tCarElt* me;
69 		vec2d currentpos;
70 		vec2d dir;
71 		double speedsqr;
72 		double speed;
73 		int currentsegid;
74 
75 	private:
76 		double cgh;		// Height of center of gravity.
77 };
78 
79 
80 class MyCar : public AbstractCar
81 {
82 	public:
83 		// Possible behaviours.
84 		enum {
85 			INSANE = 0, PUSH = 1, NORMAL = 2, CAREFUL = 3, SLOW = 4, START = 5
86 		};
87 
88 		static const double PATHERR;				// If derror > PATHERR we take actions to come back to the path [m].
89 		static const double CORRLEN;				// CORRLEN * derror is the length of the correction [m].
90 		static const double TURNTOL;				// Tolerance for end backing up [m].
91 		static const double TURNSPEED;				// If speed lower than this you can back up [m/s].
92 		static const double MARGIN;					// Security margin from track border [m].
93 		static const double STABLESPEED;			// We brake currentspeed/stablespeed if car seems unstable [m/s].
94 		static const double TIMETOCATCH;			// When do we start thinking about overtaking [s].
95 		static const double MINOVERTAKERANGE;		// Minimum length for overtaking [m].
96 		static const double OVERTAKERADIUS;			// Min allowed radius to start overtaking [m].
97 		static const double OVERTAKEDIST;			// Planned distance of CG's while overtaking [m].
98 		static const double OVERTAKEMINDIST;		// Minimal distance of CG's while overtaking [m].
99 		static const double OVERTAKEANGLE;			// [-] radians.
100 		static const double MAXALLOWEDPITCH;		// [-] radians.
101 		static const double FLYSPEED;				// Speed where antifly checking gets activated [m/s].
102 		static const double OVERLAPSTARTDIST;		// Distance where we start to check the need to let pass the opponent.
103 		static const double OVERLAPPASSDIST;		// Distance smaller than that and waiting long enough -> let him pass.
104 		static const double OVERLAPWAITTIME;		// Minimal waiting time before we consider let him pass.
105 		static const double LAPBACKTIMEPENALTY; 	// Penalty if i am able to "lap back" [s].
106 		static const double TCL_SLIP;				// [m/s] range [0..10].
107 		static const double TCL_RANGE;				// [m/s] range [0..10].
108 		static const double SHIFT;					// [-] (% of rpmredline).
109 		static const double SHIFT_MARGIN;			// [m/s].
110 		static const double MAX_SPEED;				// [m/s].
111 		static const double MAX_FUEL_PER_METER;		// [liter/m] fuel consumtion.
112 		static const double LOOKAHEAD_MAX_ERROR;	// [m].
113 		static const double LOOKAHEAD_FACTOR;		// [-].
114 		static const double STEER_D_CONTROLLER_GAIN;// [-] Output gain of steering d-controller.
115 		static const float CLUTCH_SPEED;			// [m/s]
116 		static const float CLUTCH_FULL_MAX_TIME;	// [s] Time to apply full clutch.
117 		static const int TEAM_DAMAGE_CHANGE_LEAD;	// When to change position in the team?
118 		static const float FUEL_SAFETY_MARGIN;		// [kg] How much additional fuel in parts of maxfuelperlap for random errors.
119 
120 		// Data for behavior.
121 		int bmode;
122 		double behaviour[6][8];
123 		int MAXDAMMAGE;									// If dammage > MAXDAMMAGE then we plan a pit stop [-].
124 		double DIST;									// Minimal distance to other cars [m].
125 		double MAXRELAX;								// To avoid skidding (0..0.99) [-].
126 		double MAXANGLE;								// Biggest allowed angle to the path [deg].
127 		double ACCELINC;								// Increment/decrement for acceleration [-].
128 		double SPEEDSQRFACTOR;							// Multiplier for speedsqr.
129 		double GCTIME;									// Minimal time between gear changes.
130 		double ACCELLIMIT;								// Maximal allowed acceleration.
131 		double PATHERRFACTOR;							// If derror > PATHERR*PATHERRFACTOR we compute a corrected path [-].
132 
133 		double CARWIDTH;								// Width of the car [m].
134 		double CARLEN;									// Length of the car [m].
135 		double AEROMAGIC;								// Aerodynamic lift factor [-].
136 		double CFRICTION;								// Friction "magic" coefficient [-].
137 
138 		double STEER_P_CONTROLLER_MAX;					// [-] Maximum output of p-controller.
139 		double STEER_P_CONTROLLER_GAIN;					// [-] Output gain of p-controller.
140 
141 
142 		// Static data.
143 		double cgcorr_b;
144 		double ca;
145 		double cw;
146 
147 		// Dynamic data.
148 		double mass;
149 		int destsegid;
150 		double trtime;
151 
152 		TrackSegment2D* currentseg;
153 		TrackSegment2D* destseg;
154 		int currentpathsegid;
155 		int destpathsegid;
156 		PathSeg* dynpath;
157 
158 		int undamaged;
159 		double lastfuel;
160 		double fuelperlap;
161 		double lastpitfuel;
162 
163 		double turnaround;
164 		int tr_mode;
165 		double accel;
166 		bool fuelchecked;
167 		bool startmode;
168 
169 		double derror;			// Distance to desired trajectory.
170 		float clutchtime;		// Clutch timer.
171 
172 		MyCar(TrackDesc* track, tCarElt* car, tSituation *situation);
173 		~MyCar();
174 
175 		void info(void);
176 		void update(TrackDesc* track, tCarElt* car, tSituation *situation);
177 		void loadBehaviour(int id);
178 		double querySlipSpeed(tCarElt* car);
179 		double queryAcceleration(tCarElt * car, double speed);
getDeltaPitch()180 		inline double getDeltaPitch() { return deltapitch; }
getWheelBase()181 		inline double getWheelBase() { return wheelbase; }
getWheelTrack()182 		inline double getWheelTrack() { return wheeltrack; }
getErrorSgn()183 		inline double getErrorSgn() { return derrorsgn; }
getPathfinderPtr()184 		inline Pathfinder* getPathfinderPtr() { return pf; }
185 
186 	private:
187 		enum { DRWD = 0, DFWD = 1, D4WD = 2 };
188 
189 		int drivetrain;			// RWD, FWD or 4WD.
190 		double carmass;			// Mass of car without fuel.
191 		double deltapitch;		// Angle of attack between road and car.
192 		double wheelbase;
193 		double wheeltrack;
194 		double derrorsgn;		// On which side of the trajectory am i left -1 or 1 right.
195 
196 		Pathfinder* pf;
197 
198 		void updateCa();
199 		void updateDError();
200 		void initCarGeometry();
201 };
202 
203 
204 class OtherCar: public AbstractCar
205 {
206 	public:
207 		void init(TrackDesc* itrack, tCarElt* car, tSituation *situation);
208 		void update(void);
209 
210 	private:
211 		TrackDesc* track;
212 		double dt;
213 };
214 
215 #endif // _MYCAR_H_
216 
217