1 /*
2 
3 *************************************************************************
4 
5 ArmageTron -- Just another Tron Lightcycle Game in 3D.
6 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
7 
8 **************************************************************************
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 ***************************************************************************
25 
26 */
27 
28 #ifndef ArmageTron_GAMEOBJECT_H
29 #define ArmageTron_GAMEOBJECT_H
30 
31 //#include "uInput.h"
32 #include "tList.h"
33 // #include "eGrid.h"
34 #include "eCoord.h"
35 #include "tSafePTR.h"
36 
37 class eGrid;
38 class uActionPlayer;
39 class eTeam;
40 class eWall;
41 class eCamera;
42 
43 // a generic object for the game (cycles,explosions, particles,
44 // maybe AI opponents..)
45 class eGameObject{
46     friend class eFace;
47     friend class eCamera;
48     friend class eSensor;
49     friend class eGrid;
50     friend class ePlayerNetID;
51 
52     // a list of all active gameobjects
53     int id;
54 #define GO ((eGameObject *)NULL)
55 #define GO_I ((int *)NULL)
56 
57     // small wrapper of TimestepThis doing preparation and cleanup work
58     static void TimestepThisWrapper(eGrid * grid, REAL currentTime, eGameObject *t, REAL minTimestep);
59 
60 protected:
61     // does a timestep and all interactions for this gameobject,
62     // divided in many small steps
63     static bool TimestepThis(REAL currentTime,eGameObject *t);
64 
65     // tells game objects how far they are allowed to exeed the given simulation time
66     static REAL MaxSimulateAhead();
67 
68     // a list of all eGameObjects that are interesting to watch
69     int interestingID;
70     int inactiveID;
71 
72     // shall s_Timestep delete a eGameObject requesting destruction
73     // completely (autodelete=1) or should it just be removed from the list
74     // (autodelete=0) ?
75     // (the latter may be useful if there exists other pointers to
76     // the object)
77 
78 
79     bool autodelete;
80     REAL lastTime;          // the time it was last updated
81     REAL deathTime;          // the time the thing died
82 
83     eCoord pos;               // current position,
84     eCoord dir;               // direction
85     REAL  z;									// and height (currently unused)
86 
87     tJUST_CONTROLLED_PTR< eTeam > team;       		 				// the team we belong to
88 
89     tJUST_CONTROLLED_PTR<eFace> currentFace;  // the eFace pos it is currently
90     tCHECKED_PTR(eGrid) grid;         // the game grid we are on
91 
92     // entry and deletion in the list of all eGameObjects
93 public:
94     //! tells game objects what the maximum lag caused by lazy simulation of timesteps is
95     static REAL GetMaxLazyLag();
96     //! sets the value reported by GetMaxLazyLag()
97     static void SetMaxLazyLag( REAL lag );
98 
Team()99     eTeam* Team() const { return team; }
100 
101     static uActionPlayer se_turnLeft,se_turnRight;
102 
Grid()103     eGrid* Grid()        const { return grid;        }
CurrentFace()104     eFace* CurrentFace() const { return currentFace; }
105 
106     virtual void AddRef()  = 0;          //!< adds a reference
107     virtual void Release() = 0;         //!< removes a reference
108 
109     void AddToList();
110     void RemoveFromList();
111     void RemoveFromListsAll();
112     void RemoveFromGame(); //!< removes the object physically from the game
113 
114 protected:
115     virtual void OnRemoveFromGame(); //!< called on RemoveFromGame(). Call base class implementation, too, in your implementation. Must keep the object alive.
116 private:
117     virtual void DoRemoveFromGame(); //!< called on RemoveFromGame() after OnRemoveFromGame(). Do not call base class implementation of this function, don't expect to get called from subclasses.
118 public:
119 
GOID()120     int GOID() const {return id;}
LastTime()121     REAL LastTime() const {return lastTime;}
NextInterestingTime()122     virtual REAL NextInterestingTime() const {return lastTime;} //!< the next time something interesting is going to happen with this object
123 
124     eGameObject(eGrid *grid, const eCoord &p,const eCoord &d, eFace *currentface, bool autodelete=1);
125     virtual ~eGameObject();
126 
Position()127     virtual eCoord Position()const{return pos;}
Direction()128     virtual eCoord Direction()const{return dir;}
LastDirection()129     virtual eCoord LastDirection()const{return dir;}
DeathTime()130     virtual REAL DeathTime()const{return deathTime;}
Speed()131     virtual REAL  Speed()const{return 20;}
132 
133     // position after FPS dependant extrapolation
PredictPosition()134     virtual eCoord PredictPosition() const {return pos;}
135 
136     // makes two gameObjects interact:
137     virtual void InteractWith( eGameObject *target,REAL time,int recursion=1 );
138 
139     // what happens if we pass eWall w? (at position e->p[0]*a + e->p[1]*(1-a) )
140     virtual void PassEdge( const eWall *w,REAL time,REAL a,int recursion=1 );
141 
142     // what length multiplicator does driving along the given wall get when it is the given distance away?
PathfindingModifier(const eWall * w)143     virtual REAL PathfindingModifier( const eWall *w ) const { return 1 ;}
144 
145     // moves the object from pos to dest during the timeinterval
146     // [startTime,endTime] and issues all eWall-crossing tEvents
147     void Move( const eCoord &dest, REAL startTime, REAL endTime, bool useTempWalls = true );
148 
149     // finds the eFace we are in
150     void FindCurrentFace();
151 
152     // simulates behaviour up to currentTime:
153     virtual bool Timestep(REAL currentTime);
154     // return value: shall this object be destroyed?
155 
EdgeIsDangerous(const eWall * w,REAL time,REAL a)156     virtual bool EdgeIsDangerous(const eWall *w, REAL time, REAL a) const{
157         return w;
158     }
159 
160     //! called when the round begins, after all game objects have been created,
161     //! before the first network sync is sent out.
162     virtual void OnRoundBegin();
163 
164     //! called when the round ends
165     virtual void OnRoundEnd();
166 
167     //! destroys the gameobject (in the game)
168     virtual void Kill();
169 
170     //! tells whether the object is alive
Alive()171     virtual bool Alive() const {return false;}
172 
173     //! draws object to the screen using OpenGL
174     virtual void Render(const eCamera *cam);
175 
176     //! returns whether the rendering uses alpha blending (massively, so sorting errors would show)
177     virtual bool RendersAlpha() const;
178 
179     // draws the cockpit or whatever is seen from the interior
180     // in fixed perspective, called before the main rendering
181     virtual bool RenderCockpitFixedBefore(bool primary=true);
182     // return value: draw everything else?
183 
184     // the same purpose, but called after main rendering
185     virtual void RenderCockpitFixedAfter(bool primary=true);
186 
187     // virtual perspective
188     virtual void RenderCockpitVirtual(bool primary=false);
189 
190     //sound output
SoundMix(unsigned char * dest,unsigned int len,int viewer,REAL rvol,REAL lvol)191     virtual void SoundMix(unsigned char *dest,unsigned int len,
192                           int viewer,REAL rvol,REAL lvol){}
193 
194     // internal camera
CamDir()195     virtual eCoord CamDir()  const {return dir;}
CamRise()196     virtual REAL  CamRise()  const {return 0;}
CamPos()197     virtual eCoord CamPos()  const {return pos;}
CamZ()198     virtual REAL  CamZ()     const {return z;}
CamTop()199     virtual eCoord  CamTop() const {return eCoord(0,0);}
200 
201     // sr_laggometer
Lag()202     virtual REAL Lag() const{return 0;}          //!< expected average network latency
LagThreshold()203     virtual REAL LagThreshold() const{return 0;} //!< tolerated network latency variation
204 
205 #ifdef POWERPAK_DEB
206     virtual void PPDisplay();
207 #endif
208 
209     // Receives control from ePlayer
210     virtual bool Act(uActionPlayer *Act,REAL x);
211 
212     // does a timestep and all interactions for every gameobject
213     static void s_Timestep(eGrid *grid, REAL currentTime, REAL minTimestep);
214 
215     // displays everything:
216     static void RenderAll(eGrid *grid, const eCamera *cam);
217     static void PPDisplayAll();
218 
219     // kills everything:
220     static void DeleteAll(eGrid *grid);
221 };
222 
223 // game object to be created on the heap
224 class eReferencableGameObject: public eGameObject, public tReferencable< eReferencableGameObject >
225 {
226 public:
227     eReferencableGameObject(eGrid *grid, const eCoord &p,const eCoord &d, eFace *currentface, bool autodelete=1);
228 
229     // real reference counting
230     virtual void AddRef();          //!< adds a reference
231     virtual void Release();         //!< removes a reference
232 
233 private:
234     virtual void DoRemoveFromGame(); //!< called when removed from the game
235 };
236 
237 // game object of temporary lifetime on the stack. Don't dynamically allocate this.
238 class eStackGameObject: public eGameObject
239 {
240 public:
241     eStackGameObject(eGrid *grid, const eCoord &p,const eCoord &d, eFace *currentface);
242 
243     // dummy reference counting
244     virtual void AddRef();          //!< adds a reference
245     virtual void Release();         //!< removes a reference
246 
247 private:
248     virtual void DoRemoveFromGame(); //!< called when removed from the game
249 };
250 
251 //! Exception to throw when a gameobject dies during movement
252 class eDeath
253 {
254 public:
eDeath()255     eDeath(){}   //!< constructor
~eDeath()256     ~eDeath(){}  //!< destructor
257 };
258 
259 #endif
260