Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/zone.cc212
-rw-r--r--src/core/zone.h150
2 files changed, 362 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();
+}
+
+}
diff --git a/src/core/zone.h b/src/core/zone.h
new file mode 100644
index 0000000..cd1fec2
--- /dev/null
+++ b/src/core/zone.h
@@ -0,0 +1,150 @@
+/*
+ core/zone.h
+ This file is part of the Osirion project and is distributed under
+ the terms of the GNU General Public License version 2
+*/
+
+#ifndef __INCLUDED_CORE_ZONE_H__
+#define __INCLUDED_CORE_ZONE_H__
+
+#include <string>
+#include <list>
+#include <map>
+
+namespace core {
+
+class Zone;
+
+}
+
+#include "core/entity.h"
+
+namespace core
+{
+
+/// a Zone contains a compartment of the game universe
+class Zone {
+public:
+ /// type definition for the content of a zone
+ typedef std::list<Entity *> Content;
+
+ /// type definition for the zone registry
+ typedef std::map<unsigned int, Zone *> Registry;
+
+ /* ---- static functions ----------------------------------- */
+
+ /// add a zone to the zone registry
+ static void add(Zone *zone);
+
+ /// add a zone with a specific id to the registry
+ static void add(Zone *zone, unsigned int id);
+
+ /// find a zone in the zone registry
+ static Zone *find(unsigned int id);
+
+ /// find a zone in the zone registry
+ static Zone *find(std::string const & name);
+
+ /// find a zone in the zone registry (searches on id and name)
+ static Zone *find_zone(std::string const & searchname);
+
+ /// remove a zone from the zone registry
+ static void remove(Zone *zone);
+
+ /// list the zone registry
+ static void list();
+
+ /// list the content of a single zone
+ static void list_zone(std::string const & searchname);
+
+ /// clear the zone registry
+ static void clear();
+
+ /// the zone registry
+ static inline Registry & registry() { return zone_registry; }
+
+ /* ---- Zone class ----------------------------------------- */
+
+ /// create a new zone
+ Zone(std::string const & label);
+
+ /// create a zone from stream data
+ Zone(std::istream & is);
+
+
+ /// delete a zone
+ ~Zone();
+
+ /* ---- inspectors ----------------------------------------- */
+
+ /// zone id
+ inline unsigned int id() const { return zone_id; }
+
+ /// zone label
+ inline std::string const & label() const { return zone_label; }
+
+ /// zone name
+ inline std::string const & name() const { return zone_name; }
+
+ /// the name of the texture to be used as sky for this zone
+ inline std::string const & sky() const { return zone_sky; }
+
+ /// texture id for the sky
+ inline size_t sky_texture() const { return zone_sky_texture; }
+
+ /// find an entity inside a zone
+ Entity *find_entity(Entity *entity);
+
+ /// find an entity inside a zone
+ Entity *find_entity(unsigned int id);
+
+ /* ---- mutators ------------------------------------------- */
+
+ /// set the Zone label
+ inline void set_label(std::string const & label) { zone_label.assign(label); }
+
+ /// set the Zone label
+ inline void set_name(std::string const & name) { zone_name.assign(name); }
+
+ /// set the sky texture name to be used for this zone
+ inline void set_sky(std::string const & sky) { zone_sky.assign(sky); }
+
+ /// set the texture id for the sky
+ inline void set_sky_texture(size_t texture) { zone_sky_texture = texture; }
+
+ /* ---- serializers ---------------------------------------- */
+
+ /// serialize a server-to-client update on a stream
+ void serialize_server_update(std::ostream & os) const;
+
+ /// receive a server-to-client update from a stream
+ void receive_server_update(std::istream &is);
+
+ /* ---- zone content --------------------------------------- */
+
+ /// the entities belonging to this zone
+ inline Content & content() { return zone_content; }
+
+ /// add an entity to this zone
+ void add(Entity *entity);
+
+ /// remove an entity from this zone
+ void remove(Entity *entity);
+
+private:
+ unsigned int zone_id;
+
+ std::string zone_label;
+ std::string zone_name;
+ std::string zone_sky;
+
+ size_t zone_sky_texture;
+
+ Content zone_content;
+ static Registry zone_registry;
+};
+
+}
+
+#endif // __INCLUDED_CORE_ZONE_H__
+