diff options
Diffstat (limited to 'src/game/base/patrol.cc')
-rw-r--r-- | src/game/base/patrol.cc | 114 |
1 files changed, 83 insertions, 31 deletions
diff --git a/src/game/base/patrol.cc b/src/game/base/patrol.cc index 850efb9..f3cfa07 100644 --- a/src/game/base/patrol.cc +++ b/src/game/base/patrol.cc @@ -79,6 +79,13 @@ Patrol::~Patrol() } patrol_waypoints.clear(); + // delete npc types + for (NPCTypes::iterator it = patrol_npctypes.begin(); it != patrol_npctypes.end(); ++it) { + delete(*it); + (*it) = 0; + } + patrol_npctypes.clear(); + // detach and delete remaining members for (Members::iterator it = patrol_members.begin(); it != patrol_members.end(); ++it) { NPC *member = (*it); @@ -167,22 +174,12 @@ void Patrol::validate() ++it; } } - - size_t nbships = 0; - for (core::Inventory::Items::const_iterator it = inventory()->items().begin(); it != inventory()->items().end(); it++) { - core::Item *item = (*it); - - if (item->info()->type() == ShipModel::infotype()) { - nbships++; - } - } - + if (patrol_waypoints.size() == 0) { con_warn << " Patrol '" << label() << "' without waypoints" << "\n"; - die(); - - } else if (nbships == 0) { - con_warn << " Patrol '" << label() << "' without ship types" << "\n"; + die(); + } else if (patrol_npctypes.size() == 0) { + con_warn << " Patrol '" << label() << "' without NPC types" << "\n"; die(); } else if (!(*patrol_waypoints.begin())->target()->has_flag(core::Entity::Dockable)) { con_warn << " Patrol '" << label() << "' spawn set to non-dockable target" << "\n"; @@ -194,10 +191,10 @@ void Patrol::validate() // spawn waypoint is current patrol_waypoint_current = patrol_waypoints.begin(); - con_debug << " " << label() << " patrol " << patrol_waypoints.size() << " " << aux::plural("waypoint", patrol_waypoints.size()) << " " - << nbships << " ship " << aux::plural("type", nbships) << std::endl; + con_debug << " " << label() << " patrol " + << patrol_waypoints.size() << " " << aux::plural("waypoint", patrol_waypoints.size()) << " " + << patrol_npctypes.size() << " npc " << aux::plural("type", patrol_npctypes.size()) << std::endl; } - } Patrol::WayPoint *Patrol::add_waypoint() @@ -209,6 +206,15 @@ Patrol::WayPoint *Patrol::add_waypoint() return waypoint; } +NPCType *Patrol::add_npctype() +{ + NPCType *npctype = new NPCType(); + + patrol_npctypes.push_back(npctype); + + return npctype; +} + void Patrol::add_member(NPC *npc) { npc->set_patrol(this); @@ -279,33 +285,34 @@ void Patrol::frame(const unsigned long elapsed) if (spawn->has_flag(core::Entity::Dockable)) { - // create NPC members for every ship in the patrol's inventory - for (core::Inventory::Items::const_iterator it = inventory()->items().begin(); it != inventory()->items().end(); it++) { - core::Item *item = (*it); - - if (item->info()->type() != ShipModel::infotype()) { + // create NPC members for every NPC type + for (NPCTypes::const_iterator it = patrol_npctypes.begin(); it != patrol_npctypes.end(); ++it) { + + NPCType *npctype = (*it); + + if (!npctype->shipmodel()) { continue; } - // find shipmodel - ShipModel *shipmodel = ShipModel::find(item->info()->label()); - if (!shipmodel) { + if (npctype->amount() <= 0) { continue; } - const size_t nbships = 1 + math::randomi(item->amount()); + const size_t nbships = 1 + math::randomi((unsigned int) npctype->amount()); for (size_t i = 0; i < nbships; i++) { // add NPC - NPC *npc = new NPC(patrol_profile, shipmodel); + NPC *npc = new NPC(patrol_profile, npctype->shipmodel()); // set NPC name - if (shipmodel->npc_name().size()) { - npc->set_name(shipmodel->npc_name()); + if (npctype->name().size()) { + npc->set_name(npctype->name()); } // set NPC color - if (faction()) { + if (npctype->faction()) { + npctype->faction()->apply(npc); + } else if (faction()) { faction()->apply(npc); } @@ -313,13 +320,57 @@ void Patrol::frame(const unsigned long elapsed) if (npc->has_flag(core::Entity::Dockable)) { unset_flag(core::Entity::Dockable); // delete menus - for (Menus::iterator mit = npc->menus().begin(); mit != npc->menus().end(); mit++) { + for (Menus::iterator mit = npc->menus().begin(); mit != npc->menus().end(); ++mit) { delete (*mit); (*mit) = 0; } npc->menus().clear(); } + // install inventory + if (!npc->inventory()) { + npc->add_inventory(); + } + + // install slots + if (!npc->slots()) { + npc->add_slots(); + slots()->load(model()); + } + + // install weapons + for (core::Slots::iterator slit = npc->slots()->begin(); slit != npc->slots()->end(); ++slit) { + core::Slot *slot = (*slit); + + core::Item *item = 0; + + if (slot->type() == model::Weapon::Cannon) { + if (npctype->cannon()) { + item = new core::Item(npctype->cannon()); + } + } else if (slot->type() == model::Weapon::Turret) { + if (npctype->turret()) { + item = new core::Item(npctype->turret()); + } + } + + if (item) { + // add item + item->set_flag(core::Item::Unique); + item->set_flag(core::Item::Mountable); + item->set_flag(core::Item::Unrestricted); + item->set_amount(1); + npc->inventory()->add(item); + + // mount weapon + slot->set_item(item); + slot->set_flag(core::Slot::Active); + slot->set_flag(core::Slot::Mounted); + } + } + + npc->inventory()->recalculate(); + // dock npc at spawn npc->set_zone(spawn->zone()); npc->set_dock(spawn); @@ -362,6 +413,7 @@ void Patrol::frame(const unsigned long elapsed) NPC *member = (*mit); // buy cargo if requested + // FIXME apply NPCType is_merchant if (waypoint()->dock() && waypoint()->cargo() && member->inventory()) { // erase all cargo from inventory |