cyphesis/rules/simulation/TasksProperty.cpp

223 lines
5.6 KiB
C++
Raw Permalink Normal View History

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 Alistair Riddoch
//
// 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
2018-10-30 20:12:45 +01:00
#include <common/operations/Tick.h>
#include "TasksProperty.h"
#include "rules/LocatedEntity.h"
#include "Task.h"
#include "common/debug.h"
#include "common/TypeNode.h"
2018-10-30 20:12:45 +01:00
#include "common/operations/Update.h"
using Atlas::Message::Element;
using Atlas::Message::ListType;
using Atlas::Message::MapType;
using Atlas::Objects::Root;
using Atlas::Objects::Operation::Update;
static const bool debug_flag = false;
static const std::string SERIALNO = "serialno";
TasksProperty::TasksProperty()
: PropertyBase(per_ephem)
{
}
int TasksProperty::get(Atlas::Message::Element& val) const
{
2018-08-02 21:45:20 +02:00
MapType tasks;
for (auto entry : m_tasks) {
auto& task = entry.second;
MapType taskMap;
taskMap["name"] = task->name();
auto progress = task->progress();
if (progress > 0) {
taskMap["progress"] = progress;
}
if (task->m_duration) {
taskMap["rate"] = 1.0f / *task->m_duration;
}
tasks.emplace(entry.first, std::move(taskMap));
}
2018-08-02 21:45:20 +02:00
val = std::move(tasks);
2011-09-29 17:36:26 +01:00
return 0;
}
void TasksProperty::set(const Atlas::Message::Element& val)
{
2018-08-02 21:45:20 +02:00
log(ERROR, "Cannot set 'tasks' property.");
}
TasksProperty* TasksProperty::copy() const
{
return new TasksProperty(*this);
}
int TasksProperty::updateTask(LocatedEntity* owner, OpVector& res)
{
m_flags.addFlags(flag_unsent);
Update update;
update->setTo(owner->getId());
2011-11-17 16:47:16 +00:00
res.push_back(update);
return 0;
}
2018-08-02 21:45:20 +02:00
int TasksProperty::startTask(std::string id, Ref<Task> task,
LocatedEntity* owner,
OpVector& res)
{
bool update_required = false;
2018-08-02 21:45:20 +02:00
auto tasksI = m_tasks.find(id);
if (tasksI != m_tasks.end()) {
update_required = true;
2018-08-02 21:45:20 +02:00
m_tasks.erase(id);
}
2018-08-02 21:45:20 +02:00
task->initTask(id, res);
2018-07-23 11:03:18 +02:00
if (!task->obsolete()) {
assert(!res.empty());
2018-08-02 21:45:20 +02:00
m_tasks.emplace(id, task);
update_required = true;
2018-08-02 21:45:20 +02:00
} else {
task = nullptr;
}
if (update_required) {
updateTask(owner, res);
}
2018-08-02 21:45:20 +02:00
return task ? 0 : -1;
}
2011-11-18 16:26:24 +00:00
2018-08-02 21:45:20 +02:00
int TasksProperty::clearTask(const std::string& id, LocatedEntity* owner, OpVector& res)
2011-11-18 16:26:24 +00:00
{
2018-08-02 21:45:20 +02:00
if (m_tasks.empty()) {
2011-11-18 16:26:24 +00:00
// This function should never be called when there is no task,
// except during Entity destruction
assert(owner->hasFlags(entity_destroyed));
2011-11-18 16:26:24 +00:00
return -1;
}
2018-08-02 21:45:20 +02:00
m_tasks.erase(id);
2011-11-18 16:26:24 +00:00
return updateTask(owner, res);
}
2018-08-02 21:45:20 +02:00
void TasksProperty::stopTask(const std::string& id, LocatedEntity* owner, OpVector& res)
2011-11-21 11:42:47 +00:00
{
// This is just clearTask without an assert
2018-08-02 21:45:20 +02:00
if (m_tasks.find(id) == m_tasks.end()) {
2011-11-21 11:42:47 +00:00
log(ERROR, "Tasks property stop when no task");
return;
}
2018-08-02 21:45:20 +02:00
m_tasks.erase(id);
2011-11-21 11:42:47 +00:00
updateTask(owner, res);
}
void TasksProperty::TickOperation(LocatedEntity* owner,
const Operation& op,
OpVector& res)
{
const std::vector<Root>& args = op->getArgs();
if (args.empty()) {
return;
}
const Root& arg = args.front();
2018-08-02 21:45:20 +02:00
if (arg->isDefaultId()) {
return;
}
auto taskI = m_tasks.find(arg->getId());
if (taskI == m_tasks.end()) {
return;
}
auto& id = taskI->first;
auto& task = taskI->second;
Element serialno;
if (arg->copyAttr(SERIALNO, serialno) == 0 && (serialno.isInt())) {
2018-08-02 21:45:20 +02:00
if (serialno.asInt() != task->serialno()) {
2018-07-31 13:37:19 +02:00
debug_print("Old tick");
return;
}
} else {
log(ERROR, "Character::TickOperation: No serialno in tick arg");
return;
}
2018-08-02 21:45:20 +02:00
task->tick(id, op, res);
if (task->obsolete()) {
clearTask(id, owner, res);
2018-07-31 13:37:19 +02:00
} else {
updateTask(owner, res);
}
2018-08-02 21:45:20 +02:00
if (task != nullptr && res.empty()) {
log(WARNING, String::compose("Character::%1: Task %2 has "
"stalled", __func__,
2018-08-02 21:45:20 +02:00
task->name()));
}
}
void TasksProperty::UseOperation(LocatedEntity* owner,
const Operation& op,
OpVector& res)
{
}
HandlerResult TasksProperty::operation(LocatedEntity* owner,
const Operation& op,
OpVector& res)
{
2018-07-31 13:37:19 +02:00
auto& args = op->getArgs();
if (!args.empty()) {
auto& arg = args.front();
if (arg->getName() == "task") {
TickOperation(owner, op, res);
return OPERATION_BLOCKED;
}
}
2018-07-31 13:37:19 +02:00
return OPERATION_IGNORED;
}
void TasksProperty::install(LocatedEntity* owner, const std::string& name)
2018-07-31 13:37:19 +02:00
{
owner->installDelegate(Atlas::Objects::Operation::TICK_NO, name);
}
void TasksProperty::remove(LocatedEntity* owner, const std::string& name)
2018-07-31 13:37:19 +02:00
{
owner->removeDelegate(Atlas::Objects::Operation::TICK_NO, name);
}