void ReloadWeapon(Weapon_Base weapon, PlayerBase unitPB) { Weapon wpn = Weapon.Cast(weapon); int muzzle = wpn.GetCurrentMuzzle(); int muzzles_cnt = wpn.GetMuzzleCount(); if (wpn.HasInternalMagazine(muzzle)) // Bolt action with internal mag! { if (wpn.GetInternalMagazineMaxCartridgeCount(muzzle) > wpn.GetInternalMagazineCartridgeCount(muzzle)) { int ammount = wpn.GetInternalMagazineMaxCartridgeCount(muzzle) - wpn.GetInternalMagazineCartridgeCount(muzzle); for (int i = 0; i < ammount; i++) { weapon.FillInnerMagazine(wpn.GetRandomChamberableAmmoTypeName(muzzle)); } } } else // Can fit external mags! { int muzzleIndex = weapon.GetCurrentMuzzle(); mag = wpn.GetMagazine(muzzleIndex); if (!mag) // if mag equilds NULL firearm has no mag yet { mag = weapon.SpawnAttachedMagazine(); // Creating attached magazine also cartrige shall be cah,bered too mag.LocalSetAmmoCount(mag.GetAmmoMax()); // Sync data between server and clients mag.ServerSetAmmoCount(mag.GetAmmoMax()); } else { // Is Mag is not NULL let's fill it with ammo! if (!mag.IsFullQuantity()) { ItemBase currentMag = mag; // Recast into ItemBase to set Quantity to zero currentMag.SetQuantity(0); mag.LocalSetAmmoCount(mag.GetAmmoMax()); // Fill magazine with ammunition mag.ServerSetAmmoCount(mag.GetAmmoMax()); // Sync with server } } } unitPB.SetSynchDirty(); // After all we start synchronization with server oh inventory of ours AI } override void OnUnitSpawn(eAIBase unit) { if (!unit) return; // Leave the function if unit is equals NULL unit.SetUnlimitedMags(true); // Uncomment this to give the AI unlimited magazines (new one is added on reload) PlayerBase unitPB = PlayerBase.Cast(unit); // Cast eAIBase unit into PlayersBase to give this eventhandler access to unit's inventory if (unitPB) // Validate that Cast was completed and unitPB not equal NULL { EntityAI testItemEntity = unitPB.GetInventory().CreateInInventory("Lockpick"); // Or any other classname. We saving EntityAI-link to set quantity and other EntityAI-abilities } Weapon_Base weapon = Weapon_Base.Cast(unit.GetItemInHands()); // Get FireArm from hand of spawned unit if (!weapon) return; // Leave the function if weapon equals NULL EntityAI UniLight = NULL; // UniversalLight because it's requred the Battery attached EntityAI Opt = NULL; // Opt = Optics (ACOG, PSO whateverelse...) because it's requred the Battery attached EntityAI HG = NULL; // Handguard of AK/M4 any weapon that may carry a flashlight that shoul be attached to the Handguard EntityAI ShoulderItem = NULL; // New item that shall be on shoulder! InventoryLocation dst = NULL; // Temporary for placing new spawn item to loaction Weapon wpn = NULL; // Save for future recasting Weapon_Base TStringArray Carabines = {"SKS","SKS_Black","SKS_Green","Mosin9130","Mosin9130_Black","Mosin9130_Green","Mosin9130_Camo", "Winchester70", "Winchester70_Black"}; // Random carabine/rifle for AI if (weapon) { if (weapon.GetType() == "M4A1") { HG = ItemBase.Cast(weapon.GetInventory().CreateAttachment("M4_RISHndgrd")); // Create RIS Handguard UniLight = HG.GetInventory().CreateAttachment("UniversalLight"); // Attach to the Handguard the Weapon Flashlight if (UniLight) { UniLight.GetInventory().CreateAttachment("Battery9V"); // Attach Battery to the Weapon Flashlight } weapon.GetInventory().CreateAttachment("M4_OEBttstck"); // install Buttstock weapon.GetInventory().CreateAttachment("ACOGOptic"); // Install Optics //weapon.GetInventory().CreateAttachment("M4_Suppressor"); // Uncomment if you need AI unit with suppressor ShoulderItem = unitPB.FindAttachmentBySlotName("Melee"); // Check do AI has any item at secont sholder if (ShoulderItem) { // If AI has 2nd item on sholder ShoulderItem.Delete(); // REMOVE IT! dst = new InventoryLocation(); // Prepare invetory location for future stuff ShoulderItem = unitPB.GetInventory().CreateInInventory("Winchester70"); //Spawn any type of Firearm if (ShoulderItem) { // If we had been spawned it let's move it to Sholder slot (also known as Melee lost) dst.SetAttachment(unitPB, ShoulderItem, InventorySlots.MELEE); // Perform moving action weapon = Weapon_Base.Cast(ShoulderItem); // Cast our new Firearm into Weapon_Base if (weapon) { // if everything is right - let place an attachment there weapon.GetInventory().CreateAttachment("HuntingOptic"); } } } else { // If slot MELEE is empty then let's place there an Firearm! dst = new InventoryLocation(); // Prepare invetory location for future stuff ShoulderItem = unitPB.GetInventory().CreateInInventory("Winchester70"); //Spawn any type of Firearm if (ShoulderItem) { // If we had been spawned it let's move it to Sholder slot (also known as Melee lost) dst.SetAttachment(unitPB, ShoulderItem, InventorySlots.MELEE); // Perform moving action weapon = Weapon_Base.Cast(ShoulderItem); // Cast our new Firearm into Weapon_Base if (weapon) { // if everything is right - let place an attachment there weapon.GetInventory().CreateAttachment("HuntingOptic"); } } } } else if (weapon.GetType() == "AKM") // here alittle harder example { weapon.GetInventory().CreateAttachment("AK74_Hndgrd"); // Any AK Handguard, even Rail HandGuard weapon.GetInventory().CreateAttachment("AK74_WoodBttstck"); // Any AK Buttstock Kashtan = weapon.GetInventory().CreateAttachment("KobraOptic"); // Install Optics Kashtan.GetInventory().CreateAttachment("Battery9V"); // Install Battery to the potics //weapon.GetInventory().CreateAttachment("AK_Suppressor"); // Uncomment if you need AI unit with suppressor ShoulderItem = unitPB.FindAttachmentBySlotName("Melee"); // Check do AI has any item at secont sholder if (ShoulderItem) { ShoulderItem.Delete(); CamoIndex = Carabines.GetRandomIndex(); // Get random Index (type integer) if (CamoIndex > -1) // Check is Index more then -1 { SelectedCarabine = Carabines[CamoIndex]; // Got Classname of AI's future Carabine dst = new InventoryLocation(); ShoulderItem = unitPB.GetInventory().CreateInInventory(SelectedCarabine); // Create carabine if (ShoulderItem) { dst.SetAttachment(unitPB, ShoulderItem, InventorySlots.MELEE); weapon = Weapon_Base.Cast(ShoulderItem); if (weapon) { switch (SelectedCarabine) { case "SKS": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "SKS_Black": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "SKS_Green": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Black": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Green": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Camo": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Winchester70": { weapon.GetInventory().CreateAttachment("HuntingOptic"); ReloadWeapon(weapon, unitPB); break; } case "Winchester70_Black": { weapon.GetInventory().CreateAttachment("HuntingOptic"); ReloadWeapon(weapon, unitPB); break; } } } } } CamoIndex = -1; } else { CamoIndex = Carabines.GetRandomIndex(); if (CamoIndex > -1) { SelectedCarabine = Carabines[CamoIndex]; ShoulderItem = unitPB.GetInventory().CreateInInventory(SelectedCarabine); if (ShoulderItem) { dst.SetAttachment(unitPB, ShoulderItem, InventorySlots.MELEE); weapon = Weapon_Base.Cast(ShoulderItem); if (weapon) { switch (SelectedCarabine) { case "SKS": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "SKS_Black": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "SKS_Green": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Black": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Green": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Mosin9130_Camo": { weapon.GetInventory().CreateAttachment("PUScopeOptic"); ReloadWeapon(weapon, unitPB); break; } case "Winchester70": { weapon.GetInventory().CreateAttachment("HuntingOptic"); ReloadWeapon(weapon, unitPB); break; } case "Winchester70_Black": { weapon.GetInventory().CreateAttachment("HuntingOptic"); ReloadWeapon(weapon, unitPB); break; } } } } } CamoIndex = -1; } } } }