Jump to content

[MOD] Let miners farm regular asteroids (vers 1.0)


DestroNK

Recommended Posts

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

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

 

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

 

mine.zip

Link to comment
Share on other sites

  • 4 weeks later...

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

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

  • 2 weeks later...

Here's a quick one-line fix for claimable asteroids. Find this code:

			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:

				if a.type == EntityType.Asteroid and not a:hasScript("entity/claim.lua") then

Anyway, there might be a more performant way to do the same check, but it's good enough for me.

Link to comment
Share on other sites

  • 1 month later...
  • 5 weeks later...

Trying to track why, I cannot find why the ship simply refuses to mine and then ignores any other orders.  I read in another thread something about a possible fix to not taking new orders: 

 

	if check1 == true and check2 == true then -- Mo more asteroids are available. Put down work.
		if ship.playerOwned then
		    callingPlayer = ship.factionIndex
		elseif ship.allianceOwned then
		    callingPlayer = Alliance(ship.factionIndex).leader
		end
		ship:invokeFunction("craftorders.lua", "setAIAction")
		callingPlayer = nil
		check3 = true

 

That seems to fix the exit from mining function.

 

But the mining part in of itself is just not working.  (BETA 0.19.1 r12932)

 

UPDATE: Turns out I was impatient.  Mining starts after few cycles. 

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Ok, i tried this script and there are 2 main issues for me.

 

Firstly if you have an asteroide your claimed but didn't sold or build a mine on it, the script will mine and destroy it.

 

Secondly it looks like OOS it doesn't work well, at least on my game i don't know why but as soon as i leave the sector, it simply stop working and resume once i get inside again.

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