Skip to content

Commit

Permalink
Fix serialization of resources (can't serialize a Func) (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekolis committed Apr 5, 2022
1 parent c1e9013 commit 4fec75c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions FrEee/Game/Objects/Civilization/Colony.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public ResourceQuantity StandardIncomePercentages
var aptfactor = 1d;
if (r.Aptitude != null)
aptfactor = Population.Sum(kvp => (kvp.Key.Aptitudes[r.Aptitude.Name] / 100d) * (double)kvp.Value / (double)totalpop);
var cultfactor = (100 + r.CultureModifier(Owner.Culture)) / 100d;
var cultfactor = (100 + r.GetCultureModifier(Owner.Culture)) / 100d;

result += (int)(100 * popfactor * aptfactor * cultfactor * moodfactor) * r;
}
Expand Down Expand Up @@ -257,4 +257,4 @@ public void TriggerHappinessChange(Func<HappinessModel, int> trigger)
}
}
}
}
}
6 changes: 3 additions & 3 deletions FrEee/Game/Objects/Civilization/Culture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Game.Interfaces;
using FrEee.Game.Interfaces;
using FrEee.Modding.Interfaces;
using FrEee.Utility;
using System;
Expand Down Expand Up @@ -26,7 +26,7 @@ public ResourceQuantity IncomePercentages
{
var result = new ResourceQuantity();
foreach (var r in Resource.All)
result += (100 + r.CultureModifier(this)) * r;
result += (100 + r.GetCultureModifier(this)) * r;
return result;
}
}
Expand Down Expand Up @@ -70,4 +70,4 @@ public override string ToString()
return Name;
}
}
}
}
4 changes: 2 additions & 2 deletions FrEee/Modding/ResourceFormula.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Utility;
using FrEee.Utility;
using System.Linq;

namespace FrEee.Modding
Expand Down Expand Up @@ -85,4 +85,4 @@ public ResourceQuantity Evaluate(object host)
return q;
}
}
}
}
30 changes: 20 additions & 10 deletions FrEee/Utility/Resource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Game.Interfaces;
using FrEee.Game.Interfaces;
using FrEee.Game.Objects.Civilization;
using FrEee.Utility.Extensions;
using System;
Expand Down Expand Up @@ -46,9 +46,19 @@ static Resource()
public Color Color { get; set; }

/// <summary>
/// Function to compute the cultural modifier. A modifier of zero means 100%.
/// The name of the <see cref="Culture"/> property that will be used to modify this resource's income,
/// or null to not have a modifier.
/// </summary>
public Func<Culture, int> CultureModifier { get; set; }
/// <remarks>
/// This property should be of type <see cref="int"/>.
/// </remarks>
public string? CultureModifierPropertyName { get; set; }

/// <summary>
/// Function to compute the cultural modifier. A modifier of zero means 100%; 100 means doubled, -50 means halved, etc.
/// </summary>
public int GetCultureModifier(Culture c)
=> CultureModifierPropertyName is null ? 0 : (int)c.GetPropertyValue(CultureModifierPropertyName);

/// <summary>
/// Does this resource have a "value" assigned to planets and asteroids?
Expand Down Expand Up @@ -122,7 +132,7 @@ public IEnumerable<string> PortraitPaths
HasValue = false,
PictureName = "Resource5",
Aptitude = Aptitude.Cunning,
CultureModifier = c => c.Intelligence,
CultureModifierPropertyName = nameof(Culture.Intelligence),
};

public static readonly Resource Minerals = new Resource
Expand All @@ -134,7 +144,7 @@ public IEnumerable<string> PortraitPaths
HasValue = true,
PictureName = "Resource1",
Aptitude = Aptitude.Mining,
CultureModifier = c => c.Production,
CultureModifierPropertyName = nameof(Culture.Production),
};

public static readonly Resource Organics = new Resource
Expand All @@ -146,7 +156,7 @@ public IEnumerable<string> PortraitPaths
HasValue = true,
PictureName = "Resource2",
Aptitude = Aptitude.Farming,
CultureModifier = c => c.Production,
CultureModifierPropertyName = nameof(Culture.Production),
};

public static readonly Resource Radioactives = new Resource
Expand All @@ -158,7 +168,7 @@ public IEnumerable<string> PortraitPaths
HasValue = true,
PictureName = "Resource3",
Aptitude = Aptitude.Refining,
CultureModifier = c => c.Production,
CultureModifierPropertyName = nameof(Culture.Production),
};

public static readonly Resource Research = new Resource
Expand All @@ -170,7 +180,7 @@ public IEnumerable<string> PortraitPaths
HasValue = false,
PictureName = "Resource4",
Aptitude = Aptitude.Intelligence, // no, seriously
CultureModifier = c => c.Research,
CultureModifierPropertyName = nameof(Culture.Research),
};

public static readonly Resource Supply = new Resource
Expand All @@ -182,7 +192,7 @@ public IEnumerable<string> PortraitPaths
HasValue = false,
PictureName = "Resource6",
Aptitude = null, // TODO - supply aptitude?
CultureModifier = c => 0, // TODO - supply culture modifier?
CultureModifierPropertyName = null,
};

private static IEnumerable<Resource> all;
Expand Down Expand Up @@ -236,4 +246,4 @@ public override string ToString()
return Name;
}
}
}
}

0 comments on commit 4fec75c

Please sign in to comment.