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

inventory: limit max item quantities to prevent crashing #2554

Merged
merged 1 commit into from
Mar 1, 2025

Conversation

walkawayy
Copy link
Collaborator

@walkawayy walkawayy commented Feb 26, 2025

Checklist

  • I have read the coding conventions
  • I have added a changelog entry about what my pull request accomplishes, or it is an internal change
  • I have added a readme entry about my new feature or OG bug fix, or it is a different change

Description

  1. Includes a commit to fix spacing so my local linter stops driving me insane. (TBD the Github linter likes the no space apparently)
  2. Changes inventory item quantities to be stored as an int32_t since it seems everywhere else that's the quantity type.
  3. Adds a Inv_InsertAmmo helper function similar to Inv_InsertItem. This allows us to clamp to the max quantity of MAX_QTY 999999 (open to suggestions on this). MAX_QTY prevents the give command from causing an overflow which can crash the game, overflow the item quantity, etc. This number seemed big enough to be avoided during normal gameplay, but also give a massive amount if desired. Also allows you to type something like give 1000000000 shotgun shells to max out the item for testing without worrying about crashing the game.

At first I tried experimenting with checking max int32_t values on scan and then checking to make sure the scanned value + the current quantity fit in an int32_t size, but I think just capping it like this makes the code much simpler. You will only ever hit this max quantity with cheats anyway, so it's just a theoretical cap to prevent crashes.

Requires testing all sorts of give x ammo commands, picking up normal pickups while at max value, NG+, etc. I don't see any issues but I did notice 2 quirks for feedback:

  1. In TR1, maxing out an item quantity removes the ammo string even in a non-NG+ save. I assume this is some hard coded reason for NG+.
  2. 999,999 shotgun ammo is maxed out as 166,666 because of the shotgun technically having 6x the pellets per ammo. I don't think it's an issue but I wanted to note it.

@walkawayy walkawayy added TRX bug A bug with TRX TR2 TR1 labels Feb 26, 2025
@walkawayy walkawayy self-assigned this Feb 26, 2025
@walkawayy walkawayy requested review from a team as code owners February 26, 2025 02:53
@walkawayy walkawayy requested review from rr- and lahm86 and removed request for a team February 26, 2025 02:53
Copy link

github-actions bot commented Feb 26, 2025

@walkawayy
Copy link
Collaborator Author

walkawayy commented Feb 26, 2025

This linter will be the death of me. Why does it prefer this 💀

-        TRY_OR_FAIL(VFile_TrySkip(file, num * size));                          \
+        TRY_OR_FAIL(VFile_TrySkip(file, num *size));                           \

@rr-
Copy link
Collaborator

rr- commented Feb 26, 2025

Inv_InsertItem refers to inserting an item to the inventory ring. I think a more appropriate name for InsertAmmo would be AddAmmo. WDYT?

Maybe we could support /give max shotgun ammo for some syntax sugar?

@walkawayy
Copy link
Collaborator Author

Inv_InsertItem refers to inserting an item to the inventory ring. I think a more appropriate name for InsertAmmo would be AddAmmo. WDYT?

Maybe we could support /give max shotgun ammo for some syntax sugar?

Good idea on the function name. Ya I will look into supporting the displayed max quantity of shotgun ammo instead of the underlying ammo. I will also try to look into TR1 hiding the ammo count outside of NG+.

@walkawayy walkawayy linked an issue Feb 26, 2025 that may be closed by this pull request
@walkawayy walkawayy force-pushed the inv-item-quantity branch 3 times, most recently from a7737a7 to 66250eb Compare February 26, 2025 23:53
@walkawayy
Copy link
Collaborator Author

walkawayy commented Feb 26, 2025

I believe all issues are resolved. I fixed the ammo string not displaying for large quantities in TR1. I made a TODO for the shotgun ammo max quantity. I think it will require a libtrx merge to make the shotgun_ammo common. I also reverted the lint.

@walkawayy
Copy link
Collaborator Author

@aredfan I will wait for your approval before merging. Take as long as you need, no rush.

{
weapon_ammo->ammo += qty;
// TODO Check for shotgun_ammo when merged to libtrx and clamp to MAX_QTY
// * 6.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe this can be already done using Lara_GetInfo().

@aredfan
Copy link
Collaborator

aredfan commented Feb 27, 2025

@aredfan I will wait for your approval before merging. Take as long as you need, no rush.

@walkawayy no problem and thank you. The only issue I found happens if Lara doesn't have weapons in her inventory. If I use the give command for ammo such as /give shells, the limit appears to be 1,999,998 instead of 999,999. This affects all ammo in TR1/2 except for grenades.

@walkawayy
Copy link
Collaborator Author

walkawayy commented Feb 28, 2025

Oops I fixed a TR2 bug where I had copied Uzi clip constants into the Magnum functions.

@aredfan maybe @lahm86 can correct me, but I don't think it's a bug actually. The way the ammo works underneath the hood is a little odd. Uzis for example:

  • Each Uzi clip you pickup in TR1 is technically a quantity of 1.
  • However, that 1 pickup is technically 100 bullets composed of 2 UZI_AMMO_CLIPs of 50 each. I think this is to simulate Lara's dual weapons where she would technically need a clip in each weapon in real life.
  • For display purposes, the game outputs the ammo clips' ammo strings as the quantity * 2., so the 1 picked up clip shows as 2. (InvRing_ShowItemQuantity) That's why the displayed number in inventory is 999,999 * 2 aka 1,999,998. Lara technically has 999,999 clips.
  • This applies for all ammo, even the shotgun where NUM_SG_SHELLS is 2 even though that ammo is a little different where SHOTGUN_AMMO_CLIP is 6 because Lara fires 6 pellets. Similar to that shotgun shots fired statistics issue.

The main purpose of the PR is to set a realistic cap that will never be reached in normal gameplay and avoid crashes, so the exact number isn't that important imo.

@walkawayy
Copy link
Collaborator Author

@lahm86 is it ok if I merge?

@lahm86
Copy link
Collaborator

lahm86 commented Feb 28, 2025

It LGTM. Maybe hang on for rr's approval?

@walkawayy walkawayy merged commit 3808fa7 into LostArtefacts:develop Mar 1, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TR1 TR2 TRX bug A bug with TRX
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Console command to give large quantities of items can crash the game
4 participants