Tutorial 7: a builder and ghost blueprints

Goal: you plan, a robot builds.

What you need

  • A base_mk3 (4 000 MT, 8 ports), a builder (18 000 MT, 2 ports), a hopper, a lidar, 2 battery_cell (a base_mk2 has too few ports).
  • A depot or crate holding the items you want built.

Steps

  1. On Map, click Plan ghosts, pick an item and a facing, click cells. Ghosts are free, need no item and have no range limit. Done leaves the mode. Agent:
ghost_place  item="conveyor" x=12 y=8 direction="east" blueprint="line-one"
ghost_list
  1. Put the items into a storage near the site (depot_load with id, item, quantity).
  2. Deploy the base, attach builder, hopper, lidar and both cells. While it still runs a default script, it receives this one on its own; otherwise use Reset to default:
DIRECTIONS = ["north", "east", "south", "west"]
STORES = ["depot", "storage_crate", "warehouse", "silo", "tank"]

def absolute(value):
    if value < 0:
        return -value
    return value

def free(direction):
    probe = lidar.probe(direction)
    return probe["kind"] == "clear" or probe["distance"] > 1

def step_toward(dx, dy):
    # One step along the longer axis, the other axis when that one is blocked.
    horizontal = "east" if dx > 0 else "west"
    vertical = "south" if dy > 0 else "north"
    order = []
    if absolute(dx) >= absolute(dy):
        order = [horizontal, vertical]
    else:
        order = [vertical, horizontal]
    for direction in order:
        along_x = direction in ["east", "west"]
        if ((along_x and dx != 0) or (not along_x and dy != 0)) and free(direction):
            robot.move(direction)
            return True
    return False

def explore():
    heading = memory.get("heading", "east")
    for turn in range(4):
        if free(heading):
            robot.move(heading)
            memory.set("heading", heading)
            return
        heading = DIRECTIONS[(DIRECTIONS.index(heading) + 1) % 4]

def steps_to(x, y):
    return absolute(x - robot.x) + absolute(y - robot.y)

def stocked_store(kind):
    best = None
    best_steps = 0
    for building in industry.buildings():
        if building["kind"] in STORES and building["field"] == robot.field and building["input"].get(kind, 0) > 0:
            steps = steps_to(building["x"], building["y"])
            if best == None or steps < best_steps:
                best = building
                best_steps = steps
    return best

def build(ghost):
    steps = steps_to(ghost["x"], ghost["y"])
    if steps == 1:
        builder.build_ghost(ghost["id"])
    elif steps == 0:
        # A robot cannot build on its own cell: step off the ghost.
        explore()
    elif not step_toward(ghost["x"] - robot.x, ghost["y"] - robot.y):
        explore()

def fetch(store, kind):
    steps = steps_to(store["x"], store["y"])
    if steps == 1:
        hopper.pickup(kind, 1)
    elif not step_toward(store["x"] - robot.x, store["y"] - robot.y):
        explore()

def tick():
    ghosts = builder.ghosts()
    if len(ghosts) == 0:
        return
    cargo = hopper.cargo()
    for ghost in ghosts:
        if cargo.get(ghost["kind"], 0) > 0:
            build(ghost)
            return
    for ghost in ghosts:
        store = stocked_store(ghost["kind"])
        if store != None:
            fetch(store, ghost["kind"])
            return
    robot.log("no stock for any ghost: fill a depot or the hopper")
  1. Watch Ghosts and the robot events.

The builder places one structure a turn, drawing 300 W, on a cell that touches the robot. The unit comes from its hopper, else from a touching depot or warehouse. The default script fetches one unit at a time into the hopper.

What you should see

The robot walks to a storage, picks up an item, walks beside a ghost, builds. The ghost disappears when the structure stands. no stock for any ghost in the log means just that.

Three common mistakes

  1. A bulky item. The hopper takes 4 000 kg and 1 600 L. Conveyors, cables and solar panels fit. A depot or a medium station (3 000 L) does not, so the default script cannot carry it; build those from the spacecraft.
  2. An empty battery. A newly attached cell holds nothing, and one build draws 300 W, a whole cell. Charge by day or at a station dock.
  3. Waiting for a tool to build it. There is none. ghost_place only plans; building happens in a robot's script.

Next

Tutorial 8: robots that repair