2006-12-27 00:32:32 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2006 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
|
|
|
|
|
|
|
|
|
|
|
2006-12-29 00:02:31 +00:00
|
|
|
#include "OutfitProperty.h"
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2013-01-18 19:00:57 +00:00
|
|
|
#include "rulesets/LocatedEntity.h"
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2013-01-17 10:17:07 +00:00
|
|
|
#include "common/BaseWorld.h"
|
2006-12-27 00:32:32 +00:00
|
|
|
#include "common/debug.h"
|
2013-01-17 10:17:07 +00:00
|
|
|
#include "common/log.h"
|
2007-01-11 20:36:58 +00:00
|
|
|
#include "common/Update.h"
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2007-01-11 20:36:58 +00:00
|
|
|
#include <Atlas/Objects/Anonymous.h>
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2007-01-05 00:47:21 +00:00
|
|
|
#include <sigc++/adaptors/bind.h>
|
2013-06-15 22:53:14 +02:00
|
|
|
#include <sigc++/adaptors/hide.h>
|
2007-01-05 00:47:21 +00:00
|
|
|
#include <sigc++/functors/mem_fun.h>
|
|
|
|
|
|
2013-01-18 19:00:57 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2007-01-05 00:47:21 +00:00
|
|
|
using Atlas::Message::Element;
|
2006-12-27 00:32:32 +00:00
|
|
|
using Atlas::Message::MapType;
|
2007-01-11 20:36:58 +00:00
|
|
|
using Atlas::Objects::Operation::Update;
|
|
|
|
|
using Atlas::Objects::Entity::Anonymous;
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2007-01-13 13:54:24 +00:00
|
|
|
static const bool debug_flag = false;
|
2006-12-27 00:32:32 +00:00
|
|
|
|
2006-12-29 00:41:18 +00:00
|
|
|
/// \brief OutfitProperty constructor
|
2009-05-14 00:20:09 +01:00
|
|
|
OutfitProperty::OutfitProperty()
|
2006-12-27 00:32:32 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-05 02:58:52 +00:00
|
|
|
OutfitProperty::~OutfitProperty()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-29 17:36:26 +01:00
|
|
|
int OutfitProperty::get(Atlas::Message::Element & val) const
|
2006-12-27 00:32:32 +00:00
|
|
|
{
|
|
|
|
|
val = MapType();
|
|
|
|
|
MapType & val_map = val.Map();
|
|
|
|
|
|
|
|
|
|
EntityRefMap::const_iterator I = m_data.begin();
|
|
|
|
|
EntityRefMap::const_iterator Iend = m_data.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
const EntityRef & item = I->second;
|
2007-01-03 02:06:50 +00:00
|
|
|
if (item.get() == 0) {
|
|
|
|
|
val_map[I->first] = "";
|
|
|
|
|
} else {
|
2013-11-17 22:17:40 +01:00
|
|
|
Atlas::Message::MapType refMap;
|
|
|
|
|
refMap["$eid"] = item->getId();
|
|
|
|
|
val_map[I->first] = refMap;
|
2007-01-03 02:06:50 +00:00
|
|
|
}
|
2006-12-27 00:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-29 17:36:26 +01:00
|
|
|
return 0;
|
2006-12-27 00:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
2006-12-29 00:02:31 +00:00
|
|
|
void OutfitProperty::set(const Atlas::Message::Element & val)
|
2006-12-27 00:32:32 +00:00
|
|
|
{
|
|
|
|
|
// INT id?
|
|
|
|
|
|
|
|
|
|
if (!val.isMap()) {
|
2006-12-29 00:02:31 +00:00
|
|
|
debug(std::cout << "Value of outfit is not a map" << std::endl << std::flush;);
|
2006-12-27 00:32:32 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MapType & val_map = val.Map();
|
|
|
|
|
|
|
|
|
|
MapType::const_iterator I = val_map.begin();
|
|
|
|
|
MapType::const_iterator Iend = val_map.end();
|
|
|
|
|
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
const std::string & key = I->first;
|
|
|
|
|
const Atlas::Message::Element & item = I->second;
|
|
|
|
|
|
|
|
|
|
if (item.isString()) {
|
|
|
|
|
const std::string & id = item.String();
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity * e = BaseWorld::instance().getEntity(id);
|
2006-12-27 00:32:32 +00:00
|
|
|
if (e != 0) {
|
|
|
|
|
m_data[key] = EntityRef(e);
|
|
|
|
|
}
|
|
|
|
|
} else if (item.isPtr()) {
|
2013-01-18 19:00:57 +00:00
|
|
|
LocatedEntity * e = static_cast<LocatedEntity*>(item.Ptr());
|
2006-12-27 00:32:32 +00:00
|
|
|
assert(e != 0);
|
|
|
|
|
m_data[key] = EntityRef(e);
|
2013-11-17 22:17:40 +01:00
|
|
|
} else if (item.isMap()) {
|
|
|
|
|
auto J = item.asMap().find("$eid");
|
|
|
|
|
if (J != item.asMap().end()) {
|
|
|
|
|
if (J->second.isString()) {
|
|
|
|
|
const std::string & id = J->second.String();
|
|
|
|
|
LocatedEntity * e = BaseWorld::instance().getEntity(id);
|
|
|
|
|
if (e != 0) {
|
|
|
|
|
m_data[key] = EntityRef(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-12-27 00:32:32 +00:00
|
|
|
} else {
|
|
|
|
|
debug(std::cout << "Key " << key << " is of type "
|
2006-12-29 00:02:31 +00:00
|
|
|
<< item.getType() << " when setting outfit"
|
2006-12-27 00:32:32 +00:00
|
|
|
<< std::endl << std::flush;);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-29 00:02:31 +00:00
|
|
|
void OutfitProperty::add(const std::string & key,
|
2006-12-27 00:32:32 +00:00
|
|
|
Atlas::Message::MapType & map) const
|
|
|
|
|
{
|
|
|
|
|
if (m_data.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MapType & val_map = (map[key] = MapType()).Map();
|
|
|
|
|
|
2007-01-03 02:06:50 +00:00
|
|
|
// FIXME This code is essentially shared with the add function below
|
|
|
|
|
// Move it into a protected function
|
2006-12-27 00:32:32 +00:00
|
|
|
EntityRefMap::const_iterator I = m_data.begin();
|
|
|
|
|
EntityRefMap::const_iterator Iend = m_data.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
const EntityRef & item = I->second;
|
2007-01-03 02:06:50 +00:00
|
|
|
if (item.get() != 0) {
|
2013-11-17 22:17:40 +01:00
|
|
|
Atlas::Message::MapType refMap;
|
|
|
|
|
refMap["$eid"] = item->getId();
|
|
|
|
|
val_map[I->first] = refMap;
|
2007-01-03 02:06:50 +00:00
|
|
|
} else {
|
|
|
|
|
val_map[I->first] = "";
|
|
|
|
|
}
|
2006-12-27 00:32:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-12-29 00:02:31 +00:00
|
|
|
void OutfitProperty::add(const std::string & key,
|
2006-12-27 00:32:32 +00:00
|
|
|
const Atlas::Objects::Entity::RootEntity & ent) const
|
|
|
|
|
{
|
|
|
|
|
if (m_data.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MapType val_map;
|
|
|
|
|
|
|
|
|
|
EntityRefMap::const_iterator I = m_data.begin();
|
|
|
|
|
EntityRefMap::const_iterator Iend = m_data.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
const EntityRef & item = I->second;
|
2007-01-03 02:06:50 +00:00
|
|
|
if (item.get() != 0) {
|
2013-11-17 22:17:40 +01:00
|
|
|
Atlas::Message::MapType refMap;
|
|
|
|
|
refMap["$eid"] = item->getId();
|
|
|
|
|
val_map[I->first] = refMap;
|
2007-01-03 02:06:50 +00:00
|
|
|
} else {
|
|
|
|
|
val_map[I->first] = "";
|
|
|
|
|
}
|
2006-12-27 00:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ent->setAttr(key, val_map);
|
|
|
|
|
}
|
2007-01-03 02:06:50 +00:00
|
|
|
|
2013-01-07 23:03:25 +00:00
|
|
|
OutfitProperty * OutfitProperty::copy() const
|
|
|
|
|
{
|
|
|
|
|
return new OutfitProperty(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-25 14:16:16 -04:00
|
|
|
LocatedEntity* OutfitProperty::getEntity(const std::string& key) const
|
|
|
|
|
{
|
|
|
|
|
auto iter = m_data.find(key);
|
|
|
|
|
if (iter != m_data.end()) {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-03 02:06:50 +00:00
|
|
|
void OutfitProperty::cleanUp()
|
|
|
|
|
{
|
|
|
|
|
std::set<std::string> empty_locations;
|
|
|
|
|
|
|
|
|
|
EntityRefMap::const_iterator I = m_data.begin();
|
|
|
|
|
EntityRefMap::const_iterator Iend = m_data.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
if (I->second == 0) {
|
|
|
|
|
empty_locations.insert(I->first);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::set<std::string>::const_iterator J = empty_locations.begin();
|
|
|
|
|
std::set<std::string>::const_iterator Jend = empty_locations.end();
|
|
|
|
|
for (; J != Jend; ++J) {
|
|
|
|
|
m_data.erase(*J);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-17 10:17:07 +00:00
|
|
|
void OutfitProperty::wear(LocatedEntity * wearer,
|
2007-01-11 20:36:58 +00:00
|
|
|
const std::string & location,
|
2013-01-17 10:17:07 +00:00
|
|
|
LocatedEntity * garment)
|
2007-01-03 02:06:50 +00:00
|
|
|
{
|
|
|
|
|
m_data[location] = EntityRef(garment);
|
2007-01-05 00:47:21 +00:00
|
|
|
|
2013-06-15 23:43:28 +02:00
|
|
|
sigc::connection containered_connection = garment->containered.connect(sigc::bind(sigc::hide<0>(sigc::mem_fun(this, &OutfitProperty::itemRemoved)), garment, wearer));
|
|
|
|
|
sigc::connection destroyed_connection = garment->destroyed.connect(sigc::bind(sigc::mem_fun(this, &OutfitProperty::itemRemoved), garment, wearer));
|
|
|
|
|
|
|
|
|
|
ConnectionsWrapper wrapper{containered_connection, destroyed_connection};
|
|
|
|
|
m_connections[garment] = wrapper;
|
2007-01-05 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-17 10:17:07 +00:00
|
|
|
void OutfitProperty::itemRemoved(LocatedEntity * garment,
|
|
|
|
|
LocatedEntity * wearer)
|
2007-01-05 00:47:21 +00:00
|
|
|
{
|
|
|
|
|
Element worn_attr;
|
|
|
|
|
std::string key;
|
2011-09-29 17:36:26 +01:00
|
|
|
if (garment->getAttr("worn", worn_attr) == 0) {
|
2007-01-05 00:47:21 +00:00
|
|
|
if (worn_attr.isString()) {
|
|
|
|
|
key = worn_attr.String();
|
|
|
|
|
assert(!key.empty());
|
|
|
|
|
} else {
|
2007-01-13 13:54:24 +00:00
|
|
|
log(ERROR, "WORN property of garment is not a string");
|
2007-01-05 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2014-06-25 14:16:16 -04:00
|
|
|
|
2007-01-05 00:47:21 +00:00
|
|
|
EntityRefMap::const_iterator I = m_data.begin();
|
|
|
|
|
EntityRefMap::const_iterator Iend = m_data.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
if (I->second == garment) {
|
|
|
|
|
key = I->first;
|
|
|
|
|
assert(!key.empty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (key.empty()) {
|
2007-01-13 13:54:24 +00:00
|
|
|
log(ERROR, "Outfit trying to remove garment with empty key");
|
|
|
|
|
return;
|
2007-01-05 00:47:21 +00:00
|
|
|
}
|
|
|
|
|
m_data[key] = EntityRef(0);
|
2013-06-15 23:43:28 +02:00
|
|
|
auto I = m_connections.find(garment);
|
|
|
|
|
if (I != m_connections.end()) {
|
|
|
|
|
I->second.containered.disconnect();
|
|
|
|
|
I->second.destroyed.disconnect();
|
|
|
|
|
m_connections.erase(I);
|
|
|
|
|
}
|
2007-01-11 20:36:58 +00:00
|
|
|
|
|
|
|
|
Anonymous update_arg;
|
|
|
|
|
update_arg->setId(wearer->getId());
|
|
|
|
|
update_arg->setAttr("outfit", MapType());
|
|
|
|
|
|
|
|
|
|
Update update;
|
|
|
|
|
update->setTo(wearer->getId());
|
|
|
|
|
update->setArgs1(update_arg);
|
|
|
|
|
|
|
|
|
|
wearer->sendWorld(update);
|
2007-01-03 02:06:50 +00:00
|
|
|
}
|
2014-09-14 00:21:20 +02:00
|
|
|
|
|
|
|
|
const EntityRefMap& OutfitProperty::data() const
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|