📄 scriptmaster.cpp
字号:
Event EV_ScriptThread_WaitForThread( "waitForThread" );
Event EV_ScriptThread_WaitForConsole( "waitForConsole" );
Event EV_ScriptThread_WaitForVariable( "waitForVariable" );
Event EV_ScriptThread_WaitForDeath( "waitForDeath" );
Event EV_ScriptThread_WaitForSound( "waitForSound" );
Event EV_ScriptThread_WaitForPlayer( "waitForPlayer" );
Event EV_ScriptThread_End( "end" );
Event EV_ScriptThread_Print( "print" );
Event EV_ScriptThread_PrintInt( "printint" );
Event EV_ScriptThread_PrintFloat( "printfloat" );
Event EV_ScriptThread_PrintVector( "printvector" );
Event EV_ScriptThread_NewLine( "newline" );
Event EV_ScriptThread_UserPrint( "uprint" );
Event EV_ScriptThread_UserPrintInt( "uprintint" );
Event EV_ScriptThread_UserPrintFloat( "uprintfloat" );
Event EV_ScriptThread_UserPrintVector( "uprintvector" );
Event EV_ScriptThread_UserNewLine( "unewline" );
Event EV_ScriptThread_Assert( "assert" );
Event EV_ScriptThread_Break( "break" );
Event EV_ScriptThread_Clear( "clear" );
Event EV_ScriptThread_Trigger( "trigger" );
Event EV_ScriptThread_Spawn( "spawn", EV_CHEAT );
Event EV_ScriptThread_Map( "map" );
Event EV_ScriptThread_SetCvar( "setcvar" );
Event EV_ScriptThread_CueCamera( "cuecamera" );
Event EV_ScriptThread_CuePlayer( "cueplayer" );
Event EV_ScriptThread_FreezePlayer( "freezeplayer" );
Event EV_ScriptThread_ReleasePlayer( "releaseplayer" );
Event EV_ScriptThread_Menu( "menu" );
Event EV_ScriptThread_MissionFailed( "missionfailed" );
Event EV_ScriptThread_KillEnt( "killent", EV_CONSOLE );
Event EV_ScriptThread_KillClass( "killclass", EV_CONSOLE );
Event EV_ScriptThread_RemoveEnt( "removeent", EV_CONSOLE );
Event EV_ScriptThread_RemoveClass( "removeclass", EV_CONSOLE );
// client/server flow control
Event EV_ScriptThread_ServerOnly( "server" );
Event EV_ScriptThread_ClientOnly( "client" );
Event EV_ScriptThread_StuffCommand( "stuffcmd" );
Event EV_ScriptThread_Training( "training" );
Event EV_ScriptThread_ClearSaveGames( "clearsavegames" );
//
// world stuff
//
Event EV_ScriptThread_SetLightStyle( "lightstyle" );
Event EV_ScriptThread_RegisterAlias( "alias" );
Event EV_ScriptThread_RegisterAliasAndCache( "aliascache" );
Event EV_ScriptThread_SetCinematic( "cinematic" );
Event EV_ScriptThread_SetNonCinematic( "noncinematic" );
Event EV_ScriptThread_SetSkipThread( "skipthread" );
Event EV_ScriptThread_AirClamp( "airclamp" );
// Precache specific
Event EV_ScriptThread_Precache_Model( "cachemodel" );
Event EV_ScriptThread_Precache_PlayerModel( "cacheplayermodel" );
Event EV_ScriptThread_Precache_Sound( "cachesound" );
// set dialog script
Event EV_ScriptThread_SetDialogScript( "setdialogscript" );
// fades for movies
Event EV_ScriptThread_FadeIn( "fadein" );
Event EV_ScriptThread_FadeOut( "fadeout" );
Event EV_ScriptThread_Hud( "hud" );
Event EV_ScriptThread_LoadOverlay( "loadoverlay" );
Event EV_ScriptThread_LoadIntermission( "loadintermission" );
Event EV_ScriptThread_IntermissionLayout( "intermissionlayout" );
Event EV_ScriptThread_Overlay( "overlay" );
Event EV_ScriptThread_ScreenPrint( "screenprint" );
Event EV_ScriptThread_ScreenPrintFile( "screenprintfile" );
Event EV_ScriptThread_ClearScreenPrintFile( "clearscreenprintfile" );
Event EV_ScriptThread_MapName( "mapname" );
Event EV_ScriptThread_EndGame( "endgame" );
// music command
Event EV_ScriptThread_MusicEvent( "music" );
Event EV_ScriptThread_ForceMusicEvent( "forcemusic" );
Event EV_ScriptThread_SoundtrackEvent( "soundtrack" );
// Precache specific
Event EV_ScriptThread_JC_Hearable( "jc_hearable" );
Event EV_ScriptThread_JC_Not_Hearable( "jc_not_hearable" );
// crucial dialog command
Event EV_ScriptThread_CrucialDialog( "crucialdialog" );
// dialog sound command
Event EV_ScriptThread_DialogSound( "dialogsound" );
CLASS_DECLARATION( Class, ThreadMarker, NULL );
ResponseDef ThreadMarker::Responses[] =
{
{ NULL, NULL }
};
ResponseDef ScriptMaster::Responses[] =
{
{ NULL, NULL }
};
ScriptMaster Director;
CLASS_DECLARATION( Listener, ScriptMaster, NULL );
ScriptMaster::ScriptMaster()
{
threadIndex = 0;
currentThread = NULL;
player_ready = false;
}
ScriptMaster::~ScriptMaster()
{
GameTime = NULL;
CloseScript();
}
EXPORT_FROM_DLL void ScriptMaster::CloseScript
(
void
)
{
KillThreads();
ScriptLib.CloseScripts();
GameTime = NULL;
}
EXPORT_FROM_DLL void ScriptMaster::KillThreads
(
void
)
{
int i;
int num;
num = Threads.NumObjects();
// Clear the waitfor's from each thread before deleting
// so they don't get triggered.
for( i = num; i > 0; i-- )
{
Threads.ObjectAt( i )->ClearWaitFor();
}
for( i = num; i > 0; i-- )
{
delete Threads.ObjectAt( i );
}
Threads.FreeObjectList();
threadIndex = 0;
currentThread = NULL;
}
EXPORT_FROM_DLL qboolean ScriptMaster::NotifyOtherThreads
(
int num
)
{
ScriptThread *thread1;
ScriptThread *thread2;
int i;
int n;
Event *ev;
thread1 = GetThread( num );
assert( thread1 );
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
thread2 = Threads.ObjectAt( i );
assert( thread2 );
if ( thread2->WaitingOnThread() == thread1 )
{
ev = new Event( EV_ScriptThread_ThreadCallback );
ev->SetThread( thread1 );
thread2->ProcessEvent( ev );
return true;
}
}
return false;
}
EXPORT_FROM_DLL void ScriptMaster::DeathMessage
(
const char *name
)
{
ScriptThread *thread;
Event *ev;
int i,n;
// Look for threads that are waiting for this name
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
const char *waiting;
thread = Threads.ObjectAt( i );
assert( thread );
if (thread)
{
waiting = thread->WaitingOnDeath();
if (waiting)
{
if (!strcmp(waiting, name))
{
ev = new Event( EV_ScriptThread_DeathCallback );
thread->ProcessEvent(ev);
}
}
}
}
}
EXPORT_FROM_DLL qboolean ScriptMaster::PlayerReady
(
void
)
{
return player_ready;
}
EXPORT_FROM_DLL void ScriptMaster::PlayerNotReady
(
void
)
{
player_ready = false;
}
EXPORT_FROM_DLL void ScriptMaster::PlayerSpawned
(
void
)
{
ScriptThread *thread;
int i,n;
player_ready = true;
// Look for threads that are waiting for the player
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
thread = Threads.ObjectAt( i );
assert( thread );
if (thread)
{
if ( thread->WaitingOnPlayer() )
{
thread->ClearWaitFor();
thread->Start( -1 );
}
}
}
}
EXPORT_FROM_DLL void ScriptMaster::CreateConsoleUser
(
const char *console_name,
int user
)
{
ScriptVariable *var;
ScriptVariableList *vars;
const char *v;
char varname[256];
// Create a variable that holds the value for the slider.
sprintf(varname,"console.%s",console_name);
var = GetExistingVariable( varname );
if ( !var )
{
vars = GetVarGroup( varname );
if ( !vars )
{
return;
}
v = strchr( varname, '.' );
assert( v );
var = vars->CreateVariable( v + 1, user );
}
else
{
var->setIntValue( user );
}
}
EXPORT_FROM_DLL void ScriptMaster::ConsoleVariable
(
const char *name,
const char *text
)
{
ScriptThread *thread;
ScriptVariable *var;
ScriptVariableList *vars;
Event *ev;
int i,n;
const char *v;
char varname[256];
// Create a variable that holds the value for the slider.
sprintf(varname,"console.%s",name);
var = GetExistingVariable( varname );
if ( !var )
{
vars = GetVarGroup( varname );
if ( !vars )
{
return;
}
v = strchr( varname, '.' );
assert( v );
var = vars->CreateVariable( v + 1, text );
}
else
{
var->setStringValue( text );
}
// Look for threads that are waiting for this variable
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
const char *waiting;
thread = Threads.ObjectAt( i );
assert( thread );
if (thread)
{
waiting = thread->WaitingOnVariable();
if (waiting)
{
if (!strcmp(waiting, name))
{
ev = new Event( EV_ScriptThread_VariableCallback );
thread->ProcessEvent(ev);
}
}
}
}
}
EXPORT_FROM_DLL void ScriptMaster::ConsoleInput
(
const char *name,
const char *text
)
{
ScriptThread *thread;
ScriptVariable *var;
ScriptVariableList *vars;
Event *ev;
int i,n;
const char *v;
char varname[256];
// Create a variable that holds the text.
sprintf(varname,"console.%s",name);
var = GetExistingVariable( varname );
if ( !var )
{
vars = GetVarGroup( varname );
if ( !vars )
{
return;
}
v = strchr( varname, '.' );
assert( v );
var = vars->CreateVariable( v + 1, text );
}
else
{
var->setStringValue( text );
}
// Look for threads that are waiting for input from
// this console.
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
const char *waiting;
thread = Threads.ObjectAt( i );
assert( thread );
if ( thread )
{
waiting = thread->WaitingOnConsole();
if (waiting)
{
if (!strcmp(waiting, name))
{
ev = new Event( EV_ScriptThread_ConsoleCallback );
thread->ProcessEvent(ev);
}
}
}
}
}
EXPORT_FROM_DLL qboolean ScriptMaster::KillThread
(
int num
)
{
ScriptThread *thread;
// Must be safely reentryable so that the thread destructor can tell us that it's being deleted.
thread = GetThread( num );
if ( thread )
{
if ( currentThread == thread )
{
SetCurrentThread( NULL );
}
delete thread;
return true;
}
return false;
}
EXPORT_FROM_DLL qboolean ScriptMaster::RemoveThread
(
int num
)
{
ScriptThread *thread;
int i;
int n;
// Must be safely reentryable so that the thread destructor can tell us that it's being deleted.
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
thread = Threads.ObjectAt( i );
assert( thread );
if ( thread )
{
if ( thread->ThreadNum() == num )
{
Threads.RemoveObjectAt( i );
if ( currentThread == thread )
{
SetCurrentThread( NULL );
}
return true;
}
}
}
return false;
}
EXPORT_FROM_DLL ScriptThread *ScriptMaster::CurrentThread
(
void
)
{
return currentThread;
}
EXPORT_FROM_DLL void ScriptMaster::SetCurrentThread
(
ScriptThread *thread
)
{
currentThread = thread;
}
EXPORT_FROM_DLL ScriptThread *ScriptMaster::CreateThread
(
GameScript *scr,
const char *label,
scripttype_t type
)
{
ScriptThread *thread;
int threadnum;
thread = new ScriptThread;
thread->SetType( type );
threadnum = GetUniqueThreadNumber();
Threads.AddObject( thread );
if ( !thread->Setup( threadnum, scr, label ) )
{
KillThread( threadnum );
return NULL;
}
return thread;
}
//FIXME
// Why can't these be one function?
EXPORT_FROM_DLL ScriptThread *ScriptMaster::CreateThread
(
const char *name,
scripttype_t type,
const char *label
)
{
GameScript *scr;
ScriptThread *thread;
scr = ScriptLib.GetScript( name );
if ( scr )
{
thread = CreateThread( scr, label, type );
return thread;
}
return NULL;
}
EXPORT_FROM_DLL ScriptThread *ScriptMaster::GetThread
(
int num
)
{
int i;
int n;
ScriptThread *thread;
n = Threads.NumObjects();
for( i = 1; i <= n; i++ )
{
thread = Threads.ObjectAt( i );
assert( thread );
if ( thread )
{
if ( thread->ThreadNum() == num )
{
return thread;
}
}
}
return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -