[QUESTION] Modding AiScript through XML Patch

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

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

Post Reply
ghostyga
Posts: 4
Joined: Sat, 29. Oct 22, 14:35
x4

[QUESTION] Modding AiScript through XML Patch

Post by ghostyga » Thu, 3. Nov 22, 07:08

Hi guys! Right off the bat, sorry for the long and exhausting reading, but I needed to explain everything...
I'm fairly new to the game, and yesterday I was at a space station just managing stuff, and I saw an Incartatura just land on a landing pad to do some trading, it stayed for like 5-10 seconds and took off.
And then I started thinking "man... ships in this game unload cargo crazy fast, maybe I could make a mod to change that...". So I started looking on how to do that. The main idea I had is that the time a ship should take to complete a trade needs to take into account, initially, the ware quantity and volume (or weight, but I think the game doesn't have that data).

Well then, I extracted the game files, looked around to see if I could find where the main trading logic was, and I found out that the aiscripts/order.trade.perform.xml could be what I was looking for, so I started searching for ways to implement a simple debug_text right after the main script calls the execute_trade function. The initial idea, just for testing purposes, was to add a <debug_text> right after the trade function was completed.

I then created the standard mod folder inside '/extensions' directory and setup my mod there, with the following structure:

Code: Select all

- aiscripts/
  - ImprovedTradingTime.xml
- context.xml
The mod file itself has the following code:

Code: Select all

<?xml version="1.0" encoding="utf-8"?> 

<diff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <add sel="//execute_trade" pos="after">
        <debug_text text="'Just a simple debug'" chance="100" />
    </add>
</diff>
The aiscripts/order.trade.perform.xml file has the following content (just what is required to be seen of course):

Code: Select all

...a bunch of code here
        <do_if value="$amount gt 0">
          <!-- now execute the trade -->
          <execute_trade tradeoffer="$tradedeal" tradepartner="this.ship" amount="$amount" result="$traderesult" custom="true" comment="Deal with fixed price" />

>>>>>> I want to debug_text right here <<<<<<

          <do_if value="$isbuying">
            <set_value name="$tradequality" exact="-$tradedeal.relativeprice"/>
          </do_if>
          <do_else>
...a bunch of code here
Well, I added the following to the steam launch properties:

Code: Select all

-skipintro -debug all -logfile log.txt -scriptlogfiles
When I load the game, the mod loads as it should. Although it throws an error saying "Ignoring root node 'diff' in XML file", it's a harmless error as I read here... Anyways...

I created a new save to test everything. When I tell a ship to trade, or even another faction's ship trades, the log don't output my 'Just a simple debug' text, not even in the in-game log window.
Either I'm stupid for not doing things correctly or something else is just not working.

Thank you for your help and time!

kuertee
EGOSOFT
EGOSOFT
Posts: 789
Joined: Sun, 14. Dec 03, 13:05
x4

Re: [QUESTION] Modding AiScript through XML Patch

Post by kuertee » Fri, 4. Nov 22, 03:56

try it with your file renamed to the same filename as the file you're patching. i.e. "aiscripts/order.trade.perform.xml"

then, ensure that your reference to the node that you are patching from is correct.
i.e. if there are more nodes that match "//execute_trade", you may need more specificity to your reference.
e.g. (//execute_trade)[1] to replace the first instance of "//execute_trade".
better still is to include the parent nodes to your reference.

i think, set pos to "before" rather than after.
when a new order is added as priority (either by the aiscript or by the player), the new order will take over and any lines of code after will not execute.
when that new order finishes, and the previous order is still in the queue, it will restart from the beginning.

i hope all that is clear.
Mods: RPG: Reputations and Professions, Social Standings and Citizenships, Crime has Consequences, Alternatives to Death. Missions/NPCs: Emergent Missions, NPC Reactions, Mod Parts Trader, High-sec Rooms are Locked, Hacking Outcomes, More Generic Missions, Waypoint Fields for Deployment. Others: Auto-cam, Friendly Fire Tweaks, Teleport From Transporter Room, Wear and Tear. QoL: Trade Analytics, Loot Mining, Ship Scanner, Signal Leak Hunter, Station Scanner, Surface Element Targeting, etc.

ghostyga
Posts: 4
Joined: Sat, 29. Oct 22, 14:35
x4

Re: [QUESTION] Modding AiScript through XML Patch

Post by ghostyga » Fri, 4. Nov 22, 05:55

First, thank you for trying to help me!
Second, thank you for actually helping me! :D

It turns out that the mod file must have the same name as the file it patches, which makes a lot of sense actually! Which as well was something that got me intrigued, because other mods doesn't do this, and it bugged me, as I was trying to find the connection between the patch and the actual file it patches. At the end, it made me think that all aiscripts get bundled in one single file when the game loads up. I'm happy that is not the case.

But yeah, thank you very much kuertee, I really appreciate it!

j.harshaw
EGOSOFT
EGOSOFT
Posts: 1847
Joined: Mon, 23. Nov 15, 18:02

Re: [QUESTION] Modding AiScript through XML Patch

Post by j.harshaw » Sat, 5. Nov 22, 23:28

pos="after" should be fine in this case since execute_trade doesn't redirect. hm, although it can be interrupted by an event handler since it's a blocking action so best keep that in mind.

have fun!

ghostyga
Posts: 4
Joined: Sat, 29. Oct 22, 14:35
x4

Re: [QUESTION] Modding AiScript through XML Patch

Post by ghostyga » Sun, 6. Nov 22, 21:10

Hm I see... I think I don't really need to use an after, I could be able to add it before just fine and maybe set command action as trading, just to give the player a feedback. Well, thank you for the heads up!

ghostyga
Posts: 4
Joined: Sat, 29. Oct 22, 14:35
x4

Re: [QUESTION] Modding AiScript through XML Patch

Post by ghostyga » Sun, 6. Nov 22, 22:29

One question, does AI add a fair amount of service crew to their ships when they place a build order? I'm concerned about this because my idea was to use service crew amount and level to define the time a ship stay docked doing the trade.

Post Reply

Return to “X4: Foundations - Scripts and Modding”