-
Notifications
You must be signed in to change notification settings - Fork 802
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
Splitting CheckInvPaste() into smaller functions #6984
Splitting CheckInvPaste() into smaller functions #6984
Conversation
I am struggling a little bit to debug the
I can't find |
Are you debugging with Visual Studio? EDIT: If so, try this... Here's the text for that second image. {
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "devilutionx.exe",
"name": "devilutionx.exe",
"args": []
},
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "devilutionx.exe",
"name": "timedemo",
"args": [
"--config-dir",
"${cmake.buildRoot}\\test\\fixtures\\timedemo\\WarriorLevel1to2",
"--save-dir",
"${cmake.buildRoot}\\test\\fixtures\\timedemo\\WarriorLevel1to2",
"--diablo",
"--spawn",
"--lang",
"en",
"--demo",
"0",
"--timedemo"
]
}
]
} And you may have to select |
Thanks for the reply, @StephenCWills . I am running on VsCode with the MSVC compilers on Windows, and the clang compilers on Ubuntu. I figured what the issue I have is: it's simply that we get the {
"name": "Timedemo Debug",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\build\\vs2022-deb\\Debug\\timedemo_test.exe",
"cwd": "${workspaceFolder}\\build\\vs2022-deb\\",
"args": [
"--config-dir",
"C:\\Users\\mathe\\development\\devilutionX\\build\\vs2022-deb\\test\\fixtures\\timedemo\\WarriorLevel1to2",
"--save-dir",
"C:\\Users\\mathe\\development\\devilutionX\\build\\vs2022-deb\\test\\fixtures\\timedemo\\WarriorLevel1to2",
"--diablo",
"--spawn",
"--lang",
"en",
"--demo",
"0",
"--timedemo"
],
"stopAtEntry": false,
"environment": [],
"console": "integratedTerminal"
}
In my case, I endup with the I do wonder: should we not use the value of the current working directory as the base path, instead of the path to the binary? Either way, I haven't fixed it yet, and the solution seems to just run the test through the test explorer and let |
Since we don't use devilutionX/test/CMakeLists.txt Lines 51 to 52 in bc4f434
That said, it seems the https://cmake.org/cmake/help/latest/module/GoogleTest.html#command:gtest_discover_tests
We do use devilutionX/test/Fixtures.cmake Line 99 in bc4f434
devilutionX/test/Fixtures.cmake Lines 1 to 3 in bc4f434
That path should match the one However, I'm not really that familiar with ctest. It looks to me like CMake is generating a bunch of files in the format Anyway, if you want to give it a shot, here's where we set pref path up to use relative paths in devilutionX/test/writehero_test.cpp Line 372 in bc4f434
|
8bd1af1
to
128c288
Compare
If you look at the source code for I kind of gave up trying to fix the prefix paths outside the ctest integration and just hardcoded the correct Either way, I think I fixed the tests... turns out I removed one too many if checks on item locations. |
Moved functions that were getting previous items into smaller ones with better names. At least you can see straight-away what each of these blocks of code do!
128c288
to
107ec7d
Compare
Looks good over all, this is a truly massive function so hard to tackle. It's fine if you do it in steps and just extract some of the obvious bits first. |
Thanks @AJenbo . I'll removed some of the comments I just realised I left, then you are free to merge this. |
Moved functions that were getting previous items into smaller ones with better names. At least you can see straight-away what each of these blocks of code do!
…eusgomes28/devilutionX into refactor-check-inventory-paste-func
Source/inv.cpp
Outdated
|
||
std::optional<int8_t> GetPrevItemId(int slot, const Player &player, const Size &itemSize) | ||
{ | ||
int8_t item_something = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a better variable name? This is quite ambiguous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, hence why this feedback request! I guess it gets the item ID from the inv grid? I was not too sure on this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like prevItemId
would have been a good name since that reflects the getter function name and this is the returned value.
Source/inv.cpp
Outdated
return; | ||
} | ||
|
||
auto const maybe_it = (il == ILOC_UNEQUIPABLE) ? GetPrevItemId(slot, player, itemSize) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a quite ambiguous variable name
Nice this is a good enough clean up that the function is now manageable to read 👍 I'll go over it in detail soon. |
Source/inv.cpp
Outdated
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to use the same break style now that the switch isn't so bloated.
Source/inv.cpp
Outdated
player.HoldItem = previouslyEquippedItem; | ||
} | ||
case ILOC_ARMOR: | ||
ChangeBodyEquipment(slot, player, il); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if these new functions for changing equipment took there arguments in the same order: Player, Slot, Other, rather then being a bit random about it.
Source/inv.cpp
Outdated
return it; | ||
} | ||
|
||
std::optional<int8_t> GetPrevItemId(int slot, const Player &player, const Size &itemSize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really have anything against std::optional, but only positive values are valid returns from this so we can use -1 to indicate that nothing was found which allows a bit more direct use with out the need to check before passing the value on to the next user.
Some thing more relevant for this function and refactoring is that if you flip the if statements you can get rid of the else cases and instead make use of early returns. This takes a lot fewer lines and avoids the pyramid structure of the nested conditions.
This also reveals something odd in that CheckOverlappingItems() is always feed a 0 as the 4th argument as this value is never set by anything before it's called.
int8_t GetPrevItemId(int slot, const Player &player, const Size &itemSize)
{
if (player.HoldItem._itype != ItemType::Gold)
return CheckOverlappingItems(slot, player, itemSize, 0);
int8_t item_cell_begin = player.InvGrid[slot - SLOTXY_INV_FIRST];
if (item_cell_begin == 0)
return 0;
if (item_cell_begin <= 0)
return -item_cell_begin;
if (player.InvList[item_cell_begin - 1]._itype != ItemType::Gold)
return item_cell_begin;
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running this PR I'm unable to place items in the belt using the mouse. Could you please look in to what is causing this?
It seems to be swallowed by this bit of code
if ((il != ILOC_UNEQUIPABLE) && (desired_loc != il))
return;
Source/inv.cpp
Outdated
} | ||
|
||
if ((il != ILOC_UNEQUIPABLE) && (desired_loc != il)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously there was and else in between these, with out it it is now not possible to place items on the belt as desired_loc
is never ILOC_BELT
.
} | |
if ((il != ILOC_UNEQUIPABLE) && (desired_loc != il)) { | |
} else if ((il != ILOC_UNEQUIPABLE) && (desired_loc != il)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I applied a few changes and did a bit further cleanups, hope you are ok with me doing so. Overall a good clean up, thanks for putting time an effort in to it.
Moved functions that were getting previous items into smaller ones with better names. At least you can see straight-away what each of these blocks of code do!
This has been sitting on my github for a few weeks, just got back to this today and realise the
timedemo
test is failing. Will fix that, but creating this to get some feedback anyway.