Skip to content

Commit

Permalink
feat: Add the send-to-orbit trigger I left out of #350. (#355)
Browse files Browse the repository at this point in the history
I said I added all the triggers in #350, but I forgot about
send-item-to-orbit.
  • Loading branch information
shpaass authored Nov 15, 2024
2 parents d6bbdb5 + 410de57 commit fe0f7a1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Yafc.Model/Data/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ public enum RecipeFlags {
HasResearchTriggerBuildEntity = 1 << 7,
/// <summary>Set when the technology has a research trigger to launch a space platform starter pack</summary>
HasResearchTriggerCreateSpacePlatform = 1 << 8,
/// <summary>Set when the technology has a research trigger to launch an arbitrary item.</summary>
HasResearchTriggerSendToOrbit = 1 << 9,

HasResearchTriggerMask = HasResearchTriggerCraft | HasResearchTriggerCaptureEntity | HasResearchTriggerMineEntity | HasResearchTriggerBuildEntity
| HasResearchTriggerCreateSpacePlatform,
| HasResearchTriggerCreateSpacePlatform | HasResearchTriggerSendToOrbit,
}

public abstract class RecipeOrTechnology : FactorioObject {
Expand Down Expand Up @@ -773,6 +775,7 @@ public class Technology : RecipeOrTechnology { // Technology is very similar to
/// </summary>
/// <remarks>Lazy-loaded so the database can load and correctly type (eg EntityCrafter, EntitySpawner, etc.) the entities without having to do another pass.</remarks>
public IReadOnlyList<Entity> triggerEntities => getTriggerEntities.Value;
public Item? triggerItem { get; internal set; }

/// <summary>
/// Sets the value used to construct <see cref="triggerEntities"/>.
Expand All @@ -795,6 +798,9 @@ public override void GetDependencies(IDependencyCollector collector, List<Factor
var items = Database.items.all.Where(i => i.factorioType == "space-platform-starter-pack");
collector.Add([.. items.Select(i => Database.objectsByTypeName["Mechanics.launch." + i.name])], DependencyList.Flags.Source);
}
if (flags.HasFlag(RecipeFlags.HasResearchTriggerSendToOrbit)) {
collector.Add([Database.objectsByTypeName["Mechanics.launch." + triggerItem]], DependencyList.Flags.Source);
}

if (hidden && !enabled) {
collector.Add(Array.Empty<FactorioId>(), DependencyList.Flags.Hidden);
Expand Down
26 changes: 18 additions & 8 deletions Yafc.Parser/Data/FactorioDataDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,7 @@ void readTrigger(LuaTable table) {
launchProducts = [];
}

var recipe = CreateSpecialRecipe(item, SpecialNames.RocketLaunch, "launched");
recipe.ingredients =
[
new Ingredient(item, item.stackSize),
new Ingredient(rocketLaunch, 1)
];
recipe.products = launchProducts;
recipe.time = 0f; // TODO what to put here?
EnsureLaunchRecipe(item, launchProducts);
}

if (GetRef(table, "spoil_result", out Item? spoiled)) {
Expand Down Expand Up @@ -485,6 +478,23 @@ void readEffect(LuaTable effect) {
}
}

/// <summary>
/// Creates, or confirms the existence of, a recipe for launching a particular item.
/// </summary>
/// <param name="item">The <see cref="Item"/> to be launched.</param>
/// <param name="launchProducts">The result of launching <see cref="Item"/>, if known. Otherwise <see langword="null"/>, to preserve
/// the existing launch products of a preexisting recipe, or set no products for a new recipe.</param>
private void EnsureLaunchRecipe(Item item, Product[]? launchProducts) {
Recipe recipe = CreateSpecialRecipe(item, SpecialNames.RocketLaunch, "launched");
recipe.ingredients =
[
new Ingredient(item, item.stackSize),
new Ingredient(rocketLaunch, 1),
];
recipe.products = launchProducts ?? recipe.products ?? [];
recipe.time = 0f; // TODO what to put here?
}

private Fluid SplitFluid(Fluid basic, int temperature) {
logger.Information("Splitting fluid {FluidName} at {Temperature}", basic.name, temperature);
basic.variants ??= [basic];
Expand Down
11 changes: 11 additions & 0 deletions Yafc.Parser/Data/FactorioDataDeserializer_RecipeAndTechnology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ private void LoadResearchTrigger(LuaTable researchTriggerTable, ref Technology t
case "create-space-platform":
technology.flags = RecipeFlags.HasResearchTriggerCreateSpacePlatform;
break;
case "send-item-to-orbit":
if (!researchTriggerTable.Get("item", out string? itemName)) {
errorCollector.Error($"Research trigger {type} of {technology.typeDotName} does not have an item field", ErrorSeverity.MinorDataLoss);
break;
}
Item item = GetObject<Item>(itemName);
technology.triggerItem = item;
technology.flags |= RecipeFlags.HasResearchTriggerSendToOrbit;
// Create a launch recipe for items without a `rocket_launch_products`, without altering existing launch recipes.
EnsureLaunchRecipe(item, null);
break;
default:
errorCollector.Error($"Research trigger of {technology.typeDotName} has an unsupported type {type}", ErrorSeverity.MinorDataLoss);
break;
Expand Down
7 changes: 7 additions & 0 deletions Yafc/Widgets/ObjectTooltip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ private static void BuildTechnology(Technology technology, ImGui gui) {
bool isResearchTriggerMine = technology.flags.HasFlag(RecipeFlags.HasResearchTriggerMineEntity);
bool isResearchTriggerBuild = technology.flags.HasFlag(RecipeFlags.HasResearchTriggerBuildEntity);
bool isResearchTriggerPlatform = technology.flags.HasFlag(RecipeFlags.HasResearchTriggerCreateSpacePlatform);
bool isResearchTriggerLaunch = technology.flags.HasFlag(RecipeFlags.HasResearchTriggerSendToOrbit);

if (!technology.flags.HasFlagAny(RecipeFlags.HasResearchTriggerMask)) {
BuildRecipe(technology, gui);
Expand Down Expand Up @@ -580,6 +581,12 @@ private static void BuildTechnology(Technology technology, ImGui gui) {
BuildIconRow(gui, items, 2);
}
}
else if (isResearchTriggerLaunch) {
BuildSubHeader(gui, "Launch this item");
using (gui.EnterGroup(contentPadding)) {
gui.BuildFactorioObjectButtonWithText(technology.triggerItem);
}
}

if (technology.unlockRecipes.Count > 0) {
BuildSubHeader(gui, "Unlocks recipes");
Expand Down

0 comments on commit fe0f7a1

Please sign in to comment.