816 lines
27 KiB
Lua
Raw Permalink Normal View History

2025-01-04 23:00:19 +08:00
ItemTool = {};
----------------------------------- 武器 -----------------------------------
--- 设置玩家所有武器槽武器的子弹属性
---@param InPawn UGCPlayerPawn_C 玩家
---@param InfiniteType EFillBulletType 无限子弹类型
---@param IsEnable bool 是否设置
function ItemTool.SetAllWeaponInfinite(InPawn, InfiniteType, IsEnable)
if IsEnable == nil then IsEnable = true; end
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
ItemTool.SetWeaponInfinite(InPawn, Weapon, InfiniteType, IsEnable);
end
end
---@param InWeapon ASTExtraShootWeapon 射击武器
---@param InfiniteType EFillBulletType 武器无限子弹类型
---@param IsEnable bool 是否开启
function ItemTool.SetWeaponInfinite(InPawn, InWeapon, InfiniteType, IsEnable)
if UE.IsValid(InWeapon) and ItemTool.IsShootWeapon(InWeapon) then
if InfiniteType == EFillBulletType.Infinite then
UGCGunSystem.EnableInfiniteBullets(InWeapon, IsEnable);
elseif InfiniteType == EFillBulletType.ClipInfinite then
UGCGunSystem.EnableClipInfiniteBullets(InWeapon, IsEnable);
elseif InfiniteType == EFillBulletType.Fill then
ItemTool.FillWeaponBullet(InPawn, InWeapon);
end
end
end
---@param InPawn UGCPlayerPawn_C
function ItemTool.FillAllWeaponBullets(InPawn)
InPawn:SetAllWeaponBulletNumToMaxOnServer(true, false)
end
---@param InPawn UGCPlayerPawn_C
---@param InWeapon ASTExtraWeapon
function ItemTool.FillWeaponBullet(InPawn, InWeapon)
InPawn:SetTargetWeaponBulletNumToMaxOnServer(InWeapon, true, false);
end
---@param InPawn UGCPlayerPawn_C
function ItemTool.FillCurrentWeapon(InPawn)
local Weapon = UGCWeaponManagerSystem.GetCurrentWeapon(InPawn);
if ItemTool.IsValidWeapon(Weapon) then
InPawn:SetTargetWeaponBulletNumToMaxOnServer(Weapon, true, false);
end
end
---@param InWeapon ASTExtraWeapon
function ItemTool.IsValidWeapon(InWeapon)
return InWeapon ~= nil and UE.IsValid(InWeapon);
end
---@param InPawn UGCPlayerPawn_C
---@param Weapons ASTExtraShootWeapon[]
function ItemTool.GetShootWeapon(InPawn, Weapons)
if Weapons == nil then Weapons = {}; end
for i, InEnum in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, InEnum);
if Weapon ~= nil and UE.IsValid(Weapon) and ItemTool.IsShootWeapon(Weapon) then
table.insert(Weapons, Weapon);
end
end
return Weapons;
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param InWeaponId int32 武器 ID
---@param FillBulletType EFillBulletType 无限子弹类型
---@param IsEnable bool 是否设置
function ItemTool.SetWeaponInfiniteById(InPawn, InWeaponId, FillBulletType, IsEnable)
if FillBulletType ~= nil then
if FillBulletType <= 3 then
-- 获取这个武器
local Weapons = ItemTool.GetWeaponsById(InPawn, InWeaponId);
for i, Weapon in pairs(Weapons) do
ItemTool.SetWeaponInfinite(InPawn, Weapon, FillBulletType, IsEnable);
end
table.func(InPawn, "SetInfiniteType", FillBulletType)
else
if WeaponAmmunitionItem[InWeaponId] and FillBulletType > 0 then
UGCBackPackSystem.AddItem(InPawn, WeaponAmmunitionItem[InWeaponId], FillBulletType);
end
end
--ItemTool.SetAllWeaponInfinite(InPawn, EFillBulletType.Fill, IsEnable);
end
end
--- 找到玩家身上的一把枪械或者近战武器这些
---@param InPawn UGCPlayerPawn_C 玩家
---@param IsForce bool 是否强制找到一把枪,默认都是强制
---@return ASTExtraWeapon
function ItemTool.GetPlayerFirstWeapon(InPawn, IsForce)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if UE.IsValid(Weapon) then return Weapon; end
end
return nil;
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param IsForce bool 是否强制要删除一个武器,默认是第一把武器
function ItemTool.DropCurrentWeapon(InPawn, IsForce)
if not UE.IsValid(InPawn) then return end
local Weapon = ItemTool.GetPlayerFirstWeapon(InPawn, IsForce);
if not UE.IsValid(Weapon) then return end
local WeaponId = Weapon:GetWeaponItemID();
local Parts = ItemTool.GetWeaponParts(Weapon)
if not table.isEmpty(Parts) then
for i, v in pairs(Parts) do
UGCBackPackSystem.DropItem(InPawn, v, 1, true);
end
end
UGCBackPackSystem.DropItem(InPawn, WeaponId, 1, true);
ItemTool.DropBullets(InPawn, WeaponId)
end
function ItemTool.DropBullets(InPawn, InWeaponId)
local AllItems = UGCBackPackSystem.GetAllItemData(InPawn)
local AmmoType = WeaponAmmunitionItem[InWeaponId];
for i, v in pairs(AllItems) do
if AmmoType == v.ItemID then
UGCBackPackSystem.DropItem(InPawn, v.ItemID, v.Count, true);
end
end
end
---@param InPawn UGCPlayerPawn_C
function ItemTool.DropMeleeWeapon(InPawn, IsAll)
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, ESurviveWeaponPropSlot.SWPS_MeleeWeapon)
if not UE.IsValid(Weapon) then return end
UGCBackPackSystem.DropItem(InPawn, Weapon:GetWeaponItemID(), 1, true);
if not IsAll then return end
-- 掉落
local AllItems = UGCBackPackSystem.GetAllItemData(InPawn)
if table.isEmpty(AllItems) then return end
for i, v in pairs(AllItems) do
if GetWeaponIdType(v.ItemID) then UGCBackPackSystem.DropItem(InPawn, v.ItemID, v.Count, true); end
end
end
---@param InPawn UGCPlayerPawn_C
---@param InWeaponId int32
function ItemTool.ReplaceSameTypeWeapon(InPawn, InWeaponId)
UGCPawnSystem.LeavePawnState(InPawn, EPawnState.GunReload);
local NextWeaponIdType = GetWeaponIdType(InWeaponId)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if UE.IsValid(Weapon) then
local WeaponId = Weapon:GetWeaponID()
local IdType = GetWeaponIdType(WeaponId);
if NextWeaponIdType == IdType then
ItemTool.ClearWeapon(InPawn, Weapon);
ItemTool.AddWeaponItem(InPawn, InWeaponId, 1, true, InPawn:GetInfiniteType())
return
end
end
end
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param InWeaponId int32 武器 ID
---@param IsDestroy bool 是否销毁
---@param bPart bool 是否要移除配件
function ItemTool.DropWeaponById(InPawn, InWeaponId, IsDestroy, bPart, all)
-- 检查是否有配件,如果有配件,也一并掉到地上
local Weapons = ItemTool.GetWeaponsById(InPawn, InWeaponId);
for i, Weapon in pairs(Weapons) do
if UE.IsValid(Weapon) then
ItemTool.DropWeaponByWeapon(Weapon, IsDestroy, bPart);
if not all then return ; end
end
end
end
--- 掉落武器和配件
---@param InWeapon ASTExtraWeapon
---@param IsDestroy bool
---@param bPart bool
function ItemTool.DropWeaponByWeapon(InWeapon, IsDestroy, bPart)
local Pawn = InWeapon:GetOwnerPawn();
if not UE.IsValid(Pawn) then return ; end
local Id = InWeapon:GetWeaponItemID();
local Parts = ItemTool.GetWeaponParts(InWeapon);
if table.isEmpty(Parts) or not bPart then
UGCBackPackSystem.DropItem(Pawn, Id, 1, IsDestroy);
return ;
end
for i, v in pairs(Parts) do
UGCBackPackSystem.DropItem(Pawn, v, 1, IsDestroy);
end
UGCBackPackSystem.DropItem(Pawn, Id, 1, IsDestroy);
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param InWeaponId int32 武器 ID
---@return ASTExtraShootWeapon* 射击武器
function ItemTool.GetWeaponsById(InPawn, InWeaponId)
local Weapons = {};
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if UE.IsValid(Weapon) then
if Weapon:GetWeaponItemID() == InWeaponId then
table.insert(Weapons, Weapon);
end
end
end
return Weapons;
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param InWeaponId int32 武器 ID
---@param InCount int32 武器数量
---@param bAppend bool 是否需要添加配件
---@param FillBulletType EFillBulletType 是否需要添加子弹
function ItemTool.AddWeaponItem(InPawn, InWeaponId, InCount, bAppend, FillBulletType)
if not UE.IsValid(InPawn) then return end
local bHadAdd = false;
repeat bHadAdd = UGCBackPackSystem.AddItem(InPawn, InWeaponId, InCount) until bHadAdd;
local Weapon = ItemTool.GetLastWeapon(InPawn, InWeaponId);
if not bAppend then return Weapon end
ItemTool.AddWeaponParts(InPawn, InWeaponId, bAppend);
if FillBulletType ~= nil then
ItemTool.SetWeaponInfinite(InPawn, Weapon, InPawn:GetInfiniteType(), true);
end
return Weapon;
end
---@param ItemDefineID FItemDefineID
function ItemTool.AddWeaponParts(InPawn, ItemDefineID, Count)
if not UE.IsValidPawn(InPawn) then return ; end
local BackpackComponent = UGCBackPackSystem.GetBackpackComponent(InPawn);
-- 添加配件
local BattleItemUseTarget = {
TargetDefineID = ItemDefineID,
TargetAssociationName = "",
FilterTargetWhenPickup = true,
};
local WeaponItemId = ItemDefineID.TypeSpecificID;
-- 获取是否存有
local AccountInfo = UGCPlayerStateSystem.GetPlayerAccountInfo(InPawn.PlayerKey);
local ArchiveData = UGCPlayerStateSystem.GetPlayerArchiveData(AccountInfo.UID);
local Weapons = ArchiveData.Weapons
local AddSubitems = {};
if Weapons and Weapons[WeaponItemId] then AddSubitems = Weapons[WeaponItemId]; end
if table.isEmpty(AddSubitems) then
if WeaponSuits[WeaponItemId] then
local TheList = WeaponSuits[WeaponItemId][EWeaponPartType.Best];
if not table.isEmpty(TheList) then
2025-01-08 22:46:12 +08:00
AddSubitems = TheList[1];
2025-01-04 23:00:19 +08:00
end
end
end
if not table.isEmpty(AddSubitems) then
for i, subItemId in pairs(AddSubitems) do
local SubItemDefineID = BackpackUtils.GenerateItemDefineIDByItemTableIDWithRandomInstanceID(subItemId)
local EquipPickupInfo = CreateStruct("BattleItemPickupInfo")
EquipPickupInfo.Count = 1
EquipPickupInfo.bAutoEquip = (2 == SubItemDefineID.Type) or (8 == SubItemDefineID.Type)
EquipPickupInfo.AutoEquipTarget = BattleItemUseTarget
BackpackComponent:PickupInitialItem(SubItemDefineID, EquipPickupInfo, true)
end
end
end
--- 替换当前的武器
---@param InPawn UGCPlayerPawn_C 玩家
---@param InWeaponId int32 武器 ID
function ItemTool.ReplaceCurrentWeapon(InPawn, InWeaponId)
UGCPawnSystem.LeavePawnState(InPawn, EPawnState.GunReload);
ItemTool.DropCurrentWeapon(InPawn, true);
ItemTool.DropMeleeWeapon(InPawn, false);
UGCLogSystem.Log("[ItemTool.ReplaceCurrentWeapon] 执行 InWeaponId = %s", tostring(InWeaponId));
ItemTool.AddWeaponItem(InPawn, InWeaponId, 1, true, InPawn:GetInfiniteType());
end
---100000 - 199999 武器
---200000 - 299999 配件
---300000 - 399999 子弹
---400000 - 499999 衣服
---500000 - 599999 背包 / 护甲 / 头盔等
---600000 - 699999 补给品、投掷物等
---700000 - 799999 降落伞
---800000 - 899999 头盔、护甲皮肤
--- >= 8300000 自定义物品
--- 清除玩家身上所有的武器和配件
---@param InPawn UGCPlayerPawn_C 玩家
---@param bWeapon bool 是否移除武器
---@param bPart bool 是否移除配件
---@param bCustomItem bool 是否移除自定义物品
---@param bRecruit bool 是否移除补给品
function ItemTool.ClearAllWeaponsAndProp(InPawn, bWeapon, bPart, bBullet, bCustomItem, bRecruit)
local AllItem = UGCBackPackSystem.GetAllItemData(InPawn);
local func = function(v)
if v == nil then return true end
return v
end
bWeapon = func(bWeapon);
bPart = func(bPart);
bBullet = func(bBullet);
bCustomItem = func(bCustomItem);
bRecruit = func(bRecruit);
if AllItem then
for Index, Item in pairs(AllItem) do
if (bWeapon and Item.ItemID >= 100000 and Item.ItemID < 200000) -- 武器
or (bPart and Item.ItemID >= 200000 and Item.ItemID < 300000) -- 配件
or (bBullet and Item.ItemID >= 300000 and Item.ItemID < 400000) -- 子弹等
or (bCustomItem and Item.ItemID >= 8300000) -- 自定义的东西
or (bRecruit and Item.ItemID > 500000 and Item.ItemID < 700000) -- 补给品
then
UGCBackPackSystem.DropItem(InPawn, Item.ItemID, Item.Count, true)
end
end
end
end
--- 清除玩家手里的武器,如果 bHandTo1 表示如果手里没武器,那么清空第一把主武器
---@param InPawn UGCPlayerPawn_C
---@param bHandTo1 bool 是否当玩家手里没武器的时候清空第一把武器?
function ItemTool.ClearCurrentWeapon(InPawn, bHandTo1)
local Weapon = UGCWeaponManagerSystem.GetCurrentWeapon(InPawn)
if Weapon == nil and bHandTo1 then
Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, ESurviveWeaponPropSlot.SWPS_MainShootWeapon1);
if Weapon == nil then return end
end
ItemTool.ClearWeapon(InPawn, Weapon);
end
---@param Weapon ASTExtraWeapon
---@param InPawn UGCPlayerPawn_C
---@return table<int32, FItemDefineID>
function ItemTool.GetWeaponPartsDefineIDs(InPawn, Weapon)
local BackComp = UGCBackPackSystem.GetBackpackComponent(InPawn);
local InstanceIds = {};
local CustomList = {};
for i = 0, 5 do
local DefineID = Weapon:GetWeaponAttachmentIDBySocketType(i);
if DefineID.bValidInstance and DefineID.bValidItem then
InstanceIds[DefineID.InstanceID] = DefineID;
CustomList[i] = {
TypeSpecificID = DefineID.TypeSpecificID,
InstanceID = DefineID.InstanceID,
Type = DefineID.Type,
};
end
end
return InstanceIds, CustomList;
end
---@param Weapon ASTExtraWeapon
---@param InPawn UGCPlayerPawn_C
function ItemTool.ClearWeapon(InPawn, Weapon)
local BackComp = UGCBackPackSystem.GetBackpackComponent(InPawn);
local InstanceIds = ItemTool.GetWeaponPartsDefineIDs(InPawn, Weapon);
for i, v in pairs(InstanceIds) do
local bDrop = BackComp:DropItem(v, 1, EBattleItemDropReason.Force);
if not bDrop then
UGCLogSystem.Log("[ItemTool.ClearWeapon] 移除配件失败Weapon Id = %d, Part Id = %d", Weapon:GetWeaponItemID(), v.TypeSpecificID);
end
end
-- 寻找当前的武器子弹,再清除这些子弹
local WeaponInstanceID = Weapon:GetItemDefineID().InstanceID
-- 找到子弹
local AmmoId = WeaponAmmunitionItem[Weapon:GetWeaponItemID()];
if type(AmmoId) ~= 'number' then return ; end
local Count = BackComp:GetItemCount(AmmoId)
if Count > 0 then
UGCBackPackSystem.DropItem(InPawn, AmmoId, Count, true);
UGCLogSystem.Log("[ItemTool.ClearWeapon] 移除 %d 子弹 %d 个", AmmoId, Count)
end
UGCLogSystem.Log("[ItemTool.ClearWeapon] 移除 武器 %d", Weapon:GetWeaponItemID())
UGCBackPackSystem.DropItemByInstanceID(InPawn, WeaponInstanceID, 1, true);
end
--- 清空背包中的所有配件和子弹
function ItemTool.OnlyClearParts(InPawn)
local Comp = UGCBackPackSystem.GetBackpackComponent(InPawn)
local AllData = UGCBackPackSystem.GetAllItemData(InPawn)
for i, v in pairs(AllData) do
if GetCustomItemType(v.ItemID) == ECustomItemType.Bullet or
GetCustomItemType(v.ItemID) == ECustomItemType.Part
then
UGCBackPackSystem.DropItemByInstanceID(InPawn, v.InstanceID, v.Count, true);
end
end
end
--- 获取武器的所有配件
---@param InWeapon ASTExtraWeapon 武器
---@return table<int32, int32>
function ItemTool.GetWeaponParts(InWeapon)
local Table = {};
if ItemTool.IsShootWeapon(InWeapon) then
if InWeapon.AttachedAttachmentID then
for i, v in pairs(InWeapon.AttachedAttachmentID) do
if not table.hasValue(Table, v) then Table[#Table + 1] = v; end
end
end
end
return Table;
end
ItemTool.ShootWeaponClass = nil;
function ItemTool.GetShootWeaponClass()
if ItemTool.ShootWeaponClass == nil then
ItemTool.ShootWeaponClass = ScriptGameplayStatics.FindClass("STExtraShootWeapon");
end
return ItemTool.ShootWeaponClass;
end
--- 是否是射击枪械类
---@param InWeapon ASTExtraWeapon 武器
---@return bool 是否是射击枪械类
function ItemTool.IsShootWeapon(InWeapon)
if InWeapon == nil or not UE.IsValid(InWeapon) then return false end
return UE.IsA(InWeapon, ItemTool.GetShootWeaponClass());
end
---@param InPawn UGCPlayerPawn_C 玩家
---@param IsAll bool 是否所有武器都需要 Reload如果不是默认手中拿的或者第一把枪
function ItemTool.Reload(InPawn, IsAll)
if UE_SERVER then
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if UE.IsValid(Weapon) and ItemTool.IsShootWeapon(Weapon) then
ItemTool.SetWeaponInfinite(InPawn, Weapon, EFillBulletType.Fill);
if not IsAll then return end
end
end
else
local Weapon = UGCWeaponManagerSystem.GetCurrentWeapon(InPawn)
if UE.IsValid(Weapon) and ItemTool.IsShootWeapon(Weapon) then Weapon:Reload(); end
end
end
---@param InPawn UGCPlayerPawn_C
---@param InFunc fun(Weapon:ASTExtraWeapon)
function ItemTool.ForeachWeapon(InPawn, InFunc)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if UE.IsValid(Weapon) then InFunc(Weapon); end
end
end
function ItemTool.ForeachShootWeapons(InPawn, InFunc)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if ItemTool.IsShootWeapon(Weapon) then InFunc(Weapon); end
end
end
---@param InWeaponId int32
---@return bool 是否是射击武器
function ItemTool.IsShootWeaponById(InWeaponId)
return GetWeaponIdType(InWeaponId) ~= EWeaponIdType.Melee;
end
---@param InWeaponId int32
---@return bool 是否是近战武器
function ItemTool.IsMeleeWeaponById(InWeaponId)
return GetWeaponIdType(InWeaponId) == EWeaponIdType.Melee;
end
----------------------------------- 普通物品 -----------------------------------
---@return FItemDefineID
function ItemTool.AddItem(InPawn, InItemId, InCount, ...)
if not ItemUtils.IsUGCItem(InItemId) then return ; end
if not UE.IsValidPawn(InPawn) then return ; end
---@type FItemDefineID
local ItemDefineID = BackpackUtils.GenerateItemDefineIDByItemTableIDWithRandomInstanceID(InItemId);
local ItemInfo = CreateStruct("BattleItemPickupInfo")
ItemInfo.bAutoEquip = (2 == ItemDefineID.Type) or (8 == ItemDefineID.Type)
ItemInfo.Count = InCount;
local BackpackComponent = UGCBackPackSystem.GetBackpackComponent(InPawn);
BackpackComponent:RegisterItemGenerated(ItemDefineID.TypeSpecificID, ItemInfo.Count, ItemDefineID.InstanceID)
BackpackComponent:PickupItem(ItemDefineID, ItemInfo, EBattleItemPickupReason.Initial)
local Params = { ... };
if table.isEmpty(Params) then return ItemDefineID; end
local Weapon = ItemTool.GetWeaponByInstanceId(InPawn, ItemDefineID.InstanceID);
UGCLogSystem.Log("[ItemTool.AddItem] 111")
if Weapon == nil or not ItemTool.IsShootWeapon(Weapon) then return ItemDefineID; end
UGCLogSystem.Log("[ItemTool.AddItem] Weapon = %s", UE.GetName(Weapon));
if type(Params[1]) == 'boolean' and type(Params[2]) == 'number' then
local bAppend = Params[1];
local FillBulletType = Params[2];
-- 那说明要添加进去
if ItemTool.IsShootWeapon(Weapon) then
ItemTool.AddWeaponParts(InPawn, ItemDefineID, bAppend);
ItemTool.SetWeaponInfinite(InPawn, Weapon, FillBulletType, true);
end
elseif type(Params[1]) == 'int32' then
local FillBulletType = Params[1];
ItemTool.SetWeaponInfinite(InPawn, Weapon, FillBulletType, true);
end
end
function ItemTool.HasSameItem(InPawn, InItemId)
local AllItems = UGCBackPackSystem.GetAllItemData(InPawn);
for i, v in pairs(AllItems) do
if v.ItemID == InItemId then return true, v.InstanceID; end
end
return false, nil;
end
function ItemTool.GetItemInstanceId(InPawn, InItemId, Last)
local AllItems = UGCBackPackSystem.GetAllItemData(InPawn);
local InstanceIds = {};
for i, v in pairs(AllItems) do
if v.ItemID == InItemId then table.insert(InstanceIds, v.InstanceID) end
end
if #InstanceIds == 1 then return InstanceIds[1]; end
if Last then
local InstanceId = -1;
for i, v in pairs(InstanceIds) do
if v > InstanceId then InstanceId = v; end
end
return InstanceId;
end
return InstanceIds[1];
end
function ItemTool.GetWeaponItemInstanceId(InPawn, InWeaponId, Last)
local InstanceIds = {};
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
if Weapon then
if Weapon:GetWeaponItemID() == InWeaponId then
table.insert(InstanceIds, Weapon:GetItemDefineID().InstanceID);
end
end
end
if #InstanceIds == 1 then return InstanceIds[1]; end
if Last then
local InstanceId = -1;
for i, v in pairs(InstanceIds) do
if v > InstanceId then InstanceId = v; end
end
return InstanceId;
end
return InstanceIds[1];
end
--- 获取最后获得到的武器
function ItemTool.GetLastWeapon(InPawn, InWeaponId)
local WeaponRet = nil;
local InstanceId = 0;
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
if Weapon then
local TheId = Weapon:GetItemDefineID().InstanceID;
local ItemId = Weapon:GetWeaponItemID();
if (InWeaponId == nil) or (InWeaponId == ItemId) then
if TheId > InstanceId then
InstanceId = TheId;
WeaponRet = Weapon;
end
end
end
end
return WeaponRet;
end
----------------------------------- 护甲 -----------------------------------
function ItemTool.ReplaceItem(InPawn, InItemId)
local Items = UGCBackPackSystem.GetAllItemData(InPawn)
local TheId = GetItemIdType(InItemId)
for i, v in pairs(Items) do
if GetCustomItemType(v.ItemID) == ECustomItemType.Equipment and GetItemIdType(v.ItemID) == TheId then
UGCBackPackSystem.DropItem(InPawn, v.ItemID, v.Count, true);
end
end
UGCBackPackSystem.AddItem(InPawn, InItemId, 1);
end
---@param InPawn UGCPlayerPawn_C
function ItemTool.RemoveArmorItem(InPawn)
local Item = UGCBackPackSystem.GetConsumablesInBackpack(InPawn);
for i, v in pairs(Item) do
local ItemTable = UE.ToTable(v)
UGCLogSystem.LogTree(string.format("[ItemTool.RemoveArmorItem] ItemTable ="), ItemTable)
end
end
---@param InPawn UGCPlayerPawn_C
---@return table<int32, ESurviveWeaponPropSlot>
function ItemTool.PawnWeaponSlots(InPawn)
local Slots = {};
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v);
if Weapon then table.insert(Slots, v); end
end
return Slots;
end
function ItemTool.GetWeaponType(InWeaponId)
return InWeaponId // 1000;
end
function ItemTool.CheckPlayerHasWeapon(InPawn, InWeaponId)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
if Weapon then
if Weapon:GetWeaponItemID() == InWeaponId then return true, v; end
end
end
return false, nil;
end
---@param InPawn UGCPlayerPawn_C
---@param InItemId int32
function ItemTool.CheckPawnHasItemId(InPawn, InItemId)
local AllItemDatas = UGCBackPackSystem.GetAllItemData(InPawn);
for i, v in pairs(AllItemDatas) do
if v.ItemID == InItemId then return true, v.Count; end
end
return false, 0;
end
--- 通过 InstanceID 获取武器实例
function ItemTool.GetWeaponByInstanceId(InPawn, InstanceId)
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
if Weapon then
if Weapon:GetItemDefineID().InstanceID == InstanceId then
return Weapon;
end
end
end
return nil;
end
function ItemTool.ClearAllItems(InPawn)
local Comp = UGCBackPackSystem.GetBackpackComponent(InPawn)
Comp:ClearItems();
end
function ItemTool.GetAllWeaponPartDefineIDs(InPawn)
local Ret = {};
for i, v in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, v)
if Weapon then
local DefineIDs = ItemTool.GetWeaponPartsDefineIDs(InPawn, Weapon);
for c, d in pairs(DefineIDs) do
Ret[c] = d;
end
end
end
return Ret;
end
---@param InPawn UGCPlayerPawn_C
---@param InList table<int32, ECustomItemType>
---@param bWeaponParts bool 是否清除武器上的配件
function ItemTool.ClearItemsByCustomItemType(InPawn, InList, bWeaponParts)
-- 找到武器上面的
local Items = UGCBackPackSystem.GetAllItemData(InPawn)
if InList == nil then
InList = {
ECustomItemType.Bullet,
ECustomItemType.Part,
}
end
local BaiMingDan = {};
if bWeaponParts and table.hasValue(InList, ECustomItemType.Part) then
BaiMingDan = ItemTool.GetAllWeaponPartDefineIDs(InPawn);
UGCLogSystem.LogTree(string.format("[ItemTool.ClearItemsByCustomItemType] BaiMingDan ="), BaiMingDan)
end
local Func = function(InType)
for i, v in pairs(InList) do
if v == InType then return true; end
end
return false;
end
for i, v in pairs(Items) do
local ItemType = GetCustomItemType(v.ItemID)
if Func(ItemType) and BaiMingDan[v.InstanceID] == nil then
local bSuccess, Times = false, 10;
repeat
bSuccess = UGCBackPackSystem.DropItemByInstanceID(InPawn, v.InstanceID, v.Count, true)
Times = Times - 1;
until bSuccess or Times <= 0;
end
end
end
--- 添加所有的握把
function ItemTool.AddAllGrip(InPawn, WithoutList)
local WithoutTable = {};
if not table.isEmpty(WithoutList) then
for i, v in pairs(WithoutList) do
WithoutTable[v] = true;
end
end
for i, v in pairs(WeaponTypeParts[EWeaponPartType.Grip]) do
if WithoutTable[v] == nil then
ItemTool.AddItem(InPawn, v, 1);
end
end
end
--- 添加枪口让玩家装备
function ItemTool.AddAllMuzzle(InPawn, InWeaponId, WithoutList)
-- 找到 WeaponSuits
if table.isEmpty(WeaponSuits[InWeaponId]) then return ; end
local Muzzles = WeaponSuits[InWeaponId][EWeaponPartType.Muzzle]
if table.isEmpty(Muzzles) then return ; end
local WithoutTable = {};
if not table.isEmpty(WithoutList) then
for i, v in pairs(WithoutList) do
WithoutTable[v] = true;
end
end
for i, v in pairs(Muzzles) do
if WithoutTable[v] == nil then
ItemTool.AddItem(InPawn, v, 1);
end
end
end
---@param InWeaponPartType EWeaponPartType
function ItemTool.GetWeaponAttachmentSocketType(InWeaponPartType)
return WeaponTypeName[InWeaponPartType].WeaponAttachmentSocketType;
end
---@param Weapon ASTExtraWeapon
---@param InType EWeaponAttachmentSocketType
---@return FItemDefineID
function ItemTool.GetDefineIDBySocketType(Weapon, InType)
local DefineId = Weapon:GetWeaponAttachmentIDBySocketType(InType)
if DefineId.bValidItem and DefineId.bValidInstance then
return DefineId;
end
return nil;
end
---@param InWeapon ASTExtraShootWeapon
---@param InPartId int32
function ItemTool.WeaponAddPart(InWeapon, InPartId)
InWeapon:RPC_ServerEquipAttachment(InPartId);
end
function ItemTool.AddRangeItems(InPawn, Begin, End)
for i = Begin, End do
if UGCItemSystem.IsUGCItem(i) then
local Times = 10;
local bSuccess = false;
repeat
bSuccess = UGCBackPackSystem.AddItem(InPawn, i, 1);
until bSuccess or Times <= 0;
end
end
end
---@param InItemId int32
---@return FBattleItem_TabRes
function ItemTool.GetItemData(InItemId)
if not UGCItemSystem.IsUGCItem(InItemId) then
UGCLogSystem.Log("[ItemTool.GetItemName] 当前物品:%s 不是和平内置的物品,请检查", InItemId);
return nil;
end
return UGCItemSystem.GetItemData(InItemId);
end
---@param InItemId int32
---@return string 物品名称
function ItemTool.GetItemName(InItemId)
local ItemData = ItemTool.GetItemData(InItemId);
if ItemData then return ItemData.ItemName; end
return "";
end
--- 获取武器类型
function ItemTool.GetWeaponTypes(InPawn)
local Ret = {};
for i, Slot in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(InPawn, Slot);
if UE.IsValid(Weapon) then
local ItemId = Weapon:GetWeaponItemID()
Ret[ItemId] = WeaponTable[ItemId].TypeNew;
end
end
return Ret;
end
--- 根据武器类型添加子弹
---@param Pawn UGCPlayerPawn_C
function ItemTool.AddAmmoByType(Pawn)
local AddAmmo = {};
for i, Slot in pairs(ShootWeaponEnums) do
local Weapon = UGCWeaponManagerSystem.GetWeaponBySlot(Pawn, Slot);
if Weapon ~= nil and ItemTool.IsShootWeapon(Weapon) then
local AmmoID = Weapon:GetCurrentUsingAmmoID();
local WeaponId = Weapon:GetWeaponItemID();
AddAmmo[AmmoID] = (AddAmmo[AmmoID] or 0) + (WeaponTypeInfo[WeaponTable[WeaponId].TypeNew].Ammo or 0);
end
end
for ItemId, Count in pairs(AddAmmo) do
ItemTool.AddItem(Pawn, ItemId, Count);
end
end