2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
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.
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.
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/>.
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.
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.
26 ===========================================================================
29 #ifndef __SCRIPT_THREAD_H__
30 #define __SCRIPT_THREAD_H__
32 extern const idEventDef EV_Thread_Execute;
33 extern const idEventDef EV_Thread_SetCallback;
34 extern const idEventDef EV_Thread_TerminateThread;
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;
39 extern const idEventDef EV_Thread_WaitForThread;
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;
50 extern const idEventDef EV_Thread_SetThreadName;
51 extern const idEventDef EV_Thread_GetEntity;
52 extern const idEventDef EV_Thread_Spawn;
53 extern const idEventDef EV_Thread_SetSpawnArg;
54 extern const idEventDef EV_Thread_SpawnString;
55 extern const idEventDef EV_Thread_SpawnFloat;
56 extern const idEventDef EV_Thread_SpawnVector;
57 extern const idEventDef EV_Thread_AngToForward;
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;
64 extern const idEventDef EV_Thread_VecDotProduct;
65 extern const idEventDef EV_Thread_VecCrossProduct;
66 extern const idEventDef EV_Thread_OnSignal;
67 extern const idEventDef EV_Thread_ClearSignal;
68 extern const idEventDef EV_Thread_SetCamera;
69 extern const idEventDef EV_Thread_FirstPerson;
70 extern const idEventDef EV_Thread_TraceFraction;
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;
77 class idThread : public idClass {
79 static idThread *currentThread;
81 idThread *waitingForThread;
84 idInterpreter interpreter;
96 static int threadIndex;
97 static idList<idThread *> threadList;
104 void Event_Execute( void );
105 void Event_SetThreadName( const char *name );
108 // script callable Events
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 void Event_GetTime( void );
125 void Event_KillThread( const char *name );
126 void Event_GetEntity( const char *name );
127 void Event_Spawn( const char *classname );
128 void Event_CopySpawnArgs( idEntity *ent );
129 void Event_SetSpawnArg( const char *key, const char *value );
130 void Event_SpawnString( const char *key, const char *defaultvalue );
131 void Event_SpawnFloat( const char *key, float defaultvalue );
132 void Event_SpawnVector( const char *key, idVec3 &defaultvalue );
133 void Event_ClearPersistantArgs( void );
134 void Event_SetPersistantArg( const char *key, const char *value );
135 void Event_GetPersistantString( const char *key );
136 void Event_GetPersistantFloat( const char *key );
137 void Event_GetPersistantVector( const char *key );
138 void Event_AngToForward( idAngles &ang );
139 void Event_AngToRight( idAngles &ang );
140 void Event_AngToUp( idAngles &ang );
141 void Event_GetSine( float angle );
142 void Event_GetCosine( float angle );
143 void Event_GetSquareRoot( float theSquare );
144 void Event_VecNormalize( idVec3 &vec );
145 void Event_VecLength( idVec3 &vec );
146 void Event_VecDotProduct( idVec3 &vec1, idVec3 &vec2 );
147 void Event_VecCrossProduct( idVec3 &vec1, idVec3 &vec2 );
148 void Event_VecToAngles( idVec3 &vec );
149 void Event_OnSignal( int signal, idEntity *ent, const char *func );
150 void Event_ClearSignalThread( int signal, idEntity *ent );
151 void Event_SetCamera( idEntity *ent );
152 void Event_FirstPerson( void );
153 void Event_Trace( const idVec3 &start, const idVec3 &end, const idVec3 &mins, const idVec3 &maxs, int contents_mask, idEntity *passEntity );
154 void Event_TracePoint( const idVec3 &start, const idVec3 &end, int contents_mask, idEntity *passEntity );
155 void Event_GetTraceFraction( void );
156 void Event_GetTraceEndPos( void );
157 void Event_GetTraceNormal( void );
158 void Event_GetTraceEntity( void );
159 void Event_GetTraceJoint( void );
160 void Event_GetTraceBody( void );
161 void Event_FadeIn( idVec3 &color, float time );
162 void Event_FadeOut( idVec3 &color, float time );
163 void Event_FadeTo( idVec3 &color, float alpha, float time );
164 void Event_SetShaderParm( int parmnum, float value );
165 void Event_StartMusic( const char *name );
166 void Event_Warning( const char *text );
167 void Event_Error( const char *text );
168 void Event_StrLen( const char *string );
169 void Event_StrLeft( const char *string, int num );
170 void Event_StrRight( const char *string, int num );
171 void Event_StrSkip( const char *string, int num );
172 void Event_StrMid( const char *string, int start, int num );
173 void Event_StrToFloat( const char *string );
174 void Event_RadiusDamage( const idVec3 &origin, idEntity *inflictor, idEntity *attacker, idEntity *ignore, const char *damageDefName, float dmgPower );
175 void Event_IsClient( void );
176 void Event_IsMultiplayer( void );
177 void Event_GetFrameTime( void );
178 void Event_GetTicsPerSecond( void );
179 void Event_CacheSoundShader( const char *soundName );
180 void Event_DebugLine( const idVec3 &color, const idVec3 &start, const idVec3 &end, const float lifetime );
181 void Event_DebugArrow( const idVec3 &color, const idVec3 &start, const idVec3 &end, const int size, const float lifetime );
182 void Event_DebugCircle( const idVec3 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const float lifetime );
183 void Event_DebugBounds( const idVec3 &color, const idVec3 &mins, const idVec3 &maxs, const float lifetime );
184 void Event_DrawText( const char *text, const idVec3 &origin, float scale, const idVec3 &color, const int align, const float lifetime );
185 void Event_InfluenceActive( void );
188 CLASS_PROTOTYPE( idThread );
191 idThread( idEntity *self, const function_t *func );
192 idThread( const function_t *func );
193 idThread( idInterpreter *source, const function_t *func, int args );
194 idThread( idInterpreter *source, idEntity *self, const function_t *func, int args );
198 // tells the thread manager not to delete this thread when it ends
199 void ManualDelete( void );
202 void Save( idSaveGame *savefile ) const; // archives object for save game file
203 void Restore( idRestoreGame *savefile ); // unarchives object from save game file
205 void EnableDebugInfo( void ) { interpreter.debug = true; };
206 void DisableDebugInfo( void ) { interpreter.debug = false; };
208 void WaitMS( int time );
209 void WaitSec( float time );
210 void WaitFrame( void );
212 // NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
213 void CallFunction( const function_t *func, bool clearStack );
215 // NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
216 void CallFunction( idEntity *obj, const function_t *func, bool clearStack );
219 static idThread *GetThread( int num );
220 static void ListThreads_f( const idCmdArgs &args );
221 static void Restart( void );
222 static void ObjectMoveDone( int threadnum, idEntity *obj );
224 static idList<idThread*>& GetThreads ( void );
226 bool IsDoneProcessing ( void );
227 bool IsDying ( void );
230 static void KillThread( const char *name );
231 static void KillThread( int num );
232 bool Execute( void );
233 void ManualControl( void ) { manualControl = true; CancelEvents( &EV_Thread_Execute ); };
234 void DoneProcessing( void ) { interpreter.doneProcessing = true; };
235 void ContinueProcessing( void ) { interpreter.doneProcessing = false; };
236 bool ThreadDying( void ) { return interpreter.threadDying; };
237 void EndThread( void ) { interpreter.threadDying = true; };
238 bool IsWaiting( void );
239 void ClearWaitFor( void );
240 bool IsWaitingFor( idEntity *obj );
241 void ObjectMoveDone( idEntity *obj );
242 void ThreadCallback( idThread *thread );
243 void DelayedStart( int delay );
245 idThread *WaitingOnThread( void );
246 void SetThreadNum( int num );
247 int GetThreadNum( void );
248 void SetThreadName( const char *name );
249 const char *GetThreadName( void );
251 void Error( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
252 void Warning( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
254 static idThread *CurrentThread( void );
255 static int CurrentThreadNum( void );
256 static bool BeginMultiFrameEvent( idEntity *ent, const idEventDef *event );
257 static void EndMultiFrameEvent( idEntity *ent, const idEventDef *event );
259 static void ReturnString( const char *text );
260 static void ReturnFloat( float value );
261 static void ReturnInt( int value );
262 static void ReturnVector( idVec3 const &vec );
263 static void ReturnEntity( idEntity *ent );
268 idThread::WaitingOnThread
271 ID_INLINE idThread *idThread::WaitingOnThread( void ) {
272 return waitingForThread;
277 idThread::SetThreadNum
280 ID_INLINE void idThread::SetThreadNum( int num ) {
286 idThread::GetThreadNum
289 ID_INLINE int idThread::GetThreadNum( void ) {
295 idThread::GetThreadName
298 ID_INLINE const char *idThread::GetThreadName( void ) {
299 return threadName.c_str();
307 ID_INLINE idList<idThread*>& idThread::GetThreads ( void ) {
313 idThread::IsDoneProcessing
316 ID_INLINE bool idThread::IsDoneProcessing ( void ) {
317 return interpreter.doneProcessing;
325 ID_INLINE bool idThread::IsDying ( void ) {
326 return interpreter.threadDying;
329 #endif /* !__SCRIPT_THREAD_H__ */