void CShop::Restore( idRestoreGame *savefile ) { int num; savefile->ReadInt( num ); _itemDefs.SetNum( num ); for( int i = 0; i < num; ++i ) { _itemDefs[i] = CShopItemPtr( new CShopItem ); _itemDefs[i]->Restore( savefile ); } savefile->ReadInt( num ); _itemsForSale.SetNum( num ); for( int i = 0; i < num; ++i ) { _itemsForSale[i] = CShopItemPtr( new CShopItem ); _itemsForSale[i]->Restore( savefile ); } savefile->ReadInt( num ); _itemsPurchased.SetNum( num ); for( int i = 0; i < num; ++i ) { _itemsPurchased[i] = CShopItemPtr( new CShopItem ); _itemsPurchased[i]->Restore( savefile ); } savefile->ReadInt( num ); _startingItems.SetNum( num ); for( int i = 0; i < num; ++i ) { _startingItems[i] = CShopItemPtr( new CShopItem ); _startingItems[i]->Restore( savefile ); } savefile->ReadInt( _gold ); savefile->ReadInt( _forSaleTop ); savefile->ReadInt( _purchasedTop ); savefile->ReadInt( _startingTop ); savefile->ReadBool( _skipShop ); }
void CShop::BuyItem(int index) { CShopItemPtr forSaleItem = _itemsForSale[_forSaleTop + index]; CShopItemPtr boughtItem = FindPurchasedByID(forSaleItem->GetID()); forSaleItem->ChangeCount(-1); ChangeGold(-(forSaleItem->GetCost())); // if the weapon class wasn't in the purchased item list, add it if (boughtItem == NULL) { boughtItem = CShopItemPtr(new CShopItem( *forSaleItem, 0, forSaleItem->GetCost(), forSaleItem->GetPersistent()) ); _itemsPurchased.Append(boughtItem); // scroll so new item is visible in purchased list if (_itemsPurchased.Num() > _purchasedTop + LIST_SIZE_PURCHASED) { _purchasedTop = _itemsPurchased.Num() - LIST_SIZE_PURCHASED; } } boughtItem->ChangeCount(1); }
void CShop::SellItem(int index) { CShopItemPtr boughtItem = _itemsPurchased[_purchasedTop + index]; CShopItemPtr forSaleItem = FindForSaleByID(boughtItem->GetID()); boughtItem->ChangeCount(-1); // If last in the purchased items list, remove it from the list if (boughtItem->GetCount() == 0) { _itemsPurchased.RemoveIndex(_purchasedTop + index); // scroll so appropriate items visible if ((_purchasedTop >= _itemsPurchased.Num()) || (_purchasedTop % LIST_SIZE_PURCHASED != 0)) { _purchasedTop = _itemsPurchased.Num() - LIST_SIZE_PURCHASED; if (_purchasedTop < 0) _purchasedTop = 0; } } ChangeGold(boughtItem->GetCost()); // If the weapon class wasn't in the for sale list (it should be), add it if (forSaleItem == NULL) { forSaleItem = CShopItemPtr(new CShopItem(*boughtItem, 0, boughtItem->GetCost(), boughtItem->GetPersistent())); _itemsForSale.Append(forSaleItem); } forSaleItem->ChangeCount(1); }
CShopItemPtr CShop::FindByID( ShopItemList &items, const char *id ) { for( int i = 0; i < items.Num(); i++ ) { const CShopItemPtr &item = items[i]; if( item != NULL && idStr::Icmp( item->GetID(), id ) == 0 ) { return item; } } return CShopItemPtr(); }
CShopItemPtr CShop::FindShopItemDefByClassName( const idStr &className ) { CShopItemPtr found = FindByID( _itemDefs, className ); if( found != NULL ) { return found; } // Check if we should run another search if( idStr::Cmpn( className.c_str(), "atdm:", 5 ) == 0 ) { return CShopItemPtr(); // atdm is already prepended, return empty } // Try again with "atdm:" prepended return FindByID( _itemDefs, "atdm:" + className ); }