# HG changeset patch # User Grumpy7 # Date 1373848492 -7200 # Node ID b691c96d8e06e3b0b4e58f1b11cef0fa3d29fb8a # Parent 9014784ae84fb230a9a0c3ba88862eb6e2f209ff Player::FindFreeInventorySlot and Player::CreateItemInInventory refactoring diff -r 9014784ae84f -r b691c96d8e06 Player.cpp --- a/Player.cpp Mon Jul 15 02:14:39 2013 +0200 +++ b/Player.cpp Mon Jul 15 02:34:52 2013 +0200 @@ -823,10 +823,10 @@ if ( (slotWidth + uSlot % INVETORYSLOTSWIDTH) <= INVETORYSLOTSWIDTH && (slotHeight + uSlot / INVETORYSLOTSWIDTH) <= INVETORYSLOTSHEIGHT ) { int startOfInnerLoop = uSlot; - for (int y = 0; y < slotHeight; y++) + for (unsigned int y = 0; y < slotHeight; y++) { int innerLoopPos = startOfInnerLoop; - for (int x = 0; x < slotWidth; x++) + for (unsigned int x = 0; x < slotWidth; x++) { if (pInventoryIndices[innerLoopPos] != 0) { @@ -843,85 +843,57 @@ // 506128: using guessed type int areWeLoadingTexture; //----- (004925E6) -------------------------------------------------------- -unsigned int Player::FindFreeInventorySlot() -{ - unsigned int result; // eax@1 - ItemGen *v2; // ecx@1 - - result = 0; - v2 = this->pInventoryItems; - while ( v2->uItemID ) - { - ++result; - ++v2; - if ( (signed int)result >= 126 ) - return -1; - } - return result; +int Player::FindFreeInventorySlot() +{ + for (int i = 0; i < 126; i++ ) + { + if (pInventoryItems[i].uItemID == 0) + { + return i; + } + } + return -1; } //----- (00492600) -------------------------------------------------------- int Player::CreateItemInInventory(unsigned int uSlot, unsigned int uItemID) { - signed int v3; // edx@1 - ItemGen *v4; // eax@1 int result; // eax@8 - unsigned int v6; // ebx@10 - //unsigned int v7; // eax@10 - Texture *v8; // esi@10 - void *v9; // esi@13 - unsigned int v10; // [sp+0h] [bp-Ch]@10 - Player *v11; // [sp+4h] [bp-8h]@1 - signed int v12; // [sp+8h] [bp-4h]@4 - unsigned int uItemIDa; // [sp+18h] [bp+Ch]@10 - - v11 = this; - v3 = 0; - v4 = this->pInventoryItems; - while ( v4->uItemID ) - { - ++v3; - ++v4; - if ( v3 >= 126 ) - { - v12 = -1; - goto LABEL_5; - } - } - v12 = v3; -LABEL_5: - if ( v12 == -1 ) + Texture *texturePtr; // esi@10 + int *v9; // esi@13 + unsigned int widthInSlots; // [sp+0h] [bp-Ch]@10 + signed int freeSlot; // [sp+8h] [bp-4h]@4 + unsigned int heightInSlots; // [sp+18h] [bp+Ch]@10 + + freeSlot = FindFreeInventorySlot(); + if ( freeSlot == -1 ) { if ( uActiveCharacter ) pPlayers[uActiveCharacter]->PlaySound(SPEECH_NoRoom, 0); - result = 0; + result = -1; } else { - v6 = uItemID; - v8 = pIcons_LOD->LoadTexturePtr(pItemsTable->pItems[uItemID].pIconName, TEXTURE_16BIT_PALETTE); - v10 = GetSizeInInventorySlots(v8->uTextureWidth); - uItemIDa = GetSizeInInventorySlots(v8->uTextureHeight); + texturePtr = pIcons_LOD->LoadTexturePtr(pItemsTable->pItems[uItemID].pIconName, TEXTURE_16BIT_PALETTE); + widthInSlots = GetSizeInInventorySlots(texturePtr->uTextureWidth); + heightInSlots = GetSizeInInventorySlots(texturePtr->uTextureHeight); if ( !areWeLoadingTexture ) { - v8->Release(); + texturePtr->Release(); pIcons_LOD->SyncLoadedFilesCount(); } - if ( (signed int)uItemIDa > 0 ) - { - v9 = &v11->pInventoryIndices[uSlot]; - do - { - if ( (signed int)v10 > 0 ) - memset32(v9, -1 - uSlot, v10); - v9 = (char *)v9 + 56; - --uItemIDa; - } - while ( uItemIDa ); - } - result = v12 + 1; - v11->pInventoryIndices[uSlot] = v12 + 1; - v11->pInventoryItems[v12].uItemID = v6; + if (widthInSlots > 0) + { + v9 = &this->pInventoryIndices[uSlot]; + for (unsigned int i = 0; i < heightInSlots; i++) + { + memset32(v9, -1 - uSlot, widthInSlots); //TODO: try to come up with a better solution. negative values are used when drawing the inventory - nothing is drawn + v9 += INVETORYSLOTSWIDTH; + } + } + result = freeSlot + 1; + this->pInventoryIndices[uSlot] = freeSlot + 1; + this->pInventoryItems[freeSlot].uItemID = uItemID; } return result; } @@ -8895,7 +8867,7 @@ int v4; // edx@1 int v5; // esi@2 //int v6; // eax@2 - unsigned int v7; // eax@3 + int v7; // eax@3 ItemGen _this; // [sp+Ch] [bp-30h]@1 //Player *v9; // [sp+30h] [bp-Ch]@1 int v10; // [sp+34h] [bp-8h]@1 diff -r 9014784ae84f -r b691c96d8e06 Player.h --- a/Player.h Mon Jul 15 02:14:39 2013 +0200 +++ b/Player.h Mon Jul 15 02:34:52 2013 +0200 @@ -540,7 +540,7 @@ int GetDisarmTrap(); char GetLearningPercent(); bool CanFitItem(unsigned int uSlot, unsigned int uItemID); - unsigned int FindFreeInventorySlot(); + int FindFreeInventorySlot(); int CreateItemInInventory(unsigned int uSlot, unsigned int uItemID); int HasSkill(unsigned int uSkillType); int WearItem(unsigned int uItemID); diff -r 9014784ae84f -r b691c96d8e06 UI/UICharacter.cpp --- a/UI/UICharacter.cpp Mon Jul 15 02:14:39 2013 +0200 +++ b/UI/UICharacter.cpp Mon Jul 15 02:34:52 2013 +0200 @@ -2640,7 +2640,7 @@ unsigned __int16 v5; // ax@7 unsigned int v6; // edi@19 int v7; // esi@27 - unsigned int v8; // eax@29 + int v8; // eax@29 int v9; // edx@32 int v10; // esi@34 int v11; // eax@34 @@ -2655,10 +2655,10 @@ int v20; // esi@60 int v21; // eax@60 unsigned int v22; // eax@61 - unsigned int v23; // eax@62 + int v23; // eax@62 int v24; // esi@65 int v25; // eax@65 - unsigned int v26; // eax@69 + int v26; // eax@69 int v27; // esi@81 int v28; // eax@81 int v29; // esi@84 @@ -2677,7 +2677,7 @@ unsigned int v49; // [sp+34h] [bp-18h]@57 unsigned int v50; // [sp+38h] [bp-14h]@50 int v51; // [sp+3Ch] [bp-10h]@1 - unsigned int v52; // [sp+40h] [bp-Ch]@5 + int v52; // [sp+40h] [bp-Ch]@5 //int v53; // [sp+44h] [bp-8h]@1 //unsigned int v54; // [sp+48h] [bp-4h]@1 @@ -2772,7 +2772,7 @@ if ( !*(int *)v7 ) { v8 = pPlayers[uActiveCharacter]->FindFreeInventorySlot(); - if ( (v8 & 0x80000000u) == 0 ) + if ( v8 >= 0 ) { v9 = v52; pParty->pPickedItem.uBodyAnchor = v52 + 1; @@ -2831,7 +2831,7 @@ else { v52 = pPlayers[uActiveCharacter]->FindFreeInventorySlot(); - if ( (v52 & 0x80000000u) != 0 ) + if ( v52 < 0 ) return; if ( !v51 ) { @@ -2906,7 +2906,7 @@ break; } v23 = pPlayers[uActiveCharacter]->FindFreeInventorySlot(); - if ( (v23 & 0x80000000u) != 0 ) + if ( v23 < 0 ) return; pParty->pPickedItem.uBodyAnchor = 1; v50 = (unsigned int)&pPlayers[uActiveCharacter]->pInventoryItems[v23]; @@ -2929,7 +2929,7 @@ if ( !v1 ) { v26 = pPlayers[uActiveCharacter]->FindFreeInventorySlot(); - if ( (v26 & 0x80000000u) != 0 ) + if ( v26 < 0 ) return; pParty->pPickedItem.uBodyAnchor = 2; v50 = (unsigned int)&pPlayers[uActiveCharacter]->pInventoryItems[v26]; @@ -3000,7 +3000,7 @@ else { v52 = pPlayers[uActiveCharacter]->FindFreeInventorySlot(); - if ( (v52 & 0x80000000u) == 0 ) + if ( v52 >= 0 ) { if ( v2 ) {