Beispiel #1
0
    public override void OnBehaviourEnter()
    {
        switch (state)
        {
        case WorkerState.Construction:

            constructionState = ConstructionState.Idle;

            break;

        case WorkerState.Harvesting:

            if (ressourceInventory.currentRessourceTransportLoad < ressourceInventory.ressourceTransportLoad)
            {
                harvestingState = HarvestingState.Idle;
            }
            else
            {
                harvestingState = HarvestingState.TransportingRessourceToHarvester;
                Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                movement.MoveTo(targetPosition);
            }

            break;

        case WorkerState.Depositing:

            state = WorkerState.Idle;

            break;
        }
    }
Beispiel #2
0
        public override bool MoveTo(Position loc)
        {
            bool result = base.MoveTo(loc);

            if (_simulation.Players[this.ObjectID.PlayerID].FogOfWar[loc.X, loc.Y] == true)
            {
                this.harvestingState = HarvestingState.nothing;
            }
            else if (_simulation.Map.Spice[loc.X, loc.Y] > 0)
            {
                this.harvestingState = HarvestingState.harvesting;
            }
            else
            {
                bool found = false;
                foreach (Building b in _simulation.Map.Buildings[loc.X, loc.Y])
                {
                    if (b.BuildingData.IsRefinery && b.ObjectID.PlayerID == this.ObjectID.PlayerID)
                    {
                        this.harvestingState = HarvestingState.returningToBase;
                        found = true;
                    }
                }
                if (found == false)
                {
                    this.harvestingState = HarvestingState.nothing;
                }
            }
            return(result);
        }
Beispiel #3
0
 public void AssignToHarvesting(EC_HarvestingBuilding harvester)
 {
     if (state == WorkerState.Idle)
     {
         PlayerManager.Instance.RemoveIdleWorker(this);
     }
     this.assignedHarvester = harvester;
     assignedHarvesterType  = harvester.type;
     state           = WorkerState.Harvesting;
     harvestingState = HarvestingState.Idle;
     nextScanTime    = Time.time;
     ChangeAcessories();
 }
Beispiel #4
0
    protected override void Update()
    {
        switch (state)
        {
        case WorkerState.Idle:

            if (Time.time > nextIdleMovementTime)
            {
                nextIdleMovementTime = Time.time + idleMovementInterval;
                Vector3 wanderPoint = UnityEngine.Random.insideUnitSphere * 4;
                wanderPoint.y = entity.transform.position.y;

                wanderPoint += entity.transform.forward * 4 + entity.transform.position;

                Vector3 basePosition = BuildingSystem.Instance.playersBaseLocation.position;
                //if he would stray off to far, bring him back to base
                if (Vector3.Distance(basePosition, wanderPoint) > maxDistanceToBaseForWander)
                {
                    wanderPoint += (basePosition - wanderPoint) / 4;
                }

                movement.MoveTo(wanderPoint);
            }

            break;

        case WorkerState.Construction:

            switch (constructionState)
            {
            case ConstructionState.Idle:

                //if idle /first get nearest Building  which needs construction, if there is no we just chill beside the base
                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (BuildingSystem.Instance.AreThereBuildingsToConstruct())
                    {
                        standingBesideBaseAndWaiting = false;

                        BuildingInConstruction nearestBuildingToConstruct = null;
                        float nearestDistance = Mathf.Infinity;

                        foreach (BuildingInConstruction building in BuildingSystem.Instance.GetBuildingsWaitingForConstruction())
                        {
                            float currentDistance = (building.transform.position - entity.transform.position).sqrMagnitude;

                            if (currentDistance < nearestDistance)
                            {
                                nearestDistance            = currentDistance;
                                nearestBuildingToConstruct = building;
                            }
                        }

                        assignedBuildingToBuild = nearestBuildingToConstruct;
                        //we move to a position from where we can build, not to a position inside the building
                        Vector3 targetPosition = nearestBuildingToConstruct.transform.position + (entity.transform.position - nearestBuildingToConstruct.transform.position).normalized * nearestBuildingToConstruct.width / 2;
                        movement.MoveTo(targetPosition);
                        constructionState = ConstructionState.MovingToBuilding;
                    }
                    else if (!standingBesideBaseAndWaiting)
                    {
                        Vector3 positonToMoveTo = UnityEngine.Random.insideUnitSphere * 8;
                        positonToMoveTo += BuildingSystem.Instance.playersBaseLocation.position;
                        standingBesideBaseAndWaiting = true;
                        movement.MoveTo(positonToMoveTo);
                    }
                }

                break;

            case ConstructionState.MovingToBuilding:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (assignedBuildingToBuild != null)
                    {
                        if (Vector3.Distance(entity.transform.position, assignedBuildingToBuild.transform.position) < assignedBuildingToBuild.width + constructionRange)
                        {
                            movement.Stop();
                            constructionState = ConstructionState.Constructing;
                            constructionTool.StartAnimation();
                            nextConstructionTime = Time.time + constructionInterval;
                        }
                    }
                    else
                    {
                        constructionState = ConstructionState.Idle;
                    }
                }

                break;

            case ConstructionState.Constructing:

                if (Time.time > nextConstructionTime)
                {
                    //check if it istn completed yet
                    if (assignedBuildingToBuild != null)
                    {
                        nextConstructionTime = Time.time + constructionInterval;
                        assignedBuildingToBuild.Construct(constructionPoints);
                    }
                    else
                    {
                        constructionState = ConstructionState.Idle;
                        constructionTool.StopAnimation();
                    }
                }

                break;
            }



            break;

        case WorkerState.Harvesting:

            switch (harvestingState)
            {
            case HarvestingState.Idle:

                //check if there are some ressources in the area - should it check with physics check or get the nearest from the ressourcesmanager?
                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    HashSet <Ressource> ressources = RessourcesManager.Instance.GetRessources(assignedHarvesterType);

                    if (ressources.Count > 0)
                    {
                        standingBesideHarvesterAndWaiting = false;


                        Ressource nearestRessource = null;
                        float     nearestDistance  = Mathf.Infinity;

                        foreach (Ressource ressource in ressources)
                        {
                            float currentDistance = (ressource.transform.position - assignedHarvester.transform.position).sqrMagnitude;

                            if (currentDistance < nearestDistance)
                            {
                                nearestDistance  = currentDistance;
                                nearestRessource = ressource;
                            }
                        }

                        currentSelectedRessource = nearestRessource;
                        Vector3 targetPosition = currentSelectedRessource.transform.position + (entity.transform.position - currentSelectedRessource.transform.position).normalized * currentSelectedRessource.width / 2;
                        movement.MoveTo(targetPosition);
                        harvestingState = HarvestingState.MovingToRessource;
                    }
                    else if (!standingBesideHarvesterAndWaiting)
                    {
                        Vector3 positonToMoveTo = UnityEngine.Random.insideUnitSphere * 5;
                        positonToMoveTo += assignedHarvester.transform.position;
                        standingBesideHarvesterAndWaiting = true;
                        movement.MoveTo(positonToMoveTo);
                    }
                }

                break;

            case HarvestingState.MovingToRessource:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;


                    if (currentSelectedRessource != null)
                    {
                        if (Vector3.Distance(entity.transform.position, currentSelectedRessource.transform.position) < currentSelectedRessource.width)
                        {
                            movement.Stop();
                            harvestingState = HarvestingState.GatheringRessource;

                            //activate the tool
                            if (assignedHarvesterType == RessourceType.fer)
                            {
                                ferHarvestingTool.StartAnimation();
                            }
                            else if (assignedHarvesterType == RessourceType.mer)
                            {
                                merHarvestingTool.StartAnimation();
                            }

                            nextRessourceGatheringTime = Time.time + ressourceGatherInterval;
                        }
                    }
                    else
                    {
                        harvestingState = HarvestingState.Idle;
                        ferHarvestingTool.StopAnimation();
                        merHarvestingTool.StopAnimation();
                    }
                }

                break;

            case HarvestingState.GatheringRessource:

                if (Time.time > nextRessourceGatheringTime)
                {
                    //check if it istn completed yet
                    if (currentSelectedRessource != null)
                    {
                        nextRessourceGatheringTime = Time.time + ressourceGatherInterval;
                        //gather but check how much will fit
                        if (ressourceInventory.currentRessourceTransportLoad + ressourceGatheringPower > ressourceInventory.ressourceTransportLoad)
                        {
                            ressourceInventory.AddRessource(currentSelectedRessource.type, currentSelectedRessource.TakeRessource(ressourceInventory.ressourceTransportLoad - ressourceInventory.currentRessourceTransportLoad));
                        }
                        else
                        {
                            ressourceInventory.AddRessource(currentSelectedRessource.type, currentSelectedRessource.TakeRessource(ressourceGatheringPower));
                        }

                        //if the sack is full, go back
                        if (ressourceInventory.currentRessourceTransportLoad == ressourceInventory.ressourceTransportLoad)
                        {
                            Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                            movement.MoveTo(targetPosition);
                            // movement.MoveTo(assignedHarvester.transform.position);
                            harvestingState = HarvestingState.TransportingRessourceToHarvester;
                            ferHarvestingTool.StopAnimation();
                            merHarvestingTool.StopAnimation();
                        }
                    }
                    else
                    {
                        ferHarvestingTool.StopAnimation();
                        merHarvestingTool.StopAnimation();

                        if (ressourceInventory.currentRessourceTransportLoad > 0)
                        {
                            Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                            movement.MoveTo(targetPosition);
                            //movement.MoveTo(assignedHarvester.transform.position);
                            harvestingState = HarvestingState.TransportingRessourceToHarvester;
                        }
                        else
                        {
                            harvestingState = HarvestingState.Idle;
                        }
                    }
                }

                break;

            case HarvestingState.TransportingRessourceToHarvester:

                if (Time.time > nextScanTime)
                {
                    nextScanTime = Time.time + scanInterval;

                    if (Vector3.Distance(entity.transform.position, assignedHarvester.transform.position) < assignedHarvester.width)
                    {
                        movement.Stop();
                        assignedHarvester.DepotRessource(ressourceInventory.currentRessourceTransportLoad);
                        ressourceInventory.Clear();

                        if (currentSelectedRessource != null)
                        {
                            harvestingState = HarvestingState.MovingToRessource;
                            movement.MoveTo(currentSelectedRessource.transform.position);
                        }
                        else
                        {
                            harvestingState = HarvestingState.Idle;
                        }
                    }
                }
                break;
            }

            break;

        case WorkerState.Depositing:

            if (Time.time > nextScanTime)
            {
                nextScanTime = Time.time + scanInterval;

                if (despositionBuilding != null)
                {
                    if (Vector3.Distance(entity.transform.position, despositionBuilding.transform.position) < despositionBuilding.width)
                    {
                        despositionBuilding.GetComponent <IDepositioneable <B_Worker> >().DepositionWorker(this);
                    }
                }
                else
                {
                    AssignToIdle();
                }
            }


            break;
        }
    }
Beispiel #5
0
        public override void DoAI()
        {
            InfoLog.WriteInfo(DoAIPrefix + " harvester DoAI", EPrefix.AI);
            if (_remainingTurnsInMove > 0 && Moving && state == UnitState.stopped)
            {
                Move();
                InfoLog.WriteInfo(DoAIPrefix + " harvester moved", EPrefix.AI);
                return;
            }
            Position loc;

            if (harvestingState == HarvestingState.harvesting)
            {
                if (this.spiceCounter == this.HarvesterData.__Capacity)
                {
                    // harvester is full
                    this.harvestingState = HarvestingState.returningToBase;
                    this.state           = UnitState.stopped;
                    return;
                }
                #region seeking for spice
                switch (state)
                {
                case UnitState.harvesting:
                    this.spiceFindingCounter = 100;
                    int spiceOnLocaltion = _map.Spice[Position.X, Position.Y];
                    if (spiceOnLocaltion == 0)
                    {
                        // seek for spice

                        if (FindNearestSpice(this.Position, out loc))
                        {
                            // found spice
                            state = UnitState.moving;
                            knowsLastKnownPosition = true;
                            lastKnownSpicePosition = loc;
                            if (MoveTo(loc, HarvestingState.harvesting) == false)
                            {
                                this.state = UnitState.stopped;
                                return;
                            }
                            InfoLog.WriteInfo(DoAIPrefix + "harvester searching for spice", EPrefix.AI);
                        }
                        else
                        {
                            // no spice visible - stopping but still seeking for spice
                            state = UnitState.stopped;
                        }
                    }
                    else if (spiceOnLocaltion > 0)
                    {
                        // collect spise

                        this.spiceCounter += 10;
                        _map.Spice[Position.X, Position.Y]--;
                    }
                    break;

                case UnitState.stopped:
                    if (_map.Spice[Position.X, Position.Y] > 0)
                    {
                        state = UnitState.harvesting;
                        break;
                    }
                    if (FindNearestSpice(this.Position, out loc))
                    {
                        // found spice
                        state = UnitState.moving;
                        knowsLastKnownPosition = true;
                        lastKnownSpicePosition = loc;
                        if (MoveTo(loc, HarvestingState.harvesting) == false)
                        {
                            state = UnitState.stopped;
                            return;
                        }
                        InfoLog.WriteInfo(DoAIPrefix + "harvester searching for spice", EPrefix.AI);
                        break;
                    }
                    if (this.Position.X == lastKnownSpicePosition.X &&
                        this.Position.Y == lastKnownSpicePosition.Y)
                    {
                        knowsLastKnownPosition = false;
                    }
                    if (knowsLastKnownPosition)
                    {
                        state = UnitState.moving;
                        MoveTo(lastKnownSpicePosition, HarvestingState.harvesting);
                        state = UnitState.moving;
                        break;
                    }
                    if (this.spiceFindingCounter-- == 0)
                    {
                        // przeliczyl sie.
                        this.spiceFindingCounter = 100;     // magic number! - przeniesc do ustawien
                        this.state           = UnitState.stopped;
                        this.harvestingState = HarvestingState.nothing;
                        break;
                    }
                    break;

                case UnitState.moving:
                    this.spiceFindingCounter = 100;
                    if (Move() == false)
                    {
                        // stopped moving.
                        state = UnitState.stopped;
                    }
                    break;
                }
                #endregion
            }
            else if (harvestingState == HarvestingState.returningToBase)
            {
                #region returning to base

                if (FindNearestSpice(this.Position, out loc))
                {
                    knowsLastKnownPosition = true;
                    lastKnownSpicePosition = loc;
                }

                switch (state)
                {
                case UnitState.moving:
                    // returning to base
                    InfoLog.WriteInfo(DoAIPrefix + "harvester returning to base", EPrefix.AI);
                    if (Move() == false)
                    {
                        // destination reached
                        this.state = UnitState.stopped;
                    }

                    break;

                case UnitState.stopped:
                    // refinery reached or refinery destroyed
                    if (RefineryReached())
                    {
                        harvestingState = HarvestingState.unloading;
                    }
                    else
                    {
                        bool canFind;
                        if (this.refineryFindingCounter >= 9)
                        {
                            canFind = true;
                        }
                        else
                        {
                            if (this.refineryFindingCounter == 0)
                            {
                                this.refineryFindingCounter = 10;
                            }
                            canFind = false;
                        }

                        this.refineryFindingCounter--;
                        if (canFind == false)
                        {
                            return;
                        }
                        if (FindNearestRefinery(this.Position, out loc))
                        {
                            // found refinery
                            state = UnitState.moving;
                            MoveTo(loc, HarvestingState.returningToBase);
                        }
                        else
                        {
                            // no refinery found
                            state = UnitState.stopped;
                        }
                    }

                    break;
                }

                #endregion
            }
            else if (harvestingState == HarvestingState.nothing)
            {
                #region nothing
                switch (state)
                {
                case UnitState.moving:
                    if (Move() == false)
                    {
                        state = UnitState.stopped;
                    }
                    break;
                }

                #endregion
            }
            else
            {
                #region unloading
                InfoLog.WriteInfo(DoAIPrefix + "harvester unloading", EPrefix.AI);
                _simulation.Players[this.ObjectID.PlayerID].Credits += spiceCounter;
                if (spiceUnload != null)
                {
                    spiceUnload(this.ObjectID.PlayerID, spiceCounter);
                }
                this.spiceCounter    = 0;
                this.harvestingState = HarvestingState.harvesting;
                this.state           = UnitState.stopped;
                #endregion
            }
            InfoLog.WriteInfo(DoAIPrefix + "harvester end DoAI", EPrefix.AI);
        }
Beispiel #6
0
 private bool MoveTo(Position loc, HarvestingState hState)
 {
     return(base.MoveTo(loc));
 }