Created
November 5, 2026 15:23
-
-
Save Ghost---Shadow/dfcd70ded9e6420d34715eb2cda709b4 to your computer and use it in GitHub Desktop.
The Miner Was Replaced (The Farmer Was Replaced DLC): ore prospector that predicts cave-ins, checks every block exactly once, mines top to bottom so no ore is lost, and walks a sorted snake route.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| prospect.py - ore prospector for The Miner Was Replaced | |
| (the mining DLC for The Farmer Was Replaced) | |
| Digs a shaft at the drone's start position and mines every ore block | |
| the resulting cave-ins expose, without checking any block twice and | |
| without losing ore to cave-ins. | |
| How it works | |
| ------------ | |
| 1. Cave-in prediction | |
| Digging lowers a tile. Any neighbour more than STABILITY higher | |
| caves in and loses its top block, and that can spread outward. | |
| The script tracks a predicted surface level for every tile and | |
| updates it after each dig, so it knows which tiles have a new | |
| block on top without walking over to look. The real level is | |
| read whenever the drone arrives at a tile, and any wrong guess | |
| is corrected on the spot. | |
| 2. Checking each block once | |
| A tile is only visited when its top block has not been checked | |
| yet. A block is identified by (x, y, level), so a tile is | |
| checked again only after a dig or cave-in exposes a new block. | |
| 3. Top to bottom | |
| Higher blocks are always handled first. Digging lower causes | |
| more cave-ins, which would destroy ore that is still exposed | |
| above. Ore is dug one block at a time, and the block underneath | |
| waits in the queue until everything higher is done. | |
| 4. Route | |
| The queue is kept sorted by insertion (binary search + insert): | |
| highest level first, then in snake order (row 0 left to right, | |
| row 1 right to left, ...). The drone takes whichever end of the | |
| highest level is closer, so it walks the snake in the shorter | |
| direction. | |
| Requirements and assumptions | |
| ---------------------------- | |
| - utils.reset_position(...) moves the drone back to its start tile. | |
| - get_pos_z() returns the surface level of the tile under the drone. | |
| Whether z goes up or down when digging is detected automatically. | |
| - STABILITY = 1 is hardcoded (max height difference between | |
| neighbours before the higher one caves in). | |
| - Movement does not use wrap-around. | |
| Usage | |
| ----- | |
| prospect(mineral_types, min_depth=0, max_depth=50) | |
| mineral_types grounds worth digging, e.g. [Grounds.Coal] | |
| min_depth shaft digs before ore checking starts | |
| max_depth shaft digs with ore checking after each one | |
| Ideas for improvement | |
| --------------------- | |
| - Snake order fits levels that fill whole rows. If one level forms | |
| a ring around the shaft, a spiral or Hilbert-curve order in | |
| snake_index() will save moves. | |
| - Ore vein adjacency is not used. Placing extra shafts next to | |
| known veins could expose more ore per dig. | |
| """ | |
| from utils import reset_position | |
| # Max height difference allowed between neighbours before the higher one caves in | |
| STABILITY = 1 | |
| def move_to(x, y): | |
| while get_pos_x() < x: | |
| move(East) | |
| while get_pos_x() > x: | |
| move(West) | |
| while get_pos_y() < y: | |
| move(North) | |
| while get_pos_y() > y: | |
| move(South) | |
| def neighbors(x, y, size): | |
| result = [] | |
| if x + 1 < size: | |
| result.append((x + 1, y)) | |
| if x > 0: | |
| result.append((x - 1, y)) | |
| if y + 1 < size: | |
| result.append((x, y + 1)) | |
| if y > 0: | |
| result.append((x, y - 1)) | |
| return result | |
| def distance_to(tile): | |
| return abs(tile[0] - get_pos_x()) + abs(tile[1] - get_pos_y()) | |
| # Level of the tile under the drone, where a bigger number always means higher ground | |
| def current_level(up_sign): | |
| return get_pos_z() * up_sign | |
| # ---------- Sort keys ---------- | |
| # Every queued tile is stored as one number. Sorting those numbers gives | |
| # highest level first, and within a level a snake path: | |
| # row 0 left to right, row 1 right to left, row 2 left to right, ... | |
| def snake_index(tile, size): | |
| x = tile[0] | |
| y = tile[1] | |
| row_goes_left = y % 2 == 1 | |
| if row_goes_left: | |
| x = size - 1 - x | |
| return y * size + x | |
| def tile_from_snake_index(index, size): | |
| y = index // size | |
| x = index % size | |
| row_goes_left = y % 2 == 1 | |
| if row_goes_left: | |
| x = size - 1 - x | |
| return (x, y) | |
| def make_key(tile, level, size): | |
| tiles_per_level = size * size | |
| return -level * tiles_per_level + snake_index(tile, size) | |
| def level_of_key(key, size): | |
| tiles_per_level = size * size | |
| return -(key // tiles_per_level) | |
| def tile_of_key(key, size): | |
| tiles_per_level = size * size | |
| return tile_from_snake_index(key % tiles_per_level, size) | |
| # Smallest possible key on a level, so every key on that level is >= this | |
| def first_key_of_level(level, size): | |
| return -level * size * size | |
| # ---------- Sorted queue ---------- | |
| # queue["keys"] sorted list of keys | |
| # queue["key_of"] tile -> its key, so a tile is never in the queue twice | |
| def make_queue(): | |
| return {"keys": [], "key_of": {}} | |
| def is_empty(queue): | |
| return len(queue["keys"]) == 0 | |
| # Binary search: index of the first key that is >= key | |
| def find_position(keys, key): | |
| low = 0 | |
| high = len(keys) | |
| while low < high: | |
| middle = (low + high) // 2 | |
| if keys[middle] < key: | |
| low = middle + 1 | |
| else: | |
| high = middle | |
| return low | |
| def remove_from_queue(queue, tile): | |
| old_key = queue["key_of"][tile] | |
| index = find_position(queue["keys"], old_key) | |
| queue["keys"].pop(index) | |
| queue["key_of"].pop(tile) | |
| # Insertion sort step: put the tile where it belongs. If it is already queued | |
| # at another level, its old entry is replaced. | |
| def add_to_queue(queue, tile, level, size): | |
| new_key = make_key(tile, level, size) | |
| already_queued = tile in queue["key_of"] | |
| if already_queued: | |
| same_spot = queue["key_of"][tile] == new_key | |
| if same_spot: | |
| return | |
| remove_from_queue(queue, tile) | |
| index = find_position(queue["keys"], new_key) | |
| queue["keys"].insert(index, new_key) | |
| queue["key_of"][tile] = new_key | |
| # Take the closer end of the highest level, so the drone walks the snake | |
| # in whichever direction is shorter from where it stands | |
| def take_next(queue, size): | |
| keys = queue["keys"] | |
| top_level = level_of_key(keys[0], size) | |
| end_of_top_level = find_position(keys, first_key_of_level(top_level - 1, size)) - 1 | |
| first_tile = tile_of_key(keys[0], size) | |
| last_tile = tile_of_key(keys[end_of_top_level], size) | |
| last_is_closer = distance_to(last_tile) < distance_to(first_tile) | |
| if last_is_closer: | |
| chosen = last_tile | |
| else: | |
| chosen = first_tile | |
| remove_from_queue(queue, chosen) | |
| return chosen | |
| # ---------- Digging ---------- | |
| # A tile that got lower can make its neighbours cave in, and that can spread further. | |
| # Every tile that caves in has a new block on top, so it gets queued for a check. | |
| def predict_cave_ins(start, surface, queue, size): | |
| stack = [start] | |
| while len(stack) > 0: | |
| tile = stack.pop() | |
| for neighbor in neighbors(tile[0], tile[1], size): | |
| too_steep = surface[neighbor] - surface[tile] > STABILITY | |
| if too_steep: | |
| surface[neighbor] = surface[tile] + STABILITY | |
| add_to_queue(queue, neighbor, surface[neighbor], size) | |
| stack.append(neighbor) | |
| def check_queued_tiles(queue, surface, checked_blocks, mineral_types, size, up_sign): | |
| while not is_empty(queue): | |
| tile = take_next(queue, size) | |
| move_to(tile[0], tile[1]) | |
| # Fix the prediction if the tile isn't where we expected | |
| real_level = current_level(up_sign) | |
| prediction_was_wrong = real_level != surface[tile] | |
| if prediction_was_wrong: | |
| surface[tile] = real_level | |
| predict_cave_ins(tile, surface, queue, size) | |
| block = (tile[0], tile[1], surface[tile]) | |
| already_checked = block in checked_blocks | |
| if already_checked: | |
| continue | |
| checked_blocks.add(block) | |
| is_ore = get_ground_type() in mineral_types | |
| if is_ore: | |
| dig() | |
| surface[tile] = current_level(up_sign) | |
| add_to_queue(queue, tile, surface[tile], size) # block underneath waits until anything higher is done | |
| predict_cave_ins(tile, surface, queue, size) | |
| def prospect(mineral_types, min_depth=0, max_depth=50): | |
| size = get_world_size() | |
| shaft_x = get_pos_x() | |
| shaft_y = get_pos_y() | |
| shaft = (shaft_x, shaft_y) | |
| up_sign = 1 | |
| surface = {} # (x, y) -> predicted surface level | |
| checked_blocks = set() # (x, y, level) blocks already looked at | |
| queue = make_queue() # tiles whose top block hasn't been looked at yet | |
| for shaft_dig in range(min_depth + max_depth): | |
| move_to(shaft_x, shaft_y) | |
| z_before = get_pos_z() | |
| dig() | |
| is_first_dig = shaft_dig == 0 | |
| if is_first_dig: | |
| z_went_up = get_pos_z() > z_before | |
| if z_went_up: | |
| up_sign = -1 | |
| start_level = z_before * up_sign | |
| for index in range(size * size): | |
| tile = tile_from_snake_index(index, size) | |
| surface[tile] = start_level | |
| add_to_queue(queue, tile, start_level, size) | |
| surface[shaft] = current_level(up_sign) | |
| add_to_queue(queue, shaft, surface[shaft], size) | |
| predict_cave_ins(shaft, surface, queue, size) | |
| past_min_depth = shaft_dig >= min_depth | |
| if past_min_depth: | |
| check_queued_tiles(queue, surface, checked_blocks, mineral_types, size, up_sign) | |
| if __name__ == '__main__': | |
| mineral_types = [ | |
| Grounds.Coal, | |
| Grounds.Iron, | |
| Grounds.Limestone | |
| ] | |
| while True: | |
| reset_position(True) | |
| prospect(mineral_types) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment