XRWS           òÇ   C  ò„ext_01.cat ext_01.dat md/Smalltalk.xml 62084 1592955089 0783773371feaac95f0c2db78865d117
<?xml version="1.0" encoding="utf-8"?>
<diff>
	<replace sel="/mdscript/cues">
	
	<cues>

    <cue name="SmalltalkTopicConditions" onfail="cancel">
      <conditions>
        <check_value value="player.module" exact="'ep1'" negate="true"/>
      </conditions>
      <actions>
        <!-- The following variables will be checked when selecting a topic. -->
        <!-- In a plot-enabled game, they can be set in this cue by other MD scripts. -->
        <!-- Example: <set_value name="md.Smalltalk.SmalltalkTopicConditions.$GateOpening" exact="true" /> -->
        <set_value name="$GateOpening" exact="true" comment="topic 3" />
        <set_value name="$DeVriesImmigration" exact="true" comment="topic 4" />
        <set_value name="$XenonResearch" exact="true" comment="topic 7" />
        <set_value name="$SenseOfWar" exact="true" comment="topic 9" />
        <set_value name="$DeVriesRebuild" exact="true" comment="topic 12" />
        <set_value name="$PMCAfterHack" exact="true" comment="topic 13" />
        <set_value name="$XenonInOmicron" exact="true" comment="topic 14" />
      </actions>
    </cue>

    <cue name="NPCTracker">
      <conditions>
        <event_object_changed_room object="player.entity" />
        <check_value value="player.platform" />
      </conditions>
      <actions>
        <find_object_component groupname="$npcs" object="player.platform" multiple="true" class="class.npc" missionactor="false">
          <match entitytype="entitytype.temporary" negate="true" />
          <match owner="faction.player" negate="true" />
        </find_object_component>
        <!-- Remove unwanted entries -->
        <do_all exact="$npcs.count" counter="$i" reverse="true">
          <set_value name="$npc" exact="$npcs.{$i}" />
          <do_if value="$npc.page and $npc.customhandler and not $npc.$SmalltalkDone?">
            <get_text_ids_in_range result="$topics" page="$npc.page" start="1000000" end="99 * 1000000" factor="1000000" />
            <do_if value="$topics.count">
              <!-- Found a suitable NPC with at least one smalltalk topic -->
              <set_value name="$npc.$TopicID" exact="0" />
              <set_value name="$npc.$TopicTimeout" exact="0s" />
              <continue />
            </do_if>
          </do_if>
          <remove_from_group group="$npcs" object="$npcs.{$i}" />
        </do_all>
        <set_value name="$lastspeaktime" exact="player.age" />
        <set_value name="$convcounter" exact="0" comment="Non-zero when a conversation is active" />
      </actions>

      <cues>
        <!-- Reset if player undocks from landing platform -->
        <cue name="NPCTrackerReset">
          <conditions>
            <event_object_changed_room object="player.entity" />
            <check_value value="not player.platform" />
          </conditions>
          <actions>
            <do_all exact="$npcs.count" counter="$i">
              <remove_value name="$npcs.{$i}.$TopicID" />
              <remove_value name="$npcs.{$i}.$TopicTimeout" />
            </do_all>
            <reset_cue cue="NPCTracker" />
          </actions>
        </cue>

        <cue name="OnNPCRemoved" instantiate="true">
          <conditions>
            <check_any>
              <event_object_destroyed group="$npcs" />
              <check_all>
                <event_object_changed_room group="$npcs" />
                <check_value value="event.param != player.platform" />
              </check_all>
              <event_cue_signalled />
            </check_any>
          </conditions>
          <actions>
            <signal_cue_instantly cue="CleanupNPC" param="event.object" />
          </actions>
        </cue>

        <cue name="CleanupNPC" instantiate="true">
          <conditions>
            <event_cue_signalled />
          </conditions>
          <actions>
            <!-- <debug_text text="'Removing NPC %1 (%2)'.[event.param, event.param.knownname]" /> -->
            <remove_value name="event.param.$TopicID" />
            <remove_value name="event.param.$TopicTimeout" />
            <remove_from_group group="$npcs" object="event.param" />
          </actions>
        </cue>

        <cue name="CheckConversationState" instantiate="true">
          <conditions>
            <check_any>
              <check_all>
                <event_conversation_started />
                <set_value name="$convcounter" operation="add" />
              </check_all>
              <check_all>
                <event_conversation_finished />
                <set_value name="$convcounter" operation="subtract" />
              </check_all>
            </check_any>
          </conditions>
        </cue>

        <!-- Input: $npc, $topics. Output: $id -->
        <library name="SelectTopic">
          <actions>
            <remove_value name="$id" />
            <do_any>
              <set_value name="$id" exact="1000000" weight="100 * ($topics.indexof.{1000000} != 0 and player.cluster == md.$Albion)" />
              <set_value name="$id" exact="2000000" weight="100 * ($topics.indexof.{2000000} != 0)" />
              <set_value name="$id" exact="3000000" weight="100 * ($topics.indexof.{3000000} != 0 and player.cluster == md.$Albion and @SmalltalkTopicConditions.$GateOpening)" />
              <set_value name="$id" exact="4000000" weight="100 * ($topics.indexof.{4000000} != 0 and player.cluster == md.$Omicron_Lyrae and @SmalltalkTopicConditions.$DeVriesImmigration)" />
              <set_value name="$id" exact="5000000" weight="100 * ($topics.indexof.{5000000} != 0)" />
              <set_value name="$id" exact="6000000" weight="100 * ($topics.indexof.{6000000} != 0)" />
              <set_value name="$id" exact="7000000" weight="100 * ($topics.indexof.{7000000} != 0 and player.cluster == md.$Albion and @SmalltalkTopicConditions.$XenonResearch)" />
              <set_value name="$id" exact="9000000" weight="100 * ($topics.indexof.{9000000} != 0) and @SmalltalkTopicConditions.$SenseOfWar" />
              <set_value name="$id" exact="10000000" weight="100 * ($topics.indexof.{10000000} != 0)" />
              <set_value name="$id" exact="11000000" weight="100 * ($topics.indexof.{11000000} != 0)" />
              <set_value name="$id" exact="12000000" weight="100 * ($topics.indexof.{12000000} != 0 and player.cluster == md.$DeVries and $npc.race == race.terran and @SmalltalkTopicConditions.$DeVriesRebuild)" />
              <set_value name="$id" exact="13000000" weight="100 * ($topics.indexof.{13000000} != 0 and player.cluster == md.$Albion and @SmalltalkTopicConditions.$PMCAfterHack)" />
              <set_value name="$id" exact="14000000" weight="100 * ($topics.indexof.{14000000} != 0 and player.cluster == md.$Omicron_Lyrae and @SmalltalkTopicConditions.$XenonInOmicron)" />
              <set_value name="$id" exact="15000000" weight="100 * ($topics.indexof.{15000000} != 0 and $npc.race == race.terran)" />
            </do_any>
          </actions>
        </library>

        <cue name="TrySpeak" instantiate="true" checkinterval="3s">
          <conditions>
            <check_age min="$lastspeaktime + 7s" />
            <check_value value="$npcs.count" />
            <check_value value="$convcounter == 0" />
          </conditions>
          <actions>
            <do_all exact="$npcs.count">
              <do_all chance="100 * 3s / 10s" comment="check interval / average speak interval per NPC">
                <set_value name="$npc" exact="$npcs.random" />
                <assert value="not $npc.$SmalltalkDone?" />
                <!-- <debug_text text="'NPC %1 (%2), distance=%3, topic timeout=%4'.[$npc, $npc.knownname, $npc.distanceto.{player.entity}, player.age - $npc.$TopicTimeout]" /> -->
                <do_if value="player.age ge $npc.$TopicTimeout + 8s">
                  <do_if value="not $npc.hasrelation.enemy.{faction.player}">
                    <do_if value="player.entity.distanceto.{$npc}" max="6m">
                        <!-- Found an NPC, now select a topic -->
                        <get_text_ids_in_range result="$topics" page="$npc.page" start="1000000" end="99 * 1000000" factor="1000000" />
                        <!-- Input: $npc, $topics. Output: $id -->
                        <include_actions ref="SelectTopic" />
                        <!-- <debug_text text="'NPC %1 (%2): Topics=%3, selected topic=%4'.[$npc, $npc.knownname, $topics, @$id]" /> -->
                        <do_if value="$id?">
                          <speak actor="$npc" line="$id" priority="0" />
                          <set_value name="$npc.$TopicID" exact="$id" />
                          <set_value name="$npc.$TopicTimeout" exact="player.age + 15s" />
                          <set_value name="$lastspeaktime" exact="player.age" />
                          <break />
                        </do_if>
                    </do_if>
                  </do_if>
                </do_if>
              </do_all>
              <do_if value="$lastspeaktime == player.age">
                <break />
              </do_if>
            </do_all>
          </actions>
        </cue>
      </cues>
    </cue>

    <cue name="SmalltalkSectionHandler" instantiate="true" namespace="this">
      <conditions>
        <check_any>
          <event_conversation_next_section sectionprefix="gSmalltalk_" />
          <event_conversation_returned_to_section sectionprefix="gSmalltalk_" />
        </check_any>
      </conditions>
      <actions>

        <do_if value="event.param == 'gSmalltalk_start'">
          <!-- NPC.$TopicID = NPC topic introduction text ID -->
          <set_value name="$topic" exact="event.object.$TopicID" />
          <set_value name="$basefactor" exact="100000" />
          <set_value name="$stepsleft" exact="3" />
          <debug_text text="'Starting smalltalk with topic ID ' + $topic" />
          <!-- Disable speech bubble -->
          <set_value name="event.object.$TopicTimeout" exact="player.age" />
          <!-- Ren Otani: About what you just said... (topic-specific if the ID exists, otherwise generic) -->
          <do_if value="readtext.{player.entity.page}.{$topic}?">
           <!-- <add_npc_line speaker="player.entity" line="$topic" hidechoices="true" />-->
          </do_if>
          <do_else>
          <!--  <add_npc_line speaker="player.entity" line="900000" hidechoices="true" />-->
          </do_else>
          <!-- Initial NPC response if it exists -->
          <do_if value="readtext.{event.object.page}.{$topic + 1}?">
           <!-- <add_npc_line line="$topic + 1" />-->
          </do_if>
          <add_player_choice_return text="{1002,20}" position="bottom_right" />
          <!-- Find possible base player choices (path = 1000000, choice IDs = 1*00000) -->
          <get_text_ids_in_range result="$basechoices" page="1002" start="$topic + $basefactor" end="$topic + $basefactor * 9" factor="$basefactor" />
          <do_if value="$basechoices.count lt 5">
            <resize_list list="$basechoices" count="5" />
          </do_if>
          <do_all exact="5" counter="$i">
            <do_if value="$basechoices.{$i}">
              <!-- Always show confidence bar and play minigame, no matter how many texts for Ren are available -->
              <add_player_choice text="readtext.{1002}.{$basechoices.{$i}}" section="gSmalltalk_step" choiceparam="[$basechoices.{$i}, $basefactor / 10, 0, 0, $basechoices, $i, $stepsleft - 1, null]" confidence="[1,1,1,1,2,2,2,3,3,4].random" />
            </do_if>
          </do_all>
          <do_if value="not static.$difficulty?">
            <set_value name="static.$difficulty" exact="1" />
            <set_value name="static.$passes" exact="2" />
            <set_value name="static.$speed" exact="8" />
          </do_if>
          <set_value name="static.$difficulty" min="1" max="5" />
          <setup_conversation_minigame difficulty="static.$difficulty" passes="static.$passes" speed="static.$speed * 0.1" />
        </do_if>

        <do_elseif value="event.param == 'gSmalltalk_step'">
          <!-- event.param2 == [path, followupfactor, penalty, bonus, basechoices, basechoiceidx, stepsleft, rewarddata] -->
          <set_value name="$path" exact="event.param2.{1}" />
          <set_value name="$followupfactor" exact="event.param2.{2}" />
          <set_value name="$penalty" exact="event.param2.{3}" />
          <set_value name="$bonus" exact="event.param2.{4}" />
          <set_value name="$basechoices" exact="event.param2.{5}" />
          <set_value name="$basechoiceidx" exact="event.param2.{6}" />
          <set_value name="$stepsleft" exact="event.param2.{7}" />
          <set_value name="$rewarddata" exact="event.param2.{8}" />
          <do_if value="event.name == 'event_conversation_returned_to_section'">
            <!-- Coming back from a reward selection sub-menu, display reward options again -->
            <include_actions ref="AddRewardOptions" comment="using event.object, event.param2 and $rewarddata" />
          </do_if>
          <do_else>
            <!-- event.param3 == minigame score -->
            <assert value="$path % 10 == 0" />
            <do_if value="$basechoices.{$basechoiceidx} == $path">
              <!-- Selected a base choice: Remove it from $basechoices -->
              <set_value name="$basechoices.{$basechoiceidx}" exact="null" />
            </do_if>
            <get_text_ids_in_range result="$speakids" page="player.entity.page" start="$path + 1" end="$path + 5" comment="TTXXXXX1-TTXXXXX5" />
            <do_if value="$speakids.count == 1">
              <!-- No minigame, the existing ID decides on fail or success -->
              <set_value name="$scoreid" exact="$speakids.{1} % 10" />
            </do_if>
            <do_elseif value="event.name == 'event_conversation_next_section' and typeof event.param3 != datatype.null">
              <!--
              Minigame 0 -> Fail 5
              Minigame 1 -> Bad 4
              Minigame 2 -> Bad 4
              Minigame 3 -> Medium 3
              Minigame 4 -> Good 2
              Minigame 5 -> Perfect 1
              -->
              <set_value name="$scoreid" exact="[5, 4, 4, 3, 2, 1].{event.param3 + 1}" />
            </do_elseif>
            <do_else>
              <!-- No minigame was played, something's wrong (e.g. $speakids empty), just assume a "neutral" score for now (no penalty, no bonus) -->
              <set_value name="$scoreid" exact="2" />
            </do_else>
            <!-- <debug_text text="'$path=%1, $followupfactor=%2, $penalty=%3, $bonus=%4, $speakids=%5, minigame score=%6, $scoreid=%7'.[$path, $followupfactor, $penalty, $bonus, $speakids, event.param3, $scoreid]" /> -->

            <!-- Find closest matching speakid. Prefer "worse" (higher) speakid over "better" (lower) speakid if distance is the same. -->
            <!-- NOTE: Even if we map on a non-fail sample, $scoreid == 5 means fail anyway. -->
            <do_all exact="$speakids.count" counter="$i">
              <do_if value="$speakids.{$i} % 10 ge $scoreid">
                <do_if value="$speakid?">
                  <do_if value="($speakids.{$i} % 10 - $scoreid) gt ($scoreid - $speakid % 10)" comment="Current distance is greater than distance of $speakid">
                    <break comment="Keep previously found $speakid, which is lower than $scoreid" />
                  </do_if>
                </do_if>
                <set_value name="$speakid" exact="$speakids.{$i}" />
                <break />
              </do_if>
              <set_value name="$speakid" exact="$speakids.{$i}" />
            </do_all>
            <do_if value="$speakid?">
              <!--<add_npc_line speaker="player.entity" line="$speakid" hidechoices="true" />-->
            </do_if>
            <do_else>
              <!-- Can only happen if $speakids is empty -->
              <debug_text text="'Missing Ren Otani lines for player choice %1 \'%2\'. Assuming a failure scenario'.[$path, readtext.{1002}.{$path}]" filter="error" />
              <set_value name="$speakid" exact="$path + 5" />
            </do_else>
            <!--
            Perfect 1 -> Penalty +0, Bonus +1 (only if $scoreid is also 1)
            Good 2    -> Penalty +0
            Medium 3  -> Penalty +1
            Bad 4     -> Penalty +2
            Fail 5    -> Penalty +3 (instant failure)
            -->
            <set_value name="$maxpenalty" exact="2 * (not $rewarddata)" comment="2 by default, but 0 if this is extended smalltalk"/>
            <set_value name="$penalty" operation="add" exact="[0, 0, 1, 2, 3].{$scoreid}" />
            <do_if value="$scoreid == 1">
              <set_value name="$bonus" operation="add" />
            </do_if>
            <debug_text text="'Minigame outcome: %1, cumulative penalty: %2, bonus: %3'.[['perfect', 'good', 'medium', 'bad', 'fail'].{$scoreid}, $penalty, $bonus * ($penalty == 0)]" />
            <do_if value="$penalty ge $maxpenalty and not $rewarddata">
              <!-- Bad score: Don't allow restarting smalltalk if player aborts now. But the player can still win if the penalty stays below the limit -->
              <set_value name="event.object.$SmalltalkDone" exact="1" />
              <signal_cue_instantly cue="CleanupNPC" param="event.object" />
            </do_if>
            <do_if value="$penalty gt $maxpenalty">
              <do_if value="$speakid % 10 == 5 and readtext.{event.object.page}.{$speakid}?">
                <!-- NPC has specific fail response -->
                <!--<add_npc_line line="$speakid" />-->
              </do_if>
              <do_else>
                <!-- Use generic fail response -->
                <get_text_ids_in_range result="$speakids" page="event.object.page" start="900500" end="900599" />
                <!--<add_npc_line line="$speakids.random" />-->
              </do_else>
              <signal_cue_instantly cue="SetupRelationReward" param="[event.object, -0.00032]" />
            </do_if>
            <do_else>
              <!-- Create $choices, possibly update $basechoices (even if smalltalk is about to finish) -->
              <include_actions ref="FindPlayerChoices" />
              <!-- Add possible player choices before adding NPC response, to determine whether this is a success state -->
              <set_value name="$finish" exact="true" comment="May be set to false below" />
              <!-- Skip the last step (shortcut to success) if there hasn't been any penalty before -->
              <do_if value="$stepsleft gt 0 and not ($stepsleft == 1 and $penalty == 0)">
                <do_all exact="5" counter="$i">
                  <do_if value="$choices.{$i}">
                    <do_if value="$finish">
                      <!-- Adding first player choice, set up dialog menu -->
                      <set_value name="$finish" exact="false" />
                      <do_if value="$rewarddata">
                        <add_player_choice text="{1002,2}" section="g_goodbye" position="bottom_right" comment="Goodbye"/>
                      </do_if>
                      <do_else>
                        <add_player_choice_return text="{1002,20}" position="bottom_right" />
                      </do_else>
                      <!-- Always show confidence bar and play minigame, no matter how many texts for Ren are available -->
                      <set_value name="static.$difficulty" min="1" max="5" />
                      <setup_conversation_minigame difficulty="static.$difficulty" passes="static.$passes" speed="static.$speed * 0.1 * (1.0 + 0.1 * ($rewarddata != null))" />
                    </do_if>
                    <do_if value="$choices.{$i}" exact="$basechoices.{$i}">
                      <!-- Base choice: Assign $followupfactor == 10000 and $basechoiceidx == $i -->
                      <add_player_choice text="readtext.{1002}.{$choices.{$i}}" section="gSmalltalk_step" position="$i" choiceparam="[$choices.{$i}, 10000, $penalty, $bonus, $basechoices, $i, $stepsleft - 1, $rewarddata]" confidence="[1,1,1,1,2,2,2,3,3,4].random" />
                    </do_if>
                    <do_else>
                      <!-- Follow-up choice: Divide $followupfactor by 10 and keep previous $basechoiceidx -->
                      <add_player_choice text="readtext.{1002}.{$choices.{$i}}" section="gSmalltalk_step" position="$i" choiceparam="[$choices.{$i}, $followupfactor / 10, $penalty, $bonus, $basechoices, $basechoiceidx, $stepsleft - 1, $rewarddata]" confidence="[1,1,1,1,2,2,2,3,3,4].random" />
                    </do_else>
                  </do_if>
                </do_all>
              </do_if>

              <!-- NPC response -->
              <get_text_ids_in_range result="$ids" page="event.object.page" start="$speakid" end="$path + 5" />
              <do_if value="$ids.count" min="1">
                <!-- NPC has specific response, minimal required quality was reached -->
                <!--<add_npc_line line="$ids.{1}" hidechoices="$finish" />-->
              </do_if>
              <do_else>
                <!-- Determine NPC mood for generic response -->
                <do_if value="not $finish">
                  <set_value name="$moodid" exact="$scoreid" />
                  <do_if value="$penalty ge 2 and $moodid lt 4">
                    <set_value name="$moodid" operation="add" comment="High penalty has impact on NPC reaction" />
                  </do_if>
                  <do_elseif value="$stepsleft ge 2 and $moodid lt 4">
                    <set_value name="$moodid" operation="add" comment="In first step, NPC is still reserved, reaction is never perfect (1 = good, 2 = medium, 3 or 4 = bad)" />
                  </do_elseif>
                  <!-- Generic NPC response based on mood -->
                  <get_text_ids_in_range result="$speakids" page="event.object.page" start="900000 + $moodid * 100" end="900000 + ($moodid * 100) + 99" />
                </do_if>
                <do_else>
                  <do_if value="$scoreid le 2 and $penalty == 0">
                    <!-- Good final player choice and no penalty: Use "perfect" generic success response -->
                    <set_value name="$moodid" exact="1" />
                  </do_if>
                  <do_else>
                    <!-- Use "good" generic success response ("perfect" only if last player choice was perfect) -->
                    <set_value name="$moodid" exact="[$scoreid, 2].min" comment="1 or 2" />
                  </do_else>
                  <!-- Generic NPC response based on mood -->
                  <get_text_ids_in_range result="$speakids" page="event.object.page" start="900000 + $moodid * 100" end="900000 + ($moodid * 100) + 2" />
                </do_else>
                <!-- <debug_text text="'$finish=%5, $scoreid=%1, $penalty=%2, $followupfactor=%3, $moodid=%4, $speakids=%5'.[$scoreid, $penalty, $followupfactor, $moodid, $finish, $speakids]" /> -->
                <!--<add_npc_line line="$speakids.random" hidechoices="$finish" />-->
              </do_else>

              <!-- Finish and show reward options? -->
              <do_if value="$finish">
                <do_if value="$penalty">
                  <set_value name="$bonus" exact="0" />
                </do_if>
                <set_value name="event.object.$SmalltalkDone" exact="2 + $bonus" />
                <signal_cue_instantly cue="SetupRelationReward" param="[event.object, 0.00032 * (1 + $bonus)]" />
                <signal_cue_instantly cue="CleanupNPC" param="event.object" />
                <!-- Determine valid reward types
                  $rewarddata: (list elements are null if hidden, zero/false if disabled, non-zero if enabled)
                  - {1}: current entity discount
                  - {2}: station discount
                  - {3}: reveal skill (value == npc_candidates)
                  - {4}: subscribe to station trade updates
                  - {5}: bonus earned by player in minigame - can be used for different reward qualities (0 to 8, bribing sets to 2, continued smalltalk adds 3)
                  - {6}: allowed to continue smalltalk (hard)
                  - {7}: unlock station info
                  - {8}: teach employees (contains randomly selected skill to be used as fallback)
                  - {9}: allowed to bribe NPC (unlocks continued smalltalk) (this is the bribe ware when selected)
                -->
                <set_value name="$wascontinued" exact="$rewarddata != null" />
                <do_if value="$wascontinued and $bonus ge 7" comment="At least four out of five times 5 points in minigame">
                  <unlock_achievement name="SMALLTALK_PERFECT" />
                </do_if>
                <set_value name="$rewarddata" exact="[null, null, false, null, $bonus, null, null, null, null]" />
                <set_value name="event.param2.{8}" exact="$rewarddata" />
                <set_value name="event.param2.{4}" exact="$bonus" comment="Write bonus back into param2, will be used in continued smalltalk" />
                <do_if value="event.object.isitemtrader or event.object.type == entitytype.upgradetrader">
                  <set_value name="$rewarddata.{1}" exact="true" />
                </do_if>
                <do_if value="event.object.container.isclass.station and event.object.container.owner != faction.player and event.object.container.tradenpc">
                  <set_value name="$rewarddata.{2}" exact="true" />
                  <do_if value="not event.object.container.hastradesubscription and not $wascontinued">
                    <set_value name="$rewarddata.{4}" exact="event.object.iscontrolentity or event.object.isitemtrader
                               or event.object.type == entitytype.upgradetrader
                               or event.object.type == entitytype.licencetrader
                               or event.object.type == entitytype.lawenforcement" />
                  </do_if>
                </do_if>
                <do_if value="player.platform and player.platform == event.object.room">
                  <!-- Find other NPCs on the platform (or in case of continued smalltalk, find on whole station) -->
                  <set_value name="$npccontext" exact="player.platform" />
                  <do_if value="$wascontinued">
                    <set_value name="$npccontext" exact="$npccontext.container" />
                  </do_if>
                  <find_object_component name="$npcs" object="$npccontext" multiple="true" class="class.npc">
                    <match owner="faction.player" negate="true" />
                    <match_relation faction="faction.player" comparison="not" relation="enemy" />
                    <!-- Don't match NPCs on ships docked at station -->
                    <match_context class="class.ship" negate="true" />
                  </find_object_component>
                  <!-- Remove unwanted entries -->
                  <set_value name="$suitabletypes" exact="[entitytype.pilot, entitytype.commander, entitytype.manager, entitytype.defencecontrol, entitytype.engineer, entitytype.architect, entitytype.marine]" />
                  <do_all exact="$npcs.count" counter="$i" reverse="true">
                    <do_if value="$npcs.{$i} != event.object and $npcs.{$i}.page and ($npcs.{$i}.isspecialist or not $npcs.{$i}.iscontrolentity) and not $npcs.{$i}.skillsvisible">
                      <do_if value="$npcs.{$i}.isspecialist or $suitabletypes.indexof.{$npcs.{$i}.type}">
                        <!-- Found a suitable NPC -->
                        <continue />
                      </do_if>
                    </do_if>
                    <remove_value name="$npcs.{$i}" />
                  </do_all>
                  <debug_text text="'$npcs=' + $npcs" />
                  <do_if value="$npcs.count">
                    <set_value name="$rewarddata.{3}" exact="$npcs" />
                  </do_if>
                </do_if>
                <do_if value="$wascontinued">
                  <!-- Upgrade employee skill - select fallback skill type -->
                  <do_any>
                    <set_value name="$rewarddata.{8}" exact="skilltype.combat"      weight="1 + [1, 1, entitytype.lawenforcement, entitytype.armsdealer].indexof.{event.object.type}" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.engineering" weight="1 + [entitytype.dronetrader, entitytype.smallshiptrader, entitytype.shiptrader, entitytype.shiptech, entitytype.upgradetrader].indexof.{event.object.type}" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.leadership"  weight="1 + [1, 1, entitytype.lawenforcement, entitytype.recruitment].indexof.{event.object.type}" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.management"  weight="2" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.morale"      weight="1 + [1, entitytype.recruitment].indexof.{event.object.type}" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.navigation"  weight="1" />
                    <set_value name="$rewarddata.{8}" exact="skilltype.science"     weight="1" />
                  </do_any>
                </do_if>
                <do_else>
                  <!-- Bribe: Set to true or false (value will contain inventory ware later) -->
                  <set_value name="$rewarddata.{9}" exact="$penalty == 0" />
                </do_else>
                <!-- Offer reward (selectable) -->
                <do_if value="not $wascontinued">
                  <!--<add_npc_line line="901750" hidechoices="true" comment="Reward introduction" />-->
                  <do_if value="$rewarddata.{9}">
                    <signal_cue_instantly cue="SetupBribeHelp" param="event.object" check="false" />
                  </do_if>
                </do_if>
                <add_npc_line line="901760" comment="Offering service" />
                <include_actions ref="AddRewardOptions" comment="using event.object, event.param2 and $rewarddata" />
              </do_if>
            </do_else>
          </do_else>
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardDiscountChoice'">
          <!-- event.param2 == $rewarddata -->
          <add_player_choice position="top_left" text="{1002,12230}" comment="Discount at station" section="gSmalltalk_rewardDiscount" choiceparam="[event.object.container.tradenpc, event.param2.{5}]" selectable="event.param2.{2}" />
          <add_player_choice position="top_right" text="{1002,12231}" comment="Personal discount" section="gSmalltalk_rewardDiscount" choiceparam="[event.object, event.param2.{5}]" selectable="event.param2.{1}" />
          <add_player_choice_return text="{1002,20}" position="bottom_right" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardDiscount'">
          <!-- event.param2 == [ discount NPC, bonus ] -->
          <do_if value="event.param2.{1} == event.object">
            <add_npc_line line="4119" comment="I'll give you a discount" />
          </do_if>
          <do_else>
            <add_npc_line line="4119" comment="Discount at this station" />
          </do_else>
          <signal_cue_instantly cue="SmalltalkRewardDiscount" param="event.param2" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardTradeUpdates'">
          <add_npc_line line="4119" comment="Sure" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardContactChoice'">
          <!-- event.param2 == $rewarddata -->
          <set_value name="$specialisttypes" exact="[
                     entitytype.specialistagriculture, entitytype.specialistpowerstorage, entitytype.specialistfood, entitytype.specialistchemical, entitytype.specialistprecision,
                     entitytype.specialistweapons, entitytype.specialistpharmaceuticals, entitytype.specialistmetals, entitytype.specialistgeophysics, entitytype.specialistsurfacesystems,
                     entitytype.specialistpowersupply, entitytype.specialistaquatics ]" />
          <set_value name="$stationcrewtypes" exact="[
                     entitytype.manager, entitytype.defencecontrol ]" />
          <set_value name="$capshipcrewtypes" exact="[
                     entitytype.commander, entitytype.engineer, entitytype.defencecontrol, entitytype.architect ]" />
          <set_value name="$myshipcrewtypes" exact="[
                     entitytype.pilot, entitytype.engineer, entitytype.marine ]" />
          <set_value name="$npcs" exact="event.param2.{3}" />
          <!-- Create list of types and determine which NPCs are actually available -->
          <create_list name="$npctypes" exact="$npcs.count" />
          <do_all counter="$i" exact="$npcs.count">
            <set_value name="$npctypes.{$i}" exact="$npcs.{$i}.type" />
            <set_value name="$hasstationcrew" exact="$stationcrewtypes.indexof.{$npctypes.{$i}}" operation="add" />
            <set_value name="$hasspecialists" exact="$specialisttypes.indexof.{$npctypes.{$i}}" operation="add" />
            <set_value name="$hascapshipcrew" exact="$capshipcrewtypes.indexof.{$npctypes.{$i}}" operation="add" />
            <set_value name="$hasmyshipcrew" exact="$myshipcrewtypes.indexof.{$npctypes.{$i}}" operation="add" />
          </do_all>
          <do_if value="not $hasspecialists">
            <resize_list list="$specialisttypes" count="0" />
          </do_if>
          <add_player_choice_sub text="{1002,12050}.[{1002,12060}]" comment="Crew for: Station..." section="gSmalltalk_rewardContactChoice_Station" position="top_right" baseparam="event.param2"
                                 choiceparam="[ $npcs, $npctypes, $specialisttypes, event.param2.{5} ]" selectable="$hasstationcrew or $hasspecialists" />
          <add_player_choice_sub text="{1002,12050}.[{1002,12061}]" comment="Crew for: Capital ship..." section="gSmalltalk_rewardContactChoice_List" position="top_left" baseparam="event.param2"
                                 choiceparam="[ $npcs, $npctypes, $capshipcrewtypes, event.param2.{5} ]" selectable="$hascapshipcrew" />
          <add_player_choice_sub text="{1002,12050}.[{1002,12063}]" comment="Crew for: Albion Skunk..." section="gSmalltalk_rewardContactChoice_List" position="bottom_left" baseparam="event.param2"
                                 choiceparam="[ $npcs, $npctypes, $myshipcrewtypes, event.param2.{5} ]" selectable="$hasmyshipcrew" />
          <add_player_choice_return text="{1002,20}" position="bottom_right" comment="Back" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardContactChoice_Station'">
          <!-- event.param2 == [ $npcs, $npctypes, $specialisttypes, $bonus ] -->
          <add_player_choice text="{1002,12022}.[entitytype.manager.name]" comment="Hire: Manager" section="gSmalltalk_rewardContact" position="top_left"
                             choiceparam="[ event.param2.{1}, event.param2.{2}, entitytype.manager, event.param2.{4} ]" selectable="event.param2.{2}.indexof.{entitytype.manager}" />
          <add_player_choice text="{1002,12022}.[entitytype.defencecontrol.name]" comment="Hire: Defence Officer" section="gSmalltalk_rewardContact" position="bottom_left"
                             choiceparam="[ event.param2.{1}, event.param2.{2}, entitytype.defencecontrol, event.param2.{4} ]" selectable="event.param2.{2}.indexof.{entitytype.defencecontrol}" />
          <!-- If no specialist available then $specialisttypes is empty -->
          <add_player_choice_sub text="{1002,12023}.[{1002,12033}]" comment="Hire: Specialist..." section="gSmalltalk_rewardContactChoice_List" position="top_right" baseparam="event.param2"
                                 choiceparam="event.param2" selectable="event.param2.{3}.count" />
          <add_player_choice_return text="{1002,20}" position="bottom_right" comment="Back" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardContactChoice_List'">
          <!-- event.param2 == [ $npcs, $npctypes, $wantedtypes, $bonus ] -->
          <add_player_choice_return text="{1002,20}" position="bottom_right" comment="Back" />
          <set_value name="$types" exact="event.param2.{3}.clone" />
          <set_value name="$slotsleft" exact="5" />
          <do_all exact="$types.count">
            <do_if value="$slotsleft == 1 and $types.count gt 1">
              <add_player_choice_sub text="{1002,12025}" comment="More" section="gSmalltalk_rewardContactChoice_List" baseparam="event.param2" choiceparam="[ event.param2.{1}, event.param2.{2}, $types, event.param2.{4} ]" />
              <break />
            </do_if>
            <add_player_choice text="{1002,12022}.[$types.{1}.name]" section="gSmalltalk_rewardContact"
                               choiceparam="[ event.param2.{1}, event.param2.{2}, $types.{1}, event.param2.{4} ]" selectable="event.param2.{2}.indexof.{$types.{1}}" />
            <remove_value name="$types.{1}" />
            <set_value name="$slotsleft" operation="subtract" />
          </do_all>
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardContact'">
          <!-- event.param2 == [ $allnpcs, $allnpctypes, $wantedtype, $bonus ] -->
          <set_value name="$allnpcs" exact="event.param2.{1}" />
          <set_value name="$allnpctypes" exact="event.param2.{2}" />
          <set_value name="$wantedtype" exact="event.param2.{3}" />
          <set_value name="$bonus" exact="event.param2.{4}" />
          <do_if value="$bonus" min="3" comment="continued smalltalk">
            <set_value name="$highskillchance" exact="100" />
          </do_if>
          <do_elseif value="$bonus">
            <set_value name="$highskillchance" exact="70" />
          </do_elseif>
          <do_else>
            <set_value name="$highskillchance" exact="30" />
          </do_else>
          <!-- Find NPC of wanted type -->
          <set_value name="$npcs" exact="[]" />
          <do_all counter="$i" exact="$allnpcs.count">
            <do_if value="$allnpctypes.{$i} == $wantedtype">
              <append_to_list name="$npcs" exact="$allnpcs.{$i}" />
              <!-- Reward for smalltalk bonus: Pick one of the best NPCs (with high chance of finding the best one) -->
              <do_if value="@$foundnpc.combinedskill lt $allnpcs.{$i}.combinedskill" chance="$highskillchance">
                <set_value name="$foundnpc" exact="$allnpcs.{$i}" />
              </do_if>
            </do_if>
          </do_all>
          <assert value="$npcs.count" />
          <do_if value="not $foundnpc?">
            <set_value name="$foundnpc" exact="$npcs.random" />
          </do_if>
          <!-- Reveal skill -->
          <set_skills_visible entity="$foundnpc" visible="true" />
          <debug_text text="'Revealed skills of NPC %1 (%2), distance=%3, combinedskill=%4'.[$foundnpc, $foundnpc.knownname, $foundnpc.distanceto.{player.entity}, $foundnpc.combinedskill]" />
          <do_if value="$foundnpc == event.object">
            <!-- Found self -->
            <add_npc_line line="4119" comment="Here you go. / Sure!" />
            <!-- Signal NewTarget cue now already, not on event_conversation_finished -->
            <set_value name="event.param2.{3}" exact="null" />
            <signal_cue_instantly cue="md.Guidance.NewTarget" param="[event.param2.{3}, false, player.platform and player.platform == event.param2.{3}.room]"/>
          </do_if>
          <do_else>
            <!-- Found someone else -->
            <do_if value="$foundnpc.combinedskill" min="50" chance="50">
              <add_npc_line line="901802" />
            </do_if>
            <do_else>
              <add_npc_line line="901801" />
            </do_else>
            <!-- Write back found NPC to event param to make it available to event_conversation_finished handler -->
            <set_value name="event.param2.{3}" exact="$foundnpc" />
          </do_else>
          <!-- TODO: Show notification? -->
          <set_value name="event.object.$GuideToNPC" exact="$foundnpc" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardSkillUpgradeChoice'">
          <!-- event.param2 == $rewarddata -->
          <set_value name="$skills" exact="event.object.type.skilltypes" />
          <!-- Find the two best skills -->
          <set_value name="$primaryskill" exact="event.param2.{8}" />
          <set_value name="$secondaryskill" exact="null" />
          <do_all exact="$skills.count" counter="$i" reverse="true">
            <do_if value="$skills.{$i} != skilltype.boarding">
              <set_value name="$skillvalue" exact="event.object.skill.{$skills.{$i}}" />
              <do_if value="$skillvalue" min="@event.object.skill.{$primaryskill}">
                <set_value name="$secondaryskill" exact="$primaryskill" />
                <set_value name="$primaryskill" exact="$skills.{$i}" />
              </do_if>
              <do_elseif value="$skillvalue" min="@event.object.skill.{$secondaryskill}">
                <set_value name="$secondaryskill" exact="$skills.{$i}" />
              </do_elseif>
            </do_if>
          </do_all>
          <!-- Always offer at least one skill. Offer two skills if NPC has multiple skills with at least 2 stars -->
          <add_player_choice text="{1002,12270}.[$primaryskill.name]" comment="Upgrade skill" section="gSmalltalk_rewardSkillUpgrade" position="top_left" choiceparam="$primaryskill" />
          <do_if value="$secondaryskill and $secondaryskill != $primaryskill">
            <do_if value="event.object.skill.{$secondaryskill} ge 2">
              <add_player_choice text="{1002,12270}.[$secondaryskill.name]" comment="Upgrade skill" section="gSmalltalk_rewardSkillUpgrade" position="top_right" choiceparam="$secondaryskill" />
            </do_if>
          </do_if>
          <add_player_choice_return text="{1002,20}" position="bottom_right" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_rewardSkillUpgrade'">
          <!-- event.param2 == $skilltype -->
          <add_npc_line line="4119" comment="Here you go. / Sure!" />
          <set_value name="$ware" exact="event.param2.ware" />
          <add_inventory entity="player.entity" ware="$ware" exact="1" />
          <show_notification caption="{1015,100}" details="[ [ $ware.icon, $ware.name, null, null, null, null ] ]" timeout="5s" sound="notification_achievement">
            <interaction text="{1001,2204}" param="'inventory interaction'" param2="$ware" silent="true" />
          </show_notification>
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_bribeChoice'">
          <!-- event.param2 == [path, followupfactor, penalty, bonus, basechoices, basechoiceidx, stepsleft, rewarddata] -->
          <!-- To allow for continued smalltalk, we have to pass on the full param2 list. But we also have to pass the choice that the player has made: store it in $rewarddata -->
          <set_value name="$choiceparam" exact="event.param2.clone" />
          <set_value name="$choiceparam.{8}" exact="$choiceparam.{8}.clone" />
          <set_value name="$choiceparam.{8}.{9}" exact="ware.inv_redwater" />
          <add_player_choice text="{1002,12290}.[ware.inv_redwater.name]" comment="Give: Red Water" section="gSmalltalk_bribe" position="top_left" choiceparam="$choiceparam" selectable="player.entity.inventory.{ware.inv_redwater}.exists" />
          <set_value name="$choiceparam" exact="event.param2.clone" />
          <set_value name="$choiceparam.{8}" exact="$choiceparam.{8}.clone" />
          <set_value name="$choiceparam.{8}.{9}" exact="ware.inv_haze" />
          <add_player_choice text="{1002,12290}.[ware.inv_haze.name]" comment="Give: Haze" section="gSmalltalk_bribe" position="bottom_left" choiceparam="$choiceparam" selectable="player.entity.inventory.{ware.inv_haze}.exists" />
          <set_value name="$choiceparam" exact="event.param2.clone" />
          <set_value name="$choiceparam.{8}" exact="$choiceparam.{8}.clone" />
          <set_value name="$choiceparam.{8}.{9}" exact="ware.inv_paranidjewellery" />
          <add_player_choice text="{1002,12290}.[ware.inv_paranidjewellery.name]" comment="Give: Paranid Jewellery" section="gSmalltalk_bribe" position="top_right" choiceparam="$choiceparam" selectable="player.entity.inventory.{ware.inv_paranidjewellery}.exists" />
          <add_player_choice_return text="{1002,20}" position="bottom_right" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_bribe'">
          <!-- event.param2 == [path, followupfactor, penalty, bonus, basechoices, basechoiceidx, stepsleft, rewarddata] -->
          <set_value name="$rewarddata" exact="event.param2.{8}" />
          <do_if value="event.name == 'event_conversation_next_section'">
            <set_value name="$ware" exact="$rewarddata.{9}" />
            <remove_inventory entity="player.entity" ware="$ware" exact="1" />
            <show_notification caption="{1015,101}.[event.object.name]" details="[ [ $ware.icon, $ware.name, {1001,1202} + {1001,120}, null, null, 1 ] ]" sound="notification_achievement" timeout="5s" />
            <set_value name="$rewarddata.{6}" exact="true" comment="allowed to continue smalltalk" />
            <set_value name="$rewarddata.{5}" exact="2" comment="set bonus to maximum (only for purpose of current rewards, not actual bonus in param2)" />
            <add_npc_line line="4320" comment="accepting gift from the player" />
          </do_if>
          <include_actions ref="AddRewardOptions" comment="using event.object, event.param2 and $rewarddata" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_continue'">
          <!-- event.param2 == [path, followupfactor, penalty, bonus, basechoices, basechoiceidx, stepsleft, rewarddata] -->
          <set_value name="$path" exact="event.param2.{1}" />
          <set_value name="$followupfactor" exact="event.param2.{2}" />
          <set_value name="$penalty" exact="event.param2.{3}" />
          <set_value name="$bonus" exact="event.param2.{4}" />
          <set_value name="$basechoices" exact="event.param2.{5}" />
          <set_value name="$basechoiceidx" exact="event.param2.{6}" />
          <set_value name="$stepsleft" exact="event.param2.{7}" />
          <set_value name="$rewarddata" exact="event.param2.{8}" />
          <!-- Add bonus -->
          <set_value name="$bonus" exact="3" operation="add" />
          <!-- Create $choices -->
          <include_actions ref="FindPlayerChoices" />
          <!-- Add possible player choices, no NPC lines here -->
          <set_value name="static.$difficulty" min="1" max="5" />
          <setup_conversation_minigame difficulty="static.$difficulty" passes="static.$passes" speed="static.$speed * 0.1 * (1.0 + 0.1)" />
          <add_player_choice text="{1002,2}" section="g_goodbye" position="bottom_right" comment="Goodbye"/>
          <do_all exact="5" counter="$i">
            <do_if value="$choices.{$i}">
              <do_if value="$choices.{$i}" exact="$basechoices.{$i}">
                <!-- Base choice: Assign $followupfactor == 10000 and $basechoiceidx == $i -->
                <add_player_choice text="readtext.{1002}.{$choices.{$i}}" section="gSmalltalk_step" position="$i" choiceparam="[$choices.{$i}, 10000, 0, $bonus, $basechoices, $i, 3, $rewarddata]" confidence="[1,1,1,1,2,2,2,3,3,4].random" />
              </do_if>
              <do_else>
                <!-- Follow-up choice: Divide $followupfactor by 10 and keep previous $basechoiceidx -->
                <add_player_choice text="readtext.{1002}.{$choices.{$i}}" section="gSmalltalk_step" position="$i" choiceparam="[$choices.{$i}, $followupfactor / 10, 0, $bonus, $basechoices, $basechoiceidx, 3, $rewarddata]" confidence="[1,1,1,1,2,2,2,3,3,4].random" />
              </do_else>
            </do_if>
          </do_all>
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_settings'">
          <do_if value="not static.$difficulty? or event.param2 == 'reset'">
            <set_value name="static.$difficulty" exact="1" />
            <set_value name="static.$passes" exact="2" />
            <set_value name="static.$speed" exact="8" />
          </do_if>
          <add_player_choice_sub text="'Difficulty: ' + static.$difficulty" position="top_left" section="gSmalltalk_settings2" choiceparam="['Difficulty', '$difficulty', 1, 5, static.$difficulty]" />
          <add_player_choice_sub text="'Passes: ' + static.$passes" position="left" section="gSmalltalk_settings2" choiceparam="['Passes', '$passes', 1, 10, static.$passes]" />
          <add_player_choice_sub text="'Speed: ' + static.$speed" position="bottom_left" section="gSmalltalk_settings2" choiceparam="['Speed', '$speed', 1, 20, static.$speed]" />
          <add_player_choice text="'Test mini-game'" position="top_right" section="gSmalltalk_settings" confidence="[event.param3, 1].max" />
          <add_player_choice text="'Reset'" position="right" section="gSmalltalk_settings" choiceparam="'reset'" />
          <add_player_choice_return text="'Back'" position="bottom_right" />
          <setup_conversation_minigame difficulty="static.$difficulty" passes="static.$passes" speed="static.$speed * 0.1" />
        </do_elseif>

        <do_elseif value="event.param == 'gSmalltalk_settings2'">
          <set_value name="static.{event.param2.{2}}" exact="event.param2.{5}" />
          <add_player_choice text="event.param2.{1} + ' -1'" position="top_left" section="gSmalltalk_settings2"
                             choiceparam="[event.param2.{1}, event.param2.{2}, event.param2.{3}, event.param2.{4}, event.param2.{5} - 1]"
                             selectable="event.param2.{5} gt event.param2.{3}" />
          <add_player_choice text="event.param2.{1} + ' +1'" position="bottom_left" section="gSmalltalk_settings2"
                             choiceparam="[event.param2.{1}, event.param2.{2}, event.param2.{3}, event.param2.{4}, event.param2.{5} + 1]"
                             selectable="event.param2.{5} lt event.param2.{4}" />
          <add_player_choice_return text="'Back (value = ' + event.param2.{5} + ')'" position="bottom_right" />
        </do_elseif>

        <do_else>
          <debug_text text="'Unexpected section \'' + event.param + '\''" filter="error" />
        </do_else>
      </actions>
    </cue>

    <cue name="SetupRelationReward" instantiate="true" namespace="this">
      <conditions>
        <event_cue_signalled />
      </conditions>
      <!-- event.param == [$actor, $relation] -->
      <actions>
        <set_value name="$actor" exact="event.param.{1}" />
        <set_value name="$relation" exact="event.param.{2}" />
      </actions>
      <cues>
        <cue name="RelationReward">
          <conditions>
            <!-- Wait until $actor has finished their smalltalk response -->
            <check_any>
              <event_speak_finished actor="$actor" />
              <event_conversation_finished comment="just in case we miss speak_finished"/>
            </check_any>
          </conditions>
          <actions>
            <add_faction_relation faction="faction.player" otherfaction="$actor.owner" value="$relation" />
          </actions>
        </cue>
      </cues>
    </cue>

    <cue name="SetupBribeHelp" namespace="this">
      <conditions>
        <event_cue_signalled />
      </conditions>
      <!-- event.param == $actor -->
      <actions>
        <set_value name="$actor" exact="event.param" />
      </actions>
      <cues>
        <cue name="BribeHelp">
          <conditions>
            <event_speak_finished actor="$actor" line="901750" />
          </conditions>
          <actions>
            <show_help line="800" comment="You were very convincing. You can impress this person even more to get substantially better offers." log="true" position="1" />
            <cancel_cue cue="BribeHelpFailed" />
          </actions>
        </cue>
        <cue name="BribeHelpFailed">
          <conditions>
            <event_conversation_finished comment="just in case we miss speak_finished" />
          </conditions>
          <actions>
            <reset_cue cue="SetupBribeHelp" />
          </actions>
        </cue>
      </cues>
    </cue>

    <cue name="RewardContactFinishedHandler" instantiate="true" namespace="this">
      <conditions>
        <event_conversation_finished outcome="gSmalltalk_rewardContact" />
        <check_value value="typeof event.param2.{3} == datatype.component" />
      </conditions>
      <actions>
        <!-- event.param2 == [ $allnpcs, $allnpctypes, $foundnpc, $bonus ] -->
        <signal_cue_instantly cue="md.Guidance.NewTarget" param="[event.param2.{3}, false, player.platform and player.platform == event.param2.{3}.room]"/>
      </actions>
    </cue>

    <cue name="RewardTradeUpdatesFinishedHandler" instantiate="true" namespace="this">
      <conditions>
        <event_conversation_finished outcome="gSmalltalk_rewardTradeUpdates" />
      </conditions>
      <actions>
        <add_trade_subscription object="event.object.container" />
        <show_help line="350" comment="Your ship computer database will automatically be updated with trade offers from this station." log="false" position="8" />
      </actions>
    </cue>

    <library name="FindPlayerChoices">
      <!-- Input: $basechoices, $path, $followupfactor; Output: $choices ($basechoices can also be updated) -->
      <actions>
        <!-- Build a choice list of base choices and follow-up choices (follow-ups take precedence) -->
        <set_value name="$choices" exact="$basechoices.clone" />
        <resize_list list="$choices" count="5" />
        <do_if value="$followupfactor gt 1">
          <get_text_ids_in_range result="$ids" page="1002" start="$path + $followupfactor" end="$path + $followupfactor * 5" factor="$followupfactor" />
          <do_if value="$ids.count" min="1">
            <!-- The first follow-up choice is placed on $basechoiceidx, the index of the base choice -->
            <set_value name="$choices.{$basechoiceidx}" exact="$ids.{1}" />
            <!-- Find slots for the remaining follow-up choices (if any) -->
            <do_all exact="$ids.count - 1" counter="$i">
              <set_value name="$curidx" exact="$choices.indexof.{null}" />
              <do_if value="$curidx" min="1" max="5">
                <!-- Found an empty slot in $choices, use it -->
                <set_value name="$choices.{$curidx}" exact="$ids.{$i + 1}" />
              </do_if>
              <do_else>
                <!-- No empty slots left, find a base choice that we can overwrite, as close to $basechoiceidx as possible -->
                <do_all exact="4" counter="$j">
                  <do_if value="$basechoiceidx + $j" max="5">
                    <set_value name="$curidx" exact="$basechoiceidx + $j" comment="above $basechoiceidx" />
                  </do_if>
                  <do_else>
                    <set_value name="$curidx" exact="5 - $j" comment="below $basechoiceidx" />
                  </do_else>
                  <do_if value="$choices.{$curidx}" exact="$basechoices.{$curidx}">
                    <set_value name="$choices.{$curidx}" exact="$ids.{$i + 1}" />
                    <break />
                  </do_if>
                </do_all>
              </do_else>
            </do_all>
          </do_if>
          <remove_value name="$ids" />
        </do_if>
        <!-- If there are still empty choices, fill them up with excess base choices above 5 -->
        <set_value name="$curidx" exact="$choices.indexof.{null}" />
        <do_while value="$curidx ge 1 and $curidx le 5 and $basechoices.count gt 5">
          <set_value name="$choices.{$curidx}" exact="$basechoices.{6}" />
          <!-- Also permanently transfer choice to empty slot in $basechoices -->
          <set_value name="$basechoices.{$curidx}" exact="$basechoices.{6}" />
          <remove_value name="$basechoices.{6}" />
          <!-- Repeat -->
          <set_value name="$curidx" exact="$choices.indexof.{null}" />
        </do_while>
        <remove_value name="$curidx" />
      </actions>
    </library>

    <library name="AddRewardOptions">
      <!-- Using event.object, event.param2 and $rewarddata -->
      <actions>
        <set_value name="$textidoffset" exact="($rewarddata.{5} ge 3) * 100" comment="In case of continued smalltalk, use text ID offset 100" />
        <!-- Ask for: Discount ([...] with submenu if multiple types of discounts available) -->
        <do_if value="typeof $rewarddata.{1} != datatype.null">
          <add_player_choice_sub position="top_left" text="readtext.{1002}.{12201 + $textidoffset}.[readtext.{1002}.{12210 + $textidoffset}]" comment="Ask for: Discount..." section="gSmalltalk_rewardDiscountChoice" baseparam="event.param2" choiceparam="$rewarddata" selectable="$rewarddata.{1} or $rewarddata.{2}" immediate="$textidoffset" />
        </do_if>
        <do_elseif value="typeof $rewarddata.{2} != datatype.null">
          <add_player_choice position="top_left" text="readtext.{1002}.{12200 + $textidoffset}.[readtext.{1002}.{12210 + $textidoffset}]" comment="Ask for: Discount" section="gSmalltalk_rewardDiscount" choiceparam="[event.object.container.tradenpc, $rewarddata.{5}]" selectable="$rewarddata.{2}" immediate="$textidoffset" />
        </do_elseif>
        <!-- Ask about: Job applicant... -->
        <do_if value="typeof $rewarddata.{3} != datatype.null">
          <add_player_choice_sub position="bottom_left" text="readtext.{1002}.{12240 + $textidoffset}" comment="Ask about: Job applicant..." section="gSmalltalk_rewardContactChoice" baseparam="event.param2" choiceparam="$rewarddata" selectable="$rewarddata.{3}" immediate="$textidoffset" />
        </do_if>
        <do_if value="typeof $rewarddata.{8} != datatype.null">
          <add_player_choice_sub position="top_right" text="{1002,12260}" comment="Ask for: Employee training" section="gSmalltalk_rewardSkillUpgradeChoice" baseparam="event.param2" choiceparam="$rewarddata" selectable="$rewarddata.{8}" immediate="$textidoffset" />
        </do_if>
        <do_elseif value="typeof $rewarddata.{4} != datatype.null">
          <add_player_choice position="top_right" text="{1002,12220}" comment="Ask for: Trade offer updates" choiceparam="$rewarddata" section="gSmalltalk_rewardTradeUpdates" selectable="$rewarddata.{4}" />
        </do_elseif>
        <do_if value="typeof $rewarddata.{6} != datatype.null">
          <add_player_choice position="right" text="{1002,900001}" comment="Continue: Smalltalk (hard)" section="gSmalltalk_continue" choiceparam="event.param2" selectable="$rewarddata.{6}" immediate="true" />
        </do_if>
        <do_elseif value="typeof $rewarddata.{9} != datatype.null">
          <add_player_choice_sub position="right" text="{1002,12280}" comment="Give: Item..." section="gSmalltalk_bribeChoice" baseparam="event.param2" choiceparam="event.param2" selectable="$rewarddata.{9}" />
        </do_elseif>
        <!-- You cannot return from the reward selection, only say goodbye -->
        <add_player_choice text="{1002,2}" section="g_goodbye" position="bottom_right" comment="Goodbye"/>
      </actions>
    </library>

    <cue name="SmalltalkRewardDiscount" instantiate="true" namespace="this">
      <conditions>
        <event_cue_signalled />
        <!-- event.param == [ discount NPC, bonus ] -->
      </conditions>
      <actions>
        <set_value name="$param" exact="event.param" />
      </actions>
      <cues>
        <cue name="SmalltalkRewardDiscount2">
          <conditions>
            <event_conversation_finished />
          </conditions>
          <delay max="500ms" />
          <actions>
            <do_if value="$param.{2}" min="3" comment="Continued smalltalk">
              <!-- $time goes up with $bonus (3 = 24h, 4 = 36h, 5 = 48h, 6 = 60h, 7 = 72h, 8 = 84h) -->
              <set_value name="$time" exact="24h + (12h * ($param.{2} - 3))" />
              <!-- amount + 1% for each extra bonus step -->
              <set_value name="$amount" min="10" max="15" />
              <set_value name="$amount" operation="add" exact="$param.{2} - 3" />
            </do_if>
            <do_elseif value="$param.{2}" comment="Bonus, but not continued">
              <set_value name="$time" exact="60min" />
              <set_value name="$amount" min="5" max="15" />
            </do_elseif>
            <do_else>
              <set_value name="$time" exact="30min" />
              <set_value name="$amount" min="5" max="10" />
            </do_else>
            <!-- Discount name: Insider Benefactor / Good Connections -->
            <add_player_discount entity="$param.{1}" amount="$amount" name="readtext.{20210}.{[10001,10011].random}" time="$time" />
          </actions>
        </cue>
      </cues>
    </cue>

  </cues>

	
	</replace>
</diff><?xml version="1.0" encoding="utf-8"?>
<content id="" name="" description="" version="107" save="false" author="Snowmane" date="2020-06-24" lastupdate="">
</content>
