public Grid(RAKTerrain terrain) { Vector3 terrainSize = terrain.terrain.terrainData.size; int numberOfXElements = (int)(terrainSize.x / ELEMENT_SIZE.x); int numberOfZElements = (int)(terrainSize.z / ELEMENT_SIZE.y); elements = new GridSector[numberOfXElements * numberOfZElements]; int elementCount = 0; Vector3 terrainPosition = terrain.transform.position; for (int x = 0; x < numberOfXElements; x++) { for (int z = 0; z < numberOfZElements; z++) { Vector2 elementWorldPosition = new Vector2(terrainPosition.x + x * ELEMENT_SIZE.x, terrainPosition.z + z * ELEMENT_SIZE.y); Vector2 elementWorldPositionEnd = new Vector2( elementWorldPosition.x + ELEMENT_SIZE.x, elementWorldPosition.y + ELEMENT_SIZE.y); elements[elementCount] = new GridSector(new Vector2(x, z), elementWorldPosition, elementWorldPositionEnd, terrain.name, terrain); elementCount++; } } Debug.Log("Grid generation complete"); }
private void observeSurroundings() { // Observe Things // float thingDistance = miscVariables[MiscVariables.CreatureMiscVariables.Observe_Distance]; Thing[] allThingsCopy = Area.GetAllThingsCopy(); Thing[] thingsWithinProximity = CreatureUtilities.GetThingsWithinProximityOf(this, thingDistance, Area.GetAllThingsSync().ToArray()); foreach (Thing thing in thingsWithinProximity) { //Debug.LogWarning("Saw " + thing.name); species.memory.AddMemory(new MemoryInstance(Verb.SAW, thing, false)); } float areaDistance = Grid.ELEMENT_SIZE.sqrMagnitude * 2; // Observe Areas // GridSector[] closeAreas = CreatureUtilities.GetPiecesOfTerrainCreatureCanSee( this, areaDistance, currentArea.GetClosestTerrainToPoint(transform.position)); GridSector currentSector = currentArea.GetCurrentGridSector(transform); foreach (GridSector element in closeAreas) { if (!knownGridSectorsVisited.ContainsKey(element)) { knownGridSectorsVisited.Add(element, false); } if (currentSector == element && !knownGridSectorsVisited[currentSector]) { knownGridSectorsVisited[currentSector] = true; } } //Debug.LogWarning("Observation"); lastObserved = Time.time; }
//Creates instance of spawn sector and has it call fillSector() found on that instance. void createSector(Vector3 createAt, string name) { GameObject newSector = Instantiate(sector, createAt, this.transform.rotation); newSector.name = name; GridSector scriptReference = newSector.GetComponent <GridSector>(); scriptReference.depthDetection(deepPlant, shallowPlant); scriptReference.fillSector(plantNode, plantDensity); }
public GridSector GetClosestUnexploredSector() { GridSector closestSector = null; float closestDistance = Mathf.Infinity; foreach (GridSector sector in knownGridSectorsVisited.Keys) { // Already visited // if (knownGridSectorsVisited[sector]) { continue; } float distance = Vector2.Distance(sector.GetTwoDLerpOfSector(), transform.position); if (distance < closestDistance) { closestDistance = distance; closestSector = sector; } } return(closestSector); }
// MAIN METHOD // public void performAction(Creature performer) { elapsedTime += Time.deltaTime; //Debug.LogWarning("Elapsed Time - " + elapsedTime + " Max - " + maxAllowedTime); if (elapsedTime > maxAllowedTime) { DebugMenu.AppendLine(performer.thingName + " has exceeded task time for task. Resetting"); Debug.Log(performer.thingName + " has exceeded task time for task. Resetting"); status = Tasks.TASK_STATUS.Failed; failReason = FailReason.ExceededTimeLimit; if (_targetThing != null) { _targetThing.MakeAvailable(performer); } return; } //Debug.Log("Performing action " + action + " For Task - " + associatedTask); // LOCATE // if (action == Actions.Locate) { // LOCATE EAT // if (associatedTask == Tasks.CreatureTasks.EAT) { //Debug.LogWarning("Locating consumeable"); Thing target = performer.GetClosestKnownReachableConsumable(); if (target == null) { // Target doesn't know of any consumables // failReason = FailReason.NoKnownFood; status = Tasks.TASK_STATUS.Failed; return; } else { _targetThing = target; _targetPosition = target.transform.position; _targetThing.MakeUnavailable(); //Debug.LogWarning("Locate task complete with target - " + _targetThing.name); status = Tasks.TASK_STATUS.Complete; } } // LOCATE SLEEP // else if (associatedTask == Tasks.CreatureTasks.SLEEP) { // Search for target ground // float boxSizeMult = performer.miscVariables[MiscVariables.CreatureMiscVariables.Agent_Locate_Sleep_Area_BoxCast_Size_Multipler]; Vector3 raycastHit = performer.BoxCastNotMeGetClosestPoint(5, Vector3.down); if (raycastHit != Vector3.positiveInfinity) { raycastHit.y = performer.transform.position.y; _targetPosition = raycastHit; performer.GetCreatureAgent().SetDestination(_targetPosition); creatureAgentDestinationHasBeenSet = true; status = Tasks.TASK_STATUS.Complete; return; } else { // Get's picked up by the TaskManager Update() // status = Tasks.TASK_STATUS.Failed; failReason = FailReason.InfinityDistance; } } // LOCATE EXPLORE // else if (associatedTask == Tasks.CreatureTasks.EXPLORE) { //Debug.LogWarning("Locating explore sector"); GridSector sector = performer.GetClosestUnexploredSector(); if (sector != null) { Vector3 explorePoint = sector.GetRandomPositionInSector; _targetPosition = explorePoint; } else { //Debug.Log("No unexplored sectors!"); _targetPosition = performer.GetRandomKnownSectorPosition(); } performer.GetCreatureAgent().SetDestination(_targetPosition); creatureAgentDestinationHasBeenSet = true; status = Tasks.TASK_STATUS.Complete; return; } // LOCATE GATHER // else if (associatedTask == Tasks.CreatureTasks.GATHER) { //Thing target = performer.GetClosestKnownReachableThing(targetBaseType); } } // MOVE TO // else if (action == Actions.MoveTo) { Debug.DrawLine(performer.transform.position, _targetPosition, Color.cyan, .5f); // The agent should have a destination before getting to this point // if (!creatureAgentDestinationHasBeenSet) { failReason = FailReason.MoveToWithNoDestination; Debug.LogError("ERROR MoveTo with no destination"); status = Tasks.TASK_STATUS.Failed; return; } else // Needs to be an else since it takes a frame for the remaining distance to calculate { if (performer.getDistanceFromDestination() == Mathf.Infinity) { Debug.Log("Infinity distance"); failReason = FailReason.InfinityDistance; status = Tasks.TASK_STATUS.Failed; return; } if (associatedTask == Tasks.CreatureTasks.MOVE_AND_OBSERVE) { performer.RequestObservationUpdate(); } // Raycast if we're close enough to the target to where we should be able to see it // float distanceBeforeRayCastCheckOnTarget = performer.miscVariables [MiscVariables.CreatureMiscVariables.Agent_MoveTo_Raycast_For_Target_When_Distance_Below]; // Arrived // float distanceToCompleteArrival; if (associatedTask != Tasks.CreatureTasks.EXPLORE) { distanceToCompleteArrival = performer.getCreatureStats().getDistanceFromTargetBeforeConsideredReached(); } else { distanceToCompleteArrival = 50; } if (performer.getDistanceFromDestination() <= distanceToCompleteArrival) { status = Tasks.TASK_STATUS.Complete; return; } // Haven't arrived yet else if (_targetThing != null) { // Check if we're close enough to start raycasting // if (performer.getDistanceFromDestination() < distanceBeforeRayCastCheckOnTarget) { if (_targetThing != null) { // Raycast forward // if (!performer.IsTargetInFrontOfMe(_targetThing)) { performer.GetCreatureAgent().SetIgnoreCollisions(false); } else { performer.GetCreatureAgent().SetIgnoreCollisions(true); } } } // Not close enough, make sure orbiting is disabled // else { performer.GetCreatureAgent().SetIgnoreCollisions(false); } } } } // ADD // else if (action == Actions.Add) { CreatureAgent agent = performer.GetCreatureAgent(); // Tractor Beam // if (agent.grabType == CreatureGrabType.TractorBeam) { // Touching Target // if (Vector3.Distance(_targetThing.transform.position, performer.transform.position) < .5f) { if (performer.AddThingToInventory(_targetThing)) { performer.GetCreatureAgent().CollisionRemoveIfPresent(_targetThing.transform); _targetThing.transform.SetParent(performer.transform); status = Tasks.TASK_STATUS.Complete; return; } else { Debug.LogWarning("Failed adding thing to inventory"); failReason = FailReason.FailureAddingToInventory; status = Tasks.TASK_STATUS.Failed; return; } } } } // EAT // else if (action == Actions.Eat) { if (_targetThing.beConsumed(performer)) { performer.PlayOneShot(); performer.ConsumeThing(_targetThing); status = Tasks.TASK_STATUS.Complete; return; } } // Land // else if (action == Actions.Land) { CreatureAgent agent = performer.GetCreatureAgent(); if (!agent.IsLanding()) { // Landing failed // agent.Land(); return; } return; } // DEACTIVATE SELF // else if (action == Actions.Sleep) { if (performer.GetCurrentState() != Creature.CREATURE_STATE.SLEEP) { performer.GetCreatureAgent().Sleep(); } if (!performer.StillNeedsSleep()) { status = Tasks.TASK_STATUS.Complete; } } }