From d389a31f9816b55d8c7685ec24b9ab814252d693 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 28 Jul 2008 19:37:31 +0000 Subject: zone support --- src/core/entity.cc | 105 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 27 deletions(-) (limited to 'src/core/entity.cc') diff --git a/src/core/entity.cc b/src/core/entity.cc index c45825b..20e34ee 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -19,17 +19,17 @@ using math::Vector3f; /* ---- Static functions for the Entity registry ------------------- */ -std::map Entity::registry; +Entity::Registry Entity::entity_registry; void Entity::add(Entity *ent) { Registry::iterator it; unsigned int id = 1; - for (it = registry.begin(); it != registry.end() && id == (*it).second->id(); it++) { + for (it = entity_registry.begin(); it != entity_registry.end() && id == (*it).second->id(); it++) { id++; } ent->entity_id = id; - registry[id] = ent; + entity_registry[id] = ent; } void Entity::add(Entity *ent, unsigned int id) @@ -39,33 +39,33 @@ void Entity::add(Entity *ent, unsigned int id) return; } ent->entity_id = id; - registry[id] = ent; + entity_registry[id] = ent; } Entity *Entity::find(unsigned int id) { - std::map::iterator it = registry.find(id); - if (it == registry.end()) + Registry::iterator it = entity_registry.find(id); + if (it == entity_registry.end()) return 0; else return (*it).second; } -void Entity::remove(unsigned int id) +void Entity::erase(unsigned int id) { - std::map::iterator it = registry.find(id); - if (it != registry.end()) { + Registry::iterator it = entity_registry.find(id); + if (it != entity_registry.end()) { delete((*it).second); - registry.erase(it); + entity_registry.erase(it); } else { - con_warn << "Could not remove entity " << id << "!\n"; + con_warn << "Could not erase entity " << id << "!\n"; } } void Entity::list() { - std::map::iterator it; - for (it = registry.begin(); it != registry.end(); it++) { + Registry::iterator it; + for (it = entity_registry.begin(); it != entity_registry.end(); it++) { std::string typeindicator; Entity *entity = (*it).second; con_print << " id " << std::setw(4) << entity->id() @@ -73,7 +73,7 @@ void Entity::list() << ":" << std::setw(4) << entity->moduletype() << " " << entity->label() << std::endl; } - con_print << registry.size() << " registered entities" << std::endl; + con_print << entity_registry.size() << " registered entities" << std::endl; } /*----- Entity ----------------------------------------------------- */ @@ -101,11 +101,15 @@ Entity::Entity(unsigned int flags) : entity_clientstate = 0; + entity_zone = 0; + add(this); } Entity::Entity(std::istream & is) { + entity_zone = 0; + // type is already determined unsigned int s; std::string n; @@ -113,6 +117,13 @@ Entity::Entity(std::istream & is) is >> entity_id; is >> entity_moduletypeid; is >> entity_flags; + is >> s; + + set_zone(Zone::find(s)); + if (entity_zone && !s) { + con_warn << "Received entity " << entity_id << " for unknown zone " << s << "!" << std::endl; + } + is >> entity_location; is >> entity_color; is >> entity_color_second; @@ -162,6 +173,29 @@ Entity::~Entity() { if (entity_clientstate) delete entity_clientstate; + + if (entity_zone) + entity_zone->remove(this); +} + +void Entity::die() +{ + entity_destroyed = true; +} + +void Entity::set_zone(Zone *zone) +{ + if (entity_zone == zone) + return; + + if (entity_zone) + entity_zone->remove(this); + + entity_zone = zone; + entity_dirty = true; + + if (entity_zone) + entity_zone->add(this); } void Entity::serialize(std::ostream & os) const @@ -170,6 +204,7 @@ void Entity::serialize(std::ostream & os) const << entity_id << " " << entity_moduletypeid << " " << entity_flags << " " + << (entity_zone ? entity_zone->id() : 0) << " " << entity_location << " " << entity_color << " " << entity_color_second << " " @@ -186,7 +221,7 @@ void Entity::serialize_client_update(std::ostream & os) const { } -void Entity::recieve_client_update(std::istream &is) +void Entity::receive_client_update(std::istream &is) { } @@ -194,7 +229,7 @@ void Entity::serialize_server_update(std::ostream & os) const { } -void Entity::recieve_server_update(std::istream &is) +void Entity::receive_server_update(std::istream &is) { } @@ -244,42 +279,58 @@ void EntityDynamic::serialize_client_update(std::ostream & os) const { } -void EntityDynamic::recieve_client_update(std::istream &is) +void EntityDynamic::receive_client_update(std::istream &is) { } void EntityDynamic::serialize_server_update(std::ostream & os) const { + os << (entity_zone ? entity_zone->id() : 0) << " "; os << entity_location << " "; os << entity_axis.forward() << " "; os << entity_axis.left() << " "; os << entity_speed; } -void EntityDynamic::recieve_server_update(std::istream &is) +void EntityDynamic::receive_server_update(std::istream &is) { + unsigned int zone_id; + is >> zone_id; is >> entity_location; // axis up vector is the crossproduct of forward and left is >> entity_axis[0]; is >> entity_axis[1]; entity_axis[2] = math::crossproduct(entity_axis.forward(), entity_axis.left()); is >> entity_speed; + + + if (!zone_id) { + if (entity_zone) { + entity_zone->remove(this); + entity_zone = 0; + } + } else { + if (zone_id != entity_zone->id()) { + set_zone(Zone::find(zone_id)); + } + } } /*----- EntityControlable ------------------------------------------ */ -EntityControlable::EntityControlable(Player *player, unsigned int flags) : +EntityControlable::EntityControlable(Player *owner, unsigned int flags) : EntityDynamic(flags) { - entity_owner = player; - if (entity_owner) - entity_owner->add_asset(this); entity_thrust = 0; target_direction = 0.0f; target_thrust = 0.0f; target_pitch = 0.0f; target_roll = 0.0f; + + entity_owner = 0; + if (owner) + owner->add_asset(this); } EntityControlable::EntityControlable(std::istream & is) : @@ -305,7 +356,7 @@ void EntityControlable::serialize(std::ostream & os) const { EntityDynamic::serialize(os); os << " " << entity_thrust; - os << " " << entity_owner->id(); + os << " " << ( entity_owner ? entity_owner->id() : 0); } void EntityControlable::serialize_client_update(std::ostream & os) const @@ -318,9 +369,9 @@ void EntityControlable::serialize_client_update(std::ostream & os) const } -void EntityControlable::recieve_client_update(std::istream &is) +void EntityControlable::receive_client_update(std::istream &is) { - EntityDynamic::recieve_client_update(is); + EntityDynamic::receive_client_update(is); is >> target_direction; is >> target_pitch; is >> target_thrust; @@ -333,9 +384,9 @@ void EntityControlable::serialize_server_update(std::ostream & os) const os << " " << entity_thrust; } -void EntityControlable::recieve_server_update(std::istream &is) +void EntityControlable::receive_server_update(std::istream &is) { - EntityDynamic::recieve_server_update(is); + EntityDynamic::receive_server_update(is); is >> entity_thrust; } -- cgit v1.2.3