Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 1f3f768

Browse files
authored
Merge pull request #109 from cortex-command-community/CF104-Multiple-Scripts-On-MOs
Cf104 multiple scripts on m os
2 parents ad7a3cf + 67d1c0b commit 1f3f768

23 files changed

+590
-485
lines changed

Activities/GAScripted.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -269,55 +269,34 @@ bool GAScripted::SceneIsCompatible(Scene *pScene, int teams)
269269
return g_LuaMan.GlobalIsDefined("TestScene");
270270
}
271271

272+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
272273

273-
//////////////////////////////////////////////////////////////////////////////////////////
274-
// Virtual Method: EnteredOrbit
275-
//////////////////////////////////////////////////////////////////////////////////////////
276-
// Description: Indicates an Actor as having left the game scene and entered orbit.
277-
// OWNERSHIP IS NOT transferred, as the Actor's inventory is just 'unloaded'.
278-
279-
void GAScripted::EnteredOrbit(Actor *pActor)
280-
{
281-
GameActivity::EnteredOrbit(pActor);
282-
283-
// Save the ACraft actor temporarily to member so the lua function can access it
284-
m_pOrbitedCraft = pActor;
274+
void GAScripted::EnteredOrbit(Actor *orbitedCraft) {
275+
GameActivity::EnteredOrbit(orbitedCraft);
285276

286-
if (m_pOrbitedCraft && g_MovableMan.IsActor(m_pOrbitedCraft))
287-
{
288-
// Call the defined function, but only after first checking if it exists
289-
g_LuaMan.RunScriptString("if " + m_LuaClassName + ".CraftEnteredOrbit then " + m_LuaClassName + ":CraftEnteredOrbit(); end");
290-
291-
// Trigger orbited craft for all global scripts
292-
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr)
293-
if ((*sItr)->IsActive())
294-
(*sItr)->EnteredOrbit(m_pOrbitedCraft);
277+
if (orbitedCraft && g_MovableMan.IsActor(orbitedCraft)) {
278+
g_LuaMan.RunScriptedFunction(m_LuaClassName + ".CraftEnteredOrbit", m_LuaClassName, {m_LuaClassName, m_LuaClassName + ".CraftEnteredOrbit"}, {orbitedCraft});
279+
for (GlobalScript *globalScript : m_GlobalScriptsList) {
280+
if (globalScript->IsActive()) { globalScript->EnteredOrbit(orbitedCraft); }
281+
}
295282
}
296-
297-
// Let go because it's about to be deleted later this frame
298-
m_pOrbitedCraft = 0;
299283
}
300284

301-
void GAScripted::OnPieMenu(Actor *pActor)
302-
{
303-
m_pPieMenuActor = pActor;
304-
if (pActor && g_MovableMan.IsActor(pActor))
305-
{
306-
m_pPieMenuActor->OnPieMenu(pActor);
307-
308-
g_MovableMan.OnPieMenu(pActor);
285+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
309286

310-
// Call the defined function, but only after first checking if it exists
311-
g_LuaMan.RunScriptString("if " + m_LuaClassName + ".OnPieMenu then " + m_LuaClassName + ":OnPieMenu(); end");
312-
313-
// Trigger pie menu for all global scripts
314-
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr)
315-
if ((*sItr)->IsActive())
316-
(*sItr)->OnPieMenu(m_pPieMenuActor);
287+
void GAScripted::OnPieMenu(Actor *pieMenuActor) {
288+
if (pieMenuActor && g_MovableMan.IsActor(pieMenuActor)) {
289+
pieMenuActor->OnPieMenu(pieMenuActor);
290+
g_MovableMan.OnPieMenu(pieMenuActor);
291+
g_LuaMan.RunScriptedFunction(m_LuaClassName + ".OnPieMenu", m_LuaClassName, {m_LuaClassName, m_LuaClassName + ".OnPieMenu"}, {pieMenuActor});
292+
for (GlobalScript *globalScript : m_GlobalScriptsList) {
293+
if (globalScript->IsActive()) { globalScript->OnPieMenu(pieMenuActor); }
294+
}
317295
}
318-
319296
}
320297

298+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
299+
321300
//////////////////////////////////////////////////////////////////////////////////////////
322301
// Virtual method: Start
323302
//////////////////////////////////////////////////////////////////////////////////////////

Activities/GAScripted.h

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,11 @@ ENTITYALLOCATION(GAScripted)
209209
virtual bool SceneIsCompatible(Scene *pScene, int teams = -1);
210210

211211

212-
//////////////////////////////////////////////////////////////////////////////////////////
213-
// Virtual method: EnteredOrbit
214-
//////////////////////////////////////////////////////////////////////////////////////////
215-
// Description: Indicates an Actor as having left the game scene and entered orbit.
216-
// OWNERSHIP IS NOT transferred, as the Actor's inventory is just 'unloaded'.
217-
// Arguments: The actor instance. Ownership IS NOT TRANSFERRED!
218-
// Return value: None.
219-
220-
virtual void EnteredOrbit(Actor *pActor);
212+
/// <summary>
213+
/// Indicates an Actor as having left the game scene and entered orbit. OWNERSHIP IS NOT transferred, as the Actor's inventory is just 'unloaded'.
214+
/// </summary>
215+
/// <param name="orbitedCraft">The actor instance that entered orbit. Ownership IS NOT TRANSFERRED!</param>
216+
virtual void EnteredOrbit(Actor *orbitedCraft);
221217

222218

223219
//////////////////////////////////////////////////////////////////////////////////////////
@@ -283,14 +279,11 @@ ENTITYALLOCATION(GAScripted)
283279
virtual void UpdateGlobalScripts(bool lateUpdate);
284280

285281

286-
//////////////////////////////////////////////////////////////////////////////////////////
287-
// Method: OnPieMenu
288-
//////////////////////////////////////////////////////////////////////////////////////////
289-
// Description: Calls this to be processed by derived classes to enable pie-menu dynamic change
290-
// Arguments: None.
291-
// Return value: None.
292-
293-
virtual void OnPieMenu(Actor *pActor);
282+
/// <summary>
283+
/// Calls this to be processed by derived classes to enable pie-menu dynamic change.
284+
/// </summary>
285+
/// <param name="pieMenuActor">The actor which triggered the pie menu event.</param>
286+
virtual void OnPieMenu(Actor *pieMenuActor);
294287

295288

296289
//////////////////////////////////////////////////////////////////////////////////////////

Activities/GameActivity.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ void GameActivity::Clear()
114114
m_GameOverTimer.Reset();
115115
m_GameOverPeriod = 5000;
116116
m_WinnerTeam = -1;
117-
m_pOrbitedCraft = 0;
118-
m_pPieMenuActor = 0;
119117
}
120118

121119

Activities/GameActivity.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -916,15 +916,7 @@ ENTITYALLOCATION(GameActivity)
916916

917917
void SetNetworkPlayerName(int player, std::string name);
918918

919-
920-
//////////////////////////////////////////////////////////////////////////////////////////
921-
// Method: OnPieMenu
922-
//////////////////////////////////////////////////////////////////////////////////////////
923-
// Description: Calls this to be processed by derived classes to enable pie-menu dynamic change
924-
// Arguments: None.
925-
// Return value: None.
926-
927-
virtual void OnPieMenu(Actor *pActor) { m_pPieMenuActor = pActor; };
919+
virtual void OnPieMenu(Actor *actor) { /* Does nothing, kept here for program control flow. Method is not pure virtual to avoid a bunch of junk implementations in non-scritped activities. */};
928920

929921
virtual void AddPieMenuSlice(std::string description, std::string functionName, PieMenuGUI::Slice::SliceDirection direction, bool isEnabled)
930922
{
@@ -1187,10 +1179,6 @@ ENTITYALLOCATION(GameActivity)
11871179
long m_GameOverPeriod;
11881180
// The winning team number, when the game is over
11891181
int m_WinnerTeam;
1190-
// Temporary member for whatever craft goes into orbit, so Lua can access it. Not owned by this
1191-
Actor *m_pOrbitedCraft;
1192-
// Temporary member for whatever actor has enabled the pie menu. Not owned by this
1193-
Actor *m_pPieMenuActor;
11941182

11951183
std::vector<PieMenuGUI::Slice *> m_CurrentPieMenuSlices;
11961184

Entities/AHuman.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,22 +3141,20 @@ void AHuman::UpdateAI()
31413141
}
31423142
}
31433143

3144-
//////////////////////////////////////////////////////////////////////////////////////////
3145-
// Virtual method: OnPieMenu
3146-
//////////////////////////////////////////////////////////////////////////////////////////
3147-
// Description: Executes the Lua-defined OnPieMenu event handler.
3144+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31483145

3149-
int AHuman::OnPieMenu(Actor * pActor)
3150-
{
3151-
int error = Actor::OnPieMenu(pActor);
3146+
int AHuman::OnPieMenu(Actor *pieMenuActor) {
3147+
int status = Actor::OnPieMenu(pieMenuActor);
31523148

3153-
// Call OnPieMenu handlers for a currently held device if any
3154-
if (m_pFGArm && m_pFGArm->IsAttached() && m_pFGArm->HoldsDevice())
3155-
return m_pFGArm->GetHeldDevice()->OnPieMenu(pActor);
3149+
if (status >= 0 && m_pFGArm && m_pFGArm->IsAttached() && m_pFGArm->HoldsDevice()) {
3150+
return m_pFGArm->GetHeldDevice()->OnPieMenu(pieMenuActor);
3151+
}
31563152

3157-
return 0;
3153+
return status;
31583154
}
31593155

3156+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3157+
31603158
//////////////////////////////////////////////////////////////////////////////////////////
31613159
// Virtual method: Update
31623160
//////////////////////////////////////////////////////////////////////////////////////////

Entities/AHuman.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -916,16 +916,12 @@ ENTITYALLOCATION(AHuman)
916916

917917
virtual void Update();
918918

919-
920-
//////////////////////////////////////////////////////////////////////////////////////////
921-
// Virtual method: OnPieMenu
922-
//////////////////////////////////////////////////////////////////////////////////////////
923-
// Description: Executes the Lua-defined OnPieMenu event handler.
924-
// Arguments: Actor which triggered the pie menu event
925-
// Return value: An error return value signaling sucess or any particular failure.
926-
// Anything below 0 is an error signal.
927-
928-
virtual int OnPieMenu(Actor * pActor);
919+
/// <summary>
920+
/// Executes the Lua-defined OnPieMenu event handler for this AHuman.
921+
/// </summary>
922+
/// <param name="pieMenuActor">The actor which triggered the pie menu event.</param>
923+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
924+
virtual int OnPieMenu(Actor *pieMenuActor);
929925

930926

931927
//////////////////////////////////////////////////////////////////////////////////////////

Entities/Actor.cpp

Lines changed: 20 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -487,41 +487,22 @@ void Actor::Destroy(bool notInherited)
487487
Clear();
488488
}
489489

490+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
490491

491-
//////////////////////////////////////////////////////////////////////////////////////////
492-
// Virtual method: LoadScripts
493-
//////////////////////////////////////////////////////////////////////////////////////////
494-
// Description: Loads the preset scripts of this object, from a specified path.
495-
496-
int Actor::LoadScripts(string scriptPath)
497-
{
498-
if (scriptPath.empty())
499-
return -1;
500-
501-
int error = 0;
502-
503-
// Clear the temporary variable names that will hold the functions read in from the file
504-
if ((error = g_LuaMan.RunScriptString("UpdateAI = nil;")) < 0)
505-
return error;
506-
507-
// Read in the Lua script function definitions for this preset
508-
if ((error = MovableObject::LoadScripts(scriptPath)) < 0)
509-
return error;
510-
511-
// Add the UpdateAI function, if it exists.. if it doesn't, that's not a problem! We'll just be using the old C++ implementation
512-
if (g_LuaMan.GlobalIsDefined("UpdateAI"))
513-
{
514-
// Mark that we have a Lua override for the UpdateAI function
515-
m_ScriptedAIUpdate = true;
516-
if ((error = g_LuaMan.RunScriptString("if UpdateAI then " + m_ScriptPresetName + ".UpdateAI = UpdateAI; end;")) < 0)
517-
return error;
492+
int Actor::LoadScript(std::string const &scriptPath, bool loadAsEnabledScript) {
493+
int status = MOSRotating::LoadScript(scriptPath, loadAsEnabledScript);
494+
if (status < 0) {
495+
return status;
518496
}
519-
else
520-
m_ScriptedAIUpdate = false;
521497

522-
return error;
498+
// If UpdateAI existed it'll be in the lua global namespace, so we can check that to know whether or not to use Lua AI
499+
m_ScriptedAIUpdate = g_LuaMan.GlobalIsDefined("UpdateAI");
500+
501+
return status;
523502
}
524503

504+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
505+
525506

526507
//////////////////////////////////////////////////////////////////////////////////////////
527508
// Virtual method: GetMass
@@ -1301,52 +1282,20 @@ bool Actor::UpdateMovePath()
13011282
return true;
13021283
}
13031284

1285+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13041286

1305-
//////////////////////////////////////////////////////////////////////////////////////////
1306-
// Virtual method: UpdateAIScripted
1307-
//////////////////////////////////////////////////////////////////////////////////////////
1308-
// Description: Updates this' AI state with the provided scripted AI Update function.
1309-
1310-
bool Actor::UpdateAIScripted()
1311-
{
1312-
// This preset doesn't seem to have any script file defined, so then just report we couldn't run scripted AI
1313-
if (!m_ScriptedAIUpdate || m_ScriptPath.empty() || m_ScriptPresetName.empty())
1287+
bool Actor::UpdateAIScripted() {
1288+
if (!m_ScriptedAIUpdate || m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
13141289
return false;
1315-
1316-
int error = 0;
1317-
1318-
// Check to make sure the preset of this is still defined in the Lua state. If not, re-create it and recover gracefully
1319-
if (!g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false))
1320-
ReloadScripts();
1321-
1322-
// First see if we even have a representation stored in the Lua state, and if not, create one
1323-
if (m_ScriptObjectName.empty())
1324-
{
1325-
// Get the unique object identifier for this object and construct the object isntance name in Lua that points to this object so we can pass it into the preset functions
1326-
m_ScriptObjectName = GetClassName() + "s." + g_LuaMan.GetNewObjectID();
1327-
1328-
// Give access to this in the Lua state
1329-
g_MovableMan.SetScriptedEntity(this);
1330-
// Create the Lua variable which will hold the object instance of this instance for as long as it exists
1331-
if ((error = g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);")) < 0)
1332-
return false;
1333-
1334-
// Call the scripted creation function, but only after first checking if it and this instance's Lua representation really exists
1335-
if ((error = g_LuaMan.RunScriptString("if " + m_ScriptPresetName + ".Create and " + m_ScriptObjectName + " then " + m_ScriptPresetName + ".Create(" + m_ScriptObjectName + "); end")) < 0)
1336-
return false;
13371290
}
13381291

1339-
// Call the defined function, but only after first checking if it and this instance's Lua representation exists
1340-
1341-
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1342-
error = g_LuaMan.RunScriptString("if " + m_ScriptPresetName + ".UpdateAI and " + m_ScriptObjectName + " then " + m_ScriptPresetName + ".UpdateAI(" + m_ScriptObjectName + "); end");
1343-
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1292+
int status = !g_LuaMan.ExpressionIsTrue(m_ScriptPresetName, false) ? ReloadScripts() : 0;
1293+
status = (status >= 0 && !ObjectScriptsInitialized()) ? InitializeObjectScripts() : status;
1294+
g_FrameMan.StartPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
1295+
status = (status >= 0) ? RunScriptedFunctionInAppropriateScripts("UpdateAI", false, true) : status;
1296+
g_FrameMan.StopPerformanceMeasurement(FrameMan::PERF_ACTORS_AI);
13441297

1345-
if (error < 0)
1346-
return false;
1347-
1348-
// We made a successful scripted AI update!
1349-
return true;
1298+
return status >= 0;
13501299
}
13511300

13521301

Entities/Actor.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Actor:
7272

7373
// Concrete allocation and cloning definitions
7474
ENTITYALLOCATION(Actor)
75+
ADD_SCRIPT_FUNCTION_NAMES(MOSRotating, "UpdateAI")
7576

7677

7778
//////////////////////////////////////////////////////////////////////////////////////////
@@ -192,16 +193,13 @@ ENTITYALLOCATION(Actor)
192193

193194
virtual void Destroy(bool notInherited = false);
194195

195-
196-
//////////////////////////////////////////////////////////////////////////////////////////
197-
// Virtual method: LoadScripts
198-
//////////////////////////////////////////////////////////////////////////////////////////
199-
// Description: Loads the preset scripts of this object, from a specified path.
200-
// Arguments: None.
201-
// Return value: An error return value signaling sucess or any particular failure.
202-
// Anything below 0 is an error signal.
203-
204-
virtual int LoadScripts(std::string scriptPath);
196+
/// <summary>
197+
/// Loads the script at the given script path onto the object, checking for appropriately named functions within it.
198+
/// </summary>
199+
/// <param name="scriptPath">The path to the script to load.</param>
200+
/// <param name="loadAsEnabledScript">Whether or not the script should load as enabled. Defaults to true.</param>
201+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
202+
virtual int LoadScript(std::string const &scriptPath, bool loadAsEnabledScript = false);
205203

206204

207205
//////////////////////////////////////////////////////////////////////////////////////////
@@ -1330,7 +1328,6 @@ ENTITYALLOCATION(Actor)
13301328

13311329
protected:
13321330

1333-
13341331
// Member variables
13351332
static Entity::ClassInfo m_sClass;
13361333

0 commit comments

Comments
 (0)