1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000,2001 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: BaseMind.cpp,v 1.113 2008-01-26 17:43:21 alriddoch Exp $
#include "BaseMind.h"
#include "Script.h"
#include "common/log.h"
#include "common/debug.h"
#include "common/op_switch.h"
#include "common/inheritance.h"
#include "common/Attack.h"
#include "common/Setup.h"
#include "common/Tick.h"
#include <Atlas/Objects/SmartPtr.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>
using Atlas::Message::Element;
using Atlas::Objects::Root;
using Atlas::Objects::Operation::Look;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;
using Atlas::Objects::smart_dynamic_cast;
static const bool debug_flag = false;
/// \brief BaseMind constructor
///
/// @param id String identifier
/// @param intId Integer identifier
/// @param body_name The name attribute of the body this mind controls
BaseMind::BaseMind(const std::string & id, long intId) :
Identified(id, intId),
MemEntity(id, intId), m_map(m_script), m_isAwake(true)
{
setVisible(true);
m_map.addEntity(this);
}
BaseMind::~BaseMind()
{
m_map.m_entities.erase(getIntId());
// FIXME Remove this once MemMap uses parent refcounting
m_location.m_loc = 0;
// debug(std::cout << getId() << ":" << getType() << " flushing mind with "
// << m_map.getEntities().size() << " entities in it"
// << std::endl << std::flush;);
m_map.flush();
}
/// \brief Process the Sight of a Create operation.
///
/// @param op The Create operation to be processed.
/// @param res The result of the operation is returned here.
void BaseMind::sightCreateOperation(const Operation & op, OpVector & res)
{
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
RootEntity ent(smart_dynamic_cast<RootEntity>(args.front()));
if (!ent.isValid()) {
log(ERROR, "Got sight(create) of non-entity");
return;
}
// This does not send a look, so anything added this way will not
// get flagged as visible until we get an appearance. This is important.
m_map.updateAdd(ent, op->getSeconds());
}
/// \brief Process the Sight of a Delete operation.
///
/// @param op The Delete operation to be processed.
/// @param res The result of the operation is returned here.
void BaseMind::sightDeleteOperation(const Operation & op, OpVector & res)
{
debug( std::cout << "Sight Delete operation" << std::endl << std::flush;);
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
const Root & obj = args.front();
const std::string & id = obj->getId();
if (!id.empty()) {
m_map.del(obj->getId());
} else {
log(WARNING, "Sight Delete with no ID");
}
}
/// \brief Process the Sight of a Move operation.
///
/// @param op The Move operation to be processed.
/// @param res The result of the operation is returned here.
void BaseMind::sightMoveOperation(const Operation & op, OpVector & res)
{
debug( std::cout << "BaseMind::sightOperation(Sight, Move)" << std::endl << std::flush;);
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
RootEntity ent(smart_dynamic_cast<RootEntity>(args.front()));
if (!ent.isValid()) {
log(ERROR, "Got sight(move) of non-entity");
return;
}
m_map.updateAdd(ent, op->getSeconds());
}
/// \brief Process the Sight of a Set operation.
///
/// @param op The Set operation to be processed.
/// @param res The result of the operation is returned here.
void BaseMind::sightSetOperation(const Operation & op, OpVector & res)
{
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
RootEntity ent(smart_dynamic_cast<RootEntity>(args.front()));
if (!ent.isValid()) {
log(ERROR, "Got sight(set) of non-entity");
return;
}
m_map.updateAdd(ent, op->getSeconds());
}
void BaseMind::SoundOperation(const Operation & op, OpVector & res)
{
// Deliver argument to sound things
// Louder sounds might eventually make character wake up
if (!m_isAwake) { return; }
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
const Root & arg = args.front();
Operation op2(Atlas::Objects::smart_dynamic_cast<Operation>(arg));
if (op2.isValid()) {
debug( std::cout << " args is an op!" << std::endl << std::flush;);
std::string event_name("sound_");
event_name += op2->getParents().front();
if (m_script->operation(event_name, op2, res) == 0) {
callSoundOperation(op2, res);
}
}
}
void BaseMind::SightOperation(const Operation & op, OpVector & res)
{
debug( std::cout << "BaseMind::SightOperation(Sight)" << std::endl << std::flush;);
// Deliver argument to sight things
if (!m_isAwake) { return; }
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
debug( std::cout << " no args!" << std::endl << std::flush;);
return;
}
const Root & arg = args.front();
Operation op2(Atlas::Objects::smart_dynamic_cast<Operation>(arg));
if (op2.isValid()) {
debug( std::cout << " args is an op!" << std::endl << std::flush;);
std::string event_name("sight_");
event_name += op2->getParents().front();
if (m_script->operation(event_name, op2, res) == 0) {
callSightOperation(op2, res);
}
} else /* if (op2->getObjtype() == "object") */ {
RootEntity ent(Atlas::Objects::smart_dynamic_cast<RootEntity>(arg));
if (!ent.isValid()) {
log(ERROR, "Arg of sigh operation is not an op or an entity");
return;
}
debug( std::cout << " arg is an entity!" << std::endl << std::flush;);
MemEntity * me = m_map.updateAdd(ent, op->getSeconds());
if (me != 0) {
me->setVisible();
}
}
}
void BaseMind::AppearanceOperation(const Operation & op, OpVector & res)
{
if (!m_isAwake) { return; }
const std::vector<Root> & args = op->getArgs();
std::vector<Root>::const_iterator Iend = args.end();
for (std::vector<Root>::const_iterator I = args.begin(); I != Iend; ++I) {
if (!(*I)->hasAttrFlag(Atlas::Objects::ID_FLAG)) {
log(ERROR, "BaseMind: Appearance op does not have ID");
continue;
}
const std::string & id = (*I)->getId();
MemEntity * me = m_map.getAdd(id);
if (me != 0) {
if ((*I)->hasAttrFlag(Atlas::Objects::STAMP_FLAG)) {
if ((int)(*I)->getStamp() != me->getSeq()) {
Look l;
Anonymous m;
m->setId(id);
l->setArgs1(m);
res.push_back(l);
}
} else {
log(ERROR, "BaseMind: Appearance op does not have stamp");
}
me->update(op->getSeconds());
me->setVisible();
}
}
}
void BaseMind::DisappearanceOperation(const Operation & op, OpVector & res)
{
if (!m_isAwake) { return; }
const std::vector<Root> & args = op->getArgs();
std::vector<Root>::const_iterator Iend = args.end();
for (std::vector<Root>::const_iterator I = args.begin(); I != Iend; ++I) {
const std::string & id = (*I)->getId();
if (id.empty()) { continue; }
MemEntity * me = m_map.get(id);
if (me != 0) {
me->update(op->getSeconds());
me->setVisible(false);
}
}
}
void BaseMind::operation(const Operation & op, OpVector & res)
{
// This might end up being quite tricky to do
// In the python the following happens here:
// Find out if the op refers to any ids we don't know about.
// If so create look operations to those ids
// Set the minds time and date
debug(std::cout << "BaseMind::operation("
<< op->getParents().front() << ")"
<< std::endl << std::flush;);
m_time.update((int)op->getSeconds());
m_map.check(op->getSeconds());
m_map.getAdd(op->getFrom());
m_map.sendLooks(res);
m_script->operation("call_triggers", op, res);
if (m_script->operation(op->getParents().front(), op, res) != 0) {
return;
}
const OpNo op_no = op->getClassNo();
switch (op_no) {
case Atlas::Objects::Operation::SIGHT_NO:
SightOperation(op, res);
break;
case Atlas::Objects::Operation::SOUND_NO:
SoundOperation(op, res);
break;
case Atlas::Objects::Operation::APPEARANCE_NO:
AppearanceOperation(op, res);
break;
case Atlas::Objects::Operation::DISAPPEARANCE_NO:
DisappearanceOperation(op, res);
break;
default:
// ERROR
break;
}
}
void BaseMind::callSightOperation(const Operation & op,
OpVector & res)
{
m_map.getAdd(op->getFrom());
OpNo op_no = op->getClassNo();
if (debug_flag && (op_no == OP_INVALID)) {
std::cout << getId() << " could not deliver sight of "
<< op->getParents().front()
<< std::endl << std::flush;
}
SUB_OP_SWITCH(op, op_no, res, sight)
}
void BaseMind::callSoundOperation(const Operation & op,
OpVector & res)
{
// This function essentially does nothing now, except add the source
// of the sound op to the map.
m_map.getAdd(op->getFrom());
OpNo op_no = op->getClassNo();
if (debug_flag && (op_no == OP_INVALID)) {
std::cout << getId() << " could not deliver sound of "
<< op->getParents().front()
<< std::endl << std::flush;
}
#if 0
const MemEntityDict & ents = m_map.getEntities();
MemEntityDict::const_iterator Iend = ents.end();
for (MemEntityDict::const_iterator I = ents.begin(); I != Iend; ++I) {
std::cout << I->second->getId() << ":" << I->second->getType() << " is "
<< ( I->second->isVisible() ? "visible " : "hid " )
<< I->second->lastSeen()
<< std::endl << std::flush;
}
#endif
// SUB_OP_SWITCH(op, op_no, res, sound)
}
|