Ejemplo n.º 1
0
        public override IEnumerable <Status> Run()
        {
            Creature.IsCloaked = false;
            if (Faction == null)
            {
                Faction = Agent.Faction;
            }
            Zone zone = Faction.GetNearestStockpile(Agent.Position, (s) => !s.IsFull() && Resources.All(resource => s.IsAllowed(resource.ResourceType)));

            if (zone != null)
            {
                var resourcesToStock = Creature.Inventory.Resources.Where(a => a.MarkedForRestock).ToList();
                foreach (var resource in resourcesToStock)
                {
                    List <Body> createdItems = Creature.Inventory.RemoveAndCreate(new ResourceAmount(resource.Resource), Inventory.RestockType.RestockResource);

                    foreach (Body b in createdItems)
                    {
                        if (zone.AddItem(b))
                        {
                            Creature.NoiseMaker.MakeNoise("Stockpile", Creature.AI.Position);
                            Creature.Stats.NumItemsGathered++;
                            Creature.CurrentCharacterMode = Creature.AttackMode;
                            Creature.Sprite.ResetAnimations(Creature.AttackMode);
                            Creature.Sprite.PlayAnimations(Creature.AttackMode);

                            while (!Creature.Sprite.AnimPlayer.IsDone())
                            {
                                yield return(Status.Running);
                            }

                            yield return(Status.Running);
                        }
                    }
                }
            }

            Timer waitTimer = new Timer(1.0f, true);
            bool  removed   = Faction.RemoveResources(Resources, Agent.Position);

            if (!removed)
            {
                yield return(Status.Fail);
            }
            else
            {
                foreach (ResourceAmount resource in Resources)
                {
                    Agent.Creature.Inventory.AddResource(resource.CloneResource(), Inventory.RestockType.None);
                }
                Agent.Creature.Sprite.ResetAnimations(Creature.AttackMode);
                while (!waitTimer.HasTriggered)
                {
                    Agent.Creature.CurrentCharacterMode = Creature.AttackMode;
                    waitTimer.Update(DwarfTime.LastTime);
                    yield return(Status.Running);
                }
                Agent.Creature.CurrentCharacterMode = CharacterMode.Idle;
                yield return(Status.Success);
            }
        }
Ejemplo n.º 2
0
        public override void Initialize()
        {
            bool hasAllResources = true;

            if (Resources != null)
            {
                foreach (Quantitiy <Resource.ResourceTags> resource in Resources)
                {
                    if (!Creature.Inventory.HasResource(resource, AllowHeterogenous))
                    {
                        hasAllResources = false;
                    }
                }
            }
            else if (ResourcesToStash != null)
            {
                foreach (ResourceAmount resource in ResourcesToStash)
                {
                    if (!Creature.Inventory.HasResource(resource))
                    {
                        hasAllResources = false;
                    }
                }
            }
            else
            {
                Tree = null;
                return;
            }

            if (Faction == null)
            {
                Faction = Agent.Faction;
            }


            if (!hasAllResources)
            {
                Stockpile nearestStockpile = Faction.GetNearestStockpile(Agent.Position, (pile) => !(pile is Graveyard));

                if (ResourcesToStash == null && Resources != null)
                {
                    ResourcesToStash = Faction.GetResourcesWithTags(Resources, AllowHeterogenous);
                }

                if (nearestStockpile == null || (ResourcesToStash != null && ResourcesToStash.Count == 0))
                {
                    Tree = null;
                    return;
                }
                else
                {
                    Tree = new Sequence(new Domain(() => HasResources(Agent), new GoToZoneAct(Agent, nearestStockpile)),
                                        new Sequence(new Condition(() => HasResources(Agent)), new StashResourcesAct(Agent, ResourcesToStash)
                    {
                        Faction = Faction
                    }),
                                        new SetBlackboardData <List <ResourceAmount> >(Agent, "ResourcesStashed", ResourcesToStash)
                                        );
                }
            }
            else
            {
                Tree = new SetBlackboardData <List <ResourceAmount> >(Agent, "ResourcesStashed", ResourcesToStash);
            }

            base.Initialize();
        }