Esempio n. 1
0
        public void Setup(RTSAgent agent)
        {
            Agent = agent;
            Body  = agent.Body;

            if (Agent.GetAbility <Attack>() && Agent.GetAbility <Attack>().IsOffensive)
            {
                AgentAI.Add(new OffensiveAI());
            }

            if (Agent.GetAbility <Harvest>())
            {
                AgentAI.Add(new HarvesterAI());
            }

            if (Agent.GetAbility <Construct>())
            {
                AgentAI.Add(new ConstructorAI());
            }

            foreach (var AI in AgentAI)
            {
                AI.OnSetup(agent);
            }
        }
Esempio n. 2
0
        private void Collect()
        {
            cachedMove.StopMove();
            CachedBody.Priority = _increasePriority ? basePriority + 1 : basePriority;

            long collect = CollectionAmount;

            // make sure that the harvester cannot collect more than it can carry
            if (currentLoadAmount + collect > Capacity)
            {
                collect = Capacity - currentLoadAmount;
            }

            if (resourceTarget.GetAbility <ResourceDeposit>().IsEmpty())
            {
                cachedAI.DecideWhatToDo();
            }
            else
            {
                resourceTarget.GetAbility <ResourceDeposit>().Remove(collect);
            }
            currentLoadAmount += collect;

            if (currentLoadAmount >= Capacity)
            {
                IsHarvesting = false;
                IsEmptying   = true;
            }
        }
Esempio n. 3
0
        public void SetConstructQueue()
        {
            while (ConstructQueue.Count > 0)
            {
                QStructure qStructure = ConstructQueue.Dequeue();
                if (qStructure.IsNotNull())
                {
                    RTSAgent  newRTSAgent  = Agent.Controller.CreateAgent(qStructure.StructureName, qStructure.BuildPoint, qStructure.RotationPoint) as RTSAgent;
                    Structure newStructure = newRTSAgent.GetAbility <Structure>();

                    if (newStructure.StructureType == StructureType.Wall)
                    {
                        newRTSAgent.transform.localScale = qStructure.LocalScale.ToVector3();
                        newStructure.IsOverlay           = true;
                    }

                    newRTSAgent.Body.HalfWidth  = qStructure.HalfWidth;
                    newRTSAgent.Body.HalfLength = qStructure.HalfLength;

                    newStructure.BuildSizeLow  = (newRTSAgent.Body.HalfWidth.CeilToInt() * 2);
                    newStructure.BuildSizeHigh = (newRTSAgent.Body.HalfLength.CeilToInt() * 2);

                    if (GridBuilder.Place(newRTSAgent.GetAbility <Structure>(), newRTSAgent.Body._position))
                    {
                        Agent.GetCommander().CachedResourceManager.RemoveResources(newRTSAgent);

                        newRTSAgent.SetPlayingArea(Agent.GetPlayerArea());
                        newRTSAgent.SetCommander(Agent.GetCommander());

                        newRTSAgent.gameObject.name  = newRTSAgent.objectName;
                        newRTSAgent.transform.parent = newStructure.StructureType == StructureType.Wall ? WallPositioningHelper.OrganizerWalls.transform
                            : ConstructionHandler.OrganizerStructures.transform;

                        newStructure.AwaitConstruction();
                        // Set to transparent material until constructor is in range to start
                        ConstructionHandler.SetTransparentMaterial(newStructure.gameObject, GameResourceManager.AllowedMaterial, true);

                        if (CurrentProject.IsNull())
                        {
                            CurrentProject = newRTSAgent;
                            StartConstructMove();
                        }
                    }
                    else
                    {
                        Debug.Log("Couldn't place building!");
                        newRTSAgent.Die();
                    }
                }
            }
        }
Esempio n. 4
0
        public void StartHarvest(RTSAgent resource)
        {
            if (resource != Agent && resource != null)
            {
                Agent.Tag = AgentTag.Harvester;
                //if (audioElement != null)
                //{
                //    audioElement.Play(startHarvestSound);
                //}
                resourceTarget = resource;
                ResourceType resourceType = resource.GetAbility <ResourceDeposit>().ResourceType;
                // we can only collect one resource at a time, other resources are lost
                if (resourceType == ResourceType.Unknown || resourceType != HarvestType)
                {
                    HarvestType       = resourceType;
                    currentLoadAmount = 0;
                }
                IsHarvesting = true;
                IsCasting    = true;
                IsEmptying   = false;

                if (!CheckRange(resourceTarget.Body))
                {
                    StartHarvestMove(resourceTarget.Body._position);
                }
            }
        }
Esempio n. 5
0
        protected virtual void OnHit(RTSAgent target, uint agentVersion, AgentController controller)
        {
            //If the shooter died, certain effects or records can't be completed
            bool         isCurrent = Agent != null && agentVersion == Agent.SpawnVersion;
            Health       healther  = target.GetAbility <Health>();
            AttackerInfo info      = new AttackerInfo(isCurrent ? Agent : null, controller);

            healther.TakeDamage(Damage, info);
            CallExtraOnHit(target, isCurrent);
        }
Esempio n. 6
0
        protected override void OnExecute(Command com)
        {
            //first check if queue command
            QueueStructure qStructure;

            if (com.TryGetData(out qStructure))
            {
                ConstructQueue.Enqueue(qStructure.Value);
            }
            else
            {
                DefaultData target;
                if (com.TryGetData(out target))
                {
                    IsFocused     = true;
                    IsBuildMoving = false;
                    Agent.Tag     = AgentTag.Builder;

                    // construction hasn't started yet, only a bool given
                    if (target.Is(DataType.Bool) && ConstructQueue.Count > 0)
                    {
                        if ((bool)target.Value)
                        {
                            SetConstructQueue();
                        }
                        else
                        {
                            ConstructQueue.Clear();
                        }
                    }
                    // otherwise this is another agent coming to help
                    // should have been sent local id of target
                    else if (target.Is(DataType.UShort))
                    {
                        RTSAgent tempTarget;
                        ushort   targetValue = (ushort)target.Value;
                        if (AgentController.TryGetAgentInstance(targetValue, out tempTarget))
                        {
                            RTSAgent building = tempTarget;
                            if (building && building.GetAbility <Structure>().NeedsConstruction)
                            {
                                CurrentProject = building;
                                StartConstructMove();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public void StartHarvest(RTSAgent resource)
        {
            resourceTarget = resource;
            ResourceType resourceType = resourceTarget.GetAbility <ResourceDeposit>().ResourceType;

            // we can only collect one resource at a time, other resources are lost
            if (resourceType == ResourceType.Unknown || resourceType != HarvestType)
            {
                HarvestType       = resourceType;
                currentLoadAmount = 0;
            }

            IsHarvesting = true;
            IsCasting    = true;
            IsEmptying   = false;

            if (!CheckRange(resourceTarget.Body))
            {
                MoveToDestination(resourceTarget.Body._position);
            }
        }
Esempio n. 8
0
 protected void ProcessBuildQueue()
 {
     currentSpawnProgress += spawnIncrement;
     if (currentSpawnProgress > _maxSpawnProgress)
     {
         if (PlayerManager.MainController.Commander)
         {
             //if (audioElement != null)
             //{
             //    audioElement.Play(finishedJobSound);
             //}
             Vector2d spawnOutside = new Vector2d(this.transform.position);
             LSAgent  agent        = PlayerManager.MainController.CreateAgent(buildQueue.Dequeue(), spawnOutside);
             RTSAgent newUnit      = agent.GetComponent <RTSAgent>();
             if (newUnit && spawnPoint != rallyPoint)
             {
                 newUnit.GetAbility <Move>().StartMove(rallyPoint.ToVector2d());
             }
         }
         currentSpawnProgress = 0;
     }
 }
Esempio n. 9
0
        protected override void OnExecute(Command com)
        {
            DefaultData target;

            if (com.TryGetData <DefaultData>(out target) && target.Is(DataType.UShort))
            {
                IsFocused     = true;
                IsBuildMoving = false;
                LSAgent tempTarget;
                ushort  targetValue = (ushort)target.Value;
                if (AgentController.TryGetAgentInstance(targetValue, out tempTarget))
                {
                    RTSAgent building = (RTSAgent)tempTarget;
                    if (building && building.GetAbility <Structure>().UnderConstruction())
                    {
                        SetBuilding(building);
                    }
                }
                else
                {
                    Debug.Log("nope");
                }
            }
        }
Esempio n. 10
0
        public void Engage(RTSAgent other)
        {
            if (other != Agent && other != null)
            {
                cachedTargetHealth = other.GetAbility <Health>();
                if (cachedTargetHealth.IsNotNull())
                {
                    OnEngage(other);
                    Target             = other;
                    targetVersion      = Target.SpawnVersion;
                    IsCasting          = true;
                    fastRangeToTarget  = Range + (Target.Body.IsNotNull() ? Target.Body.Radius : 0) + Agent.Body.Radius;
                    fastRangeToTarget *= fastRangeToTarget;

                    if (!CheckRange())
                    {
                        if (CanMove)
                        {
                            cachedMove.StartMove(Target.Body.Position);
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        private void BehaveWithTarget()
        {
            if (CurrentProject && (CurrentProject.IsActive == false || !CurrentProject.GetAbility <Structure>().NeedsConstruction))
            {
                //Target's lifecycle has ended
                StopConstruction();
            }
            else
            {
                Vector2d targetDirection = CurrentProject.Body._position - CachedBody._position;
                long     fastMag         = targetDirection.FastMagnitude();

                if (!IsWindingUp)
                {
                    if (CheckRange())
                    {
                        IsBuildMoving = false;
                        if (!inRange)
                        {
                            cachedMove.StopMove();
                            inRange = true;
                        }
                        Agent.Animator.SetState(ConstructingAnimState);

                        if (!CurrentProject.GetAbility <Structure>().ConstructionStarted)
                        {
                            CurrentProject.GetAbility <Structure>().ConstructionStarted = true;
                            // Restore material
                            ConstructionHandler.RestoreMaterial(CurrentProject.gameObject);
                        }

                        long mag;
                        targetDirection.Normalize(out mag);
                        bool withinTurn = cachedAttack.TrackAttackAngle == false ||
                                          (fastMag != 0 &&
                                           CachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                           CachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= cachedAttack.AttackAngle);
                        bool needTurn = mag != 0 && !withinTurn;
                        if (needTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                        else
                        {
                            if (constructCount >= _constructInterval)
                            {
                                StartWindup();
                            }
                        }
                    }
                    else
                    {
                        cachedMove.PauseAutoStop();
                        cachedMove.PauseCollisionStop();
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(CurrentProject.Body._position);
                            CachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (inRange)
                            {
                                cachedMove.Destination = CurrentProject.Body.Position;
                            }
                            else
                            {
                                if (repathTimer.AdvanceFrame())
                                {
                                    if (CurrentProject.Body.PositionChangedBuffer &&
                                        CurrentProject.Body.Position.FastDistance(cachedMove.Destination.x, cachedMove.Destination.y) >= (repathDistance * repathDistance))
                                    {
                                        cachedMove.StartMove(CurrentProject.Body._position);
                                        //So units don't sync up and path on the same frame
                                        repathTimer.AdvanceFrames(repathRandom);
                                    }
                                }
                            }
                        }

                        if (inRange == true)
                        {
                            inRange = false;
                        }
                    }
                }

                if (IsWindingUp)
                {
                    //TODO: Do we need AgentConditional checks here?
                    windupCount += LockstepManager.DeltaTime;
                    if (windupCount >= Windup)
                    {
                        windupCount = 0;
                        Build();
                        while (this.constructCount >= _constructInterval)
                        {
                            //resetting back down after attack is fired
                            this.constructCount -= (this._constructInterval);
                        }
                        this.constructCount += Windup;
                        IsWindingUp          = false;
                    }
                }
                else
                {
                    windupCount = 0;
                }

                if (inRange)
                {
                    cachedMove.PauseAutoStop();
                    cachedMove.PauseCollisionStop();
                }
            }
        }
Esempio n. 12
0
        void BehaveWithResource()
        {
            if (!resourceTarget ||
                resourceTarget.IsActive == false ||
                resourceTarget.GetAbility <ResourceDeposit>().IsEmpty())
            {
                //Target's lifecycle has ended
                StopHarvesting();
            }
            else
            {
                SetAnimState();

                if (!IsWindingUp)
                {
                    Vector2d targetDirection = resourceTarget.Body._position - CachedBody._position;
                    long     fastMag         = targetDirection.FastMagnitude();

                    if (CheckRange(resourceTarget.Body))
                    {
                        IsHarvestMoving = false;
                        if (!inRange)
                        {
                            cachedMove.StopMove();
                            inRange = true;
                        }
                        Agent.Animator.SetState(HarvestingAnimState);

                        long mag;
                        targetDirection.Normalize(out mag);
                        bool withinTurn = cachedAttack.TrackAttackAngle == false ||
                                          (fastMag != 0 &&
                                           CachedBody.Forward.Dot(targetDirection.x, targetDirection.y) > 0 &&
                                           CachedBody.Forward.Cross(targetDirection.x, targetDirection.y).Abs() <= cachedAttack.AttackAngle);
                        bool needTurn = mag != 0 && !withinTurn;
                        if (needTurn)
                        {
                            cachedTurn.StartTurnDirection(targetDirection);
                        }
                        else
                        {
                            if (harvestCount >= _harvestInterval)
                            {
                                StartWindup();
                            }
                        }
                    }
                    else
                    {
                        cachedMove.PauseAutoStop();
                        cachedMove.PauseCollisionStop();
                        if (cachedMove.IsMoving == false)
                        {
                            cachedMove.StartMove(resourceTarget.Body._position);
                            CachedBody.Priority = basePriority;
                        }
                        else
                        {
                            if (inRange)
                            {
                                cachedMove.Destination = resourceTarget.Body.Position;
                            }
                            else
                            {
                                if (repathTimer.AdvanceFrame())
                                {
                                    if (resourceTarget.Body.PositionChangedBuffer &&
                                        resourceTarget.Body.Position.FastDistance(cachedMove.Destination.x, cachedMove.Destination.y) >= (repathDistance * repathDistance))
                                    {
                                        cachedMove.StartMove(resourceTarget.Body._position);
                                        //So units don't sync up and path on the same frame
                                        repathTimer.AdvanceFrames(repathRandom);
                                    }
                                }
                            }
                        }

                        if (inRange == true)
                        {
                            inRange = false;
                        }
                    }
                }

                if (IsWindingUp)
                {
                    //TODO: Do we need AgentConditional checks here?
                    windupCount += LockstepManager.DeltaTime;
                    if (windupCount >= Windup)
                    {
                        windupCount = 0;
                        // begin collecting resources
                        Collect();

                        while (this.harvestCount >= _harvestInterval)
                        {
                            //resetting back down after attack is fired
                            this.harvestCount -= (this._harvestInterval);
                        }
                        this.harvestCount += Windup;
                        IsWindingUp        = false;
                    }
                }
                else
                {
                    windupCount = 0;
                }

                if (inRange)
                {
                    cachedMove.PauseAutoStop();
                    cachedMove.PauseCollisionStop();
                }
            }
        }
Esempio n. 13
0
 public override void DecideWhatToDo()
 {
     base.DecideWhatToDo();
     if (Agent.Tag == AgentTag.Harvester && cachedHarvest.IsFocused)
     {
         //convert to fast list...
         List <RTSAgent> resources = new List <RTSAgent>();
         foreach (RTSAgent nearbyObject in nearbyObjects)
         {
             ResourceDeposit resource = nearbyObject.GetAbility <ResourceDeposit>();
             if (resource && !resource.IsEmpty())
             {
                 resources.Add(nearbyObject);
             }
         }
         RTSAgent nearestObject = WorkManager.FindNearestWorldObjectInListToPosition(resources, transform.position);
         if (nearestObject)
         {
             ResourceDeposit closestResource = nearestObject.GetAbility <ResourceDeposit>();
             // only harvest resources the worker is assigned to
             if (closestResource && closestResource.ResourceType == cachedHarvest.HarvestType)
             {
                 // send harvest command
                 Command harvestCom = new Command(AbilityDataItem.FindInterfacer("Harvest").ListenInputID);
                 harvestCom.Add <DefaultData>(new DefaultData(DataType.UShort, nearestObject.GlobalID));
                 UserInputHelper.SendCommand(harvestCom);
             }
         }
     }
     if (Agent.Tag == AgentTag.Builder && cachedBuild.IsFocused)
     {
         //convert to fast array
         List <RTSAgent> buildings = new List <RTSAgent>();
         foreach (RTSAgent nearbyObject in nearbyObjects)
         {
             if (nearbyObject.GetCommander() != Agent.Controller.Commander)
             {
                 continue;
             }
             RTSAgent nearbyBuilding = nearbyObject.GetComponent <RTSAgent>();
             if (nearbyBuilding && nearbyBuilding.GetAbility <Structure>().UnderConstruction())
             {
                 buildings.Add(nearbyObject);
             }
         }
         RTSAgent nearestObject = WorkManager.FindNearestWorldObjectInListToPosition(buildings, transform.position);
         if (nearestObject)
         {
             RTSAgent closestBuilding = nearestObject.GetComponent <RTSAgent>();
             if (closestBuilding)
             {
                 // send build command
                 Command buildCom = new Command(AbilityDataItem.FindInterfacer("Construct").ListenInputID);
                 buildCom.Add <DefaultData>(new DefaultData(DataType.UShort, closestBuilding.GlobalID));
                 UserInputHelper.SendCommand(buildCom);
             }
         }
         else
         {
             cachedBuild.SetCurrentProject(null);
         }
     }
 }
Esempio n. 14
0
        private void HandleCollision(LSBody other)
        {
            if (!CanMove)
            {
                return;
            }
            if ((tempAgent = other.Agent) == null)
            {
                return;
            }

            Move otherMover = tempAgent.GetAbility <Move>();

            if (ReferenceEquals(otherMover, null) == false)
            {
                if (IsMoving)
                {
                    //If the other mover is moving to a similar point
                    if (otherMover.MyMovementGroupID == MyMovementGroupID ||
                        otherMover.targetPos.FastDistance(this.targetPos) <= (closingDistance * closingDistance))
                    {
                        if (otherMover.IsMoving == false)
                        {
                            if (otherMover.Arrived &&
                                otherMover.StoppedTime > MinimumOtherStopTime)
                            {
                                Arrive();
                            }
                        }
                        else
                        {
                            if (hasPath &&
                                otherMover.hasPath &&
                                otherMover.pathIndex > 0 &&
                                otherMover.lastTargetPos.SqrDistance(targetPos.x, targetPos.y) < closingDistance.Mul(closingDistance))
                            {
                                if (this.distance < this.closingDistance)
                                {
                                    this.pathIndex++;
                                }
                            }
                        }
                    }

                    if (GetLookingForStopPause())
                    {
                        //As soon as the original collision stop unit is released, units will start breaking out of pauses
                        if (otherMover.GetCanCollisionStop() == false)
                        {
                            StopPauseLayer = -1;
                            PauseAutoStop();
                        }
                        else if (otherMover.GetCanAutoStop() == false)
                        {
                            if (otherMover.StopPauseLayer < StopPauseLayer)
                            {
                                StopPauseLayer = otherMover.StopPauseLayer + 1;
                                PauseAutoStop();
                            }
                        }
                    }
                    else
                    {
                    }
                }
            }
        }