protected virtual float TryPathToTarget() { float cost = NotActionCost; TargetAccesible = false; bool movementDone = false; if (Target == LastTarget) { PathToTarget = MathFunctions.PathfindingWithReusing(WorldPosition2D, Target.WorldPosition2D, CurrentTerrain.IsPosAccesible, CurrentWorld.SearchRadius * 2, PathToTarget); } else { PathToTarget = MathFunctions.Pathfinding(WorldPosition2D, Target.WorldPosition2D, CurrentTerrain.IsPosAccesible, CurrentWorld.SearchRadius); } LastTarget = Target; // If a path to target is possible if (PathToTarget.Count > 0) { TargetAccesible = MathFunctions.IsTouchingTarget(PathToTarget[PathToTarget.Count - 1], Target.WorldPosition2D, CurrentTerrain.IsPosAccesible); // Try movement movementDone = CurrentTerrain.TryMoveToCell(this, PathToTarget[0]); if (movementDone) { cost = PathToTargetCost; PathToTarget.RemoveAt(0); } } return(cost); }
/// <summary> /// Updates set of beliefs. /// </summary> /// <param name="belief"> </param> /// <param name="beliefPos"></param> /// <returns></returns> private List <Tuple <int, int> > UpdateBelief(TypesBelief belief, IEnumerable <Tuple <int, int> > beliefPos) { var result = new List <Tuple <int, int> >(); foreach (var spot in beliefPos) { if (CurrentTerrain.Contains(new Tuple <int, int>(spot.Item1, spot.Item2))) { switch (belief) { case TypesBelief.PotentialWaterSpots: if (_terrain[spot.Item1, spot.Item2] >= 0) { continue; } break; case TypesBelief.ObstaclesOnTerrain: if (_terrain[spot.Item1, spot.Item2] < RunningOverThreshold) { continue; } break; } } result.Add(spot); } return(result); }
public IEnumerable <Percept> GetCurrentTerrain() { var R = SenseRadius; CurrentTerrain.Clear(); var result = new List <Percept>(); for (var i = X - R > 0 ? X - R : 0; i <= X + R; i++) { for (var j = Y; Math.Pow((j - Y), 2) + Math.Pow((i - X), 2) <= Math.Pow(R, 2); j--) { if (j < 0 || i >= _terrain.GetLength(0)) { break; } // In the circle result.AddRange(CheckTerrain(Mars.TerrainAt(i, j), new Tuple <int, int>(i, j))); CurrentTerrain.Add(new Tuple <int, int>(i, j)); UpdatePerceivedCellsDicc(new Tuple <int, int>(i, j)); } for (var j = Y + 1; (j - Y) * (j - Y) + (i - X) * (i - X) <= R * R; j++) { if (j >= _terrain.GetLength(1) || i >= _terrain.GetLength(0)) { break; } // In the circle result.AddRange(CheckTerrain(Mars.TerrainAt(i, j), new Tuple <int, int>(i, j))); CurrentTerrain.Add(new Tuple <int, int>(i, j)); UpdatePerceivedCellsDicc(new Tuple <int, int>(i, j)); } } return(result); }
protected virtual float SearchAndPathToTargetAction() { float cost = SearchCost; Target = null; WorldObject currentWorldObj; Vector2Int topLeftCorner; Vector2Int position = Vector2Int.zero; int radius, x, y, yIncrement; // Incremental radius search for (radius = 1; radius < CurrentWorld.SearchRadius && Target == null; radius++) { topLeftCorner = WorldPosition2D - Vector2Int.one * radius; for (x = 0; x <= radius * 2 && Target == null; x++) { yIncrement = (x == 0 || x == radius * 2) ? 1 : radius * 2; for (y = 0; y <= radius * 2 && Target == null; y += yIncrement) { position.x = topLeftCorner.x + x; position.y = topLeftCorner.y + y; if (position != WorldPosition2D) { currentWorldObj = CurrentTerrain.GetCellContent(position); if (IsInterestingObj(currentWorldObj)) { Target = currentWorldObj; } } } } } if (Target != null) { if (cost < NullEnergyCost) { cost = TryPathToTarget(); } else { cost += TryPathToTarget(); } } return(cost); }
protected virtual float RandomMovementAction() { bool movementDone = false; Vector2Int nextDirection; nextDirection = MathFunctions.PseudorandomDirection(WorldPosition2D, Direction, RandomGenerator, ConserveDirectionProbability, CurrentTerrain.IsPosAccesible); // If exists some possible movement if (!nextDirection.Equals(Vector2Int.zero)) { movementDone = CurrentTerrain.TryMoveToCell(this, WorldPosition2D + nextDirection); } return(movementDone ? RandomMoveCost : NotActionCost); }