X4: Foundations
0 of 0

File information

Last updated

Original upload

Created by

Language

Uploaded by

Language44

Virus scan

Safe to use

Tags for this mod

About this mod

Random Trader: Randomized trade logic

Permissions and credits
Mirrors
V1.20
- Added fleet inner communication to prevent many ships from searching for trades at the same time when they can't find anything
(this improves the performance in the case that ships are not occupied)
- Implemented trade rules
- Added ship idcode to the logfilename

V1.17
- fixed a bug where the update subordinates option would assign its own default order even to ships of not same shiptype or not assigned to defend.

V 1.16
- added split wares to preset lists
- moved logbook entry to "upkeep" section (as requested by some users)
- added option to update subordinates (only affects subordinates running RandomTrade already, no effect on freshly added subordinates having "defend" order), kicks in when script of command ship restarts.

V1.15
- split factions (Free Families and Zyarth Patriarchy) are no longer ignored

V1.13
- last patch did not help resolving the vanilla AI take over issue. Changed the mod in this version. Ships will not get assigned as subordinate to stations now, if you use the feature to copy behavior to subordinate, they will stay as fleet (just visual effect in your list, behavior not affected)

V1.12:
- fixed a bug where (when playing 3.0 beta) the vanilla AI would take over ships default order.

V1.11:
-added more wares to categories: biowares/foodwares, ship building wares and hitech wares.
-fixed a bug, where your ship would see buy offers from not unlocked shady guys (illegal wares).

V1.09:
- Improved the free trading logic, L sized ships make better decisions now. Particularly: they take trade runs with less then full cargo better into account. This brought a little bit of postprocessing into the script though, so it might be a little bit slower and a have tiny bit worse performance.

V1.08:
- fixed a bug, where Vanilla AI would randomly take over subordinate ships.

V1.07:
- fixed a bug, where the ship could not create a trade route between factions having war with each other (introduced in 1.06)
- improved the equation for better behavior in early game (related to profitscale slider)

V1.06
- implemented blacklists, the only exception here is the home station in station trader mode, the ship will trade with that station regardless
if its sector is blacklisted or not.
- Fixed a bug, where the ship could buy from and sell to the same station.

V1.05
- New feature: copy the behavior and settings to subordinate ships of the same shiptype (e.g. M-freighter to M-freighter). To use just assign
one or multiple ships per rightclick choosing "[standard behavior] defend" and wait for the script to restart.

V1.03:
 - fixed some error messages appearing in the debug log.

V1.02:
- added a notification and a logbook entry for the case that your ship cant get rid of wares in cargo
- fixed a bug, where your ship would search for trades and even qeue 0-amount-trades if cargo is full and could not get rit of wares in cargo

V1.01: fixed some minor bugs

Target:
Fast, little impact on performance.
Specialized on free trading but also support station trader mode.
Based on the management skill of the pilot.

Core idea:
Precalculate the minimum requirements (mainly profit) for a trade deal and accept
the first found trade offer, which fulfill those requirements.

General trade rules/behavior/features:
Never use trades, which are not known to the player.
Never buy from/sell to trade owners, who is hostile to the ship or located in hostile territory.

The AI has 3 modes:
Mode 1: if cargo is not empty, try to sell the wares inside it:
Get the list of the wares and shuffle it (randomize). Then do 5 attempts
for each ware to find a buyer, start searching for a buyer, who pays min
precalculated relative price and wants at least the same amount as in
the cargo of the ship, the precalculated min relative price is based on
management skill of the pilot (1 star = 0.4; 5 star = 0.7). with each of
the 5 attempt (if it can’t find a buyer): the min relative price
decreases linearly to -1 (which is minimum), the min amount decreases by
20% and the max gates distance increases by 1.
Mode 2: station trader mode:
This has the most complicated logic and consists of 2 sub modes:
2.1. Supplying the home station or its buildstorage with wares needed for
production or construction: get a list of all buy offers from home
station, then shuffle it. For each buy offer it will first search for a
sell offer from player own stations, which ask for lower or same price
as the buy offer. The amount is also variable; it is scaled based on the
stockratio (percentage of ware still available in the home stations
storage). If station completely runs out of the ware, the min amount is
25% of the ship's cargo capacity or the full buy amount, whichever is
fewer; if the buyer is a buildstorage however, the min amount is not
decreased down to the mentioned 25%. If no player sells for any buy
offers are found, the script searches for an npc seller. Here it does 2
attempts, the first one will search for offer with max relative price
based on the management skill of the pilot or the station manager
(whichever is higher) (1 star = -0.4; 5 star = -0.7) if it is lower than
the relative price of the actual offer. If nothing found, the second
attempt will search for the max price of the buy offer, the amount is
always ship’s cargo capacity or the amount of the buy offer (whichever
is lower) here.
2.2. Selling produced wares from the home station: same as in 1 it will get a list of all sell offers and shuffle it, but
here, instead of checking player own station for buy offers, it will
strait search for npc offers. Here it again will do 2 attempts for each
sell offer with the same logic as described above, just inverted, lower
gets higher and -0.4 (-0.7) becomes +0.4 (+0.7). The requirement for
amount here is replaced with total price, which is unitprice x amount,
this will make sure, that it can also pick offers, which have e.g. 10%
lower amount, but 10% higher price.
Mode 3: free trader mode
I programmed this mode first and in my opinion that this is the core of
this script. It performs incredibly, it finds profitable trades within
seconds, even if there are many offers available and you set up the max
gates distance to max. In addition, the more freedom you allow your ship
(the more offers are known to you, the more wares you allow, the more
jumps you allow), the more likely it is to find a profitable offer,
which means faster success with more data instead of the opposite.
The logic is following: precalculate the minimum profit and max relative
price depending on the management skill of the pilot (for the rel price
its: 1 star = -0.4; 5 star = -0.7; and for profit its: 1 star = 5
credits for each m³ cargo capacity of the ship and 5 star = 9
credits/m³). Then shuffle the list of the allowed wares to trade. Then
for each ware search for a sell offer with calculated max relative
price, if found a sell offer, search for a buy offer, which would bring
the minimum profit. With minimum said here, it is possible, that it
finds an offer, which just fulfills the requirement, but it is also
possible, that it finds the best possible offer for more than double of
the minimum profit, even if your pilot has 1 star management, it’s all
RNG. If nothing found continue with the next ware in list.

UI explanation:
1: Trader Home: here you can select the traders home, it has operative
when you set the home to a station and/or you check HomeBound. The main
two classes you can set as a home are sectors and stations.When you want
to build a station, you create a plot on the map; this plot has the
class “object”. Only after the first module finished construction it
gets “station”. I also allowed selection of “object” in this, in order
for the ship to be able to supply build storage even before the first
module is finished construction. If you set up “object” as your home,
remember to transfer credits as soon as it becomes station (after first
module built), otherwise your ship will stop trading, because of no
money.
2: HomeBound: if you toggle this, your ship will trade only around the home sector with the max allowed gates distance you set up
(look below). If you don't toggle it, the trader will be free and will
search for offers always around his current position.
3: Max gate distance to buy: how many gates are we max allowed to pass for buying stuff?
4: Max gate distance to sell: how many gates are we allowed to pass for selling stuff?
5: Ignore Restriction: only operative in station trader mode, will buy
from and sell to npc stations/build storages even if you restrict the
offer in the logical overview of your station.
6: Station Trader: only if this is toggled and home is not a sector (station or “object”),
the ship will start supplying your station/buildstorage and/or
distributing wares from your station.
7: Force Own Sells: this is operational in mode 2.1, when toggled, the ship will ignore Max gate
distance to buy while searching for player own stations, which sell the
wares needed to supply home station.
8: Add wares: always operational except the mode 1.
9: Copy Presets: when this is toggled, the script checks if any of the
presets (below) are toggled and adds wares to the list (above) on its
own, if not toggled, the lower 8 checkboxes get ignored. Is also ignored
if the warelist is not empty.
10: Scale Min Profit: this is operational everywhere, scales the requirements by %, e.g. you set it to
10%, then the relative price becomes -0.04 instead of -0.4 and -0.07
instead of -0.7; and the profit becomes 0.5 credits/m³ instead of 5 and
0.9 instead of 9.
11: Buy before X% filled (Station Trader): this is operational only in station trader mode. The ship will try to fill the
wares needed for production on home station at least up to given here
percentage, but will always try to fully use up the ship’s cargo.
12: Sell after X% filled (Station Trader): same as 11, just for selling
wares. If the station has less % of the producing wares in stock than
given here, the ship will ignore them, will also try to fully use up the
ship’s cargo. This number is ignored in mode 2.1, when your other
suppliers of your other stations need the ware, which this station
produces.
13: Allow all factions: similar to 9, just with factions, when this is toggled, the lower 10 checkboxes are ignored.

Remember, this script stops as soon as it finds and creates a trade opportunity.
It does not check for more profitable trades. The profit may vary from
trade to trade (RNG). It is fast; if your ship does not find any trades
within 15-30 seconds, this means there are no trades possible, so either
make more trades visible, give the trader more freedom (wares, jumps)
or scale down the profit requirement.