Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-07-28 20:56:18 +0000
committerStijn Buys <ingar@osirion.org>2008-07-28 20:56:18 +0000
commit40698bac1e7d03b8f472fa4d8d35aa644da95c5f (patch)
tree2217f7b82171078f68340500707cbd4ac7ccb42a /src/core/zone.cc
parentd389a31f9816b55d8c7685ec24b9ab814252d693 (diff)
d'oh
Diffstat (limited to 'src/core/zone.cc')
-rw-r--r--src/core/zone.cc212
1 files changed, 212 insertions, 0 deletions
diff --git a/src/core/zone.cc b/src/core/zone.cc
new file mode 100644
index 0000000..584f75d
--- /dev/null
+++ b/src/core/zone.cc
@@ -0,0 +1,212 @@
+/*
+ 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 <iomanip>
+
+#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::find_zone(std::string const & searchname)
+{
+ std::stringstream str(aux::lowercase(searchname));
+ unsigned int id;
+ Zone *zone = 0;
+ if (str >> id) {
+ zone = find(id);
+ }
+
+ if (!zone) {
+ zone = find(searchname);
+ }
+
+ return zone;
+}
+
+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::list_zone(std::string const & searchname)
+{
+ std::stringstream str(aux::lowercase(searchname));
+ unsigned int id;
+ Zone *zone = 0;
+ if (str >> id) {
+ zone = find(id);
+ }
+
+ if (!zone) {
+ zone = find(searchname);
+ }
+
+ if (!zone) {
+ con_print << "Unknown zone '" << searchname << "'" << std::endl;
+ } else {
+ con_print << "zone " << zone->id() << " " << zone->label() << " " << zone->name() << std::endl;
+
+ for (Content::iterator it = zone->zone_content.begin(); it != zone->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()
+ << " " << entity->label() << std::endl;
+ }
+ con_print << zone->zone_content.size() << " zone entities" << std::endl;
+ }
+}
+
+void Zone ::clear()
+{
+ for (Registry::iterator it = zone_registry.begin(); it != zone_registry.end(); it++) {
+ delete (*it).second;
+ }
+ zone_registry.clear();
+
+}
+/* ---- The Zone class --------------------------------------------- */
+
+Zone::Zone(std::string const & label) :
+ zone_label(label)
+{
+ zone_id = 0;
+ zone_name.clear();
+ zone_sky.clear();
+ zone_sky_texture = 0;
+}
+
+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::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;
+}
+
+void Zone::serialize_server_update(std::ostream & os) const
+{
+ os << zone_id << " ";
+ os << zone_label << " ";
+ os << "\"" << zone_name << "\" ";
+ os << "\"" << zone_sky << "\" ";
+
+}
+
+void Zone::receive_server_update(std::istream &is)
+{
+ is >> zone_label;
+
+ std::string n;
+ char c;
+
+ while ( (is.get(c)) && (c != '"'));
+ while ( (is.get(c)) && (c != '"'))
+ n += c;
+ zone_name.assign(n);
+ n.clear();
+
+ while ( (is.get(c)) && (c != '"'));
+ while ( (is.get(c)) && (c != '"'))
+ n += c;
+ zone_sky.assign(n);
+ n.clear();
+}
+
+}