There are a couple of errors in the script for this mod which is why exclude sector and possibly some other things are not working correctly. They are very minor errors which I already fixed in the files I have.
If anyone is interested, just copy and paste the code into their tatertrade script in the mod folder. Need to overwrite the whole thing.
Code: Select all
<?xml version="1.0" encoding="utf-8" ?>
<!-- TaterTrade Ai, Created by Ludsoe. -->
<aiscript name="tatertrade" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="aiscripts.xsd" version="3">
<order id="TaterTrade" name="TaterTrade" description="Advanced Trader Logic" category="trade" infinite="true">
<params>
<!-- Define where the trader will base itself -->
<param name="home" default="this.sector" type="object" text="TraderHome" comment="Home Sector/Station">
<input_param name="class" value="[class.sector,class.station]"/>
</param>
<param name="returnhome" type="bool" default="false" text="HomeBound" comment="Return Home after completing a trade?" />
<!-- Some Range configurations -->
<param name="minbuy" default="0" type="number" advanced="true" text="{1041, 10066}" comment="Min gate distance to buy. Buy range supported if $minbuy and $maxbuy are provided">
<input_param name="startvalue" value="0"/>
<input_param name="min" value="0"/>
<input_param name="max" value="[@this.ship.commander.tradenpc.skill.management, @this.ship.pilot.skill.piloting].max * 2"/>
<input_param name="step" value="1"/>
</param>
<param name="maxbuy" default="0" type="number" text="{1041, 10054}" comment="Max gate distance to buy. Buy range supported if $minbuy and $maxbuy are provided">
<input_param name="startvalue" value="0"/>
<input_param name="min" value="0"/>
<input_param name="max" value="[@this.ship.commander.tradenpc.skill.management, @this.ship.pilot.skill.piloting].max * 2"/>
<input_param name="step" value="1"/>
</param>
<param name="minsell" default="0" type="number" advanced="true" text="{1041, 10068}" comment="Min gate distance to sell. Sell range supported if $minsell and $maxsell are provided">
<input_param name="startvalue" value="0"/>
<input_param name="min" value="0"/>
<input_param name="max" value="[@this.ship.commander.tradenpc.skill.management, @this.ship.pilot.skill.piloting].max * 2"/>
<input_param name="step" value="1"/>
</param>
<param name="maxsell" default="0" type="number" text="{1041, 10057}" comment="Max gate distance to sell. Sell range supported if $minsell and $maxsell are provided">
<input_param name="startvalue" value="0"/>
<input_param name="min" value="0"/>
<input_param name="max" value="[@this.ship.commander.tradenpc.skill.management, @this.ship.pilot.skill.piloting].max * 2"/>
<input_param name="step" value="1"/>
</param>
<param name="discount" default="100" type="number" text="Owned Station Buy Mod" comment="How much we pay at stations.">
<input_param name="startvalue" value="100"/>
<input_param name="min" value="0"/>
<input_param name="max" value="200"/>
<input_param name="step" value="5"/>
</param>
<param name="discount2" default="100" type="number" text="Owned Station Sell Mod" comment="How much we pay at stations.">
<input_param name="startvalue" value="100"/>
<input_param name="min" value="0"/>
<input_param name="max" value="200"/>
<input_param name="step" value="5"/>
</param>
<param name="bypass" type="bool" default="false" text="FreeTrade" comment="Do we ignore faction restrictions on owned stations?" />
<param name="fasttrade" type="bool" default="false" text="FastTrade" comment="Attempt to optimise the search engine by skipping deals we deem improfitable." />
<!-- <param name="stationmode" type="bool" default="false" text="Station Trader" comment="When enabled, toggles the trader into station trader mode." /> -->
<!-- Im delaying station mode for this update, It requires quite a bit more work to get ready. -->
<!-- What are we hauling? -->
<param name="warebasket" default="[]" type="list" text="{1041, 10146}" comment="Wares">
<input_param name="type" value="'ware'"/>
<input_param name="cancarry" value="this.ship"/>
</param>
<param name="usepresets" type="bool" default="true" text="Copy Presets" comment="Button to enable presets." />
<param name="allwares" type="bool" default="false" text="All Wares" comment="Enable all wares in the basket." />
<param name="legalwares" type="bool" default="true" text="Legal Wares" comment="Enable all the legal wares." />
<param name="illegalwares" type="bool" default="false" text="Illegal Wares" comment="Enable all the illegal wares." />
<param name="shipbuildingwares" type="bool" default="false" text="ShipBuilding Wares" comment="Enable all the shipbuilding wares." />
<param name="stationbuildingwares" type="bool" default="false" text="StationBuilding Wares" comment="Enable all the stationbuilding wares." />
<param name="hightechwares" type="bool" default="false" text="HighTech Wares" comment="Enable all the HichTech wares." />
<param name="refinedwares" type="bool" default="false" text="Refined Wares" comment="Enable all the Refined wares." />
<param name="biowares" type="bool" default="false" text="Biological Wares" comment="Enable all the Bio wares." />
<!-- Allow Players to exclude sectors for whatever reason. -->
<param name="excludedsectors" default="[]" type="list" text="Exclude Sector" comment="Sectors the player wants excluded from trade searches.">
<input_param name="type" value="'object'"/>
<input_param name="class" value="[class.sector]"/>
</param>
</params>
<skill min="20"/> <!-- Not any hotshot can be a tatertrader, theres standards ye know? -->
<requires>
<match shiptype="shiptype.lasertower" negate="true"/>
</requires>
</order>
<interrupts>
<handler ref="AttackHandler" />
<handler ref="MissileLockHandler" />
<handler ref="ScannedHandler" />
<handler ref="InspectedHandler"/>
<handler ref="FoundAbandonedHandler"/>
<handler ref="ResupplyHandler" />
<handler ref="JobRemoveRequestHandler" />
<handler ref="TargetInvalidHandler"/>
<!-- Disable this for now, as it causes issues and doesnt actually provide the functionality i was looking for.
<handler consume="false">
<conditions>
<check_any>
<check_all>
<event_object_order_cancelled object="$object" immediate="true" />
</check_all>
<check_all>
<event_object_order_ready object="$object" order="$object.order" />
<check_value value="not $object.order.isrunning"/>
</check_all>
</check_any>
</conditions>
<actions>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Interrupt worked! '+event.name" output="false" append="true" />
<do_if value="event.name == 'event_object_order_syncpoint_released' and event.param == $object.order">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'SyncPoint Reached!'" output="false" append="true" />
<cancel_order order="event.param"/>
</do_if>
<do_if value="event.name == 'event_object_order_cancelled'">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Event Param: '+event.param.id" output="false" append="true" />
</do_if>
<do_if value="event.name == 'event_object_order_ready'">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Event Param: '+event.param.id+' my order currently is '+this.ship.order.id" output="false" append="true" />
</do_if>
<abort_called_scripts resume="orderloop" />
</actions>
</handler>
-->
</interrupts>
<init>
<set_value name="$object" exact="this.assignedcontrolled" />
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Starting Log File'" output="false" append="false" />
<set_order_syncpoint_reached order="this.ship.order"/>
<set_command_action commandaction="commandaction.searchingtrades" />
</init>
<patch sinceversion="2">
<set_value name="$discount2" exact="100"/>
</patch>
<patch sinceversion="3">
<set_value name="$fasttrade" exact="false"/>
<set_value name="$usepresets" exact="false"/>
<set_value name="$allwares" exact="false"/>
<set_value name="$legalwares" exact="false"/>
<set_value name="$illegalwares" exact="false"/>
<set_value name="$shipbuildingwares" exact="false"/>
<set_value name="$stationbuildingwares" exact="false"/>
<set_value name="$hightechwares" exact="false"/>
<set_value name="$refinedwares" exact="false"/>
<set_value name="$biowares" exact="false"/>
<create_list name="$excludedsectors" />
</patch>
<!-- Lets code the actual trade logic here. -->
<attention min="unknown">
<actions>
<do_if value="not $usepresets">
<resume label="start" />
</do_if>
<!-- Figure out what we are ment to trade, may be a combination of checkboxes and manually added wares. -->
<set_value name="$addtolist" exact="false"/>
<set_value name="$gotowarelist" exact="0"/>
<set_value name="$warestoadd" exact="[]"/>
<do_if value="$allwares">
<set_value name="$addtolist" exact="true"/>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Trade All Wares Enabled!'" output="false" append="true" />
<set_value name="$warestoadd" exact="[ware.spacefuel,ware.spaceweed,ware.advancedcomposites,ware.advancedelectronics,ware.antimattercells,ware.antimatterconverters,ware.claytronics,
ware.dronecomponents,ware.engineparts,ware.fieldcoils,ware.hullparts,ware.refinedmetals,ware.scanningarrays,ware.shieldcomponents,ware.siliconwafers,
ware.teladianium,ware.turretcomponents,ware.water,ware.wheat,ware.energycells,ware.foodrations,ware.graphene,ware.majadust,ware.majasnails,ware.meat,
ware.microchips,ware.quantumtubes,ware.medicalsupplies,ware.missilecomponents,ware.nostropoil,ware.plasmaconductors,ware.smartchips,ware.sojabeans,
ware.sojahusk,ware.spices,ware.sunriseflowers,ware.superfluidcoolant,ware.swampplant,ware.weaponcomponents]"/>
<resume label="addtowarelist" />
</do_if>
<label name="warelistloop" />
<set_value name="$listchecked" exact="false"/>
<set_value name="$addtolist" exact="false"/>
<do_if value="$gotowarelist == 0">
<do_if value="$legalwares">
<set_value name="$warestoadd" exact="[ware.advancedcomposites,ware.advancedelectronics,ware.antimattercells,ware.antimatterconverters,ware.claytronics,
ware.dronecomponents,ware.engineparts,ware.fieldcoils,ware.hullparts,ware.refinedmetals,ware.scanningarrays,ware.shieldcomponents,ware.siliconwafers,
ware.teladianium,ware.turretcomponents,ware.water,ware.wheat,ware.energycells,ware.foodrations,ware.graphene,ware.majasnails,ware.meat,
ware.microchips,ware.quantumtubes,ware.medicalsupplies,ware.missilecomponents,ware.nostropoil,ware.plasmaconductors,ware.smartchips,ware.sojabeans,
ware.sojahusk,ware.spices,ware.sunriseflowers,ware.superfluidcoolant,ware.swampplant,ware.weaponcomponents]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 1">
<do_if value="$illegalwares">
<set_value name="$warestoadd" exact="[ware.spacefuel,ware.spaceweed,ware.majadust]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 2">
<do_if value="$shipbuildingwares">
<set_value name="$warestoadd" exact="[ware.energycells,ware.engineparts,ware.hullparts,ware.fieldcoils,ware.advancedelectronics,ware.weaponcomponents,ware.turretcomponents]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 3">
<do_if value="$stationbuildingwares">
<set_value name="$warestoadd" exact="[ware.energycells,ware.hullparts,ware.claytronics,ware.advancedelectronics,ware.turretcomponents,ware.shieldcomponents,ware.fieldcoils]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 4">
<do_if value="$hightechwares">
<set_value name="$warestoadd" exact="[ware.advancedcomposites,ware.engineparts,ware.hullparts,ware.microchips,ware.plasmaconductors,ware.quantumtubes,ware.scanningarrays]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 5">
<do_if value="$refinedwares">
<set_value name="$warestoadd" exact="[ware.antimattercells,ware.graphene,ware.superfluidcoolant,ware.refinedmetals,ware.siliconwafers,ware.teladianium]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<do_if value="$gotowarelist == 6">
<do_if value="$biowares">
<set_value name="$warestoadd" exact="[ware.spices,ware.meat,ware.wheat,ware.sojabeans,ware.majasnails,ware.sunriseflowers,ware.swampplant]"/>
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
<!-- Heres a Code example on how to add a additional preset wares list.
<do_if value="$gotowarelist == 7"> - - - - Increment the number in this to allow recursive checking - - - -
<do_if value="$variable_to_enable"> - - - - Change this variable to whatever new checkbox you add to enable the list. - - - -
<set_value name="$warestoadd" exact="[]"/> - - - - This is the list of wares to add when this preset is enabled - - - -
<set_value name="$addtolist" exact="true"/>
</do_if>
<set_value name="$listchecked" exact="true"/>
<resume label="addtowarelist" />
</do_if>
-->
<label name="addtowarelist" />
<!-- This allows us to bulk add wares to the basket, but this is horribly inefficent. I need a better way to prevent adding duplicates. -->
<do_if value="$addtolist or $listchecked">
<do_if value="$warestoadd.count gt 0">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Adding wares to basket! Wares: '+$warestoadd.count" output="false" append="true" />
<do_all exact="$warestoadd.count" counter="$ware">
<set_value name="$addtolist" exact="true"/>
<do_if value="$warebasket.count gt 0">
<do_all exact="$warebasket.count" counter="$basket">
<do_if value="$warestoadd.{$ware} == $warebasket.{$basket}">
<set_value name="$addtolist" exact="false"/>
</do_if>
</do_all>
</do_if>
<do_if value="$addtolist">
<append_to_list name="$warebasket" exact="$warestoadd.{$ware}"/>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Adding Ware: ('+$warestoadd.{$ware}+') to basket'" output="false" append="true" />
</do_if>
</do_all>
</do_if>
<set_value name="$warestoadd" exact="[]"/>
<set_value name="$gotowarelist" exact="$gotowarelist+1"/>
<resume label="warelistloop" />
</do_if>
<remove_value name="$listchecked"/>
<remove_value name="$addtolist"/>
<remove_value name="$warestoadd"/>
<remove_value name="$gotowarelist"/>
<label name="start" />
<set_command_action commandaction="commandaction.searchingtrades" />
<do_if value="not $warebasket.count">
<debug_text text="'No warebasket! %1'.[$warebasket]" filter="error" />
</do_if>
<!-- Figure out what we are ment to do, free trading or station supply. -->
<!-- For now assuming sector based trade. -->
<do_if value="$home.isclass.space">
<set_value name="$range" exact="$home" />
</do_if>
<do_if value="$home.isclass.station"> <!-- The do_else was causing issues to get through, so lets attempt to filter by class -->
<set_value name="$range" exact="$home.sector" />
<do_if value="$home.owner == this.ship.owner"><!-- Check if the station we are assigned to is owned by the player, if yes it can be our commander now. -->
<set_object_commander object="this.ship" commander="$home" />
</do_if>
</do_if>
<find_cluster_in_range distances="$clusterstable" multiple="true" object="$range" mindistance="$minbuy" maxdistance="$maxbuy"/>
<set_value name="$buyspaces" exact="$clusterstable.keys.sorted" /> <!-- sort them -->
<find_cluster_in_range distances="$clusterstable" multiple="true" object="$range" mindistance="$minsell" maxdistance="$maxsell"/>
<set_value name="$sellspaces" exact="$clusterstable.keys.sorted" /> <!-- sort them -->
<remove_value name="$clusterstable"/>
<do_if value="$excludedsectors != null">
<do_if value="$excludedsectors.count gt 0">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Sector Restrictions detected!'" output="false" append="true" />
</do_if>
</do_if>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Sectors in Range Buy: '+$buyspaces.count+' Sell: '+$sellspaces.count" output="false" append="true" />
<!-- What wares do we currently have, lets try to sell them to the highest bidder. -->
<set_value name="$Cargo" exact="this.ship.cargo.list" />
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Scanning CargoBay: '+$Cargo.count" output="false" append="true" />
<do_if value="$Cargo.count gt 0">
<do_all exact="$Cargo.count" counter="$ware">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'CargoBay Contains: '+$Cargo.{$ware}+' Amount: '+this.ship.cargo.{$Cargo.{$ware}}.count"/>
</do_all>
<create_list name="$buyoffers" />
<do_all exact="$buyspaces.count" counter="$sector">
<do_if value="$buyspaces.{$sector} != null">
<do_all exact="$Cargo.count" counter="$ware">
<find_buy_offer tradepartner="this.ship" space="$buyspaces.{$sector}" result="$buyers" wares="$Cargo.{$ware}" multiple="true">
<match_buyer>
<match_relation_to object="this.ship" relation="enemy" comparison="not"/>
<match tradesknownto="this.owner"/>
</match_buyer>
</find_buy_offer>
<wait exact="1ms" />
<do_all exact="$buyers.count" counter="$buy">
<append_to_list name="$buyoffers" exact="$buyers.{$buy}"/>
</do_all>
</do_all>
</do_if>
</do_all>
<do_if value="$buyoffers.count gt 0">
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Buy Offers found: '+$buyoffers.count" output="false" append="true" /> -->
<set_value name="$Grofit" exact="100" />
<set_value name="$GrofitFound" exact="false" />
<do_all exact="$buyoffers.count" counter="$buy">
<set_value name="$buyoffer" exact="$buyoffers.{$buy}" />
<set_value name="$buycost" exact="$buyoffer.unitprice/100" />
<do_if value="$buyoffer.buyer.owner == faction.player">
<set_value name="$buycost" exact="$buyoffer.ware.averageprice/100*$discount2/100" />
</do_if>
<set_value name="$Amount" exact="[this.ship.cargo.{$buyoffer.ware}.free,$buyoffer.amount].min"/>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Buy Offer for: '+$buyoffer.ware+' at '+$buyoffer.buyer.knownname+' Amount: '+$Amount+' Grofit: '+ $Amount*$buycost" output="false" append="true" />
<do_if value="$Grofit lt $Amount*$buycost">
<set_value name="$Grofit" exact="$Amount*$buycost" />
<set_value name="$GrofferBuy" exact="$buyoffer" />
<set_value name="$Gromount" exact="$Amount" />
<set_value name="$GrofitFound" exact="true" />
</do_if>
<wait exact="1ms" />
</do_all>
</do_if>
<do_if value="$GrofitFound">
<do_if value="$returnhome">
<do_if value="$home.isclass.space">
<!-- FlyBack to home sector -->
<get_safe_pos object="this.ship" radius="this.ship.size" angle="45deg" result="$safepos" sector="$home"/>
<create_order object="this.ship" id="'MoveGeneric'" immediate="false">
<param name="destination" value="$home" />
<param name="position" value="$safepos" />
<param name="endintargetzone" value="true" />
</create_order>
</do_if>
<do_else>
<!-- FlyBack to home station -->
<create_order object="this.ship" id="'DockAt'" immediate="false">
<param name="destination" value="$home" />
</create_order>
</do_else>
</do_if>
<do_if value="$GrofferBuy">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Selling '+$Gromount+' '+$GrofferBuy.ware+' for '+$GrofferBuy.unitprice/100+' at '+$GrofferBuy.buyer.knownname" output="false" append="true" />
<write_to_logbook category="upkeep" title="'TaterTrader: '+this.ship.knownname" interaction="showonmap" object="this.ship" money="$Gromount*$GrofferBuy.unitprice" text="'Queuing up Trade order for '+$Gromount+' '+$GrofferBuy.ware+' to sell at '+$GrofferBuy.unitprice/100+' I should earn '+$Gromount*$GrofferBuy.unitprice/100+' in profit.'" />
<create_trade_order name="$GrofferBuy" object="this.object" tradeoffer="$GrofferBuy" amount="$Gromount" immediate="true" />
<resume label="orderloop" />
</do_if>
</do_if>
<remove_value name="$buyoffers"/>
</do_if>
<!-- Now that we got rid of our cargo clutter lets start trading. -->
<!-- Find trade deals we can run. -->
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Searching for trade deals'" output="false" append="true" />
<set_value name="$Grofit" exact="100" />
<set_value name="$GrofitFound" exact="false" />
<do_all exact="$warebasket.count" counter="$ware">
<!-- First grab the offers for the ware from all the sectors in range.. -->
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Checking offers for ware: '+$warebasket.{$ware}" output="false" append="true" />
<create_list name="$buyoffers" />
<do_all exact="$sellspaces.count" counter="$sector">
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Scanning Sector for buy offers: '+$sellspaces.{$sector}" output="false" append="true" /> -->
<set_value name="$notexcluded" exact="true" />
<do_if value="$excludedsectors != null">
<do_if value="$excludedsectors.count gt 0">
<do_all exact="$excludedsectors.count" counter="$excluded">
<do_if value="$sellspaces.{$sector} == $excludedsectors.{$excluded}">
<set_value name="$notexcluded" exact="false" />
</do_if>
</do_all>
</do_if>
</do_if>
<do_if value="$notexcluded">
<find_buy_offer tradepartner="this.ship" space="$sellspaces.{$sector}" result="$buyers" multiple="true" wares="$warebasket.{$ware}">
<match_buyer>
<match_relation_to object="this.ship" relation="enemy" comparison="not"/>
<match tradesknownto="this.owner"/>
</match_buyer>
</find_buy_offer>
<wait exact="1ms" />
<do_all exact="$buyers.count" counter="$buy">
<append_to_list name="$buyoffers" exact="$buyers.{$buy}"/>
</do_all>
</do_if>
</do_all>
<create_list name="$selloffers" />
<do_all exact="$buyspaces.count" counter="$sector">
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Scanning Sector for sell offers: '+$buyspaces.{$sector}" output="false" append="true" /> -->
<set_value name="$notexcluded" exact="true" />
<do_if value="$excludedsectors != null">
<do_if value="$excludedsectors.count gt 0">
<do_all exact="$excludedsectors.count" counter="$excluded">
<do_if value="$sellspaces.{$sector} == $excludedsectors.{$excluded}">
<set_value name="$notexcluded" exact="false" />
</do_if>
</do_all>
</do_if>
</do_if>
<do_if value="$notexcluded">
<find_sell_offer tradepartner="this.ship" space="$buyspaces.{$sector}" result="$sellers" multiple="true" wares="$warebasket.{$ware}">
<match_seller>
<match_relation_to object="this.ship" relation="enemy" comparison="not"/>
<match tradesknownto="this.owner"/>
</match_seller>
</find_sell_offer>
<wait exact="1ms" />
<do_all exact="$sellers.count" counter="$sell">
<append_to_list name="$selloffers" exact="$sellers.{$sell}"/>
</do_all>
</do_if>
</do_all>
<remove_value name="$sectorexcluded"/>
<!-- Clear out the best sell variable if it has something in it. -->
<set_value name="$bestrating" exact="0" />
<set_value name="$bestsell" exact="0" />
<!-- Are there any possible deals withen our search area? -->
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'I found these offers nearby, Buy: '+$buyoffers.count+' Sell: '+$selloffers.count" output="false" append="true" />
<do_if value="$buyoffers.count and $selloffers.count">
<!-- Now we compare the offers and try to get a decent deal -->
<shuffle_list list="$buyoffers"/>
<sort_trades name="$buyoffers" tradelist="$buyoffers" sorter="relativeprice"/>
<do_all exact="$buyoffers.count" counter="$buy">
<set_value name="$buyoffer" exact="$buyoffers.{$buy}" />
<do_if value="$buyoffer.available">
<set_value name="$buycost" exact="$buyoffer.unitprice/100" />
<do_if value="($buycost == 0) or ($buyoffer.buyer.owner == this.ship.owner)">
<set_value name="$buycost" exact="$buyoffer.unitprice/100*$discount2/100" />
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'This buyer has the same owner as us, or the buy price is '+$buycost" output="false" append="true" /> -->
</do_if>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="' Scanning buy offer for ('+$buyoffer.ware+') at ('+$buyoffer.buyer.knownname+') Price: '+$buycost+' Faction: '+$buyoffer.buyer.owner+' Restriction: '+$buyoffer.restriction.faction+' Raw OfferPrice: '+$buyoffer.unitprice/100+' Relation: '+$buyoffer.buyer.relationto.{this.ship.owner}" output="false" append="true" />
<!-- If fast trade is enabled, and we already scanned for the best sell offer -->
<do_if value="not $fasttrade or $bestsell == 0">
<do_all exact="$selloffers.count" counter="$sell">
<set_value name="$selloffer" exact="$selloffers.{$sell}" />
<do_if value="$selloffer.available">
<set_value name="$sellcost" exact="$selloffer.unitprice/100" />
<do_if value="$selloffer.seller.owner == this.ship.owner">
<set_value name="$sellcost" exact="$selloffer.unitprice/100*$discount/100" />
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Raw Avg: '+$selloffer.ware.averageprice/100+' Discount: '+$discount/100+' equals '+$sellcost" output="false" append="true" /> -->
</do_if>
<set_value name="$spendablemoney" exact="player.money/100" />
<do_if value="this.ship.commander">
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'I have a commander!'" output="false" append="true" /> -->
<set_value name="$spendablemoney" exact="this.ship.commander.money/100" />
</do_if>
<do_if value="$sellcost gt 0">
<set_value name="$Amount" exact="[this.ship.cargo.{$buyoffer.ware}.free,$buyoffer.amount,$selloffer.amount,$spendablemoney/(2*$sellcost)].min"/>
<set_value name="$Ratio" exact="$selloffer.ware.averageprice*100/$selloffer.unitprice"/> <!-- Lets attempt to make a value rating for each sell offer, so we can optimise the search. -->
<set_value name="$Rating" exact="[this.ship.cargo.{$buyoffer.ware}.free,$selloffer.amount,$spendablemoney/(2*$sellcost)].min*$Ratio"/>
</do_if>
<do_else>
<set_value name="$Amount" exact="[this.ship.cargo.{$buyoffer.ware}.free,$buyoffer.amount,$selloffer.amount].min"/>
<set_value name="$Ratio" exact="$selloffer.ware.averageprice*100/$selloffer.unitprice"/>
<set_value name="$Rating" exact="[this.ship.cargo.{$buyoffer.ware}.free,$selloffer.amount].min*$Ratio"/>
</do_else>
<set_value name="$CheckGrofit" exact="false"/>
<do_if value="not $bypass and (($selloffer.seller.owner == this.ship.owner) or ($buyoffer.buyer.owner == this.ship.owner))"> <!-- We're dealing with one of our owners stations, take special care here. -->
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Our owner ones one of these stations, additional checks are inplace.'" output="false" append="true" /> -->
<!-- Try to allow trade between owned stations regardless of restriction. -->
<do_if value="$selloffer.restriction.faction == $buyoffer.buyer.owner">
<set_value name="$CheckGrofit" exact="true"/>
</do_if>
<do_if value="$buyoffer.restriction.faction == $selloffer.seller.owner">
<set_value name="$CheckGrofit" exact="true"/>
</do_if>
<do_if value="$buyoffer.restriction.faction == $selloffer.restriction.faction">
<set_value name="$CheckGrofit" exact="true"/>
</do_if>
<do_if value="$selloffer.restriction.faction != null">
<set_value name="$Rating" exact="$Rating/2"/> <!-- Disadvantage restricted trade deals when calculating the bestdeal for fast trade. -->
</do_if>
</do_if>
<do_else> <!-- We arnt trading with a station our owner owns, no restrictions go! (Or maybe they dont care and allowed free trading for this ship.) -->
<set_value name="$CheckGrofit" exact="true"/>
</do_else>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="' Scanning sell offer for ('+$buyoffer.ware+') at ('+$selloffer.seller.knownname+') Faction: ('+$selloffer.seller.owner+') Restriction: ('+$selloffer.restriction.faction+') Buying at '+$buycost+' Selling at '+$sellcost+' Average: '+$buyoffer.ware.averageprice/100+' Amount: '+$Amount+' Grofit: '+ $Amount*($buycost-$sellcost)+' Ratio: '+$Ratio+' Rating: '+$Rating+' RawSellOffer Price: '+$selloffer.unitprice/100+' Relation: '+$selloffer.seller.relationto.{this.ship.owner}" output="false" append="true" />
<do_if value="$bestrating lt $Rating">
<set_value name="$bestrating" exact="$Rating"/>
<set_value name="$bestsell" exact="$selloffer"/>
</do_if>
<!-- TaterTrader, no your not allowed to buy and sell to the same station. -->
<do_if value="$buyoffer.buyer == $selloffer.seller">
<set_value name="$CheckGrofit" exact="false"/>
</do_if>
<do_if value="$CheckGrofit"><!-- Use a variable inplace of just copy pasting the section below. -->
<do_if value="$Grofit lt $Amount*($buycost-$sellcost)">
<set_value name="$Grofit" exact="$Amount*($buycost-$sellcost)" />
<set_value name="$GrofferSell" exact="$selloffer" />
<set_value name="$GrofferBuy" exact="$buyoffer" />
<set_value name="$Gromount" exact="$Amount" />
<set_value name="$GrofitFound" exact="true" />
<set_value name="$itemcostsell" exact="$sellcost" />
<set_value name="$itemcostbuy" exact="$buycost" />
</do_if>
</do_if>
</do_if>
<wait exact="1ms" />
</do_all>
</do_if>
<do_else><!-- Seems we were told to use the fast trade option. -->
<do_if value="$buyoffer.available and $bestsell.available"> <!-- Had to add a check, as some tradeships managed to cut others off ruining their scan data for the whole ware. -->
<set_value name="$sellcost" exact="$bestsell.unitprice/100" />
<do_if value="$bestsell.seller.owner == this.ship.owner">
<set_value name="$sellcost" exact="$bestsell.unitprice/100*$discount/100" />
</do_if>
<do_if value="$sellcost gt 0">
<set_value name="$Amount" exact="[this.ship.cargo.{$buyoffer.ware}.free,$buyoffer.amount,$selloffer.amount,$spendablemoney/(2*$sellcost)].min"/>
</do_if>
<do_else>
<set_value name="$Amount" exact="[this.ship.cargo.{$buyoffer.ware}.free,$buyoffer.amount,$selloffer.amount].min"/>
</do_else>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="' Scanning sell offer for ('+$buyoffer.ware+') at ('+$bestsell.seller.knownname+') Faction: ('+$bestsell.seller.owner+') Restriction: ('+$bestsell.restriction.faction+') Buying at '+$buycost+' Selling at '+$sellcost+' Average: '+$buyoffer.ware.averageprice/100+' Amount: '+$Amount+' Grofit: '+ $Amount*($buycost-$sellcost)" output="false" append="true" />
<do_if value="$Grofit lt $Amount*($buycost-$sellcost)">
<set_value name="$Grofit" exact="$Amount*($buycost-$sellcost)" />
<set_value name="$GrofferSell" exact="$bestsell" />
<set_value name="$GrofferBuy" exact="$buyoffer" />
<set_value name="$Gromount" exact="$Amount" />
<set_value name="$GrofitFound" exact="true" />
<set_value name="$itemcostsell" exact="$sellcost" />
<set_value name="$itemcostbuy" exact="$buycost" />
</do_if>
<wait exact="1ms" />
</do_if>
<do_else>
<do_if value="not $bestsell.available"> <!-- Make sure our sell offer is still valid. -->
<set_value name="$bestsell" exact="0"/>
</do_if>
</do_else>
</do_else>
</do_if>
</do_all>
</do_if>
</do_all>
<!-- We shouldn't need this bit here, requires proper testing though.-->
<!--
<do_if value="not (this.ship.order == this.ship.defaultorder)">
<create_order object="this.object" id="'TaterTrade'" immediate="true">
<param name="warebasket" value="$warebasket" />
<param name="home" value="$home" />
<param name="minbuy" value="$minbuy" />
<param name="maxbuy" value="$maxbuy" />
<param name="minsell" value="$minsell" />
<param name="maxsell" value="$maxsell" />
</create_order>
</do_if>
-->
<!-- Add a check to make sure the trades are still valid at this point -->
<do_if value="$GrofitFound">
<set_value name="$spendablemoney" exact="player.money/100" />
<do_if value="this.ship.commander">
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'I have a commander!'" output="false" append="true" /> -->
<set_value name="$spendablemoney" exact="this.ship.commander.money/100" />
</do_if>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Checking if we can afford this deal! Money: '+$spendablemoney+' TotalCost: '+($itemcostsell*$Gromount)+' PerCost: '+$itemcostsell" output="false" append="true" />
<do_if value="$spendablemoney gt ($itemcostsell*$Gromount)">
<do_if value="$GrofferSell.available and $GrofferBuy.available">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Buying '+$Gromount+' '+$GrofferSell.ware+' for '+$itemcostsell+' at '+$GrofferSell.seller.knownname+' then '+'Selling '+$Gromount+' '+$GrofferBuy.ware+' for '+$itemcostbuy+' at '+$GrofferBuy.buyer.knownname" output="false" append="true" />
<write_to_logbook category="upkeep" title="'TaterTrader: '+this.ship.knownname" interaction="showonmap" object="this.ship" money="-$Gromount*$itemcostsell*100" bonus="$Gromount*($itemcostbuy-$itemcostsell)*100" text="'Queuing up Trade order for '+$Gromount+' '+$GrofferSell.ware+' Buying at '+$itemcostsell+' to sell at '+$itemcostbuy+' I should earn '+$Gromount*($itemcostbuy-$itemcostsell)+' in profit.'" />
<create_trade_order name="$GrofferSell" object="this.object" tradeoffer="$GrofferSell" amount="$Gromount" immediate="false" />
<create_trade_order name="$GrofferBuy" object="this.object" tradeoffer="$GrofferBuy" amount="$Gromount" immediate="false" />
</do_if>
<do_else>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Looks like the trade were looking at is no longer available.'" output="false" append="true" />
</do_else>
</do_if>
<do_else>
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'We cant afford this deal anymore'" output="false" append="true" />
</do_else>
</do_if>
<do_if value="$returnhome">
<do_if value="$home.isclass.space">
<!-- FlyBack to home sector -->
<get_safe_pos object="this.ship" radius="this.ship.size" angle="45deg" result="$safepos" sector="$home"/>
<create_order object="this.ship" id="'MoveGeneric'" immediate="false">
<param name="destination" value="$home" />
<param name="position" value="$safepos" />
<param name="endintargetzone" value="true" />
</create_order>
</do_if>
<do_else>
<!-- FlyBack to home station -->
<create_order object="this.ship" id="'DockAt'" immediate="false">
<param name="destination" value="$home" />
</create_order>
</do_else>
</do_if>
<label name="orderloop" /> <!-- Lets try handling our orders ourselfs -->
<!-- <debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'OrderLoop Is go'" output="false" append="true" /> -->
<do_if value="not (this.ship.order == this.ship.defaultorder)">
<do_if value="this.ship.order.state == orderstate.ready">
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Commencing Order: '+this.ship.order.id" output="false" append="true" />
<run_order_script order="this.ship.order"/>
<!-- <resume label="orderloop" /> -->
</do_if>
<do_if value="this.ship.order.state == orderstate.started">
<wait exact="50ms"/>
<!-- <resume label="orderloop" /> -->
</do_if>
<!-- If the order state is something else, print it out and exit the loop. -->
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Order: '+this.ship.order.id+' State: '+this.ship.order.state" output="false" append="true" />
<!-- <resume label="orderloop" /> -->
</do_if>
<!-- For some reason, we never get to this point. I think when it reachs the docking sequence the game aborts executing this script. -->
<debug_to_file name="this.ship.knownname" directory="'TaterTrader'" text="'Orders Should of finished by now.'" output="false" append="true" />
<wait exact="10ms"/>
<resume label="start" />
</actions>
</attention>
</aiscript>