using System; using System.Collections.Generic; using System.RandomPool; using Game; using UnityEngine; namespace Game.Data { /// /// 通用随机池,基于权重随机 /// public class CommonRandomPool where T : class { protected Dictionary> _randomGroups; protected LevelRandom _levelRandom; protected int _poolId; protected int _seedId; public LevelRandom LevelRandom => this._levelRandom; public int ValidRofCount { get { int count = 0; foreach (var randomGroupsValue in _randomGroups.Values) { count += randomGroupsValue.ValidRofCount; } return count; } } public CommonRandomPool(CommonPoolConfig config, int poolId, int seedId) { this._poolId = poolId; this._seedId = seedId; this._randomGroups = new Dictionary>(); foreach (var contentsKey in config.poolContents.Keys) { var nodes = config.poolContents[contentsKey]; CommonRandomGroup randomGroup = config.CreateGroup(this._poolId); foreach (var node in nodes) { randomGroup.PutItem(node); } this._randomGroups[contentsKey] = randomGroup; } foreach (var groupsValue in this._randomGroups.Values) { groupsValue?.SetMask(-1); } this._levelRandom = new LevelRandom(config.poolLevelWeight , new List(this._randomGroups.Keys)); } public void PutNode(RandomNode node , int quality) { if (_randomGroups.ContainsKey(quality)) { var commonRandomGroup = _randomGroups[quality]; commonRandomGroup.PutItem(node); commonRandomGroup.SetMask(-1); } } public RandomNode GetNode(int key , int quality) { if (_randomGroups.ContainsKey(quality)) { var commonRandomGroup = _randomGroups[quality]; return commonRandomGroup.GetItem(key); } return null; } public void ResetNodeWeight(int quality ,int key , int weight) { if (this._randomGroups.TryGetValue (quality, out var commonRandomGroup)) { commonRandomGroup.ResetItemWeight (key , weight); commonRandomGroup.SetMask (-1); } } public void ResetQualityWeight(int quality , int weight) { this._levelRandom.SetWeight (quality , weight); } public void PutNodes(List> nodes , int quality) { if (this._randomGroups.TryGetValue (quality, out var commonRandomGroup)) { foreach (var node in nodes) { commonRandomGroup.PutItem(node); } commonRandomGroup.SetMask(-1); } } //回收随机节点 public void PullNode(int key , int quality) { if (_randomGroups.ContainsKey(quality)) { var commonRandomGroup = _randomGroups[quality]; commonRandomGroup.PullItem(key); commonRandomGroup.SetMask(-1); } } public void RemoveNode(int key , int quality) { if (_randomGroups.ContainsKey(quality)) { var commonRandomGroup = _randomGroups[quality]; commonRandomGroup.RemoveItem(key); commonRandomGroup.SetMask(-1); } } public void RemoveNode(int key) { foreach (int itemQuality in Enum.GetValues(typeof(int))) { RemoveNode(key , itemQuality); } } public List RandomItems(int maskCount, int count, bool isSign = false , object customMask = null ) { List nodes = new List(); List disableList = new List(); for (int i = 0; i < count; i++) { var node = this.RandomNode(maskCount, disableList , customMask); if (node != null) { nodes.Add(node.value); if (isSign) { disableList.Add(node.key); } } } return nodes; } public List RandomItems(int maskCount, int count, bool isSign, IList qualities, IList disableKeys , object customMask = null) { List nodes = new List(); List disableList = new List(); if (disableKeys != null) { disableList.AddRange(disableKeys); } for (int i = 0; i < count; i++) { RandomNode node = null; if (qualities?.Count > i) { node = this.RandomNode(maskCount, qualities[i], disableList , customMask); } if (node == null) { node = this.RandomNode(maskCount, disableList , customMask); } if (node != null) { nodes.Add(node.value); if (isSign) { disableList.Add(node.key); } } } return nodes; } /// /// 指定稀有度安全随机一个物品,如果指定稀有度没有满足条件,则递减一个稀有度重新随机,直到正确数据出一个物品,也可能会出现所有稀有度都无法满足条件,返回空 /// /// /// /// /// /// public T RandomItem(int maskCount, int quality, IList disableKeys = null, object customMask = null) { var maxCount = 10; var levelMasks = new List(); foreach (var groupsKey in this._randomGroups.Keys) { if (!this._randomGroups[groupsKey].SetMask(maskCount, disableKeys , customMask)) { levelMasks.Add(groupsKey); } } for (int i = (int)quality; i >= 0; i--) { var level = (int)i; if (!levelMasks.Contains(level) && this._randomGroups.ContainsKey(level) && this._randomGroups[level].ValidRofCount > 0) { while (maxCount-- > 0) { var node = this._randomGroups[level].RandomItem(this._poolId, this._seedId); if (node == null) { continue; } return node; } } } Debug.LogError($"随机条件不满足 ,poolId:{this._poolId} maskID: {maskCount}"); return null; } public T RandomItem(int maskCount, IList disableKeys = null, object customMask = null) { var maxCount = 10; var levelMasks = new List(); foreach (var groupsKey in this._randomGroups.Keys) { if (!this._randomGroups[groupsKey].SetMask(maskCount, disableKeys , customMask)) { levelMasks.Add(groupsKey); } } var level = this._levelRandom.RandomItemLevel(this._seedId, levelMasks); if (this._randomGroups.ContainsKey(level) && this._randomGroups[level].ValidRofCount > 0) { while (maxCount-- > 0) { var node = this._randomGroups[level].RandomItem(this._poolId, this._seedId); if (node == null) { continue; } return node; } } Debug.LogError($"随机条件不满足 ,poolId:{this._poolId} maskID: {maskCount}"); return null; } /// /// 随机一组有效的稀有度数据,用于后续随机 /// /// 数据长度 /// 最大稀有度限制 /// 期望能出现的稀有度 /// /// public List CustomRandomQuality(int count, int maxQuality, IList qualities = null , int randomGroup = 0) { List finalQualities = new List(); if (qualities != null) { finalQualities.AddRange(qualities); } List levelMasks = new List(); bool isTr = false; foreach (int value in Enum.GetValues(typeof(int))) { if (isTr) levelMasks.Add(value); else isTr = value == maxQuality; } for (int i = finalQualities.Count; i < count; i++) { finalQualities.Add(this.LevelRandom.RandomItemLevel((int) randomGroup, levelMasks)); } return finalQualities; } /// /// 随机一个指定稀有度的节点 /// /// /// /// /// /// public RandomNode RandomNode(int maskCount, int quality, IList disableKeys = null , object customMask = null) { if (this._randomGroups.ContainsKey(quality)) { if (this._randomGroups[quality].SetMask(maskCount, disableKeys , customMask)) { return this._randomGroups[quality].RandomNode(this._poolId, this._seedId); } } return null; } public RandomNode RandomNode(int maskCount, List qualities, IList disableKeys = null , object customMask = null) { var levelMasks = new List(); foreach (var groupsKey in this._randomGroups.Keys) { if (!qualities.Contains(groupsKey) || !this._randomGroups[groupsKey].SetMask(maskCount, disableKeys , customMask)) { levelMasks.Add(groupsKey); } } var level = this._levelRandom.RandomItemLevel(this._seedId, levelMasks); if (this._randomGroups.ContainsKey(level) && this._randomGroups[level].ValidRofCount > 0) { return this._randomGroups[level].RandomNode(this._poolId, this._seedId); } return null; } public RandomNode RandomNode(int maskCount, IList disableKeys = null, object customMask = null) { var maxCount = 10; var levelMasks = new List(); foreach (var groupsKey in this._randomGroups.Keys) { if (!this._randomGroups[groupsKey].SetMask(maskCount, disableKeys , customMask)) { levelMasks.Add(groupsKey); } } var level = this._levelRandom.RandomItemLevel(this._seedId, levelMasks); if (this._randomGroups.ContainsKey(level) && this._randomGroups[level].ValidRofCount > 0) { while (maxCount-- > 0) { var node = this._randomGroups[level].RandomNode(this._poolId, this._seedId); if (node == null) { continue; } return node; } } Debug.LogError($"随机条件不满足 ,poolId:{this._poolId} maskID: {maskCount}"); return null; } /// /// 获取该随机池中当前指定稀有度下的有效数 /// /// /// /// /// /// public int GetPoolValidCount(int quality, int maskCount , IList disableKeys = null, object customMask = null) { if (this._randomGroups.ContainsKey(quality)) { if (this._randomGroups[quality].SetMask(maskCount , disableKeys , customMask)) { return this._randomGroups[quality].ValidRofCount; } } return 0; } /// /// 获取该随机池中当前总共的有效数 /// /// /// /// /// public int GetPoolValidCount(int maskCount , IList disableKeys = null, object customMask = null) { int count = 0; foreach (var groupsKey in this._randomGroups.Keys) { var randomGroup = this._randomGroups[groupsKey]; if (!randomGroup.SetMask(maskCount, disableKeys , customMask)) { count += randomGroup.ValidRofCount; } } return count; } } }