Browse Source

Use unsigned types for Items array indexes consistently

Actual type used varied based on context, this addresses some type conversion warnings.
pull/3586/head
ephphatha 5 years ago committed by Anders Jenbo
parent
commit
55a52b82c2
  1. 10
      Source/items.cpp
  2. 6
      Source/items.h
  3. 14
      Source/loadsave.cpp

10
Source/items.cpp

@ -40,15 +40,15 @@ namespace devilution {
/** Contains the items on ground in the current game. */
Item Items[MAXITEMS + 1];
int ActiveItems[MAXITEMS];
int ActiveItemCount;
uint8_t ActiveItems[MAXITEMS];
uint8_t ActiveItemCount;
/**
* @brief Contains indexes of empty spaces in Items.
*
* This array effectively duplicates ActiveItems due to differences in implementation to other fixed buffers.
* Eventually this can be removed and Items can be treated the same as how Missiles are tracked
*/
int AvailableItems[MAXITEMS];
uint8_t AvailableItems[MAXITEMS];
bool ShowUniqueItemInfoBox;
CornerStoneStruct CornerStone;
bool UniqueItemFlags[128];
@ -2597,9 +2597,9 @@ void InitItems()
item._iPostDraw = false;
}
for (int i = 0; i < MAXITEMS; i++) {
for (uint8_t i = 0; i < MAXITEMS; i++) {
ActiveItems[i] = i;
AvailableItems[i] = i;
ActiveItems[i] = 0;
}
if (!setlevel) {

6
Source/items.h

@ -414,9 +414,9 @@ struct CornerStoneStruct {
struct Player;
extern Item Items[MAXITEMS + 1];
extern int ActiveItems[MAXITEMS];
extern int ActiveItemCount;
extern int AvailableItems[MAXITEMS];
extern uint8_t ActiveItems[MAXITEMS];
extern uint8_t ActiveItemCount;
extern uint8_t AvailableItems[MAXITEMS];
extern bool ShowUniqueItemInfoBox;
extern CornerStoneStruct CornerStone;
extern bool UniqueItemFlags[128];

14
Source/loadsave.cpp

@ -900,7 +900,7 @@ std::unordered_map<uint8_t, uint8_t> LoadDroppedItems(LoadHelper &file)
}
file.Skip(MAXITEMS * 2 - static_cast<size_t>(ActiveItemCount)); // Skip loading the rest of ActiveItems and AvailableItems, the indices are initialised below based on the number of active items
for (int i = 0; i < MAXITEMS; i++) {
for (uint8_t i = 0; i < MAXITEMS; i++) {
if (i < ActiveItemCount)
LoadItem(file, Items[i]);
@ -1512,13 +1512,13 @@ void SavePortal(SaveHelper *file, int i)
std::unordered_map<uint8_t, uint8_t> SaveDroppedItems(SaveHelper &file)
{
// Vanilla Diablo/Hellfire initialise the ActiveItems and AvailableItems arrays based on saved data, so write valid values for compatibility
for (int i = 0; i < MAXITEMS; i++)
file.WriteLE<int8_t>(i); // Strictly speaking everything from ActiveItemCount onwards is unused but no harm writing non-zero values here.
for (int i = 0; i < MAXITEMS; i++)
file.WriteLE<int8_t>((i + ActiveItemCount) % MAXITEMS);
for (uint8_t i = 0; i < MAXITEMS; i++)
file.WriteLE<uint8_t>(i); // Strictly speaking everything from ActiveItemCount onwards is unused but no harm writing non-zero values here.
for (uint8_t i = 0; i < MAXITEMS; i++)
file.WriteLE<uint8_t>((i + ActiveItemCount) % MAXITEMS);
std::unordered_map<uint8_t, uint8_t> itemIndexes = { { 0, 0 } };
for (int i = 0; i < ActiveItemCount; i++) {
for (uint8_t i = 0; i < ActiveItemCount; i++) {
itemIndexes[ActiveItems[i] + 1] = i + 1;
SaveItem(file, Items[ActiveItems[i]]);
}
@ -1534,7 +1534,7 @@ void SaveDroppedItemLocations(SaveHelper &file, const std::unordered_map<uint8_t
{
for (int j = 0; j < MAXDUNY; j++) {
for (int i = 0; i < MAXDUNX; i++) // NOLINT(modernize-loop-convert)
file.WriteLE<int8_t>(itemIndexes.at(dItem[i][j]));
file.WriteLE<uint8_t>(itemIndexes.at(dItem[i][j]));
}
}

Loading…
Cancel
Save