X4: Foundations

X4: Foundations

158 ratings
RandomTrade
2
2
2
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
Updated
106.232 KB
16 Mar, 2019 @ 5:00am
19 May, 2023 @ 12:21pm
27 Change Notes ( view )

Subscribe to download
RandomTrade

Description
Random Trader: Randomized trade logic

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.
213 Comments
Algebraist 28 Jul, 2024 @ 1:59am 
Is this the same file as the Nexus file or the Nexus file very out of date?
I'm using the GOG version of X4 so I can't download off the workshop
Hebrux 23 Jun, 2024 @ 12:19pm 
thank you
Andro  [author] 23 Jun, 2024 @ 12:09pm 
no, you should not assign them to the stations, I would just use fleets, use the mass assignment feature, its explained in randomsupply description at the bottom
Hebrux 23 Jun, 2024 @ 7:57am 
Thanks! I didn't know randomSupply could be used to sell my wares. Can I assign the ships that are randomly supplying to my stations so that they are not cluttered in my unassigned ships menu?
Andro  [author] 23 Jun, 2024 @ 5:45am 
No, use RandomSupply instead
Hebrux 22 Jun, 2024 @ 7:33pm 
can I use this to sell the wares my station produces?
Andro  [author] 8 Apr, 2024 @ 10:11am 
mods are old, I wrote them when the game released because the logic back then was really bad. Now the vanilla logic is actually pretty decent, so you actually don't need this one later in the game, like when you have 5 trade ships or so you can start using the vanilla logic.
randommine is difficult, I thought about discontinuing the mod because I never could get it run like I wanted after ego introduced ore depletion mechanic.
OtterSwag 7 Apr, 2024 @ 1:24pm 
This and RandomMine both couse periodic stutters in my games. both on 6.4 and 7.0 it runs fine 130 FPS then spikes with 1-4 FPS if any of these mods are enabled. any ideas? only me?
Rainer Zufall 30 Nov, 2023 @ 12:01pm 
Thanks for your "random" mods. I always found the vanilla logic too simple and restrictive or simply too much micromanagement with repeat.
So thanks again! :steamhappy:
Starsong 3 Aug, 2023 @ 3:36pm 
This doesn't work with mimic, so can't seem to form fleets of trading ships to Random Trade. Ships just generate errors