Jump to content
×
×
  • Create New...

Dynamic CExchange::CheckSpace Function


Recommended Posts

I wrote this function after being asked to Q.Q
bool CExchange::CheckSpace()
{
	
	std::vector<CGrid> s_grids;
	for (int pCount = 0; pCount < INVENTORY_PAGE_COUNT; ++pCount)
	{
		s_grids.push_back(CGrid(5, 9));
		s_grids[pCount].Clear();
	}


	LPCHARACTER victim = GetCompany()->GetOwner();
	LPITEM item;

	int i;

	for (i = 0; i < INVENTORY_MAX_NUM; ++i) {
		if (!(item = victim->GetInventoryItem(i)))
			continue;

		BYTE itemSize = item->GetSize();
		BYTE bPage = i / (INVENTORY_PAGE_SIZE) - 1;

		s_grids[bPage].Put(i - (INVENTORY_PAGE_SIZE * bPage), 1, itemSize);
	}

	static std::vector <WORD> s_vDSGrid(DRAGON_SOUL_INVENTORY_MAX_NUM);

	bool bDSInitialized = false;

	for (i = 0; i < EXCHANGE_ITEM_MAX_NUM; ++i)
	{
		if (!(item = m_apItems[i]))
			continue;

		BYTE itemSize = item->GetSize();

		if (item->IsDragonSoul())
		{
			if (!victim->DragonSoul_IsQualified())
				return false;

			if (!bDSInitialized) {
				bDSInitialized = true;
				victim->CopyDragonSoulItemGrid(s_vDSGrid);
			}

			bool bExistEmptySpace = false;
			WORD wBasePos = DSManager::instance().GetBasePosition(item);
			if (wBasePos >= DRAGON_SOUL_INVENTORY_MAX_NUM)
				return false;

			for (int i = 0; i < DRAGON_SOUL_BOX_SIZE; i++)
			{
				WORD wPos = wBasePos + i;
				if (0 == s_vDSGrid[wBasePos])
				{
					bool bEmpty = true;
					for (int j = 1; j < item->GetSize(); j++)
					{
						if (s_vDSGrid[wPos + j * DRAGON_SOUL_BOX_COLUMN_NUM])
						{
							bEmpty = false;
							break;
						}
					}
					if (bEmpty)
					{
						for (int j = 0; j < item->GetSize(); j++)
						{
							s_vDSGrid[wPos + j * DRAGON_SOUL_BOX_COLUMN_NUM] = wPos + 1;
						}
						bExistEmptySpace = true;
						break;
					}
				}
				if (bExistEmptySpace)
					break;
			}
			if (!bExistEmptySpace)
				return false;
		}
		else
		{
			bool bFound;
			for (int pCount = 0; pCount < INVENTORY_PAGE_SIZE; ++pCount)
			{
				int iPos = s_grids[pCount].FindBlank(1, itemSize);
				if (iPos >= 0) {
					s_grids[pCount].Put(iPos, 1, itemSize);
					bFound = true;
					break;
				}
			}
			if (bFound)
				continue;

			return false;  // No space left in inventory
		}
	}

	return true;
}

Credits to Martin and Think and all the other Persons who worked on the other fix I used as base.

 

This is untested oh and there is a change in common/length.h

	INVENTORY_PAGE_SIZE = 45,
	INVENTORY_PAGE_COUNT = 3,
	INVENTORY_MAX_NUM		= INVENTORY_PAGE_SIZE * INVENTORY_PAGE_COUNT,

Basically you can replace 

INVENTORY_MAX_NUM / 2 with INVENTORY_PAGE_SIZE in all files

 

and if you want to add or remove inventory pages just change INVENTORY_PAGE_COUNT

 

And to repeat it this is as for now untested I actually test it in some minutes.

 

Edit: Basically this is the same as o.o

static CGrid s_grid1(5 * INVENTORY_PAGE_COUNT, 9);

  • Love 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.