Jagged Alliance 3

Jagged Alliance 3

Tons of Guns
Want to integrate ToG weapons for your custom merc without Tog being a requirement?
Then look no further and try giving the following code a try:


function OnMsg.CampaignStarted()
--build a list of which merc should get what new item and what to replace?
local replacements = {
{"zzGentleman", "PPK_1", "PBPistol",{["Muzzle"]="Suppressor", ["Barrel"]="ToG_Comp_Pstl_Barrel_Long_1"}},
{"zzDocBlackheart", "Gewehr43_1", "Winchester1894"},
{"Vicki", "C1911_1", "HiPower"},
{"Barry", "HiPower", "ColtPeacemaker"},
}

-- see if ToG is active
local tog_Installed = false
for i=1, #ModsLoaded, 1 do
if ModsLoaded[i].id == "KKh3Yhf" then
tog_Installed = true
end
end
--if ToG is active handle replacement
if tog_Installed then
for i = 1, #replacements, 1 do
-- Okay sanity check for our input - because typos are to easy and no one needs everything to break down, just because you missed a "Colt" in front of "Peacemaker")
if not g_Classes[replacements[i][2]] or not g_Classes[replacements[i][3]] or not gv_UnitData[replacements[i][1]] then
print("Something went wrong with number ".. i .. " in the replacements list. Please check the input: ")
print("Merc-ID given: "..replacements[i][1].." New item ID given as: ".. replacements[i][2].. " Old item ID given as: ".. replacements[i][3])
goto continue
end
-- set Merc
local merc = gv_UnitData[replacements[i][1]]
--remove the item and add the new potentially modded weapon
local weapon = g_Classes[replacements[i][2]]:new()
local pos
-- add mods to weapon
if replacements[i][4] then
for mod_Slot, component in pairs(replacements[i][4]) do
weapon:SetWeaponComponent(mod_Slot, component)
end
end
merc:ForEachItem(function(item, slot) -- do the code following this on all item inventories/slots
if IsKindOf(item, replacements[i][3])then
pos = merc:GetItemPos(item) -- get exact place in inventory of item that gets removed
merc:RemoveItem(slot, item) -- Remove the item
DoneObject(item)
merc:AddItem(slot, weapon, pos) -- place new item.
end
end) -- end do for all items
--magically have standard ammo in the new weapon
local ammo = GetAmmosWithCaliber(weapon.Caliber, "sort")[1]
if ammo then
local tempAmmo = PlaceInventoryItem(ammo.id)
tempAmmo.Amount = tempAmmo.MaxStacks
weapon:Reload(tempAmmo, "suspend_fx")
DoneObject(tempAmmo)
end
--change Ammo part
-- Remove Old Ammo
local cur_AmmoAmmount
merc:ForEachItem(function(item, slot)
if IsKindOf(item, "Ammo") then
cur_AmmoAmount = item.Amount
merc:RemoveItem(slot, item) -- Remove the item
DoneObject(item)
end
end)
-- Add new Ammo
PlaceItemInInventory(ammo.id, cur_AmmoAmount, merc)
::continue::
end
end
end

Simply add a new code asset and put the code inside.
Then change the replacement lines to your lining:
{"MercID", "newTogWeaponId", "weaponToBeReplaceId"} - is a basic setup for a switch
Just look at the examples:
{"Vicki", "C1911_1", "HiPower"}, - Vicki - C1911 as new weapon; and remove the HiPower.
Now the Script will remove all HiPower from Vickis inventory and switch in the C1911. It currently doesn't deal with just switching 1 weapon (or multiple weapons with different ammunition for that matter).

Now you can also add modded weapons:

{"MercID", "newTogWeaponId", "weaponToBeReplacedId", {"SlotName1" = "ModificationtoUse1"}},
If you want multiple modifications you just need to have a comma inbetween:
{"MercID", "newTogWeaponId", "weaponToBeReplacedId", {"SlotName1" = "ModificationToUse1", "SlotName2"="ModificationToUse2"}},

Once again an example:
{"zzGentleman", "PPK_1", "PBPistol",{["Muzzle"]="Suppressor", ["Barrel"]="ToG_Comp_Pstl_Barrel_Long_1"}},
My custom Merc zzGentleman will get a PPK instead of his (already custom) PB-Pistol and i'll add a Suppressor to the Muzzle and the specific Long-Barrel in the Barrel slot.

The code should handle errors in the Merc and Weapon IDs. If there's a error in the weapon modification, it should still run without a problem, it just doesnt add the mods.
Also the code will remove the ammo for the old gun an exchange it 1:1 round for the ammo the new weapon is using.

Small caveat - it will remove all old ammo. So if you've got a custom merc with more than one weapon, using different ammo, you might need to edit the code.

if you need further help, just post here.
Last edited by LotR[Henchman]; Sep 19, 2023 @ 8:14am