Example #1
0
        internal override void invest()
        {
            // Aristocrats invests only in resource factories (and banks)
            if (Province.getResource() != null)
            {
                // if AverageFactoryWorkforceFulfilling isn't full you can get more workforce by raising salary (implement it later)
                var projects = Province.getAllInvestmentProjects(this).Where(x => x.CanProduce(Province.getResource()));

                var project = projects.MaxByRandom(x => x.GetMargin().Multiply(getBusinessSecurity(x)).get());
                if (project != null && project.GetMargin().Multiply(getBusinessSecurity(project)).isBiggerThan(Options.minMarginToInvest))
                {
                    var factoryProject = project as NewFactoryProject; // build new one
                    if (factoryProject != null)
                    {
                        // todo remove connection to grain
                        Storage resourceToBuild = factoryProject.Type.GetBuildNeeds().GetFirstSubstituteStorage(Product.Grain);

                        // try to build for grain
                        if (storage.has(resourceToBuild))
                        {
                            var factory = Province.BuildFactory(this, factoryProject.Type, Game.market.getCost(resourceToBuild));
                            storage.send(factory.getInputProductsReserve(), resourceToBuild);
                            factory.constructionNeeds.setZero();
                        }
                        else // build for money
                        {
                            ReadOnlyValue investmentCost = Game.market.getCost(resourceToBuild);
                            if (!CanPay(investmentCost))
                            {
                                Bank.GiveLackingMoneyInCredit(this, investmentCost);
                            }
                            if (CanPay(investmentCost))
                            {
                                var factory = Province.BuildFactory(this, factoryProject.Type, investmentCost);  // build new one
                                PayWithoutRecord(factory, investmentCost);
                            }
                        }
                    }
                    else
                    {
                        var factory = project as Factory;// existing one
                        if (factory != null)
                        {
                            Value investmentCost = factory.GetInvestmentCost();
                            if (!CanPay(investmentCost))
                            {
                                Bank.GiveLackingMoneyInCredit(this, investmentCost);
                            }
                            if (CanPay(investmentCost))
                            {
                                if (factory.IsOpen)
                                {
                                    factory.upgrade(this);
                                }
                                else
                                {
                                    factory.open(this, true);
                                }
                            }
                        }
                        else
                        {
                            Owners buyShare = project as Owners;
                            if (buyShare != null) // buy part of existing factory
                            {
                                Value investmentCost = buyShare.GetInvestmentCost();
                                if (!CanPay(investmentCost))
                                {
                                    Bank.GiveLackingMoneyInCredit(this, investmentCost);
                                }
                                if (CanPay(investmentCost))
                                {
                                    buyShare.BuyStandardShare(this);
                                }
                            }
                            else
                            {
                                Debug.Log("Unknown investment type");
                            }
                        }
                    }
                }
            }
            base.invest();
        }