DarkflameServer/dGame/dPropertyBehaviors/PropertyBehavior.cpp

162 lines
5.1 KiB
C++
Raw Normal View History

feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
#include "PropertyBehavior.h"
#include "Amf3.h"
#include "BehaviorStates.h"
#include "ControlBehaviorMsgs.h"
2024-05-18 09:05:55 +00:00
#include "tinyxml2.h"
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
PropertyBehavior::PropertyBehavior() {
m_LastEditedState = BehaviorState::HOME_STATE;
}
2024-11-03 04:31:54 +00:00
void PropertyBehavior::Update(float deltaTime) {
for (auto& [stateId, state] : m_States) {
state.Update(deltaTime);
}
}
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
template<>
void PropertyBehavior::HandleMsg(AddStripMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(AddActionMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(RearrangeStripMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(UpdateActionMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(UpdateStripUiMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(RemoveStripMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(RemoveActionsMessage& msg) {
m_States[msg.GetActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(SplitStripMessage& msg) {
m_States[msg.GetSourceActionContext().GetStateId()].HandleMsg(msg);
m_States[msg.GetDestinationActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetDestinationActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(MigrateActionsMessage& msg) {
m_States[msg.GetSourceActionContext().GetStateId()].HandleMsg(msg);
m_States[msg.GetDestinationActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetDestinationActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(MergeStripsMessage& msg) {
m_States[msg.GetSourceActionContext().GetStateId()].HandleMsg(msg);
m_States[msg.GetDestinationActionContext().GetStateId()].HandleMsg(msg);
m_LastEditedState = msg.GetDestinationActionContext().GetStateId();
};
template<>
void PropertyBehavior::HandleMsg(RenameMessage& msg) {
m_Name = msg.GetName();
};
template<>
void PropertyBehavior::HandleMsg(AddMessage& msg) {
// TODO Parse the corresponding behavior xml file.
m_BehaviorId = msg.GetBehaviorId();
isLoot = m_BehaviorId != 7965;
};
void PropertyBehavior::SendBehaviorListToClient(AMFArrayValue& args) const {
args.Insert("id", std::to_string(m_BehaviorId));
args.Insert("name", m_Name);
args.Insert("isLocked", isLocked);
args.Insert("isLoot", isLoot);
}
void PropertyBehavior::VerifyLastEditedState() {
if (!m_States[m_LastEditedState].IsEmpty()) return;
for (const auto& [stateId, state] : m_States) {
if (state.IsEmpty()) continue;
LOG_DEBUG("Updating last edited state to %i because %i is empty.", stateId, m_LastEditedState);
m_LastEditedState = stateId;
return;
}
LOG_DEBUG("No states found, sending default state");
m_LastEditedState = BehaviorState::HOME_STATE;
}
void PropertyBehavior::SendBehaviorBlocksToClient(AMFArrayValue& args) const {
auto* const stateArray = args.InsertArray("states");
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
for (const auto& [stateId, state] : m_States) {
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
if (state.IsEmpty()) continue;
LOG_DEBUG("Serializing state %i", stateId);
auto* const stateArgs = stateArray->PushArray();
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
stateArgs->Insert("id", static_cast<double>(stateId));
state.SendBehaviorBlocksToClient(*stateArgs);
}
auto* const executionState = args.InsertArray("executionState");
feat: partially functioning property behavior ui (no saving or loading yet) (#1384) * Add addstrip handling add SendBehaviorBlocksToClient serialization add id generation and auto updating add behaviorlisttoclient serialization * fix crash happened if you added state 0 and 6 and nothing in between * Section off code Use proper encapsulation to hide code away and only let specific objects do certain jobs. * Organize serialization Section off into operational chunks Write data at the level most appropriate * Remove and simplify BlockDefinitions Remove pointer usage for BlockDefinitions and move to optional. * ControlBehaviors: Add addaction handling * re-organization remove const from return value change to int from uint use generic methods to reduce code clutter * add strip ui position handling * add split strip functionality * fix issues fix an issue where if you were on an empty state, the server would allow you to remain on that state fix an issue where the ui would not open on the previously opened state fix an issue where deleting strips in order caused the wrong strips to be deleted * update how you remove behaviors from models * Add remove actions and rename * migrate actions * update action and rearrange strip * merge strips * add and move to inventory * Remove dead code * simplify code * nits and move finish MoveToInventory constify serialize further include path fixes use const, comments fix amf message Update ModelComponent.cpp replace operator subscript with at * Update ModelComponent.cpp * Update MigrateActionsMessage.h * const * Move to separate translation units * include amf3 its precompiled, but just in case
2024-01-03 13:34:38 +00:00
executionState->Insert("stateID", static_cast<double>(m_LastEditedState));
executionState->InsertArray("strips");
// TODO Serialize the execution state of the behavior
}
2024-05-18 09:05:55 +00:00
void PropertyBehavior::Serialize(tinyxml2::XMLElement& behavior) const {
behavior.SetAttribute("id", m_BehaviorId);
behavior.SetAttribute("name", m_Name.c_str());
behavior.SetAttribute("isLocked", isLocked);
behavior.SetAttribute("isLoot", isLoot);
for (const auto& [stateId, state] : m_States) {
if (state.IsEmpty()) continue;
auto* const stateElement = behavior.InsertNewChildElement("State");
stateElement->SetAttribute("id", static_cast<uint32_t>(stateId));
state.Serialize(*stateElement);
}
}
2024-05-18 10:36:29 +00:00
void PropertyBehavior::Deserialize(const tinyxml2::XMLElement& behavior) {
m_Name = behavior.Attribute("name");
behavior.QueryBoolAttribute("isLocked", &isLocked);
behavior.QueryBoolAttribute("isLoot", &isLoot);
for (const auto* stateElement = behavior.FirstChildElement("State"); stateElement; stateElement = stateElement->NextSiblingElement("State")) {
int32_t stateId = -1;
stateElement->QueryIntAttribute("id", &stateId);
if (stateId < 0 || stateId > 5) continue;
m_States[static_cast<BehaviorState>(stateId)].Deserialize(*stateElement);
}
}