Jump to content

[MOD] Automated Regular Asteroid Mining (v0.21.4 r15002)


BangL

Recommended Posts

Description

 

With this mod, automated mining ships (with captains) will continue with regular asteroids, when there are no resource asteroids left.

 

Client/Server

 

The modded code is performed server-side, and therefore it could be used for single-player and server-owners!

If you use this mod as mutli-player client, it won't have any affect.

 

Installation

 

1. Open up data\scripts\entity\ai\mine.lua

 

2. Below this line:

local noAsteroidsLeft = false

add this line:

local noResourcesLeft = false

 

2. Replace the whole function AIMine.findMinedAsteroid() with this modded version:

 

-- check the sector for an asteroid that can be mined
-- if there is one, assign minedAsteroid
function AIMine.findMinedAsteroid()
    local ship = Entity()
    local sector = Sector()
    local faction = Faction(ship.factionIndex)
    local x, y = sector:getCoordinates()
    local coords = tostring(x) .. ":" .. tostring(y)

    local mineables
    local nearest
    local dist

    minedAsteroid = nil

    -- Start with resources
    if noResourcesLeft == false then
        mineables = {sector:getEntitiesByComponent(ComponentType.MineableMaterial)}
        nearest = math.huge
        for _, a in pairs(mineables) do
            if a.type == EntityType.Asteroid and (a.isObviouslyMineable or ship:hasScript("systems/miningsystem.lua")) then
                local material = a:getLowestMineableMaterial()
                local resources = a:getMineableResources()
                if resources ~= nil and resources > 0 and material ~= nil then
                    -- only try to mine asteroids that are mineable by the available mining lasers
                    if material.value <= miningMaterial + 1 then
                        local dist = distance2(a.translationf, ship.translationf)
                        if dist < nearest then
                            nearest = dist
                            minedAsteroid = a
                        end
                    end
                end
            end
        end

        if minedAsteroid then
            noResourcesLeft = false
            broadcastInvokeClientFunction("setMinedAsteroid", minedAsteroid.index)
        else
            if noResourcesLeft == false then
                noResourcesLeft = true
                if faction then
                    faction:sendChatMessage(ship.name or "", ChatMessageType.Error, "Your mining ship in sector %s can't find any more asteroids. Progressing with normal asteroids."%_T, coords)
                    faction:sendChatMessage(ship.name or "", ChatMessageType.Normal, "Sir, we can't find any more asteroids in \\s(%s)! Progressing with normal asteroids."%_T, coords)
                end
            end
        end
    end

    -- Continue with regular asteroids
    if noResourcesLeft == true and noAsteroidsLeft == false then
        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
            if noAsteroidsLeft == false then
                noAsteroidsLeft = true
                if faction then
                    faction:sendChatMessage(ship.name or "", ChatMessageType.Error, "Your mining ship in sector %s can't find any more asteroids."%_T, coords)
                    faction:sendChatMessage(ship.name or "", ChatMessageType.Normal, "Sir, we can't find any more asteroids in \\s(%s)!"%_T, coords)
                end
                ShipAI():setPassive()
            end
        end
    end
end

 

Enjoy!

 

Version/Changes

 

This version is the very first release of this script. No changes have been made yet.

Tested on: v0.21.4 r15002

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...