Skip to content

Commit

Permalink
feat: Show the 'spoiling' information for captive spawners.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan committed Nov 9, 2024
1 parent 1b85957 commit ea1bb01
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Yafc.Model/Data/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,23 @@ public float Power(Quality quality)
public int size { get; internal set; }
internal override FactorioObjectSortOrder sortingOrder => FactorioObjectSortOrder.Entities;
public override string type => "Entity";
/// <summary>
/// Gets the result when this entity spoils (possibly <see langword="null"/>, if the entity burns out with no replacement),
/// or <see langword="null"/> if this entity doesn't spoil.
/// </summary>
public Entity? spoilResult => getSpoilResult?.Value;
/// <summary>
/// Gets the time it takes for a base-quality entity to spoil, in seconds, or 0 if this entity doesn't spoil.
/// </summary>
public float baseSpoilTime { get; internal set; }
/// <summary>
/// Gets the <see cref="Quality"/>-adjusted spoilage time for this entity, in seconds, or 0 if this entity doesn't spoil.
/// </summary>
public float GetSpoilTime(Quality quality) => quality.ApplyStandardBonus(baseSpoilTime);
/// <summary>
/// The lazy store for getting the spoilage result, if this entity spoils.
/// </summary>
internal Lazy<Entity?>? getSpoilResult;

public override void GetDependencies(IDependencyCollector collector, List<FactorioObject> temp) {
if (energy != null) {
Expand Down
14 changes: 14 additions & 0 deletions Yafc.Parser/Data/FactorioDataDeserializer_Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,20 @@ void parseEffect(LuaTable effect) {
if (entity.energy == voidEntityEnergy || entity.energy == laborEntityEnergy) {
fuelUsers.Add(entity, SpecialNames.Void);
}

if (table.Get("production_health_effect", out LuaTable? healthEffect) && healthEffect.Get("not_producing", out float? lossPerTick)) {
entity.baseSpoilTime = (float)(table.Get<float>("max_health") * -60 * lossPerTick.Value);
table.Get<LuaTable>("dying_trigger_effect")?.ReadObjectOrArray(readDeathEffect);

void readDeathEffect(LuaTable effect) {
if (effect.Get("type", "") == "create-entity" && effect.Get("entity_name", out string? spoilEntity)) {
entity.getSpoilResult = new(() => {
Database.objectsByTypeName.TryGetValue("Entity." + spoilEntity, out FactorioObject? spoil);
return spoil as Entity;
});
}
}
}
}

private float EstimateArgument(LuaTable args, string name, float def = 0) => args.Get(name, out LuaTable? res) ? EstimateNoiseExpression(res) : def;
Expand Down
2 changes: 1 addition & 1 deletion Yafc/Widgets/ImmediateWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void BuildFactorioObjectIcon(this ImGui gui, IFactorioObjectWrappe

gui.DrawIcon(qualityRect, quality.icon, SchemeColor.Source);
}
if (gui.isBuilding && obj.target is Item { baseSpoilTime: > 0 }) {
if (gui.isBuilding && obj.target is Item { baseSpoilTime: > 0 } or Entity { baseSpoilTime: > 0 }) {
Vector2 size = new Vector2(displayStyle.Size / 2.5f);
Rect spoilableRect = new Rect(gui.lastRect.TopLeft, size);

Expand Down
17 changes: 16 additions & 1 deletion Yafc/Widgets/ObjectTooltip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void BuildCommon(FactorioObject target, ImGui gui) {
{EntityEnergyType.SolidFuel, "Solid fuel energy usage: "},
};

private static void BuildEntity(Entity entity, Quality quality, ImGui gui) {
private void BuildEntity(Entity entity, Quality quality, ImGui gui) {
if (entity.loot.Length > 0) {
BuildSubHeader(gui, "Loot");
using (gui.EnterGroup(contentPadding)) {
Expand Down Expand Up @@ -240,6 +240,21 @@ private static void BuildEntity(Entity entity, Quality quality, ImGui gui) {
}
}

float spoilTime = entity.GetSpoilTime(quality); // The spoiling rate setting does not apply to entities.
if (spoilTime != 0f) {
BuildSubHeader(gui, "Perishable");
using (gui.EnterGroup(contentPadding)) {
if (entity.spoilResult != null) {
gui.BuildText($"After {DataUtils.FormatTime(spoilTime)} of no production, spoils into");
gui.BuildFactorioObjectButtonWithText(new ObjectWithQuality<Entity>(entity.spoilResult, quality), iconDisplayStyle: IconDisplayStyle.Default with { AlwaysAccessible = true });
}
else {
gui.BuildText($"Expires after {DataUtils.FormatTime(spoilTime)} of no production");
}
tooltipOptions.ExtraSpoilInformation?.Invoke(gui);
}
}

if (entity.energy != null) {
string energyUsage = EnergyDescriptions[entity.energy.type] + DataUtils.FormatAmount(entity.Power(quality), UnitOfMeasure.Megawatt);
if (entity.energy.drain > 0f) {
Expand Down
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Date:
- Add the remaining research triggers and the mining-with-fluid research effect to the dependency/milestone
analysis.
- Explain what to do if Yafc fails to load a mod.
- (SA) Add spoiling time and result to item tooltips. Add a clock overlay to icons for perishable items.
- (SA) Add spoiling time and result to tooltips. Add a clock overlay to icons for perishable items/entities.
Internal changes:
- Using the LuaContext after it is freed now produces a better error.
----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit ea1bb01

Please sign in to comment.