diff Player.cpp @ 1365:b691c96d8e06

Player::FindFreeInventorySlot and Player::CreateItemInInventory refactoring
author Grumpy7
date Mon, 15 Jul 2013 02:34:52 +0200
parents fd661c9e7f97
children 8f339c3bbdaf
line wrap: on
line diff
--- 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