//------------------------------------------------------------------------------ // Set slot functions //------------------------------------------------------------------------------ bool StoresMgr::setSlotStores(const Basic::PairStream* const msg) { // First let our base class do everything that it needs to. BaseClass::setSlotStores(msg); // --- // Clear all previous stores and assigned weapons // --- weaponsList = 0; externalList = 0; fuelList = 0; gunPtr = 0; // --- // Use the stores list that the Stores class just processed. Basic::PairStream* stores = getStores(); if (stores != 0){ // Create the new weapons list that contains all weapons { Basic::PairStream* newWeapons = new Basic::PairStream(); searchAndAdd(stores, typeid(Weapon), newWeapons); if (newWeapons->entries() > 0) weaponsList = newWeapons; newWeapons->unref(); } // Create the new external stores list that contains all // non-weapon, external stores (e.g., fuel tanks, pods, guns) { Basic::PairStream* newExternal = new Basic::PairStream(); searchAndAdd(stores, typeid(ExternalStore), newExternal); if (newExternal->entries() > 0) externalList = newExternal; newExternal->unref(); } // Create the new fuel tank list that contains all fuel tanks { Basic::PairStream* newFuel = new Basic::PairStream(); searchAndAdd(stores, typeid(FuelTank), newFuel); if (newFuel->entries() > 0) fuelList = newFuel; newFuel->unref(); } // Find the primary gun; i.e., the first gun found on our stores Basic::List::Item* item = stores->getFirstItem(); while (item != 0 && gunPtr == 0) { Basic::Pair* pair = (Basic::Pair*)(item->getValue()); Gun* p = dynamic_cast<Gun*>( pair->object() ); if (p != 0) gunPtr = p; item = item->getNext(); } stores->unref(); stores = 0; } return true; }
// Set the stores bool Stores::setSlotStores(const Basic::PairStream* const msg) { // --- // Quick out if the number of stations hasn't been set. // --- if (ns == 0 && msg != 0) { std::cerr << "Stores::setSlotStation() Number of stations is not set!" << std::endl; return false; } // --- // Clear the previous stores and assigned weapons // --- storesList = 0; for (unsigned int s = 1; s <= ns; s++) { assignWeaponToStation(s,0); assignExtStoreToStation(s,0); } numWpn = 0; numEs = 0; // --- // Quick out if 'msg' is zero // --- if (msg == 0) return true; bool ok = true; // --- // Create the new external stores list // // For all items in the 'msg' list ... // -- Make sure that it's a weapon or other type of external store, and // that it has a valid station number. // -- Clone the store and if it's a weapon then assign it to the station. // --- Basic::PairStream* newStores = new Basic::PairStream(); const Basic::List::Item* item = msg->getFirstItem(); while (item != 0) { const Basic::Pair* pair = static_cast<const Basic::Pair*>(item->getValue()); const Basic::Component* p = static_cast<const Basic::Component*>(pair->object()); if (p != 0) { // get the station number from the stores' slot name int stationNumber = 0; const Basic::Identifier* stationName = pair->slot(); if (stationName->isInteger()) { stationNumber = stationName->getInteger(); } if (stationNumber > 0 && stationNumber <= static_cast<int>(ns)) { // check the type of component bool isWpn = p->isClassType(typeid(Weapon)); bool isEE = p->isClassType(typeid(ExternalStore)); if ( isWpn || isEE ) { // Clone the weapon pair and set us as its container Basic::Pair* cpair = pair->clone(); Component* cp = static_cast<Component*>(cpair->object()); cp->container(this); if ( isWpn ) { // Weapon types ... // Assign the weapon to the station Weapon* cwpn = static_cast<Weapon*>( cpair->object() ); assignWeaponToStation(stationNumber, cwpn); } if ( isEE ) { // External stores types ... // Assign the external store to the station ExternalStore* cwpn = static_cast<ExternalStore*>( cpair->object() ); assignExtStoreToStation(stationNumber, cwpn); } if (cpair != 0) { // Add to the new stores list newStores->put(cpair); cpair->unref(); // the new list has it. } } else { std::cerr << "Stores::setSlotStores(): invalid external stores type; use Weapon or Stores classes" << std::endl; ok = false; } } else { std::cerr << "Stores::setSlotStores(): invalid station number from the store's slot name." << std::endl; ok = false; } } item = item->getNext(); } // Make the new stores list the active list if (ok && newStores->entries() > 0) { storesList = newStores; } else { for (unsigned int s = 1; s <= ns; s++) { assignWeaponToStation(s,0); } numWpn = 0; } newStores->unref(); return ok; }