Пример #1
0
        protected override bool TryExecuteWorker(IncidentParms parms)
        {
            if (!TryGetRandomAvailableTargetMap(out map))
            {
                return(false);
            }

            SettlementBase settlementBase = RandomNearbyTradeableSettlement(map.Tile);

            if (settlementBase == null)
            {
                return(false);
            }

            int destination = Rand.Chance(directConnectionChance) ? map.Tile : AllyOfNearbySettlement(settlementBase)?.Tile ?? map.Tile;

            int maxPriority = settlementBase.Faction.def.techLevel >= TechLevel.Medieval ? 30 : 20;

            RoadDef roadToBuild = DefDatabase <RoadDef> .AllDefsListForReading.Where(x => x.priority <= maxPriority).RandomElement();

            WorldPath path = WorldPath.NotFound;
            //StringBuilder stringBuilder = new StringBuilder();

            int    cost2       = 12000;
            int    timeToBuild = 0;
            string letterTitle = "MFI_RoadWorks".Translate();
            List <WorldObject_RoadConstruction> list = new List <WorldObject_RoadConstruction>();

            using (path = Find.WorldPathFinder.FindPath(destination, settlementBase.Tile, null))
            {
                if (path != null && path != WorldPath.NotFound)
                {
                    float roadCount = path.NodesReversed.Count(x => !Find.WorldGrid[x].Roads.NullOrEmpty() &&
                                                               Find.WorldGrid[x].Roads.Any(roadLink => roadLink.road.priority >= roadToBuild.priority) ||
                                                               Find.WorldObjects.AnyWorldObjectOfDefAt(MFI_DefOf.MFI_RoadUnderConstruction, x));

                    if (roadCount / path.NodesReversed.Count >= maxRoadCoverage)
                    {
                        Log.Message($"MFI :: too many roads leading from {(Find.WorldObjects.AnyWorldObjectAt(destination) ? Find.WorldObjects.ObjectsAt(destination).FirstOrDefault()?.Label : destination.ToString())} to {settlementBase} for road project");
                        return(false);
                    }
                    //stringBuilder.Append($"Path found from {settlementBase.Label} to {map.info.parent.Label}. Length = {path.NodesReversed.Count} ");
                    //not 0 and - 1
                    for (int i = 1; i < path.NodesReversed.Count - 1; i++)
                    {
                        cost2 += Caravan_PathFollower.CostToMove(CaravanTicksPerMoveUtility.DefaultTicksPerMove, path.NodesReversed[i], path.NodesReversed[i + 1]);

                        timeToBuild += (int)(2 * GenDate.TicksPerHour
                                             * WorldPathGrid.CalculatedMovementDifficultyAt(path.NodesReversed[i], true)
                                             * Find.WorldGrid.GetRoadMovementDifficultyMultiplier(i, i + 1));

                        if (!Find.WorldGrid[path.NodesReversed[i]].Roads.NullOrEmpty() &&
                            Find.WorldGrid[path.NodesReversed[i]].Roads.Any(roadLink => roadLink.road.priority >= roadToBuild.priority))
                        {
                            timeToBuild = timeToBuild / 2;
                        }

                        WorldObject_RoadConstruction roadConstruction = (WorldObject_RoadConstruction)WorldObjectMaker.MakeWorldObject(MFI_DefOf.MFI_RoadUnderConstruction);
                        roadConstruction.Tile     = path.NodesReversed[i];
                        roadConstruction.nextTile = path.NodesReversed[i + 1];
                        roadConstruction.road     = roadToBuild;
                        roadConstruction.SetFaction(settlementBase.Faction);
                        roadConstruction.projectedTimeOfCompletion = Find.TickManager.TicksGame + timeToBuild;
                        list.Add(roadConstruction);
                    }
                    cost2 = cost2 / 10;
                    DiaNode   node   = new DiaNode("MFI_RoadWorksDialogue".Translate(settlementBase, path.NodesReversed.Count, cost2)); // {settlementBase} wants {cost2 / 10} to build a road of {path.NodesReversed.Count}");
                    DiaOption accept = new DiaOption("OK".Translate())
                    {
                        resolveTree = true,
                        action      = () =>
                        {
                            TradeUtility.LaunchSilver(TradeUtility.PlayerHomeMapWithMostLaunchableSilver(), cost2);
                            foreach (WorldObject_RoadConstruction worldObjectRoadConstruction in list)
                            {
                                Find.WorldObjects.Add(worldObjectRoadConstruction);
                            }
                            list.Clear();
                        }
                    };

                    if (!TradeUtility.ColonyHasEnoughSilver(TradeUtility.PlayerHomeMapWithMostLaunchableSilver(), cost2))
                    {
                        accept.Disable("NeedSilverLaunchable".Translate(cost2));
                    }
                    DiaOption reject = new DiaOption("RejectLetter".Translate())
                    {
                        resolveTree = true,
                        action      = () =>
                        {
                            for (int i = list.Count - 1; i >= 0; i--)
                            {
                                list[i] = null;
                            }
                            list.Clear();
                        }
                    };

                    node.options.Add(accept);
                    node.options.Add(reject);

                    //Log.Message(stringBuilder.ToString());
                    Find.WindowStack.Add(new Dialog_NodeTreeWithFactionInfo(node, settlementBase.Faction));
                    Find.Archive.Add(new ArchivedDialog(node.text, letterTitle, settlementBase.Faction));
                }
            }
            return(true);
        }
        protected override bool TryExecuteWorker(IncidentParms parms)
        {
            if (!TryFindAdjcentSettlemet(out Settlement bomber))
            {
                return(false);
            }
            float        silver = silverCurve.Evaluate(1 - (1 / Find.AnyPlayerHomeMap.wealthWatcher.WealthTotal));
            List <Thing> demand = new List <Thing>();

            GenerateDemands(demand, silver);

            silver = GenThing.GetMarketValue(demand);

            int    countdown = countDown.RandomInRange * Global.DayInTicks;
            string text      = TranslatorFormattedStringExtensions.Translate("BombardmentThreat", bomber.Faction.leader, bomber.Faction.def.leaderTitle, bomber.Name, silver.ToStringMoney(null), GenLabel.ThingsLabel(demand, string.Empty), countdown.ToStringTicksToPeriod());

            GenThing.TryAppendSingleRewardInfo(ref text, demand);

            DiaNode nodeRoot = new DiaNode(text);

            nodeRoot.options.Add(new DiaOption("BombardmentThreat_AcceptThings".Translate())
            {
                action = () =>
                {
                    foreach (Thing t in demand)
                    {
                        TradeUtility.LaunchThingsOfType(t.def, t.stackCount, Find.AnyPlayerHomeMap, null);
                    }
                },
                link = new DiaNode(TranslatorFormattedStringExtensions.Translate("BombardmentThreatAcceptThings", bomber.Faction.leader))
                {
                    options =
                    {
                        new DiaOption("OK".Translate())
                        {
                            resolveTree = true
                        }
                    }
                }
            });
            if (TradeUtility.ColonyHasEnoughSilver(TradeUtility.PlayerHomeMapWithMostLaunchableSilver(), (int)silver * 2))
            {
                nodeRoot.options.Add(new DiaOption("BombardmentThreat_AcceptSilver".Translate((silver * 2).ToStringMoney()))
                {
                    action = () =>
                    {
                        TradeUtility.LaunchSilver(Find.AnyPlayerHomeMap, (int)silver * 2);
                    },
                    link = new DiaNode(TranslatorFormattedStringExtensions.Translate("BombardmentThreatAcceptThings", bomber.Faction.leader))
                    {
                        options =
                        {
                            new DiaOption("OK".Translate())
                            {
                                resolveTree = true
                            }
                        }
                    }
                });
            }
            else
            {
                nodeRoot.options.Add(new DiaOption("BombardmentThreat_AcceptSilver".Translate(((float)silver * 2).ToStringMoney()))
                {
                    disabled       = true,
                    disabledReason = "BombardmentThreat_AcceptSilverDisabled".Translate()
                });
            }
            nodeRoot.options.Add(new DiaOption("BombardmentThreat_Refusal".Translate())
            {
                action = () =>
                {
                    Find.AnyPlayerHomeMap.GetComponent <FE_MapComponent_Bombardment>().StartComp(def.durationDays.RandomInRange * Global.DayInTicks, bomber, countdown);
                },
                link = new DiaNode("BombardmentThreatRefusal".Translate(bomber.Faction.leader))
                {
                    options =
                    {
                        new DiaOption("OK".Translate())
                        {
                            resolveTree = true
                        }
                    }
                }
            });
            string title = "LetterLabelBombardmentTitle".Translate();

            Find.WindowStack.Add(new Dialog_NodeTreeWithFactionInfo(nodeRoot, bomber.Faction, true, true, title));
            Find.Archive.Add(new ArchivedDialog(nodeRoot.text, title, bomber.Faction));
            return(true);
        }