bool CExchange::CheckSpace()
{
static CGrid s_grid1(5, INVENTORY_MAX_NUM / 5 / 4); // inven page 1 9 Rows a 5 Columns
static CGrid s_grid2(5, INVENTORY_MAX_NUM / 5 / 4); // inven page 2 9 Rows a 5 Columns
static CGrid s_grid3(5, INVENTORY_MAX_NUM / 5 / 4); // inven page 3 9 Rows a 5 Columns
static CGrid s_grid4(5, INVENTORY_MAX_NUM / 5 / 4); // inven page 4 9 Rows a 5 Columns
#ifdef WJ_SPLIT_INVENTORY_SYSTEM
static CGrid s_grid5(5, SKILL_BOOK_INVENTORY_MAX_NUM / 5 / 3);
static CGrid s_grid6(5, UPGRADE_ITEMS_INVENTORY_MAX_NUM / 5 / 3);
static CGrid s_grid7(5, STONE_INVENTORY_MAX_NUM / 5 / 3);
#endif
s_grid1.Clear();
s_grid2.Clear();
s_grid3.Clear();
s_grid4.Clear();
#ifdef WJ_SPLIT_INVENTORY_SYSTEM
s_grid5.Clear();
s_grid6.Clear();
s_grid7.Clear();
#endif
LPCHARACTER victim = GetCompany()->GetOwner();
LPITEM item;
int i;
const int perPageSlotCount = INVENTORY_MAX_NUM / 4;
for (i = 0; i < INVENTORY_MAX_NUM; ++i) {
if (!(item = victim->GetInventoryItem(i)))
continue;
#ifdef NEW_ADD_INVENTORY
int envanterblack;
if (item->IsDragonSoul())
envanterblack = victim->GetEmptyDragonSoulInventory(item);
else
envanterblack = victim->GetEmptyInventory(item->GetSize());
if (envanterblack < 0)
return false;
#endif
BYTE itemSize = item->GetSize();
if (i < perPageSlotCount) // Notice: This is adjusted for 4 Pages only!
s_grid1.Put(i, 1, itemSize);
else if (i < perPageSlotCount * 2)
s_grid2.Put(i - perPageSlotCount, 1, itemSize);
else if (i < perPageSlotCount * 3)
s_grid3.Put(i - perPageSlotCount * 2, 1, itemSize);
else
s_grid4.Put(i - perPageSlotCount * 3, 1, itemSize);
}
#ifdef WJ_SPLIT_INVENTORY_SYSTEM
int x;
int y;
const int perPageSkillBookSlotCount = SKILL_BOOK_INVENTORY_MAX_NUM / 1;
const int perPageUpgradeItemsSlotCount = UPGRADE_ITEMS_INVENTORY_MAX_NUM / 1;
const int perPageStoneSlotCount = STONE_INVENTORY_MAX_NUM / 1;
for (x = 0; x < SKILL_BOOK_INVENTORY_MAX_NUM; ++x) {
if (!(item = victim->GetSkillBookInventoryItem(x)))
continue;
BYTE itemSize = item->GetSize();
if (x < perPageSkillBookSlotCount)
s_grid5.Put(x, 1, itemSize);
}
for (y = 0; y < UPGRADE_ITEMS_INVENTORY_MAX_NUM; ++y) {
if (!(item = victim->GetUpgradeItemsInventoryItem(y)))
continue;
BYTE itemSize = item->GetSize();
if (y < perPageUpgradeItemsSlotCount)
s_grid6.Put(y, 1, itemSize);
}
for (y = 0; y < STONE_INVENTORY_MAX_NUM; ++y) {
if (!(item = victim->GetStoneInventoryItem(y)))
continue;
BYTE itemSize = item->GetSize();
if (y < perPageStoneSlotCount)
s_grid7.Put(y, 1, itemSize);
}
#endif
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;
}
#ifdef WJ_SPLIT_INVENTORY_SYSTEM
else if (item->IsSkillBook())
{
int iPos = s_grid5.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid5.Put(iPos, 1, itemSize);
continue;
}
return false;
}
else if (item->IsUpgradeItem())
{
int iPos = s_grid6.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid6.Put(iPos, 1, itemSize);
continue;
}
return false;
}
else if (item->IsStone())
{
int iPos = s_grid7.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid7.Put(iPos, 1, itemSize);
continue;
}
return false;
}
#endif
else
{
int iPos = s_grid1.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid1.Put(iPos, 1, itemSize);
continue;
}
iPos = s_grid2.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid2.Put(iPos, 1, itemSize);
continue;
}
iPos = s_grid3.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid3.Put(iPos, 1, itemSize);
continue;
}
iPos = s_grid4.FindBlank(1, itemSize);
if (iPos >= 0) {
s_grid4.Put(iPos, 1, itemSize);
continue;
}
return false; // No space left in inventory
}
}
return true;
}