Author Topic: [Patch][Test]Bullet saving patch!  (Read 5768 times)

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
[Patch][Test]Bullet saving patch!
« on: November 04, 2015, 03:34:51 am »
Picture this: You have this fancy gun. One magazine, which contains a dozen shots, costs a million. You have two. So you save the shots for when it's going to count. One day, you fire once when a chryssalid jumps at you and, upon your return to base, are told your clip with 11/12 bullets doesn't meet regulations. The quartermaster takes it back and doesn't have another to give you, so you're down to one clip. As you sadly walk away, you can hear him cackling with glee as he fires the other 11/12 shots! Same thing happens to your buddy when he goes to see the quartermaster after the mission.

That's how it was in XCom: If you had fired a shot, that clip was useless.

In OpenXCom: You're a bit more clever. First make a tally of all available shots for a clip type, divide by the number of shot per clip, and round down. So you put your bullets together and save a clip before going to see the quartermaster. It is a HUGE improvement, but it is not yet quite there. You still get short-changed on those leftover bullets that could have amounted to up to 99.999999999999999% of a clip.

So here comes my proposition:
Take those leftover bullets, divide by the number of bullets in a clip and that's your odds of getting a "free" clip. "FREE?!" you say? well, not really, but you are getting some extra bullets for free, IF you get the clip. If you don't, then you lose all the extra bullets you had.

Take a gun with 6 shots per clip. You fire 3 shots, you have 50% chance of getting a clip back. If you do, yay!, but next time you fire 3 shots, maybe you'll lose the whole 6 shots clip. Over time, it will average to losing a clip (=6 shots) every other battle, which means 3 shots per battle. Instead of the current situation where you lose a clip every time.

This is especially visible in some mods with special weapons of which you only take a few. For example, the Fusion Torch or Heavy Machine Gun. You take one, you fire once, you lose a whole clip. Annoyingly, it makes you gun-shy. But once you fire once in the mission, since you know you'll lose the clip, you can happily fire until you run out. I decided I'd go for a more average approach.

So I went into the OpenXCom code to look at DebriefingState.cpp, and I found:
Code: [Select]
https:// calculate the clips for each type based on the recovered rounds.
        for (std::map<RuleItem*, int>::const_iterator i = _rounds.begin(); i != _rounds.end(); ++i)
        {
               int total_clips = i->second / i->first->getClipSize();
                if (total_clips > 0)
                        base->getItems()->addItem(i->first->getType(), total_clips);
        }
This is how OpenXCom gets the total number of rounds, divides by the number of rounds per clip and it is rounded down since we are doing integer arithmetic. I think... Then if there are clips left, they are added back to the stock at the base.

The easiest way to implement my change is simply to add 0 to clipSize-1 "free" bullets at the end of the battle. If you get enough that the total goes once more over the clip size, you get a free clip. If it wasn't enough to make a difference, you don't and you lose all the extra bullets you had. I implemented this as:

- int total_clips = i->second / i->first->getClipSize();
+ int total_clips = ( i->second + RNG::generate(0, ( i->first->getClipSize() - 1 ) ) )/ i->first->getClipSize();


So say you had a clip of 6 shots and fired 3 shots. This would add 0 to 5 "free" shots, then divide by 6. If you get 0, 1 or 2 free shots (bringing the total to 3, 4 or 5), you don't get a clip back and lose your unused shots. If you got lucky with 3-4-5 free shots (total 6-7-8), you can a clip back. So you used 50% of your shots and you have 50% chance of getting the clip back.

If you had 3 clips of six shots and fired 3 shots, then you have 15 shots left, plus 0-5 "free shots". 0-1-2 gives you 15-16-17, so you get two clips back. 3-4-5 gives you 18-19-20, so you get 3. Theoretically, it should work.

In practice? I'm not 100% sure, since I'm not 100% sure that I coded it right (it's only my second patch for OpenXCom, and second attempt at coding in c++). I ran a quick trial by assaulting a base, firing 3 shots out of a weapon with 6 shots/clip and then aborting, repeat (ie go back to base and come back, not reload, although that could have worked with save scumming turned on). It took 22 missions to lose 8 clips. The expected number would be 16 missions (lose a clip every other mission), so I was lucky? Or I was dumb and coded something wrong.. Not sure.. The sample size is a bit small, but the experiment got boring ;)
« Last Edit: November 04, 2015, 03:47:00 am by Arthanor »

Offline 7Saturn

  • Colonel
  • ****
  • Posts: 457
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #1 on: November 04, 2015, 10:51:48 pm »
I like the idea. In principle, statistically speaking, you'd not lose any more clips than you used them up entirely.

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #2 on: November 05, 2015, 12:39:29 am »
Indeed, the aim is to have something which is statistically equivalent to tracking bullets, without having to track bullets ;)

I am playing with it now, and removing that little pressure to not use fancy ammo is enjoyable, as is the idea that conserving the other shots is worth it.

Offline Coincident

  • Captain
  • ***
  • Posts: 54
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #3 on: November 05, 2015, 01:56:40 am »
Very interesting, and statistically correct :)

It's funny - I have implemented exactly the same thing in the past, in an experimental doom mod of mine :P
A roundRandom() function that gives decimal precision (statistically) for stuff that is stored in INT values.

You can apply the same idea to ANY value in the game that is "INT'ed" - especially to things that are usually in small numbers (like ammo clips) - where getting +1 or -1 can make a difference.

niculinux

  • Guest
Re: [Patch][Test]Bullet saving patch!
« Reply #4 on: December 06, 2015, 07:22:07 pm »
May be suggested as an openxcom preinstalled mod, or even merged in openxcom extended!

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #5 on: December 06, 2015, 07:46:30 pm »
Not that for some reason, the RNG isn't included in DebriefingState.cpp anymore, so you need to add:

Code: [Select]
#include "../Engine/RNG.h"

to the top of the file.

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #6 on: February 12, 2016, 07:42:45 pm »
For anybody using git, here is a simple patch file to make the changes:

Offline Xtendo-com

  • Colonel
  • ****
  • Posts: 118
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #7 on: April 25, 2016, 11:43:45 am »
Verbose version, so you can check that clip saving patch actually works as intended
Spoileroutput from console:
[VERB]   Total STR_RIFLE_CLIP by available bullets: 49 (bullets: 997) (clipsize: 20)
[VERB]   Total STR_RIFLE_CLIP by available bullets and chance: 50 (bullets=997) (additional bullets=11) (clipsize:=20)
[VERB]   Total STR_HEAVY_PLASMA_CLIP by available bullets: 4 (bullets: 172) (clipsize: 35)
[VERB]   Total STR_HEAVY_PLASMA_CLIP by available bullets and chance: 5 (bullets=172) (additional bullets=5) (clipsize:=35)
[VERB]   Total STR_PLASMA_RIFLE_CLIP by available bullets: 4 (bullets: 137) (clipsize: 28)
[VERB]   Total STR_PLASMA_RIFLE_CLIP by available bullets and chance: 5 (bullets=137) (additional bullets=3) (clipsize:=28)
[VERB]   Total STR_PLASMA_PISTOL_CLIP by available bullets: 5 (bullets: 130) (clipsize: 26)
[VERB]   Total STR_PLASMA_PISTOL_CLIP by available bullets and chance: 5 (bullets=130) (additional bullets=17) (clipsize:=26)

Offline Arthanor

  • Commander
  • *****
  • Posts: 2488
  • XCom Armoury Quartermaster
    • View Profile
Re: [Patch][Test]Bullet saving patch!
« Reply #8 on: April 25, 2016, 02:32:05 pm »
Nice! Thank you for the improvement