cyphesis/rulesets/Stackable.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

108 lines
2.9 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/Root.h>
#include <Atlas/Objects/Operation/Combine.h>
#include <Atlas/Objects/Operation/Divide.h>
#include <Atlas/Objects/Operation/Delete.h>
#include <Atlas/Objects/Operation/Login.h>
#include <common/const.h>
// A stackable object, ie one which can represent multiple object of the
// same type. Used for things like coins.
#include "Stackable.h"
#include "Script.h"
using Atlas::Message::Object;
Stackable::Stackable() : num(1)
{
}
Stackable::~Stackable()
{
}
const Object & Stackable::operator[](const string & aname)
{
if (aname == "num") {
attributes[aname] = Object(num);
}
return Thing::operator[](aname);
}
void Stackable::set(const string & aname, const Object & attr)
{
if ((aname == "num") && attr.IsInt()) {
num = attr.AsInt();
} else {
Thing::set(aname, attr);
}
}
void Stackable::addToObject(Object & obj) const
{
Object::MapType & omap = obj.AsMap();
if (num != 1) {
omap["num"] = Object(num);
}
Thing::addToObject(obj);
}
oplist Stackable::Operation(const Combine & op)
{
oplist res;
if (script->Operation("combine", op, res) != 0) {
return(res);
}
const Object::ListType & args = op.GetArgs();
for(Object::ListType::const_iterator I= args.begin(); I!= args.end(); I++) {
const string & id = I->AsMap().find("id")->second.AsString();
if (id == fullid) { continue; }
Stackable * obj = (Stackable*)world->getObject(id);
if (obj->type != type) { continue; }
num = num + obj->num;
Delete * d = new Delete(Delete::Instantiate());
Object::MapType dent;
dent["id"] = id;
d->SetTo(id);
d->SetArgs(Object::ListType(1,dent));
res.push_back(d);
}
return res;
// FIXME DO we need to send a sight?
}
oplist Stackable::Operation(const Divide & op)
{
oplist res;
if (script->Operation("divide", op, res) != 0) {
return(res);
}
const Object::ListType & args = op.GetArgs();
for(Object::ListType::const_iterator I= args.begin(); I!= args.end(); I++) {
const Object::MapType & ent = I->AsMap();
int new_num = 1;
Object::MapType::const_iterator J = ent.find("num");
if (J != ent.end()) {
if (J->second.IsInt()) { new_num = J->second.AsInt(); }
}
if (num <= new_num) { continue; }
Object::MapType new_ent;
Object::ListType parents(1,type);
new_ent["parents"] = parents;
new_ent["num"] = new_num;
Create * c = new Create( Create::Instantiate());
c->SetArgs(Object::ListType(1,new_ent));
c->SetTo(fullid);
res.push_back(c);
}
return res;
// FIXME DO we need to send a sight?
}