public void TestIntQueueUpdatePriority() { StablePriorityQueue <Node> stableQueue = new StablePriorityQueue <Node>(100); Node n1 = new Node(); Node n2 = new Node(); Node n3 = new Node(); Node n4 = new Node(); stableQueue.Enqueue(n1, 1); stableQueue.Enqueue(n2, 1); stableQueue.Enqueue(n3, 1); stableQueue.Enqueue(n4, 1); Node node = stableQueue.First; Assert.IsTrue(node == n1); stableQueue.UpdatePriority(n1, 1); node = stableQueue.First; Assert.IsTrue(node == n1); stableQueue.UpdatePriority(n1, 2); node = stableQueue.First; Assert.IsTrue(node == n2); stableQueue.UpdatePriority(n1, 1); node = stableQueue.First; Assert.IsTrue(node == n1); stableQueue.Dequeue(); node = stableQueue.First; Assert.IsTrue(node == n2); stableQueue.Dequeue(); node = stableQueue.First; Assert.IsTrue(node == n3); }
/// <summary> /// Unlikely to be thread safe, will return null if object is missing. /// </summary> /// <returns></returns> public T CheckPop() { if (queue.Count > 0) { lock (LockObject) { return(queue.Dequeue()); } } return(null); }
private void DoWaitingDataUpdate() { while (dataWaitingQueue.Count > 0 && operations.Count < MaxLoadingCount) { AssetLoaderData data = dataWaitingQueue.Dequeue(); StartLoadingData(data); data.State = AssetLoaderDataState.Loading; dataLoadingList.Add(data); DebugLog.Debug(AssetConst.LOGGER_NAME, $"AssetLoader::DoWaitingDataUpdate->Start Load Data.data = {data}"); } }
/// <summary> /// Iterates through all GameEvents gathered by the enqueue function, in order of priority. /// Each individual tier of priority is gathered and processed before searching for the next one. /// This is done so that GameEffects that affect other GameEffects at a further priority can take effect immediately. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="enqueueFunction"></param> /// <returns></returns> public static IEnumerable <Tuple <GameEventOwner, Character, T> > IterateEvents <T>(EventEnqueueFunction <T> enqueueFunction) where T : GameEvent { Priority maxPriority = Priority.Invalid; while (true) { Priority nextPriority = Priority.Invalid; StablePriorityQueue <GameEventPriority, Tuple <GameEventOwner, Character, T> > queue = new StablePriorityQueue <GameEventPriority, Tuple <GameEventOwner, Character, T> >(); enqueueFunction(queue, maxPriority, ref nextPriority); if (queue.Count == 0) { break; } else { while (queue.Count > 0) { Tuple <GameEventOwner, Character, T> effect = queue.Dequeue(); yield return(effect); } maxPriority = nextPriority; } } }
public void Update() { int count = 0; using (new LockWait(ref _lock_message)) { count = processesPerFrame == -1? _queue.count : Math.Min(processesPerFrame, _queue.count); if (count == 0) { return; } for (int i = 0; i < count; i++) { var node = _queue.Dequeue(); _list.Remove(node); Message message; if (excuteMap.TryGetValue(node, out message)) { _tmp.Enqueue(message); } } if (_list.Count > 0) { for (int i = _list.Count - 1; i >= 0; i--) { _queue.UpdatePriority(_list[i], _list[i].priority - 1); } } } for (int i = 0; i < count; i++) { var message = _tmp.Dequeue(); HandleMessage(message); } }
public T Dequeue() { if (queue.Count <= 0) { throw new InvalidOperationException("Cannot call Dequeue () on an empty queue"); } SimpleNode node = queue.Dequeue(); return(node.Data); }
public void Update() { int count = 0; Queue <Message> _tmp = Framework.GlobalAllocate <Queue <Message> >(); using (new LockWait(ref _lock_message)) { count = processesPerFrame == -1 ? _priorityQueue.count : Math.Min(processesPerFrame, _priorityQueue.count); if (count == 0) { return; } for (int i = 0; i < count; i++) { StablePriorityQueueNode node = _priorityQueue.Dequeue(); Message message; if (excuteMap.TryGetValue(node, out message)) { _tmp.Enqueue(message); excuteMap.Remove(node); } _updatelist.Remove(node); node.GlobalRecyle(); } if (_updatelist.Count > 0) { for (int i = _updatelist.Count - 1; i >= 0; i--) { _priorityQueue.UpdatePriority(_updatelist[i], _updatelist[i].priority - 1); } } } for (int i = 0; i < count; i++) { var message = _tmp.Dequeue(); HandleMessage(message); } _tmp.GlobalRecyle(); }
public virtual void Update(FrameTick elapsedTime) { time += elapsedTime; while (TilesToEmit.Count > 0 && time >= TilesToEmit.FrontPriority()) { int priority = TilesToEmit.FrontPriority(); Loc tile = TilesToEmit.Dequeue(); FiniteEmitter tileEmitter = (FiniteEmitter)TileEmitter.Clone(); tileEmitter.SetupEmit(tile * GraphicsManager.TileSize, User.MapLoc, User.CharDir); DungeonScene.Instance.CreateAnim(tileEmitter, DrawLayer.NoDraw); } }
public IEnumerator <YieldInstruction> ProcessHitQueue(List <Hitbox> hitboxes, HitboxEffect burstEffect, HitboxEffect tileEffect) { //set the NextStep to update the hit queue; aka call this method again on next (available) update //stop doing it only if, for all hitboxes, everything has been hit and it's "done" //assume none of the hitboxes are blocking while (true) { StablePriorityQueue <int, HitboxHit> hitTargets = new StablePriorityQueue <int, HitboxHit>(); foreach (Hitbox hitbox in hitboxes) { hitbox.UpdateHitQueue(hitTargets); } //hit each target while (hitTargets.Count > 0) { HitboxHit tile = hitTargets.Dequeue(); if (tile.Explode) { yield return(CoroutineManager.Instance.StartCoroutine(burstEffect(tile.Loc))); } else { yield return(CoroutineManager.Instance.StartCoroutine(tileEffect(tile.Loc))); } } bool allDone = true; foreach (Hitbox hitbox in hitboxes) { if (!hitbox.ProcessesDone()) { allDone = false; break; } } if (allDone) { yield break; } yield return(new WaitForFrames(1)); } }
public static List <PointD> ExtractOrderListFromOPTICS(List <PointD> list_of_points, double epsilon, int min_points) { List <PointD> OrderedList = new List <PointD>(); Dictionary <PointD, Tuple <bool, double?> > DictionnaryOfOPTICS = new Dictionary <PointD, Tuple <bool, double?> >(); foreach (PointD point in list_of_points.Distinct().ToList()) { DictionnaryOfOPTICS.Add(point, new Tuple <bool, double?>(false, null)); } foreach (PointD point in DictionnaryOfOPTICS.Keys.ToList()) { if (DictionnaryOfOPTICS[point].Item1 == false) { DictionnaryOfOPTICS[point] = new Tuple <bool, double?>(true, DictionnaryOfOPTICS[point].Item2); OrderedList.Add(point); if (Core_distance(DictionnaryOfOPTICS, point, epsilon, min_points) != null) { List <PointD> list_of_neighbors = Get_neighbors_points_for_Optics(DictionnaryOfOPTICS.Keys.ToList(), point, epsilon); // Get Neighbors StablePriorityQueue <PointDQueue> seeds = new StablePriorityQueue <PointDQueue>(1440); Update_OPTICS(ref DictionnaryOfOPTICS, list_of_neighbors, point, ref seeds, epsilon, min_points); while (seeds.Count > 0) { PointDQueue point_temp = seeds.Dequeue(); PointD neighbor_point = new PointD(point_temp.x, point_temp.y); DictionnaryOfOPTICS[neighbor_point] = new Tuple <bool, double?>(true, DictionnaryOfOPTICS[neighbor_point].Item2); OrderedList.Add(neighbor_point); if (Core_distance(DictionnaryOfOPTICS, point, epsilon, min_points) != null) { Update_OPTICS(ref DictionnaryOfOPTICS, list_of_neighbors, neighbor_point, ref seeds, epsilon, min_points); } } } } } return(OrderedList); }
public IEnumerator <YieldInstruction> BeforeExplosion(BattleContext context) { EventEnqueueFunction <BattleEvent> function = (StablePriorityQueue <GameEventPriority, Tuple <GameEventOwner, Character, BattleEvent> > queue, Priority maxPriority, ref Priority nextPriority) => { DataManager.Instance.UniversalEvent.AddEventsToQueue(queue, maxPriority, ref nextPriority, DataManager.Instance.UniversalEvent.BeforeExplosions); context.Data.AddEventsToQueue <BattleEvent>(queue, maxPriority, ref nextPriority, context.Data.BeforeExplosions); StablePriorityQueue <int, Character> charQueue = new StablePriorityQueue <int, Character>(); foreach (Character character in ZoneManager.Instance.CurrentMap.IterateCharacters()) { if (!character.Dead) { charQueue.Enqueue(-character.Speed, character); } } int portPriority = 0; while (charQueue.Count > 0) { Character character = charQueue.Dequeue(); foreach (PassiveContext effectContext in character.IterateProximityPassives(context.User, context.ExplosionTile, portPriority)) { effectContext.AddEventsToQueue(queue, maxPriority, ref nextPriority, ((ProximityData)effectContext.EventData).BeforeExplosions); } portPriority++; } }; foreach (Tuple <GameEventOwner, Character, BattleEvent> effect in IterateEvents <BattleEvent>(function)) { yield return(CoroutineManager.Instance.StartCoroutine(effect.Item3.Apply(effect.Item1, effect.Item2, context))); if (context.CancelState.Cancel) { yield break; } } }
public void UpdateHitQueue(StablePriorityQueue <int, HitboxHit> hitTargets) { //when updating, the base will update the time elapsed //when this method is reached, hits will be delegated accordingly while (TilesToHit.Count > 0 && (Finished || time >= TilesToHit.FrontPriority())) { int priority = TilesToHit.FrontPriority(); Loc tile = TilesToHit.Dequeue(); //filter out the hitboxes that are not wanted TargetHitType type = IsValidTileTarget(tile); if (type == TargetHitType.Burst) { hitTargets.Enqueue(priority, new HitboxHit(tile, true)); } else if (type == TargetHitType.Tile) { hitTargets.Enqueue(priority, new HitboxHit(tile, false)); } } }
private GameAction DumbAvoid(Character controlledChar, bool preThink, List <Character> seenCharacters, CharIndex ownIndex, IRandom rand) { StablePriorityQueue <double, Dir8> candidateDirs = new StablePriorityQueue <double, Dir8>(); //choose the single direction that avoids other characters the most bool respectPeers = !preThink; for (int ii = -1; ii < DirExt.DIR8_COUNT; ii++) { Loc checkLoc = controlledChar.CharLoc + ((Dir8)ii).GetLoc(); double dirDistance = 0; //iterated in increasing character indices foreach (Character seenChar in seenCharacters) { if (RunFromAllies && !RunFromFoes) { //only avoid if their character index is lower than this one, aka higher ranking member CharIndex seenIndex = ZoneManager.Instance.CurrentMap.GetCharIndex(seenChar); if (seenIndex.Team > ownIndex.Team) { break; } else if (seenIndex.Team == ownIndex.Team) { if (seenIndex.Char > ownIndex.Char && seenChar.MemberTeam.LeaderIndex != seenIndex.Char) { continue; } } } dirDistance += Math.Sqrt((checkLoc - seenChar.CharLoc).DistSquared()); } candidateDirs.Enqueue(-dirDistance, (Dir8)ii); } Grid.LocTest checkDiagBlock = (Loc testLoc) => { return(ZoneManager.Instance.CurrentMap.TileBlocked(testLoc, controlledChar.Mobility, true)); //enemy/ally blockings don't matter for diagonals }; Grid.LocTest checkBlock = (Loc testLoc) => { if (ZoneManager.Instance.CurrentMap.TileBlocked(testLoc, controlledChar.Mobility)) { return(true); } if ((IQ & AIFlags.TrapAvoider) != AIFlags.None) { Tile tile = ZoneManager.Instance.CurrentMap.Tiles[testLoc.X][testLoc.Y]; if (tile.Effect.ID > -1) { TileData entry = DataManager.Instance.GetTile(tile.Effect.ID); if (entry.StepType == TileData.TriggerType.Trap || entry.StepType == TileData.TriggerType.Site || entry.StepType == TileData.TriggerType.Switch) { return(true); } } } if (respectPeers && BlockedByChar(testLoc, Alignment.Self | Alignment.Foe)) { return(true); } return(false); }; //try each direction from most appealing to least appealing, stopping if we get to "none" while (candidateDirs.Count > 0) { Dir8 highestDir = candidateDirs.Dequeue(); if (highestDir == Dir8.None) { if (AbortIfCornered)//this plan will be aborted, try the next plan in the list { return(null); } else//cry in a corner { return(new GameAction(GameAction.ActionType.Wait, Dir8.None)); } } else { //check to see if we can walk this way if (!Grid.IsDirBlocked(controlledChar.CharLoc, highestDir, checkBlock, checkDiagBlock)) { return(TrySelectWalk(controlledChar, highestDir)); } } } if (AbortIfCornered)//this plan will be aborted, try the next plan in the list { return(null); } else//cry in a corner { return(new GameAction(GameAction.ActionType.Wait, Dir8.None)); } }