Script_Thread.h 13 KB

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