2009-12-30 01:32:31 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
|
#include "SpawnEntity.h"
|
|
|
|
|
|
2009-12-30 11:29:12 +00:00
|
|
|
#include "rulesets/Entity.h"
|
2009-12-31 15:10:07 +00:00
|
|
|
#include "rulesets/AreaProperty.h"
|
|
|
|
|
|
|
|
|
|
#include "common/log.h"
|
2010-01-02 02:06:16 +00:00
|
|
|
#include "common/random.h"
|
|
|
|
|
#include "common/type_utils_impl.h"
|
2009-12-30 11:29:12 +00:00
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
#include <Atlas/Message/Element.h>
|
2009-12-30 11:44:35 +00:00
|
|
|
#include <Atlas/Objects/Anonymous.h>
|
|
|
|
|
#include <Atlas/Objects/Operation.h>
|
2009-12-30 11:29:12 +00:00
|
|
|
#include <Atlas/Objects/RootEntity.h>
|
|
|
|
|
#include <Atlas/Objects/SmartPtr.h>
|
2009-12-30 11:12:33 +00:00
|
|
|
|
2010-01-02 02:06:16 +00:00
|
|
|
#include <wfmath/polygon.h>
|
|
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
using Atlas::Message::ListType;
|
|
|
|
|
using Atlas::Message::MapType;
|
2009-12-30 11:44:35 +00:00
|
|
|
using Atlas::Objects::Entity::Anonymous;
|
2009-12-30 01:32:31 +00:00
|
|
|
using Atlas::Objects::Entity::RootEntity;
|
2009-12-30 11:44:35 +00:00
|
|
|
using Atlas::Objects::Operation::Create;
|
2009-12-30 01:32:31 +00:00
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
SpawnEntity::SpawnEntity(Entity * e, const MapType & data) : m_ent(e)
|
|
|
|
|
{
|
|
|
|
|
MapType::const_iterator I = data.find("character_types");
|
2010-01-03 02:16:35 +00:00
|
|
|
MapType::const_iterator Iend = data.end();
|
|
|
|
|
if (I != Iend && I->second.isList()) {
|
2009-12-30 11:12:33 +00:00
|
|
|
m_characterTypes = I->second.List();
|
|
|
|
|
}
|
2010-01-03 02:16:35 +00:00
|
|
|
I = data.find("contains");
|
|
|
|
|
if (I != Iend && I->second.isList()) {
|
|
|
|
|
m_inventory = I->second.List();
|
|
|
|
|
}
|
2009-12-30 11:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
2009-12-30 11:35:08 +00:00
|
|
|
static const int check_character_type(const std::string & type,
|
|
|
|
|
const Atlas::Message::ListType & types)
|
|
|
|
|
{
|
|
|
|
|
ListType::const_iterator I = types.begin();
|
2009-12-30 15:22:37 +00:00
|
|
|
ListType::const_iterator Iend = types.end();
|
2009-12-30 11:35:08 +00:00
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
if (*I == type) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
int SpawnEntity::spawnEntity(const std::string & type,
|
|
|
|
|
const RootEntity & dsc)
|
2009-12-30 01:32:31 +00:00
|
|
|
{
|
2009-12-30 11:29:12 +00:00
|
|
|
if (m_ent.get() == 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2009-12-30 11:35:08 +00:00
|
|
|
if (check_character_type(type, m_characterTypes) != 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2010-01-02 02:06:16 +00:00
|
|
|
dsc->setLoc(m_ent->m_location.m_loc->getId());
|
2009-12-31 15:10:07 +00:00
|
|
|
const AreaProperty * ap = m_ent->getPropertyClass<AreaProperty>("area");
|
|
|
|
|
if (ap != 0) {
|
2010-01-02 02:06:16 +00:00
|
|
|
const CornerList & line = ap->line();
|
|
|
|
|
WFMath::Polygon<2> spawn_area;
|
|
|
|
|
WFMath::CoordType lx = WFMATH_MAX, ly = WFMATH_MAX,
|
|
|
|
|
hx = WFMATH_MIN, hy = WFMATH_MIN;
|
|
|
|
|
spawn_area.resize(line.size());
|
|
|
|
|
CornerList::const_iterator I = line.begin();
|
|
|
|
|
CornerList::const_iterator Iend = line.end();
|
|
|
|
|
for (int i = 0; I != Iend; ++I, ++i) {
|
|
|
|
|
WFMath::CoordType x = I->x(), y = I->y();
|
|
|
|
|
spawn_area.addCorner(i, Corner(x, y));
|
|
|
|
|
if (x < lx) { lx = x; }
|
|
|
|
|
if (y < ly) { ly = y; }
|
2009-12-31 15:10:07 +00:00
|
|
|
}
|
2010-01-02 02:06:16 +00:00
|
|
|
Point3D new_pos = m_ent->m_location.pos();
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
|
WFMath::CoordType x = uniform(lx, hx);
|
|
|
|
|
WFMath::CoordType y = uniform(ly, hy);
|
|
|
|
|
if (Intersect(spawn_area, Corner(x, y), true)) {
|
2010-01-02 02:17:31 +00:00
|
|
|
new_pos += Vector3D(x, y, 0);
|
2010-01-02 02:06:16 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2009-12-31 15:10:07 +00:00
|
|
|
}
|
2010-01-02 02:17:31 +00:00
|
|
|
::addToEntity(new_pos, dsc->modifyPos());
|
2010-01-02 02:06:16 +00:00
|
|
|
} else if (m_ent->m_location.bBox().isValid()) {
|
2010-01-02 02:17:31 +00:00
|
|
|
const BBox & b = m_ent->m_location.bBox();
|
|
|
|
|
::addToEntity(Point3D(uniform(b.lowCorner().x(), b.highCorner().x()),
|
|
|
|
|
uniform(b.lowCorner().y(), b.highCorner().y()),
|
|
|
|
|
0), dsc->modifyPos());
|
2010-01-02 02:06:16 +00:00
|
|
|
// Locate in bbox
|
|
|
|
|
} else {
|
|
|
|
|
::addToEntity(m_ent->m_location.pos(), dsc->modifyPos());
|
2009-12-31 15:10:07 +00:00
|
|
|
}
|
2009-12-30 11:35:08 +00:00
|
|
|
// FIXME this is exactly the same location as the spawn entity
|
|
|
|
|
return 0;
|
2009-12-30 01:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
int SpawnEntity::populateEntity(Entity * ent,
|
|
|
|
|
const RootEntity & dsc,
|
|
|
|
|
OpVector & res)
|
2009-12-30 01:32:31 +00:00
|
|
|
{
|
2009-12-30 11:44:35 +00:00
|
|
|
// Hack in default objects
|
|
|
|
|
// This needs to be done in a generic way
|
|
|
|
|
Anonymous create_arg;
|
|
|
|
|
::addToEntity(Point3D(0,0,0), create_arg->modifyPos());
|
|
|
|
|
create_arg->setLoc(ent->getId());
|
2010-01-03 02:16:35 +00:00
|
|
|
|
|
|
|
|
ListType::const_iterator I = m_inventory.begin();
|
|
|
|
|
ListType::const_iterator Iend = m_inventory.end();
|
|
|
|
|
for (; I != Iend; ++I) {
|
|
|
|
|
if (!I->isString()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2009-12-30 11:44:35 +00:00
|
|
|
Create c;
|
|
|
|
|
c->setTo(ent->getId());
|
2010-01-03 02:16:35 +00:00
|
|
|
create_arg = create_arg.copy();
|
|
|
|
|
create_arg->setParents(std::list<std::string>(1, I->String()));
|
2009-12-30 11:44:35 +00:00
|
|
|
c->setArgs1(create_arg);
|
|
|
|
|
res.push_back(c);
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-30 11:12:33 +00:00
|
|
|
return 0;
|
2009-12-30 01:32:31 +00:00
|
|
|
}
|
2009-12-30 11:29:12 +00:00
|
|
|
|
|
|
|
|
int SpawnEntity::addToMessage(MapType & msg) const
|
|
|
|
|
{
|
|
|
|
|
msg.insert(std::make_pair("character_types", m_characterTypes));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|