No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-21 13:05:52 +02:00
md flares settings 2026-10-21 13:05:52 +02:00
screenshots add screenshot 2026-10-21 03:04:19 +02:00
t flares settings 2026-10-21 13:05:52 +02:00
content.xml init 2026-10-21 02:18:58 +02:00
README.adoc flares settings 2026-10-21 13:05:52 +02:00

Repair at Nearest

Adds two orders to the context menu of your ships:

  • Repair: fly to the nearest reachable shipyard, wharf, equipment dock or own supply ship and repair the hull and damaged components.

  • Repair and Resupply: same, and also top up missiles, flares (countermeasures), deployables and drones to the ship’s Automatic Resupply setting.

The tooltip of Repair and Resupply lists what would be bought. Repair is greyed out when nothing is damaged; Repair and Resupply when nothing is damaged and there is nothing to buy.

The order runs before the rest of the ship’s queue, which then carries on. With ships selected, right-clicking one of your ships sends it and the whole selection; the entries then appear under the selection’s orders, like vanilla’s Remove all orders. Each ship goes to its own nearest location.

Requires X4: Foundations 9.00 and SirNukes Mod Support APIs. Install by copying or symlinking this folder to extensions/repair_nearest. Not save-dependent.

Options

In the game’s Extension Options (provided by SirNukes Mod Support APIs), under Repair at Nearest:

  • Flares-only resupply (default on): whether Repair and Resupply sends a ship that only needs flares. When off, a ship is never sent away just for flares, and flares alone don’t show in the tooltip. Flares are still topped up when the ship goes for repairs or other ammo. This matches vanilla the Resupply order, which never sends a ship just for flares.

How it works

Finding a destination

  1. Search the whole galaxy for candidates:

    • your supply ships and carriers;

    • known, non-hostile stations with a ship trader that can equip the ship’s size class: shipyards, wharves and equipment docks.

  2. Drop candidates the ship can’t use: no docking permission, gridlocked, or no ship trader when repairs are needed.

  3. Pick the one with the fewest gate jumps, skipping sectors the ship’s travel blacklist forbids, then the closest one if it’s in the same sector. When restocking, what a place can supply right now comes first: places that can build everything on the shopping list and have the materials for it, then places that can supply part of it, then places that can supply none of it. The last kind is only used for a damaged ship, for the repair, and skipped otherwise. This keeps a ship from flying to, say, a carrier with an empty hold.

Repair

The ship gets a vanilla Repair order for the destination: hull to 100% and all damaged components. At a place you don’t own, the repair is paid when you click, at the ship’s repair price, the same way the ship configuration menu charges up front. If you can’t afford it, the repair is skipped.

Repair and Resupply

  1. Target loadout: the ship’s Automatic Resupply setting (Off/Low/Medium/High, under the ship’s Individual Instructions). It can be set to use the commander’s setting; at the top of the chain is Default setting for automatic resupply in Global Orders. Off means nothing is bought. The engine turns the setting into an amount per missile, deployable and drone type.

  2. Shopping list: every type below its target is bought up to it, as far as free storage allows. Flares fill all free countermeasure space. With the flares option off (see Options), a ship that needs nothing else gets an empty list instead.

  3. Destination, as above.

  4. What the destination can supply: a missile type it can’t supply is swapped for one of the same kind (dumbfire, guided, torpedo, guided torpedo) that the ship already uses and the destination can supply. Anything else it can’t build is dropped, and the rest is limited to its stock.

  5. Orders: if the ship is damaged, a Repair order as above. The purchase becomes an equipment build at the destination, which creates an Equip order. The build includes everything the ship already carries, so nothing is removed. At stations you don’t own, you pay the station’s price for what’s bought. If you can’t afford it, the restock is skipped with a notification, the same as for repairs.

The Equip order runs first and the Repair waits behind it, so a damaged ship restocks, then repairs at the same place, then carries on with its queue.

The mod is one MD script, md/repair_nearest.xml, using the SirNukes Interact Menu API, plus English text in t/. It has no Lua and doesn’t patch any game files.

Why not use the vanilla Resupply order?

Vanilla Resupply takes a destination (param_station), so the mod could just find the place and hand it over. An earlier version did exactly that. Finding the place is the part vanilla can’t do: your ships only look in their fleet, the current sector and your own stations (see below). But vanilla then still decides what to buy, and that has drawbacks the mod avoids:

  • 20% threshold: vanilla only restocks an undamaged ship once its missile or countermeasure storage is under 20% full. A ship at 5/20 missiles (25% full) gets nothing. The mod tops it up to its target. At High that is presumably the full 20; at a lower setting the target may already be 5 or less, and nothing is bought.

  • Countermeasure quirk: in vanilla, a ship that only needs flares usually gets none. The mod fills free countermeasure space whenever it restocks, and with Flares-only resupply on (the default) it also sends a ship just for flares.

  • Subordinates: vanilla Resupply also makes a commander’s subordinates check their own needs and come along. The mod only handles the ships you send. Escorts still follow their commander, see the notes.

  • Repairs only at shipyards and wharves: vanilla Resupply only repairs at a shipyard or wharf that builds the ship’s class, or a supply ship. The mod uses vanilla Repair directly, which works at any station with a ship trader, so equipment docks count too.

  • All or nothing at the destination: vanilla only restocks at a place that can build every item on its list and has the resources for all of it. It does swap a missile type the place can’t build for one of the same kind that it can. Otherwise it repairs only and drops the ammo. The mod swaps missile types the same way, then buys whatever the place can build and has in stock, and prefers places that can supply everything.

  • Knowing beforehand: vanilla only works out what to buy once the order runs, and silently: if it decides there’s nothing to do, the order just ends, and you can’t tell why. Because the mod builds the shopping list itself, the tooltip shows what would be bought, and the button is greyed out when there is nothing to buy.

The 20% threshold is a single value in vanilla (interrupt.restock.xml line 62), so a one-line diff patch could raise it for the ships the mod sends, and the mod could go back to handing the destination to Resupply. The other points would remain, and the mod would patch a vanilla file, which it otherwise doesn’t.

If any Egosoft developer is reading this: a way to call the Resupply order without the 20% threshold, for example an order parameter, would be very welcome.

Targets, which drones and missiles, limiting to the destination’s stock and the equipment build itself use the same engine functions as vanilla.

Vanilla behaviour

For reference: what the game’s own hidden Repair and Resupply orders do. Fleets use them with their supply ships, and ships also run the Resupply check on their own at certain moments: after fleeing an attack, on each pass of an order loop, roughly every 30 minutes on patrol and trade routines, from escort orders in a fleet, and in various mining, salvage, protect and trade orders.

Repair (aiscripts/order.repair.xml)

  1. Stop if the destination isn’t operational or has no ship trader, or if the ship isn’t damaged.

  2. Fly to the destination and dock. At a station, the ship’s escorts dock along with it.

  3. Ask the station to restore the hull to the requested percentage and fix the listed components, and wait for it (up to 10 minutes).

  4. The script itself doesn’t charge you. When you repair through the ship configuration menu, the menu takes the money when you confirm.

Resupply (order.restock.xml, logic in ResupplyCheck in interrupt.restock.xml)

  1. Already on its way? If the ship has a pending Repair or Equip order, stop.

  2. Repair needed? A damaged ship repairs and restocks anything below its target. An undamaged ship only restocks below a 20% threshold (steps 3 and 6).

  3. Ammo at all? For an undamaged ship, ammo (missiles, countermeasures and deployables) is only considered if its missile or countermeasure storage is under 20% full; a ship at 5/20 missiles (25% full) stops here. Missile storage only counts if the ship has a weapon that fires missiles or bombs.

  4. Drones at all? Only for ships with a docking bay for small ships and free drone space.

  5. Target loadout: the Automatic Resupply setting, as described above.

  6. Shopping list: each missile, deployable and drone type below its target (undamaged: under 20% of it) is filled up to the target; the other types stay on the list at their current amount. Countermeasures are not on this list. If the ship has free deployable storage and none of its types needed buying, the whole ammo list is emptied, and an empty list ends ammo restocking here.

  7. Subordinates are told to run the same check, and those that need something go to the same place.

  8. Destination: supply ships and carriers in the ship’s own fleet, wherever they are; otherwise your carriers and fleet-assigned supply ships in the current sector; and, for the automatic checks, your own stations in the current sector (for repairs, a shipyard or wharf that builds the ship’s class). Never NPC stations, so a ship next to a friendly NPC shipyard will not repair there by itself.

    Places that can’t build everything on the list or lack the resources are skipped. If none is left, try again with repairs only, dropping the ammo.

  9. Countermeasures are added to the list only now, and only if ammo restocking is still on: the free space, split evenly over every type the destination builds.

    This is why a ship that only needs flares usually gets none. Low flares get it past step 3, but in step 6 nothing is bought, so the list is emptied before flares are ever added. Flares only come along if something else is bought, or if the ship carries missiles (which keep the list non-empty) and has no free deployable storage (so the list isn’t emptied).

  10. Orders: a Repair order if needed. The list is limited to the destination’s stock and becomes an equipment build there, which creates the Equip order.

Notes

  • The tooltip’s list is worked out before a destination is chosen, so it’s a maximum: the destination may not build everything or have it in stock.

  • The destination is picked when you click.

  • When a ship docks at a station, for repairs or restocking, its escorts dock along with it and wait (vanilla Repair and Equip do this). Nothing is repaired or bought for them.

  • There’s currently no errors shown for insufficient funds. A ship that docks for a resupply it cannot afford will leave without a message.