Motivation
==========
With the base game, most X4 players have experienced at some point, and at various levels, performance issues when a game is consuming most of the resources available on their rig. At the other end, brilliant modders have pushed the game boundaries to new heights, introducing new features, opening new perspectives, new game-plays and tactics, improving AI capabilities, etc. Even total conversion mods.
Frustration
===========
Alas, my current game has reached a plateau. Since my first game start, I aimed to reach a level where I could think of building an empire. I've been able to eradicate Xenon (it was not easy but it's done) and I currently own all their sectors. Same for the Kaaks, I bottled them in such a way they are now confined in their usual spawning spots and reduced to dust the minute a installation is spawning by one of my local Asgard Battle groups that permanently patrol each of these sectors. Actually I have so many of these battle groups (One Asgard with 6 Osaka configured as "FlakPz" escort) that I don't know how much exactly I have... with 5-6 "Station-buster" specialized battle groups with again one Asgard accompanied by 6 Sym Destroyers (all-ARG plasma turrets). Money is not a problem.
So peace at last in all sectors of the galaxy. I'm at > +25 relations with all factions (+30 with VIG, which I thought it would give me the option of buying another Astrid, but no). Only ALI is stuck at +18.
No, most concerning is what I'm experiencing now (besides low performance when using left or right menus and filters) is the trading levels are (from other factions I means) anemic at best. Whole sectors, once flourishing are now literally regressing... Even Argon Prime now have only 5 stations left... (from >15 they had initially). Their stations are not destroyed by some belligerent faction sneaking while I'm not looking, no. They are simply withering... And, after some time, disappears completely. Even some TEL stations with which I had constant trade exchanges to fuel my local terraforming efforts had gone "out of business" and disappeared.
Hopes
=====
I perused existing economy+finances mods from modders like mysterial, kungfury, ilimuno and others, seeking a way to improve the "I" in the AI without killing my gaming PC's few remaining resources with yet more lengthy XML scripts. In hope to have an open and more dynamic economic model that would be able to generate realistic trading activities, yet aligned with the current situation and chain of events happening in the universe. Given the fact, X4 AI rules are mostly XML scripts (MD and cues) fed to a single-threaded process tied to a 5.1 lua engine, improving the AI by extra-feeding the engine with just bigger, more elaborated scripts, can just translate into a reduced processing capability... That is not good...
Inspiration
=========
Besides X4, I'm a long time fan of Arma2 and Arma3 Series. This franchise introduced with Arma2 (circa 2008) a clever way to reduce resources contentions on a gaming PC (graphics, CPU, disks, memory) through a mechanism called "Headless client". It is still in operation with Arma3 and will most likely (I hope) be refactored to work along with the upcoming new flagship Arma4 (currently sandboxed as "Arma Reforger") as resources contentions still exists and will continue to exists in the future... Even with Intel i9 ddr5 memory, SSD drive...). "How it relates with X4" you may say? Please read along.
The main objective of such "Headless Client" is to offload AI calculations away from the graphics rendering device ie. the gaming PC hosting the game. Which currently have to renders all the 3D graphics, and manage the game's world/universe dynamics. So in essence, the gaming PC keeps the intensive hard work of graphics rendering and delegate chunks of AI-related calculations and evolving universe' dynamics processing to 1..n headless client instance(s). Those headless clients are running on resources provided by other interconnected device(s), such as laptops, family PC, LAN server, locally hosted VMs, etc.). In short, an "headless client" is meshed with the central game PC as a slave processing node.
Multiple individual processing nodes can be tied to a game instance for additional power and provide scalability for multi-player games. MP not being a use-case in X4, but extra processing would indeed be very welcome to support late game phases, or address extensive wars that develop at some point between factions (ARG Vs TER, or Paranid civil war(s), etc.) Some rules are enforced though, in order to preserve the game instance's integrity and reduce potential risks of cheating. An headless client is bounded to one and only one game instance, which doesn't allow arbitrary connections from headless clients once a game is started/running.
Concept
=======
From an technical infrastructure PoV, the idea I'm currently looking at is to deploy (on one LAN server connected with the main gaming PC) a semi-autonomous lua container able to mimic the lua engine running on the game PC. Which could process stuff instead of letting the central engine doing the processing all on its own. Things that could be offloaded and processed asynchronously from the central engine, could be :
- additional/improved MD and cue scripts providing better AI trading choices and rules;
- Better economic ask-bid/availability-scarcity models and calculations for trading items;
- Better AI ships captains routes determinations and waypoints calculations;
- Better Trade items management by Stations managers and resolve once and for all problem of having two different type of solids (or liquids) harvested by miners on one station...
- AI factions station managers could have better economical stances and situational awareness over geopolitical situations among factions to do better choices in sector development and stations improvements (Can't get out of my head Mysterial mods series to fulfill that requirement

- comportment and social interactions, (insert 1..n kuertee mods you like here as examples

- non-ui QoL scripts improvements (kind of a long shot this one...) but anyway lets think having an externalized dynamic filter managed at the sector-level instead of being at the universe level as currently. Recalculate and regenerate dynamically any given sector's short-list of ships for a specific zoom level can hold some gain in performance from the ui.
- etc. --> your suggestions are very welcome
If these objectives are too ambitious, my prime objective is actually to offload only some specific parts of already existing cues and MDs scripts from the base game to another node. Such as good number of the trading scripts or some other well-scoped/defined set of scripts. even that would be beneficial for the gaming PC to use the spared resources to process something else. Sounds nice so far? keep on digging...
Communications between a remote lua container and the base game
===========================================================
First, I want to say that I'm far of being a modder. I'm only starting to begin to comprehend how the game is working internally. but I'm a IT guy specialized in infrastructures and application/components & software integration. So while perusing those very interesting mods and all, I saw a opportunity I wish to share. So better, more knowledgeable people are welcome to pitch in, provide guidance and make suggestion. And if after some discussions it is clear it is not feasible, we simply close the thread and move along. No hard feelings! And will all probably wait for X5, with the hope that future X instalment architecture will be modularised/containerised enough to enable asynch, mesh-enabled, inter-processes collaboration and inter-networking.
Any half-decent PC nowadays can run docker-desktop. So crafting a container as a computing node with the very same lua 5.1 engine provided with the game should be somewhat simple (given I know the intricacies and dependencies between the lua engine, the game objects and how they are managed. Now the question: How communications between the game PC and a given processing node can be handled?
I found two potential answers to that one that I want to share with the community and have your take. Every people in X4 universe that ever took a look at X4 mods and modding has most likely already came across over the truly amazing work of SirNuke python pipe server. I also found an alternate communication handling interesting approach with Alias5's REST API & server POC (both are on github). Unfortunately though, I never had the time to fully dig into that work so far. If someone have that perspective and able to share, I'll happy to read your thoughts in the comments.
I started experimenting with the Python pipe server, which currently uses local pipes. I looked into the code 3-4 months ago and imho only some minor adjustments to the existing code should be required to support named pipes over a network. The capability to read a simple config file is already there, so we can simply add necessary network parameters to the python server prior launch. Parameters like the IP address of the gaming PC running the game, additional IP(s)- one for each headless client(s) members of the mesh network to communicate with, node prioritization, etc. The intent is to package that meshed version python pipe server in the same container as the lua engine, which shouldn't be too difficult either. And because we don't want additional python pipe server(s) to cannibalize already scarce resources on the gaming PC. It would defeat the whole purpose.
BUUUTT!
=======
The hard part is beginning here: According to SirNuke's extensive documentation (kudos to this guy really), the way the communication between the lua engine residing on the gaming PC and the outside world is through an existing dll (debug-ui.dll if my memory is correct). So what I understood so far (please correct me if I'm wrong) is that hack used between the engine and the dll has significant limitations and prone to contentions. So asynchronous communications through that dll toward the Lua Engine wont work with multiple channels/processing nodes. It can still work from a single source, so it is still manageable (under certain conditions).
The best option would be for the dev team to provide a dedicated open dll (and its code) to bridge communications in and out with the central engine. I doubt they have the time and resources to do that with the upcoming sortie of KE DLC. So moving on.
An alternative would be to put a local "master" python server on the gaming PC, which would act as an orchestration tier and funnel both egress and ingress communications with the various processing nodes. And be "The One" to the lua engine able to talk through the dll bridge. That way, it is possible to keep contention risks to a minimum while the python master server handle multiples asynchronous ingress communications, queuing them (if necessary) into an XML stream in an orderly fashion. Because the core of the Pipe server is written in Python, extending the original server to support these new tasks and functions should also fairly easy to do. The drawback is : to offload chunks of AI works and calculations to somewhere else, we have to run another process locally. So for that local master server, due care in its coding will be paramount to keep its resources consumption at its lowest. it would be preferable not to containerized that master server, to avoid the installation of additional stuff, like docker-desktop or any supporting runtime(s).
I'm currently in the experimentation phase to assess the technical feasibility of that aspect. SirNuke if you're reading this, I have some questions and I think only you can shed a light over some implementation intricacies and also identify potential pitfalls that you may see. Thanks in advance
Way ahead
=========
Imho Egress communications are simpler to handle (from the game engine to an external processing node), as the various functions are triggered by cues and MD scripts. I have another concept, this time within the game, to ties such exchanges with game's events, Universe dynamics and trigger in a timely manner these egress communications. Luckily, the aspects of the game I flagged as "candidates" for offloading are, 1) indeed process-intensive (of course); but also, 2) asynchronous in nature. So the game universe can perfectly live without having to wait for an answer from a processing node. That topic will be the subject of my next post.
thanks all,