doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Script_Thread.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __SCRIPT_THREAD_H__
30 #define __SCRIPT_THREAD_H__
31 
32 extern const idEventDef EV_Thread_Execute;
35 extern const idEventDef EV_Thread_Pause;
36 extern const idEventDef EV_Thread_Wait;
37 extern const idEventDef EV_Thread_WaitFrame;
38 extern const idEventDef EV_Thread_WaitFor;
40 extern const idEventDef EV_Thread_Print;
41 extern const idEventDef EV_Thread_PrintLn;
42 extern const idEventDef EV_Thread_Say;
43 extern const idEventDef EV_Thread_Assert;
44 extern const idEventDef EV_Thread_Trigger;
45 extern const idEventDef EV_Thread_SetCvar;
46 extern const idEventDef EV_Thread_GetCvar;
47 extern const idEventDef EV_Thread_Random;
48 extern const idEventDef EV_Thread_GetTime;
49 extern const idEventDef EV_Thread_KillThread;
51 extern const idEventDef EV_Thread_GetEntity;
52 extern const idEventDef EV_Thread_Spawn;
55 extern const idEventDef EV_Thread_SpawnFloat;
58 extern const idEventDef EV_Thread_AngToRight;
59 extern const idEventDef EV_Thread_AngToUp;
60 extern const idEventDef EV_Thread_Sine;
61 extern const idEventDef EV_Thread_Cosine;
62 extern const idEventDef EV_Thread_Normalize;
63 extern const idEventDef EV_Thread_VecLength;
66 extern const idEventDef EV_Thread_OnSignal;
68 extern const idEventDef EV_Thread_SetCamera;
71 extern const idEventDef EV_Thread_TracePos;
72 extern const idEventDef EV_Thread_FadeIn;
73 extern const idEventDef EV_Thread_FadeOut;
74 extern const idEventDef EV_Thread_FadeTo;
75 extern const idEventDef EV_Thread_Restart;
76 
77 class idThread : public idClass {
78 private:
80 
85 
87 
88  int threadNum;
90 
93 
95 
96  static int threadIndex;
98 
99  static trace_t trace;
100 
101  void Init( void );
102  void Pause( void );
103 
104  void Event_Execute( void );
105  void Event_SetThreadName( const char *name );
106 
107  //
108  // script callable Events
109  //
110  void Event_TerminateThread( int num );
111  void Event_Pause( void );
112  void Event_Wait( float time );
113  void Event_WaitFrame( void );
114  void Event_WaitFor( idEntity *ent );
115  void Event_WaitForThread( int num );
116  void Event_Print( const char *text );
117  void Event_PrintLn( const char *text );
118  void Event_Say( const char *text );
119  void Event_Assert( float value );
120  void Event_Trigger( idEntity *ent );
121  void Event_SetCvar( const char *name, const char *value ) const;
122  void Event_GetCvar( const char *name ) const;
123  void Event_Random( float range ) const;
124 #ifdef _D3XP
125  void Event_RandomInt( int range ) const;
126 #endif
127  void Event_GetTime( void );
128  void Event_KillThread( const char *name );
129  void Event_GetEntity( const char *name );
130  void Event_Spawn( const char *classname );
131  void Event_CopySpawnArgs( idEntity *ent );
132  void Event_SetSpawnArg( const char *key, const char *value );
133  void Event_SpawnString( const char *key, const char *defaultvalue );
134  void Event_SpawnFloat( const char *key, float defaultvalue );
135  void Event_SpawnVector( const char *key, idVec3 &defaultvalue );
136  void Event_ClearPersistantArgs( void );
137  void Event_SetPersistantArg( const char *key, const char *value );
138  void Event_GetPersistantString( const char *key );
139  void Event_GetPersistantFloat( const char *key );
140  void Event_GetPersistantVector( const char *key );
141  void Event_AngToForward( idAngles &ang );
142  void Event_AngToRight( idAngles &ang );
143  void Event_AngToUp( idAngles &ang );
144  void Event_GetSine( float angle );
145  void Event_GetCosine( float angle );
146 #ifdef _D3XP
147  void Event_GetArcSine( float a );
148  void Event_GetArcCosine( float a );
149 #endif
150  void Event_GetSquareRoot( float theSquare );
151  void Event_VecNormalize( idVec3 &vec );
152  void Event_VecLength( idVec3 &vec );
153  void Event_VecDotProduct( idVec3 &vec1, idVec3 &vec2 );
154  void Event_VecCrossProduct( idVec3 &vec1, idVec3 &vec2 );
155  void Event_VecToAngles( idVec3 &vec );
156 #ifdef _D3XP
157  void Event_VecToOrthoBasisAngles( idVec3 &vec );
158  void Event_RotateVector( idVec3 &vec, idVec3 &ang );
159 #endif
160  void Event_OnSignal( int signal, idEntity *ent, const char *func );
161  void Event_ClearSignalThread( int signal, idEntity *ent );
162  void Event_SetCamera( idEntity *ent );
163  void Event_FirstPerson( void );
164  void Event_Trace( const idVec3 &start, const idVec3 &end, const idVec3 &mins, const idVec3 &maxs, int contents_mask, idEntity *passEntity );
165  void Event_TracePoint( const idVec3 &start, const idVec3 &end, int contents_mask, idEntity *passEntity );
166  void Event_GetTraceFraction( void );
167  void Event_GetTraceEndPos( void );
168  void Event_GetTraceNormal( void );
169  void Event_GetTraceEntity( void );
170  void Event_GetTraceJoint( void );
171  void Event_GetTraceBody( void );
172  void Event_FadeIn( idVec3 &color, float time );
173  void Event_FadeOut( idVec3 &color, float time );
174  void Event_FadeTo( idVec3 &color, float alpha, float time );
175  void Event_SetShaderParm( int parmnum, float value );
176  void Event_StartMusic( const char *name );
177  void Event_Warning( const char *text );
178  void Event_Error( const char *text );
179  void Event_StrLen( const char *string );
180  void Event_StrLeft( const char *string, int num );
181  void Event_StrRight( const char *string, int num );
182  void Event_StrSkip( const char *string, int num );
183  void Event_StrMid( const char *string, int start, int num );
184  void Event_StrToFloat( const char *string );
185  void Event_RadiusDamage( const idVec3 &origin, idEntity *inflictor, idEntity *attacker, idEntity *ignore, const char *damageDefName, float dmgPower );
186  void Event_IsClient( void );
187  void Event_IsMultiplayer( void );
188  void Event_GetFrameTime( void );
189  void Event_GetTicsPerSecond( void );
190  void Event_CacheSoundShader( const char *soundName );
191  void Event_DebugLine( const idVec3 &color, const idVec3 &start, const idVec3 &end, const float lifetime );
192  void Event_DebugArrow( const idVec3 &color, const idVec3 &start, const idVec3 &end, const int size, const float lifetime );
193  void Event_DebugCircle( const idVec3 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const float lifetime );
194  void Event_DebugBounds( const idVec3 &color, const idVec3 &mins, const idVec3 &maxs, const float lifetime );
195  void Event_DrawText( const char *text, const idVec3 &origin, float scale, const idVec3 &color, const int align, const float lifetime );
196  void Event_InfluenceActive( void );
197 
198 public:
200 
201  idThread();
202  idThread( idEntity *self, const function_t *func );
203  idThread( const function_t *func );
204  idThread( idInterpreter *source, const function_t *func, int args );
205  idThread( idInterpreter *source, idEntity *self, const function_t *func, int args );
206 
207  virtual ~idThread();
208 
209  // tells the thread manager not to delete this thread when it ends
210  void ManualDelete( void );
211 
212  // save games
213  void Save( idSaveGame *savefile ) const; // archives object for save game file
214  void Restore( idRestoreGame *savefile ); // unarchives object from save game file
215 
216  void EnableDebugInfo( void ) { interpreter.debug = true; };
217  void DisableDebugInfo( void ) { interpreter.debug = false; };
218 
219  void WaitMS( int time );
220  void WaitSec( float time );
221  void WaitFrame( void );
222 
223  // NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
224  void CallFunction( const function_t *func, bool clearStack );
225 
226  // NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
227  void CallFunction( idEntity *obj, const function_t *func, bool clearStack );
228 
229  void DisplayInfo();
230  static idThread *GetThread( int num );
231  static void ListThreads_f( const idCmdArgs &args );
232  static void Restart( void );
233  static void ObjectMoveDone( int threadnum, idEntity *obj );
234 
235  static idList<idThread*>& GetThreads ( void );
236 
237  bool IsDoneProcessing ( void );
238  bool IsDying ( void );
239 
240  void End( void );
241  static void KillThread( const char *name );
242  static void KillThread( int num );
243  bool Execute( void );
244  void ManualControl( void ) { manualControl = true; CancelEvents( &EV_Thread_Execute ); };
245  void DoneProcessing( void ) { interpreter.doneProcessing = true; };
246  void ContinueProcessing( void ) { interpreter.doneProcessing = false; };
247  bool ThreadDying( void ) { return interpreter.threadDying; };
248  void EndThread( void ) { interpreter.threadDying = true; };
249  bool IsWaiting( void );
250  void ClearWaitFor( void );
251  bool IsWaitingFor( idEntity *obj );
252  void ObjectMoveDone( idEntity *obj );
253  void ThreadCallback( idThread *thread );
254  void DelayedStart( int delay );
255  bool Start( void );
256  idThread *WaitingOnThread( void );
257  void SetThreadNum( int num );
258  int GetThreadNum( void );
259  void SetThreadName( const char *name );
260  const char *GetThreadName( void );
261 
262  void Error( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
263  void Warning( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
264 
265  static idThread *CurrentThread( void );
266  static int CurrentThreadNum( void );
267  static bool BeginMultiFrameEvent( idEntity *ent, const idEventDef *event );
268  static void EndMultiFrameEvent( idEntity *ent, const idEventDef *event );
269 
270  static void ReturnString( const char *text );
271  static void ReturnFloat( float value );
272  static void ReturnInt( int value );
273  static void ReturnVector( idVec3 const &vec );
274  static void ReturnEntity( idEntity *ent );
275 };
276 
277 /*
278 ================
279 idThread::WaitingOnThread
280 ================
281 */
282 ID_INLINE idThread *idThread::WaitingOnThread( void ) {
283  return waitingForThread;
284 }
285 
286 /*
287 ================
288 idThread::SetThreadNum
289 ================
290 */
291 ID_INLINE void idThread::SetThreadNum( int num ) {
292  threadNum = num;
293 }
294 
295 /*
296 ================
297 idThread::GetThreadNum
298 ================
299 */
300 ID_INLINE int idThread::GetThreadNum( void ) {
301  return threadNum;
302 }
303 
304 /*
305 ================
306 idThread::GetThreadName
307 ================
308 */
309 ID_INLINE const char *idThread::GetThreadName( void ) {
310  return threadName.c_str();
311 }
312 
313 /*
314 ================
315 idThread::GetThreads
316 ================
317 */
319  return threadList;
320 }
321 
322 /*
323 ================
324 idThread::IsDoneProcessing
325 ================
326 */
327 ID_INLINE bool idThread::IsDoneProcessing ( void ) {
329 }
330 
331 /*
332 ================
333 idThread::IsDying
334 ================
335 */
336 ID_INLINE bool idThread::IsDying ( void ) {
337  return interpreter.threadDying;
338 }
339 
340 #endif /* !__SCRIPT_THREAD_H__ */
const idEventDef EV_Thread_OnSignal
static bool BeginMultiFrameEvent(idEntity *ent, const idEventDef *event)
const idEventDef EV_Thread_FadeIn
void Event_StrRight(const char *string, int num)
void Event_GetEntity(const char *name)
bool ThreadDying(void)
const idEventDef EV_Thread_Trigger
const idEventDef EV_Thread_Say
byte color[4]
Definition: MegaTexture.cpp:54
void Event_GetTraceBody(void)
void Event_Say(const char *text)
GLsizei const GLfloat * value
Definition: glext.h:3614
static trace_t trace
Definition: Script_Thread.h:99
void void static idThread * CurrentThread(void)
void Event_TracePoint(const idVec3 &start, const idVec3 &end, int contents_mask, idEntity *passEntity)
void EndThread(void)
const idEventDef EV_Thread_GetCvar
const idEventDef EV_Thread_Cosine
void Event_CopySpawnArgs(idEntity *ent)
void CallFunction(const function_t *func, bool clearStack)
const idEventDef EV_Thread_TracePos
void Event_WaitForThread(int num)
const idEventDef EV_Thread_VecCrossProduct
const idEventDef EV_Thread_Pause
void DelayedStart(int delay)
idThread * waitingForThread
Definition: Script_Thread.h:81
static void EndMultiFrameEvent(idEntity *ent, const idEventDef *event)
GLenum GLsizei GLenum format
Definition: glext.h:2846
const idEventDef EV_Thread_Restart
void Event_GetCvar(const char *name) const
const idEventDef EV_Thread_FadeTo
void Event_SetPersistantArg(const char *key, const char *value)
void Event_SetThreadName(const char *name)
void SetThreadNum(int num)
const idEventDef EV_Thread_TraceFraction
void Event_DebugCircle(const idVec3 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const float lifetime)
#define const
Definition: getdate.c:251
void ManualDelete(void)
idThread * WaitingOnThread(void)
const idEventDef EV_Thread_AngToForward
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:4804
void Event_StrToFloat(const char *string)
static int CurrentThreadNum(void)
const char * GetThreadName(void)
void Event_AngToForward(idAngles &ang)
void WaitMS(int time)
void Event_GetTraceJoint(void)
const idEventDef EV_Thread_SetCallback
void Save(idSaveGame *savefile) const
bool Start(void)
void Event_SetCamera(idEntity *ent)
void Event_GetSine(float angle)
void Event_InfluenceActive(void)
const idEventDef EV_Thread_AngToUp
void Event_GetSquareRoot(float theSquare)
Definition: Vector.h:316
void Event_GetTime(void)
const idEventDef EV_Thread_KillThread
void Event_IsMultiplayer(void)
const idEventDef EV_Thread_GetEntity
void Event_TerminateThread(int num)
const idEventDef EV_Thread_SetSpawnArg
void Event_FadeIn(idVec3 &color, float time)
void Event_SpawnString(const char *key, const char *defaultvalue)
static void ReturnVector(idVec3 const &vec)
void Event_DebugArrow(const idVec3 &color, const idVec3 &start, const idVec3 &end, const int size, const float lifetime)
void Event_FirstPerson(void)
GLclampf GLclampf GLclampf alpha
Definition: glext.h:2843
void Event_WaitFor(idEntity *ent)
GLhandleARB obj
Definition: glext.h:3602
const idEventDef EV_Thread_SetThreadName
void Event_SetSpawnArg(const char *key, const char *value)
GLuint GLuint num
Definition: glext.h:5390
void Event_StrLeft(const char *string, int num)
void Event_DebugLine(const idVec3 &color, const idVec3 &start, const idVec3 &end, const float lifetime)
void SetThreadName(const char *name)
void Event_Execute(void)
GLsizei range
Definition: glext.h:4368
void Event_VecCrossProduct(idVec3 &vec1, idVec3 &vec2)
void Event_GetTraceEntity(void)
Definition: Class.h:174
const idEventDef EV_Thread_Sine
static void KillThread(const char *name)
void ContinueProcessing(void)
void Event_WaitFrame(void)
const idEventDef EV_Thread_Spawn
void Event_StartMusic(const char *name)
int GetThreadNum(void)
static int threadIndex
Definition: Script_Thread.h:96
void ManualControl(void)
bool IsDoneProcessing(void)
void Event_DrawText(const char *text, const idVec3 &origin, float scale, const idVec3 &color, const int align, const float lifetime)
GLsizei GLsizei GLcharARB * source
Definition: glext.h:3633
const idEventDef EV_Thread_FirstPerson
const idEventDef EV_Thread_Assert
void Event_IsClient(void)
void DisableDebugInfo(void)
int lastExecuteTime
Definition: Script_Thread.h:91
int threadNum
Definition: Script_Thread.h:88
const idEventDef EV_Thread_GetTime
void Event_Pause(void)
void Event_Assert(float value)
void Event_ClearPersistantArgs(void)
void DoneProcessing(void)
static void ListThreads_f(const idCmdArgs &args)
GLuint GLuint end
Definition: glext.h:2845
void Event_SetShaderParm(int parmnum, float value)
const idEventDef EV_Thread_Print
static idThread * GetThread(int num)
int waitingUntil
Definition: Script_Thread.h:83
void Event_VecNormalize(idVec3 &vec)
Definition: Dict.h:65
const idEventDef EV_Thread_Random
int waitingFor
Definition: Script_Thread.h:82
void Event_Trigger(idEntity *ent)
void DisplayInfo()
bool IsDying(void)
const idEventDef EV_Thread_SpawnString
void Event_GetTraceNormal(void)
const idEventDef EV_Thread_WaitFrame
void CancelEvents(const idEventDef *ev)
Definition: Class.cpp:619
virtual ~idThread()
void Restore(idRestoreGame *savefile)
void Event_AngToRight(idAngles &ang)
static void ReturnFloat(float value)
const idEventDef EV_Thread_Normalize
void Event_FadeOut(idVec3 &color, float time)
void Event_CacheSoundShader(const char *soundName)
const idEventDef EV_Thread_SpawnFloat
const idEventDef EV_Thread_WaitFor
static void ObjectMoveDone(int threadnum, idEntity *obj)
static idList< idThread * > & GetThreads(void)
idDict spawnArgs
Definition: Script_Thread.h:86
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
void Event_FadeTo(idVec3 &color, float alpha, float time)
void Event_KillThread(const char *name)
void ClearWaitFor(void)
void Event_GetFrameTime(void)
const idEventDef EV_Thread_TerminateThread
void Event_Spawn(const char *classname)
void Event_SetCvar(const char *name, const char *value) const
void Event_Random(float range) const
bool IsWaiting(void)
void Event_Wait(float time)
void Event_VecToAngles(idVec3 &vec)
const idEventDef EV_Thread_SpawnVector
void Event_RadiusDamage(const idVec3 &origin, idEntity *inflictor, idEntity *attacker, idEntity *ignore, const char *damageDefName, float dmgPower)
void End(void)
void Event_OnSignal(int signal, idEntity *ent, const char *func)
static void ReturnString(const char *text)
#define id_attribute(x)
Definition: sys_public.h:139
void Event_ClearSignalThread(int signal, idEntity *ent)
void Event_VecDotProduct(idVec3 &vec1, idVec3 &vec2)
const idEventDef EV_Thread_Wait
void Pause(void)
static void Restart(void)
void ThreadCallback(idThread *thread)
const idEventDef EV_Thread_WaitForThread
void Event_StrSkip(const char *string, int num)
void Event_Warning(const char *text)
static idList< idThread * > threadList
Definition: Script_Thread.h:97
const GLcharARB * name
Definition: glext.h:3629
GLsizeiptr size
Definition: glext.h:3112
void Event_GetCosine(float angle)
void Event_AngToUp(idAngles &ang)
const idEventDef EV_Thread_VecDotProduct
Definition: Str.h:116
CLASS_PROTOTYPE(idThread)
int creationTime
Definition: Script_Thread.h:92
bool manualControl
Definition: Script_Thread.h:94
void Event_GetTraceEndPos(void)
const idEventDef EV_Thread_Execute
const idEventDef EV_Thread_VecLength
static void ReturnEntity(idEntity *ent)
const idEventDef EV_Thread_ClearSignal
const char * c_str(void) const
Definition: Str.h:487
void Error(const char *fmt,...) const id_attribute((format(printf
void EnableDebugInfo(void)
static idThread * currentThread
Definition: Script_Thread.h:79
void Event_Trace(const idVec3 &start, const idVec3 &end, const idVec3 &mins, const idVec3 &maxs, int contents_mask, idEntity *passEntity)
void Event_StrLen(const char *string)
void Init(void)
const idEventDef EV_Thread_SetCvar
const idEventDef EV_Thread_AngToRight
void Event_SpawnVector(const char *key, idVec3 &defaultvalue)
void Event_GetTraceFraction(void)
void Event_DebugBounds(const idVec3 &color, const idVec3 &mins, const idVec3 &maxs, const float lifetime)
void WaitFrame(void)
void Event_GetPersistantString(const char *key)
const idEventDef EV_Thread_PrintLn
void Event_Error(const char *text)
void Event_Print(const char *text)
void Event_StrMid(const char *string, int start, int num)
void Event_GetTicsPerSecond(void)
idStr threadName
Definition: Script_Thread.h:89
void Event_SpawnFloat(const char *key, float defaultvalue)
const idEventDef EV_Thread_FadeOut
bool IsWaitingFor(idEntity *obj)
idInterpreter interpreter
Definition: Script_Thread.h:84
void Event_VecLength(idVec3 &vec)
bool Execute(void)
static void ReturnInt(int value)
const idEventDef EV_Thread_SetCamera
void Event_GetPersistantVector(const char *key)
GLuint start
Definition: glext.h:2845
void Event_PrintLn(const char *text)
void Event_GetPersistantFloat(const char *key)
void void Warning(const char *fmt,...) const id_attribute((format(printf
void WaitSec(float time)