XRWS           ¤ì  –  £Vext_01.cat ext_01.dat md/fleet_reconstitution.xml 151 1739670937 c9f81f80c3a3b92e7a978510651c6797
md/PlayerRestockSubordinates.xml 38530 1754759281 25bed3aad87dfcd45b0114eb23b5ff82
t/0001-L081.xml 785 1725730030 6f2315a0efa92c1598058f16739440dc
t/0001.xml 788 1725730031 b9f5476010724b7a802147326ae78466
ui.xml 335 1739667516 8646e41fd36a820734b5fb1e0acdef53
ui/interactmenu.lua 1225 1754758548 9e2f681517ed858087c421ad31291786
<?xml version="1.0" encoding="utf-8"?>
<diff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<remove sel="//cue[@name='Init']/cues"/>
</diff><?xml version="1.0" encoding="utf-8"?>
<mdscript name="PlayerRestockSubordinates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../extracted/md/md.xsd">
  <cues>
    <cue name="Init" version="2">
      <actions>
        <enable_lost_ship_replacement/>
      </actions>
      <delay exact="5s"/>
      <actions>
        <find_ship_by_true_owner name="$AllMaybeLeads" multiple="true" faction="faction.player" space="player.galaxy" hascommander="false"/>
        <find_station_by_true_owner name="$AllMaybeLeads" multiple="true" append="true" faction="faction.player" space="player.galaxy"/>
        <do_for_each name="$Obj" in="$AllMaybeLeads">
          <raise_lua_event name="'ConfirmRestockSetting'" param="$Obj"/>
        </do_for_each>
        <remove_value name="$AllMaybeLeads"/>
        <remove_value name="$Obj"/>
      </actions>
      <patch sinceversion="2">
        <reset_cue cue="Init"/>
      </patch>
    </cue>
    <cue name="Conversation" instantiate="true" namespace="this">
      <conditions>
        <check_any>
          <event_conversation_started conversation="default" />
          <event_conversation_returned_to_section section="default"/>
        </check_any>
        <check_value value="@event.object.isclass.entity"/>
        <check_any>
          <check_value value="@event.object.assignedcontrolled.isclass.ship and event.object.assignedcontrolled.pilot == event.object" />
          <check_value value="@event.object.assignedcontrolled.isclass.station and event.object.assignedcontrolled.tradenpc == event.object" />
        </check_any>
        <check_value value="event.object.isplayerowned and event.object.assignedcontrolled.isplayerowned" />
        <check_value value="not @event.object.$Restock"/>
      </conditions>
      <actions>
        <add_player_choice text="{7171,4000}" section="EnableRestock"/>
      </actions>
    </cue>
    <cue name="EnableRestock" instantiate="true" namespace="this">
      <conditions>
        <event_conversation_next_section section="EnableRestock"/>
      </conditions>
      <actions>
        <raise_lua_event name="'EnableRestockSetting'" param="event.object.assignedcontrolled"/>
        <signal_cue_instantly cue="RestockManager" param="event.object"/>
        <speak actor="event.object" line="2103"/>
      </actions>
    </cue>
    <cue name="EnableFromUI" instantiate="true" namespace="this">
      <conditions>
        <event_ui_triggered screen="'player_restock_subordinates'" control="'toggle'"/>
        <check_value value="component.{event.param3.{1}}.exists and event.param3.{2}"/>
      </conditions>
      <actions>
        <set_value name="$Obj" exact="component.{event.param3.{1}}"/>
        <signal_cue_instantly cue="RestockManager" param="if $Obj.isclass.ship then $Obj.pilot else $Obj.tradenpc"/>
      </actions>
    </cue>
    <!-- return value: [Loadout (does not include deployables), ware table (includes ship hull ware and software), crew loadout [service crew, marine], assignment, group ID, ship name, ship type, ammo/deployable table, unit table] -->
    <library name="RecordLoadout" purpose="run_actions">
      <params>
        <param name="Ship"/>
        <param name="DebugChance" default="0"/>
      </params>
      <actions>
        <find_object_component name="$Comps" multiple="true" object="$Ship" checkoperational="false">
          <match class="[class.room, class.ship, class.entity]" negate="true"/>
        </find_object_component>
        <set_value name="$Wares" exact="table[]"/>
        <do_for_each name="$Obj" in="$Comps">
          <do_if value="$Obj.macro.ware">
            <set_value name="$Wares.{$Obj.macro.ware}" exact="1" operation="add"/>
          </do_if>
        </do_for_each>
        <do_for_each name="$Item" in="$Ship.software.installed.list">
          <set_value name="$Wares.{$Item}" exact="1" operation="add"/>
        </do_for_each>
        <!-- note: can't use add here because despite the separate-seeming categories, some items like missiles show up in multiple lists -->
        <evaluate_ammo_storage object="$Ship" type="deployable" wares="$Ammo" amounts="$AmmoAmt"/>
        <do_for_each name="$Item" in="$Ammo">
          <!-- deployables sometimes return the capacity here instead of the amount the ship was loaded with. Use the amount in storage, even if it results in a lower number than we really want,
                since it's unlikely the player wants a full stock of satellites or lasertowers
            -->
          <set_value name="$Wares.{$Item}" exact="[$Ship.ammostorage.{$Item.objectmacro}.count, 1].max"/>
        </do_for_each>
        <evaluate_ammo_storage object="$Ship" type="countermeasure" wares="$Ammo" amounts="$AmmoAmt"/>
        <do_for_each name="$Item" in="$Ammo">
          <set_value name="$Wares.{$Item}" exact="[$AmmoAmt.{loop.index}, $Ship.ammostorage.{$Item.objectmacro}.count].max"/>
        </do_for_each>
        <evaluate_ammo_storage object="$Ship" type="missile" wares="$Ammo" amounts="$AmmoAmt"/>
        <do_for_each name="$Item" in="$Ammo">
          <set_value name="$Wares.{$Item}" exact="[$AmmoAmt.{loop.index}, $Ship.ammostorage.{$Item.objectmacro}.count].max"/>
        </do_for_each>
        <!-- drones can be destroyed (even in low attention) and there's no way to determine the original value. Assume we want to fill up, making sure we satisfy the current storage before adding any potential extra. -->
        <evaluate_unit_storage object="$Ship" macros="$Drones" amounts="$DroneAmt" level="1.0"/>
        <!-- make sure to include drones the ship has right now -->
        <do_for_each name="$Category" in="[unitcategory.attack, unitcategory.build, unitcategory.defence, unitcategory.repair, unitcategory.transport]">
          <do_for_each name="$Item" in="$Ship.units.{$Category}.list">
            <do_if value="$Item.ware and $Drones.indexof.{$Item} == 0 and $Ship.units.{$Item}.count">
              <set_value name="$Wares.{$Item.ware}" exact="$Ship.units.{$Item}.count"/>
            </do_if>
          </do_for_each>
        </do_for_each>
        <set_value name="$Overflow" exact="table[]"/>
        <do_for_each name="$Item" in="$Drones">
          <do_if value="$Item.ware">
            <set_value name="$Amt" exact="$Ship.units.{$Item}.count"/>
            <do_if value="$Amt gt 0">
              <set_value name="$Wares.{$Item.ware}" exact="$Amt"/>
            </do_if>
            <do_if value="$DroneAmt.{loop.index} gt $Amt">
              <set_value name="$Overflow.{$Item.ware}" exact="$DroneAmt.{loop.index} - $Amt"/>
            </do_if>
          </do_if>
        </do_for_each>
        <set_value name="$RemainingAmt" exact="$Ship.units.free"/>
        <do_for_each name="$Item" valuename="$Amt" in="$Overflow">
          <set_value name="$Amt" exact="[$Amt, $RemainingAmt].min"/>
          <set_value name="$Wares.{$Item}" exact="$Amt" operation="add"/>
          <set_value name="$RemainingAmt" exact="$Amt" operation="subtract"/>
          <do_if value="$RemainingAmt le 0">
            <break/>
          </do_if>
        </do_for_each>
        <!-- ugh, we have to ask which thruster it is by looping, because you can't actually get it directly in MD -->
        <get_ware_definition result="$AllThrusters" group="'thrusters'" />
        <do_for_each name="$Item" in="$AllThrusters">
          <check_object result="$GotIt" object="$Ship">
            <match_loadout wares="[$Item]"/>
          </check_object>
          <do_if value="$GotIt">
            <set_value name="$Wares.{$Item}" exact="1"/>
            <break/>
          </do_if>
        </do_for_each>

        <!-- remove deployables and units from loadout generation. It doesn't consistently give us the desired amount, and as of 4.0 is bugged to fill the ship while lying in the "used" table -->
        <set_value name="$LoadoutWares" exact="table[]"/>
        <set_value name="$Ammo" exact="table[]"/>
        <set_value name="$Units" exact="table[]"/>
        <do_for_each name="$Item" valuename="$Amt" in="$Wares">
          <do_if value="@$Item.macro.isunit">
            <set_value name="$Units.{$Item}" exact="$Amt"/>
          </do_if>
          <do_elseif value="@$Item.macro.isdeployable or $Item.isammo">
            <set_value name="$Ammo.{$Item}" exact="$Amt"/>
          </do_elseif>
          <do_else>
            <set_value name="$LoadoutWares.{$Item}" exact="$Amt"/>
          </do_else>
        </do_for_each>
        
        <!-- TODO: temporarily using faction.criminal - with hacks in loadoutrules.xml - because it has better rules for deployables and drones. Has no effect otherwise (probably) -->
        <!-- will need to hack the deployable/drone loadout as a separate build to get reliably matching results to the current specs -->
        <generate_loadout result="$Loadouts" macro="$Ship.macro" level="1.0" variation="0.0" wares="$LoadoutWares" faction="faction.player" used="$Used"/>
        <debug_text text="'Loadout attempt for ' + $Ship.knownname + ' ' + $Wares + ' ' + $Used" chance="$DebugChance"/>
        <set_value name="$Used.{$Ship.macro.ware}" exact="1"/>
        <set_value name="$Used.{ware.crew}" exact="$Ship.people.{entityrole.service}.count + $Ship.people.{entityrole.marine}.count"/>
        <!-- add ammo/deployables/units to the Used table for pricing -->
        <do_all exact="2" counter="$i">
          <do_for_each name="$Item" valuename="$Amt" in="if ($i == 1) then $Ammo else $Units">
            <assert value="not $Used.{$Item}?"/>
            <set_value name="$Used.{$Item}" exact="$Amt"/>
          </do_for_each>
        </do_all>
        <set_value name="$People" exact="[$Ship.people.{entityrole.service}.count, $Ship.people.{entityrole.marine}.count]"/>
        <return value="[$Loadouts.{1}, $Used, $People, $Ship.assignment, $Ship.subordinategroupid, $Ship.rawname, $Ship.type, $Ammo, $Units]"/>
      </actions>
    </library>
    <cue name="RestockManager" instantiate="true" namespace="this" version="5">
      <conditions>
        <event_cue_signalled/>
        <check_value value="@event.param.isclass.entity"/>
        <check_any>
          <check_value value="@event.param.assignedcontrolled.isclass.ship and event.param.assignedcontrolled.pilot == event.param" />
          <check_value value="@event.param.assignedcontrolled.isclass.station and event.param.assignedcontrolled.tradenpc == event.param" />
        </check_any>
        <check_value value="event.param.isplayerowned and event.param.assignedcontrolled.isplayerowned" />
        <check_value value="not @event.param.$Restock"/>
      </conditions>
      <actions>
        <set_value name="$DebugChance" exact="100"/>
        <set_value name="$Entity" exact="event.param"/>
        <set_value name="$Commander" exact="event.param.assignedcontrolled"/>
        <set_value name="$Entity.$Restock" exact="true"/>
        <create_group groupname="$Subordinates"/>
        <do_for_each name="$Obj" in="$Commander.subordinates">
          <do_if value="not $Obj.isunit and not $Obj.isclass.ship_xs">
            <add_to_group groupname="$Subordinates" object="$Obj"/>
          </do_if>
        </do_for_each>
        <set_value name="$RebuildCues" exact="[]"/>
        <set_value name="$Loadouts" exact="table[]"/>
        <do_for_each name="$Obj" in="$Subordinates">
          <run_actions ref="RecordLoadout" result="$NewLoadout">
            <param name="Ship" value="$Obj"/>
            <param name="DebugChance" value="$DebugChance"/>
          </run_actions>
          <set_value name="$Loadouts.{$Obj}" exact="$NewLoadout"/>
        </do_for_each>
        <remove_value name="$Obj"/>
      </actions>
      <patch sinceversion="3">
        <do_for_each name="$Obj" in="$Subordinates" reverse="true">
          <do_if value="$Obj.isunit or $Obj.isclass.ship_xs">
            <remove_value name="$Loadouts.{$Obj}"/>
            <remove_from_group group="$Subordinates" object="$Obj"/>
          </do_if>
        </do_for_each>
        <remove_value name="$Obj"/>
      </patch>
      <patch sinceversion="4">
        <!-- regenerate loadouts to handle ammo/deployable/unit build split -->
        <do_for_each name="$Obj" in="$Subordinates">
          <run_actions ref="RecordLoadout" result="$NewLoadout">
            <param name="Ship" value="$Obj"/>
            <param name="DebugChance" value="100"/>
          </run_actions>
          <set_value name="$Loadouts.{$Obj}" exact="$NewLoadout"/>
        </do_for_each>
      </patch>
      <patch sinceversion="5">
        <raise_lua_event name="'EnableRestockSetting'" param="$Entity.assignedcontrolled"/>
      </patch>
      <cues>
        <cue name="CancelConversation" instantiate="true">
          <conditions>
            <check_any>
              <event_conversation_started conversation="default" />
              <event_conversation_returned_to_section section="default"/>
            </check_any>
            <check_value value="event.object == $Entity"/>
          </conditions>
          <actions>
            <add_player_choice text="{7171,4001}" section="DisableRestock"/>
          </actions>
        </cue>
        <cue name="DisableRestock">
          <conditions>
            <event_conversation_next_section section="DisableRestock"/>
            <check_value value="event.object == $Entity"/>
          </conditions>
          <actions>
            <debug_text text="'Restock for ' + $Commander.knownname + ' ended by player '" chance="$DebugChance"/>
            <set_value name="$Entity.$Restock" exact="false"/>
            <raise_lua_event name="'DisableRestockSetting'" param="event.object.assignedcontrolled"/>
            <cancel_cue cue="RestockManager"/>
            <speak actor="event.object" line="2103"/>
          </actions>
        </cue>
        <cue name="DisableFromUI">
          <conditions>
            <event_ui_triggered screen="'player_restock_subordinates'" control="'toggle'"/>
            <check_value value="component.{event.param3.{1}} == $Entity.controllable and not event.param3.{2}"/>
          </conditions>
          <actions>
            <debug_text text="'Restock for ' + $Commander.knownname + ' ended by player (via UI) '" chance="$DebugChance"/>
            <set_value name="$Entity.$Restock" exact="false"/>
            <cancel_cue cue="RestockManager"/>
          </actions>
        </cue>
        <cue name="CommanderDestroyed">
          <conditions>
            <check_any>
              <event_object_destroyed object="$Commander"/>
              <event_object_changed_owner object="$Commander"/>
              <event_object_abandoned object="$Commander"/>
            </check_any>
          </conditions>
          <actions>
            <debug_text text="'Restock for ' + $Commander.knownname + ' ended by ' + event.name" chance="$DebugChance"/>
            <cancel_cue cue="RestockManager"/>
          </actions>
        </cue>
        <cue name="EntityChanged">
          <conditions>
            <check_any>
              <event_control_entity_added object="$Commander"/>
              <event_control_entity_removed object="$Commander"/>
              <event_object_destroyed object="$Entity"/>
            </check_any>
            <check_value value="@$Commander.pilot != $Entity and @$Commander.tradenpc != $Entity and @$Commander.assignedaipilot != $Entity"/>
          </conditions>
          <actions>
            <debug_text text="'Control entity for ' + $Commander.knownname + ' was changed, cancelling restock behavior'" chance="$DebugChance"/>
            <!-- remove instead of set false since this is not an explicit player override -->
            <remove_value name="$Entity.$Restock"/>
            <cancel_cue cue="RestockManager"/>
          </actions>
        </cue>
        <cue name="NewSubordinate" instantiate="true">
          <conditions>
            <event_object_subordinate_added object="$Commander"/>
            <check_value value="not event.param.isunit"/>
          </conditions>
          <actions>
            <add_to_group groupname="$Subordinates" object="event.param"/>
            <run_actions ref="RecordLoadout" result="$NewLoadout">
              <param name="Ship" value="event.param"/>
              <param name="DebugChance" value="$DebugChance"/>
            </run_actions>
            <set_value name="$Loadouts.{event.param}" exact="$NewLoadout"/>
          </actions>
        </cue>
        <cue name="LostSubordinate" instantiate="true">
          <conditions>
            <event_object_subordinate_removed object="$Commander"/>
            <!-- apparently this event gets fired before it's officially dead
            <check_value value="event.param.isoperational"/>
            -->
          </conditions>
          <actions>
            <set_value name="this.$Lost" exact="event.param"/>
          </actions>
          <delay exact="1s"/>
          <actions>
            <!-- ignore ships that the player takes over, since they'll be restored when the player relinquishes control -->
            <do_if value="@this.$Lost.pilot == player.entity">
              <signal_cue_instantly cue="HandlePlayerTakeover" param="this.$Lost"/>
            </do_if>
            <!-- handle edge case that commander got reattached -->
            <do_elseif value="not this.$Lost.exists or (this.$Lost.commander != $Commander and this.$Lost.pilot != player.entity)">
              <do_if value="this.$Lost.exists" comment="only need to do this on non-death removal">
                <remove_from_group group="$Subordinates" object="this.$Lost"/>
              </do_if>
              <remove_value name="$Loadouts.{this.$Lost}"/>
            </do_elseif>
          </actions>
        </cue>
        <!-- this cue handles the case where the player takes over a ship
             the commander is technically null and restored when the player leaves, but we want to maintain it in the list unless the commander gets truly removed,
             so that if the player gets it killed it gets replaced as expected
         -->
        <cue name="HandlePlayerTakeover" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <set_value name="this.$TakenShip" exact="event.param"/>
          </actions>
          <cues>
            <cue name="HandlePlayerTakeoverFinished" checkinterval="15s" instantiate="true">
              <actions>
                <do_if value="not parent.$TakenShip.exists or parent.$TakenShip.commander == $Commander">
                  <!-- no longer exists or got reconnected so all is well -->
                  <cancel_cue cue="parent"/>
                </do_if>
                <do_elseif value="parent.pilot != player.entity">
                  <!-- we keep repeating this until we end up with the wrong/no commander or the commander gets applied and it falls into the block above -->
                  <!-- this accounts for the player removing the assign commander order to restore commander or any other edge case that prevents the commander from being restored -->
                  <set_value name="this.$NewCommander" exact="parent.$TakenShip.commander"/>
                  <do_if value="not this.$NewCommander.exists">
                    <do_for_each name="this.$Order" in="parent.$TakenShip.orders">
                      <do_if value="this.$Order.id == 'AssignCommander'">
                        <set_value name="this.$NewCommander" exact="this.$Order.$commander"/>
                      </do_if>
                    </do_for_each>
                  </do_if>
                  <do_if value="this.$NewCommander != $Commander">
                    <remove_from_group group="$Subordinates" object="parent.$TakenShip"/>
                    <remove_value name="$Loadouts.{parent.$TakenShip}"/>
                    <cancel_cue cue="parent"/>
                  </do_if>
                </do_elseif>
              </actions>
            </cue>
          </cues>
        </cue>
        <cue name="UpdateSubordinate" instantiate="true">
          <conditions>
            <event_object_undocked group="$Subordinates" comment="closest available event"/>
            <check_value value="event.param.cansupplyclass.{event.object.class}"/>
          </conditions>
          <actions>
            <run_actions ref="RecordLoadout" result="$NewLoadout">
              <param name="Ship" value="event.object"/>
              <param name="DebugChance" value="$DebugChance"/>
            </run_actions>
            <set_value name="$Loadouts.{event.object}" exact="$NewLoadout"/>
          </actions>
        </cue>
        <cue name="DestroyHandler" instantiate="true" namespace="this" version="2">
          <conditions>
            <check_any>
              <event_object_destroyed group="parent.$Subordinates"/>
              <check_all>
                <event_object_changed_owner group="parent.$Subordinates"/>
                <!-- there are exceptions, but the most likely reason for ownership to go from player to another faction is because the ship was sold. In that case, we don't want to rebuild it. -->
                <check_value value="event.param == faction.ownerless"/>
              </check_all>
              <event_object_abandoned group="parent.$Subordinates"/>
            </check_any>
            <check_value value="parent.$Loadouts.{event.object}?"/>
          </conditions>
          <actions>
            <debug_text text="'detected ' + event.object.knownname + ' destroyed ' + event.name + ' ' + parent.$Subordinates.indexof.{event.object}"/>
            <set_value name="$Commander" exact="parent.$Commander"/>
            <set_value name="$Data" exact="parent.$Loadouts.{event.object}"/>
            <remove_value name="parent.$Loadouts.{event.object}"/>
            <set_value name="$Loadout" exact="$Data.{1}"/>
            <set_value name="$WareTable" exact="$Data.{2}"/>
            <set_value name="$Crew" exact="$Data.{3}"/>
            <set_value name="$Assignment" exact="$Data.{4}"/>
            <set_value name="$GroupID" exact="$Data.{5}"/>
            <set_value name="$ShipName" exact="$Data.{6}"/>
            <set_value name="$ShipType" exact="$Data.{7}"/>
            <set_value name="$Ammo" exact="$Data.{8}"/>
            <set_value name="$Units" exact="$Data.{9}"/>
            <set_value name="$Macro" exact="event.object.macro"/>
            <set_value name="$DebugChance" exact="parent.$DebugChance"/>
            <set_value name="$NextRetryTime" exact="0s"/>
            <append_to_list name="parent.$RebuildCues" exact="this"/>
          </actions>
          <patch sinceversion="2">
            <do_if value="$Macro.isunit or $Macro.isclass.ship_xs">
              <cancel_cue cue="DestroyHandler"/>
            </do_if>
          </patch>
          <cues>
            <!-- TODO: setting to wait for commander to be out of combat -->
            <cue name="StartRebuild" checkinterval="30s">
              <conditions>
                <check_age min="$NextRetryTime"/>
                <count_gravidar_contacts object="$Commander" exact="0" invulnerable="false" indestructible="false" maybeattackedby="$Commander">
                  <match_relation_to object="$Commander" relation="kill" comparison="le"/>
                </count_gravidar_contacts>
              </conditions>
              <actions>
                <set_value name="$NextRetryTime" exact="player.age + 30min"/>
                <!-- find a shipyard to build the ship -->
                <find_station_by_true_owner name="$Yards" multiple="true" space="player.galaxy" canbuildclass="$Macro.class" canbuildmacro="$Macro" canbuildfor="faction.player" faction="faction.player"/>
                <do_if value="$Yards.count gt 0">
                  <!-- for player shipyard only, require blueprints for ALL equipment -->
                  <do_for_each name="$TestYard" in="$Yards" counter="$i" reverse="true">
                    <do_if value="not $TestYard.maybuildfor.{faction.player} or not $TestYard.shiptrader.exists">
                      <remove_value name="$Yards.{$i}"/>
                    </do_if>
                    <do_for_each name="$Ware" in="$WareTable">
                      <do_if value="$Ware.tags.indexof.{tag.equipment} gt 0 and $Ware.tags.indexof.{tag.software} == 0 and not $TestYard.buildequipment.wares.{$Ware}.exists">
                        <debug_text text="' -- cannot rebuild ' + $ShipName + ' at ' + $TestYard.knownname + ' because of missing equipment ' + $Ware" chance="$DebugChance"/>
                        <set_value name="this.$PlayerFailedWare" exact="$Ware"/>
                        <remove_value name="$Yards.{$i}"/>
                        <break/>
                      </do_if>
                    </do_for_each>
                  </do_for_each>
                </do_if>
                <do_if value="$Yards.count == 0">
                  <find_station name="$Yards" multiple="true" space="player.galaxy" canbuildclass="$Macro.class" canbuildmacro="$Macro" canbuildfor="faction.player" knownto="faction.player">
                    <match trueowner="faction.player" negate="true"/>
                    <match_relation_to faction="faction.player" relation="dock" comparison="ge"/>
                  </find_station>
                  <!-- the filtering does not seem to work, so manually check for build capability (copying behavior in build.shiptrader) -->
                  <do_for_each name="$TestYard" in="$Yards" counter="$i" reverse="true">
                    <set_value name="this.$CanBuild" exact="false"/>
                    <do_for_each name="$Processor" in="$TestYard.buildprocessors">
                      <do_if value="$Processor.canbuildclass.{$Macro.class} and $Processor.canbuildmacro.{$Macro}">
                        <set_value name="this.$CanBuild" exact="true"/>
                        <!-- in certain cases (e.g. ALI Wharf in pirate DLC) facility has license to build but does not stock needed resources -->
                        <do_for_each name="$Ware" in="$WareTable">
                          <!-- TODO: does this work if the player gifts a faction another race's build module? -->
                          <do_for_each name="$Resource" in="$Ware.raceresources.{$TestYard.trueowner.primaryrace}.list">
                            <do_if value="not $TestYard.resources.{$Resource}.exists">
                              <set_value name="this.$CanBuild" exact="false"/>
                              <break/>
                            </do_if>
                          </do_for_each>
                          <do_if value="not this.$CanBuild">
                            <break/>
                          </do_if>
                        </do_for_each>
                        <break/>
                      </do_if>
                    </do_for_each>
                    <do_if value="not this.$CanBuild">
                      <remove_value name="$Yards.{$i}"/>
                    </do_if>
                  </do_for_each>
                  <!-- check for licence -->
                  <!-- FIXME: can't get required licence here, ugh! -->
                  <do_if value="($Macro.primarypurpose == purpose.fight or $Macro.primarypurpose == purpose.auxiliary) and $ShipType != shiptype.scout">
                    <do_if value="$Macro.class == class.ship_l or $Macro.class == class.ship_xl">
                      <do_for_each name="$TestYard" in="$Yards" counter="$i" reverse="true">
                        <do_if value="not faction.player.haslicence.capitalship.{$TestYard.trueowner}">
                          <remove_value name="$Yards.{$i}"/>
                        </do_if>
                      </do_for_each>
                    </do_if>
                    <do_else>
                      <do_for_each name="$TestYard" in="$Yards" counter="$i" reverse="true">
                        <do_if value="not faction.player.haslicence.militaryship.{$TestYard.trueowner}">
                          <remove_value name="$Yards.{$i}"/>
                        </do_if>
                      </do_for_each>
                    </do_else>
                  </do_if>
                </do_if>
                <!-- lower is better -->
                <set_value name="$BestRating" exact="100000"/>
                <do_for_each name="$TestYard" in="$Yards">
                  <set_value name="$Rating" exact="$Commander.gatedistance.{$TestYard}"/>
                  <do_if value="$TestYard.builds.queued.count + $TestYard.builds.inprogress.count ge $TestYard.buildprocessors.count">
                    <set_value name="$Rating" exact="100" operation="add"/>
                  </do_if>
                  <do_else>
                    <do_for_each name="$TestBuild" in="$TestYard.builds.queued">
                      <do_if value="$TestYard.neededbuildresources.{$TestBuild}.count gt 0">
                        <set_value name="$Rating" exact="1" operation="add"/>
                        <break/>
                      </do_if>
                    </do_for_each>
                  </do_else>
                  <do_if value="$Rating lt $BestRating">
                    <set_value name="$Shipyard" exact="$TestYard"/>
                    <set_value name="$BestRating" exact="$Rating"/>
                  </do_if>
                </do_for_each>
                <do_if value="not @$Shipyard.exists">
                  <do_if value="this.$PlayerFailedWare?">
                    <substitute_text text="$LogMsg" source="{7171,4013}">
                      <replace string="'$COMMANDER$'" with="$Commander.knownname"/>
                      <replace string="'$SHIP$'" with="$ShipName"/>
                      <replace string="'$WARE$'" with="this.$PlayerFailedWare.name"/>
                    </substitute_text>
                  </do_if>
                  <do_else>
                    <substitute_text text="$LogMsg" source="{7171,4012}">
                      <replace string="'$COMMANDER$'" with="$Commander.knownname"/>
                      <replace string="'$SHIP$'" with="$ShipName"/>
                    </substitute_text>
                  </do_else>
                  <write_to_logbook object="$Commander" category="upkeep" interaction="showonmap" title="$LogMsg"/>
                  <reset_cue cue="StartRebuild"/>
                </do_if>
                <do_else>
                  <debug_text text="'Rebuilding ' + $Macro + ' at ' + $Shipyard.knownname + ' ' + $Shipyard.buildpricefactor" chance="$DebugChance"/>
                  <!-- is there any better way to figure out the price...? -->
                  <set_value name="$Price" exact="0Cr"/>
                  <do_if value="not $Shipyard.isplayerowned">
                    <do_for_each name="$Ware" valuename="$Count" in="$WareTable">
                      <set_value name="$ItemPrice" exact="(($Shipyard.buyprice.{$Ware})LF * ($Shipyard.buildpricefactor)LF * 0.01LF)Cr * $Count"/>
                      <debug_text text="' --- price for ' + $Ware + ' x' + $Count + ' is ' + $ItemPrice.formatted.default" chance="$DebugChance"/>
                      <set_value name="$Price" exact="$ItemPrice" operation="add"/>
                    </do_for_each>
                  </do_if>
                  <debug_text text="'Price for ' + $Macro + ' is ' + $Price.formatted.default" chance="$DebugChance"/>
                  <do_if value="faction.player.money ge $Price">
                    <!-- we can't use the price element because it charges when the build is done, but refunds on cancel! -->
                    <transfer_money from="faction.player" to="$Shipyard" amount="$Price"/>
                    <substitute_text text="$LogMsg" source="{7171,4010}">
                      <replace string="'$COMMANDER$'" with="$Commander.knownname"/>
                      <replace string="'$SHIP$'" with="$ShipName"/>
                      <replace string="'$SHIPYARD$'" with="$Shipyard.knownname"/>
                      <replace string="'$SECTOR$'" with="$Shipyard.sector.knownname"/>
                    </substitute_text>
                    <write_to_logbook object="$Commander" category="upkeep" interaction="showonmap" title="$LogMsg" money="if $Price gt 0Cr then ($Price * -1) else null"/>
                    <add_build_to_construct_ship result="$Build" object="$Shipyard" macro="$Macro" faction="faction.player" loadout="$Loadout"/>
                  </do_if>
                  <do_else>
                    <substitute_text text="$LogMsg" source="{7171,4011}">
                      <replace string="'$COMMANDER$'" with="$Commander.knownname"/>
                      <replace string="'$SHIP$'" with="$ShipName"/>
                    </substitute_text>
                    <write_to_logbook object="$Commander" category="upkeep" interaction="showonmap" title="$LogMsg"/>
                    <reset_cue cue="StartRebuild"/>
                  </do_else>
                </do_else>
              </actions>
              <cues>
                <!-- sigh, apparently player cancel does not actually call the event -->
                <cue name="BuildResolvedFallback" checkinterval="1s">
                  <conditions>
                    <check_value value="not $Build.exists and $Shipyard.exists"/>
                  </conditions>
                  <actions>
                    <signal_cue cue="BuildResolved" check="false"/>
                  </actions>
                </cue>
                <cue name="BuildResolved">
                  <conditions>
                    <check_any>
                      <event_build_finished object="$Shipyard" build="$Build"/>
                      <event_build_cancelled object="$Shipyard" build="$Build"/>
                      <event_object_destroyed object="$Shipyard"/>
                      <event_cue_signalled/>
                    </check_any>
                  </conditions>
                  <actions>
                    <do_if value="event.name == 'event_build_finished'">
                      <set_value name="$NewShip" exact="event.param.buildanchor"/>
                      <assert value="$NewShip.exists"/>
                      <set_object_name object="$NewShip" name="$ShipName"/>
                    </do_if>
                    <do_elseif value="(event.name == 'event_build_cancelled' or event.name == 'event_cue_signalled') and $Price gt 0Cr">
                      <!-- refund price -->
                      <transfer_money from="$Shipyard" to="faction.player" amount="$Price"/>
                    </do_elseif>
                  </actions>
                  <!-- delay to let the shipyard do stuff first (assign pilot, mostly) -->
                  <delay exact="1s"/>
                  <actions>
                    <do_if value="@$NewShip.exists">
                      <!-- transferring people didn't work and there's no way to get at the DB to generate any, so we'll just clone the pilot and set skills manually -->
                      <set_value name="$Roles" exact="[entityrole.service, entityrole.marine]"/>
                      <do_all exact="$Roles.count" counter="$i">
                        <do_if value="@$Crew.{$i} gt 0">
                          <do_all exact="$Crew.{$i}">
                            <create_npc_template name="$NPC" object="$NewShip" role="$Roles.{$i}" macro="$NewShip.pilot.macro"/>
                            <set_skill type="skilltype.engineering" object="$NewShip" template="$NPC" exact="0"/>
                            <set_skill type="skilltype.morale" object="$NewShip" template="$NPC" min="1" max="5"/>
                            <set_skill type="skilltype.boarding" object="$NewShip" template="$NPC" exact="0"/>
                            <set_skill type="skilltype.management" object="$NewShip" template="$NPC" exact="0"/>
                            <set_skill type="skilltype.piloting" object="$NewShip" template="$NPC" exact="0"/>
                            <do_if value="$Roles.{$i} == entityrole.marine">
                              <set_skill type="skilltype.boarding" object="$NewShip" template="$NPC" min="1" max="4"/>
                            </do_if>
                            <do_else>
                              <set_skill type="skilltype.engineering" object="$NewShip" template="$NPC" min="1" max="5"/>
                              <set_skill type="skilltype.management" object="$NewShip" template="$NPC" min="0" max="3"/>
                            </do_else>
                          </do_all>
                        </do_if>
                      </do_all>
                      <do_if value="$Ammo? or $Units?">
                        <add_build_to_modify_ship object="$Shipyard" buildobject="$NewShip" internal="true" ammo="$Ammo" units="$Units" immediate="true"/>
                      </do_if>
                      <create_order object="$NewShip" id="'AssignCommander'">
                        <param name="commander" value="$Commander"/>
                        <param name="assignment" value="$Assignment"/>
                        <param name="subordinategroup" value="$GroupID"/>
                        <param name="setgroupassignment" value="true"/>
                      </create_order>
                      <!-- toggle Egosoft setting to clear the fleet backups that we're not currently using -->
                      <raise_lua_event name="'DisableRestockSetting'" param="$Commander"/>
                      <raise_lua_event name="'EnableRestockSetting'" param="$Commander"/>
                    </do_if>
                    <remove_from_list name="namespace.parent.$RebuildCues" exact="this"/>
                    <cancel_cue cue="DestroyHandler"/>
                  </actions>
                </cue>
              </cues>
            </cue>
          </cues>
        </cue>
      </cues>
    </cue>
  </cues>
</mdscript><?xml version="1.0" encoding="UTF-8" ?>
<language id="44">

<page id="7171" title="Mysterial's Mod Text" descr="Text for the missions created by Mysterial" voice="no">
 <t id="4000">部下の補充を有効化</t>
 <t id="4001">部下の補充を無効化</t>
 <t id="4010">$COMMANDER$は、$SECTOR$の$SHIPYARD$で$SHIP$の補充を発注しました。</t>
 <t id="4011">$COMMANDER$は資金不足で$SHIP$の補充に失敗しました。</t>
 <t id="4012">$COMMANDER$は発注可能な施設がなかったため、$SHIP$の補充に失敗しました。</t>
 <t id="4013">$COMMANDER$ failed to replace $SHIP$ because no shipyard capable and willing to build the ship could be found. The builder you own does not have the blueprints for $WARE$.</t>
</page>

</language><?xml version="1.0" encoding="UTF-8" ?>
<language id="44">

<page id="7171" title="Mysterial's Mod Text" descr="Text for the missions created by Mysterial" voice="no">
 <t id="4000">Enable Restock Subordinates</t>
 <t id="4001">Disable Restock Subordinates</t>
 <t id="4010">$COMMANDER$ has ordered replacement for $SHIP$ from $SHIPYARD$ in $SECTOR$.</t>
 <t id="4011">$COMMANDER$ failed to replace $SHIP$ due to insufficient funds.</t>
 <t id="4012">$COMMANDER$ failed to replace $SHIP$ because no shipyard capable and willing to build the ship could be found.</t>
 <t id="4013">$COMMANDER$ failed to replace $SHIP$ because no shipyard capable and willing to build the ship could be found. The builder you own does not have the blueprints for $WARE$.</t>
</page>

</language><?xml version="1.0" encoding="UTF-8"?>
<addon name="player_restock_subordinates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../ui/core/addon.xsd">
  <environment type="menus">
    <file name="ui/interactmenu.lua" />
    <dependency name="ego_interactmenu" />
  </environment>
</addon>local menu = nil
local Funcs = {}

local function InitMenu()
	for _, OrigMenu in ipairs(Menus) do
		if OrigMenu.name == "InteractMenu" then
			menu = OrigMenu
			Funcs.Super_buttonFleetManagement = menu.buttonFleetManagement
			menu.buttonFleetManagement = Funcs.buttonFleetManagement
			break
		end
	end
end

function Funcs.buttonFleetManagement(controllable, enable)
	AddUITriggeredEvent("player_restock_subordinates", "toggle", {ConvertStringTo64Bit(tostring(controllable)), enable})
	Funcs.Super_buttonFleetManagement(controllable, enable)
end

local function EnableRestockSetting(event, param)
	Funcs.Super_buttonFleetManagement(ConvertStringTo64Bit(tostring(param)), true)
end
local function DisableRestockSetting(event, param)
	Funcs.Super_buttonFleetManagement(ConvertStringTo64Bit(tostring(param)), false)
end
local function ConfirmRestockSetting(event, param)
	local obj = ConvertStringTo64Bit(tostring(param))
	Funcs.buttonFleetManagement(obj, GetComponentData(obj, "isfleetlead"))
end

InitMenu()
RegisterEvent("EnableRestockSetting", EnableRestockSetting)
RegisterEvent("DisableRestockSetting", DisableRestockSetting)
RegisterEvent("ConfirmRestockSetting", ConfirmRestockSetting)<?xml version="1.0" encoding="utf-8"?>
<content id="" author="Mysterial (mysterial@mysterial.net)" name="" description="" version="103" date="2025-08-09" save="false" lastupdate="">
  <text language="7" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="33" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="34" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="39" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="44" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="49" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="55" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="81" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="82" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="86" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
  <text language="88" name="Player Restock Subordinates" description="Adds the ability to set fleet commanders to automatically repurchase or rebuild lost subordinates." author="Mysterial (mysterial@mysterial.net)"/>
</content>
