diff --git a/Source/inv.cpp b/Source/inv.cpp index 836c72c75..042d30dc3 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -1130,18 +1130,11 @@ bool PutItem(Player &player, Point &position) if (CanPut(position)) return true; - for (int l = 1; l < 50; l++) { - for (int j = -l; j <= l; j++) { - int yp = j + player.position.tile.y; - for (int i = -l; i <= l; i++) { - int xp = i + player.position.tile.x; - if (!CanPut({ xp, yp })) - continue; - - position = { xp, yp }; - return true; - } - } + std::optional itemPosition = FindClosestValidPosition(CanPut, player.position.tile, 1, 50); + + if (itemPosition) { + position = *itemPosition; + return true; } return false; diff --git a/Source/items.cpp b/Source/items.cpp index 2bb363746..9917a66b4 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -3229,19 +3229,9 @@ int AllocateItem() Point GetSuperItemLoc(Point position) { - for (int k = 1; k < 50; k++) { - for (int j = -k; j <= k; j++) { - for (int i = -k; i <= k; i++) { - Displacement offset = { i, j }; - Point positionToCheck = position + offset; - if (ItemSpaceOk(positionToCheck)) { - return positionToCheck; - } - } - } - } + std::optional itemPosition = FindClosestValidPosition(ItemSpaceOk, position, 1, 50); - return { 0, 0 }; // TODO handle no space for dropping items + return itemPosition.value_or(Point { 0, 0 }); // TODO handle no space for dropping items } void GetItemAttrs(Item &item, int itemData, int lvl)