///<summary> ///this methods checks to see if there is an existing record for the ///given key. If not a new <see cref="NavigationalMemoryRecord"/> ///record is made and added to the memory map. /// </summary> private void MakeNewRecordIfNotAlreadyPresent(NavigationalMemoryKey key) { //check to see if this key already exists in memory. If it doesn't, //create a new record if (!MemoryMap.ContainsKey(key)) { MemoryMap[key] = new NavigationalMemoryRecord(Owner, key); } }
///<summary> ///Remember this ///</summary> ///<param name="mode"></param> ///<param name="status"></param> ///<param name="sourceNodeIndex"></param> ///<param name="destinationNodeIndex"></param> ///<param name="timeTaken"></param> ///<param name="timeExpected"></param> public void RecordTimeTaken( TravelModes mode, TravelStatuses status, int sourceNodeIndex, int destinationNodeIndex, float timeTaken, float timeExpected) { NavigationalMemoryKey key = new NavigationalMemoryKey( mode, sourceNodeIndex, destinationNodeIndex); MakeNewRecordIfNotAlreadyPresent(key); NavigationalMemoryRecord record = MemoryMap[key]; switch (status) { case TravelStatuses.Completed: record.CompletedCount++; record.RunningAverageTimeTaken += (timeTaken - record.RunningAverageTimeTaken)/ record.CompletedCount; record.CompletedMarginRunningAverage += (timeExpected - timeTaken - record.CompletedMarginRunningAverage)/ record.CompletedCount; break; case TravelStatuses.Failed: record.FailedCount++; float estimatedRemainingTime; Owner.CalculateTimeToReachNode( destinationNodeIndex, out estimatedRemainingTime); record.FailedMarginRunningAverage += (estimatedRemainingTime - record.FailedMarginRunningAverage)/ record.FailedCount; break; case TravelStatuses.Abandoned: Assert.Fatal(false, "NavigationalMemory,RecordTimeTaken: " + "abandoned status not implemented yet."); break; } record.CompletedPerSecond = record.CompletedCount/Time.TimeNow; record.FailedPerSecond = record.FailedCount/Time.TimeNow; LogUtil.WriteLineIfLogNavigationalMemory( "NavigationalMemory.RecordTimeTaken: " + record); DumpMemory(DumpFrequency); }
///<summary> ///Get time to reach destination from current position ///</summary> ///<param name="destination"></param> ///<param name="time"></param> ///<returns></returns> public bool TimeToReachDestination(Vector2 destination, out float time) { int sourceNodeIndex = Owner.PathPlanner.GetClosestNodeToPosition(Owner.Position); int destinationNodeIndex = Owner.PathPlanner.GetClosestNodeToPosition(destination); NavigationalMemoryKey key = new NavigationalMemoryKey( TravelModes.FollowPath, sourceNodeIndex, destinationNodeIndex); MakeNewRecordIfNotAlreadyPresent(key); NavigationalMemoryRecord record = MemoryMap[key]; time = record.RunningAverageTimeTaken; LogUtil.WriteLineIfLogNavigationalMemory( "NavigationalMemory.TimeToReachDestination: " + record); return record.CompletedCount == 0; //if zero, time was calculated }
///<summary> ///Sets a reference to a particular memory for ease of access (to avoid ///multiple lookups). ///</summary> ///<param name="mode"></param> ///<param name="sourceNodeIndex"></param> ///<param name="destinationNodeIndex"></param> public void MakeCurrent( TravelModes mode, int sourceNodeIndex, int destinationNodeIndex) { NavigationalMemoryKey key = new NavigationalMemoryKey( mode, sourceNodeIndex, destinationNodeIndex); MakeNewRecordIfNotAlreadyPresent(key); Current = MemoryMap[key]; }