plaidman / qud-mods

0 stars 1 forks source link

Consider using Text Replacers for Salt Shuffle Revival #35

Closed librarianmage closed 1 week ago

librarianmage commented 2 weeks ago

For instance, NameWhose could be removed and ResolveCardAgainstCard could be rewritten like

    public static string ResolveText = "=subject[0].T's= =object[0].name= {{r|=successVerb=}} =subject[1].t's= =object[1].name=. (=renown= renown)\n";

    private static string niceNumber(int i) => i.ToString("+0;-#");

    public static string ResolveCardAgainstCard(SSR_Card yourCard, int foeCardIndex, int foe, int you) {
      var enemyField = CardZones[foe, FieldZone];
      var enemyDeck = CardZones[foe, DeckZone];
      var foeCard = enemyField[foeCardIndex];
      int margin = CardStatsAgainstCard(yourCard, foeCard);

      var result = ResolveText.StartReplace()
        .AddSubject(you == PlayerCards ? The.Player : Opponent)
        .AddObject(yourCard.ParentObject)
        .AddSubject(foe != PlayerCards ? The.Player : Opponent)
        .AddObject(foeCard.ParentObject);

      if (margin == 3) {
        // returned to hand
        enemyField.RemoveAt(foeCardIndex);
        enemyDeck.Add(foeCard);

        int penalty = CardCrushPenalty(yourCard, foeCard);
        ScorePoints(you, penalty * -1);

        return result.AddReplacer("successVerb", "crushes")
          .AddReplacer("renown", niceNumber(-penalty))
          .ToText();
      }

      if (margin == 2) {
        if (foeCard.PointValue > yourCard.PointValue) {
          // banished from play
          enemyField.RemoveAt(foeCardIndex);

          ScorePoints(you, foeCard.PointValue);

          return result.AddReplacer("successVerb", "topples")
            .AddReplacer("renown", niceNumber(foeCard.PointValue))
            .ToText();
        } else {
          // returned to hand
          enemyField.RemoveAt(foeCardIndex);
          enemyDeck.Add(foeCard);

          ScorePoints(you, 1);

          return result.AddReplacer("successVerb", "vanquishes")
            .AddReplacer("renown", niceNumber(1))
            .ToText();
        }
      }

      return "";
    }

Note that this is an incomplete solution. Currently, display names will not be exactly the same and coloring in names will be removed if I read the code correctly.

plaidman commented 2 weeks ago

I like this suggestion but it doesn't play very well with StringBuilder, which I ended up going with - I think that will be more efficient. I did take some parts of your suggestion, like DRYing up the string generation, and the formatting of the penalty/score number. Thanks!

plaidman commented 1 week ago

Just a heads up but ReplaceBuilder can work with a StringBuilder by calling .StartReplace() on a StringBuilder and then calling .Execute() instead of .ToText(), which will modify the given StringBuilder in place

plaidman commented 1 week ago

I think it should be as simple as including =object[0].cardName= and then .AddReplacer("cardName", Context => Context.Target.GetPart().ShortDisplayName) if you want exactly the same result as current