1+ local Ammo = {
2+ counter = 0 ,
3+ isInfiniteAmmo = false ,
4+ isInfiniteAmmoNoReload = false ,
5+ lastMagazineAmmoCount = 0 ,
6+ lastActiveWeapon = nil
7+ }
8+
9+ local Utilities = require (" utility" )
10+ local Inventory = require (" inventory" )
11+
12+ -- All credits to Nexusmods user "TheBs65422" for the infinite ammo script
13+ function Ammo .AddAmmo ()
14+ local moduleName = " Refill All Ammunition"
15+ Utilities .StartProtocol (moduleName )
16+
17+ Game .AddToInventory (" Ammo.HandgunAmmo" , 1000 )
18+ Game .AddToInventory (" Ammo.ShotgunAmmo" , 1000 )
19+ Game .AddToInventory (" Ammo.RifleAmmo" , 1000 )
20+ Game .AddToInventory (" Ammo.SniperRifleAmmo" , 1000 )
21+ Game .AddToInventory (" Ammo.Special" , 1000 )
22+
23+ Utilities .FinishProtocol (moduleName )
24+ end
25+
26+ function Ammo .GetHashAndLength (itemID )
27+ if itemID == nil then
28+ return nil
29+ end
30+
31+ local hash = tostring (itemID ):match (" = (%g+)," )
32+ local length = tostring (itemID ):match (" = (%g+) }" )
33+ local result = nil
34+
35+ if hash ~= nil and length ~= nil then
36+ result = { hash , length }
37+ end
38+
39+ return result
40+ end
41+
42+ function Ammo .IsNewWeapon (weapon )
43+ if weapon == nil then
44+ return false
45+ elseif Ammo .lastActiveWeapon == nil then
46+ return true
47+ else
48+ local currentWeaponData = Ammo .GetHashAndLength (weapon :GetItemID ())
49+
50+ if currentWeaponData == nil then
51+ return false
52+ end
53+
54+ local lastWeaponData = Ammo .GetHashAndLength (Ammo .lastActiveWeapon .itemID )
55+
56+ if lastWeaponData == nil then
57+ return true
58+ end
59+
60+ if currentWeaponData [1 ] ~= lastWeaponData [1 ] and currentWeaponData [2 ] ~= lastWeaponData [2 ] then
61+ return true
62+ else
63+ return false
64+ end
65+ end
66+ end
67+
68+ function Ammo .SetNewWeapon (weapon )
69+ if weapon ~= nil and Game ~= nil then
70+ local statsSystem = Game .GetStatsSystem ()
71+ local weaponItemData = weapon :GetItemData ()
72+
73+ if statsSystem ~= nil and weaponItemData ~= nil then
74+ local weaponStatsObjectID = weaponItemData :GetStatsObjectID ()
75+
76+ if weaponStatsObjectID ~= nil then
77+ Ammo .lastActiveWeapon = {}
78+
79+ Ammo .lastActiveWeapon .statsObjectID = weaponStatsObjectID
80+ Ammo .lastActiveWeapon .itemID = weapon :GetItemID ()
81+ Ammo .lastActiveWeapon .numShotsToFire = statsSystem :GetStatValue (weaponStatsObjectID , Game .EnumValueFromString (" gamedataStatType" , " NumShotsToFire" ))
82+ end
83+ end
84+ end
85+ end
86+
87+ function Ammo .RestoreLastWeaponStats (isModifiedStats )
88+ if Ammo .lastActiveWeapon ~= nil then
89+ if isModifiedStats then
90+ local statModifier = Game [' gameRPGManager::CreateStatModifier;gamedataStatTypegameStatModifierTypeFloat' ](Game .EnumValueFromString (" gamedataStatType" , " NumShotsToFire" ), Game .EnumValueFromString (" gameStatModifierType" , " Additive" ), Ammo .lastActiveWeapon .numShotsToFire )
91+
92+ if statModifier ~= nil then
93+ local statsSystem = Game .GetStatsSystem ()
94+
95+ if statsSystem ~= nil then
96+ statsSystem :AddModifier (Ammo .lastActiveWeapon .statsObjectID , statModifier )
97+ end
98+ end
99+ end
100+
101+ Ammo .lastActiveWeapon = nil
102+ Ammo .lastMagazineAmmoCount = 0
103+ end
104+ end
105+
106+ function Ammo .RefillAmmo (weapon , amount )
107+ if weapon ~= nil then
108+ local ammoType = Game [' gameweaponObject::GetAmmoType;WeaponObject' ](weapon )
109+
110+ if ammoType ~= nil and Game ~= nil then
111+ local transactionSystem = Game .GetTransactionSystem ()
112+ local player = Game .GetPlayer ()
113+
114+ if transactionSystem ~= nil and player ~= nil then
115+ transactionSystem :GiveItem (player , ammoType , amount )
116+ end
117+ end
118+ end
119+ end
120+
121+ function Ammo .InfiniteAmmoToggle ()
122+ local moduleName = " Auto Refill Ammo Toggle"
123+ Utilities .StartProtocol (moduleName )
124+
125+ if Ammo .isInfiniteAmmo then
126+ Ammo .RestoreLastWeaponStats (false )
127+ end
128+
129+ Ammo .isInfiniteAmmo = not Ammo .isInfiniteAmmo
130+
131+ if Ammo .isInfiniteAmmo and Ammo .isInfiniteAmmoNoReload then
132+ Ammo .InfiniteAmmoNoReloadToggle ()
133+ end
134+ print (" Auto Refill:" , Ammo .isInfiniteAmmo )
135+ Utilities .FinishProtocol (moduleName )
136+ end
137+
138+ function Ammo .InfiniteAmmoNoReloadToggle ()
139+ local moduleName = " No Reload Toggle"
140+ Utilities .StartProtocol (moduleName )
141+
142+ if Ammo .isInfiniteAmmoNoReload then
143+ Ammo .RestoreLastWeaponStats (true )
144+ end
145+
146+ Ammo .isInfiniteAmmoNoReload = not Ammo .isInfiniteAmmoNoReload
147+
148+ if Ammo .isInfiniteAmmoNoReload and Ammo .isInfiniteAmmo then
149+ Ammo .InfiniteAmmoToggle ()
150+ end
151+ print (" No Reload:" , Ammo .isInfiniteAmmoNoReload )
152+ Utilities .FinishProtocol (moduleName )
153+ end
154+
155+ function Ammo .OnUpdateAmmo (deltaTime )
156+ if (Ammo .isInfiniteAmmo or Ammo .isInfiniteAmmoNoReload ) and Game ~= nil then
157+ Ammo .counter = Ammo .counter + deltaTime
158+ if (Ammo .counter > 0.1 ) then
159+ Ammo .counter = Ammo .counter - 0.1
160+
161+ local player = Game .GetPlayer ()
162+
163+ if player ~= nil then
164+ local activeWeapon = Game .GetTransactionSystem ():GetItemInSlot (player , TweakDBID .new (' AttachmentSlots.WeaponRight' ))
165+
166+ if activeWeapon ~= nil and Game [' gameweaponObject::IsRanged;ItemID' ](activeWeapon :GetItemID ()) then
167+ if Ammo .isInfiniteAmmo then
168+ Ammo .SetInfiniteAmmo (activeWeapon )
169+ elseif Ammo .isInfiniteAmmoNoReload then
170+ Ammo .SetInfiniteAmmoNoReload (activeWeapon )
171+ end
172+ end
173+ end
174+ end
175+ end
176+ end
177+
178+ function Ammo .SetInfiniteAmmo (weapon )
179+ if weapon ~= nil and Game ~= nil then
180+ if Ammo .IsNewWeapon (weapon ) then
181+ Ammo .RestoreLastWeaponStats (false )
182+ Ammo .SetNewWeapon (weapon )
183+ end
184+
185+ if Ammo .lastMagazineAmmoCount < 1 then
186+ Ammo .lastMagazineAmmoCount = Game [' gameweaponObject::GetMagazineCapacity;WeaponObject' ](weapon )
187+ end
188+
189+ local currentMagazineAmmoCount = Game [' gameweaponObject::GetMagazineAmmoCount;WeaponObject' ](weapon )
190+
191+ if currentMagazineAmmoCount < Ammo .lastMagazineAmmoCount then
192+ Ammo .RefillAmmo (weapon , Ammo .lastMagazineAmmoCount - currentMagazineAmmoCount )
193+
194+ Ammo .lastMagazineAmmoCount = currentMagazineAmmoCount
195+ end
196+ end
197+ end
198+
199+ function Ammo .SetInfiniteAmmoNoReload (weapon )
200+ if weapon ~= nil and Ammo .IsNewWeapon (weapon ) and Game ~= nil then
201+ Ammo .RestoreLastWeaponStats (true )
202+ Ammo .SetNewWeapon (weapon )
203+
204+ if Ammo .lastActiveWeapon ~= nil then
205+ local statModifier = Game [' gameRPGManager::CreateStatModifier;gamedataStatTypegameStatModifierTypeFloat' ](Game .EnumValueFromString (" gamedataStatType" , " NumShotsToFire" ), Game .EnumValueFromString (" gameStatModifierType" , " Additive" ), - Ammo .lastActiveWeapon .numShotsToFire )
206+ local statsSystem = Game .GetStatsSystem ()
207+
208+ if statModifier ~= nil and statsSystem ~= nil then
209+ statsSystem :AddModifier (Ammo .lastActiveWeapon .statsObjectID , statModifier )
210+ end
211+ end
212+ end
213+ end
214+
215+ return Ammo
0 commit comments