ZoneController.DataLocation GetClosestPredator(AnimalScript.AnimalData animalData, List <int> zonesInSightRange) { ZoneController.DataLocation closestPredator = new ZoneController.DataLocation(ZoneController.DataLocation.DataType.None, -1); float predatorDistance = -1; for (int i = 0; i < zonesInSightRange.Count; i++) { for (int j = 0; j < predatorFoodTypes.Length; j++) { List <ZoneController.DataLocation> animalsInZone = ZoneCalculator.GetOrganismsInZoneByFoodType(organismsByFoodTypeInZones, zonesInSightRange[i], predatorFoodTypes[j]); for (int f = 0; f < animalsInZone.Count; f++) { if (animalsInZone[f].dataType == ZoneController.DataLocation.DataType.Animal) { float directDistance = GetDistance(animalData.position, allAnimals[animalsInZone[f].dataIndex].position); if (!(DistanceInSmellRange(directDistance) || DistanceInSightRange(GetClosestDistanceFromTwoPositions(animalData.animalEyePosition, allAnimals[animalsInZone[f].dataIndex].position)))) { continue; } if (closestPredator.dataType == ZoneController.DataLocation.DataType.None || directDistance < predatorDistance) { closestPredator = animalsInZone[f]; predatorDistance = directDistance; } } } } } return(closestPredator); }
private void PlaceNewZoneUpToPosition(Vector3 endPoint) { Vector3Int minPoint = Vector3Int.FloorToInt(_startPoint); Vector3Int maxPoint = Vector3Int.FloorToInt(endPoint); ZoneCalculator.PrepareStartAndEndPoints(_startPoint, endPoint, ref minPoint, ref maxPoint, _mapBottomLeftCorner); HashSet <Vector3Int> newPositionsSet = _grid.GetAllPositionsFromTo(minPoint, maxPoint); newPositionsSet = CalculateZoneCost(newPositionsSet); _previousEndPosition = endPoint; ZoneCalculator.CalculateZone(newPositionsSet, _structuresToBeModified, _gameObjectsToReuse); foreach (var positionToPlaceStructure in newPositionsSet) { if (_grid.IsCellTaken(positionToPlaceStructure)) { continue; } GameObject structureToAdd = null; if (_gameObjectsToReuse.Count > 0) { var gameObjectToReuse = _gameObjectsToReuse.Dequeue(); gameObjectToReuse.SetActive(true); structureToAdd = _placementManager.MoveStructureOnTheMap(positionToPlaceStructure, gameObjectToReuse, _structureData.prefab); } else { structureToAdd = _placementManager.CreateGhostStructure(positionToPlaceStructure, _structureData.prefab); } _structuresToBeModified.Add(positionToPlaceStructure, structureToAdd); } }
public void ZoneCalculatorTestCalculateZoneSmallerhanPreviousPlacement() { HashSet <Vector3Int> newPositionsList = new HashSet <Vector3Int>(); Dictionary <Vector3Int, GameObject> structuresToBeModified = new Dictionary <Vector3Int, GameObject>(); GameObject structure = new GameObject(); Queue <GameObject> gameObjectsToReuse = new Queue <GameObject>(); newPositionsList.Add(new Vector3Int(0, 0, 0)); newPositionsList.Add(new Vector3Int(3, 0, 0)); newPositionsList.Add(new Vector3Int(6, 0, 0)); structuresToBeModified.Add(Vector3Int.zero, structure); structuresToBeModified.Add(new Vector3Int(3, 0, 0), structure); structuresToBeModified.Add(new Vector3Int(6, 0, 0), structure); structuresToBeModified.Add(new Vector3Int(9, 0, 0), structure); ZoneCalculator.CalculateZone(newPositionsList, structuresToBeModified, gameObjectsToReuse); Assert.IsTrue(structuresToBeModified.Count == 3); Assert.IsTrue(structuresToBeModified.ContainsKey(new Vector3Int(3, 0, 0))); Assert.IsTrue(structuresToBeModified.ContainsKey(new Vector3Int(6, 0, 0))); Assert.IsTrue(gameObjectsToReuse.Count == 1); Assert.IsTrue(structure.activeSelf == false); Assert.IsTrue(newPositionsList.Count == 0); }
public void ZoneCalculatorTestCheckIfpositionHasChangedTrue() { GridStructure grid = new GridStructure(3, 10, 10); Vector3 gridPosition = new Vector3(3, 0, 3); Vector3 previousPosition = new Vector3(2, 0, 2); Assert.IsTrue(ZoneCalculator.CheckIfPositionHasChanged(gridPosition, previousPosition, grid)); }
private static void Main(string[] args) { ZoneCalculator zoneCalc1 = ShippingCalculatorDelegate.Zone1; ZoneCalculator zoneCalc2 = ShippingCalculatorDelegate.Zone2; ZoneCalculator zoneCalc3 = ShippingCalculatorDelegate.Zone3; ZoneCalculator zoneCalc4 = ShippingCalculatorDelegate.Zone4; Console.Write("Please enter zone (1 - 4): "); // Throw exception when entered number is null var zoneSwitch = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); if (zoneSwitch > 0 || zoneSwitch <= 4) { switch (zoneSwitch) { case 1: Console.Write("Please enter price: "); // Throw exception when entered number is null double zone1 = Double.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); double Zone1 = zoneCalc1(zone1); Console.WriteLine("Calculated price is {0}", Zone1); break; case 2: Console.Write("Please enter price: "); // Throw exception when entered number is null double zone2 = Double.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); double Zone2 = zoneCalc2(zone2); Console.WriteLine("Calculated price is {0}", Zone2); break; case 3: Console.Write("Please enter price: "); // Throw exception when entered number is null double zone3 = Double.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); double Zone3 = zoneCalc3(zone3); Console.WriteLine("Calculated price is {0}", Zone3); break; case 4: Console.Write("Please enter price: "); // Throw exception when entered number is null double zone4 = Double.Parse(Console.ReadLine() ?? throw new InvalidOperationException()); double Zone4 = zoneCalc4(zone4); Console.WriteLine("Calculated price is {0}", Zone4); break; default: break; } } else { Console.WriteLine("Please enter a valid zone (1 - 4)"); } Console.ReadKey(); }
public void ZoneCalculatorTestPrepareStartAndEndPositionTopRightBOttomLeft() { Vector3 startPosition = new Vector3(3, 0, 3); Vector3 endPosition = Vector3.zero; Vector3Int minPoint = Vector3Int.FloorToInt(startPosition); Vector3Int maxPoint = Vector3Int.FloorToInt(endPosition); ZoneCalculator.PrepareStartAndEndPoints(startPosition, endPosition, ref minPoint, ref maxPoint, Vector3.zero); Assert.AreEqual(Vector3Int.FloorToInt(startPosition), maxPoint); Assert.AreEqual(Vector3Int.FloorToInt(endPosition), minPoint); }
public void ZoneCalculatorTestPrepareStartAndEndPositionBottomLeftTopRight() { Vector3 startPosition = new Vector3(3, 0, 0); Vector3 endPosition = new Vector3(0, 0, 3); Vector3Int minPoint = Vector3Int.FloorToInt(startPosition); Vector3Int maxPoint = Vector3Int.FloorToInt(endPosition); ZoneCalculator.PrepareStartAndEndPosition(startPosition, endPosition, ref minPoint, ref maxPoint, Vector3.zero); Assert.AreEqual(new Vector3Int((int)startPosition.x, 0, (int)endPosition.z), maxPoint); Assert.AreEqual(new Vector3Int((int)endPosition.x, 0, (int)startPosition.z), minPoint); }
public override void PrepareStructureForModification(Vector3 inputPosition, string structureName, StructureType structureType) { base.PrepareStructureForModification(inputPosition, structureName, structureType); Vector3 gridPositon = grid.CalculateGridPosition(inputPosition); if (startPositionAcquired == false && grid.IsCellTaken(gridPositon) == false) { startPosition = gridPositon; startPositionAcquired = true; } if (startPositionAcquired && previousEndPositon == null || ZoneCalculator.CheckIfPositionHasChanged(gridPositon, previousEndPositon.Value, grid)) { PlaceNewZoneUpTo(gridPositon); } }
ZoneController.DataLocation GetClosestBestMouthFood(AnimalScript.AnimalData animalData) { ZoneController.DataLocation closestBestFood = new ZoneController.DataLocation(ZoneController.DataLocation.DataType.None, -1); float foodDistance = -1; List <int> zonesInMouthRange = ZoneCalculator.GetNearbyZones(zones, neiboringZones, animalData.zone, animalData.animalMouthPosition, speciesEatRange); for (int j = 0; j < eddibleFoodTypes.Length; j++) { for (int i = 0; i < zonesInMouthRange.Count; i++) { List <ZoneController.DataLocation> foodInZone = ZoneCalculator.GetOrganismsInZoneByFoodType(organismsByFoodTypeInZones, zonesInMouthRange[i], eddibleFoodTypes[j]); for (int f = 0; f < foodInZone.Count; f++) { if (foodInZone[f].dataType == ZoneController.DataLocation.DataType.Plant) { float mouthDistance = GetDistance(animalData.animalMouthPosition, allPlants[foodInZone[f].dataIndex].position); if (!DistanceInEatRange(mouthDistance)) { continue; } if (!(closestBestFood.dataType == ZoneController.DataLocation.DataType.None || mouthDistance < foodDistance)) { continue; } closestBestFood = foodInZone[f]; foodDistance = mouthDistance; continue; } if (foodInZone[f].dataType == ZoneController.DataLocation.DataType.Animal) { float mouthDistance = GetDistance(animalData.animalMouthPosition, allAnimals[foodInZone[f].dataIndex].position); if (!DistanceInEatRange(mouthDistance)) { continue; } if (!(closestBestFood.dataType == ZoneController.DataLocation.DataType.None || mouthDistance < foodDistance)) { continue; } closestBestFood = foodInZone[f]; foodDistance = mouthDistance; continue; } } } } return(closestBestFood); }
ZoneController.DataLocation GetClosestBestSightFood(AnimalScript.AnimalData animalData, List <int> zonesInSightRange) { ZoneController.DataLocation closestBestFood = new ZoneController.DataLocation(ZoneController.DataLocation.DataType.None, -1); float foodDistance = -1; for (int j = 0; j < eddibleFoodTypes.Length; j++) { for (int i = 0; i < zonesInSightRange.Count; i++) { List <ZoneController.DataLocation> foodInZone = ZoneCalculator.GetOrganismsInZoneByFoodType(organismsByFoodTypeInZones, zonesInSightRange[i], eddibleFoodTypes[j]); for (int f = 0; f < foodInZone.Count; f++) { if (foodInZone[f].dataType == ZoneController.DataLocation.DataType.Plant) { float directDistance = GetDistance(animalData.position, allPlants[foodInZone[f].dataIndex].position); if (!(DistanceInSmellRange(directDistance) || DistanceInSightRange(GetEyeDistance(animalData.animalEyePosition, allPlants[foodInZone[f].dataIndex].position)))) { continue; } if (!(closestBestFood.dataType == ZoneController.DataLocation.DataType.None || directDistance < foodDistance)) { continue; } closestBestFood = foodInZone[f]; foodDistance = directDistance; continue; } if (foodInZone[f].dataType == ZoneController.DataLocation.DataType.Animal) { float directDistance = GetDistance(animalData.position, allAnimals[foodInZone[f].dataIndex].position); if (!(DistanceInSmellRange(directDistance) || DistanceInSightRange(GetEyeDistance(animalData.animalEyePosition, allAnimals[foodInZone[f].dataIndex].position)))) { continue; } if (!(closestBestFood.dataType == ZoneController.DataLocation.DataType.None || directDistance < foodDistance)) { continue; } closestBestFood = foodInZone[f]; foodDistance = directDistance; continue; } } } } return(closestBestFood); }
FindZoneController.FindZoneData FindNearbyZone(int index) { List <int> nearbyZones = ZoneCalculator.GetNearbyZones(zones, neiboringZones, findZones[index].zone, findZones[index].position, findZones[index].range); float distance = GetDistanceBetweenPoints(findZones[index].position, zones[nearbyZones[0]].position); int zoneIndex = nearbyZones[0]; for (int i = 1; i < nearbyZones.Count; i++) { float newDistance = GetDistanceBetweenPoints(findZones[index].position, zones[nearbyZones[i]].position); if (newDistance < distance) { distance = newDistance; zoneIndex = nearbyZones[i]; } } return(new FindZoneController.FindZoneData(findZones[index], zoneIndex)); }
ZoneController.DataLocation GetClosestAvailableMate(AnimalScript.AnimalData animalData, List <int> zonesInSightRange) { ZoneController.DataLocation closestMate = new ZoneController.DataLocation(ZoneController.DataLocation.DataType.None, -1); float mateDistance = -1; for (int i = 0; i < zonesInSightRange.Count; i++) { List <ZoneController.DataLocation> organismsInZone = ZoneCalculator.GetOrganismsInZoneByFoodType(organismsByFoodTypeInZones, zonesInSightRange[i], speciesFoodType); for (int f = 0; f < organismsInZone.Count; f++) { if (organismsInZone[f].dataType != ZoneController.DataLocation.DataType.Animal) { continue; } if (allAnimals[organismsInZone[f].dataIndex].speciesIndex != animalData.speciesIndex) { continue; } if (animalData.animalHasMate || allAnimals[organismsInZone[f].dataIndex].animalHasMate) { continue; } if (animalData.animalSex == allAnimals[organismsInZone[f].dataIndex].animalSex) { continue; } float directDistance = GetDistance(animalData.position, allAnimals[organismsInZone[f].dataIndex].position); if (!(DistanceInSmellRange(directDistance) || DistanceInSightRange(GetEyeDistance(animalData.animalEyePosition, allAnimals[organismsInZone[f].dataIndex].position)))) { continue; } if (!(closestMate.dataType == ZoneController.DataLocation.DataType.None || directDistance < mateDistance)) { continue; } closestMate = organismsInZone[f]; mateDistance = directDistance; } } return(closestMate); }
public void Execute(int animalIndex) { AnimalScript.AnimalData animal = allAnimals[updateAnimals[animalIndex]]; if (animal.zone == -1) { Debug.LogError("Animal zone was not set. stage: " + animal.stage + " species: " + animal.speciesIndex + " animalIndex: " + animal.animalIndex); } List <int> zonesInSightRange; if (speciesEyeType == EyesScript.EyeTypes.Foward) { zonesInSightRange = ZoneCalculator.GetNearbyZones(zones, neiboringZones, animal.zone, animal.animalEyePosition.c0, speciesSightRange); } else { zonesInSightRange = ZoneCalculator.GetNearbyZonesFromTwoPositions(zones, neiboringZones, animal.zone, animal.animalEyePosition, speciesSightRange); } ZoneController.DataLocation closestPredator = GetClosestPredator(animal, zonesInSightRange); if (closestPredator.dataIndex != -1) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.RunFromPredator, closestPredator); return; } if (!IsAnimalFull(animal)) { ZoneController.DataLocation closestBestMouthFood = GetClosestBestMouthFood(animal); if (closestBestMouthFood.dataType != ZoneController.DataLocation.DataType.None) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.EatFood, closestBestMouthFood); return; } if (IsAnimalHungry(animal)) { ZoneController.DataLocation closestBestSightFood = GetClosestBestSightFood(animal, zonesInSightRange); if (closestBestSightFood.dataType != ZoneController.DataLocation.DataType.None) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.GoToFood, closestBestSightFood); return; } } } if (!IsAnimalHungry(animal)) { if (IsAnimalMature(animal) && DoesAnimalHaveMate(animal)) { if (AnimalPairReadyToAttemptReproduction(animal)) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.AttemptReproduction, new ZoneController.DataLocation()); return; } } else { ZoneController.DataLocation closestAvailableMate = GetClosestAvailableMate(animal, zonesInSightRange); if (closestAvailableMate.dataType != ZoneController.DataLocation.DataType.None) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.AttemptToMate, closestAvailableMate); return; } } } if (IsAnimalHungry(animal) || !DoesAnimalHaveMate(animal)) { animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.Explore); return; } animalActions[animalIndex] = new AnimalScript.AnimalActions(AnimalScript.AnimalActions.ActionType.Idle); }