Reloading algorithm

Posted By: exile

Reloading algorithm - 05/26/12 05:40

Hey guys, so I am looking for a reloading algorithm. Something where...

X = Current Ammo (Maximum of 30)
Y = Ammo left

So when you activate the reload sequence...

if X=24 and Y=60, then X=30 and Y=54

if X=15 and Y=4, then X=19 and Y=0

if X=19 and Y=15, then X=30 and Y=4

I have been trying and trying to get this working and to no avail. All of the solutions I made work, but they require a ton of if/else statements and I know there is a much more simple way of doing this. So, anyone have any ideas?
Posted By: MasterQ32

Re: Reloading algorithm - 05/26/12 07:28

Code:
var ammo_reload = minv(
    ammo_left,          // Maximum is ammo_left, so we can't reload bullets we don't have
    30 - ammo_current); // Calculate missing ammo in weapon
ammo_left -= ammo_reload;
ammo_current += ammmo_reload;


Should do the stuff you want
Posted By: exile

Re: Reloading algorithm - 05/26/12 08:04

Perfect! I never even knew about minv! After seeing how its used I could use this to reduce the amount of code I have. Thanks MasterQ32, With your permission would I be able to add this to a contribution I am making? Essentially, its a big contribution with code snippets and other useful things for beginning and advanced programmers alike. With credit going to you of course.
Posted By: MasterQ32

Re: Reloading algorithm - 05/26/12 11:01

no problem, you don't need to credit me for this snippet, but if you want, you can grin
© 2024 lite-C Forums