📄 script.cpp
字号:
//----------------------------------------------------------
//
// MODULE : SCRIPT.CPP
//
// PURPOSE : Script object
//
// CREATED : 01/13/98
//
//----------------------------------------------------------
// Includes...
#include <stdio.h>
#include "generic_msg_de.h"
#include "Script.h"
#include "SharedDefs.h"
#include "ObjectUtilities.h"
#include "ClientServerShared.h"
#include "CameraObj.h"
#include "FireFX.h"
#include "Trigger.h"
#include "Explosion.h"
#include "SoundTypes.h"
#include <mbstring.h>
// Subgroup commands
// 015 :OBJECT
// 001 CREATE
// 002 MOVE
//
char *NextToken(char *pszString, const char *pszSeps);
BEGIN_CLASS(Script)
ADD_STRINGPROP(ScriptName, "script.txt") // Script file to load
ADD_REALPROP(StartTime, 0.0f) // Start time, or wait for Trigger
END_CLASS_DEFAULT_FLAGS(Script, B2BaseClass, NULL, NULL, CF_ALWAYSLOAD)
// ----------------------------------------------------------------------- //
//
// ROUTINE: Script::Script()
//
// PURPOSE: Initialize object
//
// ----------------------------------------------------------------------- //
Script::Script() : B2BaseClass(OT_NORMAL)
{
m_nObjectIndex = 0;
m_nCameraIndex = 0;
m_nCurrLine = 0;
m_hstrScriptName = DNULL;
m_bScriptLoaded = DFALSE;
m_bCheckOverTime = DFALSE;
m_bCheckStartTime = DTRUE;
m_nDefineCnt = 0;
m_bScriptStarted = DFALSE;
memset(ScriptObject, 0, sizeof(ScriptObject));
memset(ScriptCamera, 0, sizeof(ScriptCamera));
memset(ScriptSound, 0, sizeof(ScriptSound));
for (int i=0; i<MAX_MOVEOBJS+1; i++)
{
m_nMoveObject[i] = -1;
m_nMoveCamera[i] = -1;
}
}
// ----------------------------------------------------------------------- //
//
// ROUTINE: Script::~Script()
//
// PURPOSE: Deallocate object
//
// ----------------------------------------------------------------------- //
Script::~Script()
{
if (!g_pServerDE) return;
if (m_hstrScriptName)
g_pServerDE->FreeString(m_hstrScriptName);
}
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::ReadProp()
//
// PURPOSE: Reads properties
//
// --------------------------------------------------------------------------- //
DBOOL Script::ReadProp(ObjectCreateStruct *pStruct)
{
if (!g_pServerDE) return DFALSE;
GenericProp genProp;
if (g_pServerDE->GetPropGeneric("ScriptName", &genProp) == DE_OK)
{
if (genProp.m_String[0]) m_hstrScriptName = g_pServerDE->CreateString(genProp.m_String);
}
if (g_pServerDE->GetPropGeneric("StartTime", &genProp) == DE_OK)
{
m_fStartTime = genProp.m_Float;
if (m_fStartTime <= 0.0f) m_fStartTime = 0.001f;
}
return DTRUE;
}
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::PostPropRead()
//
// PURPOSE: Initializes data
//
// --------------------------------------------------------------------------- //
void Script::PostPropRead(ObjectCreateStruct *pStruct)
{
// Set the Update!
pStruct->m_NextUpdate = 0.01f;
}
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::InitialUpdate()
//
// PURPOSE: Initializes data.
//
// --------------------------------------------------------------------------- //
DBOOL Script::InitialUpdate(DVector *pMovement)
{
// load the script
if (!ProcessScript())
return DFALSE;
// Set next update to the script start time
g_pServerDE->SetNextUpdate(m_hObject, m_fStartTime);
return DTRUE;
}
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::ProcessScript()
//
// PURPOSE: Load the Script and parse the commands (1 line per update)
//
// --------------------------------------------------------------------------- //
DBOOL Script::ProcessScript()
{
int nEOF = 0;
// LoadScriptStart
// Open the File
// Make sure the Script data is Clear
m_scriptCmdList.RemoveAll();
// Initial Update... Lets read in the Script File.
if (m_hstrScriptName)
{
if (!(infile = fopen(g_pServerDE->GetStringData(m_hstrScriptName), "r")))
{
return DFALSE;
}
}
else
{
g_pServerDE->BPrint("ERROR Scriptfile not found or PreProcessing Error.");
g_pServerDE->SetNextUpdate(m_hObject, 0.0f);
g_pServerDE->RemoveObject(m_hObject);
return DFALSE;
}
// Initialize the time cout
m_fNextScriptTime = 0.0f;
while (nEOF == 0)
{
if (LoadScript(nEOF) == DFALSE)
{
// If Returns DFALSE then ERROR
return DFALSE;
}
}
m_bScriptLoaded = DTRUE;
fclose(infile);
return DTRUE;
}
/*
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::CheckStartTime()
//
// PURPOSE: Check to see if its time to run the script
//
// --------------------------------------------------------------------------- //
DBOOL Script::CheckStartTime()
{
// StartTime check
// If StartTime = 0.0 then wait for a Trigger
// if StartTime = -1.0 then Stop the Script (set the StartTime to 0.0
// Trigger STOP
if (m_fStartTime < 0.0f)
{
// Slow down the Update
g_pServerDE->SetNextUpdate(m_hObject, 0.1f);
m_fStartTime = 0.0f;
return DTRUE;
}
else if (m_fStartTime == 0.0f)
{
// Wait for Trigger to Set timer
return DTRUE;
}
// Check if its TIME to Start Script
if (g_pServerDE->GetTime() > m_fScriptTime + m_fStartTime )
{
// Increase the Update
g_pServerDE->SetNextUpdate(m_hObject, 0.01f);
m_fScriptTime = g_pServerDE->GetTime();
m_bCheckStartTime = DFALSE;
}
else
{
return DTRUE;
}
return DTRUE;
}
*/
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::Update()
//
// PURPOSE: Script update function, updates the current state.
//
// --------------------------------------------------------------------------- //
DBOOL Script::Update(DVector *pMovement)
{
g_pServerDE->SetNextUpdate(m_hObject, (DFLOAT).01);
//
// First Load the script
//
if (m_bScriptLoaded == DFALSE)
{
return DFALSE;
}
m_curScriptCmd.command = SCRIPT_SCMD_UNKNOWN;
// Check for Movement of Objects...
if (m_bCheckOverTime == DTRUE)
{
DBOOL bStillUpdate = DFALSE;
// Only Support MAX_MOVEOBJS at 1 time
for (int i=0; i<MAX_MOVEOBJS+1; i++)
{
if (m_nMoveObject[i] != -1)
{
if (ScriptObject[m_nMoveObject[i]].m_bMovement == DTRUE)
{
ObjectData *pObj = &ScriptObject[m_nMoveObject[i]];
if (g_pServerDE->GetTime() > pObj->m_fStartTime + pObj->m_fDuration)
{
pObj->m_bMovement = DFALSE;
m_nMoveObject[i] = -1;
}
else
{
pObj->m_fCurrentTime = g_pServerDE->GetTime();
ObjectMoveOverTimeUpdate(pObj);
bStillUpdate = DTRUE;
}
}
}
if (m_nMoveCamera[i] != -1)
{
if (ScriptCamera[m_nMoveCamera[i]].m_bMovement == DTRUE)
{
ObjectData *pObj = &ScriptCamera[m_nMoveCamera[i]];
if (g_pServerDE->GetTime() > pObj->m_fStartTime + pObj->m_fDuration)
{
pObj->m_bMovement = DFALSE;
m_nMoveCamera[i] = -1;
}
else
{
pObj->m_fCurrentTime = g_pServerDE->GetTime();
ObjectMoveOverTimeUpdate(pObj);
bStillUpdate = DTRUE;
}
}
}
}
if (!bStillUpdate)
m_bCheckOverTime = DFALSE;
}
//
// Check for script Updates
//
while (m_scriptCmdList.GetNumItems() > m_nCurrLine &&
m_scriptCmdList[m_nCurrLine]->m_bExecuted == DFALSE &&
g_pServerDE->GetTime() >= m_fNextScriptTime)
{
switch (m_scriptCmdList[m_nCurrLine]->command)
{
case SCRIPT_SCMD_START: StartScript(); break;
case SCRIPT_SCMD_CUTSCENE : SetCutScene(); break;
case SCRIPT_SCMD_SUBTITLE_DISPLAY : SubTitleDisplay(); break;
case SCRIPT_SCMD_SUBTITLE_FADE : SubTitleFade(); break;
case SCRIPT_SCMD_OBJECT_CREATE_AT : ObjectCreateAt(); break;
case SCRIPT_SCMD_OBJECT_CREATE_NEAR : ObjectCreateNear(); break;
case SCRIPT_SCMD_OBJECT_ASSIGN : ObjectAssign(); break;
case SCRIPT_SCMD_OBJECT_CREATE_MODEL_AT : ObjectCreateModelAt(); break;
case SCRIPT_SCMD_OBJECT_CREATE_MODEL_NEAR: ObjectCreateModelNear(); break;
case SCRIPT_SCMD_OBJECT_CREATE_FIRE : ObjectCreateFire(); break;
case SCRIPT_SCMD_OBJECT_CREATE_EXPLOSION_AT: ObjectCreateExplosionAt(); break;
case SCRIPT_SCMD_OBJECT_CREATE_EXPLOSION_NEAR: ObjectCreateExplosionNear(); break;
case SCRIPT_SCMD_OBJECT_MOVE : ObjectMove(); break;
case SCRIPT_SCMD_OBJECT_MOVE_OVERTIME : ObjectMoveObj(); break;
case SCRIPT_SCMD_OBJECT_TELEPORT : ObjectTeleport(); break;
case SCRIPT_SCMD_OBJECT_FLAGS : ObjectFlags(); break;
case SCRIPT_SCMD_OBJECT_FLAG_VISIBLE : ObjectFlagVisible();break;
case SCRIPT_SCMD_OBJECT_FLAG_SOLID : ObjectFlagSolid();break;
case SCRIPT_SCMD_OBJECT_FLAG_GRAVITY : ObjectFlagGravity();break;
case SCRIPT_SCMD_OBJECT_DIMS : ObjectDims(); break;
case SCRIPT_SCMD_OBJECT_GIB : ObjectGib(); break;
case SCRIPT_SCMD_OBJECT_SCALE : ObjectScale(); break;
case SCRIPT_SCMD_OBJECT_DESTROY : ObjectDestroy(); break;
case SCRIPT_SCMD_WAIT : WaitDelay(); break;
case SCRIPT_SCMD_PLAY_ANIM : PlayAnimation(); break;
case SCRIPT_SCMD_PLAY_SOUND_POS : PlaySoundPos(); break;
case SCRIPT_SCMD_PLAY_SOUND_OBJ : PlaySoundObj(); break;
case SCRIPT_SCMD_KILL_SOUND : KillSound(); break;
// case SCRIPT_SCMD_CAMERA_RESET : CameraReset(); break;
case SCRIPT_SCMD_CAMERA_CREATE : CameraCreate(); break;
case SCRIPT_SCMD_CAMERA_DESTROY : CameraDestroy(); break;
case SCRIPT_SCMD_CAMERA_SELECT : CameraSelect(); break;
case SCRIPT_SCMD_CAMERA_MOVE_OBJ : CameraMoveObj(); break;
case SCRIPT_SCMD_CAMERA_TELEPORT : CameraTeleport(); break;
case SCRIPT_SCMD_CAMERA_TELEPORT_OBJ : CameraTeleportObj();break;
case SCRIPT_SCMD_CAMERA_ROTATE : CameraRotate(); break;
case SCRIPT_SCMD_CAMERA_LINKSPOT : CameraLinkSpot(); break;
case SCRIPT_SCMD_CAMERA_ZOOM : CameraZoom(); break;
case SCRIPT_SCMD_ARG : break;
case SCRIPT_SCMD_SEND_AI_SCRIPT : SendAIScript(); break;
case SCRIPT_SCMD_SEND_TRIGGER : SendTrigger(); break;
case SCRIPT_SCMD_SEND_TRIGGER_NAMED : SendTriggerNamed(); break;
case SCRIPT_SCMD_END : EndScript(); break;
default:
{
g_pServerDE->BPrint("Error: Unknown Command: %d", m_scriptCmdList[m_nCurrLine]->command);
return DFALSE;
}
}
m_scriptCmdList[m_nCurrLine]->m_bExecuted = DTRUE;
// Check for Last line
if (m_scriptCmdList.GetNumItems() > m_nCurrLine)
{
m_nCurrLine++;
}
else
{
break;
// Turn off update, maybe remove object?
// g_pServerDE->BPrint("Script DONE!");
}
}
return DTRUE;
}
// --------------------------------------------------------------------------- //
//
// ROUTINE: Script::EngineMessageFn()
//
// PURPOSE: Handler for engine messages
//
// --------------------------------------------------------------------------- //
DDWORD Script::EngineMessageFn(DDWORD messageID, void *pData, float fData)
{
switch (messageID)
{
case MID_UPDATE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -