Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Prevent item deletion when dropping items #7006

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 0 additions & 36 deletions Source/controls/plrctrls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1929,42 +1929,6 @@ void UpdateSpellTarget(SpellID spell)
cursPosition = myPlayer.position.future + Displacement(myPlayer._pdir) * range;
}

/**
* @brief Try dropping item in all 9 possible places
*/
bool TryDropItem()
{
Player &myPlayer = *MyPlayer;

if (myPlayer.HoldItem.isEmpty()) {
return false;
}

if (leveltype == DTYPE_TOWN) {
if (UseItemOpensHive(myPlayer.HoldItem, myPlayer.position.tile)) {
OpenHive();
NewCursor(CURSOR_HAND);
return true;
}
if (UseItemOpensGrave(myPlayer.HoldItem, myPlayer.position.tile)) {
OpenGrave();
NewCursor(CURSOR_HAND);
return true;
}
}

std::optional<Point> itemTile = FindAdjacentPositionForItem(myPlayer.position.future, myPlayer._pdir);
if (!itemTile) {
myPlayer.Say(HeroSpeech::WhereWouldIPutThis);
return false;
}

NetSendCmdPItem(true, CMD_PUTITEM, *itemTile, myPlayer.HoldItem);
myPlayer.HoldItem.clear();
NewCursor(CURSOR_HAND);
return true;
}

void PerformSpellAction()
{
if (InGameMenu() || QuestLogIsOpen || sbookflag)
Expand Down
1 change: 0 additions & 1 deletion Source/controls/plrctrls.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ void PerformPrimaryAction();
// Open chests, doors, pickup items.
void PerformSecondaryAction();
void UpdateSpellTarget(SpellID spell);
bool TryDropItem();
void InvalidateInventorySlot();
void FocusOnInventory();
void PerformSpellAction();
Expand Down
80 changes: 55 additions & 25 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,60 @@ extern void plrctrls_after_check_curs_move();
extern void plrctrls_every_frame();
extern void plrctrls_after_game_logic();

bool TryOpenDungeon(bool useCursorPosition)
{
if (leveltype != DTYPE_TOWN)
return false;

Item &holdItem = MyPlayer->HoldItem;
WorldTilePosition &position = MyPlayer->position.tile;

// Common function to attempt opening based on input method.
auto attemptOpen = [&]() -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What need does the arrow function serve, it seems it should work the same with the code directly instead.

if ((useCursorPosition && holdItem.IDidx == IDI_RUNEBOMB && OpensHive(cursPosition)) || (!useCursorPosition && UseItemOpensHive(holdItem, position))) {
OpenHive();
NewCursor(CURSOR_HAND);
return true;
}
if ((useCursorPosition && holdItem.IDidx == IDI_MAPOFDOOM && OpensGrave(cursPosition)) || (!useCursorPosition && UseItemOpensGrave(holdItem, position))) {
OpenGrave();
NewCursor(CURSOR_HAND);
return true;
}
return false;
};

return attemptOpen();
}

/**
* @brief Try dropping item in all 9 possible places
*/
bool TryDropItem(bool useCursorPosition /*= false*/)
{
Player &myPlayer = *MyPlayer;

if (myPlayer.HoldItem.isEmpty()) {
return false;
}

if (TryOpenDungeon(useCursorPosition))
return true;

Direction dir = useCursorPosition ? GetDirection(myPlayer.position.tile, cursPosition) : myPlayer._pdir;
std::optional<Point> itemTile = FindAdjacentPositionForItem(myPlayer.position.tile, dir);

if (!itemTile) {
Copy link
Member

@AJenbo AJenbo Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets call it freeTile, avalibleTile or something like that instead.

myPlayer.Say(HeroSpeech::WhereWouldIPutThis);
return false;
}

NetSendCmdPItem(true, CMD_PUTITEM, *itemTile, myPlayer.HoldItem);
myPlayer.HoldItem.clear();
NewCursor(CURSOR_HAND);
return true;
}

namespace {

char gszVersionNumber[64] = "internal version unknown";
Expand Down Expand Up @@ -302,23 +356,6 @@ void LeftMouseCmd(bool bShift)
}
}

bool TryOpenDungeonWithMouse()
{
if (leveltype != DTYPE_TOWN)
return false;

Item &holdItem = MyPlayer->HoldItem;
if (holdItem.IDidx == IDI_RUNEBOMB && OpensHive(cursPosition))
OpenHive();
else if (holdItem.IDidx == IDI_MAPOFDOOM && OpensGrave(cursPosition))
OpenGrave();
else
return false;

NewCursor(CURSOR_HAND);
return true;
}

void LeftMouseDown(uint16_t modState)
{
LastMouseButtonAction = MouseActionType::None;
Expand Down Expand Up @@ -377,14 +414,7 @@ void LeftMouseDown(uint16_t modState)
} else if (sbookflag && GetRightPanel().contains(MousePosition)) {
CheckSBook();
} else if (!MyPlayer->HoldItem.isEmpty()) {
if (!TryOpenDungeonWithMouse()) {
Point currentPosition = MyPlayer->position.tile;
std::optional<Point> itemTile = FindAdjacentPositionForItem(currentPosition, GetDirection(currentPosition, cursPosition));
if (itemTile) {
NetSendCmdPItem(true, CMD_PUTITEM, *itemTile, MyPlayer->HoldItem);
NewCursor(CURSOR_HAND);
}
}
TryDropItem(true);
} else {
CheckLvlBtn();
if (!lvlbtndown)
Expand Down
1 change: 1 addition & 0 deletions Source/diablo.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void DisableInputEventHandler(const SDL_Event &event, uint16_t modState);
void LoadGameLevel(bool firstflag, lvl_entry lvldir);
bool IsDiabloAlive(bool playSFX);
void PrintScreen(SDL_Keycode vkey);
bool TryDropItem(bool useCursorPosition = false);

/**
* @param bStartup Process additional ticks before returning
Expand Down
9 changes: 4 additions & 5 deletions Source/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,12 +1356,11 @@ size_t OnPutItem(const TCmd *pCmd, Player &player)
pfile_update(true);
}
return sizeof(message);
} else {
PutItemRecord(dwSeed, wCI, wIndx);
DeltaPutItem(message, position, player);
if (isSelf)
pfile_update(true);
}
PutItemRecord(dwSeed, wCI, wIndx);
DeltaPutItem(message, position, player);
if (isSelf)
pfile_update(true);
}

return sizeof(message);
Expand Down