Need someone to take a look at this script please

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 19:00

Alright, I need some help if anyone can think of anything because I have been trying to get something to work for a while now and nothing I try works.

So, there are two scripts from vanilla that handle the building of new factories. One is FactionGoal_Hold_Space which handles the stations that have shortages, Hightech and lowtech wares like hull parts, engine parts, silicon wafers, etc. Not all wares though. The other script is FactionLogic_Economy. These are the only scripts I am aware of that build new factories (not talking about defense stations or shipyards, etc. just the trade factories)

So this is the block of code

Code: Select all

<do_if value="$WantedHightechFactories gt $HightechFactories and ($OtherFactories + $HightechFactories) lt $ExistingFactoryThreshold">
                  <do_all exact="$WantedHightechFactories - $HightechFactories">
                    <set_value name="$Yaw" min="0deg" max="360deg"/>
                    <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>

                    <shuffle_list list="$HightechWares"/>
                    <do_for_each name="$HightechWare" in="$HightechWares">
                      <get_module_definition reference="$ResultRef" faction="$Faction" ware="$HightechWare"/>
                      <do_if value="$ResultRef">
                        <set_value name="$Yaw" min="0deg" max="360deg"/>
                        <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>
                        <set_value name="$PlacementDist" min="0km" max="$SectorCoreSize" profile="bell" scale="2"/>
                        <create_factory name="$Station" modules="$Modules" sector="$Target" race="$Faction.primaryrace" owner="$Faction">
                          <compatibilities>
                            <limits production="2"/>
                          </compatibilities>
                          <select ware="$HightechWare" race="$Faction.primaryrace"/>
                          <safepos x="$SectorCentre.x + sin($Yaw) * $PlacementDist" y="$Y" z="$SectorCentre.z + cos($Yaw) * $PlacementDist" includeplotbox="true"/>
                        </create_factory>
                        <do_if value="$Station">
                          <debug_text filter="economy_verbose" text="'#FLE#;%1;SeedHighTechFactory;%2;%3;%4;%5;%6;%7'.[player.age, $Station.knownname, $Station, $Station.idcode, $Target.knownname, $Faction.id, $HightechWare, '', '']" context="false"/>
                          <debug_text text="$DebugText + 'Building high tech factory ' + $Station + ' for ware ' + $HightechWare" context="false" chance="$DebugChance"/>
                          <signal_cue_instantly cue="md.FinaliseStations.NewStation_GenerateFactory_Signal" param="table[
                                                $station = $Station,
                                                $plannedmodules = $Modules,
                                                $moduleset = $Station.modulesets.{1},
                                                $debugoutput = if $DebugChance == 100 then true else false
                                                ]"/>
                        </do_if>
                        <break/>
                      </do_if>
                    </do_for_each>
                  </do_all>
                </do_if>

Especially this part

Code: Select all

 <create_factory name="$Station" modules="$Modules" sector="$Target" race="$Faction.primaryrace" owner="$Faction">
                          <compatibilities>
                            <limits production="2"/>
                          </compatibilities>
                          <select ware="$HightechWare" race="$Faction.primaryrace"/>
                          <safepos x="$SectorCentre.x + sin($Yaw) * $PlacementDist" y="$Y" z="$SectorCentre.z + cos($Yaw) * $PlacementDist" includeplotbox="true"/>
                        </create_factory>
Now if I take this part <create_factory name="$Station" modules="$Modules" sector="$Target" race="$Faction.primaryrace" owner="$Faction"> and put instead owner="faction.argon"> just as an example. It strangely builds an infinite amount of stations that belong to argon for all the wares over and over again. I wanted to have it build these stations for the corporations instead of the main government factions but even if I put faction.argonship which is one of my corporations I get the same issue.

I tried several other ways to get this to work to no avail, including <set_value name="$Main_Factions" exact="faction.argonship"/> No matter what it ends up building an infinite amount of stations. Even when I get it to build the right factory for the right faction it builds infinite amounts of them.

Anyone have any ideas what is going on here?

User avatar
Marvin Martian
Posts: 3547
Joined: Sun, 8. Apr 12, 09:40
x4

Re: Need someone to take a look at this script please

Post by Marvin Martian » Wed, 1. Jul 20, 21:30

create_factory will "order" to build a station instead of create_station what spawns it immediately

so i would think your script still "orders" too much (and/or get executed too often) :gruebel:

have you tryed to use create_station for testing to see whats your skript want to produce finally?

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 22:01

Marvin Martian wrote:
Wed, 1. Jul 20, 21:30
create_factory will "order" to build a station instead of create_station what spawns it immediately

so i would think your script still "orders" too much (and/or get executed too often) :gruebel:

have you tryed to use create_station for testing to see whats your skript want to produce finally?
Hey Marvin, I am not making a new script here, just slightly editing the already existing vanilla script. This is exactly the same script that is in the vanilla game, I just changed the owner. So not sure why it would make infinite stations?

SirNukes
Posts: 546
Joined: Sat, 31. Mar 07, 23:44
x4

Re: Need someone to take a look at this script please

Post by SirNukes » Wed, 1. Jul 20, 22:36

Without looking at the script, infinite stations seems a reasonable outcome of your change. "Split needs another hull parts station, make a Split hull parts station, Split has enough hull parts stations" -> "Split needs another hull parts station, make an Argon hull parts station, Split needs another hull parts station, make an Argon hull parts station, ...".

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 23:03

SirNukes wrote:
Wed, 1. Jul 20, 22:36
Without looking at the script, infinite stations seems a reasonable outcome of your change. "Split needs another hull parts station, make a Split hull parts station, Split has enough hull parts stations" -> "Split needs another hull parts station, make an Argon hull parts station, Split needs another hull parts station, make an Argon hull parts station, ...".
Maybe I am not being clear. I did not change anything in the vanilla script except for the faction ownership. It is literally a copy and paste of the already existing vanilla script in the game. The only thing I did was change the owner.

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 23:06

Here is the vanilla script I am talking about so if anyone can look through it.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<mdscript name="FactionGoal_Hold_Space" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="md.xsd">
  <cues>
    <!--Add goal to global variable-->
    <cue name="RegisterGoal">
      <actions>
        <do_if value="not global.$FactionGoals?">
          <create_list name="global.$FactionGoals"/>
        </do_if>
        <set_value name="Start.$Name" exact="'Hold Space'"/>
        <append_to_list name="global.$FactionGoals" exact="table[$EvaluationCue = Evaluate, $EvaluateEventLib = null, $TriggerCue = Start]"/>
      </actions>
    </cue>

    <library name="Evaluate">
      <actions>
        <do_all exact="$ClaimedSectors.count" counter="$Eval_Counter1">
          <set_value name="$EvaluationTarget" exact="$ClaimedSectors.{$Eval_Counter1}"/>
          <debug_text text="'%1 evaluating %2 for hold space.'.[$Faction, $EvaluationTarget.knownname]" chance="0"/>
          <set_value name="$Valid" exact="true"/>
          <!--Check if we are already holding the sector-->
          <do_all exact="$Goals.count" counter="$Eval_Counter2">
            <do_if value="$Goals.{$Eval_Counter2}.static == md.FactionGoal_Hold_Space.Start">
              <do_if value="$Goals.{$Eval_Counter2}.$Target == $EvaluationTarget">
                <debug_text text="'%1 evaluating %2 for hold target. Target already taken care of, discarding.'.[$Faction, $EvaluationTarget.knownname]" chance="0"/>
                <set_value name="$Valid" exact="false"/>
                <break/>
              </do_if>
            </do_if>
          </do_all>
          <do_if value="$Valid">
            <set_value name="$MatchingIntel" exact="null"/>
            <do_all exact="$Intel.count" counter="$Intel_Counter">
              <do_if value="$Intel.{$Intel_Counter}.{1} == $EvaluationTarget">
                <set_value name="$MatchingIntel" exact="$Intel.{$Intel_Counter}"/>
                <break/>
              </do_if>
            </do_all>

            <do_if value="$MatchingIntel">
              <set_value name="$ReconData" exact="[$MatchingIntel.{2}, $MatchingIntel.{3}]"/>
              <debug_text text="'Evaluation: %1 has prior intel on %2. Recon_NumObjects: %3 Last updated: %4'.[$Faction, $EvaluationTarget.knownname, $ReconData.{1}, $ReconData.{2}]" chance="$DebugChance2"/>
            </do_if>
            <do_else>
              <set_value name="$ReconData" exact="[10, 0]"/>
              <debug_text text="'Evaluation: %1 has no intel on %2. Recon_NumObjects: %3 Last updated: %4'.[$Faction, $EvaluationTarget.knownname, $ReconData.{1}, $ReconData.{2}]" chance="$DebugChance2"/>
            </do_else>

            <append_to_list name="$PriorityGoals" exact="table[
                                  $FactionCue = namespace,
                                  $Faction = $Faction,
                                  $TriggerCue = global.$FactionGoals.{$i}.$TriggerCue,
                                  $Target = $EvaluationTarget,
                                  $ReconData = $ReconData,
                                  $DebugChance = $DebugChance,
                                  $DebugChance2 = $DebugChance2]"/>

            <remove_value name="$MatchingIntel"/>
            <remove_value name="$ReconData"/>

            <debug_text text="'Set hold target for %1 as a priority target for %2.'.[$EvaluationTarget.knownname, $Faction]" chance="$DebugChance"/>
          </do_if>
        </do_all>
      </actions>
    </library>

    <!--Instance the goal by passing a list.
    This cue may be signalled with other parameters to trigger child cues e.g. event.param.{1} == 'generate_report' - See Process_Signal cue-->
    <cue name="Start" instantiate="true" namespace="this" version="2">
      <conditions>
        <event_cue_signalled/>
        <check_value value="this.state == cuestate.waiting and typeof event.param == datatype.table"/>
      </conditions>
      <actions>
        <!--Params-->
        <set_value name="$FactionCue" exact="event.param.$FactionCue"/>
        <set_value name="$Faction" exact="event.param.$Faction"/>
        <set_value name="$Target" exact="event.param.$Target" comment="Target sector"/>
        <assert value="$Target.isclass.sector" text="$DebugText + 'Target is not a sector, not currently supported [Owen]'"/>

        <!-- Recon_NumObjects is the number of hostile object "points" this faction thinks there are in the target space. If they have no clue, they assume there will be 10.
             Recon_LastUpdate is when Recon_NumObjects was last updated. If working on an unverified assumption, this is 0.
             IntelCurrent is set if the last time the faction got an update regarding Recon_NumObjects in the target space is less than an hour ago.
             IntelUsed is set if Recon_NumObjects was already used to reset WantedShips.

             If any new intel comes in via a mission, NewIntel is set in EvaluateEvent. This resets IntelUsed so that Faction will revise WantedShips. -->
        <set_value name="$Recon_NumObjects" exact="event.param.$ReconData.{1}" comment="threat-level"/>
        <set_value name="$Recon_LastUpdate" exact="event.param.$ReconData.{2}"/>

        <!--***Debug***-->
        <set_value name="$DebugChance" exact="@event.param.$DebugChance"/>
        <set_value name="$DebugChance2" exact="@event.param.$DebugChance2"/>

        <set_value name="$DebugText" exact="'(' + $Faction.id + ') ' + static.$Name + ': ' + $Target.knownname + '(' + $Target.owner.id + ') - '"/>
        <debug_text text="$DebugText" chance="$DebugChance"/>

        <!--***Initialise***-->
        <set_value name="$Mood" exact="null"/>
        <set_value name="$StateEvaluationDelay" exact="1ms"/>
        <set_value name="$CurrentPhaseSubGoals" exact="[]"/>

        <!--***Goal States***-->
        <set_value name="$UnclaimTime" exact="-1s" comment="Time at which the faction no longer has a claim on the sector"/>

        <!--***Invasions***-->
        <set_value name="$Invasions" exact="[]"/>
        <!--Find existing invasion goals of enemy factions to this sector-->
        <set_value name="$FactionManagerKeys" exact="global.$FactionManagers.keys.list"/>
        <do_all exact="$FactionManagerKeys.count" counter="$i">
          <do_if value="global.$FactionManagers.{$FactionManagerKeys.{$i}}.$Faction.hasrelation.enemy.{$Faction}">
            <set_value name="$EnemyGoals" exact="global.$FactionManagers.{$FactionManagerKeys.{$i}}.$Goals"/>
            <do_all exact="$EnemyGoals.count" counter="$k">
              <do_if value="$EnemyGoals.{$k}.static == md.FactionGoal_Invade_Space.Start">
                <do_if value="$EnemyGoals.{$k}.$Target == $Target">
                  <debug_text text="$DebugText + 'Faction ' + $EnemyGoals.{$k}.$Faction + ' is already invading ' + $Target.knownname" context="false" chance="$DebugChance"/>
                  <append_to_list name="$Invasions" exact="$EnemyGoals.{$k}"/>
                </do_if>
              </do_if>
            </do_all>
            <remove_value name="$EnemyGoals"/>
          </do_if>
        </do_all>
        <remove_value name="$FactionManagerKeys"/>

        <!--Standby Ships
        Ships already commandeered by the factiongoal but not currently used in a subgoal-->
        <create_group groupname="$StandbyShips"/>
        <!--Key = Ship, Value = StandbyStartTime-->
        <set_value name="$StandbyShipTable" exact="table[]"/>

        <set_value name="$ForceStrength" exact="0"/>

        <!--TODO @Owen move to library to allow updating after initialisation-->
        <!--Find sectors which may be a threat-->
        <find_cluster_in_range name="$LocalClusters" object="$Target" maxdistance="2" multiple="true"/>
        <set_value name="$LocalSectors" exact="[]"/>
        <do_all exact="$LocalClusters.count" counter="$i">
          <find_sector name="$LocalSectors" space="$LocalClusters.{$i}" multiple="true" append="true">
            <!--<match_relation_of faction="$Faction" relation="neutral" comparison="ge"/>-->
          </find_sector>
        </do_all>
        <remove_value name="$LocalClusters"/>

        <!--TODO @Owen better check that entry points are actually in control of this faction before idling there-->
        <create_group groupname="$EntryPoints"/>
        <create_group groupname="$EnemyEntryPoints"/>
        <find_cluster_in_range name="$AdjacentClusters" object="$Target" maxdistance="1" multiple="true"/>
        <set_value name="$AdjacentSectors" exact="[]"/>
        <do_all exact="$AdjacentClusters.count" counter="$i">
          <find_sector name="$AdjacentSectors" space="$AdjacentClusters.{$i}" multiple="true" append="true"/>
        </do_all>
        <remove_value name="$AdjacentClusters"/>
        <set_value name="$LocalEntryPoints" exact="[]"/>
        <set_value name="$TargetCluster" exact="$Target.cluster"/>
        <do_all exact="$AdjacentSectors.count" counter="$i">
          <do_if value="$AdjacentSectors.{$i}.cluster == $TargetCluster">
            <find_object name="$LocalEntryPoints" class="class.highwayentrygate" space="$AdjacentSectors.{$i}" destination="$Target" multiple="true"/>
          </do_if>
          <do_else>
            <find_gate name="$LocalEntryPoints" destination="$Target" space="$AdjacentSectors.{$i}" multiple="true"/>
          </do_else>
          <set_value name="this.$IsEnemy" exact="$AdjacentSectors.{$i}.hasrelation.enemy.{$Faction}"/>
          <do_all exact="$LocalEntryPoints.count" counter="$k">
            <add_to_group groupname="$EntryPoints" object="$LocalEntryPoints.{$k}.exit"/>
            <do_if value="this.$IsEnemy">
              <add_to_group groupname="$EnemyEntryPoints" object="$LocalEntryPoints.{$k}.exit"/>
            </do_if>
          </do_all>
        </do_all>
        <remove_value name="$TargetCluster"/>
        <remove_value name="$LocalEntryPoints"/>
      </actions>
      <patch sinceversion="2" state="complete">
        <remove_value name="$Phase"/>
        <remove_value name="$NewPhase"/>
        <remove_value name="$PrepareStagingAreaStrength"/>
        <set_value name="$UnclaimTime" exact="-1s" comment="Time at which the faction no longer has a claim on the sector"/>
      </patch>
      <cues>
        <cue name="Init">
          <actions>
            <do_if value="@$FeedbackValue">
              <!--Something happened during setup which requires this goal to be aborted-->
              <signal_cue_instantly cue="Cleanup"/>
            </do_if>
            <do_else>
              <!--Register this goal with the faction logic-->
              <append_to_list name="$FactionCue.$Goals" exact="namespace"/>
            </do_else>
          </actions>
        </cue>

        <!--event.param.{1} == Signal ID
        Other elements are custom-->
        <cue name="Process_Signal" instantiate="true">
          <conditions>
            <event_cue_signalled cue="Start"/>
          </conditions>
          <actions>
            <do_if value="event.param.{1} == 'evaluate_event'">
              <signal_cue_instantly cue="Evaluate_Event" param="event.param.{2}"/>
            </do_if>
            <do_elseif value="event.param.{1} == 'add_subgoal'">
              <signal_cue_instantly cue="Add_Subgoal" param="event.param.{2}"/>
            </do_elseif>
            <do_elseif value="event.param.{1} == 'remove_subgoal'">
              <signal_cue_instantly cue="Remove_Subgoal" param="event.param.{2}"/>
            </do_elseif>
            <do_elseif value="event.param.{1} == 'reactivate_subgoal'">
              <signal_cue_instantly cue="Reactivate_Subgoal" param="event.param.{2}"/>
            </do_elseif>
            <do_elseif value="event.param.{1} == 'add_standby_ship'">
              <!--event.param = ['add_standby_ship', Ship, FeedbackList (Element 1 set to true if ship added as standby ship)-->
              <signal_cue_instantly cue="Add_Standby_Ship" param="event.param" comment="Pass all parameters"/>
            </do_elseif>
            <do_elseif value="event.param.{1} == 'remove_standby_ship'">
              <!--event.param = ['remove_standby_ship', Ship, Uncommandeer? (bool)-->
              <signal_cue_instantly cue="Remove_Standby_Ship" param="event.param"/>
            </do_elseif>
            <do_elseif value="event.param.{1} == 'generate_report'">
              <signal_cue_instantly cue="Generate_Report"/>
            </do_elseif>
            <do_else>
              <assert value="false" text="'Signal ' + event.param + ' was not recognised [Owen]\n' + $DebugText" break="1"/>
            </do_else>
          </actions>
        </cue>

        <!--Signal cues-->
        <cue name="Evaluate_Event" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <assert value="typeof event.param == datatype.list and event.param.count ge 2" text="'Invalid event parameters' + event.param + ' [Owen]\n' + $DebugText" break="1"/>
            <set_value name="$EventParams" exact="event.param"/>
            <do_if value="$EventParams.{2} == 'recon update'">
              <!--Faction is holding this area-->
              <do_if value="$Target == $EventParams.{4} or $EventParams.{4}.hascontext.{$Target}">
                <do_if value="typeof $EventParams.{5} == datatype.list and (typeof $EventParams.{5}.{1}).isnumeric">
                  <set_value name="$Recon_NumObjects" exact="$EventParams.{5}.{1}" comment="threat-level"/>
                  <set_value name="$Recon_LastUpdate" exact="$EventParams.{5}.{2}"/>
                  <set_value name="$NewIntel"/>
                  <debug_text text="'New Intel received by %1 at %3: \n OpFor numbers updated: %2'.[$Faction, $Recon_NumObjects, $Recon_LastUpdate]" chance="$DebugChance2"/>

                  <!--TODO @Owen check the recon processing for holding space. Check if the below comment is still relevant-->
                  <!-- $Intel == [[$TargetSpace, $Recon_NumObjects, $Recon_LastUpdate], etc]
                        to do: if/when invasions actually take ownership of space:
                                  if invasion is successful, wipe this entry from $Intel,
                                  if invasion is not successful, update with what the invasion actually finds there?

                          Owen mentioned NPC ships reconnoitering. That could be used to update this as well if/when it gets in. -->
                  <set_value name="$Intel" exact="$FactionCue.$Intel"/>
                  <do_all exact="$Intel.count + 1" counter="$Intel_Counter">
                    <do_if value="$Intel_Counter gt $Intel.count">
                      <append_to_list name="$Intel" exact="[$Target, $EventParams.{5}.{1}, $EventParams.{5}.{2}]"/>
                      <debug_text text="'%1 has no prior intel on %2. Updating: OpFor: %3 Last Updated: %4'.[$Faction, $Intel.{$Intel.count}.{1}.knownname, $Intel.{$Intel.count}.{2}, $Intel.{$Intel.count}.{3}]" chance="$DebugChance2"/>
                    </do_if>
                    <do_elseif value="$Intel.{$Intel_Counter}.{1}" exact="$Target">
                      <set_value name="$Intel.{$Intel_Counter}.{2}" exact="$EventParams.{5}.{1}"/>
                      <set_value name="$Intel.{$Intel_Counter}.{3}" exact="$EventParams.{5}.{2}"/>
                      <debug_text text="'%1 updating intel on %2. Updating: OpFor: %3 Last Updated: %4'.[$Faction, $Intel.{$Intel_Counter}.{1}.knownname, $Intel.{$Intel_Counter}.{2}, $Intel.{$Intel_Counter}.{3}]" chance="$DebugChance2"/>
                      <break/>
                    </do_elseif>
                  </do_all>
                  <remove_value name="$Intel"/>
                </do_if>
                <do_else>
                  <debug_text text="'Unexpected recon update passed to this goal. ' + $EventParams.{5}" filter="error"/>
                </do_else>
                <break/>
              </do_if>
            </do_if>
            <remove_value name="$EventParams"/>
          </actions>
        </cue>

        <cue name="Generate_Report" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <debug_text text="'Hold Space - ' + $Faction.knownname + ' is set to hold Space: ' + $Target.knownname + ' (' + $Target.owner + ')'" context="false"/>
            <debug_text text="'Has ' + $CurrentPhaseSubGoals.count + ' subgoals'" context="false"/>
            <do_all exact="$CurrentPhaseSubGoals.count" counter="$i">
              <do_if value="$CurrentPhaseSubGoals.{$i}.$GenerateReportCue?">
                <signal_cue_instantly cue="$CurrentPhaseSubGoals.{$i}.$GenerateReportCue"/>
              </do_if>
              <do_else>
                <debug_text text="'SUBGOAL - ' + $CurrentPhaseSubGoals.{$i}.$Cue" context="false"/>
              </do_else>
            </do_all>
          </actions>
        </cue>

        <!--event.param = Subgoal registry table-->
        <cue name="Add_Subgoal" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <!--TODO - flag subgoal as being related to something e.g. an invasion?-->
            <assert value="typeof event.param == datatype.table" text="'parameter ' + event.param + ' was not a table [Owen]'" break="1"/>
            <set_value name="this.$IDX" exact="$CurrentPhaseSubGoals.indexof.{event.param}"/>
            <do_if value="this.$IDX">
              <assert value="not $CurrentPhaseSubGoals.indexof.{event.param}" text="'Subgoal ' + event.param + ' was already in $CurrentPhaseSubGoals [Owen]'" break="1"/>
            </do_if>
            <do_else>
              <append_to_list name="$CurrentPhaseSubGoals" exact="event.param"/>
              <debug_text text="$DebugText + 'Adding subgoal ' + event.param.$Cue.$DebugText + ' - now has ' + $CurrentPhaseSubGoals.count + ' subgoals'" context="false" chance="$DebugChance"/>
            </do_else>
          </actions>
        </cue>

        <!--event.param = Subgoal registry table-->
        <cue name="Remove_Subgoal" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <assert value="typeof event.param == datatype.table" text="'parameter ' + event.param + ' was not a table [Owen]'" break="1"/>
            <assert value="$CurrentPhaseSubGoals.indexof.{event.param}" text="'Subgoal ' + event.param + ' is not a $CurrentPhaseSubGoals where it is expected to be [Owen]'" break="1"/>
            <set_value name="this.$IDX" exact="$CurrentPhaseSubGoals.indexof.{event.param}"/>
            <do_if value="this.$IDX">
              <debug_text text="$DebugText + ' Removing subgoal: ' + event.param + ' from $CurrentPhaseSubGoals'" chance="$DebugChance"/>
              <remove_value name="$CurrentPhaseSubGoals.{this.$IDX}"/>
            </do_if>
          </actions>
        </cue>

        <!--event.param = Subgoal registry table-->
        <cue name="Reactivate_Subgoal" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>

          </actions>
        </cue>

        <!--
        event.param.{1} = signal ID 'add_standby_ship' (optional)
        event.param.{2} = Ship
        event.param.{3} = Result feedback list ref. Element 1 set to true if ship added as standby ship-->
        <cue name="Add_Standby_Ship" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <set_value name="this.$Ship" exact="event.param.{2}"/>
            <do_if value="typeof this.$Ship == datatype.component and this.$Ship.isclass.ship">
              <assert value="this.$Ship.iscommandeered" text="'Standby ship ' + this.$Ship + ' ' + this.$Ship.knownname + ' is not commandeered [Owen]'"/>
              <debug_text text="$DebugText + 'Adding ship ' + this.$Ship.knownname + ' ' + this.$Ship + ' as a standby ship. $StandbyShips: ' + $StandbyShips.count + ' $StandbyShipTable: ' + $StandbyShipTable.keys.count" context="false" chance="$DebugChance"/>

              <assert value="not $StandbyShips.indexof.{this.$Ship}" text="'Object ' + this.$Ship + ' ' + this.$Ship.knownname + ' is already in group $StandbyShips [Owen]\n' + $DebugText" break="1"/>
              <add_to_group groupname="$StandbyShips" object="this.$Ship"/>

              <assert value="not $StandbyShipTable.{this.$Ship}?" text="'Object ' + this.$Ship + ' ' + this.$Ship.knownname + ' is already in $StandbyShipTable [Owen]\n' + $DebugText" break="1"/>
              <set_value name="$StandbyShipTable.{this.$Ship}" exact="player.age"/>

              <assert value="$StandbyShips.count == $StandbyShipTable.keys.count" text="'Counts of $StandbyShips and $StandbyShipTable do not match [Owen]\n' + $DebugText" break="1"/>
              <set_value name="event.param.{3}.{1}" exact="true"/>
            </do_if>
            <do_else>
              <assert value="typeof this.$Ship == datatype.component" text="'Invalid event parameters' + this.$Ship + ' [Owen]\n' + $DebugText" break="1"/>
            </do_else>
          </actions>
        </cue>

        <!--
        event.param.{1} = signal ID 'remove_standby_ship' (optional)
        event.param.{2} = Ship
        event.param.{3} = Uncommandeer?-->
        <cue name="Remove_Standby_Ship" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <set_value name="this.$Ship" exact="event.param.{2}"/>
            <do_if value="typeof this.$Ship == datatype.component">
              <debug_text text="$DebugText + 'Removing ship ' + this.$Ship.knownname + ' ' + this.$Ship + ' as a standby ship. $StandbyShips: ' + $StandbyShips.count + ' $StandbyShipTable: ' + $StandbyShipTable.keys.count" context="false" chance="$DebugChance"/>

              <do_if value="$StandbyShips.indexof.{this.$Ship}">
                <remove_from_group group="$StandbyShips" object="this.$Ship"/>
              </do_if>
              <do_else>
                <assert value="$StandbyShips.indexof.{this.$Ship}" text="'Object ' + this.$Ship + ' ' + this.$Ship.knownname + ' is not in group $StandbyShips [Owen]\n' + $DebugText" break="1"/>
              </do_else>
              <do_if value="$StandbyShipTable.{this.$Ship}?">
                <remove_value name="$StandbyShipTable.{this.$Ship}"/>
              </do_if>
              <do_else>
                <assert value="$StandbyShipTable.{this.$Ship}?" text="'Object ' + this.$Ship + ' ' + this.$Ship.knownname + ' is not in $StandbyShipTable [Owen]\n' + $DebugText" break="1"/>
              </do_else>

              <assert value="$StandbyShips.count == $StandbyShipTable.keys.count" text="'Counts of $StandbyShips and $StandbyShipTable do not match [Owen]\n' + $DebugText" break="1"/>
              <do_if value="event.param.{3}">
                <release_commandeered_object object="this.$Ship"/>
              </do_if>
            </do_if>
            <do_else>
              <assert value="typeof this.$Ship == datatype.component" text="'Invalid event parameters' + this.$Ship + ' [Owen]\n' + $DebugText" break="1"/>
            </do_else>
          </actions>
        </cue>

        <cue name="Standby_Ship_Lost" instantiate="true">
          <conditions>
            <check_any>
              <event_object_destroyed group="$StandbyShips"/>
              <event_object_changed_owner group="$StandbyShips"/>
              <event_object_changed_true_owner group="$StandbyShips"/>
            </check_any>
          </conditions>
          <actions>
            <debug_text text="$DebugText + 'Standby ship ' + event.param.knownname + ' ' + event.param + ' lost due to ' + event.name + ' $StandbyShips: ' + $StandbyShips.count + ' $StandbyShipTable: ' + $StandbyShipTable.keys.count" context="false" chance="$DebugChance"/>
            <!-- TODO @Ownen: please review this // is it correct that the ship is removed from the list upon each triggered owner change event (i.e. even if it "changed" to the faction for which standby ships were added?) -->
            <do_if value="event.name == 'event_object_changed_true_owner' or event.name == 'event_object_changed_owner'">
              <remove_from_group group="$StandbyShips" object="event.object"/>
            </do_if>

            <do_if value="$StandbyShipTable.{event.object}?">
              <remove_value name="$StandbyShipTable.{event.object}"/>
            </do_if>
            <do_else>
              <assert value="$StandbyShipTable.{event.object}?" text="'Object ' + event.object + ' ' + event.object.knownname + ' is not in $StandbyShipTable [Owen]\n' + $DebugText" break="1"/>
            </do_else>
          </actions>
        </cue>

        <!--An invasion has started against this sector. Keep track of the invasion faction goal cue. Invader may be non-enemy.-->
        <cue name="Sector_Faction_Logic_Goal_Signal" instantiate="true">
          <conditions>
            <event_object_signalled object="$Target"/>
            <check_value value="event.param == 'faction_logic_goal_event'"/>
          </conditions>
          <actions>
            <do_if value="typeof event.param2 == datatype.cue">
              <do_if value="event.param2.static == md.FactionGoal_Invade_Space.Start">
                <debug_text text="$DebugText + 'Sector is being invaded by ' + event.param2.$Faction + ' which are in phase ' + event.param2.$Phase" context="false" chance="$DebugChance"/>
                <do_if value="not $Invasions.indexof.{event.param2}">
                  <append_to_list name="$Invasions" exact="event.param2"/>
                </do_if>
                <do_if value="true" chance="5">
                  <!--Small chance to react immediatly-->
                  <!--TODO @Owen base chance on recon of invasion source sector?-->
                  <set_value name="$StateEvaluationDelay" exact="1s"/>
                  <reset_cue cue="EvaluateState"/>
                </do_if>
              </do_if>
            </do_if>
          </actions>
        </cue>

        <!-- returns $ForceStrength -->
        <library name="EvaluateForceStrength">
          <actions>
            <!--TODO @Owen base strength on other factors.-->
            <set_value name="$ForceStrength" exact="0"/>

            <do_all exact="$CurrentPhaseSubGoals.count" counter="$i">
              <set_value name="$CurrentSubGoal" exact="$CurrentPhaseSubGoals.{$i}"/>

              <!--Current Recon Subgoal-->
              <do_if value="$CurrentSubGoal.$Cue.$Descriptor == '$SUBGOAL_Recon'">
                <do_if value="$CurrentSubGoal.$EvaluateShipsCue?">
                  <signal_cue_instantly cue="$CurrentSubGoal.$EvaluateShipStrengthCue"/>
                  <set_value name="$ForceStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                </do_if>
              </do_if>

              <!--Current Defend Area Subgoal-->
              <do_elseif value="$CurrentSubGoal.$Cue.$Descriptor == '$SUBGOAL_DefendArea'">
                <do_if value="$CurrentSubGoal.$EvaluateShipsCue?">
                  <signal_cue_instantly cue="$CurrentSubGoal.$EvaluateShipStrengthCue"/>
                  <set_value name="$ForceStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                </do_if>
              </do_elseif>

              <!--Current Build Defence Station Subgoal - Do nothing-->
            </do_all>
          </actions>
        </library>

        <cue name="EvaluateState">
          <delay exact="$StateEvaluationDelay"/>
          <actions>
            <set_value name="$StateEvaluationDelay" exact="0s"/>
            <set_value name="$ForceStrength" exact="0"/>

            <do_if value="@$NewIntel">
              <remove_value name="$IntelUsed"/>
              <remove_value name="$NewIntel"/>
            </do_if>

            <!--Release any commandeered standby ships which have not been requested by a subgoal-->
            <do_all exact="$StandbyShips.count" counter="$i" reverse="true">
              <do_if value="player.age gt $StandbyShipTable.{$StandbyShips.{$i}} + 2min">
                <signal_cue_instantly cue="Remove_Standby_Ship" param="[null, $StandbyShips.{$i}, true]"/>
              </do_if>
            </do_all>

            <do_all exact="$Invasions.count" counter="$i" reverse="true">
              <do_if value="not $Invasions.{$i}.exists">
                <remove_value name="$Invasions.{$i}"/>
              </do_if>
            </do_all>

            <do_if value="$Faction.isactive">
              <set_value name="this.$EnemyContestingFactions" exact="[]"/>
              <set_value name="this.$ContestingFactions" exact="$Target.contestingfactions"/>
              <set_value name="this.$HasClaim" exact="if this.$ContestingFactions.count then this.$ContestingFactions.indexof.{$Faction} else $Target.owner == $Faction"/>
              <do_all exact="this.$ContestingFactions.count" counter="$i">
                <do_if value="$Faction.hasrelation.enemy.{this.$ContestingFactions.{$i}}">
                  <append_to_list name="this.$EnemyContestingFactions" exact="this.$ContestingFactions.{$i}"/>
                </do_if>
              </do_all>
              <do_if value="this.$HasClaim">
                <set_value name="$UnclaimTime" exact="-1"/>
              </do_if>
              <do_elseif value="$UnclaimTime lt 0">
                <set_value name="$UnclaimTime" exact="player.age"/>
                <debug_text text="$DebugText + ' Sector no longer is claimed by the faction. Aborting hold space goal.'" chance="$DebugChance"/>
              </do_elseif>
            </do_if>

            <do_if value="not $Faction.isactive">
              <debug_text text="$DebugText + ' Faction is no longer active. Aborting hold space goal.'" chance="$DebugChance"/>
              <signal_cue_instantly cue="Cleanup"/>
            </do_if>
            <do_elseif value="$UnclaimTime gt 0 and player.age gt $UnclaimTime + 10min">
              <debug_text text="$DebugText + ' Sector has been unclaimed for too long. Aborting hold space goal.'" chance="$DebugChance"/>
              <signal_cue_instantly cue="Cleanup"/>
            </do_elseif>
            <do_else>
              <!--Evaluate all current goals, checking their states and seeing if any should begin handing off and ending.-->
              <set_value name="$CurrentGoalStates" exact="table[]"/>
              <set_value name="$CurrentSubGoalDescriptor" exact="null"/>
              <set_value name="$PrepareStagingAreaStrength" exact="0"/>
              <set_value name="$ActiveDefenceStationGoals" exact="0"/>
              <do_all exact="$CurrentPhaseSubGoals.count" counter="$i" reverse="true">
                <set_value name="$CurrentSubGoal" exact="$CurrentPhaseSubGoals.{$i}"/>
                <do_if value="$CurrentSubGoal.$EvaluateCue?">
                  <signal_cue_instantly cue="$CurrentSubGoal.$EvaluateCue"/>
                  <do_if value="$CurrentSubGoal.$EvaluationResult?">
                    <set_value name="$CurrentSubGoalDescriptor" exact="$CurrentSubGoal.$Cue.$Descriptor"/>
                    <set_value name="$Handoff" exact="false"/>
                    <do_if value="not $CurrentGoalStates.{$CurrentSubGoalDescriptor}?">
                      <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}" exact="table[]"/>
                    </do_if>
                    <do_if value="$CurrentSubGoalDescriptor == '$SUBGOAL_Recon'">
                      <do_if value="$CurrentSubGoal.$EvaluationResult == 'failed'">
                        <set_value name="$Handoff" exact="true"/>
                      </do_if>
                      <do_else>
                        <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$DesiredShipStrength" operation="add" exact="$CurrentSubGoal.$DesiredShipStrength"/>
                        <do_if value="$CurrentSubGoal.$EvaluationResult == 'performing'">
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$ReadyShipStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                        </do_if>
                        <do_else>
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$UnreadyShipStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                        </do_else>
                      </do_else>
                    </do_if>
                    <do_elseif value="$CurrentSubGoalDescriptor == '$SUBGOAL_DefendArea'">
                      <do_if value="$CurrentSubGoal.$EvaluationResult == 'failed' or $CurrentSubGoal.$EvaluationResult == 'ending'">
                        <set_value name="$Handoff" exact="true"/>
                      </do_if>
                      <do_else>
                        <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$DesiredShipStrength" operation="add" exact="$CurrentSubGoal.$DesiredShipStrength"/>
                        <do_if value="$CurrentSubGoal.$EvaluationResult == 'performing'">
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$ReadyShipStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                        </do_if>
                        <do_else>
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$UnreadyShipStrength" operation="add" exact="$CurrentSubGoal.$ShipStrength"/>
                        </do_else>
                        <!--Count how many subgoals are in reaction to an invasion or a contesting faction-->
                        <do_if value="$CurrentSubGoal.$DefendArea.$Invasion? and $Invasions.indexof.{$CurrentSubGoal.$DefendArea.$Invasion}">
                          <do_if value="not $CurrentGoalStates.{$CurrentSubGoalDescriptor}.$Invasions?">
                            <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$Invasions" exact="table[]"/>
                          </do_if>
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$Invasions.{$CurrentSubGoal.$DefendArea.$Invasion}" operation="add"/>
                        </do_if>
                        <do_elseif value="$CurrentSubGoal.$DefendArea.$ContestingFaction? and this.$ContestingFactions.indexof.{$CurrentSubGoal.$DefendArea.$ContestingFaction}">
                          <do_if value="not $CurrentGoalStates.{$CurrentSubGoalDescriptor}.$ContestingFactions?">
                            <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$ContestingFactions" exact="table[]"/>
                          </do_if>
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$ContestingFactions.{$CurrentSubGoal.$DefendArea.$ContestingFaction}" operation="add"/>
                        </do_elseif>
                        <do_if value="@$CurrentSubGoal.$DefendArea.$TargetObject.isrealclass.station">
                          <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$StationTargetCount" operation="add"/>
                        </do_if>
                      </do_else>
                    </do_elseif>
                    <do_elseif value="$CurrentSubGoalDescriptor == '$SUBGOAL_BuildDefenceStation'">
                      <!--TODO @Owen-->
                      <do_if value="$CurrentSubGoal.$EvaluationResult == 'failed'">
                        <set_value name="$Handoff" exact="true"/>
                      </do_if>
                      <do_elseif value="$CurrentSubGoal.$EvaluationResult == 'achieved'">
                        <set_value name="$Handoff" exact="true"/>
                      </do_elseif>
                      <do_else>
                        <set_value name="$ActiveDefenceStationGoals" operation="add"/>
                      </do_else>
                    </do_elseif>
                    <do_else>
                      <assert value="false" text="'Faction Subgoal ' + [$CurrentSubGoal.$Cue] +' was not recognised for this Faction Goal [Owen]\n' + $DebugText"/>
                    </do_else>
                    <do_if value="$Handoff">
                      <signal_cue_instantly cue="$CurrentPhaseSubGoals.{$i}.$UpdateSubgoalCue" param="['handoff']"/>
                    </do_if>
                    <do_else>
                      <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.$Counter" operation="add"/>
                      <set_value name="$CurrentGoalStates.{$CurrentSubGoalDescriptor}.{'$' + $CurrentSubGoal.$EvaluationResult}" operation="add"/>
                    </do_else>
                  </do_if>
                </do_if>
              </do_all>
              <remove_value name="$CurrentSubGoalDescriptor"/>

              <!--Set base wanted strength based on the current aggression-->
              <set_value name="this.$MaxDefenceSubGoals" exact="5"/>
              <set_value name="this.$MaxAttackEnemyStationSubGoals" exact="1" comment="Also using Defend Area subgoal"/>
              <set_value name="$Mood" exact="$Faction.mood.aggression"/>
              <do_if value="$Mood == moodlevel.verylow">
                <set_value name="$MinimumStrengthFactor" exact="1.2f"/>
                <set_value name="$MaximumStrengthFactor" exact="1.3f"/>
              </do_if>
              <do_elseif value="$Mood == moodlevel.low">
                <set_value name="$MinimumStrengthFactor" exact="1.1f"/>
                <set_value name="$MaximumStrengthFactor" exact="1.3f"/>
              </do_elseif>
              <do_elseif value="$Mood == moodlevel.normal">
                <set_value name="$MinimumStrengthFactor" exact="1.0f"/>
                <set_value name="$MaximumStrengthFactor" exact="1.4f"/>
              </do_elseif>
              <do_elseif value="$Mood == moodlevel.high">
                <set_value name="$MinimumStrengthFactor" exact="0.9f"/>
                <set_value name="$MaximumStrengthFactor" exact="1.8f"/>
                <set_value name="this.$MaxDefenceSubGoals" exact="7"/>
                <set_value name="this.$MaxAttackEnemyStationSubGoals" exact="2" comment="Also using Defend Area subgoal"/>
              </do_elseif>
              <do_elseif value="$Mood == moodlevel.veryhigh">
                <set_value name="$MinimumStrengthFactor" exact="0.8f"/>
                <set_value name="$MaximumStrengthFactor" exact="2.0f"/>
                <set_value name="this.$MaxDefenceSubGoals" exact="9"/>
                <set_value name="this.$MaxAttackEnemyStationSubGoals" exact="2" comment="Also using Defend Area subgoal"/>
              </do_elseif>
              <remove_value name="$Mood"/>

              <!--TODO @Owen check what $Recon_NumObjects actually is (a threat-level) -->
              <!--<set_value name="$MinimumStrength" exact="[$MinimumStrength, $MinimumStrengthFactor * $Recon_NumObjects].max"/>
              <set_value name="$MaximumStrength" exact="[$MinimumStrength, $MaximumStrengthFactor * $Recon_NumObjects].max"/>-->
              <set_value name="$IntelUsed"/>

              <!--Check if new subgoals should be requested for the current situation-->

              <set_value name="this.$PotentialDefendAreaDefinitions" exact="[]"/>

              <!--Invasions
              Invasions can trigger the following subgoals:
              - DefendArea, around the entry point if possible (if enemy)
              - DefendArea, around important stations (if enemy)
              - Recon, into enemy space-->

              <do_all exact="$Invasions.count" counter="$i">

                <!--Defend Area sub goals against invasions. Limit number per invasion.-->
                <!--First, check if there are already enough active DefendArea goals for this invasion-->
                <set_value name="this.$WantedDefendAreaSubgoals" exact="3 - @$CurrentGoalStates.$SUBGOAL_DefendArea.$Invasions.{$Invasions.{$i}}"/>
                <do_if value="this.$WantedDefendAreaSubgoals gt 0">
                  <debug_text text="$DebugText + ' Wants to react to invasion ' + [$Invasions.{$i}] + ' with ' + this.$WantedDefendAreaSubgoals + ' defend area goals'" context="false" chance="$DebugChance"/>
                  <set_value name="this.$HasExistingInvasionDefenceSubgoal" exact="@$CurrentGoalStates.$SUBGOAL_DefendArea.$Invasions.{$Invasions.{$i}} != 0"/>
                  <!--First, check if there are entry points to be guarded. These take priority.-->
                  <do_all exact="$Invasions.{$i}.$AlliedEntryPoints.count" counter="$k">
                    <assert value="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit.sector == $Target" text="'Invasion exit point for enemy faction leads to sector ' + $Invasions.{$i}.$AlliedEntryPoints.{$k}.exit.sector.knownname + ' instead of ' + $Target.knownname + ' [Owen]'"/>
                    <do_if value="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit.sector == $Target">
                      <create_position name="this.$DefendPos" object="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit" space="$Target"/>
                      <set_value name="this.$AlreadyDefendingEntryPoint" exact="false"/>
                      <do_all exact="$CurrentPhaseSubGoals.count" counter="$j">
                        <set_value name="this.$CurrentSubGoal" exact="$CurrentPhaseSubGoals.{$j}"/>
                        <do_if value="this.$CurrentSubGoal.$Cue.$Descriptor == '$SUBGOAL_DefendArea'">
                          <signal_cue_instantly cue="this.$CurrentSubGoal.$IsDefendingPositionCue" param="[this, $Target, this.$DefendPos, false]"/>
                          <!--Existing defend area subgoal is close to this position. Leave it to do its job.-->
                          <set_value name="this.$AlreadyDefendingEntryPoint" exact="true"/>
                          <debug_text text="$DebugText + ' Exiting defendArea subgoal has is already guarding entry point'" context="false" chance="$DebugChance2"/>
                        </do_if>
                      </do_all>
                      <do_if value="not this.$AlreadyDefendingEntryPoint">
                        <debug_text text="$DebugText + ' Will attempt to defend entrypoint: ' + $Invasions.{$i}.$AlliedEntryPoints.{$k}.exit +  ' ' + $Invasions.{$i}.$AlliedEntryPoints.{$k}.exit.knownname" chance="$DebugChance2"/>
                        <!--<debug_text text="'pos ' + this.$DefendPos"/>
                          <debug_text text="'$CurrentPhaseSubGoals.count ' + $CurrentPhaseSubGoals.count"/>-->

                        <!--Check if it's safe to go to the defend area position immediatly, or if the ships should gather at a staging area-->
                        <find_ship_by_true_owner name="this.$OwnShips" faction="$Faction" space="$Target" multiple="true">
                          <match_distance object="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit" max="20km"/>
                        </find_ship_by_true_owner>
                        <find_ship_by_true_owner name="this.$EnemyShips" faction="$Invasions.{$i}.$Faction" space="$Target" multiple="true">
                          <match_distance object="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit" max="20km"/>
                        </find_ship_by_true_owner>
                        <signal_cue_instantly cue="md.FactionLogic.EvaluateForceStrength" param="[this, this.$OwnShips, md.$DefaultShipStrengthTable, md.$DefaultSubordinateStrengthTable]"/>
                        <set_value name="this.$OwnShipsStrength" exact="this.$EFS_Result"/>
                        <signal_cue_instantly cue="md.FactionLogic.EvaluateForceStrength" param="[this, this.$EnemyShips, md.$DefaultShipStrengthTable, md.$DefaultSubordinateStrengthTable]"/>
                        <set_value name="this.$EnemyShipsStrength" exact="this.$EFS_Result"/>
                        <remove_value name="this.$EFS_Result"/>
                        <set_value name="this.$StagingAreaDefinition" exact="null"/>
                        <do_if value="this.$OwnShipsStrength lt this.$EnemyShipsStrength">
                          <!--More enemies at the entry point than own ships. Gather ships at safe staging area-->
                          <set_value name="this.$StagingAreaDefinition" exact="table[]"/>
                          <!--Try to find any allied station within range-->
                          <find_station name="this.$AlliedStation" space="$Target">
                            <match_relation_to faction="$Faction" relation="neutral" comparison="ge"/>
                            <match_distance object="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit" max="50km"/>
                          </find_station>
                          <do_if value="this.$AlliedStation">
                            <create_position name="this.$StagingAreaPos" object="this.$AlliedStation" space="$Target"/>
                            <set_value name="this.$StagingAreaDefinition.$Sector"     exact="$Target"/>
                            <set_value name="this.$StagingAreaDefinition.$Position"   exact="this.$StagingAreaPos"/>
                            <set_value name="this.$StagingAreaDefinition.$Range"      exact="20km"/>
                          </do_if>
                          <do_else>
                            <!--TODO @Owen - positioning. Currently a position pushed from the entry point-->
                            <create_position name="this.$StagingAreaPos" object="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit" z="50km" max="5km" space="$Invasions.{$i}.$AlliedEntryPoints.{$k}.exit.sector"/>
                            <set_value name="this.$StagingAreaDefinition.$Sector"     exact="$Target"/>
                            <set_value name="this.$StagingAreaDefinition.$Position"   exact="this.$StagingAreaPos"/>
                            <set_value name="this.$StagingAreaDefinition.$Range"      exact="25km"/>
                          </do_else>
                        </do_if>
                        <!--Entry point is in need of defence. Add defintion to the list to process after this loop-->
                        <set_value name="this.$DefenceDefinition"                   exact="table[]"/>
                        <!--If this is the first defence subgoal, request a larger force-->
                        <do_if value="this.$HasExistingInvasionDefenceSubgoal">
                          <set_value name="this.$WantedDefenceStrength" exact="md.$DefaultShipStrengthTable.{class.ship_m} * 4"/>
                          <set_value name="this.$DefenceDefinition.$Strength" min="this.$WantedDefenceStrength" max="this.$WantedDefenceStrength + md.$DefaultShipStrengthTable.{class.ship_l}"/>
                        </do_if>
                        <do_else>
                          <set_value name="this.$WantedDefenceStrength" exact="md.$DefaultShipStrengthTable.{class.ship_xl}"/>
                          <set_value name="this.$DefenceDefinition.$Strength" min="this.$WantedDefenceStrength" max="this.$WantedDefenceStrength + (md.$DefaultShipStrengthTable.{class.ship_m} * 2)"/>
                          <set_value name="this.$HasExistingInvasionDefenceSubgoal" exact="true"/>
                        </do_else>
                        <set_value name="this.$DefenceDefinition.$RequestStrengthAllowance" min="this.$DefenceDefinition.$Strength / 2" max="this.$DefenceDefinition.$Strength" profile="decreasing" comment="Chance of all ships to be requested to be built"/>
                        <set_value name="this.$DefenceDefinition.$StagingArea"      exact="this.$StagingAreaDefinition"/>
                        <set_value name="this.$AreaDefintion"                       exact="table[]"/>
                        <set_value name="this.$AreaDefintion.$Sector"               exact="$Target"/>
                        <set_value name="this.$AreaDefintion.$Position"             exact="this.$DefendPos"/>
                        <set_value name="this.$AreaDefintion.$Range"                exact="25km"/>
                        <set_value name="this.$AreaDefintion.$Invasion"             exact="$Invasions.{$i}"/>
                        <set_value name="this.$DefenceDefinition.$DefendArea"       exact="this.$AreaDefintion"/>
                        <append_to_list name="this.$PotentialDefendAreaDefinitions" exact="this.$DefenceDefinition"/>
                        <set_value name="this.$WantedDefendAreaSubgoals" operation="subtract"/>
                      </do_if>
                      <remove_value name="this.$AlreadyDefendingEntryPoint"/>
                    </do_if>
                    <do_if value="this.$WantedDefendAreaSubgoals le 0">
                      <break/>
                    </do_if>
                  </do_all>
                  <do_all exact="this.$WantedDefendAreaSubgoals">
                    <!--Find non-entry point places to defend-->
                    <!--TODO @Owen better selection of areas-->
                    <find_station name="this.$AlliedStation" space="$Target">
                      <match_relation_to faction="$Faction" relation="neutral" comparison="ge"/>
                    </find_station>
                    <do_if value="this.$AlliedStation">
                      <set_value name="this.$DefenceDefinition"                   exact="table[]"/>
                      <!--If this is the first defence subgoal, request a larger force-->
                      <do_if value="this.$HasExistingInvasionDefenceSubgoal">
                        <set_value name="this.$WantedDefenceStrength" exact="md.$DefaultShipStrengthTable.{class.ship_m} * 4"/>
                        <set_value name="this.$DefenceDefinition.$Strength" min="this.$WantedDefenceStrength" max="this.$WantedDefenceStrength + md.$DefaultShipStrengthTable.{class.ship_l}"/>
                      </do_if>
                      <do_else>
                        <set_value name="this.$WantedDefenceStrength" exact="md.$DefaultShipStrengthTable.{class.ship_xl}"/>
                        <set_value name="this.$DefenceDefinition.$Strength" min="this.$WantedDefenceStrength" max="this.$WantedDefenceStrength + (md.$DefaultShipStrengthTable.{class.ship_m} * 2)"/>
                        <set_value name="this.$HasExistingInvasionDefenceSubgoal" exact="true"/>
                      </do_else>
                      <set_value name="this.$DefenceDefinition.$RequestStrengthAllowance" min="this.$DefenceDefinition.$Strength / 2" max="this.$DefenceDefinition.$Strength" profile="decreasing" comment="Chance of all ships to be requested to be built"/>
                      <set_value name="this.$DefenceDefinition.$StagingArea"      exact="null"/>
                      <set_value name="this.$AreaDefintion"                       exact="table[]"/>
                      <set_value name="this.$AreaDefintion.$Sector"               exact="$Target"/>
                      <create_position name="this.$AreaDefintion.$Position" object="this.$AlliedStation" max="3km" space="$Target"/>
                      <set_value name="this.$AreaDefintion.$Range"                exact="15km"/>
                      <set_value name="this.$AreaDefintion.$Invasion"             exact="$Invasions.{$i}"/>
                      <set_value name="this.$DefenceDefinition.$DefendArea"       exact="this.$AreaDefintion"/>
                      <append_to_list name="this.$PotentialDefendAreaDefinitions" exact="this.$DefenceDefinition"/>
                      <set_value name="this.$WantedDefendAreaSubgoals" operation="subtract"/>
                    </do_if>
                    <do_else>
                      <!--TODO @Owen fallback to a position instead of a station-->
                      <break/>
                    </do_else>
                  </do_all>
                </do_if>
              </do_all>


Need to split it into two, so will post the next part below

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 23:06

Second part of the code.

Code: Select all

 <!--Contesting factions
              Contesting factions can trigger the following subgoals:
              - DefendArea, around enemy stations (essentially an attack group)
              - DefendArea, around important stations (if enemy)-->

              <set_value name="this.$AllowedNumDefenceSubGoals" exact="this.$MaxDefenceSubGoals - @$CurrentGoalStates.$SUBGOAL_DefendArea.$Counter"/>

              <!--Defend Area sub goals-->
              <set_value name="this.$AllowedAttackEnemyStationSubgoals" exact="[this.$AllowedNumDefenceSubGoals, this.$MaxAttackEnemyStationSubGoals - @$CurrentGoalStates.$SUBGOAL_DefendArea.$StationTargetCount].min"/>
              <do_if value="this.$AllowedAttackEnemyStationSubgoals gt 0">
                <!--TODO @Owen - check sensor ranges / recon?-->
                <find_station name="this.$EnemyStations" space="$Target" multiple="true">
                  <match_relation_to faction="$Faction" relation="kill" comparison="le"/>
                </find_station>
                <do_if value="this.$EnemyStations.count">
                  <!--Build up ships to attack an enemy station, with the staging area around an allied station-->
                  <find_station_by_true_owner name="this.$AlliedStation" faction="$Faction" space="$Target" checkoperational="false">
                    <match state="componentstate.wreck" negate="true"/>
                  </find_station_by_true_owner>
                  <do_if value="not this.$AlliedStation">
                    <find_station name="this.$AlliedStation" space="$Target">
                      <match_relation_to faction="$Faction" relation="neutral" comparison="ge"/>
                    </find_station>
                  </do_if>
                  <do_if value="this.$AlliedStation">
                    <!--Find nearest enemy station-->
                    <set_value name="this.$NearestStation" exact="null"/>
                    <set_value name="this.$NearestDistance" exact="null"/>
                    <do_all exact="this.$EnemyStations.count" counter="$station_i">
                      <do_if value="not this.$NearestStation">
                        <set_value name="this.$NearestStation" exact="this.$EnemyStations.{$station_i}"/>
                        <set_value name="this.$NearestDistance" exact="this.$AlliedStation.distanceto.{this.$EnemyStations.{$station_i}}"/>
                      </do_if>
                      <do_else>
                        <set_value name="this.$TempDist" exact="this.$AlliedStation.distanceto.{this.$EnemyStations.{$station_i}}"/>
                        <do_if value="this.$TempDist lt this.$NearestDistance">
                          <set_value name="this.$NearestStation" exact="this.$EnemyStations.{$station_i}"/>
                          <set_value name="this.$NearestDistance" exact="this.$TempDist"/>
                        </do_if>
                      </do_else>
                    </do_all>
                    <do_if value="this.$NearestStation">
                      <debug_text text="'Staging area station ' + this.$AlliedStation + ' ' + this.$AlliedStation.knownname + ' target: ' + this.$NearestStation + ' ' + this.$NearestStation.knownname" chance="$DebugChance"/>
                      <set_value name="this.$DefenceDefinition"                   exact="table[]"/>
                      <set_value name="this.$WantedDefenceStrength"               exact="md.$DefaultShipStrengthTable.{class.ship_xl}"/>
                      <set_value name="this.$DefenceDefinition.$Strength"         min="this.$WantedDefenceStrength" max="this.$WantedDefenceStrength + (md.$DefaultShipStrengthTable.{class.ship_m} * 2)"/>
                      <set_value name="this.$HasExistingInvasionDefenceSubgoal"   exact="true"/>
                      <set_value name="this.$DefenceDefinition.$RequestStrengthAllowance" min="this.$DefenceDefinition.$Strength / 2" max="this.$DefenceDefinition.$Strength" profile="decreasing" comment="Chance of all ships to be requested to be built"/>
                      <set_value name="this.$DefenceDefinition.$StagingArea"      exact="table[]"/>
                      <set_value name="this.$DefenceDefinition.$StagingArea.$Sector"  exact="$Target"/>
                      <create_position name="this.$DefenceDefinition.$StagingArea.$Position" object="this.$AlliedStation" max="3km" space="$Target"/>
                      <set_value name="this.$DefenceDefinition.$StagingArea.$Range"   exact="15km"/>
                      <set_value name="this.$AreaDefintion"                       exact="table[]"/>
                      <set_value name="this.$AreaDefintion.$Sector"               exact="$Target"/>
                      <set_value name="this.$AreaDefintion.$TargetObject"         exact="this.$NearestStation"/>
                      <create_position name="this.$AreaDefintion.$Position" object="this.$NearestStation" max="3km" space="$Target"/>
                      <set_value name="this.$AreaDefintion.$Range"                exact="15km"/>
                      <set_value name="this.$DefenceDefinition.$DefendArea"       exact="this.$AreaDefintion"/>
                      <append_to_list name="this.$PotentialDefendAreaDefinitions" exact="this.$DefenceDefinition"/>
                    </do_if>
                  </do_if>
                </do_if>
              </do_if>

              <shuffle_list list="this.$PotentialDefendAreaDefinitions"/>
              <do_all exact="[this.$AllowedNumDefenceSubGoals, this.$PotentialDefendAreaDefinitions.count].min" counter="$i">
                <debug_text text="$DebugText + 'Decided to defend area: ' + this.$PotentialDefendAreaDefinitions.{$i}.$DefendArea" chance="$DebugChance"/>
                <set_value name="$NewSubgoalTable" exact="table[]"/>
                <set_value name="$NewSubgoalTable.$OwnerGoal"                 exact="namespace"/>
                <set_value name="$NewSubgoalTable.$Descriptor"                exact="'$SUBGOAL_DefendArea'"/>
                <set_value name="$NewSubgoalTable.$PrepareEndTime"            exact="player.age + 20min"/>
                <set_value name="$NewSubgoalTable.$DesiredEndTime"            exact="player.age + 45min"/>
                <set_value name="$NewSubgoalTable.$OwnerStandbyShipsTable"    exact="$StandbyShipTable"/>
                <set_value name="$NewSubgoalTable.$ShipStrengthTable"         exact="md.$DefaultShipStrengthTable.clone"/>
                <set_value name="$NewSubgoalTable.$SubordinateStrengthTable"  exact="md.$DefaultSubordinateStrengthTable.clone"/>
                <set_value name="$NewSubgoalTable.$DesiredShipStrength"       exact="this.$PotentialDefendAreaDefinitions.{$i}.$Strength"/>
                <set_value name="$NewSubgoalTable.$RequestStrengthAllowance"  exact="this.$PotentialDefendAreaDefinitions.{$i}.$RequestStrengthAllowance"/>
                <!--TODO @Owen find scope of search area for commandeering ships-->
                <set_value name="$NewSubgoalTable.$FindShipSpaces"            exact="$LocalSectors.clone"/>

                <set_value name="$NewSubgoalTable.$StagingArea"               exact="this.$PotentialDefendAreaDefinitions.{$i}.$StagingArea"/>
                <set_value name="$NewSubgoalTable.$DefendArea"                exact="this.$PotentialDefendAreaDefinitions.{$i}.$DefendArea"/>

                <set_value name="$NewSubgoalTable.$DebugChance"               exact="$DebugChance"/>
                <set_value name="$NewSubgoalTable.$DebugChance2"              exact="$DebugChance2"/>

                <signal_cue_instantly cue="md.FactionSubgoal_DefendArea.Start" param="$NewSubgoalTable"/>
              </do_all>

              <!--Start with the minimum number of each subgoal-->
              <set_value name="$DesiredSubgoals" exact="table[]"/>

              <!--How many of each goal is desired?-->
              <!--TODO @Owen if recon data is very out of date, increase the number of desired recon subgoals-->
              <set_value name="$DesiredSubgoals.$SUBGOAL_Recon" exact="2 - @$CurrentGoalStates.$SUBGOAL_Recon.$Counter"/>

              <!--TODO @Owen Better decide how many and what stations should be built-->
              <do_if value="$Invasions.count">

                <!-- count how many defense-stations exist, to avoid creating too many-->
                <find_station_by_true_owner name="this.$DefenceStations" faction="$Faction" planneddefencestation="true" plannedshipyard="false" plannedwharf="false" plannedequipmentdock="false" plannedtradestation="false" space="$Target" checkoperational="false" multiple="true"/>

                <!--<debug_text text="'Number of defence stations in ' + $Target + ' ' + $Target.knownname + ' for ' + $Faction + ' - ' + this.$DefenceStations.count" chance="100"/>-->
                <do_if value="(this.$DefenceStations.count + @$CurrentGoalStates.$SUBGOAL_BuildDefenceStation.$Counter) le 1">
                  <set_value name="$DesiredSubgoals.$SUBGOAL_BuildDefenceStation" exact="2 - (this.$DefenceStations.count + @$CurrentGoalStates.$SUBGOAL_BuildDefenceStation.$Counter)"/>
                </do_if>
              </do_if>

              <set_value name="$DesiredSubgoalsKeys" exact="$DesiredSubgoals.keys.list"/>
              <do_all exact="$DesiredSubgoalsKeys.count" counter="$i">
                <do_if value="$DesiredSubgoals.{$DesiredSubgoalsKeys.{$i}} gt 0">
                  <do_all exact="$DesiredSubgoals.{$DesiredSubgoalsKeys.{$i}}">

                    <do_if value="$DesiredSubgoalsKeys.{$i} == '$SUBGOAL_Recon'">
                      <!--Trigger new Recon Subgoal-->
                      <find_zone name="$HomeZone" normalzone="true" space="$Target" />
                      <find_zone name="$ReconTarget" normalzone="true" space="$Target" />

                      <do_if value="$HomeZone and $ReconTarget">
                        <set_value name="$NewSubgoalTable" exact="table[]"/>
                        <set_value name="$NewSubgoalTable.$OwnerGoal"                 exact="namespace"/>
                        <set_value name="$NewSubgoalTable.$Descriptor"                exact="$DesiredSubgoalsKeys.{$i}"/>
                        <set_value name="$NewSubgoalTable.$ShipStrengthTable"         exact="md.$DefaultShipStrengthTable.clone"/>
                        <set_value name="$NewSubgoalTable.$DesiredEndTime"            exact="player.age + 1h"/>
                        <set_value name="$NewSubgoalTable.$SubordinateStrengthTable"  exact="md.$DefaultSubordinateStrengthTable.clone"/>
                        <set_value name="$NewSubgoalTable.$DesiredShipStrength"       exact="md.$DefaultShipStrengthTable.{class.ship_s} * 2"/>
                        <!--TODO @Owen find scope of search area for commandeering ships-->
                        <set_value name="$NewSubgoalTable.$FindShipSpaces"            exact="$LocalSectors.clone"/>
                        <set_value name="$NewSubgoalTable.$Target"                    exact="$ReconTarget"/>
                        <set_value name="$NewSubgoalTable.$Home"                      exact="$HomeZone"/>
                        <set_value name="$NewSubgoalTable.$DebugChance"               exact="$DebugChance"/>
                        <set_value name="$NewSubgoalTable.$DebugChance2"              exact="$DebugChance2"/>

                        <signal_cue_instantly cue="md.FactionSubgoal_Recon.Start" param="$NewSubgoalTable"/>
                      </do_if>
                    </do_if>

                    <do_elseif value="$DesiredSubgoalsKeys.{$i} == '$SUBGOAL_BuildDefenceStation'">
                      <!--TODO @Owen better selection-->
                      <set_value name="$StationTag" exact="tag.defence"/>
                      <set_value name="$StationType" exact="'defence'"/>
                      <get_construction_plan result="$ConstructionPlan" faction="$Faction" tags="$StationTag" rawname="$StationRawName"/>
                      <!--TODO @Owen - this is creating construction plans with no upgrades.-->
                      <do_if value="$ConstructionPlan">
                        <get_module_set_macro result="$StationMacro" race="$Faction.primaryrace" type="$StationType"/>
                        <do_if value="not $StationMacro and ($Faction.primaryrace == race.argon or $Faction.primaryrace == race.paranid or $Faction.primaryrace == race.teladi or $Faction.primaryrace == race.split)">
                          <set_value name="$StationMacro" exact="macro.station_gen_factory_base_01_macro"/>
                        </do_if>
                        <do_if value="$StationMacro">
                          <!--Trigger new BuildStation Subgoal-->
                          <set_value name="$NewSubgoalTable" exact="table[]"/>
                          <set_value name="$NewSubgoalTable.$OwnerGoal"                 exact="namespace"/>
                          <set_value name="$NewSubgoalTable.$Descriptor"                exact="$DesiredSubgoalsKeys.{$i}"/>
                          <set_value name="$NewSubgoalTable.$DesiredEndTime"            exact="null"/>
                          <set_value name="$NewSubgoalTable.$StationMacro"              exact="$StationMacro"/>
                          <set_value name="$NewSubgoalTable.$ConstructionPlan"          exact="$ConstructionPlan"/>
                          <set_value name="$NewSubgoalTable.$StationRawName"            exact="$StationRawName"/>
                          <set_value name="$NewSubgoalTable.$DeployImmediate"           exact="true"/>
                          <!--TODO @Owen find scope of search area for commandeering ships-->
                          <set_value name="$NewSubgoalTable.$FindShipSpaces"            exact="$LocalSectors.clone"/>

                          <set_value name="$NewSubgoalTable.$Target"                    exact="$Target"/>
                          <!--TODO @Owen better radius based on plot size-->
                          <!--TODO @Owen better positioning-->
                          <get_safe_pos result="$NewSubgoalTable.$Position"             sector="$NewSubgoalTable.$Target" radius="5km" object="$NewSubgoalTable.$Target" max="100km" allowyaxis="false" comment="position of new to build station"/>
                          <set_value name="$NewSubgoalTable.$DebugChance"               exact="$DebugChance"/>
                          <set_value name="$NewSubgoalTable.$DebugChance2"              exact="$DebugChance2"/>

                          <signal_cue_instantly cue="md.FactionSubgoal_BuildStation.Start" param="$NewSubgoalTable"/>
                          <set_value name="$ActiveDefenceStationGoals" operation="add"/>
                        </do_if>
                        <do_else>
                          <debug_text text="$DebugText + 'Unable to find suitable station macro for ' + $Faction.primaryrace + ' ' + $StationType" context="false" chance="$DebugChance2"/>
                        </do_else>
                        <remove_value name="$StationMacro"/>
                      </do_if>
                      <do_else>
                        <debug_text text="$DebugText + 'Unable to find suitable station macro for ' + $Faction + ' ' + $StationTag" context="false" chance="$DebugChance2"/>
                      </do_else>
                      <remove_value name="$StationTag"/>
                      <remove_value name="$StationType"/>
                      <remove_value name="$ConstructionPlan"/>
                    </do_elseif>
                  </do_all>
                </do_if>
              </do_all>

              <!--If there are no active goals to build a defense station, see if any factories can be built in an low population sector-->
              <do_if value="$ActiveDefenceStationGoals == 0 and player.age gt namespace.time + 1h">
                <set_value name="$HightechFactories" exact="0"/>
                <set_value name="$OtherFactories" exact="0"/>

                <get_ware_definition result="$HightechWares" faction="$Faction" group="'hightech'"/>

                <set_value name="$WantedHightechFactories" exact="if $HightechWares.count then 2 else 0"/>
                <set_value name="$WantedOtherFactories" exact="2"/>
                <!--Amount of existing factories at which to no longer consider adding new ones-->
                <set_value name="$ExistingFactoryThreshold" exact="4"/>

                <find_station_by_true_owner name="$PotentialFactories" faction="$Faction" space="$Target" multiple="true" checkoperational="false">
                  <!-- TODO @Owen match planned module products or .plannedproducts
                  <match_products/>-->
                </find_station_by_true_owner>
                <do_for_each name="$PotentialFactory" in="$PotentialFactories">
                  <set_value name="$ConstructionPlan" exact="$PotentialFactory.plannedconstruction.sequence"/>
                  <do_if value="not @$ConstructionPlan.count">
                    <!--There is an factory without a construction sequence. Assume that it will receive one and do not attempt to build new productions in this iteration.-->
                    <set_value name="$WantedHightechFactories" exact="0"/>
                    <set_value name="$WantedOtherFactories" exact="0"/>
                    <break/>
                  </do_if>

                  <set_value name="$IsFactory" exact="false"/>
                  <set_value name="$IsHighTechFactory" exact="false"/>
                  <do_all exact="$ConstructionPlan.count" counter="$plan_i">
                    <do_if value="$ConstructionPlan.{$plan_i}.macro.isclass.production">
                      <set_value name="$IsFactory" exact="true"/>

                      <do_if value="$HightechWares.count">
                        <set_value name="$Products" exact="$ConstructionPlan.{$plan_i}.macro.products.list"/>
                        <do_for_each name="$Product" in="$Products">
                          <do_if value="$HightechWares.indexof.{$Product}">
                            <set_value name="$IsHighTechFactory" exact="true"/>
                            <break/>
                          </do_if>
                        </do_for_each>
                      </do_if>

                      <do_if value="$IsHighTechFactory">
                        <break/>
                      </do_if>
                    </do_if>
                  </do_all>

                  <do_if value="$IsHighTechFactory">
                    <set_value name="$HightechFactories" operation="add"/>
                  </do_if>
                  <do_elseif value="$IsFactory">
                    <set_value name="$OtherFactories" operation="add"/>
                  </do_elseif>

                  <!--Check if there are already enough detected factories so we can early out-->
                  <do_if value="$HightechFactories ge $WantedHightechFactories and $OtherFactories ge $WantedOtherFactories">
                    <break/>
                  </do_if>
                </do_for_each>

                <set_value name="$SectorCentre" exact="$Target.coreposition"/>
                <set_value name="$SectorCoreSize" exact="$Target.coresize / 2"/>

                <do_if value="$WantedHightechFactories gt $HightechFactories and ($OtherFactories + $HightechFactories) lt $ExistingFactoryThreshold">
                  <do_all exact="$WantedHightechFactories - $HightechFactories">
                    <set_value name="$Yaw" min="0deg" max="360deg"/>
                    <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>

                    <shuffle_list list="$HightechWares"/>
                    <do_for_each name="$HightechWare" in="$HightechWares">
                      <get_module_definition reference="$ResultRef" faction="$Faction" ware="$HightechWare"/>
                      <do_if value="$ResultRef">
                        <set_value name="$Yaw" min="0deg" max="360deg"/>
                        <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>
                        <set_value name="$PlacementDist" min="0km" max="$SectorCoreSize" profile="bell" scale="2"/>
                        <create_factory name="$Station" modules="$Modules" sector="$Target" race="$Faction.primaryrace" owner="$Faction">
                          <compatibilities>
                            <limits production="2"/>
                          </compatibilities>
                          <select ware="$HightechWare" race="$Faction.primaryrace"/>
                          <safepos x="$SectorCentre.x + sin($Yaw) * $PlacementDist" y="$Y" z="$SectorCentre.z + cos($Yaw) * $PlacementDist" includeplotbox="true"/>
                        </create_factory>
                        <do_if value="$Station">
                          <debug_text filter="economy_verbose" text="'#FLE#;%1;SeedHighTechFactory;%2;%3;%4;%5;%6;%7'.[player.age, $Station.knownname, $Station, $Station.idcode, $Target.knownname, $Faction.id, $HightechWare, '', '']" context="false"/>
                          <debug_text text="$DebugText + 'Building high tech factory ' + $Station + ' for ware ' + $HightechWare" context="false" chance="$DebugChance"/>
                          <signal_cue_instantly cue="md.FinaliseStations.NewStation_GenerateFactory_Signal" param="table[
                                                $station = $Station,
                                                $plannedmodules = $Modules,
                                                $moduleset = $Station.modulesets.{1},
                                                $debugoutput = if $DebugChance == 100 then true else false
                                                ]"/>
                        </do_if>
                        <break/>
                      </do_if>
                    </do_for_each>
                  </do_all>
                </do_if>

                <do_if value="$WantedOtherFactories gt $OtherFactories">
                  <!--Grab some low tech wares to produce-->
                  <get_ware_definition result="$LowtechWares" faction="$Faction" group="'energy'"/>
                  <!--TODO @Owen append?-->
                  <get_ware_definition result="$RefinedWares" faction="$Faction" group="'refined'"/>
                  <append_list_elements name="$LowtechWares" other="$RefinedWares"/>
                  <!--TODO @Owen @Michael check if this is a good place for refinery type wares-->
                  <do_if value="$LowtechWares.count">
                    <do_all exact="$WantedOtherFactories - $OtherFactories">
                      <set_value name="$Yaw" min="0deg" max="360deg"/>
                      <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>

                      <shuffle_list list="$LowtechWares"/>
                      <do_for_each name="$LowtechWare" in="$LowtechWares">
                        <get_module_definition reference="$ResultRef" faction="$Faction" ware="$LowtechWare"/>
                        <do_if value="$ResultRef">
                          <set_value name="$Yaw" min="0deg" max="360deg"/>
                          <set_value name="$Y" min="$SectorCentre.y - 10km" max="$SectorCentre.y + 10km"/>
                          <set_value name="$PlacementDist" min="0km" max="$SectorCoreSize" profile="bell" scale="2"/>
                          <create_factory name="$Station" modules="$Modules" sector="$Target" race="$Faction.primaryrace" owner="$Faction">
                            <compatibilities>
                              <limits production="2"/>
                            </compatibilities>
                            <select ware="$LowtechWare" race="$Faction.primaryrace"/>
                            <safepos x="$SectorCentre.x + sin($Yaw) * $PlacementDist" y="$Y" z="$SectorCentre.z + cos($Yaw) * $PlacementDist" includeplotbox="true"/>
                          </create_factory>
                          <do_if value="$Station">
                            <debug_text filter="economy_verbose" text="'#FLE#;%1;SeedLowTechFactory;%2;%3;%4;%5;%6;%7'.[player.age, $Station.knownname, $Station, $Station.idcode, $Target.knownname, $Faction.id, $LowtechWare, '', '']" context="false"/>
                            <debug_text text="$DebugText + 'Building low tech factory ' + $Station + ' for ware ' + $LowtechWare" context="false" chance="$DebugChance"/>
                            <signal_cue_instantly cue="md.FinaliseStations.NewStation_GenerateFactory_Signal" param="table[
                                                $station = $Station,
                                                $plannedmodules = $Modules,
                                                $moduleset = $Station.modulesets.{1},
                                                $debugoutput = if $DebugChance == 100 then true else false
                                                ]"/>
                          </do_if>
                          <break/>
                        </do_if>
                      </do_for_each>
                    </do_all>
                  </do_if>
                </do_if>
                <remove_value name="$SectorCentre"/>
                <remove_value name="$SectorCoreSize"/>

              </do_if>

              <remove_value name="$DesiredSubgoals"/>
              <remove_value name="$NewSubgoalTable"/>

              <set_value name="$StateEvaluationDelay" min="10s" max="15s"/>
              <reset_cue cue="EvaluateState"/>
            </do_else>
          </actions>
        </cue>

        <!--TODO @Owen - update end cue-->
        <cue name="Cleanup">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <!--<delay min="10s" max="1min"/>-->
          <actions>
            <debug_text text="$DebugText + 'Ending'" context="false" chance="$DebugChance"/>

            <substitute_text source="'' + [namespace]" text="$DebugGoalText">
              <replace string="'['" with="''"/>
              <replace string="']'" with="''"/>
            </substitute_text>
            <!--curtime, output_id, goal_id, subgoal_id, object_name, object_idcode, sector_name, faction_id, target_owner_id-->
            <debug_text filter="economy_verbose" text="'#FL#;%1;%2;%3;%4;%5;%6;%7;%8;%9'.[player.age, 'cleanup', $DebugGoalText, null, null, null, $Target.knownname, $Faction.id, $Target.owner.id]" context="false"/>

            <!--<do_if value="$FeedbackValue == -1">
              <debug_text text="$DebugText + 'Feedback: ' + $FeedbackValue + ' - Unable to find entry point.'" context="false" chance="$DebugChance"/>
            </do_if>-->
            <do_all exact="$StandbyShips.count" counter="$i">
              <debug_text text="$DebugText + 'Releaseing Standby ships'" context="false" chance="if $i == 1 then $DebugChance else 0"/>
              <debug_text text="$DebugText + '%1 resuming prior orders.'.[$StandbyShips.{$i}.knownname]" context="false" chance="$DebugChance"/>
              <release_commandeered_object object="$StandbyShips.{$i}"/>
            </do_all>

            <debug_text text="$DebugText + 'Ended'" context="false" chance="$DebugChance"/>
            <set_value name="$IDX" exact="$FactionCue.$Goals.indexof.{namespace}"/>
            <do_if value="$IDX">
              <remove_value name="$FactionCue.$Goals.{$IDX}"/>
              <!--TODO @Owen signal the faction logic? Store the result for future decisions?-->
              <!--<signal_cue_instantly cue="md.FactionLogic.FactionSignalled" param="[$Faction, namespace]"/>-->
            </do_if>
            <cancel_cue cue="Start"/>
          </actions>
        </cue>
      </cues>
    </cue>

  </cues>
</mdscript>

You can do a search for create_factory to see the exact part of it.

SirNukes
Posts: 546
Joined: Sat, 31. Mar 07, 23:44
x4

Re: Need someone to take a look at this script please

Post by SirNukes » Wed, 1. Jul 20, 23:34

It is still unclear on your reasoning behind expecting your owner edit to be stable. Why would Split trying to build enough Split stations in a Split sector stop just because there are a million Argon (or whatever modded faction) stations already present, when they don't care about Argon stations? Did you change the find_station_by_true_owner logic to included the modded in faction?

I'm guess what you want is: use a table lookup to match a primary faction (and maybe ware) to a wanted subfaction to own the station, and ensure $PotentialFactories includes all such subfactions before deciding if more factories should be built.

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Wed, 1. Jul 20, 23:50

SirNukes wrote:
Wed, 1. Jul 20, 23:34
It is still unclear on your reasoning behind expecting your owner edit to be stable. Why would Split trying to build enough Split stations in a Split sector stop just because there are a million Argon (or whatever modded faction) stations already present, when they don't care about Argon stations? Did you change the find_station_by_true_owner logic to included the modded in faction?

I'm guess what you want is: use a table lookup to match a primary faction (and maybe ware) to a wanted subfaction to own the station, and ensure $PotentialFactories includes all such subfactions before deciding if more factories should be built.
I see what you are saying now and somehow missed that, thanks for the help. I am face palming myself right now lol.

Buzz2005
Posts: 2202
Joined: Sat, 26. Feb 05, 01:47
x4

Re: Need someone to take a look at this script please

Post by Buzz2005 » Thu, 2. Jul 20, 19:23

I would recommend Blackrain to go to Discord ego channel, all this q's would be answered very quickly as a lot of modders talk there
Fixed ships getting spawned away from ship configuration menu at resupply ships from automatically getting deployables.

BlackRain
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 7411
Joined: Mon, 15. Dec 03, 18:53
x4

Re: Need someone to take a look at this script please

Post by BlackRain » Thu, 2. Jul 20, 19:36

Buzz2005 wrote:
Thu, 2. Jul 20, 19:23
I would recommend Blackrain to go to Discord ego channel, all this q's would be answered very quickly as a lot of modders talk there
Someone told me about it already, but thanks.

Post Reply

Return to “X4: Foundations - Scripts and Modding”