DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Script_Thread.h (13075B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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;
     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;
     76 
     77 class idThread : public idClass {
     78 private:
     79 	static idThread				*currentThread;
     80 
     81 	idThread					*waitingForThread;
     82 	int							waitingFor;
     83 	int							waitingUntil;
     84 	idInterpreter				interpreter;
     85 
     86 	idDict						spawnArgs;
     87 								
     88 	int 						threadNum;
     89 	idStr 						threadName;
     90 
     91 	int							lastExecuteTime;
     92 	int							creationTime;
     93 
     94 	bool						manualControl;
     95 
     96 	static int					threadIndex;
     97 	static idList<idThread *, TAG_THREAD>	threadList;
     98 
     99 	static trace_t				trace;
    100 
    101 	void						Init();
    102 	void						Pause();
    103 
    104 	void						Event_Execute();
    105 	void						Event_SetThreadName( const char *name );
    106 
    107 	//
    108 	// script callable Events
    109 	//
    110 	void						Event_TerminateThread( int num );
    111 	void						Event_Pause();
    112 	void						Event_Wait( float time );
    113 	void						Event_WaitFrame();
    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_RandomInt( int range ) const;
    125 	void						Event_GetTime();
    126 	void						Event_KillThread( const char *name );
    127 	void						Event_GetEntity( const char *name );
    128 	void						Event_Spawn( const char *classname );
    129 	void						Event_CopySpawnArgs( idEntity *ent );
    130 	void						Event_SetSpawnArg( const char *key, const char *value );
    131 	void						Event_SpawnString( const char *key, const char *defaultvalue );
    132 	void						Event_SpawnFloat( const char *key, float defaultvalue );
    133 	void						Event_SpawnVector( const char *key, idVec3 &defaultvalue );
    134 	void						Event_ClearPersistantArgs();
    135 	void 						Event_SetPersistantArg( const char *key, const char *value );
    136 	void 						Event_GetPersistantString( const char *key );
    137 	void 						Event_GetPersistantFloat( const char *key );
    138 	void 						Event_GetPersistantVector( const char *key );
    139 	void						Event_AngToForward( idAngles &ang );
    140 	void						Event_AngToRight( idAngles &ang );
    141 	void						Event_AngToUp( idAngles &ang );
    142 	void						Event_GetSine( float angle );
    143 	void						Event_GetCosine( float angle );
    144 	void						Event_GetArcSine( float a );
    145 	void						Event_GetArcCosine( float a );
    146 	void						Event_GetSquareRoot( float theSquare );
    147 	void						Event_VecNormalize( idVec3 &vec );
    148 	void						Event_VecLength( idVec3 &vec );
    149 	void						Event_VecDotProduct( idVec3 &vec1, idVec3 &vec2 );
    150 	void						Event_VecCrossProduct( idVec3 &vec1, idVec3 &vec2 );
    151 	void						Event_VecToAngles( idVec3 &vec );
    152 	void						Event_VecToOrthoBasisAngles( idVec3 &vec );
    153 	void						Event_RotateVector( idVec3 &vec, idVec3 &ang );
    154 	void						Event_OnSignal( int signal, idEntity *ent, const char *func );
    155 	void						Event_ClearSignalThread( int signal, idEntity *ent );
    156 	void						Event_SetCamera( idEntity *ent );
    157 	void						Event_FirstPerson();
    158 	void						Event_Trace( const idVec3 &start, const idVec3 &end, const idVec3 &mins, const idVec3 &maxs, int contents_mask, idEntity *passEntity );
    159 	void						Event_TracePoint( const idVec3 &start, const idVec3 &end, int contents_mask, idEntity *passEntity );
    160 	void						Event_GetTraceFraction();
    161 	void						Event_GetTraceEndPos();
    162 	void						Event_GetTraceNormal();
    163 	void						Event_GetTraceEntity();
    164 	void						Event_GetTraceJoint();
    165 	void						Event_GetTraceBody();
    166 	void						Event_FadeIn( idVec3 &color, float time );
    167 	void						Event_FadeOut( idVec3 &color, float time );
    168 	void						Event_FadeTo( idVec3 &color, float alpha, float time );
    169 	void						Event_SetShaderParm( int parmnum, float value );
    170 	void						Event_StartMusic( const char *name );
    171 	void						Event_Warning( const char *text );
    172 	void						Event_Error( const char *text );
    173 	void 						Event_StrLen( const char *string );
    174 	void 						Event_StrLeft( const char *string, int num );
    175 	void 						Event_StrRight( const char *string, int num );
    176 	void 						Event_StrSkip( const char *string, int num );
    177 	void 						Event_StrMid( const char *string, int start, int num );
    178 	void						Event_StrToFloat( const char *string );
    179 	void						Event_RadiusDamage( const idVec3 &origin, idEntity *inflictor, idEntity *attacker, idEntity *ignore, const char *damageDefName, float dmgPower );
    180 	void						Event_IsClient();
    181 	void 						Event_IsMultiplayer();
    182 	void 						Event_GetFrameTime();
    183 	void 						Event_GetTicsPerSecond();
    184 	void						Event_CacheSoundShader( const char *soundName );
    185 	void						Event_DebugLine( const idVec3 &color, const idVec3 &start, const idVec3 &end, const float lifetime );
    186 	void						Event_DebugArrow( const idVec3 &color, const idVec3 &start, const idVec3 &end, const int size, const float lifetime );
    187 	void						Event_DebugCircle( const idVec3 &color, const idVec3 &origin, const idVec3 &dir, const float radius, const int numSteps, const float lifetime );
    188 	void						Event_DebugBounds( const idVec3 &color, const idVec3 &mins, const idVec3 &maxs, const float lifetime );
    189 	void						Event_DrawText( const char *text, const idVec3 &origin, float scale, const idVec3 &color, const int align, const float lifetime );
    190 	void						Event_InfluenceActive();
    191 
    192 public:							
    193 								CLASS_PROTOTYPE( idThread );
    194 								
    195 								idThread();
    196 								idThread( idEntity *self, const function_t *func );
    197 								idThread( const function_t *func );
    198 								idThread( idInterpreter *source, const function_t *func, int args );
    199 								idThread( idInterpreter *source, idEntity *self, const function_t *func, int args );
    200 
    201 	virtual						~idThread();
    202 
    203 								// tells the thread manager not to delete this thread when it ends
    204 	void						ManualDelete();
    205 
    206 	// save games
    207 	void						Save( idSaveGame *savefile ) const;				// archives object for save game file
    208 	void						Restore( idRestoreGame *savefile );				// unarchives object from save game file
    209 
    210 	void						EnableDebugInfo() { interpreter.debug = true; };
    211 	void						DisableDebugInfo() { interpreter.debug = false; };
    212 
    213 	void						WaitMS( int time );
    214 	void						WaitSec( float time );
    215 	void						WaitFrame();
    216 								
    217 								// NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
    218 	void						CallFunction( const function_t	*func, bool clearStack );
    219 
    220 								// NOTE: If this is called from within a event called by this thread, the function arguments will be invalid after calling this function.
    221 	void						CallFunction( idEntity *obj, const function_t *func, bool clearStack );
    222 
    223 	void						DisplayInfo();
    224 	static idThread				*GetThread( int num );
    225 	static void					ListThreads_f( const idCmdArgs &args );
    226 	static void					Restart();
    227 	static void					ObjectMoveDone( int threadnum, idEntity *obj );
    228 								
    229 	static idList<idThread*>&	GetThreads ();
    230 	
    231 	bool						IsDoneProcessing ();
    232 	bool						IsDying			 ();	
    233 								
    234 	void						End();
    235 	static void					KillThread( const char *name );
    236 	static void					KillThread( int num );
    237 	bool						Execute();
    238 	void						ManualControl() { manualControl = true; CancelEvents( &EV_Thread_Execute ); };
    239 	void						DoneProcessing() { interpreter.doneProcessing = true; };
    240 	void						ContinueProcessing() { interpreter.doneProcessing = false; };
    241 	bool						ThreadDying() { return interpreter.threadDying; };
    242 	void						EndThread() { interpreter.threadDying = true; };
    243 	bool						IsWaiting();
    244 	void						ClearWaitFor();
    245 	bool						IsWaitingFor( idEntity *obj );
    246 	void						ObjectMoveDone( idEntity *obj );
    247 	void						ThreadCallback( idThread *thread );
    248 	void						DelayedStart( int delay );
    249 	bool						Start();
    250 	idThread					*WaitingOnThread();
    251 	void						SetThreadNum( int num );
    252 	int 						GetThreadNum();
    253 	void						SetThreadName( const char *name );
    254 	const char					*GetThreadName();
    255 
    256 	void						Error( VERIFY_FORMAT_STRING const char *fmt, ... ) const;
    257 	void						Warning( VERIFY_FORMAT_STRING const char *fmt, ... ) const;
    258 								
    259 	static idThread				*CurrentThread();
    260 	static int					CurrentThreadNum();
    261 	static bool					BeginMultiFrameEvent( idEntity *ent, const idEventDef *event );
    262 	static void					EndMultiFrameEvent( idEntity *ent, const idEventDef *event );
    263 
    264 	static void					ReturnString( const char *text );
    265 	static void					ReturnFloat( float value );
    266 	static void					ReturnInt( int value );
    267 	static void					ReturnVector( idVec3 const &vec );
    268 	static void					ReturnEntity( idEntity *ent );
    269 };
    270 
    271 /*
    272 ================
    273 idThread::WaitingOnThread
    274 ================
    275 */
    276 ID_INLINE idThread *idThread::WaitingOnThread() {
    277 	return waitingForThread;
    278 }
    279 
    280 /*
    281 ================
    282 idThread::SetThreadNum
    283 ================
    284 */
    285 ID_INLINE void idThread::SetThreadNum( int num ) {
    286 	threadNum = num;
    287 }
    288 
    289 /*
    290 ================
    291 idThread::GetThreadNum
    292 ================
    293 */
    294 ID_INLINE int idThread::GetThreadNum() {
    295 	return threadNum;
    296 }
    297 
    298 /*
    299 ================
    300 idThread::GetThreadName
    301 ================
    302 */
    303 ID_INLINE const char *idThread::GetThreadName() {
    304 	return threadName.c_str();
    305 }
    306 
    307 /*
    308 ================
    309 idThread::GetThreads
    310 ================
    311 */
    312 ID_INLINE idList<idThread*>& idThread::GetThreads () {
    313 	return threadList;
    314 }	
    315 
    316 /*
    317 ================
    318 idThread::IsDoneProcessing
    319 ================
    320 */
    321 ID_INLINE bool idThread::IsDoneProcessing () {
    322 	return interpreter.doneProcessing;
    323 }
    324 
    325 /*
    326 ================
    327 idThread::IsDying
    328 ================
    329 */
    330 ID_INLINE bool idThread::IsDying () {
    331 	return interpreter.threadDying;
    332 }
    333 
    334 #endif /* !__SCRIPT_THREAD_H__ */