Files
farmer-was-replaced/utils.py

140 lines
3.0 KiB
Python

from __builtins__ import *
world_size = get_world_size()
def move_to(x, y):
while x > get_pos_x():
move(East)
while x < get_pos_x():
move(West)
while y > get_pos_y():
move(North)
while y < get_pos_y():
move(South)
# Harvest all tiles in a grid.
#
# Returns:
# None
def harvest_grid(width, height):
res = {}
x = get_pos_x()
y = get_pos_y()
grid_size = get_world_size()
if (x + width) > grid_size:
width = grid_size - x
if (y + height) > grid_size:
height = grid_size - y
others = {East: West, West: East}
side_dir = East
for row in range(height):
for col in range(width):
if can_harvest():
harvest()
# if it's not the last pass, then move
if col < width - 1:
if side_dir == East:
x = (x + 1) % grid_size
elif side_dir == West:
x = (x - 1) % grid_size
move(side_dir)
if row < height - 1:
side_dir = others[side_dir]
move(North)
y = (y + 1) % grid_size
return res
# Plant a square of the given list of entities.
#
# If the water level is below waterBelow,
# it will water the tile. If requireSoil is true, it will till the tile if it's not soil.
# plantWith - list of entities to plant. The entities will alternate if repeated.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
# plantWith (list): A list of entities to plant.
# waterBelow (float): The water level below which the tile will be watered.
# requireSoil (bool): If true, the tile will be tilled if it's not soil.
# onPlant (function): A function to call after each planting operation (optional). Parameters: x, y
#
# Returns:
# None
def plant_grid(width, height, plant_with, water_below, require_soil, on_plant = None):
res = {}
x = get_pos_x()
y = get_pos_y()
grid_size = get_world_size()
if (x + width) > grid_size:
width = grid_size - x
if (y + height) > grid_size:
height = grid_size - y
tracker = 0
others = {East: West, West: East}
side_dir = East
# first pass over, checking if anything can be harvested. if so, harvest.
# then check what type of ground is down, if it's not soil, then till.
# then if it's below 75% water and I have some water, give it some.
# then plant a pumpkin.
for row in range(height):
for col in range(width):
if can_harvest():
harvest()
if require_soil:
if get_ground_type() != Grounds.Soil:
till()
while get_water() < water_below:
if not use_item(Items.Water):
break
index = tracker % len(plant_with)
tracker += 1
success = plant(plant_with[index])
if not success:
print("Failed to plant at " + str(x) + ", " + str(y))
# if on_plant is not None: is giving me errors in the game, but this seems to be what they want
if on_plant:
on_plant(x, y)
# if it's not the last pass, then move
if col < width - 1:
if side_dir == East:
x = (x + 1) % grid_size
elif side_dir == West:
x = (x - 1) % grid_size
move(side_dir)
if row < height - 1:
side_dir = others[side_dir]
move(North)
y = (y + 1) % grid_size
return res