I’ve been looking at the code for mining for a while, looking for ways to improve it. Finally got going and made a mod. First time doing an actual mod instead of doing changes to the original code. The code isn’t very pretty, but so far it only changes two things and they should be considered suggestions to the devs.
Been following @RepeatPan’s instructions on monkey patching to change the existing methods so props.
I’m holding off with releasing it on steam for now.
Download version 2: better_mining.smod (6.2 KB)
Finding adjacent to blocks in ceilings
This feels like a bug. In mining_service.lua comments say that the methods get_adjacent_for_destination_block and get_adjacent_for_destination_region should be kept in sync. However, the first one looks for adjacent on all sides below the block as long as the bottom face is exposed. The second one doesn’t. It only finds adjacent positions below the block if the face on that side of the block is exposed.
This mod adds this to get_adjacent_for_destination_region:
local below = region:translated(-Point3.unit_y)
for _, direction in ipairs(csg_lib.XZ_DIRECTIONS) do
local temp = below:translated(direction)
temp = temp:extruded(‘y’, self._max_reach_up-1, 0)
Slightly improved mining of unsupported blocks
Blocks that don’t have blocks above or below are called unsupported and are handled separately. They are prioritized by how many exposed sides they have, starting with the most exposed. Only the blocks with the highest number of exposed sides are allowed to be mined (as long as the highest isn’t 4).
This works in simple cases but for complex shapes this often makes the hearthlings mine the block they need to access the rest.
The change I made so far is to check if a block is part of a one block wide bridge (straight bridge, corner bridge or connected to one).
The new order of priorities is: end piece, corner, edge, narrow bridge, surrounded blocks, flying.
There is still a lot of problems with this. If they get to a point where all blocks are considered narrow bridges then they will just take the closest one. Also, I don’t check if it’s a bridge on the side of a wall.
It could be improved more by checking the terrain more, however checking terrain isn’t good for performance (according to comments) and it won’t find ladders, witch should be considered important access points, so I’m thinking that maybe the nav mesh has some useful information stored that’s quick and easy to check. Any suggestion and pointers in the right direction are very welcome.
A quick summary for now. Found a bunch of special cases that wasn’t handled and have solved most of them, so they should now be much better at the basic mining up, down and horizontal. Slightly better at dealing with unsupported blocks.
The special cases that I didn’t solve should hopefully not be a big problem once I get them to prioritize mining upwards.
As long as your mining zone includes a stair they should be able to mine it out to get to the top. However, if they start at the bottom they tend to mine out the bottom first. This makes it highly likely that they will eventually create at least some flying blocks. Sometimes with hearthlings on them. But it could be a lot worse.
Did some changes that should help performance, but added new stuff that probably counter that.
At some point I want to try to add automatically placed ladders (and maybe even scaffolding) but I haven’t started looking at that yet. Since that would mean extra work for the hearthlings it’s better to try to make them mine in a way that gives them access to as much as possible.
My current plan is to try to make them mine narrow stairs to the top of the mining zone and work down from there.
When mining blocks from the side they should (if they can) skip the one on the same hight as them selves and mine 3 blocks above it. Then they should look for higher places to stand and mine instead of looking for adjacent blocks to mine. If I can make the workers keep a little distance from each other and make them prioritize mining 2x2 spiral stairs, they should hopefully not interfere too much with each other.
I have some notes on minor things I noticed when looking through mining_zone_component. Will post them later.
Try it out and if you have any questions or suggestions I’m happy to hear it.