Esempio n. 1
0
        public override void Update()
        {
            base.Update();

            if (tradingPosts != null && tradingPosts.Count > 0)
            {
                ResourceManager.TradingPost tradingPost = tradingPosts[0];
                if (path.Count <= 0)
                {
                    MoveToTile(tradingPost.zeroPointTile, false);
                }
                if (overTile == tradingPost.zeroPointTile)
                {
                    foreach (ResourceManager.ReservedResources rr in tradingPost.GetInventory().TakeReservedResources(this))
                    {
                        foreach (ResourceManager.ResourceAmount ra in rr.resources)
                        {
                            caravan.GetInventory().ChangeResourceAmount(ra.resource, ra.amount, false);

                            ResourceManager.ConfirmedTradeResourceAmount confirmedTradeResourceAmount = caravan.confirmedResourcesToTrade.Find(crtt => crtt.resource == ra.resource);
                            if (confirmedTradeResourceAmount != null)
                            {
                                confirmedTradeResourceAmount.amountRemaining += ra.amount;
                            }
                        }
                    }
                    tradingPosts.RemoveAt(0);
                }
                if (tradingPosts.Count <= 0)
                {
                    JobManager.Job job = new JobManager.Job(
                        tradingPost.tile,
                        GameManager.resourceM.GetObjectPrefabByEnum(ResourceManager.ObjectEnum.CollectResources),
                        null,
                        0
                        )
                    {
                        transferResources = new List <ResourceManager.ResourceAmount>()
                    };
                    foreach (ResourceManager.ConfirmedTradeResourceAmount confirmedTradeResourceAmount in caravan.confirmedResourcesToTrade)
                    {
                        if (confirmedTradeResourceAmount.tradeAmount > 0)
                        {
                            tradingPost.GetInventory().ChangeResourceAmount(confirmedTradeResourceAmount.resource, confirmedTradeResourceAmount.tradeAmount, false);
                            confirmedTradeResourceAmount.amountRemaining = 0;
                            job.transferResources.Add(new ResourceManager.ResourceAmount(confirmedTradeResourceAmount.resource, confirmedTradeResourceAmount.tradeAmount));
                        }
                    }
                    caravan.confirmedResourcesToTrade.Clear();
                    GameManager.jobM.CreateJob(job);
                }
            }
            else
            {
                if (path.Count <= 0)
                {
                    Wander(caravan.targetTile, 4);
                }
                else
                {
                    wanderTimer = UnityEngine.Random.Range(10f, 20f);
                }
            }
        }
Esempio n. 2
0
        public void ConfirmTrade()
        {
            if (leaving)
            {
                leaving    = false;
                leaveTimer = 0;
                foreach (Trader trader in traders)
                {
                    trader.MoveToTile(trader.overTile, false);
                }
            }

            foreach (ResourceManager.TradeResourceAmount tradeResourceAmount in resourcesToTrade)
            {
                ResourceManager.ConfirmedTradeResourceAmount existingConfirmedTradeResourceAmount = confirmedResourcesToTrade.Find(crtt => crtt.resource == tradeResourceAmount.resource);
                if (existingConfirmedTradeResourceAmount != null)
                {
                    existingConfirmedTradeResourceAmount.tradeAmount     += tradeResourceAmount.GetTradeAmount();
                    existingConfirmedTradeResourceAmount.amountRemaining += tradeResourceAmount.GetTradeAmount();
                }
                else
                {
                    confirmedResourcesToTrade.Add(new ResourceManager.ConfirmedTradeResourceAmount(tradeResourceAmount.resource, tradeResourceAmount.GetTradeAmount()));
                }
            }
            List <ResourceManager.ResourceAmount> resourcesToReserve = new List <ResourceManager.ResourceAmount>();

            foreach (ResourceManager.TradeResourceAmount tradeResourceAmount in resourcesToTrade)
            {
                if (tradeResourceAmount.GetTradeAmount() < 0)
                {
                    resourcesToReserve.Add(new ResourceManager.ResourceAmount(tradeResourceAmount.resource, Mathf.Abs(tradeResourceAmount.GetTradeAmount())));
                }
            }

            foreach (ResourceManager.TradeResourceAmount tradeResourceAmount in resourcesToTrade)
            {
                if (tradeResourceAmount.GetTradeAmount() > 0)
                {
                    inventory.ChangeResourceAmount(tradeResourceAmount.resource, -tradeResourceAmount.GetTradeAmount(), false);
                }
            }

            resourcesToTrade.Clear();

            List <ResourceManager.TradingPost> tradingPostsWithReservedResources = new List <ResourceManager.TradingPost>();

            Trader primaryTrader = traders[0];

            if (primaryTrader != null)
            {
                foreach (ResourceManager.TradingPost tradingPost in GameManager.resourceM.GetTradingPostsInRegion(primaryTrader.overTile.region).OrderBy(tp => PathManager.RegionBlockDistance(primaryTrader.overTile.regionBlock, tp.zeroPointTile.regionBlock, true, true, false)))
                {
                    List <ResourceManager.ResourceAmount> resourcesToReserveAtThisTradingPost = new List <ResourceManager.ResourceAmount>();
                    List <ResourceManager.ResourceAmount> resourcesToReserveToRemove          = new List <ResourceManager.ResourceAmount>();
                    foreach (ResourceManager.ResourceAmount resourceToReserve in resourcesToReserve)
                    {
                        ResourceManager.ResourceAmount resourceAmount = tradingPost.GetInventory().resources.Find(r => r.resource == resourceToReserve.resource);
                        if (resourceAmount != null)
                        {
                            int amountToReserve = resourceToReserve.amount < resourceAmount.amount ? resourceToReserve.amount : resourceAmount.amount;
                            resourcesToReserveAtThisTradingPost.Add(new ResourceManager.ResourceAmount(resourceToReserve.resource, amountToReserve));
                            resourceToReserve.amount -= amountToReserve;
                            if (resourceToReserve.amount == 0)
                            {
                                resourcesToReserveToRemove.Add(resourceToReserve);
                            }
                        }
                    }
                    if (resourcesToReserveAtThisTradingPost.Count > 0)
                    {
                        tradingPost.GetInventory().ReserveResources(resourcesToReserveAtThisTradingPost, primaryTrader);
                        tradingPostsWithReservedResources.Add(tradingPost);
                    }
                    foreach (ResourceManager.ResourceAmount resourceToReserveToRemove in resourcesToReserveToRemove)
                    {
                        resourcesToReserve.Remove(resourceToReserveToRemove);
                    }
                    resourcesToReserveToRemove.Clear();
                }

                if (tradingPostsWithReservedResources.Count > 0)
                {
                    primaryTrader.tradingPosts = tradingPostsWithReservedResources;
                }
            }
        }