mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-10 17:38:08 +00:00
doesnt compile
This commit is contained in:
@@ -504,10 +504,7 @@ void Entity::Initialize() {
|
||||
}
|
||||
|
||||
AddCallbackTimer(0.0f, [this]() {
|
||||
auto scripts = CppScripts::GetEntityScripts(this);
|
||||
std::for_each(scripts.begin(), scripts.end(), [this](const auto& script) {
|
||||
script->OnStartup(this);
|
||||
});
|
||||
GetScript()->OnStartup(this);
|
||||
});
|
||||
|
||||
// Load data specific to this LOT first. These act as defaults for the components.
|
||||
@@ -523,7 +520,7 @@ void Entity::Initialize() {
|
||||
});
|
||||
|
||||
/**
|
||||
* @brief Startup all the components. Some components need or want data from other components so
|
||||
* Startup all the components. Some components need or want data from other components so
|
||||
* we want to ensure that
|
||||
* A) Most if not all components are newed and ready to be accessed.
|
||||
* B) All components have their personal data loaded and ready to be used.
|
||||
@@ -533,7 +530,7 @@ void Entity::Initialize() {
|
||||
});
|
||||
|
||||
/**
|
||||
* @brief Load the player save data from XML. Ideally we do this after all initialization so the player
|
||||
* Load the player save data from XML. Ideally we do this after all initialization so the player
|
||||
* save data overrides any defaults that may be applied.
|
||||
*/
|
||||
if (!IsPlayer()) std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) {
|
||||
@@ -647,7 +644,7 @@ void Entity::SetGMLevel(eGameMasterLevel value) {
|
||||
GameMessages::SendGMLevelBroadcast(m_ObjectID, value);
|
||||
}
|
||||
|
||||
void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, eReplicaPacketType packetType) {
|
||||
void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, const eReplicaPacketType packetType) {
|
||||
if (packetType == eReplicaPacketType::CONSTRUCTION) {
|
||||
outBitStream->Write(m_ObjectID);
|
||||
outBitStream->Write(m_TemplateID);
|
||||
@@ -768,7 +765,7 @@ void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, eReplicaPacke
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::WriteComponents(RakNet::BitStream* outBitStream, eReplicaPacketType packetType) {
|
||||
void Entity::WriteComponents(RakNet::BitStream* outBitStream, const eReplicaPacketType packetType) {
|
||||
|
||||
}
|
||||
|
||||
@@ -787,34 +784,36 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) {
|
||||
}
|
||||
}
|
||||
|
||||
CppScripts::Script* Entity::GetScript() const {
|
||||
auto* scriptComponent = GetComponent<ScriptComponent>();
|
||||
auto* script = scriptComponent->GetScript();
|
||||
DluAssert(script != nullptr);
|
||||
return script;
|
||||
}
|
||||
|
||||
void Entity::Update(const float deltaTime) {
|
||||
uint32_t timerPosition;
|
||||
timerPosition = 0;
|
||||
while (timerPosition < m_Timers.size()) {
|
||||
m_Timers[timerPosition]->Update(deltaTime);
|
||||
if (m_Timers[timerPosition]->GetTime() <= 0) {
|
||||
const auto timerName = m_Timers[timerPosition]->GetName();
|
||||
|
||||
delete m_Timers[timerPosition];
|
||||
m_Timers.erase(m_Timers.begin() + timerPosition);
|
||||
|
||||
for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) {
|
||||
script->OnTimerDone(this, timerName);
|
||||
}
|
||||
auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](EntityTimer* timer) {
|
||||
timer->Update(deltaTime);
|
||||
if (timer->GetTime() <= 0) {
|
||||
GetScript()->OnTimerDone(this, timer->GetName());
|
||||
TriggerEvent(eTriggerEventType::TIMER_DONE, this);
|
||||
} else {
|
||||
timerPosition++;
|
||||
delete timer;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
m_Timers.erase(namedTimerItr, m_Timers.end());
|
||||
|
||||
for (int i = 0; i < m_CallbackTimers.size(); i++) {
|
||||
m_CallbackTimers[i]->Update(deltaTime);
|
||||
if (m_CallbackTimers[i]->GetTime() <= 0) {
|
||||
m_CallbackTimers[i]->GetCallback()();
|
||||
delete m_CallbackTimers[i];
|
||||
m_CallbackTimers.erase(m_CallbackTimers.begin() + i);
|
||||
auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](EntityCallbackTimer* timer) {
|
||||
timer->Update(deltaTime);
|
||||
if (timer->GetTime() <= 0) {
|
||||
timer->GetCallback()();
|
||||
delete timer;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
m_CallbackTimers.erase(callbackTimerItr, m_CallbackTimers.end());
|
||||
|
||||
// Add pending timers to the list of timers so they start next tick.
|
||||
if (m_PendingTimers.size() > 0) {
|
||||
@@ -1294,10 +1293,6 @@ void Entity::CancelTimer(const std::string& name) {
|
||||
}
|
||||
|
||||
void Entity::CancelAllTimers() {
|
||||
/*for (auto timer : m_Timers) {
|
||||
if (timer) delete timer;
|
||||
}*/
|
||||
|
||||
for (auto* timer : m_Timers) {
|
||||
delete timer;
|
||||
}
|
||||
|
@@ -223,6 +223,7 @@ public:
|
||||
|
||||
virtual NiPoint3 GetRespawnPosition() const { return NiPoint3::ZERO; }
|
||||
virtual NiQuaternion GetRespawnRotation() const { return NiQuaternion::IDENTITY; }
|
||||
CppScripts::Script* GetScript() const;
|
||||
|
||||
void Sleep();
|
||||
void Wake();
|
||||
|
@@ -36,6 +36,7 @@ set(DGAME_DCOMPONENTS_SOURCES "BaseCombatAIComponent.cpp"
|
||||
"RigidbodyPhantomPhysicsComponent.cpp"
|
||||
"MultiZoneEntranceComponent.cpp"
|
||||
"RocketLaunchpadControlComponent.cpp"
|
||||
"ScriptComponent.cpp"
|
||||
"ScriptedActivityComponent.cpp"
|
||||
"ShootingGalleryComponent.cpp"
|
||||
"SimplePhysicsComponent.cpp"
|
||||
|
72
dGame/dComponents/ScriptComponent.cpp
Normal file
72
dGame/dComponents/ScriptComponent.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Darkflame Universe
|
||||
* Copyright 2018
|
||||
*/
|
||||
|
||||
#include "Entity.h"
|
||||
#include "ScriptComponent.h"
|
||||
#include "CDClientManager.h"
|
||||
#include "CDScriptComponentTable.h"
|
||||
#include "CDZoneTableTable.h"
|
||||
#include "dZoneManager.h"
|
||||
|
||||
ScriptComponent::ScriptComponent(Entity* parent, const std::string& scriptName) : Component(parent) {
|
||||
SetScript(scriptName);
|
||||
}
|
||||
|
||||
void ScriptComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
|
||||
if (bIsInitialUpdate) {
|
||||
const auto& networkSettings = m_ParentEntity->GetNetworkSettings();
|
||||
auto hasNetworkSettings = !networkSettings.empty();
|
||||
outBitStream->Write(hasNetworkSettings);
|
||||
|
||||
if (hasNetworkSettings) {
|
||||
|
||||
// First write the most inner LDF data
|
||||
RakNet::BitStream ldfData;
|
||||
ldfData.Write<uint8_t>(0);
|
||||
ldfData.Write<uint32_t>(networkSettings.size());
|
||||
|
||||
for (auto* networkSetting : networkSettings) {
|
||||
networkSetting->WriteToPacket(&ldfData);
|
||||
}
|
||||
|
||||
// Finally write everything to the stream
|
||||
outBitStream->Write<uint32_t>(ldfData.GetNumberOfBytesUsed());
|
||||
outBitStream->Write(ldfData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CppScripts::Script* ScriptComponent::GetScript() {
|
||||
return m_Script;
|
||||
}
|
||||
|
||||
void ScriptComponent::SetScript(const std::string& scriptName) {
|
||||
m_Script = CppScripts::GetScript(m_ParentEntity, scriptName);
|
||||
}
|
||||
|
||||
const std::string ScriptComponent::GetScriptName(Entity* parentEntity, const uint32_t componentId) {
|
||||
if (!parentEntity || componentId == 0) return "";
|
||||
// LDF key script overrides script component Id
|
||||
const auto customScriptServer = parentEntity->GetVarAsString(u"custom_script_server");
|
||||
if (!customScriptServer.empty()) return customScriptServer;
|
||||
|
||||
auto* scriptCompTable = CDClientManager::Instance().GetTable<CDScriptComponentTable>();
|
||||
CDScriptComponent scriptCompData = scriptCompTable->GetByID(componentId);
|
||||
return scriptCompData.script_name;
|
||||
}
|
||||
|
||||
const std::string ScriptComponent::GetZoneScriptName(const uint32_t componentId) {
|
||||
auto* zoneTable = CDClientManager::Instance().GetTable<CDZoneTableTable>();
|
||||
const auto zoneID = dZoneManager::Instance()->GetZoneID();
|
||||
const auto* zoneData = zoneTable->Query(zoneID.GetMapID());
|
||||
|
||||
if (!zoneData) return "";
|
||||
|
||||
int zoneScriptID = zoneData->scriptID;
|
||||
if (zoneScriptID == -1) return "";
|
||||
auto* scriptCompTable = CDClientManager::Instance().GetTable<CDScriptComponentTable>();
|
||||
const auto& zoneScriptData = scriptCompTable->GetByID(zoneScriptID);
|
||||
return zoneScriptData.script_name;
|
||||
}
|
53
dGame/dComponents/ScriptComponent.h
Normal file
53
dGame/dComponents/ScriptComponent.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Darkflame Universe
|
||||
* Copyright 2018
|
||||
*/
|
||||
|
||||
#ifndef SCRIPTCOMPONENT_H
|
||||
#define SCRIPTCOMPONENT_H
|
||||
|
||||
#include "CppScripts.h"
|
||||
#include "Component.h"
|
||||
#include <string>
|
||||
#include "eReplicaComponentType.h"
|
||||
|
||||
class Entity;
|
||||
|
||||
/**
|
||||
* Handles the loading and execution of server side scripts on entities, scripts were originally written in Lua,
|
||||
* here they're written in C++
|
||||
*/
|
||||
class ScriptComponent : public Component {
|
||||
public:
|
||||
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT;
|
||||
|
||||
ScriptComponent(Entity* parent, const std::string& scriptName);
|
||||
|
||||
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags);
|
||||
|
||||
/**
|
||||
* Returns the script that's attached to this entity
|
||||
* @return the script that's attached to this entity
|
||||
*/
|
||||
CppScripts::Script* GetScript();
|
||||
|
||||
/**
|
||||
* Sets the script using a path by looking through dScripts for a script that matches
|
||||
* @param scriptName the name of the script to find
|
||||
*/
|
||||
void SetScript(const std::string& scriptName);
|
||||
|
||||
// Get the script attached to the provided component id.
|
||||
// LDF key custom_script_server overrides component id script.
|
||||
static const std::string GetScriptName(Entity* parentEntity, const uint32_t componentId = 0);
|
||||
|
||||
static const std::string GetZoneScriptName(const uint32_t componentId);
|
||||
private:
|
||||
|
||||
/**
|
||||
* The script attached to this entity
|
||||
*/
|
||||
CppScripts::Script* m_Script;
|
||||
};
|
||||
|
||||
#endif // SCRIPTCOMPONENT_H
|
Reference in New Issue
Block a user