对象池修改

This commit is contained in:
wuwenbo 2026-04-24 18:22:21 +08:00
parent ce17970f6e
commit 5cc43bd155
26 changed files with 276 additions and 76 deletions

View File

@ -16,6 +16,7 @@ using Logic;
using Logic.Action;
using Logic.AI;
using Logic.CrashSight;
using Logic.Pool;
using Logic.Skill;
using MemoryPack;
using TH1_Core.Events;
@ -434,7 +435,7 @@ namespace RuntimeData
// 通过玩家 ID 查找首都城市 cityData
public bool GetCapitalCityDataByPlayerId(uint pid, out CityData cityData)
{
var cityList = new List<CityData>();
using var pooledCityList = THCollectionPool.GetListHandle<CityData>(out var cityList);
GetCityDataListByPlayerId(pid, cityList);
foreach (var city in cityList)
{
@ -467,7 +468,7 @@ namespace RuntimeData
CollectManager.Instance.PlayerGameEndCollect(this, oldPlayerData, newPlayerData);
//step #2 所有unit都销毁
var unitDataList = new List<UnitData>();
using var pooledUnitDataList = THCollectionPool.GetListHandle<UnitData>(out var unitDataList);
GetUnitDataListByCityId(cid,unitDataList);
foreach (var unitData in unitDataList)
unitData.Renderer(this)?.Die();
@ -502,7 +503,7 @@ namespace RuntimeData
}
}
//开始将所有cid的小兵都转移到oldPlayerCapitalCityData下去
var tmpUnitDataList = new List<UnitData>();
using var pooledTmpUnitDataList = THCollectionPool.GetListHandle<UnitData>(out var tmpUnitDataList);
GetUnitDataListByCityId(cid, tmpUnitDataList);
foreach(var tmpUnitData in tmpUnitDataList)
SetUnitIdToCityId(tmpUnitData.Id,oldPlayerCapitalCityData.Id);
@ -1588,9 +1589,7 @@ namespace RuntimeData
gameRecord.Score = PlayerMap.SelfPlayerData.PlayerScore;
DateTime now = DateTime.Now;
gameRecord.Time = now.ToString("yyyy.MM.dd HH:mm");
var cityList = new List<CityData>();
GetCityDataListByPlayerId(PlayerMap.SelfPlayerId, cityList);
gameRecord.CityCount = (uint)cityList.Count;
gameRecord.CityCount = (uint)GetCityCount(PlayerMap.SelfPlayerId);
gameRecord.MapWidth = MapConfig.Width;
gameRecord.MapHeight = MapConfig.Height;
gameRecord.PlayerCount = MapConfig.PlayerCount;

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using Logic.Action;
using Logic.AI;
using Logic.Audio;
using Logic.Pool;
using MemoryPack;
using NUnit.Framework;
using TH1_Core.Events;
@ -1023,7 +1024,7 @@ namespace RuntimeData
public void Surrender(MapData map)
{
IsSurrender = true;
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(Id, selfUnits);
// 先播放Fog特效并销毁渲染层再清除数据层
foreach (var unit in selfUnits)
@ -1569,7 +1570,7 @@ namespace RuntimeData
_tmpGridListBuf ??= new List<GridData>();
_tmpGridListBuf.Clear();
map.GridMap.GetAroundGridData(1, 1, cityGrid, _tmpGridListBuf);
var randomList = new List<GridData>();
using var pooledRandomList = THCollectionPool.GetListHandle<GridData>(out var randomList);
foreach (var grid in _tmpGridListBuf)
{
if (grid == cityGrid) continue;

View File

@ -10,6 +10,7 @@ using System;
using System.Collections.Generic;
using Logic.Action;
using Logic.CrashSight;
using Logic.Pool;
using MemoryPack;
using OPS.Obfuscator.Attribute;
using RuntimeData;
@ -2434,7 +2435,7 @@ namespace Logic.AI
public void CheckIsActionInPlayerSight()
{
IsInSight = true;
var checkGridList = new List<GridData>();
using var pooledCheckGridList = THCollectionPool.GetListHandle<GridData>(out var checkGridList);
if (Param.MapData.GridMap.GetGridDataByGid(Param.GridId, out var targetGrid)) checkGridList.Add(targetGrid);
if (Param.MapData.GetGridDataByUnitId(Param.UnitId, out var unitGrid) ) checkGridList.Add(unitGrid);
if (Param.MapData.GetGridDataByUnitId(Param.TargetUnitId, out var targetUnit)) checkGridList.Add(targetUnit);

View File

@ -9,6 +9,7 @@
using System.Collections.Generic;
using Logic.Action;
using Logic.CrashSight;
using Logic.Pool;
using RuntimeData;
using TH1_Logic.Action;
using TH1_Logic.Core;
@ -279,9 +280,9 @@ namespace Logic.AI
var data = new AICalculatorData();
data.Map = map;
data.Player = selfPlayer;
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(selfPlayer.Id, selfUnits);
var selfCities = new HashSet<CityData>();
using var pooledSelfCities = THCollectionPool.GetHashSetHandle<CityData>(out var selfCities);
map.GetCityDataListByPlayerId(selfPlayer.Id, selfCities);
data.TargetParam.MapData = map;
@ -343,9 +344,9 @@ namespace Logic.AI
var data = new AICalculatorData();
data.Map = map;
data.Player = selfPlayer;
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(selfPlayer.Id, selfUnits);
var selfCities = new HashSet<CityData>();
using var pooledSelfCities = THCollectionPool.GetHashSetHandle<CityData>(out var selfCities);
map.GetCityDataListByPlayerId(selfPlayer.Id, selfCities);
data.TargetParam.MapData = map;
@ -731,4 +732,4 @@ namespace Logic.AI
}
}
}
}
}

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using Logic.Action;
using Logic.CrashSight;
using Logic.Pool;
using TH1_Logic.Core;
using UnityEngine;
using Debug = UnityEngine.Debug;
@ -764,7 +765,7 @@ namespace Logic.AI
{
//TODO Check PlayerData Right
if (aiAction.Param.GridData.VisibleUnit(aiAction.Param.MapData,aiAction.Param.PlayerData, out _)) return false;
var selfCity = new HashSet<CityData>();
using var pooledSelfCity = THCollectionPool.GetHashSetHandle<CityData>(out var selfCity);
aiAction.Param.MapData.GetCityDataListByPlayerId(aiAction.Param.PlayerData.Id, selfCity);
if (!selfCity.Contains(city)) return true;
}

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using Logic.Action;
using Logic.AI;
using Logic.CrashSight;
using Logic.Pool;
using MemoryPack;
using RuntimeData;
using TH1_Logic.Core;
@ -36,13 +37,13 @@ namespace TH1_Logic.AITrain
// 2 + 40 + 400 + 160 + 200 = 802 维度
public float[] GetMapState(MapData map, PlayerData selfPlayer)
{
List<float> state = new();
using var pooledState = THCollectionPool.GetListHandle<float>(out var state);
// 1. 己方信息
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(selfPlayer.Id, selfUnits);
var selfCities = new HashSet<CityData>();
using var pooledSelfCities = THCollectionPool.GetHashSetHandle<CityData>(out var selfCities);
map.GetCityDataListByPlayerId(selfPlayer.Id, selfCities);
var maxScore = map.PlayerMap.GetMaxPlayerScore();
@ -51,8 +52,8 @@ namespace TH1_Logic.AITrain
// 我的 Player Index
state.Add(GetPlayerIndex(map, selfPlayer.Id) / 10f);
var units = new List<UnitData>();
var cities = new List<CityData>();
using var pooledUnits = THCollectionPool.GetListHandle<UnitData>(out var units);
using var pooledCities = THCollectionPool.GetListHandle<CityData>(out var cities);
foreach (var id in selfPlayer.Sight.SightGidSet)
{
//TODO 白哉确认
@ -376,7 +377,7 @@ namespace TH1_Logic.AITrain
private float GetUnitsScore(MapData mapData, PlayerData playerData)
{
float score = 0f;
var units = new HashSet<UnitData>();
using var pooledUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var units);
mapData.GetUnitDataListByPlayerId(playerData.Id, units);
foreach (var unit in mapData.UnitMap.UnitList)
{
@ -392,7 +393,7 @@ namespace TH1_Logic.AITrain
private float GetCityScore(MapData mapData, PlayerData playerData)
{
float score = 0f;
var cities = new HashSet<CityData>();
using var pooledCities = THCollectionPool.GetHashSetHandle<CityData>(out var cities);
mapData.GetCityDataListByPlayerId(playerData.Id, cities);
foreach (var city in mapData.CityMap.CityList)
{
@ -538,4 +539,4 @@ namespace TH1_Logic.AITrain
return true;
}
}
}
}

View File

@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using UnityEngine;
@ -661,7 +662,7 @@ namespace Logic.Achievement
map.GridMap.GetAroundGridData(OffsetX, OffsetY, gridData, _aroundBuf);
_recordCount = 0;
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(map.PlayerMap.SelfPlayerId, selfUnits);
foreach (var aroundGrid in _aroundBuf)
{
@ -747,7 +748,7 @@ namespace Logic.Achievement
map.GridMap.GetAroundGridData(OffsetX, OffsetY, gridData, _aroundBuf);
_recordCount = 0;
var selfUnits = new HashSet<UnitData>();
using var pooledSelfUnits = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnits);
map.GetUnitDataListByPlayerId(map.PlayerMap.SelfPlayerId, selfUnits);
foreach (var aroundGrid in _aroundBuf)
{
@ -867,4 +868,4 @@ namespace Logic.Achievement
return false;
}
}
}
}

View File

@ -1214,7 +1214,6 @@ namespace Logic.Action
&& _actionId.ActionType != CommonActionType.TurnStart
&& _actionId.ActionType != CommonActionType.AIParamControl)
{
LogSystem.LogWarning($"{Time.time}");
foreach (var pData in actionParams.MapData.PlayerMap.PlayerDataList)
{
pData.RefreshFeelingValue(actionParams.MapData);
@ -1231,9 +1230,10 @@ namespace Logic.Action
EventManager.Publish(new UpdateUITopTopBar() { UpdateType = UpdateTopBarType.UpdateCulturePerTurn });
EventManager.Publish(new UpdateUITopTopBar() { UpdateType = UpdateTopBarType.UpdateFaith });
}
Main.PlayerLogic.Update(actionParams.MapData);
if (actionParams.MapData == Main.MapData)
{
// AI 演算不调用 PlayerLogic.Update节省性能
Main.PlayerLogic.Update(actionParams.MapData);
MatchSettlementLogicFactory.RefreshMatchSettlementInfo(actionParams.MapData);
foreach (var item in actionParams.PlayerData.MomentData.Items)
{

View File

@ -1,6 +1,7 @@
using System;
using System;
using System.Collections.Generic;
using Logic.CrashSight;
using Logic.Pool;
using MongoDB.Driver.Core.Misc;
using ParadoxNotion;
using UnityEngine;
@ -357,7 +358,7 @@ namespace Logic
Main.PlayerLogic.UpdateSight_LogicView(mapData,playerData,mapData.GridMap.GetAroundGridIdList(2,gridData));
var newTerritoryArea = new List<uint>();
using var pooledNewTerritoryArea = THCollectionPool.GetListHandle<uint>(out var newTerritoryArea);
foreach (var aroundGrid in _aroundBuf)
{
@ -545,7 +546,7 @@ namespace Logic
int x = gridData.Pos.X;
int y = gridData.Pos.Y;
bool ret = false;
HashSet<int> haveType = new HashSet<int>();
using var pooledHaveType = THCollectionPool.GetHashSetHandle<int>(out var haveType);
for(int i = 0;i<=1;i++)
for (int j = 0; j <= 1; j++)
{

View File

@ -10,6 +10,7 @@ using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using Logic.Pool;
using RuntimeData;
using TH1_Logic.Core;
using Unity.VisualScripting;
@ -1140,14 +1141,14 @@ namespace Logic
if (leyLineCount <= 0) return;
// Step #2 构建首都排除区仅首都±1范围非首都村庄不排除
var cradleExclude = new HashSet<uint>();
using var pooledCradleExclude = THCollectionPool.GetHashSetHandle<uint>(out var cradleExclude);
foreach (var cradle in PlayerCivOri)
for (int x = cradle.X - 1; x <= cradle.X + 1; x++)
for (int y = cradle.Y - 1; y <= cradle.Y + 1; y++)
cradleExclude.Add(MapPosition.CalculatePosId(x, y));
// Step #3 收集候选格子:陆地 + 非CityCenter + 不在首都1格范围内
var candidates = new List<GridData>();
using var pooledCandidates = THCollectionPool.GetListHandle<GridData>(out var candidates);
foreach (var gridData in mapData.GridMap.GridList)
{
if (gridData.Terrain != TerrainType.Land) continue;
@ -1207,4 +1208,4 @@ namespace Logic
}
}
}

View File

@ -6,6 +6,7 @@ using Logic.Action;
using Logic.Audio;
using Logic.Config;
using Logic.CrashSight;
using Logic.Pool;
using Logic.Skill;
using TH1_Anim;
using TH1_Anim.Fragments;
@ -485,7 +486,7 @@ namespace Logic
//Step #6 处理占领城市的特效(所有领土会升起一面旗帜
HashSet<uint> gridList = new HashSet<uint>();
using var pooledGridList = THCollectionPool.GetHashSetHandle<uint>(out var gridList);
cityData.Territory.GetAllTerritoryArea(gridList);
foreach (var gd in gridList)
{
@ -510,7 +511,8 @@ namespace Logic
foreach (var tg in _tmpAroundBuf)
tg.Renderer(mapData)?.InstantUpdateGrid(true);
List<uint> list = new List<uint>(cityData.Territory.TerritoryArea);
using var pooledTerritoryList = THCollectionPool.GetListHandle<uint>(out var list);
list.AddRange(cityData.Territory.TerritoryArea);
//更新player的视野
Main.PlayerLogic.UpdateSight_LogicView(mapData, playerData, list);
@ -706,9 +708,9 @@ namespace Logic
//step #1 初始化将联通的城市存于Hashset中
if (playerData == null) return;
if (!mapData.GetCapitalCityDataByPlayerId(playerData.Id, out var capitalCity)) return;
var cityList = new List<CityData>();
using var pooledCityList = THCollectionPool.GetListHandle<CityData>(out var cityList);
mapData.GetCityDataListByPlayerId(playerData.Id, cityList);
var connectList = new HashSet<uint>();
using var pooledConnectList = THCollectionPool.GetHashSetHandle<uint>(out var connectList);
//Step #2 如果玩家还有原始首都,计算联通情况,否则所有城市都是不联通
if (playerData.CradleCityId == capitalCity.Id)
{
@ -731,7 +733,7 @@ namespace Logic
var citySet = mapData.GetCityDataSetByPlayerId(playerData.Id);
var gridSet = mapData.GetGridDataSetByPlayerId(playerData.Id);
//获取所有视野内的grid然后去除非盟友的有主海域去除所有土地这是所有港口可以计算最短路的格子用来计算通路用的
var waterRoadGidSet = new HashSet<uint>();
using var pooledWaterRoadGidSet = THCollectionPool.GetHashSetHandle<uint>(out var waterRoadGidSet);
foreach (var gid in playerData.Sight.SightGidSet)
if (mapData.GridMap.GetGridDataByGid(gid, out var grid) && grid.Terrain != TerrainType.Land)
{
@ -767,9 +769,9 @@ namespace Logic
//Debug.Log($"candidatePort is {portCandidate.Id}");
// 记录前驱节点
var prev = new Dictionary<uint, uint>();
var dist = new Dictionary<uint, int>();
var visited = new HashSet<uint>();
using var pooledPrev = THCollectionPool.GetDictionaryHandle<uint, uint>(out var prev);
using var pooledDist = THCollectionPool.GetDictionaryHandle<uint, int>(out var dist);
using var pooledVisited = THCollectionPool.GetHashSetHandle<uint>(out var visited);
// 初始港口格子
uint startId = portCandidate.Id;
@ -828,9 +830,9 @@ namespace Logic
//如果step步以内已经可达targetPort那么也不需要新增最短路
int step = 6;
var visitedSet = new HashSet<uint>();
var startSet = new HashSet<uint>();
var nextSet = new HashSet<uint>();
using var pooledVisitedSet = THCollectionPool.GetHashSetHandle<uint>(out var visitedSet);
using var pooledStartSet = THCollectionPool.GetHashSetHandle<uint>(out var startSet);
using var pooledNextSet = THCollectionPool.GetHashSetHandle<uint>(out var nextSet);
startSet.Add(portCandidate.Id);
while(step > 0)
{
@ -848,8 +850,10 @@ namespace Logic
nextSet.Add(tmpAroundGrid.Id);
}
var swap = startSet;
startSet = nextSet;
nextSet = new HashSet<uint>();
nextSet = swap;
nextSet.Clear();
step--;
}
@ -912,7 +916,7 @@ namespace Logic
//#1-5 从首都出发进行floodfill计算哪些城市可以被抵达。是否联通只需要判断两个格子是否都具有road属性即可(不过有些文明要特殊判断)
var cn = new HashSet<uint>();
using var pooledCn = THCollectionPool.GetHashSetHandle<uint>(out var cn);
//floodfill算一遍从首都出发的连通性
if (!mapData.GetCapitalCityDataByPlayerId(playerData.Id, out var capital)) return;
if (!mapData.GetGridDataByCityId(capital.Id, out var capitalGrid)) return;
@ -1110,7 +1114,11 @@ namespace Logic
if (p1.TechTree.CheckIfHasTech(TechType.Diplomacy))
{
if(map.GetGridIdByCityId(p2.CradleCityId, out var cradleGid))
UpdateSight_LogicView(map,p1,new List<uint>{cradleGid});
{
using var pooledGidList = THCollectionPool.GetListHandle<uint>(out var gidList);
gidList.Add(cradleGid);
UpdateSight_LogicView(map, p1, gidList);
}
}
//step #5
@ -1219,7 +1227,11 @@ namespace Logic
if (!atomInfo.IsAddSkill) continue;
//是否给全员增加,还是有条件增加
bool forAll = atomInfo.AddSkillCondition.Count == 0;
var condition = new HashSet<UnitFullType>(atomInfo.AddSkillCondition);
using var pooledCondition = THCollectionPool.GetHashSetHandle<UnitFullType>(out var condition);
if (!forAll)
{
condition.UnionWith(atomInfo.AddSkillCondition);
}
foreach (var unit in mapData.UnitMap.UnitList)
{
if (!mapData.CheckUnitIdBelongPlayerId(unit.Id, playerData.Id)) continue;
@ -1545,7 +1557,7 @@ namespace Logic
public void DebugGetAllSight(MapData mapData, PlayerData playerData)
{
var gidList = new List<uint>();
using var pooledGidList = THCollectionPool.GetListHandle<uint>(out var gidList);
foreach(var gridData in mapData.GridMap.GridList)
gidList.Add(gridData.Id);
UpdateSight_LogicView(mapData,playerData,gidList);
@ -1563,7 +1575,9 @@ namespace Logic
if (player.Id == playerId) continue;
if (!map.PlayerMap.GetPlayerDataByPlayerID(playerId, out var player2)) continue;
if (!map.GetGridDataByCityId(player2.CradleCityId, out var grid)) continue;
UpdateSight_LogicView(map, player, new List<uint> { grid.Id });
using var pooledGidList = THCollectionPool.GetListHandle<uint>(out var gidList);
gidList.Add(grid.Id);
UpdateSight_LogicView(map, player, gidList);
}
if (count != player.MeetPlayers.Count) continue;

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 74a09dcb039c66b43b201022058a7491
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,148 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Logic.Pool
{
/// <summary>
/// Lightweight collection pool for temporary List/Dictionary/HashSet allocations.
/// </summary>
public static class THCollectionPool
{
private const int DefaultMaxPoolSizePerType = 256;
private static class TypePool<T> where T : class, new()
{
internal static readonly Stack<T> Items = new Stack<T>(32);
internal static readonly object Sync = new object();
internal static readonly Action<T> Reset = BuildResetAction();
private static Action<T> BuildResetAction()
{
var type = typeof(T);
if (typeof(IDictionary).IsAssignableFrom(type))
{
return instance => ((IDictionary)instance).Clear();
}
if (typeof(IList).IsAssignableFrom(type))
{
return instance => ((IList)instance).Clear();
}
var clearMethod = type.GetMethod("Clear", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null);
if (clearMethod == null)
{
return _ => { };
}
try
{
return (Action<T>)Delegate.CreateDelegate(typeof(Action<T>), null, clearMethod);
}
catch
{
return instance => clearMethod.Invoke(instance, null);
}
}
}
public static T Get<T>() where T : class, new()
{
var pool = TypePool<T>.Items;
lock (TypePool<T>.Sync)
{
if (pool.Count > 0)
{
return pool.Pop();
}
}
return new T();
}
public static void Release<T>(T instance) where T : class, new()
{
if (instance == null)
{
return;
}
TypePool<T>.Reset(instance);
var pool = TypePool<T>.Items;
lock (TypePool<T>.Sync)
{
if (pool.Count >= DefaultMaxPoolSizePerType)
{
return;
}
pool.Push(instance);
}
}
public static List<T> GetList<T>()
{
return Get<List<T>>();
}
public static Dictionary<TKey, TValue> GetDictionary<TKey, TValue>()
{
return Get<Dictionary<TKey, TValue>>();
}
public static HashSet<T> GetHashSet<T>()
{
return Get<HashSet<T>>();
}
public static void ReleaseList<T>(List<T> instance)
{
Release(instance);
}
public static void ReleaseDictionary<TKey, TValue>(Dictionary<TKey, TValue> instance)
{
Release(instance);
}
public static void ReleaseHashSet<T>(HashSet<T> instance)
{
Release(instance);
}
public static PooledHandle<List<T>> GetListHandle<T>(out List<T> instance)
{
instance = GetList<T>();
return new PooledHandle<List<T>>(instance);
}
public static PooledHandle<Dictionary<TKey, TValue>> GetDictionaryHandle<TKey, TValue>(out Dictionary<TKey, TValue> instance)
{
instance = GetDictionary<TKey, TValue>();
return new PooledHandle<Dictionary<TKey, TValue>>(instance);
}
public static PooledHandle<HashSet<T>> GetHashSetHandle<T>(out HashSet<T> instance)
{
instance = GetHashSet<T>();
return new PooledHandle<HashSet<T>>(instance);
}
}
public readonly struct PooledHandle<T> : IDisposable where T : class, new()
{
private readonly T _instance;
public PooledHandle(T instance)
{
_instance = instance;
}
public void Dispose()
{
THCollectionPool.Release(_instance);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bc6e12cd44d921444a16fd79b12c108d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,6 +7,7 @@
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using MemoryPack;
@ -33,7 +34,7 @@ namespace Logic.Skill
if (self == null) return;
var player = self.Player(mapData);
if (player == null) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(player.Id, selfUnitList);
if (!mapData.GetGridDataByUnitId(self.Id, out var targetGrid)) return;
var aroundBuf = RentAroundBuf();
@ -49,4 +50,4 @@ namespace Logic.Skill
ReturnAroundBuf();
}
}
}
}

View File

@ -7,6 +7,7 @@
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using Logic.CrashSight;
using TH1_Anim.Fragments;
@ -108,7 +109,7 @@ namespace Logic.Skill
List<SkillVisualStep> visualSteps = (isMainMap && scope == null) ? new List<SkillVisualStep>() : null;
// 收集需要刷新外观的存活单位
var unitsToRefresh = new List<uint>();
using var pooledUnitsToRefresh = THCollectionPool.GetListHandle<uint>(out var unitsToRefresh);
if (_level >= 2)
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using MemoryPack;
using TH1_Anim.Fragments;
@ -28,7 +29,7 @@ namespace Logic.Skill
if (attackInfo.DamageOrigin == null || attackInfo.DamageTargetGrid == null) return;
if (!mapData.GetPlayerDataByUnitId(attackInfo.DamageOrigin.Id, out var selfPlayer)) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(selfPlayer.Id, selfUnitList);
bool isMainMap = mapData == Main.MapData;

View File

@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using Logic.Pool;
using MemoryPack;
using RuntimeData;
using UnityEngine;
@ -55,7 +56,7 @@ namespace Logic.Skill
_moveFlag = true;
var player = self.Player(mapData);
if (player == null) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(player.Id, selfUnitList);
if (!mapData.GetGridDataByUnitId(self.Id, out var targetGrid)) return;

View File

@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using Logic.Pool;
using MemoryPack;
using RuntimeData;
using TH1_Logic.Core;
@ -39,10 +40,10 @@ namespace Logic.Skill
if (path == null) return;
int pathRk = 0;
//统计所有已经被伤害到的unit。每个unit只被伤害1次
var damaged = new HashSet<UnitData>();
using var pooledDamaged = THCollectionPool.GetHashSetHandle<UnitData>(out var damaged);
// 收集所有需要延迟执行的视觉更新。Renderer 必须在 DamageSettlement 前缓存。
var visualSteps = new List<(int pathIndex, UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled)>();
using var pooledVisualSteps = THCollectionPool.GetListHandle<(int pathIndex, UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled)>(out var visualSteps);
foreach (var vec2 in path)
{

View File

@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using Logic.Pool;
using MemoryPack;
using RuntimeData;
using UnityEngine;
@ -54,7 +55,7 @@ namespace Logic.Skill
if (moveType != MoveType.SkillMove) return;
var player = self.Player(mapData);
if (player == null) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(player.Id, selfUnitList);
if (!mapData.GetGridDataByUnitId(self.Id, out var targetGrid)) return;

View File

@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using Logic.Pool;
using MemoryPack;
using RuntimeData;
using UnityEngine;
@ -43,7 +44,7 @@ namespace Logic.Skill
if (moveType != MoveType.SkillMove) return;
var player = self.Player(mapData);
if (player == null) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(player.Id, selfUnitList);
if (!mapData.GetGridDataByUnitId(self.Id, out var targetGrid)) return;
@ -65,4 +66,4 @@ namespace Logic.Skill
if (isExcute) self.AddActionPoint(ActionPointType.Attack);
}
}
}
}

View File

@ -10,6 +10,7 @@ using System.Collections.Generic;
using RuntimeData;
using System;
using Logic.CrashSight;
using Logic.Pool;
using TH1_Logic.Core;
using TH1_Anim.Fragments;
using TH1_Core.Managers;
@ -42,7 +43,7 @@ namespace Logic.Skill
if (info.DamageOrigin == null )return;//|| info.DamageTarget == null) return;
if (!mapData.GetPlayerDataByUnitId(info.DamageOrigin.Id, out var player)) return;
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetUnitDataListByPlayerId(player.Id, selfUnitList);
if (info.DamageTargetGrid == null)
@ -53,7 +54,7 @@ namespace Logic.Skill
// 收集需要延迟刷新的单位。注意Renderer 必须在 DamageSettlement 前缓存,
// 否则死亡后 ROUnitMap 若被其他路径清理将无法再查到。
var unitsToRefresh = new List<(UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled)>();
using var pooledUnitsToRefresh = THCollectionPool.GetListHandle<(UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled)>(out var unitsToRefresh);
var aroundBuf = RentAroundBuf();
mapData.GridMap.GetAroundGridData(1, 1, info.DamageTargetGrid, aroundBuf);
@ -155,4 +156,4 @@ namespace Logic.Skill
}
}
}
}
}

View File

@ -9,6 +9,7 @@
using System.Collections.Generic;
using RuntimeData;
using System;
using Logic.Pool;
using TH1_Logic.Core;
using TH1_Anim.Fragments;
using TH1_Core.Managers;
@ -36,12 +37,12 @@ namespace Logic.Skill
public override void OnMove(UnitData self, GridData grid, MapData mapData, MoveType moveType, List<Vector2Int> path = null)
{
var selfUnitList = new HashSet<UnitData>();
using var pooledSelfUnitList = THCollectionPool.GetHashSetHandle<UnitData>(out var selfUnitList);
mapData.GetPlayerDataByUnitId(self.Id, out var selfPlayer);
mapData.GetUnitDataListByPlayerId(selfPlayer.Id, selfUnitList);
// 收集需要视觉更新的数据。Renderer 必须在 DamageSettlement 前缓存。
var visualUpdates = new List<(UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled, GridRenderer gridRenderer)>();
using var pooledVisualUpdates = THCollectionPool.GetListHandle<(UnitData unit, GridData grid, UnitRenderer renderer, int damage, bool wasKilled, GridRenderer gridRenderer)>(out var visualUpdates);
//遍历会溅射的所有格子
var aroundBuf = RentAroundBuf();
@ -146,4 +147,4 @@ namespace Logic.Skill
}
}
}
}
}

View File

@ -7,6 +7,7 @@
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using TH1_Anim;
using TH1_Anim.Fragments;
@ -117,7 +118,7 @@ namespace Logic.Skill
bool hasRadiation = selfUnit.GetSkill(SkillType.UtsuhoRadiation, out _);
if (hasRadiation)
{
var splashedUnits = new HashSet<uint>();
using var pooledSplashedUnits = THCollectionPool.GetHashSetHandle<uint>(out var splashedUnits);
for (int i = 1; i < path.Count; i++)
{
if (!mapData.GridMap.GetGridDataByV2(path[i], out var pathGrid)) continue;

View File

@ -6,6 +6,7 @@
*/
using System.Collections.Generic;
using Logic.Pool;
using RuntimeData;
using MemoryPack;
using TH1_Anim;
@ -80,7 +81,7 @@ namespace Logic.Skill
bool hasRadiation = selfUnit.GetSkill(SkillType.UtsuhoRadiation, out _);
if (hasRadiation)
{
var splashedUnits = new HashSet<uint>();
using var pooledSplashedUnits = THCollectionPool.GetHashSetHandle<uint>(out var splashedUnits);
for (int i = 1; i < path.Count; i++)
{
if (!mapData.GridMap.GetGridDataByV2(path[i], out var pathGrid)) continue;

View File

@ -14,6 +14,7 @@ using Animancer;
using Logic.Action;
using Logic.Audio;
using Logic.CrashSight;
using Logic.Pool;
using Logic.Skill;
using ParadoxNotion;
using UnityEngine;
@ -1157,7 +1158,7 @@ namespace Logic
&& cityAsPlayer.Id == playerData.Id)
{
//遍历所有我方城市 如果位置没有人且联通,就可以移动过去
var cityDataList = new List<CityData>();
using var pooledCityDataList = THCollectionPool.GetListHandle<CityData>(out var cityDataList);
mapData.GetCityDataListByPlayerId(playerData.Id, cityDataList);
foreach (var cityB in cityDataList)
{
@ -1174,7 +1175,7 @@ namespace Logic
if (unitData.GetSkill(SkillType.MORIYAKNIGHTMOVE,out var _) && unitData.Grid(mapData,out var curGrid) && (curGrid.Resource is ResourceType.MoriyaMilitary or ResourceType.CityCenter))
{
//遍历所有我方城市和军营 如果位置没有人就可以移动过去
var cityDataList = new List<CityData>();
using var pooledCityDataList = THCollectionPool.GetListHandle<CityData>(out var cityDataList);
mapData.GetCityDataListByPlayerId(playerData.Id, cityDataList);
foreach (var targetCity in cityDataList)
{
@ -1204,7 +1205,7 @@ namespace Logic
var sanaeWindXSkill = skill as SanaeWindXSkill;
if (sanaeWindXSkill != null)
{
var targetVec2 = new List<Vector2Int>();
using var pooledTargetVec2 = THCollectionPool.GetListHandle<Vector2Int>(out var targetVec2);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
@ -1419,7 +1420,7 @@ namespace Logic
}
var queue = new Queue<Vector2Int>();
// 用字典记录路径来源,同时兼作“已访问”集合
var cameFrom = new Dictionary<Vector2Int, Vector2Int>();
using var pooledCameFrom = THCollectionPool.GetDictionaryHandle<Vector2Int, Vector2Int>(out var cameFrom);
uint width = map.MapConfig.Width;
uint height = map.MapConfig.Height;
@ -1577,13 +1578,13 @@ namespace Logic
if (originInfo == null || targetInfo == null) return;
bool useCache = unitData.FullTypeCache == new UnitFullType(targetType, giantType, unitLevel);
var skillCache = new Dictionary<SkillType, SkillBase>();
var originSkills = new List<SkillBase>();
using var pooledSkillCache = THCollectionPool.GetDictionaryHandle<SkillType, SkillBase>(out var skillCache);
using var pooledOriginSkills = THCollectionPool.GetListHandle<SkillBase>(out var originSkills);
foreach (var t in unitData.Skills) originSkills.Add(t);
foreach (var t in unitData.SkillCache) skillCache[t.GetSkillType()] = t;
unitData.SkillCache.Clear();
var reservedSkillTypes = new HashSet<SkillType>();
using var pooledReservedSkillTypes = THCollectionPool.GetHashSetHandle<SkillType>(out var reservedSkillTypes);
foreach (var skill in originSkills)
{
// if (targetInfo.Skills.Contains(skillType)) continue;