137 lines
2.9 KiB
Python
137 lines
2.9 KiB
Python
from __builtins__ import *
|
|
from farm import WORLD_SIZE
|
|
|
|
|
|
def goto_origin():
|
|
goto(0, 0)
|
|
|
|
|
|
def goto_simple(x: int, y: int):
|
|
while get_pos_x() != x:
|
|
if x > get_pos_x():
|
|
move(East)
|
|
else:
|
|
move(West)
|
|
while get_pos_y() != y:
|
|
if y > get_pos_y():
|
|
move(North)
|
|
else:
|
|
move(South)
|
|
|
|
|
|
def goto(target: int | tuple[int, int], y: int | None = None):
|
|
if y == None:
|
|
x, y = target
|
|
else:
|
|
x = target
|
|
goto_coord(x, y)
|
|
|
|
|
|
def goto_coord(x: int, y: int):
|
|
# """
|
|
# Moves the drone to a specified position on the farm, wrapping at edges.
|
|
#
|
|
# The function calculates the shortest path to move from the current
|
|
# position to a target position `(x, y)` on the farm with wrapping
|
|
# boundaries. The movement is performed in the x-direction first,
|
|
# followed by the y-direction. The wrapping grid ensures that if
|
|
# the destination is closer when wrapping around, the function
|
|
# chooses the shorter path accordingly.
|
|
#
|
|
# Parameters:
|
|
# x: int
|
|
# The target x-coordinate on the grid.
|
|
# y: int
|
|
# The target y-coordinate on the grid.
|
|
#
|
|
# Raises:
|
|
# KeyError
|
|
# If the involved direction constants (e.g., East, West) are not
|
|
# properly defined in the environment where this function is used.
|
|
#
|
|
# """
|
|
cur_x = get_pos_x()
|
|
cur_y = get_pos_y()
|
|
|
|
dx = (x - cur_x) % WORLD_SIZE
|
|
dy = (y - cur_y) % WORLD_SIZE
|
|
|
|
if dx > WORLD_SIZE - dx:
|
|
dx_dir, dx_steps = West, WORLD_SIZE - dx
|
|
else:
|
|
dx_dir, dx_steps = East, dx
|
|
|
|
if dy > WORLD_SIZE - dy:
|
|
dy_dir, dy_steps = South, WORLD_SIZE - dy
|
|
else:
|
|
dy_dir, dy_steps = North, dy
|
|
|
|
for _ in range(dx_steps):
|
|
move(dx_dir)
|
|
for _ in range(dy_steps):
|
|
move(dy_dir)
|
|
|
|
|
|
def harvest_except(excluded_entity):
|
|
if get_entity_type() != excluded_entity and can_harvest():
|
|
harvest()
|
|
|
|
|
|
def auto_till():
|
|
if get_ground_type() != Grounds.Soil:
|
|
till()
|
|
|
|
|
|
def get_loc() -> tuple[int, int]:
|
|
# """
|
|
# Gets the current position as a tuple of x and y coordinates.
|
|
#
|
|
# This function retrieves the x and y coordinates by calling the
|
|
# `get_pos_x` and `get_pos_y` functions, respectively.
|
|
#
|
|
# Returns:
|
|
# tuple[int, int]: A tuple containing the x and y coordinates as integers.
|
|
# """
|
|
return get_pos_x(), get_pos_y()
|
|
|
|
|
|
def goto_next():
|
|
# """
|
|
# Moves the drone to the next position on the farm grid.
|
|
#
|
|
# This function handles the logic for the drone to iterate across a farm grid. It performs a
|
|
# single step movement to allow actions on the current tile before moving. If the drone is at
|
|
# the top edge of the farm grid, it first moves east, otherwise it moves north unconditionally.
|
|
#
|
|
# Returns:
|
|
# None
|
|
# """
|
|
if get_pos_y() == WORLD_SIZE - 1:
|
|
move(East)
|
|
move(North)
|
|
|
|
def for_all(f):
|
|
def row():
|
|
for _ in range(get_world_size()-1):
|
|
f()
|
|
move(East)
|
|
f()
|
|
|
|
drones = []
|
|
for _ in range(get_world_size()):
|
|
maybe = spawn_drone(row)
|
|
if not maybe:
|
|
row()
|
|
else:
|
|
drones.append(maybe)
|
|
move(North)
|
|
return drones
|
|
|
|
def wait_for_harvest():
|
|
while not can_harvest():
|
|
pass
|
|
harvest()
|
|
|
|
def await_all(drones):
|
|
for thread in drones:
|
|
wait_for(thread) |