Infinite ammo

Some map hacks involve using the player weapon functions on an entity like an info_notnull. In order to get this working, you need to set the ammo level on that entity, usually to some large number. Nonetheless, some mappers may think that setting a large number isn’t enough – potentially that ammo could run out and the entity would stop working! I’m here to reassure you that we can make an infinite shooter, and the trick to doing so is very simple. Just set the ammo count to 99999999 or more and it will literally never run out. No matter how many times the code tries to subtract 1 from this number, the ammo will never decrease.

To see why, we need to know how floating point numbers work. If you already know what a floating point number is, skip to the last paragraph. It’s easiest to understand floating point numbers after seeing scientific notation for numbers. The number 124,000 can be rewritten as 1.24×105, and any number in this pattern of “number between 1 and 10 multiplied by a power of 10” is written in scientific notation. It lets us write large numbers like 6.022×1023 or small numbers like 1.6×10-35 in a compact way. Both of those numbers have been rounded to a number of significant figures, and the way we write them makes it easy to see how many – just count the digits.

Floating point numbers on computers work in a similar way. The most important difference is that computers work in binary, so the number of digits in a float is measured against the number in binary, and instead of ×10n the number ends with ×2n instead. Typical 32 bits floats use 1 bit to record the sign, 8 bits to record the power of 2 and 23 bits to record the digits after the decimal point. Glossing over a few of the details of the format, it’s a number stored with 24 significant figures in binary. Visually, we are talking×2b where there are 23 binary digits in “aaa~a”. Note that because we are in binary, the number must start with a 1!

So what is special about 99999999? Well, all numbers in QuakeC are stored in 32 bit floating point. If we think of a QuakeC number between 223 and 224, and try multiplying out the floating point representation, we would notice that the least significant figure is now in the 1’s column – we can only accurately represent whole numbers of this size. Go up to 224 and 225, and we cannot represent the odd numbers, because the least significant figure is in the 2’s column (we’re working in binary!). So what happens if you subtract 1 from an even number above 224? Well, there’s a chance that it would round the calculation down to the next even number. But if we go one step higher to 225, we can be sure that subtracting 1 does nothing! 225 = 33554432, so leaving ourselves a little margin for error we are sure 99999999 cannot be reduced by 1.


3 thoughts on “Infinite ammo

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s