Jump to content

[MOD] Goods Hauler (Automatically buy/sell/move goods!)


Splutty

Recommended Posts

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):

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:

 

 

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:

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.

	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:

    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:

end

function checkCaptain()

 

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

 

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.

 

haulgoods_cmd-v0.9.2.zip

Link to comment
Share on other sites

  • Replies 116
  • Created
  • Last Reply

Top Posters In This Topic

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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:

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! :)

Link to comment
Share on other sites

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:

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! :)

 

There's pretty much no impact from those messages.

 

I'm not sure if I already had the time delays in 0.9.2, but those checks happen at most once every 5 seconds. And when the ship can't find a useable trading route, it'll increase the wait time 5 seconds every time to a max of 60 seconds.

 

The only thing that costs any cycles at all is the initial building of the list, and even that's only minimal. I might put timings around some stuff to see how long it actually takes.

 

As for the client side, if you want people to be able to use the Orders -> Haul Goods command, you're going to have to do that yes. Otherwise it can be fully run on the server side, as long as you add the command to the right permissions group.

 

0.9.3 also has a change in debugging, where you can set some levels.

 

If you don't want debug messages at all, just change the debug_msg function.

 

 

Link to comment
Share on other sites

Thanks for the mod. Was hoping for something like this to improve the station building aspect of the game. I can test it out cause I am playing in Rustys Galaxy and we recently got it there. I tried it out a little (for like 20 to 15 mins on  the server) and i have a suggestion: a command that forces trade with only player owned stations (or non-npc stations). I built liquor chain in a sector with tradepost that bought wheat. The little bugger was always selling it to the tradepost.

 

I found 2 issues:

1. The haulers tend to collide with ai ships already docked at stations. sad for rep.

2. One of the hauler had a cargo bay filled with lots of power cells that *I THINK* it wasnt selling. but that maybe due to the fact that it had bought it and there was a server restart in between. not sure.

 

Anywho, thanks for your work and your effort. It was great seeing the ship do its thing. It makes a great addition to the game. i am looking forward to playing with it more in an empty sector :)

Link to comment
Share on other sites

Thanks for the mod. Was hoping for something like this to improve the station building aspect of the game. I can test it out cause I am playing in Rustys Galaxy and we recently got it there. I tried it out a little (for like 20 to 15 mins on  the server) and i have a suggestion: a command that forces trade with only player owned stations (or non-npc stations). I built liquor chain in a sector with tradepost that bought wheat. The little bugger was always selling it to the tradepost.

 

Of course! That's where it can actually earn money  ;D

 

I also built in a check where it only transfers the largest amount possible. So if 1000 is needed somewhere, and a station sells 500 and another 100, it'll always pick the 500.

 

I'll have to think about how to implement a limitation on what stations it's allowed to visit.

 

I found 2 issues:

1. The haulers tend to collide with ai ships already docked at stations. sad for rep.

2. One of the hauler had a cargo bay filled with lots of power cells that *I THINK* it wasnt selling. but that maybe due to the fact that it had bought it and there was a server restart in between. not sure.

 

As to 1: There's not much I can do about that :( I've had NPC haulers squish my ships as well, and when I'm running 3 of them at once, they squish each other. The 'get a docking port' routine seems to always pick the same one, so everything piles up there.

 

I've been messing around a bit with the dock code, but I'm not sure if I can fix it in such a way that it doesn't affect a lot of other things. And I can't fix the 'playership getting crushed' issue at all.

 

For number 2, yes. If it's bought things and gets interrupted, then depending on who's the actual dispatcher and at what point it picks up again, it won't sell it. I've tried to alleviate this problem somewhat by making it so the hauler tries to sell everything if there's more in the cargo bay than what was originally planned, but that doesn't always work either.

 

For those things, there's still some manual intervention required, unfortunately.

 

Thanks for using it and posting the issues :)

Link to comment
Share on other sites

Have been playing around more with your mod and have an idea regarding point 2.

 

For number 2, yes. If it's bought things and gets interrupted, then depending on who's the actual dispatcher and at what point it picks up again, it won't sell it. I've tried to alleviate this problem somewhat by making it so the hauler tries to sell everything if there's more in the cargo bay than what was originally planned, but that doesn't always work either.

 

Might an if check which looks for what the hauler has in its cargo and all the things it can sell in the sector before it starts doing trade routes alleviate the problem?

 

I must confess, i havent looked at your script yet. And after i am done doing these trade runs, i will have a look  8). if i have better ideas, i will let you know.

Link to comment
Share on other sites

Have been playing around more with your mod and have an idea regarding point 2.

 

For number 2, yes. If it's bought things and gets interrupted, then depending on who's the actual dispatcher and at what point it picks up again, it won't sell it. I've tried to alleviate this problem somewhat by making it so the hauler tries to sell everything if there's more in the cargo bay than what was originally planned, but that doesn't always work either.

 

Might an if check which looks for what the hauler has in its cargo and all the things it can sell in the sector before it starts doing trade routes alleviate the problem?

 

I must confess, i havent looked at your script yet. And after i am done doing these trade runs, i will have a look  8). if i have better ideas, i will let you know.

 

It might, but that's not really what the mod was for :)

 

I've thought about building in a 'recovery' mode, which saves and loads what it was doing last and tries to continue doing that, but haven't gotten around to that yet.

 

I mainly built all this for my own sector with my own factories, so it's not really very multi purpose. It will work in a sector where you can do trade between NPC stations as well, since that was only a small adjustment to make.

 

A bigger problem is ships getting stuck on each other when they're at the same docking port. Since getting a 'random' docking port doesn't seem to be random at all :)

Link to comment
Share on other sites

Had a skim of the script. The relative complexity and my lack of knowledge of lua led to very shallow understanding. Thankfully, i found the /haulgoods status command while re-reading your title post.

 

I mainly built all this for my own sector with my own factories

 

 

It might, but that's not really what the mod was for :)

 

I mainly built all this for my own sector with my own factories, so it's not really very multi purpose. It will work in a sector where you can do trade between NPC stations as well, since that was only a small adjustment to make.

 

This is what I am hoping to use it for. the problem is I built my things in a neutral zone where there are trade posts. So it sometimes makes those trades. Thats why I was thinking maybe when u populate your table in your "buildGoodsList" function, there can be column which stores the owner of the station. Later that information maybe used to restrict the hauler to only the players station.

 

I've thought about building in a 'recovery' mode, which saves and loads what it was doing last and tries to continue doing that, but haven't gotten around to that yet.

 

Regarding the recovery mode, it can be useful even when building in an empty sector because of servers crashes. I do not know what is easier, checking the cargo, or periodically writing the state. I am gonna see if there is any way to give captain commands to the ship i am commanding. Sth I would really like to be able to do. Also gonna play around with the haulgoods status command.

 

P.S  I myself imagine/assume many things are possible (modwise), but my lack of programming experience makes me overzealous. so excuse me if i sound thick.  :)

 

Cheerio

Link to comment
Share on other sites

I do not know what is easier, checking the cargo, or periodically writing the state.

 

I know I certainly don't know the answer to that for scripting, but if we look at it in terms of how it might work in the real world... you know what cargo you have, but you may have forgotten your route.  So since you KNOW what cargo you have, and your job is to sell whatever you can... start from scratch.  It's a long chain query, though, because there's probably a nasty, nasty web of possibilities to choose from.

 

I'd do something like:

 

List of all stations that will buy my cargo

 

Link to comment
Share on other sites

From the metadata of your quote i thought u were talking to Splutty. hehe

 

List of all stations that will buy my cargo

 

Anywho, the code already makes a list of the goods being bought and sold in the sector. It is done using the buildGoodsList function in the haulgoods.lua file. incase you feel inspired to have a look at the script and change it for the rest of us ;)

Link to comment
Share on other sites

 

Anywho, the code already makes a list of the goods being bought and sold in the sector. It is done using the buildGoodsList function in the haulgoods.lua file. incase you feel inspired to have a look at the script and change it for the rest of us ;)

 

I have no idea what happened, but that post stopped about 1/4 of the way through what I recall having typed up... and now I can't remember any of it.  It probably seemed brilliant at the time, too.  Developers around the world will mourn the loss of my genius. :)

 

In future, I will attempt to post less frequently while running on caffeine instead of sleep.

Link to comment
Share on other sites

After testing it a while on our server it seems there is an increase in server load when multiple ships are hauling goods in multiple sectors. It came to mind after jumping around trying to narrow down the cause but all I could find were 3 afk players hauling goods in their sectors that could have a significant load.

It might have more to do with how the game handles updates as one laggy sector slows down everything else and the cause might also lie somewhere else as we use many more mods tho.

 

Would it be possible to somehow limit the amount of ships a player can use to haul goods?

The few players I found were using 3-6 ships so that might have something to do with it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...