2016-01-19 15:42:05 +01:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2016 Erik Ogenvik
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2016-01-18 15:37:05 +01:00
|
|
|
|
|
|
|
|
#include "ModeProperty.h"
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/LocatedEntity.h"
|
2016-01-18 15:37:05 +01:00
|
|
|
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/QuaternionProperty.h"
|
2016-09-02 23:38:02 +02:00
|
|
|
|
2018-10-31 21:38:35 +01:00
|
|
|
#include "rules/simulation/BaseWorld.h"
|
2016-09-02 23:38:02 +02:00
|
|
|
|
|
|
|
|
#include <Atlas/Objects/Entity.h>
|
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
|
|
|
|
#include <wfmath/atlasconv.h>
|
2016-01-19 15:42:05 +01:00
|
|
|
|
2016-01-18 15:37:05 +01:00
|
|
|
|
2016-09-02 23:38:02 +02:00
|
|
|
ModeProperty::ModeProperty()
|
2016-09-19 16:40:39 +02:00
|
|
|
: m_mode(Mode::Free)
|
2016-09-02 23:38:02 +02:00
|
|
|
{
|
2016-01-18 15:37:05 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-02 23:38:02 +02:00
|
|
|
void ModeProperty::apply(LocatedEntity *entity)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (m_data == "planted") {
|
|
|
|
|
//See if there's a rotation we should apply
|
2018-10-30 19:31:27 +01:00
|
|
|
const auto* plantedRotation = entity->getPropertyClass<QuaternionProperty>("planted_rotation");
|
2016-09-02 23:38:02 +02:00
|
|
|
if (plantedRotation && plantedRotation->data().isValid()) {
|
|
|
|
|
//Check that the rotation is applied already, otherwise apply it.
|
2018-10-30 19:31:27 +01:00
|
|
|
auto* activeRotationProp = entity->requirePropertyClass<QuaternionProperty>("active_rotation");
|
2016-09-02 23:38:02 +02:00
|
|
|
if (activeRotationProp->data() != plantedRotation->data()) {
|
|
|
|
|
WFMath::Quaternion currentOrientation = entity->m_location.orientation();
|
|
|
|
|
|
|
|
|
|
if (activeRotationProp->data().isValid() && activeRotationProp->data() != WFMath::Quaternion::Identity()) {
|
|
|
|
|
WFMath::Quaternion rotation = activeRotationProp->data().inverse();
|
|
|
|
|
//normalize to avoid drift
|
|
|
|
|
rotation.normalize();
|
|
|
|
|
currentOrientation = rotation * currentOrientation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WFMath::Quaternion rotation = plantedRotation->data();
|
|
|
|
|
//normalize to avoid drift
|
|
|
|
|
rotation.normalize();
|
|
|
|
|
currentOrientation = rotation * currentOrientation;
|
|
|
|
|
|
|
|
|
|
activeRotationProp->data() = plantedRotation->data();
|
|
|
|
|
activeRotationProp->apply(entity);
|
2018-04-25 12:21:44 +02:00
|
|
|
activeRotationProp->removeFlags(per_clean);
|
|
|
|
|
activeRotationProp->addFlags(flag_unsent);
|
2016-09-02 23:38:02 +02:00
|
|
|
|
|
|
|
|
Atlas::Objects::Entity::Anonymous move_arg;
|
|
|
|
|
move_arg->setId(entity->getId());
|
|
|
|
|
move_arg->setAttr("orientation", currentOrientation.toAtlas());
|
|
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Move moveOp;
|
|
|
|
|
moveOp->setTo(entity->getId());
|
|
|
|
|
moveOp->setSeconds(BaseWorld::instance().getTime());
|
|
|
|
|
moveOp->setArgs1(move_arg);
|
|
|
|
|
entity->sendWorld(moveOp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 15:42:05 +01:00
|
|
|
} else {
|
2018-07-30 13:35:51 +02:00
|
|
|
if (entity->hasAttr("active_rotation")) {
|
2018-10-30 19:31:27 +01:00
|
|
|
auto* activeRotationProp = entity->modPropertyClass<QuaternionProperty>("active_rotation");
|
2018-02-20 22:24:32 +01:00
|
|
|
if (activeRotationProp->data().isValid()) {
|
|
|
|
|
WFMath::Quaternion currentOrientation = entity->m_location.orientation();
|
|
|
|
|
|
|
|
|
|
WFMath::Quaternion rotation = activeRotationProp->data().inverse();
|
|
|
|
|
//normalize to avoid drift
|
|
|
|
|
rotation.normalize();
|
|
|
|
|
currentOrientation = rotation * currentOrientation;
|
|
|
|
|
|
|
|
|
|
activeRotationProp->data() = WFMath::Quaternion::Identity();
|
|
|
|
|
activeRotationProp->apply(entity);
|
2018-04-25 12:21:44 +02:00
|
|
|
activeRotationProp->removeFlags(per_clean);
|
|
|
|
|
activeRotationProp->addFlags(flag_unsent);
|
2018-02-20 22:24:32 +01:00
|
|
|
|
|
|
|
|
Atlas::Objects::Entity::Anonymous move_arg;
|
|
|
|
|
move_arg->setId(entity->getId());
|
|
|
|
|
move_arg->setAttr("orientation", currentOrientation.toAtlas());
|
|
|
|
|
|
|
|
|
|
Atlas::Objects::Operation::Move moveOp;
|
|
|
|
|
moveOp->setTo(entity->getId());
|
|
|
|
|
moveOp->setSeconds(BaseWorld::instance().getTime());
|
|
|
|
|
moveOp->setArgs1(move_arg);
|
|
|
|
|
entity->sendWorld(moveOp);
|
2016-09-02 23:38:02 +02:00
|
|
|
|
2018-02-20 22:24:32 +01:00
|
|
|
}
|
2016-01-18 15:37:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 16:13:03 +01:00
|
|
|
ModeProperty * ModeProperty::copy() const
|
|
|
|
|
{
|
|
|
|
|
return new ModeProperty(*this);
|
|
|
|
|
}
|
2016-09-19 16:40:39 +02:00
|
|
|
|
|
|
|
|
void ModeProperty::set(const Atlas::Message::Element & val)
|
|
|
|
|
{
|
|
|
|
|
Property<std::string>::set(val);
|
2018-02-06 13:08:23 +01:00
|
|
|
if (m_data == "free" || m_data.empty()) {
|
2016-09-19 16:40:39 +02:00
|
|
|
m_mode = Mode::Free;
|
|
|
|
|
} else if (m_data == "planted") {
|
|
|
|
|
m_mode = Mode::Planted;
|
|
|
|
|
} else if (m_data == "fixed") {
|
|
|
|
|
m_mode = Mode::Fixed;
|
2018-02-24 00:20:02 +01:00
|
|
|
} else if (m_data == "submerged") {
|
|
|
|
|
m_mode = Mode::Submerged;
|
2016-09-19 16:40:39 +02:00
|
|
|
} else {
|
|
|
|
|
m_mode = Mode::Unknown;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|