Skip to content

Commit 6f59121

Browse files
committed
Allow dynamically enabling/disabling global scripts
1 parent e0a4a22 commit 6f59121

File tree

4 files changed

+80
-33
lines changed

4 files changed

+80
-33
lines changed

Activities/GAScripted.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ void GAScripted::HandleCraftEnteringOrbit(ACraft *orbitedCraft) {
294294

295295
if (orbitedCraft && g_MovableMan.IsActor(orbitedCraft)) {
296296
g_LuaMan.GetMasterScriptState().RunScriptFunctionString(m_LuaClassName + ".CraftEnteredOrbit", m_LuaClassName, {m_LuaClassName, m_LuaClassName + ".CraftEnteredOrbit"}, {orbitedCraft});
297-
for (const GlobalScript *globalScript : m_GlobalScriptsList) {
298-
if (globalScript->IsActive()) { globalScript->HandleCraftEnteringOrbit(orbitedCraft); }
297+
for (GlobalScript *globalScript : m_GlobalScriptsList) {
298+
globalScript->HandleCraftEnteringOrbit(orbitedCraft);
299299
}
300300
}
301301
}
@@ -346,18 +346,11 @@ int GAScripted::Start() {
346346
g_PresetMan.GetAllOfType(globalScripts, "GlobalScript");
347347

348348
for (std::list<Entity *>::iterator sItr = globalScripts.begin(); sItr != globalScripts.end(); ++sItr) {
349-
GlobalScript * script = dynamic_cast<GlobalScript *>(*sItr);
350-
if (script && g_SettingsMan.IsGlobalScriptEnabled(script->GetModuleAndPresetName())) {
351-
m_GlobalScriptsList.push_back(dynamic_cast<GlobalScript*>(script->Clone()));
352-
}
349+
m_GlobalScriptsList.push_back(dynamic_cast<GlobalScript*>((*sItr)->Clone()));
353350
}
354351

355352
// Start all global scripts
356353
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
357-
if (g_SettingsMan.PrintDebugInfo()) {
358-
g_ConsoleMan.PrintString("DEBUG: Start Global Script: " + (*sItr)->GetPresetName());
359-
}
360-
361354
(*sItr)->Start();
362355
}
363356

@@ -377,9 +370,7 @@ void GAScripted::SetPaused(bool pause) {
377370

378371
// Pause all global scripts
379372
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
380-
if ((*sItr)->IsActive()) {
381-
(*sItr)->Pause(pause);
382-
}
373+
(*sItr)->Pause(pause);
383374
}
384375
}
385376

@@ -396,12 +387,7 @@ void GAScripted::End() {
396387

397388
// End all global scripts
398389
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
399-
if ((*sItr)->IsActive()) {
400-
if (g_SettingsMan.PrintDebugInfo()) {
401-
g_ConsoleMan.PrintString("DEBUG: End Global Script: " + (*sItr)->GetPresetName());
402-
}
403-
(*sItr)->End();
404-
}
390+
(*sItr)->End();
405391
}
406392

407393
// Delete all global scripts, in case destructor is not called when activity restarts
@@ -464,7 +450,7 @@ void GAScripted::Update() {
464450
void GAScripted::UpdateGlobalScripts(bool lateUpdate) {
465451
// Update all global scripts
466452
for (std::vector<GlobalScript *>::iterator sItr = m_GlobalScriptsList.begin(); sItr < m_GlobalScriptsList.end(); ++sItr) {
467-
if ((*sItr)->IsActive() && (*sItr)->ShouldLateUpdate() == lateUpdate) {
453+
if ((*sItr)->ShouldLateUpdate() == lateUpdate) {
468454
(*sItr)->Update();
469455
}
470456
}
@@ -580,10 +566,8 @@ void GAScripted::AddPieSlicesToActiveActorPieMenus() {
580566
controlledActorPieMenu->AddPieSliceIfPresetNameIsUnique(pieSlice.get(), this, true);
581567
}
582568
for (const GlobalScript *globalScript : m_GlobalScriptsList) {
583-
if (globalScript->IsActive()) {
584-
for (const std::unique_ptr<PieSlice> &pieSlice : globalScript->GetPieSlicesToAdd()) {
585-
controlledActorPieMenu->AddPieSliceIfPresetNameIsUnique(pieSlice.get(), globalScript, true);
586-
}
569+
for (const std::unique_ptr<PieSlice> &pieSlice : globalScript->GetPieSlicesToAdd()) {
570+
controlledActorPieMenu->AddPieSliceIfPresetNameIsUnique(pieSlice.get(), globalScript, true);
587571
}
588572
}
589573
}

Entities/Activity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ namespace RTE {
190190
int GetInCampaignStage() const { return m_InCampaignStage; }
191191

192192
/// <summary>
193+
///
193194
/// Sets in which stage of the Campaign this appears.
194195
/// </summary>
195196
/// <param name="newStage">The new stage to set. -1 means it doesn't appear in the campaign.</param>

Entities/GlobalScript.cpp

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "ACraft.h"
88
#include "PieSlice.h"
99

10+
#include "ConsoleMan.h"
11+
#include "SettingsMan.h"
12+
1013
namespace RTE {
1114

1215
ConcreteClassInfo(GlobalScript, Entity, 10);
@@ -16,7 +19,8 @@ namespace RTE {
1619
void GlobalScript::Clear() {
1720
m_ScriptPath.clear();
1821
m_LuaClassName.clear();
19-
m_IsActive = false;
22+
m_IsActive = true;
23+
m_HasStarted = false;
2024
m_LateUpdate = false;
2125
m_PieSlicesToAdd.clear();
2226
}
@@ -29,6 +33,7 @@ namespace RTE {
2933
m_ScriptPath = reference.m_ScriptPath;
3034
m_LuaClassName = reference.m_LuaClassName;
3135
m_IsActive = reference.m_IsActive;
36+
m_HasStarted = reference.m_HasStarted;
3237
m_LateUpdate = reference.m_LateUpdate;
3338

3439
for (const std::unique_ptr<PieSlice> &referencePieSliceToAdd : reference.m_PieSlicesToAdd) {
@@ -67,6 +72,17 @@ namespace RTE {
6772
return 0;
6873
}
6974

75+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76+
77+
const std::vector<std::unique_ptr<PieSlice>>& GlobalScript::GetPieSlicesToAdd() const {
78+
const std::vector<std::unique_ptr<PieSlice>> emptyVector;
79+
if (!m_HasStarted || !m_IsActive || !g_SettingsMan.IsGlobalScriptEnabled(GetModuleAndPresetName())) {
80+
return emptyVector;
81+
}
82+
83+
return m_PieSlicesToAdd;
84+
}
85+
7086
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7187

7288
int GlobalScript::ReloadScripts() {
@@ -86,37 +102,82 @@ namespace RTE {
86102
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87103

88104
int GlobalScript::Start() {
105+
if (!g_SettingsMan.IsGlobalScriptEnabled(GetModuleAndPresetName())) {
106+
return 0;
107+
}
108+
109+
if (g_SettingsMan.PrintDebugInfo()) {
110+
g_ConsoleMan.PrintString("DEBUG: Start Global Script: " + GetPresetName());
111+
}
112+
89113
int error = ReloadScripts();
90-
if (error == 0) { error = g_LuaMan.GetMasterScriptState().RunScriptString("if " + m_LuaClassName + ".StartScript then " + m_LuaClassName + ":StartScript(); end"); }
91-
m_IsActive = error == 0;
114+
if (error == 0) {
115+
error = g_LuaMan.GetMasterScriptState().RunScriptString("if " + m_LuaClassName + ".StartScript then " + m_LuaClassName + ":StartScript(); end");
116+
m_HasStarted = true;
117+
}
92118

119+
m_IsActive = error == 0;
93120
return error;
94121
}
95122

96123
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97124

98125
int GlobalScript::Pause(bool pause) const {
126+
if (!m_IsActive || !m_HasStarted || !g_SettingsMan.IsGlobalScriptEnabled(GetModuleAndPresetName())) {
127+
return 0;
128+
}
129+
99130
return g_LuaMan.GetMasterScriptState().RunScriptString("if " + m_LuaClassName + ".PauseScript then " + m_LuaClassName + ":PauseScript(" + (pause ? "true" : "false") + "); end");
100131
}
101132

102133
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103134

104135
int GlobalScript::End() const {
136+
if (!m_HasStarted) {
137+
return 0;
138+
}
139+
140+
if (g_SettingsMan.PrintDebugInfo()) {
141+
g_ConsoleMan.PrintString("DEBUG: End Global Script: " + GetPresetName());
142+
}
143+
105144
return g_LuaMan.GetMasterScriptState().RunScriptString("if " + m_LuaClassName + ".EndScript then " + m_LuaClassName + ":EndScript(); end");
106145
}
107146

108147
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109148

110-
void GlobalScript::HandleCraftEnteringOrbit(const ACraft *orbitedCraft) const {
111-
if (orbitedCraft && g_MovableMan.IsActor(orbitedCraft)) {
112-
g_LuaMan.GetMasterScriptState().RunScriptFunctionString(m_LuaClassName + ".CraftEnteredOrbit", m_LuaClassName, { m_LuaClassName, m_LuaClassName + ".CraftEnteredOrbit" }, { orbitedCraft });
149+
void GlobalScript::HandleCraftEnteringOrbit(const ACraft *orbitedCraft) {
150+
if (!m_IsActive || !!m_HasStarted || orbitedCraft == nullptr || !g_MovableMan.IsActor(orbitedCraft) || !g_SettingsMan.IsGlobalScriptEnabled(GetModuleAndPresetName())) {
151+
return;
152+
}
153+
154+
int error = g_LuaMan.GetMasterScriptState().RunScriptFunctionString(m_LuaClassName + ".CraftEnteredOrbit", m_LuaClassName, { m_LuaClassName, m_LuaClassName + ".CraftEnteredOrbit" }, { orbitedCraft });
155+
if (error) {
156+
m_IsActive = false;
113157
}
114158
}
115159

116160
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
117161

118162
void GlobalScript::Update() {
163+
if (!m_IsActive) {
164+
return;
165+
}
166+
167+
if (!g_SettingsMan.IsGlobalScriptEnabled(GetModuleAndPresetName())) {
168+
if (m_HasStarted) {
169+
End();
170+
}
171+
return;
172+
}
173+
174+
if (!m_HasStarted) {
175+
Start();
176+
}
177+
119178
int error = g_LuaMan.GetMasterScriptState().RunScriptString("if " + m_LuaClassName + ".UpdateScript then " + m_LuaClassName + ":UpdateScript(); end");
120-
if (error) { SetActive(false); }
179+
if (error) {
180+
m_IsActive = false;
181+
}
121182
}
122183
}

Entities/GlobalScript.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ namespace RTE {
8080
/// Gets the list of PieSlices this GlobalScript adds to any active Actor PieMenus.
8181
/// </summary>
8282
/// <returns>The list of PieSilces this GlobalScript adds to any active Actor PieMenus</returns>
83-
const std::vector<std::unique_ptr<PieSlice>> & GetPieSlicesToAdd() const { return m_PieSlicesToAdd; }
83+
const std::vector<std::unique_ptr<PieSlice>>& GetPieSlicesToAdd() const;
8484
#pragma endregion
8585

8686
#pragma region Concrete Methods
@@ -113,7 +113,7 @@ namespace RTE {
113113
/// Handles when an ACraft has left the game scene and entered orbit by running the appropriate Lua function. Ownership is NOT transferred!
114114
/// </summary>
115115
/// <param name="orbitedCraft">The ACraft instance that entered orbit. Ownership is NOT transferred!</param>
116-
void HandleCraftEnteringOrbit(const ACraft *orbitedCraft) const;
116+
void HandleCraftEnteringOrbit(const ACraft *orbitedCraft);
117117

118118
/// <summary>
119119
/// Updates the state of this GlobalScript every frame.
@@ -128,6 +128,7 @@ namespace RTE {
128128
std::string m_ScriptPath; //!< The path to the Lua script file that defines this' behaviors in update.
129129
std::string m_LuaClassName; //!< The name of the class (table) defining the logic of this in Lua, as specified in the script file.
130130
bool m_IsActive; //!< Whether this GlobalScript is currently allowed to run.
131+
bool m_HasStarted; //!< Whether this script has already been started.
131132
bool m_LateUpdate; //!< Whether or not this GlobalScript should be updated late, i.e. after the standard MovableMan update.
132133

133134
std::vector<std::unique_ptr<PieSlice>> m_PieSlicesToAdd; //!< A vector of PieSlices that should be added to any PieMenus opened while this GlobalScript is active.

0 commit comments

Comments
 (0)