From 253f769f5330fda55c2dfca8aa5e27987c3de98e Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 27 Apr 2010 19:24:28 +0000 Subject: [PATCH] Merged sabermonster2. git-svn-id: https://bd85.net/svn/cs3505_group@166 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- CarFire/CarFire/CarFire/SaberMonster2.cs | 152 ++++++++++++++++------- 1 file changed, 105 insertions(+), 47 deletions(-) diff --git a/CarFire/CarFire/CarFire/SaberMonster2.cs b/CarFire/CarFire/CarFire/SaberMonster2.cs index 33f94d7..5515458 100644 --- a/CarFire/CarFire/CarFire/SaberMonster2.cs +++ b/CarFire/CarFire/CarFire/SaberMonster2.cs @@ -42,13 +42,13 @@ namespace CarFire { mId = identifier; mMotion = new MovementManager(position); - + mRetryInterval = 2 + (position.X * 25789 + position.Y * 259) % 30; + + // We need to keep the game reference in order to get the grid when we // need to find paths. mGame = game; - pathFinder = null; - // Get the speed of the monster. If not set in the map, it defaults to // whatever the default of MovementManager is... 1 I think. string speedString; @@ -66,7 +66,7 @@ namespace CarFire foreach (string pathPoint in idlePathPoints) { Point? point = Parse.Coordinates(pathPoint); - if (point != null) mIdlePath.Add(point.Value); + if (point != null) mWaypoints.Add(point.Value); } } // Start doing something... @@ -107,19 +107,8 @@ namespace CarFire } */ - // Find the path to get to the closest waypoint. - if (pathFinder == null) - pathFinder = new PathFinder(mGame.Grid); - - mPath = new List(32); - mPath.Add(Coordinates); - List path = pathFinder.GetPath(mMotion.Coordinates, new Point(X,Y)); - if (path != null) - { - mPath.AddRange(path); - //mPath.Add(mIdlePath[mIdlePathIndex]); - } - mPathIndex = 0; + SetWaypoint(new Point(X, Y)); + ChartPath(); } /// @@ -129,54 +118,94 @@ namespace CarFire /// public void StartPacing() { - mState = AiState.Pacing; + if (mWaypoints.Count == 0) return; - if (mIdlePath.Count == 0) return; + mState = AiState.Pacing; // Determine the best (closest) waypoint to start at. // We may not be on the path, so we have to walk to get on it. - mIdlePathIndex = 0; + mWaypointIndex = 0; int closest = int.MaxValue; - for (int i = 0; i < mIdlePath.Count; i++) + for (int i = 0; i < mWaypoints.Count; i++) { - int distance = PathFinder.GetManhattanDistance(Coordinates, mIdlePath[i]); + int distance = PathFinder.GetManhattanDistance(Coordinates, mWaypoints[i]); if (distance < closest) { - mIdlePathIndex = i; + mWaypointIndex = i; closest = distance; } } // Find the path to get to the closest waypoint. - PathFinder pathFinder = new PathFinder(mGame.Grid); - mPath = new List(32); - mPath.Add(Coordinates); - List path = pathFinder.GetPath(mMotion.Coordinates, mIdlePath[mIdlePathIndex]); - if (path != null) - { - mPath.AddRange(path); - mPath.Add(mIdlePath[mIdlePathIndex]); - } - mPathIndex = 0; + ChartPath(); } Direction GetDirectionToNextCell() { - if (mPathIndex >= mPath.Count) + if (mPath != null) { - mState = AiState.Standing; + if (mPathIndex >= mPath.Count) + { + // We're done with the current path, so find the path to + // the next waypoint... forever. + switch (mState) + { + case AiState.Pacing: + SetWaypoint(); + ChartPath(); + break; + + default: + mPathIndex = 0; + mPath = null; + mPathDirection = Direction.None; + break; + } + } + + // We need to make sure our direction is set to the next cell + // we want to be. If our current coordinates match that, we need + // to change our direction to get to the next cell. + else if (mPath[mPathIndex] == mMotion.Coordinates) + { + mPathIndex++; + mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]); + } + + return mPathDirection; } + else return Direction.None; + } - // We need to make sure out direction is set to the next cell - // we want to be. If our current coordinates match that, we need - // to change our direction to get to the next cell. - if (mPath[mPathIndex % mPath.Count] == mMotion.Coordinates) + void SetWaypoint() + { + mWaypointIndex++; + mWaypoint = mWaypoints[mWaypointIndex % mWaypoints.Count]; + } + + void SetWaypoint(Point point) + { + mWaypoint = point; + } + + void ChartPath() + { + if (mPathSearch == null) { - mPathIndex++; - mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[mPathIndex % mPath.Count]); + PathFinder pathFinder = new PathFinder(mGame.Grid); + Point? nearby = pathFinder.GetNearbyOpenCell(mWaypoint); + if (nearby != null) mPathSearch = pathFinder.GetPathAsync(mMotion.Coordinates, nearby.Value); + + mPathIndex = 0; + mPath = null; } + else if (mPathSearch.IsCompleted) + { + mPath = (List)mPathSearch.Path; + mPathSearch = null; - return mPathDirection; + if (mPath != null) mPathDirection = MovementManager.GetDirection(mMotion.Coordinates, mPath[0]); + } } @@ -235,14 +264,41 @@ namespace CarFire { DefaultAction(); } - + + Direction direction = GetDirectionToNextCell(); + Point destination = MovementManager.GetNeighbor(Coordinates, direction); + switch (mState) { case AiState.Pacing: + + if (mGame.IsCellOpen(destination)) + { + mMotion.Update(timeSpan, direction); + } + else + { + if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath(); + mMotion.Update(timeSpan); + } + break; + case AiState.Chasing: - mMotion.Update(timeSpan, GetDirectionToNextCell()); + + if (mGame.IsCellOpen(destination)) + { + mMotion.Update(timeSpan, direction); + } + else + { + if (mGame.CurrentFrameNumber % mRetryInterval == 0) ChartPath(); + mMotion.Update(timeSpan); + } break; + case AiState.Standing: + mMotion.Update(timeSpan); + break; case AiState.Dazed: case AiState.Fighting: case AiState.Retreating: @@ -310,14 +366,16 @@ namespace CarFire char mId; MovementManager mMotion; AI mAI; - PathFinder pathFinder; - List mIdlePath = new List(); // List of waypoints that we got from the map. - int mIdlePathIndex; // Index to the waypoint we're heading for. + List mWaypoints = new List(); // List of waypoints that we got from the map. + int mWaypointIndex; // Index to the waypoint we're heading for. + Point mWaypoint; List mPath; // List of cells in the path between the position between where // we started and the waypoint we're heading for. int mPathIndex; // Index to the cell we're heading for. Direction mPathDirection; // The direction between our current position and the place we're going. + int mRetryInterval; + PathFinder.AsyncTask mPathSearch; // If a path search is in progress, this is the task object. AiState mState; // What is the monster doing? -- 2.45.2