Skip to content

Commit

Permalink
Fixed incorrect logic when computing descriptions of stacked abilities (
Browse files Browse the repository at this point in the history
  • Loading branch information
ekolis committed Jan 21, 2023
1 parent 8ffe29e commit b9abf92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 15 additions & 4 deletions FrEee/Game/Objects/Abilities/Ability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ public Ability(IAbilityObject container)
Values = new List<Formula<string>>();
}

public Ability(IAbilityObject container, AbilityRule rule, string? description = null, params object[] values)
public Ability(IAbilityObject container, AbilityRule rule, Formula<string>? description = null, params object[] values)
{
Container = container;
Rule = rule;
Description = description;
Values = new List<Formula<string>>();
foreach (var val in values)
{
if (val is IFormula)
Values.Add((val as IFormula).ToStringFormula());
if (val is IFormula f)
{
Values.Add(f.ToStringFormula());
}
else if (val is IEnumerable<IFormula> fs)
{
foreach (var ff in fs)
{
Values.Add(ff.ToStringFormula());
}
}
else
{
Values.Add(new LiteralFormula<string>(val.ToString()));
}
}
}

Expand Down Expand Up @@ -73,7 +84,7 @@ public Ability(IAbilityObject container, AbilityRule rule, string? description =
/// A description of the ability's effects.
/// Can use, e.g. [%Amount1%] to specify the amount in the Value 1 field.
/// </summary>
public Formula<string> Description { get; set; }
public Formula<string>? Description { get; set; }

/// <summary>
/// Key for ability groups.
Expand Down
21 changes: 18 additions & 3 deletions FrEee/Utility/Extensions/GameEnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand Down Expand Up @@ -289,19 +289,34 @@ public static IEnumerable<Ability> StackAbilities(this IEnumerable<IAbilityObjec
/// <returns></returns>
public static ILookup<Ability, Ability> StackToTree(this IEnumerable<Ability> abilities, IAbilityObject stackTo)
{
// create result list
var stacked = new List<Tuple<Ability, Ability>>();

// group abilities by rule
var grouped = abilities.GroupBy(a => a.Rule);

foreach (var group in grouped)
{
if (group.Key == null)
continue; // invalid ability rule

// stack this ability group
var lookup = group.Key.GroupAndStack(group, stackTo);

foreach (var lgroup in lookup)
{
// create a merged ability with a generated description (since ability values are stacked when merged)
var mergedAbility = new Ability(stackTo, lgroup.Key.Rule, description: null, values: lgroup.Key.Values);

foreach (var abil in group)
stacked.Add(Tuple.Create(lgroup.Key, abil));
{
// add this ability to the current group in the result list, using the merged ability as the key
stacked.Add(Tuple.Create(mergedAbility, abil));
}
}
}

// create a lookup from the result list
return stacked.ToLookup(t => t.Item1, t => t.Item2);
}

Expand Down Expand Up @@ -361,4 +376,4 @@ public static Cargo Sum<T>(this IEnumerable<T> stuff, Func<T, Cargo> selector)
return list.BelongingTo(null);
}
}
}
}

0 comments on commit b9abf92

Please sign in to comment.