Example #1
0
bool Player::substractMoneyContainer(Container *container, unsigned long *money)
{
	int goldcoins;
	int platcoins;
	NetworkMessage msg;
	
	for(int i=0; i<container->size();i++)
	{
		Item *item = container->getItem(i);
		
		Container* new_container = dynamic_cast<Container*>(item);
		if(new_container && *money)
		{
			substractMoneyContainer(new_container, money);
		}
		else if(item && *money)
		{
			switch(item->getID())
			{
			case ITEM_COINS_GOLD:
				//gold coins
				msg.Reset();
				goldcoins = item->getItemCountOrSubtype();
				
				for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
					if(cit->second == container) {
						//remove item
						msg.AddByte(0x72);
						msg.AddByte(cit->first);
						msg.AddByte(i);
					}
				}
				
				container->removeItem(item);
				
				if(*money >= goldcoins)
				{
					i--; // If we remove an item from the container then we need substract 1 to the container's main item counter
					*money -= goldcoins;
					//delete item;
					item->releaseThing();
				}
				else
				{
					item->setItemCountOrSubtype(goldcoins - *money);
					*money = 0;
					container->addItem(item);
					for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
						if(cit->second == container) {
							//add item
							msg.AddByte(0x70);
							msg.AddByte(cit->first);
							msg.AddU16(item->getID());
							msg.AddByte(item->getItemCountOrSubtype());
						}
					}
				}
				
				sendNetworkMessage(&msg);
				break;
			case ITEM_COINS_PLATINUM:
				//platinum coins
				msg.Reset();
				goldcoins = item->getItemCountOrSubtype() * 100;
				
				NetworkMessage msg2;
				for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
					if(cit->second == container) {
						//remove item
						msg.AddByte(0x72);
						msg.AddByte(cit->first);
						msg.AddByte(i);
					}
				}
				container->removeItem(item);
				
				if(*money >= goldcoins)
				{
					i--; // If we remove an item from the container then we need substract 1 to the container's main item counter
					*money -= goldcoins;
					//delete item;
					item->releaseThing();
				}
				else
				{
					platcoins = (int)((goldcoins - *money)/100);
					goldcoins = (int)(goldcoins - *money)%100;
					*money = 0;
					
					if(platcoins)
					{
						item->setItemCountOrSubtype(platcoins);
						
						container->addItem(item);
						for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
							if(cit->second == container) {
								//add item
								msg.AddByte(0x70);
								msg.AddByte(cit->first);
								msg.AddU16(item->getID());
								msg.AddByte(item->getItemCountOrSubtype());
							}
						}
						
						if(goldcoins)
						{
							Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins);
							Container *default_container = dynamic_cast<Container*>(getItem(SLOT_BACKPACK));
							
							if(default_container && default_container->addItem(new_item)) // There is space in container
							{
								for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
									if(cit->second == default_container) {
										//add item
										msg.AddByte(0x70);
										msg.AddByte(cit->first);
										msg.AddU16(new_item->getID());
										msg.AddByte(new_item->getItemCountOrSubtype());
									}
								}
							}
							else // There is no space in container
							{
								//TODO: place the item in ground...
								delete new_item;
							}
						}
					}
					else
					{
						if(goldcoins)
						{
							//delete item;
							item->releaseThing();
							Item *new_item = Item::CreateItem(ITEM_COINS_GOLD, goldcoins);
							item = new_item;
							container->addItem(item);
							
							for(containerLayout::const_iterator cit = getContainers(); cit != getEndContainer(); ++cit) {
								if(cit->second == container) {
									//add item
									msg.AddByte(0x70);
									msg.AddByte(cit->first);
									msg.AddU16(item->getID());
									msg.AddByte(item->getItemCountOrSubtype());
								}
							}
						}
						else
						{
							//delete item;
							item->releaseThing();
							item = NULL;
						}
					}
				}
				
				sendNetworkMessage(&msg);
				break;
			}
		}
	}
	
	if(*money == 0)
		return true;
	else
		return false;
}