Author Topic: [MOD] Let miners farm regular asteroids (vers 1.0)  (Read 1139 times)

DestroNK

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    12
    • View Profile
on: September 16, 2018, 05:06:15 AM
So Hello there :)
I had the problem that npc-traders couldn't reach my stations, since too many asteroids were blocking the way. But what to do ? Farm 2000+ asteroids yourself, since your npc ships are too lazy doing this ? No mods around there for doing this :( at least none I found.

Good thing though I'm a software developer IRL, so I gave this a try, let me introduce you to my very first mod :D :D


Caution: These are my very first experiences with lua !! Create backups before using this! I might have done some mistakes I'm not aware of yet! Lua is an entirely new coding-language for me!



1. What does this mod exactly do?

Pretty simple to explain.

Normal behaviour of commanding your ship to mine:
It starts mining ressource asteroids and once there are no more, it tells you "Yo captain, everything is mined. I'll get a cup of coffee and relax. Peace!"

Changed behaviour by using my mod:
Regular mining behaviour still entirely happens as before. Means: Miners will look out for resource asteroids (also using all the same AI as it did before). Once all ressource asteroids are gone, it will tell you the message:
"Sir, we can't find any more asteroids in (sector)! We continue farming normal asteroids."

They continue as said. Once, ALL asteroids are gone, it will post you the message:
"All asteroids in (sector) are gone."

Any reactivation or new order has to be done by the player himself. Ship is set to idle. Ofc this whole thing does also work when there aren't any resource asteroids right in the beginning. You just get the insta message, no more resource asteroids are there to mine, continueing with the normal ones :)



2. Requirements:

2.1 Game Version "Beta 0.17.1 r12316 a186915a28c6" or "Beta 0.18.3 r12783 a1770b4cb86e"
Other game versions might malfunction this mod, since there could be changes to farming behaviour !

2.2 No other mods installed that influence the mining-behaviour
It depends, which stuff changed - but if this mod gets in touch with another mod it could be that it won't live in harmony together. I will post 2 ways to implement it, way 1 download file and put in, way 2 implement it yourself using my posted source code (advanced)



3. Implementing the mod

3.1 Referring to 2.2, this is the "easy" way, make sure all the described conditions are met, just go ahead, download the file "mine.zip", unzip it, and place the included "mine.lua" into "Steam\steamapps\common\Avorion\data\scripts\entity\ai". Replace it with the existing file.
IMPORTANT: Create a Backup of the original file inbefore, if anything goes wrong!

3.2 Referring to 2.2, this is the "advanced" way of implementing the code yourself.

Find the file mine.lua, as mentioned in the path above.

HINT: Use a program for making the change of code easier for yourself. I suggest Notepad++

HINT2: As I already said above, one more reminder, Create a backup of this file!!

First of all, the "vanilla" untouched main function I touched is this one (Starting at line 85, ending at line 133):
Code: [Select]
-- check the sector for an asteroid that can be mined
-- if there is one, assign minedAsteroid
function AIMine.findMinedAsteroid()
    local radius = 20
    local ship = Entity()
    local sector = Sector()

    minedAsteroid = nil

    local mineables = {sector:getEntitiesByComponent(ComponentType.MineableMaterial)}
    local nearest = math.huge

    for _, a in pairs(mineables) do
        if a.type == EntityType.Asteroid then
            local resources = a:getMineableResources()
            if resources ~= nil and resources > 0 then

                local dist = distance2(a.translationf, ship.translationf)
                if dist < nearest then
                    nearest = dist
                    minedAsteroid = a
                end

            end
        end
    end

    if minedAsteroid then
        noAsteroidsLeft = false
        broadcastInvokeClientFunction("setMinedAsteroid", minedAsteroid.index)
    else
        if noAsteroidsLeft == false then
            noAsteroidsLeft = true

            local player = Player(Entity().factionIndex)
            if player then
                local x, y = Sector():getCoordinates()
                local coords = tostring(x) .. ":" .. tostring(y)

                player:sendChatMessage(ship.name or "", ChatMessageType.Error, "Your mining ship in sector %s can't find any more asteroids."%_T, coords)
                player:sendChatMessage(ship.name or "", ChatMessageType.Normal, "Sir, we can't find any more asteroids in \\s(%s)!"%_T, coords)
            end

            ShipAI(ship.index):setPassive()
            ship:invokeFunction("craftorders.lua", "setAIAction")
        end
    end

end

What you gotta do, is first involve local variables. Right on top in the first few lines, you will find the line:
local noAsteroidsLeft = true

Create a new line right after this. Then insert these here:
local firstmsg = false
local check1 = false
local check2 = false
local check3 = false

After that, head for the line:
function AIMine.findMinedAsteroid()
(you will find it in about the middle)

When you found the beginning, you have to find the end of it, since you will have to replace this whole function with mine.

You will find the end tag of it just above the starting of the function "function AIMine.updateMining(timeStep)", THIS LINE ITSELF NOT INCLUDED! So just head for the "function AIMine.findMinedAsteroid()" to "end" and replace it with this function:
 
Code: [Select]
function AIMine.findMinedAsteroid()
if check3 == false then
local radius = 20
local ship = Entity()
local sector = Sector()
local player = Player(Entity().factionIndex)
local x, y = Sector():getCoordinates()
local coords = tostring(x) .. ":" .. tostring(y)
local mineables
local nearest
local resources

minedAsteroid = nil

if check1 == false then -- Fange mit Resourcen-Asteroiden an
mineables = {sector:getEntitiesByComponent(ComponentType.MineableMaterial)}
nearest = math.huge
for _, a in pairs(mineables) do
if a.type == EntityType.Asteroid then
resources = a:getMineableResources()
if resources ~= nil and resources > 0 then

local dist = distance2(a.translationf, ship.translationf)
if dist < nearest then
nearest = dist
minedAsteroid = a
end

end
end
end

if minedAsteroid then
broadcastInvokeClientFunction("setMinedAsteroid", minedAsteroid.index)
else
check1 = true
end
end

if check1 == true and check2 == false then -- Keine Resourcen-Asteroiden sind mehr vorhanden. Mache mit normalen weiter.

if firstmsg == false then
player:sendChatMessage(ship.name or "", ChatMessageType.Error, "No more asteroids in sector %s. We continue farming normal asteroids."%_T, coords)
player:sendChatMessage(ship.name or "", ChatMessageType.Normal, "Sir, we can't find any more asteroids in \\s(%s)! We continue farming normal asteroids."%_T, coords)
firstmsg = true
end

mineables = {sector:getEntitiesByType(EntityType.Asteroid)}
nearest = math.huge

for _, a in pairs(mineables) do
if a.type == EntityType.Asteroid then
dist = distance2(a.translationf, ship.translationf)
if dist < nearest then
nearest = dist
minedAsteroid = a
end
end
end

if minedAsteroid then
noAsteroidsLeft = false
broadcastInvokeClientFunction("setMinedAsteroid", minedAsteroid.index)
else
noAsteroidsLeft = true
check2 = true
end
end

if check1 == true and check2 == true then -- Garkeine Asteroiden sind mehr vorhanden. Lege Arbeit nieder.
player:sendChatMessage(ship.name or "", ChatMessageType.Error, "All asteroids in %s are gone."%_T, coords)
player:sendChatMessage(ship.name or "", ChatMessageType.Normal, "All asteroids in \\s(%s) are gone."%_T, coords)
ShipAI(ship.index):setPassive()
ship:invokeFunction("craftorders.lua", "setAIAction")
check3 = true
end
end
end




4. A few words

As I said, this is my first one. Big shoutout to the user "Laserzwei", he assisted me a little bit with the creation of the mod and some help of lua in general.

If you find anything to improve, feel free to share it with me, since I like the game and want to improve my lua skills fast. There is for example 1 thing I don't understand yet - these lines seem to not work at all:
ShipAI(ship.index):setPassive()
ship:invokeFunction("craftorders.lua", "setAIAction")

Why? :( As I understand, it should set the ship to passive mode. I did a workaround by just using "check3" to bypass the mine-command.
Any feedback about this mod is deeply appreciated, I do, as everyone like props for my work :D Feel free to use this mod for your very own creation of mods, a referral of this mod here would be nice though.

Sincerely, DestroNK

« Last Edit: September 18, 2018, 07:55:33 PM by DestroNK »



DestroNK

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    12
    • View Profile
on: September 16, 2018, 05:06:49 AM
18.09.2018 - Mod still works with new game version Beta 0.18.3 r12783 a1770b4cb86e
« Last Edit: September 18, 2018, 07:55:56 PM by DestroNK »



Jacob345k

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    1
    • View Profile
on: October 12, 2018, 04:17:50 PM
Hey, does this mod work with Better Mining AI? that mod makes it so that when the ships are done mining an asteriod, they "Fly" to the next asteriod, drastically decreasing mining time.



Zigzidu

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    1
    • View Profile
on: October 13, 2018, 10:05:07 PM
This looks like a fantastic mod for those of us who like vast mining fleets but don't like micromanaging them constantly.  Please tell me you're 100% sure they won't mine an unclaimed (or, for that matter, claimed) factory capable asteroid? I would feel very bad if I wasted infinite credits for a few thousand Iron ;).



DestroNK

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    12
    • View Profile
on: October 22, 2018, 02:02:14 PM
This looks like a fantastic mod for those of us who like vast mining fleets but don't like micromanaging them constantly.  Please tell me you're 100% sure they won't mine an unclaimed (or, for that matter, claimed) factory capable asteroid? I would feel very bad if I wasted infinite credits for a few thousand Iron ;).

Hey! Sorry I didnt see your reply ^^ did not check that yet tbh cause i am in other games atm. If you can find it out feel free to share :D if yes then I‘ll try to fix that.

Hey, does this mod work with Better Mining AI? that mod makes it so that when the ships are done mining an asteriod, they "Fly" to the next asteriod, drastically decreasing mining time.

Same answer to you ^^ idk how better ai works. If it is an external plugin it „should“ worl well but if it has content in the same files i am using it might malfunction. If you want to make sure then use the manual described way and create a backup of your file inbefore.
« Last Edit: October 22, 2018, 02:05:51 PM by DestroNK »



Cranberry

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    1
    • View Profile
on: October 30, 2018, 11:57:07 PM
as of version beta 0.19.1,  the ai will target claimed asteroids for mining.



EyeDeck

  • Newbie
  • *
  • Status:
    Offline
    Posts:
    3
    • View Profile
on: November 03, 2018, 06:28:13 AM
Here's a quick one-line fix for claimable asteroids. Find this code:
Code: [Select]
for _, a in pairs(mineables) do
if a.type == EntityType.Asteroid then
dist = distance2(a.translationf, ship.translationf)
Don't confuse this with the first occurrence of if a.type == EntityType.Asteroid then since this line comes up twice, the first for resource asteroids (irrelevant here) and the second for generic asteroids, which is the one we want to modify.  Replace the middle line with:
Code: [Select]
if a.type == EntityType.Asteroid and not a:hasScript("entity/claim.lua") thenAnyway, there might be a more performant way to do the same check, but it's good enough for me.
« Last Edit: November 03, 2018, 06:57:11 AM by EyeDeck »