Skip to content

Instantly share code, notes, and snippets.

@Mobius1
Last active January 10, 2021 19:40
Show Gist options
  • Save Mobius1/6bcb15d535706f121f76664613a42a56 to your computer and use it in GitHub Desktop.
Save Mobius1/6bcb15d535706f121f76664613a42a56 to your computer and use it in GitHub Desktop.
Spawn a dropped weapon with attachments in Venice Unleashed
-- Spawns dropped weapons and adds random attachments.
-- List of weapons to add
local weapons = {
{
name = "M98B",
container = "Weapons/Model98B/U_M98B",
slot = WeaponSlot.WeaponSlot_0,
attachments = {
optics = {
"Weapons/Model98B/U_M98B_Rifle_Scope",
"Weapons/Model98B/U_M98B_Ballistic_Scope",
"Weapons/Model98B/U_M98B_Acog",
"Weapons/Model98B/U_M98B_EOTech",
"Weapons/Model98B/U_M98B_RX01",
"Weapons/Model98B/U_M98B_IRNV",
"Weapons/Model98B/U_M98B_PKS-07",
"Weapons/Model98B/U_M98B_M145",
"Weapons/Model98B/U_M98B_PSO-1",
"Weapons/Model98B/U_M98B_PK-AS",
"Weapons/Model98B/U_M98B_Kobra",
"Weapons/Model98B/U_M98B_PKA",
},
primary = {
"Weapons/Model98B/U_M98B_Bipod",
"Weapons/Model98B/U_M98B_StraightPull",
},
secondary = {
"Weapons/Model98B/U_M98B_Silencer",
}
},
},
{
name = "AEK971",
container = "Weapons/AEK971/U_AEK971",
slot = WeaponSlot.WeaponSlot_0,
attachments = {
optics = {
"Weapons/AEK971/U_AEK971_PSO-1",
"Weapons/AEK971/U_AEK971_Kobra",
"Weapons/AEK971/U_AEK971_PK-AS",
"Weapons/AEK971/U_AEK971_IRNV",
"Weapons/AEK971/U_AEK971_PKS-07",
"Weapons/AEK971/U_AEK971_PKA",
"Weapons/AEK971/U_AEK971_Acog",
"Weapons/AEK971/U_AEK971_RX01",
"Weapons/AEK971/U_AEK971_EOTech",
"Weapons/AEK971/U_AEK971_Rifle_Scope",
"Weapons/AEK971/U_AEK971_M145",
"Weapons/AEK971/U_AEK971_Ballistic_Scope",
},
primary = {
"Weapons/AEK971/U_AEK971_Foregrip",
"Weapons/AEK971/U_AEK971_Bipod",
},
secondary = {
"Weapons/AEK971/U_AEK971_HeavyBarrel",
"Weapons/AEK971/U_AEK971_TargetPointer",
}
},
},
{
name = "M1014",
container = "Weapons/M1014/U_M1014",
slot = WeaponSlot.WeaponSlot_0,
attachments = {
optics = {
"Weapons/M1014/U_M1014_RX01",
"Weapons/M1014/U_M1014_Eotech",
"Weapons/M1014/U_M1014_Acog",
"Weapons/M1014/U_M1014_IRNV",
"Weapons/M1014/U_M1014_Kobra",
},
primary = {
"Weapons/M1014/U_M1014_ExtendedMag",
"Weapons/M1014/U_M1014_Targetpointer",
},
secondary = {
"Weapons/Common/12gBuckshot",
"Weapons/M1014/U_M1014_Flechette",
"Weapons/M1014/U_M1014_Frag",
"Weapons/M1014/U_M1014_Slug",
},
},
},
}
-- World part to add weapons to
-- This one is for Conquest Large on Operation Firestorm (MP_012)
local worldPartGuid = Guid('BF70E325-DE62-4B0B-A511-8AC9C7EFE22A') -- https://github.com/EmulatorNexus/Venice-EBX/blob/master/Levels/MP_012/CQ_L_Logic.txt#L3402
Events:Subscribe('Level:RegisterEntityResources', function()
-- Get WorldPartData().
local worldPart = WorldPartData(ResourceManager:SearchForInstanceByGuid(worldPartGuid))
worldPart:MakeWritable()
for k, v in pairs(weapons) do
local asset = SoldierWeaponUnlockAsset(ResourceManager:SearchForDataContainer(v.container))
local weaponData = SoldierWeaponData(asset.weapon.object)
local weaponAndSlot = UnlockWeaponAndSlot()
weaponAndSlot.weapon = SoldierWeaponUnlockAsset(asset)
weaponAndSlot.slot = v.slot
local weapon = WeaponUnlockPickupData()
weapon.unlockWeaponAndSlot = weaponAndSlot
weapon.minAmmo = 10
weapon.maxAmmo = 100
weapon.defaultToFullAmmo = false
local entityData = WeaponUnlockPickupEntityData()
entityData.weapons:add(weapon)
-- Add the weapon data to the table to be spawned later
v.entityData = entityData
local blueprint = SpatialPrefabBlueprint()
blueprint.needNetworkId = true
blueprint.alwaysCreateEntityBusClient = true
blueprint.alwaysCreateEntityBusServer = true
blueprint.interfaceHasConnections = true
blueprint.objects:add(entityData)
local referenceObject = ReferenceObjectData()
referenceObject.blueprint = blueprint
referenceObject.blueprintTransform = LinearTransform(
Vec3(0.496058, 0.000000, -0.868289),
Vec3(0.000000, 1.000000, 0.000000),
Vec3(0.868289, 0.000000, 0.496058),
Vec3(-370, 72, 310)
)
worldPart.objects:add(referenceObject)
local registry = RegistryContainer()
registry.blueprintRegistry:add(blueprint)
registry.entityRegistry:add(entityData)
registry.referenceObjectRegistry:add(referenceObject)
ResourceManager:AddRegistry(registry, ResourceCompartment.ResourceCompartment_Game)
end
end)
Events:Subscribe('Spawn:Weapon', function(weapon, pos)
if weapon == nil then
error("Weapon name not defined!")
return
end
if pos == nil then
error("Weapon spawn position not defined!")
return
end
for k, v in pairs(weapons) do
if v.name == weapon then
SpawnWeapon(v, pos)
end
end
end)
function SpawnWeapon(data, pos)
-- Check we have available attachments
if data.attachments ~= nil then
-- Get random attachments
local attachments = GetRandomAttachments(data.attachments)
-- Clear previous attachments otherwise we'll have the previous ones attached!
data.entityData.weapons[1].unlockWeaponAndSlot.unlockAssets:clear()
-- Add new attachments
for _, attachment in ipairs(attachments) do
data.entityData.weapons[1].unlockWeaponAndSlot.unlockAssets:add(UnlockAssetBase(ResourceManager:SearchForDataContainer(attachment)))
end
end
-- Creation params
local params = EntityCreationParams()
params.networked = true
params.transform = LinearTransform()
params.transform.trans = pos
-- Create weapon
local entity = EntityManager:CreateEntity(data.entityData, params)
-- Spawn weapon
if entity ~= nil then
entity:Init(Realm.Realm_ClientAndServer, true)
end
end
-- Selects random attachments from the weapons table
function GetRandomAttachments(tbl)
local attachments = {}
-- Select an optic
if tbl.optics ~= nil then
table.insert(attachments, tbl.optics[ math.random( #tbl.optics )])
end
-- Select primary attachment
if tbl.primary ~= nil then
table.insert(attachments, tbl.primary[ math.random( #tbl.primary )])
end
-- Select secondary attachments
if tbl.secondary ~= nil then
local secondary = GetRandomSelections(tbl.secondary, math.random(1, #tbl.secondary))
for k, v in pairs(secondary) do
table.insert(attachments, v)
end
end
return attachments
end
-- Get random elements from table
function GetRandomSelections(tbl, count)
-- Shuffle table
for i = #tbl, 2, -1 do
local j = math.random(i)
tbl[i], tbl[j] = tbl[j], tbl[i]
end
local randomSelections = {}
for index = 1, count do
table.insert(randomSelections, tbl[index])
end
return randomSelections
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment