1 /***************************************************************************
2 
3     file                 : mycar.h
4     created              : Mon Oct 10 13:51:00 CET 2001
5     copyright            : (C) 2001-2002 by Bernhard Wymann
6     email                : berniw@bluewin.ch
7     version              : $Id: mycar.h,v 1.1.2.1 2008/11/09 17:50:19 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 "trackdesc.h"
39 #include "pathfinder.h"
40 #include "linalg.h"
41 
42 
43 class Pathfinder;
44 class PathSeg;
45 
46 class AbstractCar
47 {
48 	public:
AbstractCar()49 		AbstractCar() {};
~AbstractCar()50 		~AbstractCar() {};
getCarPtr()51 		inline tCarElt* getCarPtr() { return me; }
getCurrentPos()52 		inline v3d* getCurrentPos() { return &currentpos; }
getDir()53 		inline v3d* getDir() { return &dir; }
getSpeedSqr()54 		inline double getSpeedSqr() { return speedsqr; }
getSpeed()55 		inline double getSpeed() { return speed; }
getCurrentSegId()56 		inline int getCurrentSegId() { return currentsegid; }
57 
58 	protected:
setCarPtr(tCarElt * car)59 		inline void setCarPtr(tCarElt* car) { me = car; }
updateDir()60 		inline void updateDir() { dir.x = cos(me->_yaw); dir.y = sin(me->_yaw); dir.z = 0.0; }
updatePos()61 		inline void updatePos() { currentpos.x = me->_pos_X; currentpos.y = me->_pos_Y; currentpos.z = me->_pos_Z - cgh; }
updateSpeedSqr()62 		inline void updateSpeedSqr() { speedsqr = (me->_speed_x)*(me->_speed_x) + (me->_speed_y)*(me->_speed_y) + (me->_speed_z)*(me->_speed_z); }
updateSpeed()63 		inline void updateSpeed() { speed = sqrt(speedsqr); }
initCGh()64 		inline void initCGh() { cgh = GfParmGetNum(me->_carHandle, SECT_CAR, PRM_GCHEIGHT, NULL, 0.0); }
65 
66 		tCarElt* me;
67 		v3d currentpos;
68 		v3d dir;
69 		double speedsqr;
70 		double speed;
71 		int currentsegid;
72 
73 	private:
74 		double cgh;			/* height of center of gravity */
75 };
76 
77 
78 class MyCar : public AbstractCar
79 {
80 	public:
81 		/* possible behaviours */
82 		enum {
83 			INSANE = 0, PUSH = 1, NORMAL = 2, CAREFUL = 3, SLOW = 4, START = 5
84 		};
85 
86 		static const double PATHERR;			/* if derror > PATHERR we take actions to come back to the path [m] */
87 		static const double CORRLEN;			/* CORRLEN * derror is the length of the correction [m] */
88 		static const double TURNTOL;			/* tolerance for end backing up [m] */
89 		static const double TURNSPEED;			/* if speed lower than this you can back up [m/s] */
90 		static const double MARGIN;				/* security margin from track border [m] */
91 		static const double STABLESPEED;		/* we brake currentspeed/stablespeed if car seems unstable [m/s] */
92 		static const double TIMETOCATCH;		/* when do we start thinking about overtaking [s]*/
93 		static const double MINOVERTAKERANGE;	/* minimum length for overtaking [m] */
94 		static const double OVERTAKERADIUS;		/* min allowed radius to start overtaking [m] */
95 		static const double OVERTAKEDIST;		/* planned distance of CG's while overtaking [m] */
96 		static const double OVERTAKEMINDIST;	/* minimal distance of CG's while overtaking [m] */
97 		static const double OVERTAKEANGLE;		/* [-] radians */
98 		static const double OVERTAKEMARGIN;		/* [m] */
99 		static const double MAXALLOWEDPITCH;	/* [-] radians */
100 		static const double FLYSPEED;			/* speed where antifly checking gets activated [m/s] */
101 		static const double OVERLAPSTARTDIST;	/* distance where we start to check the need to let pass the opponent */
102 		static const double OVERLAPPASSDIST;	/* distance smaller than that and waiting long enough -> let him pass */
103 		static const double OVERLAPWAITTIME;	/* minimal waiting time before we consider let him pass */
104 		static const double LAPBACKTIMEPENALTY; /* penalty if i am able to "lap back" [s] */
105 		static const double TCL_SLIP;			/* [m/s] range [0..10] */
106 		static const double TCL_RANGE;			/* [m/s] range [0..10] */
107 		static const double SHIFT;				/* [-] (% of rpmredline) */
108 		static const double SHIFT_MARGIN;		/* [m/s] */
109 		static const double MAX_SPEED;			/* [m/s] */
110 		static const double MAX_FUEL_PER_METER;	/* [liter/m] fuel consumtion */
111 		static const double LOOKAHEAD_MAX_ERROR;/* [m] */
112 		static const double LOOKAHEAD_FACTOR;	/* [-] */
113 
114 		/* data for behavior */
115 		int bmode;
116 		double behaviour[6][12];
117 		int MAXDAMMAGE;									/* if dammage > MAXDAMMAGE then we plan a pit stop [-] */
118 		double DIST;									/* minimal distance to other cars [m] */
119 		double MAXRELAX;								/* to avoid skidding (0..0.99) [-] */
120 		double MAXANGLE;								/* biggest allowed angle to the path [deg] */
121 		double ACCELINC;								/* increment/decrement for acceleration [-] */
122 		double SPEEDSQRFACTOR;							/* multiplier for speedsqr */
123 		double GCTIME;									/* minimal time between gear changes */
124 		double ACCELLIMIT;								/* maximal allowed acceleration */
125 		double PATHERRFACTOR;							/* if derror > PATHERR*PATHERRFACTOR we compute a corrected path [-] */
126 		double CARWIDTH;								/* width of the car [m] */
127 		double CARLEN;									/* length of the car [m] */
128 		double AEROMAGIC;								/* aerodynamic lift factor [-] */
129 		double CFRICTION;								/* friction "magic" coefficient [-] */
130 
131 		/* static data */
132 		double cgcorr_b;
133 		double ca;
134 		double cw;
135 
136 		/* dynamic data */
137 		double mass;
138 		int destsegid;
139 		double trtime;
140 
141 		TrackSegment* currentseg;
142 		TrackSegment* destseg;
143 		PathSeg* currentpathseg;
144 		PathSeg* destpathseg;
145 
146 		int undamaged;
147 		double lastfuel;
148 		double fuelperlap;
149 		double lastpitfuel;
150 
151 		double turnaround;
152 		int tr_mode;
153 		double accel;
154 		bool fuelchecked;
155 		bool startmode;
156 
157 		double derror;			/* distance to desired trajectory */
158 
159 		MyCar(TrackDesc* track, tCarElt* car, tSituation *situation);
160 		~MyCar();
161 
162 		void info(void);
163 		void update(TrackDesc* track, tCarElt* car, tSituation *situation);
164 		void loadBehaviour(int id);
165 		double querySlipSpeed(tCarElt* car);
166 		double queryAcceleration(tCarElt * car, double speed);
getDeltaPitch()167 		inline double getDeltaPitch() { return deltapitch; }
getWheelBase()168 		inline double getWheelBase() { return wheelbase; }
getWheelTrack()169 		inline double getWheelTrack() { return wheeltrack; }
getErrorSgn()170 		inline double getErrorSgn() { return derrorsgn; }
getPathfinderPtr()171 		inline Pathfinder* getPathfinderPtr() { return pf; }
172 
173 	private:
174 		enum { DRWD = 0, DFWD = 1, D4WD = 2 };
175 
176 		int drivetrain;			/* RWD, FWD or 4WD */
177 		double carmass;			/* mass of car without fuel */
178 		double deltapitch;		/* angle between road and car */
179 		double wheelbase;
180 		double wheeltrack;
181 		double derrorsgn;		/* on which side of the trajectory am i left -1 or 1 right */
182 
183 		Pathfinder* pf;
184 
185 		void updateCa();
186 		void updateDError();
187 		void initCarGeometry();
188 };
189 
190 
191 class OtherCar: public AbstractCar
192 {
193 	public:
194 		void init(TrackDesc* itrack, tCarElt* car, tSituation *situation);
195 		void update(void);
196 
197 	private:
198 		TrackDesc* track;
199 		double dt;
200 };
201 
202 #endif // _MYCAR_H_
203 
204