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

Commit

Permalink
Added global messages (sent to activity and all MOs in simulation)
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Nov 5, 2023
1 parent 31aa270 commit 6acc92a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Activities/GAScripted.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class GAScripted : public GameActivity {

public:

ScriptFunctionNames("StartActivity", "UpdateActivity", "PauseActivity", "EndActivity", "OnSave", "CraftEnteredOrbit", "OnMessage");
ScriptFunctionNames("StartActivity", "UpdateActivity", "PauseActivity", "EndActivity", "OnSave", "CraftEnteredOrbit", "OnMessage", "OnGlobalMessage");

// Concrete allocation and cloning definitions
EntityAllocation(GAScripted);
Expand Down
2 changes: 1 addition & 1 deletion Entities/MovableObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ friend struct EntityLuaBindings;

public:

ScriptFunctionNames("Create", "Destroy", "Update", "SyncedUpdate", "OnScriptDisable", "OnScriptEnable", "OnCollideWithTerrain", "OnCollideWithMO", "WhilePieMenuOpen", "OnGameSave", "OnMessage");
ScriptFunctionNames("Create", "Destroy", "Update", "SyncedUpdate", "OnScriptDisable", "OnScriptEnable", "OnCollideWithTerrain", "OnCollideWithMO", "WhilePieMenuOpen", "OnGameSave", "OnMessage", "OnGlobalMessage");
SerializableOverrideMethods;
ClassInfoGetters;

Expand Down
3 changes: 3 additions & 0 deletions Lua/LuaAdapterDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ namespace RTE {
/// <param name="movableMan">A reference to MovableMan, provided by Lua.</param>
/// <param name="particle">A pointer to the particle to be added.</param>
static void AddParticle(MovableMan &movableMan, MovableObject *particle);

static void SendGlobalMessage1(MovableMan &movableMan, const std::string& message);
static void SendGlobalMessage2(MovableMan &movableMan, const std::string& message, luabind::object context);
};
#pragma endregion

Expand Down
20 changes: 20 additions & 0 deletions Lua/LuaAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ namespace RTE {
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersMovableMan::SendGlobalMessage1(MovableMan &movableMan, const std::string &message) {
luabind::object context;
SendGlobalMessage2(movableMan, message, context);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void LuaAdaptersMovableMan::SendGlobalMessage2(MovableMan &movableMan, const std::string &message, luabind::object context) {
LuabindObjectWrapper wrapper(&context, "", false);

GAScripted* scriptedActivity = dynamic_cast<GAScripted*>(g_ActivityMan.GetActivity());
if (scriptedActivity) {
scriptedActivity->RunLuaFunction("OnGlobalMessage", {}, { message }, { &wrapper });
}

movableMan.RunLuaFunctionOnAllMOs("OnGlobalMessage", {}, { message }, { &wrapper });
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

double LuaAdaptersTimerMan::GetDeltaTimeTicks(const TimerMan &timerMan) {
Expand Down
4 changes: 3 additions & 1 deletion Lua/LuaBindingsManagers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ namespace RTE {
.def("GetMOsInRadius", (const std::vector<MovableObject *> * (MovableMan::*)(const Vector &centre, float radius) const)&MovableMan::GetMOsInRadius, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)
.def("GetMOsInRadius", (const std::vector<MovableObject *> * (MovableMan::*)(const Vector &centre, float radius, int ignoreTeam) const)&MovableMan::GetMOsInRadius, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)
.def("GetMOsInRadius", (const std::vector<MovableObject *> * (MovableMan::*)(const Vector &centre, float radius, int ignoreTeam, bool getsHitByMOsOnly) const)&MovableMan::GetMOsInRadius, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)


.def("SendGlobalMessage", &LuaAdaptersMovableMan::SendGlobalMessage1)
.def("SendGlobalMessage", &LuaAdaptersMovableMan::SendGlobalMessage2)
.def("AddMO", &LuaAdaptersMovableMan::AddMO, luabind::adopt(_2))
.def("AddActor", &LuaAdaptersMovableMan::AddActor, luabind::adopt(_2))
.def("AddItem", &LuaAdaptersMovableMan::AddItem, luabind::adopt(_2))
Expand Down
1 change: 0 additions & 1 deletion Managers/LuaMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Singleton.h"
#include "Entity.h"
#include "RTETools.h"
#include "LuabindObjectWrapper.h"

#define g_LuaMan LuaMan::Instance()

Expand Down
50 changes: 46 additions & 4 deletions Managers/MovableMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,11 +1551,9 @@ void MovableMan::RedrawOverlappingMOIDs(MovableObject *pOverlapsThis)
}
}

void updateMultiThreadedScripts(MovableObject* mo, LuaStateWrapper* luaState) {
if (!luaState || mo->GetLuaState() == luaState) {
mo->UpdateScripts(ThreadScriptsToRun::MultiThreaded);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void updateMultiThreadedScripts(MovableObject* mo, LuaStateWrapper* luaState) {
if (MOSRotating* mosr = dynamic_cast<MOSRotating*>(mo)) {
for (auto attachablrItr = mosr->GetAttachableList().begin(); attachablrItr != mosr->GetAttachableList().end(); ) {
Attachable* attachable = *attachablrItr;
Expand All @@ -1577,8 +1575,52 @@ void updateMultiThreadedScripts(MovableObject* mo, LuaStateWrapper* luaState) {
updateMultiThreadedScripts(wound, luaState);
}
}

if (!luaState || mo->GetLuaState() == luaState) {
mo->UpdateScripts(ThreadScriptsToRun::MultiThreaded);
}
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void callLuaFunctionOnMORecursive(MovableObject* mo, const std::string& functionName, const std::vector<const Entity*>& functionEntityArguments, const std::vector<std::string_view>& functionLiteralArguments, const std::vector<LuabindObjectWrapper*>& functionObjectArguments, ThreadScriptsToRun scriptsToRun) {
if (MOSRotating* mosr = dynamic_cast<MOSRotating*>(mo)) {
for (auto attachablrItr = mosr->GetAttachableList().begin(); attachablrItr != mosr->GetAttachableList().end(); ) {
Attachable* attachable = *attachablrItr;
++attachablrItr;

attachable->RunScriptedFunctionInAppropriateScripts("OnGlobalMessage", false, false, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
callLuaFunctionOnMORecursive(attachable, functionName, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
}

for (auto woundItr = mosr->GetWoundList().begin(); woundItr != mosr->GetWoundList().end(); ) {
AEmitter* wound = *woundItr;
++woundItr;

wound->RunScriptedFunctionInAppropriateScripts("OnGlobalMessage", false, false, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
callLuaFunctionOnMORecursive(wound, functionName, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
}
}

mo->RunScriptedFunctionInAppropriateScripts("OnGlobalMessage", false, false, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void MovableMan::RunLuaFunctionOnAllMOs(const std::string &functionName, const std::vector<const Entity*> &functionEntityArguments, const std::vector<std::string_view> &functionLiteralArguments, const std::vector<LuabindObjectWrapper*> &functionObjectArguments, ThreadScriptsToRun scriptsToRun) {
for (Actor *actor : m_Actors) {
callLuaFunctionOnMORecursive(actor, functionName, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
}

for (MovableObject *item : m_Items) {
callLuaFunctionOnMORecursive(item, functionName, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
}

for (MovableObject* particle : m_Particles) {
callLuaFunctionOnMORecursive(particle, functionName, functionEntityArguments, functionLiteralArguments, functionObjectArguments, scriptsToRun);
}
}

//////////////////////////////////////////////////////////////////////////////////////////
// Method: Update
//////////////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions Managers/MovableMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AHuman;
class SceneLayer;
class SceneObject;
class Box;
class LuabindObjectWrapper;


//////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -940,6 +941,10 @@ class MovableMan : public Singleton<MovableMan>, public Serializable {
/// <returns>Pointers to the MOs that are within the specified radius of the given centre position.</returns>
const std::vector<MovableObject *> * GetMOsInRadius(const Vector &centre, float radius) const { return GetMOsInRadius(centre, radius, Activity::NoTeam); }

/// <summary>
/// Runs a lua function on all MOs in the simulation, including owned child MOs.
/// </summary>
void RunLuaFunctionOnAllMOs(const std::string& functionName, const std::vector<const Entity*>& functionEntityArguments = std::vector<const Entity*>(), const std::vector<std::string_view>& functionLiteralArguments = std::vector<std::string_view>(), const std::vector<LuabindObjectWrapper*>& functionObjectArguments = std::vector<LuabindObjectWrapper*>(), ThreadScriptsToRun scriptsToRun = ThreadScriptsToRun::Both);

//////////////////////////////////////////////////////////////////////////////////////////
// Protected member variable and method declarations
Expand Down

0 comments on commit 6acc92a

Please sign in to comment.