doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Script_Interpreter.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_INTERPRETER_H__
30 #define __SCRIPT_INTERPRETER_H__
31 
32 #define MAX_STACK_DEPTH 64
33 #define LOCALSTACK_SIZE 6144
34 
35 typedef struct prstack_s {
36  int s;
37  const function_t *f;
38  int stackbase;
39 } prstack_t;
40 
42 private:
46 
51 
54 
55  int popParms;
58 
60 
61  void PopParms( int numParms );
62  void PushString( const char *string );
63  void Push( int value );
64  const char *FloatToString( float value );
65  void AppendString( idVarDef *def, const char *from );
66  void SetString( idVarDef *def, const char *from );
67  const char *GetString( idVarDef *def );
69  idEntity *GetEntity( int entnum ) const;
70  idScriptObject *GetScriptObject( int entnum ) const;
71  void NextInstruction( int position );
72 
73  void LeaveFunction( idVarDef *returnDef );
74  void CallEvent( const function_t *func, int argsize );
75  void CallSysEvent( const function_t *func, int argsize );
76 
77 public:
81  bool debug;
82 
83  idInterpreter();
84 
85  // save games
86  void Save( idSaveGame *savefile ) const; // archives object for save game file
87  void Restore( idRestoreGame *savefile ); // unarchives object from save game file
88 
89  void SetThread( idThread *pThread );
90 
91  void StackTrace( void ) const;
92 
93  int CurrentLine( void ) const;
94  const char *CurrentFile( void ) const;
95 
96  void Error( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
97  void Warning( const char *fmt, ... ) const id_attribute((format(printf,2,3)));
98  void DisplayInfo( void ) const;
99 
100  bool BeginMultiFrameEvent( idEntity *ent, const idEventDef *event );
101  void EndMultiFrameEvent( idEntity *ent, const idEventDef *event );
102  bool MultiFrameEventInProgress( void ) const;
103 
104  void ThreadCall( idInterpreter *source, const function_t *func, int args );
105  void EnterFunction( const function_t *func, bool clearStack );
106  void EnterObjectFunction( idEntity *self, const function_t *func, bool clearStack );
107 
108  bool Execute( void );
109  void Reset( void );
110 
111  bool GetRegisterValue( const char *name, idStr &out, int scopeDepth );
112  int GetCallstackDepth( void ) const;
113  const prstack_t *GetCallstack( void ) const;
114  const function_t *GetCurrentFunction( void ) const;
115  idThread *GetThread( void ) const;
116 
117 };
118 
119 /*
120 ====================
121 idInterpreter::PopParms
122 ====================
123 */
124 ID_INLINE void idInterpreter::PopParms( int numParms ) {
125  // pop our parms off the stack
126  if ( localstackUsed < numParms ) {
127  Error( "locals stack underflow\n" );
128  }
129 
130  localstackUsed -= numParms;
131 }
132 
133 /*
134 ====================
135 idInterpreter::Push
136 ====================
137 */
138 ID_INLINE void idInterpreter::Push( int value ) {
139  if ( localstackUsed + sizeof( int ) > LOCALSTACK_SIZE ) {
140  Error( "Push: locals stack overflow\n" );
141  }
142  *( int * )&localstack[ localstackUsed ] = value;
143  localstackUsed += sizeof( int );
144 }
145 
146 /*
147 ====================
148 idInterpreter::PushString
149 ====================
150 */
151 ID_INLINE void idInterpreter::PushString( const char *string ) {
153  Error( "PushString: locals stack overflow\n" );
154  }
155  idStr::Copynz( ( char * )&localstack[ localstackUsed ], string, MAX_STRING_LEN );
156  localstackUsed += MAX_STRING_LEN;
157 }
158 
159 /*
160 ====================
161 idInterpreter::FloatToString
162 ====================
163 */
164 ID_INLINE const char *idInterpreter::FloatToString( float value ) {
165  static char text[ 32 ];
166 
167  if ( value == ( float )( int )value ) {
168  sprintf( text, "%d", ( int )value );
169  } else {
170  sprintf( text, "%f", value );
171  }
172  return text;
173 }
174 
175 /*
176 ====================
177 idInterpreter::AppendString
178 ====================
179 */
180 ID_INLINE void idInterpreter::AppendString( idVarDef *def, const char *from ) {
181  if ( def->initialized == idVarDef::stackVariable ) {
183  } else {
185  }
186 }
187 
188 /*
189 ====================
190 idInterpreter::SetString
191 ====================
192 */
193 ID_INLINE void idInterpreter::SetString( idVarDef *def, const char *from ) {
194  if ( def->initialized == idVarDef::stackVariable ) {
196  } else {
198  }
199 }
200 
201 /*
202 ====================
203 idInterpreter::GetString
204 ====================
205 */
206 ID_INLINE const char *idInterpreter::GetString( idVarDef *def ) {
207  if ( def->initialized == idVarDef::stackVariable ) {
208  return ( char * )&localstack[ localstackBase + def->value.stackOffset ];
209  } else {
210  return def->value.stringPtr;
211  }
212 }
213 
214 /*
215 ====================
216 idInterpreter::GetVariable
217 ====================
218 */
220  if ( def->initialized == idVarDef::stackVariable ) {
221  varEval_t val;
222  val.intPtr = ( int * )&localstack[ localstackBase + def->value.stackOffset ];
223  return val;
224  } else {
225  return def->value;
226  }
227 }
228 
229 /*
230 ================
231 idInterpreter::GetEntity
232 ================
233 */
234 ID_INLINE idEntity *idInterpreter::GetEntity( int entnum ) const{
235  assert( entnum <= MAX_GENTITIES );
236  if ( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) ) {
237  return gameLocal.entities[ entnum - 1 ];
238  }
239  return NULL;
240 }
241 
242 /*
243 ================
244 idInterpreter::GetScriptObject
245 ================
246 */
247 ID_INLINE idScriptObject *idInterpreter::GetScriptObject( int entnum ) const {
248  idEntity *ent;
249 
250  assert( entnum <= MAX_GENTITIES );
251  if ( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) ) {
252  ent = gameLocal.entities[ entnum - 1 ];
253  if ( ent && ent->scriptObject.data ) {
254  return &ent->scriptObject;
255  }
256  }
257  return NULL;
258 }
259 
260 /*
261 ====================
262 idInterpreter::NextInstruction
263 ====================
264 */
265 ID_INLINE void idInterpreter::NextInstruction( int position ) {
266  // Before we execute an instruction, we increment instructionPointer,
267  // therefore we need to compensate for that here.
268  instructionPointer = position - 1;
269 }
270 
271 #endif /* !__SCRIPT_INTERPRETER_H__ */
const function_t * GetCurrentFunction(void) const
byte localstack[LOCALSTACK_SIZE]
GLsizei const GLfloat * value
Definition: glext.h:3614
assert(prefInfo.fullscreenBtn)
char * stringPtr
#define MAX_GENTITIES
Definition: Game_local.h:83
GLenum GLsizei GLenum format
Definition: glext.h:2846
#define const
Definition: getdate.c:251
idScriptObject scriptObject
Definition: Entity.h:123
varEval_t value
case const int
Definition: Callbacks.cpp:52
void SetString(idVarDef *def, const char *from)
void PushString(const char *string)
void EndMultiFrameEvent(idEntity *ent, const idEventDef *event)
const function_t * f
#define MAX_STRING_LEN
int CurrentLine(void) const
bool MultiFrameEventInProgress(void) const
void AppendString(idVarDef *def, const char *from)
GLsizei GLsizei GLcharARB * source
Definition: glext.h:3633
bool BeginMultiFrameEvent(idEntity *ent, const idEventDef *event)
void void Warning(const char *fmt,...) const id_attribute((format(printf
const idEventDef * multiFrameEvent
const char * FloatToString(float value)
struct prstack_s prstack_t
varEval_t GetVariable(idVarDef *def)
void CallEvent(const function_t *func, int argsize)
#define NULL
Definition: Lib.h:88
void ThreadCall(idInterpreter *source, const function_t *func, int args)
void Error(const char *fmt,...) const id_attribute((format(printf
static void Copynz(char *dest, const char *src, int destsize)
Definition: Str.cpp:1376
initialized_t initialized
idGameLocal gameLocal
Definition: Game_local.cpp:64
bool GetRegisterValue(const char *name, idStr &out, int scopeDepth)
idEntity * eventEntity
void Save(idSaveGame *savefile) const
void Push(int value)
const function_t * currentFunction
const char * GetString(idVarDef *def)
idEntity * entities[MAX_GENTITIES]
Definition: Game_local.h:275
idThread * GetThread(void) const
idScriptObject * GetScriptObject(int entnum) const
#define id_attribute(x)
Definition: sys_public.h:139
void CallSysEvent(const function_t *func, int argsize)
void Append(const char a)
Definition: Str.h:729
unsigned char byte
Definition: Lib.h:75
void EnterObjectFunction(idEntity *self, const function_t *func, bool clearStack)
#define MAX_STACK_DEPTH
const GLcharARB * name
Definition: glext.h:3629
void EnterFunction(const function_t *func, bool clearStack)
void void void DisplayInfo(void) const
void NextInstruction(int position)
Definition: Str.h:116
void StackTrace(void) const
void SetThread(idThread *pThread)
#define LOCALSTACK_SIZE
int GetCallstackDepth(void) const
const char * CurrentFile(void) const
idEntity * GetEntity(int entnum) const
void LeaveFunction(idVarDef *returnDef)
void Restore(idRestoreGame *savefile)
const prstack_t * GetCallstack(void) const
int sprintf(idStr &string, const char *fmt,...)
Definition: Str.cpp:1528
void PopParms(int numParms)
prstack_t callStack[MAX_STACK_DEPTH]