/// <summary> /// 周期调用触发任务 /// </summary> public static void Tick() { m_unTick += (uint)m_stopWatch.ElapsedMilliseconds; m_stopWatch.Reset(); m_stopWatch.Start(); while (m_queue.Count != 0) { AbsTimerData p; lock (m_queueLock) p = m_queue.Peek(); if (m_unTick < p.UnNextTick) { break; } lock (m_queueLock) m_queue.Dequeue(); if (p.NInterval > 0) { p.UnNextTick += (ulong)p.NInterval; lock (m_queueLock) m_queue.Enqueue(p.NTimerId, p, p.UnNextTick); p.DoAction(); } else { p.DoAction(); } } }
/// <summary> /// 周期调用触发任务 /// </summary> public static void Tick() { m_unTick = (uint)(1000 * Time.time); while (m_queue.Count != 0) { AbsTimerData p; lock (m_queueLock) p = m_queue.Peek(); if (m_unTick < p.UnNextTick) { break; } lock (m_queueLock) m_queue.Dequeue(); if (p.NInterval > 0) { p.UnNextTick += (ulong)p.NInterval; lock (m_queueLock) m_queue.Enqueue(p.NTimerId, p, p.UnNextTick); p.DoAction(); } else { p.DoAction(); } } }
/// <summary> /// 周期调用触发任务 /// </summary> public static void Tick() { m_unTick += (uint)(1000 * Time.deltaTime); //LoggerHelper.Error("m_unTick: " + (uint)(1000 * Time.deltaTime) + " Time.timeScale: " + Time.timeScale + " Time.deltaTime: " + Time.deltaTime); while (m_queue.Count != 0) { AbsTimerData p; lock (m_queueLock) p = m_queue.Peek(); if (m_unTick < p.UnNextTick) { break; } lock (m_queueLock) m_queue.Dequeue(); if (p.NInterval > 0) { p.UnNextTick += (ulong)p.NInterval; lock (m_queueLock) m_queue.Enqueue(p.NTimerId, p, p.UnNextTick); p.DoAction(); } else { p.DoAction(); } } }
public static void Tick() { mCurrentTick += (uint)(1000 * Time.deltaTime); while (mPriorityQueue.Count != 0) { AbstractTimerData p; lock (mQueueLock) p = mPriorityQueue.Peek(); if (mCurrentTick < p.mNextTick) { break; } lock (mQueueLock) mPriorityQueue.Dequeue(); if (p.mInterval > 0) { p.mNextTick += (ulong)p.mInterval; lock (mQueueLock) mPriorityQueue.Enqueue(p.mTimerId, p, p.mNextTick); p.DoAction(); } else { p.DoAction(); } } }
public static void Tick() { mCurrentTick += (uint)mStopWatch.ElapsedMilliseconds; mStopWatch.Reset(); mStopWatch.Start(); while (mPriorityQueue.Count != 0) { TimerTask p; lock (mQueueLock) { p = mPriorityQueue.Peek(); } if (mCurrentTick < p.NextTick) { break; } lock (mQueueLock) { mPriorityQueue.Dequeue(); } if (p.Interval > 0) { p.NextTick += (ulong)p.Interval; lock (mQueueLock) { mPriorityQueue.Enqueue(p.TimerId, p, p.NextTick); } p.DoAction(); } else { p.DoAction(); } } }
public static void Reset() { m_unTick = 0; m_nNextTimerId = 0; lock (m_queueLock) { while (m_queue.Count != 0) { m_queue.Dequeue(); } } }
public void Update(float elapseSeconds) { LinkedListNode <ITaskAgent <T> > current = m_WorkingAgents.First; while (current != null) { if (current.Value.Task.Done) { LinkedListNode <ITaskAgent <T> > next = current.Next; current.Value.Reset(); m_FreeAgents.Push(current.Value); m_WorkingAgents.Remove(current); current = next; continue; } current.Value.Update(elapseSeconds); current = current.Next; } while (FreeAgentCount > 0 && WaitingTaskCount > 0) { ITaskAgent <T> agent = m_FreeAgents.Pop(); LinkedListNode <ITaskAgent <T> > agentNode = m_WorkingAgents.AddLast(agent); T task = m_WaitingTasks.Dequeue(); agent.Start(task); if (task.Done) { agent.Reset(); m_FreeAgents.Push(agent); m_WorkingAgents.Remove(agentNode); } } }
private void OnTimerCallback(object ignored) { CallbackInfo ci = null; try { lock (locker) { if (State.Equals(WorkflowRuntimeServiceState.Started)) { ci = pendingScheduleRequests.Peek(); if (ci != null) { if (ci.IsExpired) { WorkflowTrace.Host.TraceEvent(TraceEventType.Information, 0, "Timeout occured for timer for instance {0}", ci.InstanceId); threadRunning = true; pendingScheduleRequests.Dequeue(); } else { callbackTimer = CreateTimerCallback(ci); } } } } if (threadRunning) { ci.Callback(ci.InstanceId); // delivers the timer message RunWorkflow(ci.InstanceId); } } catch (ThreadAbortException e) { WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, "Timeout for instance, {0} threw exception {1}", ci == null ? Guid.Empty : ci.InstanceId, e.Message); RaiseServicesExceptionNotHandledEvent(e, ci.InstanceId); throw; } catch (Exception e) { WorkflowTrace.Host.TraceEvent(TraceEventType.Error, 0, "Timeout for instance, {0} threw exception {1}", ci == null ? Guid.Empty : ci.InstanceId, e.Message); RaiseServicesExceptionNotHandledEvent(e, ci.InstanceId); } finally { lock (locker) { if (threadRunning) { threadRunning = false; ci = pendingScheduleRequests.Peek(); if (ci != null) { callbackTimer = CreateTimerCallback(ci); } } } } }
private static HashSet <EdgeID> DeleteEdges(KeyedPriorityQueue <int, HalfEdge, float> priorityQueue, List <Vector2> vertes) { HashSet <EdgeID> deletedEdgeSet = new HashSet <EdgeID>(); while (priorityQueue.Count > 0) { HalfEdge edgeToRemove = priorityQueue.Dequeue(); deletedEdgeSet.Add(GetEdgeId(edgeToRemove.mIndex, edgeToRemove.mNext.mIndex)); UpdateEdge(edgeToRemove, priorityQueue, deletedEdgeSet, vertes); UpdateEdge(edgeToRemove.mPartner, priorityQueue, deletedEdgeSet, vertes); } return(deletedEdgeSet); }
/// <summary> /// 周期调用触发任务 /// </summary> public static void Tick() { //累计获取逝去的时间 m_unTick += (uint)m_stopWatch.ElapsedMilliseconds; //重置StopWatch m_stopWatch.Reset(); m_stopWatch.Start(); while (m_queue.Count != 0) { //队列出列 AbsTimerData p; lock (m_queueLock) p = m_queue.Peek(); //判断出列Timer是否到时间 if (m_unTick < p.UnNextTick) { break; } //出列 lock (m_queueLock) m_queue.Dequeue(); //间隔时间不等于0,重复执行Timer if (p.NInterval > 0) { //更新下一次Tick时间 p.UnNextTick += (ulong)p.NInterval; //重新入列 lock (m_queueLock) m_queue.Enqueue(p.NTimerId, p, p.UnNextTick); p.DoAction(); } else { p.DoAction(); } } }
public void Update(float deltaTime, float unscaledDeltaTime) { mUpdating = true; int len = mSortedUpdaters.Count; mNewPriority = int.MaxValue; mNewCount = mNewUpdaters.Count; for (int i = 0; i < mNewCount; i++) { mSortedUpdaters.Add(null); } if (mNewCount > 0) { mNewPriority = mNewUpdaters.Peek().priority; } for (mInterior = len - 1; mInterior >= -1;) { UpdateItem updater = null; if (mInterior < 0) { if (mNewCount > 0) { mNewCount--; updater = mNewUpdaters.Dequeue(); updater.index = mNewCount; mSortedUpdaters[mNewCount] = updater; mUpdaters.Add(updater.key, updater); } else { break; } } else { updater = mSortedUpdaters[mInterior]; if (mNewCount <= 0) { mInterior--; } else if (mNewPriority < updater.priority) { UpdateItem newUpdater = mNewUpdaters.Dequeue(); newUpdater.index = mInterior + mNewCount; mSortedUpdaters[newUpdater.index] = newUpdater; mUpdaters.Add(newUpdater.key, newUpdater); updater = newUpdater; mNewCount--; } else { updater.index = mInterior + mNewCount; mSortedUpdaters[updater.index] = updater; mSortedUpdaters[mInterior] = null; mInterior--; } } if (updater.behaviour == null || updater.behaviour.isActiveAndEnabled) { try { updater.update(updater.unscaled ? unscaledDeltaTime : deltaTime); } catch (System.Exception e) { Debug.LogException(e); } } } for (int i = 0, imax = mToAddUpdaters.Count; i < imax; i++) { UpdateItem updater = mToAddUpdaters[i]; mNewUpdaters.Enqueue(updater.key, updater, updater.priority); } mToAddUpdaters.Clear(); mUpdating = false; }
public List <Vector2i> PathFind(Vector2i start, Vector2i end) { bool found = false; mOpenTable.Clear(); mResultPath.Clear(); mOpenStatusValue += 2; mCloseStatusValue += 2; int closeNodeCounter = 0; ushort location = (ushort)((start[1] << mGridXLog2) + start[0]); ushort endLocation = (ushort)((end[1] << mGridXLog2) + end[0]); mGridNode[location].G = 0; mGridNode[location].F = _hEstimate; mGridNode[location].PX = (ushort)start[0]; mGridNode[location].PY = (ushort)start[1]; mGridNode[location].Status = mOpenStatusValue; mOpenTable.Enqueue(location, location, mGridNode[location].F); ushort locationX; ushort locationY; ushort mHoriz = 0; sbyte[,] direction = _useDiagonal ? AStarDirection.DiagonalDirection : AStarDirection.NormalDirection; int directionCount = _useDiagonal ? 8 : 4; while (mOpenTable.Count > 0) { location = mOpenTable.Dequeue(); if (mGridNode[location].Status == mCloseStatusValue) { continue; } if (location == endLocation) { mGridNode[location].Status = mCloseStatusValue; found = true; break; } if (closeNodeCounter > _searchLimit) { break; } locationX = (ushort)(location & mGridXMod); locationY = (ushort)(location >> mGridXLog2); if (_usePunish) { mHoriz = (ushort)(locationX - mGridNode[location].PX); } int newG; for (int i = 0; i < directionCount; i++) { ushort newLocationX = (ushort)(locationX + direction[i, 0]); ushort newLocationY = (ushort)(locationY + direction[i, 1]); ushort newLocation = (ushort)((newLocationY << mGridXLog2) + newLocationX); if (newLocationX >= mGridX || newLocationY >= mGridY) { continue; } if (mGridNode[newLocation].Status == mCloseStatusValue && !ReuseClose) { continue; } if (mGrid[newLocationX, newLocationY] == 0) { continue; } if (_useDiagonal && i > 3) { newG = mGridNode[location].G + (int)(mGrid[newLocationX, newLocationY] * 2.41); } else { newG = mGridNode[location].G + mGrid[newLocationX, newLocationY]; } if (Punish) { if ((newLocationX - locationX) != 0) { if (mHoriz == 0) { newG += Math.Abs(newLocationX - end[0]) + Math.Abs(newLocationY - end[1]); } } if ((newLocationY - locationY) != 0) { if (mHoriz != 0) { newG += Math.Abs(newLocationX - end[0]) + Math.Abs(newLocationY - end[1]); } } } if (mGridNode[newLocation].Status == mOpenStatusValue || mGridNode[newLocation].Status == mCloseStatusValue) { if (mGridNode[newLocation].G <= newG) { continue; } } mGridNode[newLocation].PX = locationX; mGridNode[newLocation].PY = locationY; mGridNode[newLocation].G = newG; int newH = 0; switch (_useFormula) { case AStarFormula.Manhattan: newH = _hEstimate * (Math.Abs(newLocationX - end[0]) + Math.Abs(newLocationY - end[1])); break; case AStarFormula.MaxDXDY: newH = _hEstimate * (Math.Max(Math.Abs(newLocationX - end[0]), Math.Abs(newLocationY - end[1]))); break; case AStarFormula.DiagonalShortCut: int h_diagonal = Math.Min(Math.Abs(newLocationX - end[0]), Math.Abs(newLocationY - end[1])); int h_straight = (Math.Abs(newLocationX - end[0]) + Math.Abs(newLocationY - end[1])); newH = (_hEstimate * 2) * h_diagonal + _hEstimate * (h_straight - 2 * h_diagonal); break; case AStarFormula.Euclidean: newH = (int)(_hEstimate * Math.Sqrt(Math.Pow((newLocationY - end[0]), 2) + Math.Pow((newLocationY - end[1]), 2))); break; case AStarFormula.EuclideanNoSQR: newH = (int)(_hEstimate * (Math.Pow((newLocationX - end[0]), 2) + Math.Pow((newLocationY - end[1]), 2))); break; case AStarFormula.Custom: Vector2i dxy = new Vector2i(Math.Abs(end[0] - newLocationX), Math.Abs(end[1] - newLocationY)); int Orthogonal = Math.Abs(dxy[0] - dxy[1]); int Diagonal = Math.Abs(((dxy[0] + dxy[1]) - Orthogonal) / 2); newH = _hEstimate * (Diagonal + Orthogonal + dxy[0] + dxy[1]); break; } if (_useTieBreaker) { int dx1 = locationX - end[0]; int dy1 = locationY - end[1]; int dx2 = start[0] - end[0]; int dy2 = start[1] - end[1]; int cross = Math.Abs(dx1 * dy2 - dx2 * dy1); newH = (int)(newH + cross * _multiple); } mGridNode[newLocation].F = newG + newH; mOpenTable.Enqueue(newLocation, newLocation, mGridNode[newLocation].F); mGridNode[newLocation].Status = mOpenStatusValue; } closeNodeCounter++; mGridNode[location].Status = mCloseStatusValue; } if (found) { mResultPath.Clear(); PathNode tmp = mGridNode[(end[1] << mGridXLog2) + end[0]]; PathNodeResult node = new PathNodeResult(); node.F = tmp.F; node.G = tmp.G; node.H = 0; node.PX = tmp.PX; node.PY = tmp.PY; node.X = end[0]; node.Y = end[1]; while (node.X != node.PX || node.Y != node.PY) { mResultPath.Add(node); ushort posX = node.PX; ushort posY = node.PY; tmp = mGridNode[(posY << mGridXLog2) + posX]; node = new PathNodeResult(); node.F = tmp.F; node.G = tmp.G; node.H = 0; node.PX = tmp.PX; node.PY = tmp.PY; node.X = posX; node.Y = posY; } mResultPath.Add(node); mResultPath.Reverse(0, mResultPath.Count); List <Vector2i> res = new List <Vector2i>(); foreach (PathNodeResult n in mResultPath) { res.Add(new Vector2i(n.X, n.Y)); } return(res); } return(null); }
public void GenerateDisjktraMatrix(NodeType startNode, float infinite) { if (mAdjacencyLists.Count == 0) { return; } mCostMatrix = new float[mAdjacencyLists.Count * mAdjacencyLists.Count]; mLeastNodeArray = new NodeType[mAdjacencyLists.Count]; //mCostMatrixIndices.Clear(); //foreach (var key in mAdjacencyLists.Keys) //{ // mCostMatrixIndices.Add(key); //} //mCostMatrixIndices.Sort((NodeType node1, NodeType node2) => { return mCompareFunc(node1, node2); }); // 由小到大排序 for (int i = 0; i < mAdjacencyLists.Count * mAdjacencyLists.Count; ++i) { mCostMatrix[i] = infinite; } int adjacentIndex = GetIndexByKey(startNode); if (adjacentIndex == -1) { throw new Exception("-1 null"); } for (int r = 0; r < mAdjacencyLists.Count; ++r) { mCostMatrix[r * mAdjacencyLists.Count + adjacentIndex] = 0.0f; } mRootNode = startNode; int row = 0; NodeType currentNode = startNode; Dictionary <NodeType, float> adjacencyList; float edgeWeight, adjacentNodeWeight, currentNodeWeight = 0.0f; NodeType adjacentKey; Dictionary <NodeType, float> openSet = new Dictionary <NodeType, float>(); while (row < mAdjacencyLists.Count - 1) { adjacencyList = mAdjacencyLists[currentNode]; foreach (var v in adjacencyList) { edgeWeight = v.Value; adjacentKey = v.Key; adjacentIndex = GetIndexByKey(adjacentKey); adjacentNodeWeight = mCostMatrix[row * mAdjacencyLists.Count + adjacentIndex]; if (currentNodeWeight + edgeWeight < adjacentNodeWeight) { // Update the weight for the adjacent node for (int r = row; r < mAdjacencyLists.Count; r++) { mCostMatrix[r * mAdjacencyLists.Count + adjacentIndex] = currentNodeWeight + edgeWeight; } if (!openSet.ContainsKey(adjacentKey)) { openSet.Add(adjacentKey, currentNodeWeight + edgeWeight); } else { openSet[adjacentKey] = currentNodeWeight + edgeWeight; } } } KeyedPriorityQueue <NodeType, NodeType, float> minHeap = new KeyedPriorityQueue <NodeType, NodeType, float>(); foreach (var open in openSet) { minHeap.Enqueue(open.Key, open.Key, open.Value); } if (minHeap.Count == 0) { isValidPath = true; break; } currentNodeWeight = minHeap.PeekPriority(); mLeastNodeArray[row] = minHeap.Dequeue(); currentNode = mLeastNodeArray[row]; openSet.Remove(currentNode); row++; } isValidPath = true; }
/// <summary> /// 周期调用触发任务 /// </summary> public void Tick() { if (isPause) { return; } if (!m_stopWatch.IsRunning) { m_stopWatch.Start(); } m_unTick = (uint)(UnityEngine.Time.time * 1000); m_checkTimeTmp += m_invokeReaptingTime; m_timeSystemTimerTmp += m_invokeReaptingTime; if (m_cheat == false) { if (m_checkTimeTmp > m_checkPerTime) { m_checkTimeTmp = 0; CheckCheat(); } } bool profilerSample = UnityEngine.Debug.isDebugBuild || UnityEngine.Application.isEditor; while (m_queue.Count != 0) { AbsTimerData p; lock (m_queueLock) p = m_queue.Peek(); if (m_unTick < p.UnNextTick) { break; } lock (m_queueLock) m_queue.Dequeue(); if (p.NInterval > 0) { p.UnNextTick += (ulong)p.NInterval; lock (m_queueLock) m_queue.Enqueue(p.NTimerId, p, p.UnNextTick); if (profilerSample) { var name = string.IsNullOrEmpty(p.StackTrack) ? p.Action.Method.Name : p.StackTrack; Profiler.BeginSample(name); } p.DoAction(); if (profilerSample) { Profiler.EndSample(); } } else { if (profilerSample) { var name = string.IsNullOrEmpty(p.StackTrack) ? p.Action.Method.Name : p.StackTrack; Profiler.BeginSample(name); } p.DoAction(); if (profilerSample) { Profiler.EndSample(); } } } }