cyphesis/rulesets/MemMap.cpp
Al Riddoch 2ccfaadef3 2001-04-14 Al Riddoch <alriddoch@zepler.org>
* Substantial re-work of object model. Moved many attributes out
	  of BaseEntity class. Changed WoldRouter so it now handles
	  objects in terms of Entity, rather than BaseEntity.

	* Code cleanups, including removing reduntant includes, and
	  getting rid of C style comments.

	* Fixed the way attributes are handled in Set operations and
	  when merging from an Atlas::Message::Object. This fixed
	  problem with setting gender of characters.

	* rulesets/Pedestrian.*, rulesets/Movement.*: Re-named
	  Movement variables to make them clearer. Added in
	  separate handling for collisions from moving to
	  target position. Modified collision handling so
	  objects can slide next to each other.

	* rulesets/Thing.cpp, rulesets/Character.cpp: Cleaned up and
	  rationalised movement code. Much shorter, faster and
	  more compact than before.

	* Made better use of iterators throughout, eliminating unnecessary
	  lookups, and copying of data.

	* Moved all virtual functions out of header files, making
	  less work for the linker.

	* Made more use of const references to avoid the need to copy
	  data before being able to inspect it.

Al Riddoch <alriddoch@zepler.org>
2001-04-16 16:26:44 +00:00

134 lines
4 KiB
C++

// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2000,2001 Alistair Riddoch
#include <Atlas/Message/Object.h>
#include <Atlas/Objects/Operation/Login.h>
#include <Atlas/Objects/Operation/Look.h>
using Atlas::Objects::Operation::Look;
#include <modules/Location.h>
#include <common/debug.h>
#include "Entity.h"
#include "MemMap.h"
#include "MemMap_methods.h"
#include "Script.h"
Entity * MemMap::add(const Object & entity)
{
debug( cout << "MemMap::add" << endl << flush;);
if (!entity.IsMap()) {
return NULL;
}
const Object::MapType & entmap = entity.AsMap();
Object::MapType::const_iterator I = entmap.find("id");
if ((I == entmap.end()) || (I->second.AsString().size() == 0)) {
return NULL;
}
const string & id = I->second.AsString();
if (get(id)) {
return update(entity);
}
Entity * thing = new Entity;
thing->fullid = id;
I = entmap.find("name");
if ((I != entmap.end()) && I->second.IsString()) {
thing->name = I->second.AsString();
}
I = entmap.find("type");
if ((I != entmap.end()) && I->second.IsString()) {
thing->type = I->second.AsString();
}
thing->merge(entmap);
I = entmap.find("loc");
if (I != entmap.end()) {
getAdd(I->second.AsString());
}
thing->getLocation(entmap, things);
return addObject(thing);
}
Entity * MemMap::update(const Object & entity)
{
debug( cout << "MemMap::update" << endl << flush;);
if (!entity.IsMap()) {
return NULL;
}
const Object::MapType & entmap = entity.AsMap();
Object::MapType::const_iterator I = entmap.find("id");
if (I == entmap.end()) {
return NULL;
}
const string & id = I->second.AsString();
if (id.size() == 0) {
return NULL;
}
debug( cout << " updating " << id << endl << flush;);
edict_t::iterator J = things.find(id);
if (J == things.end()) {
return add(entity);
}
debug( cout << " " << id << " has already been spotted" << endl << flush;);
Entity * thing = J->second;
debug( cout << " got " << thing << endl << flush;);
// I am not sure what the deal is with all the "needTrueValue stuff
// below yet. FIXME find out exactly what is required.
I = entmap.find("name");
if (I != entmap.end() && I->second.IsString()) {
thing->name = I->second.AsString();
}
I = entmap.find("type");
if (I != entmap.end() && I->second.IsString()) {
thing->type = I->second.AsString();
}
debug( cout << " got " << thing << endl << flush;);
thing->merge(entmap);
thing->getLocation(entmap,things);
//needTrueValue=["type","contains","instance","id","location","stamp"];
//for (/*(key,value) in entity.__dict__.items()*/) {
//if (value or not key in needTrueValue) {
//setattr(obj,key,value);
//}
//}
list<string>::const_iterator K;
for(K = updateHooks.begin(); K != updateHooks.end(); K++) {
script->hook(*K, thing);
}
//for (/*hook in MemMap::update_hooks*/) {
//hook(obj);
//}
return thing;
}
list<Entity *> MemMap::findByType(const string & what)
{
list<Entity *> res;
edict_t::const_iterator I;
for(I = things.begin(); I != things.end(); I++) {
Entity * item = (Entity *)I->second;
debug( cout << "F" << what << ":" << item->type << ":" << item->fullid << endl << flush;);
if (item->type == what) {
res.push_back((Entity*)I->second);
}
}
return res;
}
list<Entity *> MemMap::findByLocation(const Location & loc, double radius)
{
list<Entity *> res;
edict_t::const_iterator I;
for(I = things.begin(); I != things.end(); I++) {
const Location & oloc = I->second->location;
if (!loc || !oloc) {
continue;
}
if ((oloc.ref->fullid == loc.ref->fullid) &&
(loc.coords.distance(oloc.coords) < radius)) {
res.push_back((Entity*)I->second);
}
}
return res;
}