Script_Thread.h 13 KB

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