From 019157ad34fe8e3ce083b4a35abbdcf1882393d3 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 9 Jul 2020 00:25:44 +0200 Subject: [PATCH] improved auto-buying logic --- .../Content/Localization/Localization.json | 1 - TouchyTickets/Options.cs | 2 - TouchyTickets/ParkMap.cs | 63 +++++++++++-------- TouchyTickets/Ui.cs | 9 --- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/TouchyTickets/Content/Localization/Localization.json b/TouchyTickets/Content/Localization/Localization.json index 94d6bd4..161aaea 100644 --- a/TouchyTickets/Content/Localization/Localization.json +++ b/TouchyTickets/Content/Localization/Localization.json @@ -28,7 +28,6 @@ "OtherOptions": "Other", "AutoBuyEnabled": "Auto-Buying Enabled", "MinTicketsForAutoBuy": "Min Tickets for Auto-Buying", - "AutoBuyInterval": "Auto-Buying Interval/s", "----- Tutorial -----": "", "Tutorial1": "Hi! Welcome to Touchy Tickets. To start the game, simply tap the ticket booth to sell a . Start by racking up 50!", "Tutorial2": "Great! Now, you can buy your first attraction. Access the menu on the right by swiping and purchase a carousel.", diff --git a/TouchyTickets/Options.cs b/TouchyTickets/Options.cs index 9ecce2c..8100065 100644 --- a/TouchyTickets/Options.cs +++ b/TouchyTickets/Options.cs @@ -35,8 +35,6 @@ namespace TouchyTickets { public bool AutoBuyEnabled = true; [DataMember] public int MinTicketsForAutoBuy = 50000; - [DataMember] - public float AutoBuyIntervalSecs = 60; public static void Save() { var file = GetOptionsFile(true); diff --git a/TouchyTickets/ParkMap.cs b/TouchyTickets/ParkMap.cs index 040fa59..8cabca9 100644 --- a/TouchyTickets/ParkMap.cs +++ b/TouchyTickets/ParkMap.cs @@ -16,6 +16,8 @@ namespace TouchyTickets { public class ParkMap { private const int AdditionalRadius = 15; + private const int AutoBuyIntervalSecs = 30; + [DataMember] public readonly int Width; [DataMember] @@ -33,7 +35,7 @@ namespace TouchyTickets { public Point PlacingPosition; public Point? SelectedPosition; private bool draggingAttraction; - private TimeSpan autoBuyCounter; + private double autoBuyCounter; public ParkMap(int width, int height) { this.Width = width; @@ -73,11 +75,9 @@ namespace TouchyTickets { var toSimulate = wasAway ? new TimeSpan(passed.Ticks / 2) : passed; // handle auto-buying - this.autoBuyCounter += toSimulate; - if (this.autoBuyCounter.TotalSeconds >= Options.Instance.AutoBuyIntervalSecs) { - this.autoBuyCounter = TimeSpan.Zero; - this.TryAutoBuy(); - } + this.autoBuyCounter += toSimulate.TotalSeconds; + this.TryAutoBuy(); + var autoBuysPerAttraction = ((float) this.autoBuyCounter / AutoBuyIntervalSecs / this.attractions.Count).Ceil(); // update tickets this.TicketsPerSecond = 0; @@ -85,9 +85,15 @@ namespace TouchyTickets { var genPerSecond = attraction.Update(toSimulate, this, pos); this.TicketsPerSecond += genPerSecond; - // after each attraction has sold their tickets, try auto-buying again if we were away - if (wasAway && passed.TotalSeconds >= Options.Instance.AutoBuyIntervalSecs) - this.TryAutoBuy(); + // if we were away, we have to catch up with auto-buys while also taking into account + // the amount of tickets that each ride generates. The easiest way we can do this is + // to progress, between updating each ride, by a percentage of the total update amount + if (wasAway) { + for (var i = autoBuysPerAttraction; i > 0; i--) { + if (!this.TryAutoBuy()) + break; + } + } } // map movement @@ -241,28 +247,31 @@ namespace TouchyTickets { return newMap; } - private void TryAutoBuy() { + private bool TryAutoBuy() { if (!Options.Instance.AutoBuyEnabled) - return; - while (GameImpl.Instance.Tickets >= Options.Instance.MinTicketsForAutoBuy) { - var success = false; + return false; + if (GameImpl.Instance.Tickets < Options.Instance.MinTicketsForAutoBuy) + return false; + if (this.autoBuyCounter < AutoBuyIntervalSecs) + return false; + this.autoBuyCounter -= AutoBuyIntervalSecs; - // auto-buy modifiers - if (Upgrade.AutoPlaceModifiers[0].IsActive()) { - foreach (var modifier in AttractionModifier.Modifiers.Values) { - var match = this.attractions.Select(pa => pa.Item2).Where(modifier.IsAffected); - // if we don't have level 2, we only want to increase existing modifiers - if (!Upgrade.AutoPlaceModifiers[1].IsActive()) - match = match.Where(a => a.GetModifierAmount(modifier) > 0); - var attraction = match.OrderBy(a => a.GetModifierAmount(modifier)).FirstOrDefault(); - if (attraction != null && modifier.Buy(attraction)) - success = true; - } + var success = false; + // auto-buy modifiers + if (Upgrade.AutoPlaceModifiers[0].IsActive()) { + // loop through all attractions, but look at attractions with fewer applied modifiers first + foreach (var attraction in this.attractions.Select(kv => kv.Item2).OrderBy(a => a.GetModifierAmount(null))) { + var match = AttractionModifier.Modifiers.Values.Where(m => m.IsAffected(attraction)); + // if we don't have level 2, we only want to increase existing modifiers + if (!Upgrade.AutoPlaceModifiers[1].IsActive()) + match = match.Where(m => attraction.GetModifierAmount(m) > 0); + // we want to apply the least applied modifier on this attraction + var modifier = match.OrderBy(m => attraction.GetModifierAmount(m)).FirstOrDefault(); + if (modifier != null && modifier.Buy(attraction)) + success = true; } - - if (!success) - break; } + return success; } } diff --git a/TouchyTickets/Ui.cs b/TouchyTickets/Ui.cs index d33bd2c..b423f88 100644 --- a/TouchyTickets/Ui.cs +++ b/TouchyTickets/Ui.cs @@ -391,15 +391,6 @@ namespace TouchyTickets { } })); num.PositionOffset = new Vector2(0, 1); - optionList.AddChild(new Paragraph(Anchor.AutoLeft, 1, p => Localization.Get("AutoBuyInterval") + ": " + Options.Instance.AutoBuyIntervalSecs)); - optionList.AddChild(new Slider(Anchor.AutoLeft, new Vector2(1, 20), 10, 299) { - PositionOffset = new Vector2(0, 1), - CurrentValue = Options.Instance.AutoBuyIntervalSecs - 1, - OnValueChanged = (s, v) => { - Options.Instance.AutoBuyIntervalSecs = (int) v + 1; - Options.Save(); - } - }); optionList.AddChild(new Paragraph(Anchor.AutoCenter, 1, Localization.Get("OtherOptions"), true) { PositionOffset = new Vector2(0, 10),