StarOS: first scripts
Copy, then edit. Each says which parts it needs. See the overview and troubleshooting.
1. Wander
Parts: lidar.
DIRECTIONS = ["north", "east", "south", "west"]
def tick():
heading = memory.get("heading", "east")
for attempt in range(4):
ahead = lidar.probe(heading)
if ahead["kind"] == "clear" or ahead["distance"] > 1:
robot.move(heading)
break
heading = DIRECTIONS[(DIRECTIONS.index(heading) + 1) % 4]
memory.set("heading", heading)
2. Mine and unload
Parts: drill, hopper (3 ports, so a Mk2 or better). Park the robot on an ore cell with a depot on a side cell. It digs until nearly full, then unloads.
def tick():
if hopper.free() >= 400:
drill.dig()
else:
hopper.unload()
3. Solar-aware walker
Parts: none. Moves only with good light, so a robot with no battery does not spend on moves at dusk.
MIN_LIGHT = 300
def tick():
if sun.light() < MIN_LIGHT:
robot.log("resting, light " + str(sun.light()))
return
robot.move("east" if (robot.turn // 8) % 2 == 0 else "west")
4. Pay a supplier
Parts: none. Pays once per game day. Change the name to a real player.
SUPPLIER = "Ada"
AMOUNT = 2000000 # drops: 2 MT
KEEP = 100000000 # never go under 100 MT
def tick():
day = robot.turn // sun.day_turns
if memory.get("paid_day", -1) == day:
return
if wallet.balance() - AMOUNT < KEEP:
return
wallet.pay(SUPPLIER, AMOUNT, "supplies, day " + str(day))
memory.set("paid_day", day)
The payment can still be refused; check the robot's events.
5. React to a message
Parts: modem, switched on from the robot page. Replace ada with your topic name. Publish {"go": "north"} on me/ada/orders from another device.
TOPIC = "me/ada/orders"
def tick():
if not net.online():
return
net.subscribe(TOPIC)
for message in net.inbox():
payload = message["payload"]
if message["topic"] == TOPIC and type(payload) == "dict":
direction = payload.get("go")
if direction in ["north", "east", "south", "west"]:
robot.move(direction)
net.publish("me/ada/done", {"robot": robot.name, "went": direction})
6. Protect against low battery
Parts: battery_cell, lidar. Under a quarter it stops until half full.
def tick():
capacity = battery.capacity()
charge = battery.charge()
resting = memory.get("resting", False)
if charge * 4 < capacity:
resting = True
elif charge * 2 >= capacity:
resting = False
memory.set("resting", resting)
if resting:
robot.log("resting at " + str(charge) + " Wh")
return
ahead = lidar.probe("east")
if ahead["kind"] == "clear" or ahead["distance"] > 1:
robot.move("east")
7. Build your ghosts
Parts: builder, hopper. Plan ghosts on the map, carry the items, and let it build those within reach. It walks straight and has no path finding.
def tick():
ghosts = builder.ghosts()
if len(ghosts) == 0:
return
cargo = hopper.cargo()
for ghost in ghosts:
if cargo.get(ghost["kind"], 0) > 0:
dx = ghost["x"] - robot.x
dy = ghost["y"] - robot.y
if abs(dx) + abs(dy) == 1:
builder.build_ghost(ghost["id"])
elif dx > 1 or (dx == 1 and dy != 0):
robot.move("east")
elif dx < -1 or (dx == -1 and dy != 0):
robot.move("west")
elif dy > 0:
robot.move("south")
elif dy < 0:
robot.move("north")
else:
robot.move("east") # standing on the ghost: step off
return
robot.log("no item for any ghost in the hopper")
8. A simple trader, as a contract
This is a contract, not a robot script: deploy it on the Contracts page (10 MT). Once a game day it buys if the cheapest ask is low enough. Fund it by calling fund with drops attached. Goods go to your spacecraft hold.
ITEM = "iron_ore"
MAX_PRICE = 2000000 # drops a unit
QUANTITY = 10
EVERY = 24
def fund():
return wallet.balance()
def withdraw():
if wallet.caller != wallet.owner:
fail("only the deployer may withdraw")
amount = wallet.balance()
if amount > 0:
wallet.pay(wallet.owner, amount)
return amount
def tick():
ask = market.best_ask(ITEM)
if ask == None or ask > MAX_PRICE:
return
if wallet.balance() < QUANTITY * MAX_PRICE:
log("not enough funds")
return
log("order " + market.buy(ITEM, QUANTITY, price=MAX_PRICE))
The source is public. The schedule's gas is paid by the deployer.