doom3-bfg/neo/d3xp/script/Script_Interpreter.h

306 lines
7.8 KiB
C
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
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.
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.
===========================================================================
*/
#ifndef __SCRIPT_INTERPRETER_H__
#define __SCRIPT_INTERPRETER_H__
#define MAX_STACK_DEPTH 64
#define LOCALSTACK_SIZE 6144
typedef struct prstack_s
{
2012-11-26 18:58:24 +00:00
int s;
const function_t* f;
2012-11-26 18:58:24 +00:00
int stackbase;
} prstack_t;
class idInterpreter
{
2012-11-26 18:58:24 +00:00
private:
prstack_t callStack[ MAX_STACK_DEPTH ];
int callStackDepth;
int maxStackDepth;
2012-11-26 18:58:24 +00:00
byte localstack[ LOCALSTACK_SIZE ];
int localstackUsed;
int localstackBase;
int maxLocalstackUsed;
const function_t* currentFunction;
2012-11-26 18:58:24 +00:00
int instructionPointer;
2012-11-26 18:58:24 +00:00
int popParms;
const idEventDef* multiFrameEvent;
idEntity* eventEntity;
idThread* thread;
2012-11-26 18:58:24 +00:00
void PopParms( int numParms );
void PushString( const char* string );
2012-11-26 18:58:24 +00:00
void Push( int value );
const char* FloatToString( float value );
void AppendString( idVarDef* def, const char* from );
void SetString( idVarDef* def, const char* from );
const char* GetString( idVarDef* def );
varEval_t GetVariable( idVarDef* def );
idEntity* GetEntity( int entnum ) const;
idScriptObject* GetScriptObject( int entnum ) const;
2012-11-26 18:58:24 +00:00
void NextInstruction( int position );
void LeaveFunction( idVarDef* returnDef );
void CallEvent( const function_t* func, int argsize );
void CallSysEvent( const function_t* func, int argsize );
2012-11-26 18:58:24 +00:00
public:
bool doneProcessing;
bool threadDying;
bool terminateOnExit;
bool debug;
idInterpreter();
2012-11-26 18:58:24 +00:00
// save games
void Save( idSaveGame* savefile ) const; // archives object for save game file
void Restore( idRestoreGame* savefile ); // unarchives object from save game file
void SetThread( idThread* pThread );
2012-11-26 18:58:24 +00:00
void StackTrace() const;
2012-11-26 18:58:24 +00:00
int CurrentLine() const;
const char* CurrentFile() const;
void Error( VERIFY_FORMAT_STRING const char* fmt, ... ) const;
void Warning( VERIFY_FORMAT_STRING const char* fmt, ... ) const;
2012-11-26 18:58:24 +00:00
void DisplayInfo() const;
bool BeginMultiFrameEvent( idEntity* ent, const idEventDef* event );
void EndMultiFrameEvent( idEntity* ent, const idEventDef* event );
2012-11-26 18:58:24 +00:00
bool MultiFrameEventInProgress() const;
void ThreadCall( idInterpreter* source, const function_t* func, int args );
void EnterFunction( const function_t* func, bool clearStack );
void EnterObjectFunction( idEntity* self, const function_t* func, bool clearStack );
2012-11-26 18:58:24 +00:00
bool Execute();
void Reset();
bool GetRegisterValue( const char* name, idStr& out, int scopeDepth );
2012-11-26 18:58:24 +00:00
int GetCallstackDepth() const;
const prstack_t* GetCallstack() const;
const function_t* GetCurrentFunction() const;
idThread* GetThread() const;
2012-11-26 18:58:24 +00:00
};
/*
====================
idInterpreter::PopParms
====================
*/
ID_INLINE void idInterpreter::PopParms( int numParms )
{
2012-11-26 18:58:24 +00:00
// pop our parms off the stack
if( localstackUsed < numParms )
{
2012-11-26 18:58:24 +00:00
Error( "locals stack underflow\n" );
}
2012-11-26 18:58:24 +00:00
localstackUsed -= numParms;
}
/*
====================
idInterpreter::Push
====================
*/
ID_INLINE void idInterpreter::Push( int value )
{
if( localstackUsed + sizeof( int ) > LOCALSTACK_SIZE )
{
2012-11-26 18:58:24 +00:00
Error( "Push: locals stack overflow\n" );
}
*( int* )&localstack[ localstackUsed ] = value;
2012-11-26 18:58:24 +00:00
localstackUsed += sizeof( int );
}
/*
====================
idInterpreter::PushString
====================
*/
ID_INLINE void idInterpreter::PushString( const char* string )
{
if( localstackUsed + MAX_STRING_LEN > LOCALSTACK_SIZE )
{
2012-11-26 18:58:24 +00:00
Error( "PushString: locals stack overflow\n" );
}
idStr::Copynz( ( char* )&localstack[ localstackUsed ], string, MAX_STRING_LEN );
2012-11-26 18:58:24 +00:00
localstackUsed += MAX_STRING_LEN;
}
/*
====================
idInterpreter::FloatToString
====================
*/
ID_INLINE const char* idInterpreter::FloatToString( float value )
{
2012-11-26 18:58:24 +00:00
static char text[ 32 ];
if( value == ( float )( int )value )
{
2012-11-26 18:58:24 +00:00
sprintf( text, "%d", ( int )value );
}
else
{
2012-11-26 18:58:24 +00:00
sprintf( text, "%f", value );
}
return text;
}
/*
====================
idInterpreter::AppendString
====================
*/
ID_INLINE void idInterpreter::AppendString( idVarDef* def, const char* from )
{
if( def->initialized == idVarDef::stackVariable )
{
idStr::Append( ( char* )&localstack[ localstackBase + def->value.stackOffset ], MAX_STRING_LEN, from );
}
else
{
2012-11-26 18:58:24 +00:00
idStr::Append( def->value.stringPtr, MAX_STRING_LEN, from );
}
}
/*
====================
idInterpreter::SetString
====================
*/
ID_INLINE void idInterpreter::SetString( idVarDef* def, const char* from )
{
if( def->initialized == idVarDef::stackVariable )
{
idStr::Copynz( ( char* )&localstack[ localstackBase + def->value.stackOffset ], from, MAX_STRING_LEN );
}
else
{
2012-11-26 18:58:24 +00:00
idStr::Copynz( def->value.stringPtr, from, MAX_STRING_LEN );
}
}
/*
====================
idInterpreter::GetString
====================
*/
ID_INLINE const char* idInterpreter::GetString( idVarDef* def )
{
if( def->initialized == idVarDef::stackVariable )
{
return ( char* )&localstack[ localstackBase + def->value.stackOffset ];
}
else
{
2012-11-26 18:58:24 +00:00
return def->value.stringPtr;
}
}
/*
====================
idInterpreter::GetVariable
====================
*/
ID_INLINE varEval_t idInterpreter::GetVariable( idVarDef* def )
{
if( def->initialized == idVarDef::stackVariable )
{
2012-11-26 18:58:24 +00:00
varEval_t val;
val.intPtr = ( int* )&localstack[ localstackBase + def->value.stackOffset ];
2012-11-26 18:58:24 +00:00
return val;
}
else
{
2012-11-26 18:58:24 +00:00
return def->value;
}
}
/*
================
idInterpreter::GetEntity
================
*/
ID_INLINE idEntity* idInterpreter::GetEntity( int entnum ) const
{
2012-11-26 18:58:24 +00:00
assert( entnum <= MAX_GENTITIES );
if( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) )
{
2012-11-26 18:58:24 +00:00
return gameLocal.entities[ entnum - 1 ];
}
return NULL;
}
/*
================
idInterpreter::GetScriptObject
================
*/
ID_INLINE idScriptObject* idInterpreter::GetScriptObject( int entnum ) const
{
idEntity* ent;
2012-11-26 18:58:24 +00:00
assert( entnum <= MAX_GENTITIES );
if( ( entnum > 0 ) && ( entnum <= MAX_GENTITIES ) )
{
2012-11-26 18:58:24 +00:00
ent = gameLocal.entities[ entnum - 1 ];
if( ent && ent->scriptObject.data )
{
2012-11-26 18:58:24 +00:00
return &ent->scriptObject;
}
}
return NULL;
}
/*
====================
idInterpreter::NextInstruction
====================
*/
ID_INLINE void idInterpreter::NextInstruction( int position )
{
2012-11-26 18:58:24 +00:00
// Before we execute an instruction, we increment instructionPointer,
// therefore we need to compensate for that here.
instructionPointer = position - 1;
}
#endif /* !__SCRIPT_INTERPRETER_H__ */