Tutoriel 7 : un bâtisseur et des plans fantômes

But : vous planifiez, un robot bâtit.

Ce qu'il vous faut

  • Une base_mk3 (4 000 MT, 8 ports), un builder (18 000 MT, 2 ports), une hopper, un lidar, 2 battery_cell (une base_mk2 manque de ports).
  • Un dépôt ou une caisse qui contient les articles à construire.

Étapes

  1. Sur Map, cliquez Plan ghosts, choisissez un article et une orientation, cliquez des cases. Les fantômes sont gratuits, ne demandent aucun article et n'ont pas de limite de portée. Done quitte le mode. Agent :
ghost_place  item="conveyor" x=12 y=8 direction="east" blueprint="line-one"
ghost_list
  1. Mettez les articles dans un stockage près du chantier (depot_load avec id, item, quantity).
  2. Déployez la base, attachez builder, hopper, lidar et les deux cellules. S'il roule encore un script par défaut, il reçoit celui-ci tout seul ; sinon utilisez 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. Surveillez Ghosts et les événements du robot.

Le bâtisseur pose une structure par tour, à 300 W, sur une case qui le touche. L'unité vient de sa trémie, sinon d'un dépôt ou d'un entrepôt adjacent. Le script par défaut va chercher une unité à la fois.

Ce que vous devriez voir

Le robot marche au stockage, prend un article, se place à côté d'un fantôme, bâtit. Le fantôme disparaît quand la structure est debout. no stock for any ghost dans le journal veut dire exactement ça.

Trois erreurs courantes

  1. Un article volumineux. La trémie prend 4 000 kg et 1 600 L. Convoyeurs, câbles et panneaux entrent. Un dépôt ou un poste moyen (3 000 L) n'entre pas, donc le script par défaut ne peut pas le transporter ; posez-les depuis le vaisseau.
  2. Une batterie vide. Une cellule neuve est vide, et une pose tire 300 W, toute une cellule. Chargez le jour ou au quai d'un poste.
  3. Chercher un outil qui construit. Il n'y en a pas. ghost_place ne fait que planifier ; la construction se passe dans le script d'un robot.

La suite

Tutoriel 8 : des robots qui réparent