Ejemplo n.º 1
0
        public bool AddMemory(MemoryInstance memory)
        {
            MemoryInstance recentMemory = isRecentMemory(memory);

            if (recentMemory != null)
            {
                recentMemory.AddIteration();
                //Debug.LogWarning("Iterating memory");
                return(true);
            }
            if (currentMemoryIndex + 1 == shortTermMemory.Length)
            {
                currentMemoryIndex = 0;
            }
            if (currentMemoryIndex < shortTermMemory.Length)
            {
                shortTermMemory[currentMemoryIndex] = memory;
                currentMemoryIndex++;
                if (World.ISDEBUGSCENE)
                {
                    StringBuilder builder = new StringBuilder("I will remember I ");
                    if (memory.invertVerb)
                    {
                        builder.Append(" NOT ");
                    }
                    builder.Append(memory.verb);
                    builder.Append(" " + memory.subject.GetThing().thingName);
                    //Debug.LogWarning(builder.ToString());
                    DebugMenu.AppendLine(builder.ToString());
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        // 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;
                }
            }
        }
Ejemplo n.º 3
0
        // Update is called once per frame
        void Update()
        {
            if (_target == null)
            {
                _target            = CreatureBrowser.SelectedCreature.transform;
                transform.position = _target.position + cameraOffset;
            }
            else
            {
                transform.position = Vector3.Lerp(transform.position, _target.position + cameraOffset, Time.deltaTime * 3f);
                transform.LookAt(_target);
                Vector3 positionForward;
                float   currentDistance = Vector3.Distance(transform.position, _target.position);
                // Too small of a difference to alter //
                float precision = .1f;
                if (Mathf.Abs(currentDistance - targetZoomDistance) < precision)
                {
                    return;
                }
                if (currentDistance > targetZoomDistance)
                {
                    positionForward = transform.forward * Time.deltaTime;
                }
                else
                {
                    positionForward = -transform.forward * Time.deltaTime;
                }
                cameraOffset += positionForward;
            }
            if (_ignoreInputs <= 0f)
            {
                Vector3 distanceMouseMoved = _lastMousePosition - Input.mousePosition;
                _lastMousePosition = Input.mousePosition;

                float mouseWheelAmount = Input.GetAxis("Mouse ScrollWheel");
                if (Mathf.Abs(mouseWheelAmount) > 0f)
                {
                    targetZoomDistance -= mouseWheelAmount * mouseWheelZoomModifier;
                    DebugMenu.AppendLine("Current zoom - " + targetZoomDistance);
                    if (targetZoomDistance < maxZoomIn)
                    {
                        targetZoomDistance = maxZoomIn;
                    }
                    else if (targetZoomDistance > maxZoomOut)
                    {
                        targetZoomDistance = maxZoomOut;
                    }
                }
                // Middle mouse button //
                else if (Input.GetMouseButtonDown(2))
                {
                    cameraOffset       = _defaultOffset;
                    targetZoomDistance = _defaultOffset.z;
                    transform.position = _target.position + _defaultOffset;
                    transform.LookAt(_target.position);
                }
                // Left Mouse //
                if (Input.GetMouseButtonDown(0))
                {
                    _moveWithMouse = true;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    if (_moveWithMouse)
                    {
                        _moveWithMouse = false;
                    }
                }
                // Right Mouse //
                else if (Input.GetMouseButton(1))
                {
                }
                if (distanceMouseMoved != Vector3.zero && _moveWithMouse)
                {
                    cameraOffset += transform.right * distanceMouseMoved.x * mouseLookSpeed * Time.deltaTime;
                    cameraOffset += transform.up * distanceMouseMoved.y * mouseLookSpeed * Time.deltaTime;
                }
            }
            else
            {
                _ignoreInputs -= Time.deltaTime;
            }
        }