From 2c98d3eef488233b99a76ca44d69c1c9d53404af Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 7 Dec 2014 16:12:49 +0000 Subject: Cleanup of the slots code, unified model weapon and dock tags into a single slots list, load dock tags into entity slots, represent entity slot locations in entity coordinate space, have r_slots render fixed-size slot indicators regardless of model scale. --- src/game/base/game.cc | 4 +++- src/game/base/patrol.cc | 17 ++++++----------- src/game/base/platform.cc | 12 +++++------- src/game/base/ship.cc | 40 +++++++++++++++++++++++++--------------- src/game/base/weapon.h | 8 ++++---- 5 files changed, 43 insertions(+), 38 deletions(-) (limited to 'src/game') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 201b18c..f802ff2 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -2579,8 +2579,10 @@ bool Game::validate_zone(core::Zone *zone) } if (entity->model()) { + const float modelscale = entity->radius() / entity->model()->radius(); + entity->add_slots(); - entity->slots()->load(entity->model()); + entity->slots()->load(entity->model(), modelscale); } } diff --git a/src/game/base/patrol.cc b/src/game/base/patrol.cc index 51b7e2a..9b805de 100644 --- a/src/game/base/patrol.cc +++ b/src/game/base/patrol.cc @@ -318,16 +318,11 @@ void Patrol::create_patrol() npc->menus().clear(); } - // install inventory - if (!npc->inventory()) { - npc->add_inventory(); - } + // inventory has been added by the Ship constructor + assert (npc->inventory()); - // install slots - if (!npc->slots()) { - npc->add_slots(); - slots()->load(model()); - } + // slots have been added by the Ship constructor + assert(npc->slots()); // install weapons for (core::Slots::iterator slit = npc->slots()->begin(); slit != npc->slots()->end(); ++slit) { @@ -335,11 +330,11 @@ void Patrol::create_patrol() core::Item *item = 0; - if (slot->type() == model::Weapon::Cannon) { + if (slot->type() == model::Slot::Cannon) { if (npctype->cannon()) { item = new core::Item(npctype->cannon()); } - } else if (slot->type() == model::Weapon::Turret) { + } else if (slot->type() == model::Slot::Turret) { if (npctype->turret()) { item = new core::Item(npctype->turret()); } diff --git a/src/game/base/platform.cc b/src/game/base/platform.cc index 77059ae..bfad7ef 100644 --- a/src/game/base/platform.cc +++ b/src/game/base/platform.cc @@ -88,17 +88,15 @@ void Platform::frame(const unsigned long elapsed) // platforms do not need weapons in inventory to fire if (enemylist.size()) { - const float modelscale = radius() / (model() ? model()->radius() : 1.0f); - - for (core::Slots::iterator it = slots()->begin(); it != slots()->end(); it++) { - core::Slot *slot = (*it); + for (core::Slots::iterator slit = slots()->begin(); slit != slots()->end(); slit++) { + core::Slot *slot = (*slit); // found out if this slot is a cannon or a turret const Weapon *weapon = 0; - if (slot->type() == model::Weapon::Cannon) { + if (slot->type() == model::Slot::Cannon) { weapon = cannon(); - } else if (slot->type() == model::Weapon::Turret) { + } else if (slot->type() == model::Slot::Turret) { weapon = turret(); } @@ -111,7 +109,7 @@ void Platform::frame(const unsigned long elapsed) } // location of the slot in world coordinates - const math::Vector3f slot_location(location() + (axis() * slot->location() * modelscale)); + const math::Vector3f slot_location(location() + (axis() * slot->location())); // find a target for this slot Ship *current_enemy = 0; diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index b3f6fd0..9a4218f 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -96,8 +96,10 @@ Ship::Ship(core::Player *owner, const ShipModel *shipmodel) : core::EntityContro inventory()->set_capacity(ship_shipmodel->maxcargo()); if (model()) { + const float modelscale = radius() / model()->radius(); + add_slots(); - slots()->load(model()); + slots()->load(model(), modelscale); } // menus for docked players @@ -537,20 +539,30 @@ void Ship::launch() get_axis().assign(ship_dock->axis()); get_location().assign(ship_dock->location() + (ship_dock->axis().forward() * (PLANET_SAFE_DISTANCE + this->radius() + ship_dock->radius()))); - } else { - if (ship_dock->model() && ship_dock->model()->docks().size()) { - // choose a random dock to launch from - model::Model::Docks::iterator dit = ship_dock->model()->docks().begin(); - int launchbay_index = math::randomi(ship_dock->model()->docks().size()); - for (int i = 0; i < launchbay_index; i++) { - ++dit; + } else { + int nbdocks = 0; + + if (ship_dock->slots()) { + for (core::Slots::iterator slit = ship_dock->slots()->begin(); slit != ship_dock->slots()->end(); ++slit) { + if ((*slit)->type() == model::Slot::Dock) { + ++nbdocks; + } } - model::Dock *launchbay = (*dit); - - const float modelscale = ship_dock->radius() / ship_dock->model()->radius(); + } + + if (nbdocks) { + // choose a random dock to launch from + core::Slots::iterator slit = ship_dock->slots()->begin(); + int launchbay_index = math::randomi(nbdocks); + for (int i = 0; i < launchbay_index; ++slit) { + if ((*slit)->type() == model::Slot::Dock) { + ++i; + } + } + core::Slot *launchbay = (*slit); get_axis().assign(ship_dock->axis() * launchbay->axis()); - get_location().assign(ship_dock->location() + ship_dock->axis() * ( launchbay->location() * modelscale + launchbay->axis().forward() * radius())); + get_location().assign(ship_dock->location() + ship_dock->axis() * ( launchbay->location() + launchbay->axis().forward() * radius())); } else { const float r = radius() + ship_dock->radius(); @@ -1173,8 +1185,6 @@ void Ship::frame(const unsigned long elapsed) // fire weapons if (slots() && (state() == core::Entity::Normal) && has_target_controlflag(core::EntityControlable::ControlFlagFire)) { - const float modelscale = radius() / (model() ? model()->radius() : 1.0f); - for (core::Slots::iterator it = slots()->begin(); it != slots()->end(); it++) { core::Slot *slot = (*it); @@ -1198,7 +1208,7 @@ void Ship::frame(const unsigned long elapsed) const float projectile_radius = core::PROJECTILE_RADIUS; // FIXME this should be defined somewhere math::Axis projectile_axis(axis() * slot->axis()); - math::Vector3f projectile_location(location() + (axis() * slot->location() * modelscale) + projectile_axis.forward() * projectile_radius); + math::Vector3f projectile_location(location() + axis() * slot->location() + projectile_axis.forward() * projectile_radius); math::Vector3f projectile_direction(target_aim - projectile_location); projectile_direction.normalize(); float cosa = math::dotproduct(projectile_direction, projectile_axis.forward()); diff --git a/src/game/base/weapon.h b/src/game/base/weapon.h index a6a5731..857b3cd 100644 --- a/src/game/base/weapon.h +++ b/src/game/base/weapon.h @@ -88,17 +88,17 @@ public: return weapon_projectile_soundname; } - inline const model::Weapon::Type slot_type() const + inline const model::Slot::Type slot_type() const { switch (weapon_subtype) { case Cannon: - return model::Weapon::Cannon; + return model::Slot::Cannon; break; case Turret: - return model::Weapon::Turret; + return model::Slot::Turret; break; default: - return model::Weapon::Unmountable; + return model::Slot::None; } } -- cgit v1.2.3