Author Topic: [MOD] Goods Hauler (Automatically buy/sell/move goods!)  (Read 42146 times)

Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
So I have a new ship design now that doesn't crash into stations, I still think the calculations are off, but at least my mod still works.

A version for the latest version by Evil33: http://www.avorion.net/forum/index.php/topic,2896.msg22268.html#msg22268

Short description: After you've done all the adjusting in this post, you'll have a 'Haul Goods' order on your ship, which will automatically try to figure out what needs to be transported and what can make a profit in the sector you're in. Obviously you need a ship with a captain and some cargo space.

If you want to easy install with commands instead of changing the orders list for ships, then the first part is enough. This gives you a /haulgoods command instead of an entry in the Orders menu. I actually prefer this option myself :)

You can give multiple ships the 'Haul Goods' order, and they will all work together so they don't start doing the same thing. You can see all that at work if you open your console window, quote-key by default.

It only works in your current sector. I mainly use it to run all the supply lines of my factories (0 profit is considered a valid traderoute for my AI :), but I've tested a bit on trade runs in NPC sectors as well, which seems to work.

It produces a fair amount of debug logging, which you can disable at the following place (if you run into problems, turn this back on):
Code: [Select]
function debug_msg(message)
-- if (true) then return end

Easy installation with commands:
The haulgoods_cmd-v0.9.2.zip file contains 3 files:
commands\haulgoods.lua
entity\ai\haulgoods.lua
entity\ai\haulgood_cmd.lua

If you unzip the zip file into the data\scripts directory of your Avorion install directory (generally with an 'extract to..' option), these will be put where they belong. These are all server side, but can be installed on your client side as well for single player.

Usage: Target a ship you want to use as a hauler (needs a captain), open your chat input (enter by default) and type /haulgoods start

The commands are:
/haulgoods start
/haulgoods stop
/haulgoods status

Always make sure you have the right ship targeted (you can do this from your fleet list)

By default debugging is on, and you can follow what's going on in the console window (quote key by default), which should show you exactly what your ship is doing (or more than one if you give more than one orders)

If you want a button in the Orders list of your ship, then follow these instructions:

Installation of a menu entry for 'Orders' on a ship:
Spoiler: show

As always, make a backup before making changes!

Changes to be made to data\scripts\entity\craftorders.lua.

In InitUI(), around line 119 and below make the following changes:

At the end of these lines there might already be some entries extra (from for example the salvage mod) but this is how it should look by default without changes:
Code: [Select]
local splitter = UIHorizontalMultiSplitter(Rect(window.size), 10, 10, 6)

    window:createButton(splitter:partition(0), "Idle"%_t, "onIdleButtonPressed")
    window:createButton(splitter:partition(1), "Passive"%_t, "onPassiveButtonPressed")
    window:createButton(splitter:partition(2), "Guard This Position"%_t, "onGuardButtonPressed")
    window:createButton(splitter:partition(3), "Patrol Sector"%_t, "onPatrolButtonPressed")
    window:createButton(splitter:partition(4), "Escort Me"%_t, "onEscortMeButtonPressed")
    window:createButton(splitter:partition(5), "Attack Enemies"%_t, "onAttackEnemiesButtonPressed")
    window:createButton(splitter:partition(6), "Mine"%_t, "onMineButtonPressed")

Add the following line to the list and change the 6 in the first line to a 7 as well.
Code: [Select]
window:createButton(splitter:partition(7), "Haul Goods"%_t, "onHaulGoodsButtonPressed")
The 7 can be a different number if there are already more entries than the first 6. If that is the case, you need to change the last number 6 in the first line to something other than 7 as well.

For example, the list looks like this in my file:
Code: [Select]
    local splitter = UIHorizontalMultiSplitter(Rect(window.size), 10, 10, 9)

    window:createButton(splitter:partition(0), "Idle"%_t, "onIdleButtonPressed")
    window:createButton(splitter:partition(1), "Passive"%_t, "onPassiveButtonPressed")
    window:createButton(splitter:partition(2), "Guard This Position"%_t, "onGuardButtonPressed")
    window:createButton(splitter:partition(3), "Patrol Sector"%_t, "onPatrolButtonPressed")
    window:createButton(splitter:partition(4), "Escort Me"%_t, "onEscortMeButtonPressed")
    window:createButton(splitter:partition(5), "Attack Enemies"%_t, "onAttackEnemiesButtonPressed")
    window:createButton(splitter:partition(6), "Mine"%_t, "onMineButtonPressed")
window:createButton(splitter:partition(7), "Salvage"%_t, "onSalvageButtonPressed")
window:createButton(splitter:partition(8), "Clean Sector"%_t, "onSalvageAllButtonPressed")
window:createButton(splitter:partition(9), "Haul Goods"%_t, "onHaulGoodsButtonPressed")

Then at this spot:
Code: [Select]
end

function checkCaptain()

copy the following code between the 'end' and 'function'

Code: [Select]
function onHaulGoodsButtonPressed()
    if onClient() then
        invokeServerFunction("onHaulGoodsButtonPressed")
        ScriptUI():stopInteraction()
        return
    end

    if checkCaptain() then
        removeSpecialOrders()
        Entity():addScript("ai/haulgoods.lua")
    end
end

There doesn't seem to be a nice way yet to add commands to a ship from other scripts. If anyone knows how to do this nicely, please let me know.
« Last Edit: January 02, 2018, 11:14:28 PM by Splutty »



Mutiegenesis

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    41
    • View Profile
    • Free Realms - [US-east]
on: March 17, 2017, 02:31:04 AM
Sounds like something my players may enjoy. ServerSide? ClientSide? Do you modify the existing trade AI to prefer player stations for their own routes (if that is how that works), or whole new scripts being processed?



Devious

  • Hero Member
  • *****
  • Status:
    Offline
    Posts:
    579
  • Have you seen Rusty?
    • View Profile
    • Rusty's Universe and Community
on: March 17, 2017, 03:01:40 AM
Looking forward to test it out :)



Pob

  • Full Member
  • ***
  • Status:
    Offline
    Posts:
    159
    • View Profile
on: March 17, 2017, 03:50:35 AM
Nice this is much needed.



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 17, 2017, 04:07:25 AM
Sounds like something my players may enjoy. ServerSide? ClientSide? Do you modify the existing trade AI to prefer player stations for their own routes (if that is how that works), or whole new scripts being processed?

Completely new scripts for player ships. And for now it only works on your own factories.

I use tiny bits of the trader mechanic (mainly the docking sequencing), but everything else is completely AI ship driven.

So you're going to need at least a hauler with a captain for it to work.

I'm currently experimenting with where I want to run it, but it's designed to run server side.



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 17, 2017, 10:29:44 PM
Modified the original post and released version 0.9, please let me know if you have issues, whether it works, if it does what you hoped it would do, if things need to be added, if you want me to sacrifice a chicken in your name, or whatever you can come up with.

It's been ages since I've coded any LUA, and this is my first foray into the Avorion API, so any comments on what I could've done better are very welcome!

If some things look a bit Perl/Python/C ish... Then that's because that's what I normally code in..  ;D



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 18, 2017, 04:14:41 PM
Uploaded version 0.9.1 which has a fix to recognize all factories. My need for clarity caused the factory check to not always succeed :)



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 18, 2017, 10:53:27 PM
I guess no one has any comments. Haha.

I've found an issue which I'm currently running some debugs on to see if I can fix it at all, in that if I run this in a busy system with a number of haulers going at the same time, they kinda crush into each other at some docking ports :)

So I'm going to see if I can make some sort of queueing mechanism for docking ports (at first only for my haulers, but it might be useful to have that in dock.lua by default)




weedworm31

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    12
    • View Profile
on: March 19, 2017, 01:14:15 AM
well i have a question. could you make the install instructions slightly more concise i would like to use this mod as it is exactly what ive been looking for but im not entirely sure what i need to do to get it to work correctly. drag a drop edit files what?



Wilponderoci

  • Full Member
  • ***
  • Status:
    Offline
    Posts:
    203
  • one fish two fists
    • View Profile
on: March 19, 2017, 01:24:00 AM
Yes better and clearer instructions boioh ;)

I assume it goes into the data/scripts/entity folder?

Also it should be made clear as to if it works on single player as well.
“Everything that irritates us about others can lead us to an understanding of ourselves.”



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 19, 2017, 01:32:07 AM
I have no idea how to make better instructions. I could just pack up both files and you can extract them, but that would mess with other mods.

I'll give it a try tomorrow.



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 19, 2017, 10:39:52 AM
I have created a set of commands to control your ships, and have made an attempt at making the install instructions for changing the orders list of your ship somewhat easier..

If you get the commands installed, it will all work fine, though.



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 21, 2017, 02:11:42 AM
Disabled this for now, with the new upgrades ships keep ramming into docks at full speed  ::)



Splutty

  • Sr. Member
  • ****
  • Status:
    Offline
    Posts:
    258
    • View Profile
on: March 22, 2017, 12:12:32 PM
I have some working haulers again, so I guess the mod is fine. Still some weird behaviour on certain ships :) Use at your own risk!  ;D



Devious

  • Hero Member
  • *****
  • Status:
    Offline
    Posts:
    579
  • Have you seen Rusty?
    • View Profile
    • Rusty's Universe and Community
on: March 30, 2017, 11:01:06 PM
Loving your mod so far!

I'm curious towards the performance usage when a lot of players are using this at the same time, as the debug output is quite extensive.
I've also added it on the client so they can use the UI assuming there is not extra going on, but I could be mistaken ofcourse.

This spam is what concerned me:
Code: [Select]
Assert: Trading route reset, no viable trading route found in object
v0.9.2 Thu Mar 30 23:02:59 2017 (Lemming) data/scripts/entity/ai/haulgoods.lua: Empty list received, no queue left.
v0.9.2 Thu Mar 30 23:02:59 2017 (Lemming) data/scripts/entity/ai/haulgoods.lua: Found a dispatcher. (J_C 1)

Looking forward to your answer! :)
« Last Edit: March 30, 2017, 11:03:55 PM by Devious »