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
|
#include <assert.h>
#include <algorithm>
#include "tileset.h"
#include "generator_object.h"
#include "mrt/random.h"
#include "utils.h"
void Tileset::getPrimaryBoxes(std::deque<std::string> &boxes) {
boxes.clear();
for(Objects::const_iterator i = _objects.begin(); i != _objects.end() ; ++i) {
boxes.push_back(i->first);
}
}
void Tileset::start(const std::string &name, Attrs &attr) {
if (name == "tileset")
return;
if (name == "background" && attr["id"].empty()) {
throw_ex(("empty id for element %s", name.c_str()));
}
if (name == "box") {
if (attr["in"].empty() && attr["out"].empty())
throw_ex(("box must provide at least one of 'in'/'out' attrs."));
attr["id"] = attr["in"] + "|" + attr["out"];
}
_cdata.clear();
_attr = attr;
}
void Tileset::cdata(const std::string &data) {
_cdata += data;
}
void Tileset::end(const std::string &name) {
if (name == "tileset")
return;
if (_objects.find(name) != _objects.end())
throw_ex(("duplicate id %s", name.c_str()));
std::string id = _attr["id"];
GeneratorObject *o = GeneratorObject::create(name, _attr, _cdata);
LOG_DEBUG(("adding '%s' object with id '%s' (%p)", name.c_str(), id.c_str(), (void *)o));
_objects.insert(Objects::value_type(id, o));
}
const GeneratorObject *Tileset::getObject(const std::string &name) const {
if (name == "?") {
if (_objects.empty())
return NULL;
int n = mrt::random(_objects.size());
Objects::const_iterator i = _objects.begin();
while(n--) {
++i;
}
return i->second;
}
Objects::const_iterator i = _objects.find(name);
if (i == _objects.end())
return NULL;
assert(i->second != NULL);
return i->second;
}
Tileset::~Tileset() {
std::for_each(_objects.begin(), _objects.end(), delete_ptr2<Objects::value_type>());
}
|