[MOD] Mules and Warehouses v4.0

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

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

Post Reply
LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

[MOD] Mules and Warehouses v4.0

Post by LegionOfOne » Thu, 6. Jun 19, 20:41

Changelog :
Show
- v4.0 (2019/06/18): Two new Mules just arrived into the family : the TravelMule and the DistriMule ! One handles selling from your warehouses to NPCs, the other equalising wares between your warehouses.
Keep in mind they are still newborns. Be gentle with them and report any strange behavior.

New : from now on, all Mules will stay docked between trades ! No more useless docking-undocking.
I had to replace all the contents of orders.base.xml to do it, which may cause compatibility issues with other mods :
Any mods that alters that same file and comes before alphabetically, will see their changes overwritten.
Any mod that comes after alphabetically will overwrite my change.
If that happens, let me know, compatibility can be arranged !

Various fixes to the StationMule, including :
Rewrite of the 'Make Target Warehouse' option, so that it doen't use buy and sell offers anymore, but lists of products and tradewares instead. Translation : some bugs fixed.
Logic to forbid a StationMule from ever trading between two warehouses.


- v3.5 (2019/06/15): Complete rewrite of trade logic.
I realized that sorting trades only work on amount and volume, and sometimes not even that, because the variables behind have some strange behaviors, and some are not calculated correctly (like stocklevel).

So I wrote my own logic to choose which need to fullfill :
When "Seller priority" is activated for a trade, I still use volume to sort, that works well with sell offers.
But, when it is not activated, and in every case of in-cluster trade, I now sort wares according to how empty the storage is in %, which I calculate myself from reserved storage and amount of ware in cargo.
Check the description for details. I'm really happy about this, I was already trying to get this behavior back when X4 was in v1.4...

I changed the rules for low-cargo trades, to always allow a low-cargo trade when it is to supply the last few units of a ware used to build a station, or station drones.

I made a small but important change : now, when selling wares to one of your stations, Mules will not consider the quantity of wares the station can buy (variable amount) but instead the quantity it would buy if it had the budget (variable desiredamount). Not only does it solve a number of vanilla bugs and annoyances, it allows the delivery of wares to stations with no money !
It is very important for your warehouses : now, you can prevent a warehouse from ever buying wares from a NPC by setting the station budget to 1 credit. The 'amounts' of every buy offer on this station will be 0, so no regular trader will ever sell to the station. But Mules will... Then you can allow your warehouse to trade with other factions, which will mean to sell to other factions only !
Your warehouse will start to earn money from these sales, but as soon as the budget goes above 2x what you gave initially, they will transfer the excess to you. So the station budget will stay at 2 credits, and you will get all the money from the sales quickly in your own account.

The mod is now named Mules and Warehouses. It is more concise, and it has a swanky acronym : MaW !
I can tell you're all really excited about that last one.

- v3.32 (2019/06/13): Improvements to in-cluster trade logic :
Now, Mules will consider the needs of shipyards and factories at the same time.
Since shipyards generally have larger storage capacity, their buy offers are larger, and they should still get priority.
But once their needs are smaller, your mules will deliver to your factories as well, without waiting for your shipyard to be full first. The needs of station construction still have absolute priority, and the 'needs' of warehouses always come last.

Improved logic when a factory needs a ware, but produces it as well.
Now, the Mules will not bypass the volume of the buy offer set by the manager in that specific case, which is usually zero for a good reason : the factory can supply itself.

- v3.3 (2019/06/13): Restored the feature of the 3.1 update, lost in v3.2
Other small fixes and code reorganisation following v3.2
The Mules will now deliver wares to a station even if the amount of that ware requested by the manager is zero, as long as the station still actually has reserved storage for that ware.
This solves a number of vanilla bugs caused by strange manager behavior.

- v3.2 (2019/06/12): Fixes to some logic that could allow trades with zero quantity.
Fix for an issue that prevented in-cluster trade to 'see' all player trades properly.
Fix for 'Create Trade' option in cases when warehouse stops buying because stores are full : will no longer try to add these wares at every run.

Mules now handle having extra cargo, like when a Sale cancels after they already bought the wares, or if you assign them to Mule duty while their cargo is not empty. They will sell it anywhere in the Source station's cluster before resuming regular Mule duties. If it cannot sell the ware, it will idle.

Updated choice of trade partner when trading in-cluster :
Mules will now sell to your construction sites, then if no valid trade to your shipyards, then to your factories, then your warehouses. They will as usual choose the offer with the largest volume first.
They will buy from your factories first, then your warehouses.
Other back-end optimisations of the code, the script is back at 300 lines in all. Easier debug and better performances

- v3.1 (2019/06/10): Small fix : Mules now always allow the trade of energy cells when trading with station build storages.

- v3.0 (2019/06/09): Added ability to create Warehouses and Trade Stations !
Also added new default behavior : now, when idling (no trade between Source and Target), the Mules default to trade in the whole cluster of the Source station, only between player objects.
Mules supply station build storages first, then all your stations.

- v2.0 (2019/06/08) : first upload on this thread, previous versions on the original Mule's thread
Congratulations, you just found what will soon be your favourite trade script collection. 8)
It is based on the wonderful original Station Mule by leecarter, with "a few" extra options on top.

Now also available on the Steam Workshop, and NexusMods for good measure.
Check the comments here and on Steam for advice.

The original Mules did one thing : take two stations, and trade every ware one of them makes that the other wants to buy, without caring about prices, profits, or ever idling.

That worked so fast and well I wanted Mules to handle everything. So I added a bunch of options, some of them with leecarter's help (thanks !).
Now, you can make Mules trade in priority to station build storages, or trade freely among all your stations in a cluster, and even turn one of your stations into a Warehouse, capable of buying and selling any transportable ware.
They now also handle selling to other factions, and distributing wares between your warehouses, thanks to the two new Mules in the Family.
The description is long, but hang in there, it is worth it !

One note : this script does NOT 'cheat' in any way, by teleporting wares or ships, or knowing more than it should, or any other shenanigans.
Anything the Mules do you could make them do manually, except create warehouses.

New in v4.0: from now on, all Mules will stay docked between trades ! No more useless docking-undocking.

The Station Supply Mule, aka StationMule
Options and Features
Show
These Mules were the first in the Family, and should be your most numerous ones.
They can handle all the supply between your factories, construction sites, and shipyards.
They can even create warehouses, to properly manage your industrial empire.

List of Features :

Here are the options you can activate :
- Source Station : Station from which the Mule will buy
- Target Station : Station to which the Mule will sell
- Assign Source as commander (in 2.5, the orders are no longer editable after it has a commander. To change anything after that, you have to liberate it from its commander before giving a new order.)
- Allow Energy Cells : forbidden by default, except to build stations and drones
- Allow Food and Meds : they are forbidden by default
- Never illegal wares : they are allowed by default
- Only illegal wares : creating the obviously-named DrugMule.
- Two-way trade After buying from Source and selling to Target, do a return trip buying from Target to sell to Source. You can use it to sell your factories' production to a Warehouse, and get the resources you need in return.
- Seller Priority Put priority on wares the Source has the most of, instead of wares the Target needs the most. Useful when Target is a Warehouse.
- Return : Seller Priority does the same for the return trades, when the Mule buys from the Target to sell to the Source.
- No in-system trade : when there is no trade between source and target, the Mule stays idle instead of trading between player-owned stations.
- Allow low-volume : the Mule allows trades below 80% cargo (will always allow it when supplying the last wares to build a station, or station drones).
- Make Target Warehouse see below.


- Default Behavior :
The Mule will first look at the wares the Target needs, and the Source sells, and select one need according to the sorting order explained below.
If Seller Priority is activated, it will instead select the ware with the highest volume on sale on the Source station.
Note : When looking at the needs of one of your stations, it will consider what the station really wants to buy regardless of budget constraints, as long as that station has a budget of at least 1 credit.
This helps solve a few bugs and annoyances linked to vanilla managers, and can be very useful for warehouses, as explained later.

If Two-way trade is activated, it will also look at the wares the Target needs, and the Source sells, and select one as explained below, unless 'Return : Seller Priority' if activated.
By chaining trade orders like that, the Mule can buy on the Source, undock, sell on the Target, then immediately buy on the Target without undocking, and sell on the Source.

If there was no valid trade between Source and Target, your Mule will default to trade between all player-owned objects in the same cluster as the Source, unless 'No in-system trade' is activated.
Cluster means they will cross Accelerator gates but not Jump gates, so they can trade between the three systems of Grand Exchange, the three systems of First Contact, etc.
You can 'force' a Mule to do in-system trade by affecting to your Mule a source or target without trades, like a defense station.

Station build storages get highest priority, and will get supplied first. If no trade is found, then the Mule will try all your stations in the cluster.
The way needs are sorted and selected is as follows :
First, each gets sorted into one of five groups.
1 - the highest priority is wares used in production under 10% storage, that the station does not also produce. Always includes special needs to build drones.
2 - then wares used in production under 25% storage that the station does not also produce.
3 - then wares used in production under 50% storage
4 - then wares used in production under 75% storage
5 - then wares above 75% storage, and all wares not used in production (the offers from warehouses)

The Mule will try to trade every ware in group 1 (in random order, to equalise supply), then group 2, etc, stopping as soon as it finds a valid trade.
To choose where to buy the ware from, the offers in the cluster are sorted by total amount on sale.
If after all that your Mule still has not found a trade, it has earned 10 seconds of idle time before looking again.
Shipyards get no special rules, but they should get adequately supplied like all other factories, now that I finally managed to sort on storage %.

Make Target Warehouse
This option allows the creation of player warehouses and trade stations !
To use it properly, activate the option, and all the other regular options you want your Mule to use, and select as Target Station a Station you own.
That target station must have :
- A dock/pier, container storage modules, and a manager.
- No production modules at all ! This would most likely mess up buy/sell offers calculations and storage allocation for resources/products.

The Mule will automatically create buy and sell orders on the Target Station for every ware the Source Station wants to sell, then start the regular work of a Mule.
For that reason, no need to have more than one Mule with that option activated between each factory and your warehouse.

You will be able to change the sale price of every ware in the Warehouse's Logical Overview once there is at least one unit of that ware in stock.
The buy price, unfortunately, cannot be changed or even seen in the Overview, so it is locked on automatic pricing.
That is a problem if you want your warehouse to sell to other factions, but not buy from them.

Important note :
Contrary to what I said previously, the Warehouse needs to have a budget large enough to publish buy offers, 1 or 2 credits is not enough.
Make sure your warehouse is publishing buy offers by right-clicking it, selecting "trade with", and seeing if there are amounts that the warehouse buys, or just an empty column.
Boost the budget until the buy offers appear.

You can use Mules or other trade scripts to buy from your Warehouse and sell to foreign stations, even if the Warehouse is not allowed to trade with other factions directly.
A vanilla autotrader roaming aroung could work, but a vanilla station trader assigned directly to trade for the warehouse will not. I have not tested other scripts, so I do not know precisely which one works.

If you need precise control over buy offer prices, there is an UI mod to be able to see and change them : my version of the Player-Owned Warehouses mod.


Mules work essentially the same between player-owned stations and when trading with NPC stations.
But remember : they do not look at prices AT ALL, let alone calculate profits. They do not care. You told them to buy somewhere and sell somewhere else, they do it.
They are perfect little drones who never think, just relentlessly look for a trade without useless calculations.
If it involves a NPC station, the Mule will pay or get paid through its commander's account, or yours if it has no commanding station.

The Ware Distribution Mule, aka DistriMule
Options and Features
Show
This simple Mule is meant to distribute wares between your warehouses, so that every warehouse has at least some of every ware.
That way you can use a network of warehouses to supply factories in various systems.

It offers you a few options to control its behavior :
- Source Warehouse : the main Warehouse from which this Mule will trade
- Min Storage : the minimum storage % of every ware that the Mule will try to keep on every warehouse
- Static Storage : Mules will not take wares from a warehouse if storage % is under this value
- Max Storage : Storage % over which Mules will always try to bring the ware to another warehouse, as long as that warehouse has at least 10% less storage filled.
- Wares : optionnal, if you do not give a list all wares will be traded
- Target Warehouses : list of other Warehouses to trade with

The way this Mule looks for a 'deal' is simple :
First, it will look for all wares it can sell from the Source, starting with the largest volumes on sale.
It will then look for buyers among the Targets.
To be a valid trade :
either the storage % of that ware on the Source must be over 'Static Storage', and the storage % on the target must be below 'Min Storage' ;
or the storage % on the Source must be over 'Max Storage' and greater by 10% than the storage % on the Target (to avoid the same ware moving back and forth)
At this first step, Mules only allow trades over 80% cargo.

If there is a valid trade starting from the Source, the Mule looks for a trade buying from the Target to sell back to the Source.
The same rules apply, except that Mules will allow all trades above 50% cargo.

If there is no valid trade starting from the Source, the Mule will look for a trade between any Target Warehouse and the Source, with the same rules as the first step.
After that they will idle.

The Travelling Salesman Mule, aka TravelMule
Options and Features
Show
This Mule is the only one to care at all about prices and profits. It is the one you will use to sell from your warehouses to every other faction.

It offers you a few options to control its behavior :
- Source Warehouse : the main Warehouse from which this Mule will trade
- Target NPC Shipyards : Target all NPC shipyards, wharfs, and equipment docks in Jump range
- Target All NPC Stations : Target all NPC stations in Jump range
- Max Jumps : Maximum number of gates crossed. The Mule calculates profits but not distance, so use wisely.
- Wares : optionnal, if you do not give a list all wares will be traded
- Customers : optionnal, list of stations to trade with. Will replace the two 'Target' options, and does not care about Max Jumps.

The way this Mule looks for a trade is simple :
It looks at all the wares the Source sells. Starting with the highest volume, it tries to sell to Targets.
Only trades above 80% cargo, as usual.
It will check all buyers of the first ware, and select the best profit he can make.
If no trade has a positive profit, he moves on to the next ware.

The way 'profit' is calculated is based on the sale price of wares on your Warehouse : the buy price of the NPC station must be at least one more to register profit and validate the trade.
So you can manage the sell prices yourself, to fine-tune the behavior of your TravelMules (and extract every drop of profit !), or let the auto-pricing do its job, varing the price in inverse measure to the storage %.

Important : you need to know the trade offers of NPC stations for the Mule to see them, so use satellites or trade subscriptions.

With that beautiful and growing family, you will be able to build an industrial empire far more efficient than anything the AI factions can achieve.
Try to find a good reason not to crush every AI after that...
Don't forget to check out the Mule Family Tree for a vision of the future.

And here it is finally, the Mega Download Link !
and the Rapidgator Download Link ! as an alternative.

Just drop the mod folder in your X4/extensions folder, and delete to uninstall.

Like most scripts, this mod works on existing saves and does not break saves when you remove it.
All that happens when you remove the mod is that your former Mules lose their orders and start idling. Now why would you want that ?
The mod is compatible with all game versions, no update should ever break the script.

Final note : if you are now wondering, "Where will I start my Kingdom ?", check out another of my mods, Nopileos' True Fortune.
It is made to give you the most comfortable player base possible, with a PHQ and all production resources in the Nopileos system.

Have fun !
Last edited by LegionOfOne on Tue, 18. Jun 19, 19:00, edited 82 times in total.

rosssack
Posts: 50
Joined: Fri, 8. Mar 19, 13:44
x4

Re: [MOD] Station Mule Expanded

Post by rosssack » Fri, 7. Jun 19, 13:31

hi download link not working

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Fri, 7. Jun 19, 18:21

rosssack wrote:
Fri, 7. Jun 19, 13:31
hi download link not working
Works for me, but Mega is not accessible in every country.
I just added a rapidgator link, hope it works for you.

Azran_Zala
Posts: 69
Joined: Tue, 13. Mar 18, 01:25
x4

Re: [MOD] Station Mule Expanded

Post by Azran_Zala » Fri, 7. Jun 19, 18:24

If it trades to build storage it really will be my favorite script.

Edit: If possible, may I request, pretty please, if you'd consider adding a config option to allow it 'only' to trade to build storage?
(IE if expanding a ship yard - one may want it to supply hull-parts for the build instead fulfilling the order for a 100k hull parts the shipyard wants to stock)

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Fri, 7. Jun 19, 19:17

Azran_Zala wrote:
Fri, 7. Jun 19, 18:24
If it trades to build storage it really will be my favorite script.
It doesn't trade to build storages at all of now, unfortunately. I'll see what I can do about it, but it seems tricky without altering a lot of the script.

Also : the first upload contained a small stupid typo, that of course broke the mod. It is fixed now, sorry !

rosssack
Posts: 50
Joined: Fri, 8. Mar 19, 13:44
x4

Re: [MOD] Station Mule Expanded

Post by rosssack » Fri, 7. Jun 19, 21:07

nope links still don't work ,im in uk if this helps

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Sat, 8. Jun 19, 16:25

rosssack wrote:
Fri, 7. Jun 19, 21:07
nope links still don't work ,im in uk if this helps
Neither of them ?? Did you all Brexit from the Internet too ?
Sorry, couldn't resist.

What filehosts usually work for you ?

Edit : added a clicknupload link, I hope this one does the trick.

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Sat, 8. Jun 19, 21:27

Azran_Zala wrote:
Fri, 7. Jun 19, 18:24
If it trades to build storage it really will be my favorite script.

Edit: If possible, may I request, pretty please, if you'd consider adding a config option to allow it 'only' to trade to build storage?
Done ! New version posted. See the changes in default behavior. Good news is it added very little complexity to the script, so it is still fast. The script itself is shorter than the description in the OP !
The trade requests of a new station's build storage show up immediately, but when you restart construction on an existing station sometimes it takes a while for the buy wares offers to be 'published', so it takes a while for Mules to see them.
Last edited by LegionOfOne on Sat, 8. Jun 19, 23:49, edited 2 times in total.

rosssack
Posts: 50
Joined: Fri, 8. Mar 19, 13:44
x4

Re: [MOD] Station Mule Expanded

Post by rosssack » Sat, 8. Jun 19, 23:01

LegionOfOne wrote:
Sat, 8. Jun 19, 16:25
rosssack wrote:
Fri, 7. Jun 19, 21:07
nope links still don't work ,im in uk if this helps
Neither of them ?? Did you all Brexit from the Internet too ?
Sorry, couldn't resist.

What filehosts usually work for you ?

Edit : added a clicknupload link, I hope this one does the trick.
hi links working now ,,,the way are government is working on this Brexit it will be 2040

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Sat, 8. Jun 19, 23:51

Well tell me if, at least, my new options work :)

My new (long-term) project, now that my Mules can create warehouses :

The Muliverse (patent and working prototype pending)
A complete overhaul of AI factions (except Xenon and Khaak) to make most of their trade ships into Mules, with one central Warehouse in each cluster which will buy from and supply all of this faction's factories in the cluster.
This should make trading much more efficient and make the game run faster. Maybe we can even reduce the overall number of trade ships in existence and still get a more efficient supply chain.

It's not going to be quick or easy though, so any modder who wants to help is welcome !

Azran_Zala
Posts: 69
Joined: Tue, 13. Mar 18, 01:25
x4

Re: [MOD] Station Mule Expanded

Post by Azran_Zala » Sun, 9. Jun 19, 19:10

LegionOfOne wrote:
Sat, 8. Jun 19, 21:27
Azran_Zala wrote:
Fri, 7. Jun 19, 18:24
If it trades to build storage it really will be my favorite script.

Edit: If possible, may I request, pretty please, if you'd consider adding a config option to allow it 'only' to trade to build storage?
Done ! New version posted. See the changes in default behavior. Good news is it added very little complexity to the script, so it is still fast. The script itself is shorter than the description in the OP !
The trade requests of a new station's build storage show up immediately, but when you restart construction on an existing station sometimes it takes a while for the buy wares offers to be 'published', so it takes a while for Mules to see them.
Wow you are an absolute star my man!

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Station Mule Expanded

Post by LegionOfOne » Sun, 9. Jun 19, 23:23

I appreciate the praise, but the part I'm most proud of is making them create warehouses.

Defaulting to trade in the cluster, including build storages, when idle was surprisingly easy, though it is very useful.
Thank you very much for the suggestion !

Enjoy !

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 14:16
x4

Re: [MOD] Mules and Warehouses v3.5

Post by LegionOfOne » Sat, 15. Jun 19, 15:40

I uploaded a new version, 3.5, that rewrites the trade selection logic.

I have tested it in different situations and it works just as expected so far, but please tell me if anything doesn't work like it should for you.

Now that i rewrote the trade selection entirely, I thinks that Mules might stay in that stable version for a while, barring bug fixes or new ideas.
So, if any of you want to post this mod on other forums, like NexusMods, now you have my permission. [Edit : Uploaded it to Nexus myself]
Standard rules : include links to this page and the Steam Workshop one, tell people to only post comments and bugs on these two, and that I only upload new versions to these two.
Also tell me about it, just so I know.

My next project might be Trade Station Ships, with a special XL trade ship, and a custom Mule to work with it.
Anyone interested in that ? :)
Last edited by LegionOfOne on Sun, 16. Jun 19, 15:17, edited 1 time in total.

Buzz2005
Posts: 1337
Joined: Sat, 26. Feb 05, 02:47
x4

Re: [MOD] Mules and Warehouses v3.5

Post by Buzz2005 » Sat, 15. Jun 19, 20:48

dude :D

finally my hq megaplex with all eq for shipbuilding can exclusively fill all shipyards and wharfs

and then my own shipyard/wharf when I finally build it, the Xenon are getting cocky, Holy Vision and haticvas are barely holding

thank you very much
Fixed credits not being able to claim abandoned ships sometimes getting stuck when boarding operation sometimes getting some owner other than the player builds a station module or surface element.

Ramokthan
Posts: 103
Joined: Fri, 22. Apr 16, 21:25
x4

Re: [MOD] Mules and Warehouses v3.5

Post by Ramokthan » Sat, 15. Jun 19, 23:32

I fail to get that warehouse concept running.

I followed the description and set up a station with storage, docks and no producing modules and have traders from other stations i own set to target the warehouse, two way trade, make target warehouse, but they kinda refuse to deposit the products of the stations in there, well sometimes they do, but not often enough so my production comes to an halt if i dont give them the order manually... am right now close to scrapping that idea bc direct supply worked better....

Post Reply

Return to “X4: Foundations - Scripts and Modding”