/* core/zone.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include "auxiliary/functions.h" #include "core/zone.h" #include "sys/sys.h" namespace core { /* ---- Static functions for the Zone registry ------------------- */ Zone::Registry Zone::zone_registry; void Zone::add(Zone *zone) { unsigned int id = 1; for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end() && id == (*it).second->zone_id; it++) { id++; } zone->zone_id = id; zone_registry[id] = zone; } void Zone::add(Zone *zone, unsigned int id) { Registry::iterator it = zone_registry.find(id); if (it == zone_registry.end()) { zone->zone_id = id; zone_registry[id] = zone; } } Zone *Zone::find(unsigned int id) { Registry::iterator it = zone_registry.find(id); if (it == zone_registry.end()) return 0; else return (*it).second; } Zone *Zone::find(std::string const & name) { for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) { if ((*it).second->label().compare(name) == 0) { return (*it).second; } } return 0; } Zone *Zone::search(std::string const & searchname) { std::string strsearchkey(aux::lowercase(searchname)); std::stringstream str(strsearchkey); unsigned int id; if (str >> id) return find(id); if (strsearchkey.size() < 3) { return 0; } std::string label; std::string name; for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) { Zone *zone = (*it).second; label.assign(zone->label()); if (label.size() && (label.find(strsearchkey) != std::string::npos)) { return zone; } name.assign(aux::lowercase(zone->name())); if (name.size() && (name.find(strsearchkey) != std::string::npos)) { return zone; } } return 0; } void Zone::list() { for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) { Zone *zone = (*it).second; con_print << " id " << std::setw(4) << zone->id() << " " << zone->label() << std::endl; } con_print << zone_registry.size() << " registered zones" << std::endl; } void Zone ::clear() { for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) { delete(*it).second; } zone_registry.clear(); } /* ---- class Zone ------------------------------------------------- */ Zone::Zone(std::string const & label) { zone_id = 0; zone_sky.clear(); zone_sky_texture = 0; zone_defaultview = 0; set_label(label); } Zone::Zone(std::istream & is) { zone_id = 0; zone_sky_texture = 0; receive_server_update(is); } Zone::~Zone() { for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { } zone_content.clear(); } void Zone::print() { con_print << "id: ^B" << id() << " ^Nlabel: ^B" << label() << " ^Nname: ^B" << name() << std::endl; for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { Entity *entity = (*it); con_print << " id " << std::setw(4) << entity->id() << " type " << std::setw(4) << entity->type() << ":" << std::setw(4) << entity->moduletype() << " " << std::setw(24) << entity->label() << " ^B" << entity->name() << "^N" << std::endl; } con_print << zone_content.size() << " registered zone entities" << std::endl; } void Zone::add(Entity *entity) { zone_content.push_back(entity); } void Zone::remove(Entity *entity) { for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { if ((*it) == entity) { zone_content.erase(it); return; } } } Entity *Zone::find_entity(Entity *entity) { for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { if ((*it) == entity) { return entity; } } return 0; } Entity *Zone::find_entity(unsigned int id) { for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { if ((*it)->id() == id) { return (*it); } } return 0; } Entity *Zone::find_entity(const std::string & label) { for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { if ((*it)->label().compare(label) == 0) { return (*it); } } return 0; } Entity *Zone::search_entity(const std::string & searchname) { std::string strsearchkey(aux::lowercase(searchname)); std::stringstream str(strsearchkey); unsigned int id; if (str >> id) return find_entity(id); if (strsearchkey.size() < 3) { return 0; } std::string label; std::string name; for (Content::iterator it = zone_content.begin(); it != zone_content.end(); it++) { Entity *entity = (*it); label.assign(entity->label()); if (label.size() && (label.find(strsearchkey) != std::string::npos)) { return entity; } name.assign(aux::lowercase(entity->name())); if (name.size() && (name.find(strsearchkey) != std::string::npos)) { return entity; } } return 0; } void Zone::serialize_server_update(std::ostream & os) const { os << label() << " "; os << "\"" << name() << "\" "; os << "\"" << zone_sky << "\" "; os << (zone_defaultview ? zone_defaultview->id() : 0); } void Zone::receive_server_update(std::istream &is) { unsigned int id = 0; std::string n; char c; is >> n; set_label(n); n.clear(); while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; set_name(n); n.clear(); while ((is.get(c)) && (c != '"')); while ((is.get(c)) && (c != '"')) n += c; zone_sky.assign(n); n.clear(); is >> id; zone_defaultview = Entity::find(id); } }