From ac93118995d3acc811246805696fd0a101cd5548 Mon Sep 17 00:00:00 2001 From: Lars Dehler Date: Sun, 28 Aug 2022 15:52:50 +0200 Subject: [PATCH] better check changed game conditions causing the trader request action to become invalid --- Languages/English/Keyed/CallATrader.xml | 10 +++- Languages/German/Keyed/CallATrader.xml | 10 +++- Source/Properties/AssemblyInfo.cs | 4 +- Source/rimworld/ChoiceLetter_SelectTrader.cs | 18 +++--- ...cidentWorker_OrbitalTraderVisitingOffer.cs | 60 ++++++++++++++++++- 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/Languages/English/Keyed/CallATrader.xml b/Languages/English/Keyed/CallATrader.xml index 51b6bdb..57b84d9 100644 --- a/Languages/English/Keyed/CallATrader.xml +++ b/Languages/English/Keyed/CallATrader.xml @@ -9,15 +9,21 @@ A trading request was send to the Orbital Traders Hub. They will not answer any requests from you for {0}. + + Refusal notice from Orbital Traders Hub + The Orbital Traders Hub has to decline your trading request. + This is because there are too many ({0}) tradeships at your location already. + This is because they currently have no traders available near your colony. + Offer from Orbital Traders Hub The Orbital Traders Hub offers to send an orbital trader ship to your location in {0}.\n\nThey request an upfront fee of {1} silver to cover their costs for adjusting the ships route.\n\nThe silver will be teleported to the hub immediately - if you accept.\n Request {0} Decline the offer {0}/{1} silver - The requested trader refuses to show up because there are too many tradeships at your location already. + Wait there are no traders to select ... The fee of {0} Silver was yeeted to the Orbital Traders Hub - + Cost range (silver) Range of silver the Orbital Traders Hub will request to send a ship diff --git a/Languages/German/Keyed/CallATrader.xml b/Languages/German/Keyed/CallATrader.xml index 7cced43..074f070 100644 --- a/Languages/German/Keyed/CallATrader.xml +++ b/Languages/German/Keyed/CallATrader.xml @@ -9,15 +9,21 @@ Es wurde eine Handelsanfrage an die Fliegenden Hausierer gestellt. Sie werden für {0} keine weiteren Anfragen akzeptieren. + + Ablehnung der Fliegenden Hausierer + Die Fliegenden Hausierer haben deine Anfrage einen Händler zu schicken abgelehnt. + Dies begründen sie damit, dass bereits zu viele ({0}) Handelsshiffe in deiner Kolonie zugegen sind. + Dies begründen sie damit, dass sie aktuell aus Gründen keine Handelsshiffe zu deiner Kolonie senden können. + Angebot der Fliegenden Hausierer Die Fliegenden Hausierer bieten dir an eines ihrer Schiffe zu deiner Kolonie zu senden. Dies wird {0} dauern.\n\nSie fordern allerdings eine Sofortzahlung von {1} Silber um die Kosten der Routenanpassung zu decken.\n\nBei Angebotsannahme wird diese Summe sofort zur Zentrale der Fliegenden Hausierer teleportiert.\n Frage {0} an Angebot ablehnen {0}/{1} Silber - Der angeforderte Händler verweigert die Anreise, da aktuell zu viele Handelsshiffe in der Region sind. + Warte, hier gibts es keine Händler zur Auswahl ... Die Zahlung von {0} Silber wurde zur Zentrale der Fliegenden Hausierer teleportiert. - + Kosten (Silber) Spannweite der Kosten, die die Fliegenden Hausierer für ihren Service einfordern werden diff --git a/Source/Properties/AssemblyInfo.cs b/Source/Properties/AssemblyInfo.cs index c424da7..f84849d 100644 --- a/Source/Properties/AssemblyInfo.cs +++ b/Source/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.1.0.0")] +[assembly: AssemblyVersion("1.1.1.0")] +[assembly: AssemblyFileVersion("1.1.1.0")] diff --git a/Source/rimworld/ChoiceLetter_SelectTrader.cs b/Source/rimworld/ChoiceLetter_SelectTrader.cs index 6f7db36..2c04ee1 100644 --- a/Source/rimworld/ChoiceLetter_SelectTrader.cs +++ b/Source/rimworld/ChoiceLetter_SelectTrader.cs @@ -1,4 +1,5 @@ using RimWorld; +using System; using System.Collections.Generic; using System.Linq; using Verse; @@ -58,6 +59,15 @@ public override IEnumerable Choices IList orbitalTraderDefs = DefDatabase.AllDefsListForReading.FindAll(def => def.orbital); + // check game conditions still allow triggering the offer event now to determine if trader selection can continue + // if not a refusal notice will be send here as well + if (IncidentWorker_OrbitalTraderVisitingOffer.TryExecuteWorkerInternal( + this.map, orbitalTraderDefs, IncidentWorker_OrbitalTraderVisitingOffer.IncidentTrigger.LETTER_EVENT)) + { + yield return CreateDiaOption((Constants.MOD_PREFIX + TRADER_LETTER + "error.ok").Translate()); + yield break; + } + if (!this.canSelectTraderType) { orbitalTraderDefs = new TraderKindDef[] { orbitalTraderDefs.RandomElement() }; @@ -70,14 +80,6 @@ public override IEnumerable Choices { Find.LetterStack.RemoveLetter(this); - // hardcoded limit of 5 in the basegame - if (this.map.passingShipManager.passingShips.Count >= 5) - { - // send info message so player knowns too many ships present already - Messages.Message((Constants.MOD_PREFIX + TRADER_LETTER + "toomanytraders").Translate(), null, MessageTypeDefOf.NeutralEvent, true); - return; - } - Find.Storyteller.incidentQueue.Add(new QueuedIncident( new FiringIncident(IncidentDefOf.OrbitalTraderArrival, null, new IncidentParms() diff --git a/Source/rimworld/IncidentWorker_OrbitalTraderVisitingOffer.cs b/Source/rimworld/IncidentWorker_OrbitalTraderVisitingOffer.cs index d13d882..e109935 100644 --- a/Source/rimworld/IncidentWorker_OrbitalTraderVisitingOffer.cs +++ b/Source/rimworld/IncidentWorker_OrbitalTraderVisitingOffer.cs @@ -11,6 +11,15 @@ namespace Arakos.CallATrader public class IncidentWorker_OrbitalTraderVisitingOffer : IncidentWorker { + private static readonly String INCIDENT = ".incident."; + + private static readonly int HARDCODED_SHIP_LIMIT = 5; + + public enum IncidentTrigger + { + RANDOM_EVENT, PLAYER_EVENT, LETTER_EVENT + } + protected override bool CanFireNowSub(IncidentParms parms) { return parms.forced || CallATrader.settings.randomEventAllowed; @@ -18,8 +27,55 @@ protected override bool CanFireNowSub(IncidentParms parms) protected override bool TryExecuteWorker(IncidentParms parms) { - Find.LetterStack.ReceiveLetter(new ChoiceLetter_SelectTrader((Map)parms.target)); - return true; + return TryExecuteWorkerInternal((Map)parms.target, + DefDatabase.AllDefsListForReading, + parms.forced ? IncidentTrigger.PLAYER_EVENT : IncidentTrigger.RANDOM_EVENT); + } + + public static bool TryExecuteWorkerInternal(Map map, IList orbitalTraderDefs, IncidentTrigger eventTrigger) + { + String displayableError = GetDisplayableError(map, orbitalTraderDefs); + Letter letter = null; + + // if error but from non-interactive event trigger - ignore silently to not confuse player with random refusal messages + if (displayableError != null && eventTrigger != IncidentTrigger.RANDOM_EVENT) + { + letter = LetterMaker.MakeLetter( + (Constants.MOD_PREFIX + INCIDENT + "refusal.label").Translate(), + (Constants.MOD_PREFIX + INCIDENT + "refusal.base").Translate() + "\n\n" + displayableError, + LetterDefOf.NeutralEvent); + } + + // if no error and not triggered by a letter event already send the choice letter + if (displayableError == null && eventTrigger != IncidentTrigger.LETTER_EVENT) + { + letter = new ChoiceLetter_SelectTrader(map); + } + + if (letter != null) + { + Find.LetterStack.ReceiveLetter(letter); + return true; + } + + return false; + } + + public static String GetDisplayableError(Map map, IList orbitalTraderDefs) + { + // colony situation causing no orbital traders to be available + if (!orbitalTraderDefs.Any(def => def.orbital)) + { + return (Constants.MOD_PREFIX + INCIDENT + "refusal.noorbitaltradersavailable").Translate(); + } + + // check the limit of ships in the basegame + if (map.passingShipManager.passingShips.Count >= HARDCODED_SHIP_LIMIT) + { + return (Constants.MOD_PREFIX + INCIDENT + "refusal.toomanytraders").Translate(HARDCODED_SHIP_LIMIT); + } + + return null; } } }