The Farmer Was Replaced - Code Examples

Complete code examples from the game with detailed explanations. Copy and paste these codes directly into your game to improve your farming automation skills.

Example 1: Basic Auto Harvest

The simplest automation script that continuously harvests mature grass.

# Auto harvest grass
while True:
    if can_harvest():
        harvest()

Example 2: 3×3 Farm Auto Traversal

Automatically traverse all tiles and harvest.

# Traverse entire farm for harvesting
while True:
    if can_harvest():
        harvest()

    # Move east
    move(East)

    # When reaching end of row, move north
    if get_pos_x() == get_world_size() - 1:
        move(North)

Example 3: Multi-Crop Automation (Grass + Bush + Carrot)

Plant different crops based on column position.

# First column: grass, second: bushes, third: carrots
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()

    if x == 0:
        # First column: grass (no planting needed)
        pass
    elif x == 1:
        # Second column: bushes
        plant(Entities.Bush)
    elif x == 2:
        # Third column: carrots
        if get_ground_type() != Grounds.Soil:
            till()
        if num_items(Items.Carrot_Seed) < 1:
            trade(Items.Carrot_Seed)
        plant(Entities.Carrot)

    move(East)
    if x == get_world_size() - 1:
        move(North)

Example 4: Tree Checkerboard Planting

Trees need checkerboard planting to avoid adjacency.

# Checkerboard pattern for trees and bushes
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()
    y = get_pos_y()

    # Checkerboard logic
    if (x % 2 == 0 and y % 2 == 0) or (x % 2 == 1 and y % 2 == 1):
        plant(Entities.Tree)
    else:
        plant(Entities.Bush)

    move(East)
    if x == get_world_size() - 1:
        move(North)

Example 5: Resource Priority Management

Intelligent resource management, prioritize collecting scarce resources.

# Auto-adjust planting strategy based on resources
while True:
    if can_harvest():
        harvest()

    x = get_pos_x()

    # Priority: ensure hay inventory first
    if num_items(Items.Hay) < 500:
        # Don't plant, let grass grow naturally
        pass
    # Second priority: ensure wood inventory
    elif num_items(Items.Wood) < 300:
        plant(Entities.Bush)
    # Finally: plant carrots
    elif num_items(Items.Carrot) < 200:
        if get_ground_type() != Grounds.Soil:
            till()
        if num_items(Items.Carrot_Seed) == 0:
            trade(Items.Carrot_Seed)
        plant(Entities.Carrot)

    move(East)
    if x == get_world_size() - 1:
        move(North)

Example 6: Auto Watering System

Automated watering system, speeds up crop growth up to 5x.

# Auto buy water tanks and water crops
while True:
    # Ensure enough water tanks
    if num_items(Items.Water_Tank) < 100:
        trade(Items.Empty_Tank)

    # Check and water
    if get_water() < 0.75:
        use_item(Items.Water_Tank)

    if can_harvest():
        harvest()

    # Planting and movement logic
    if get_ground_type() != Grounds.Soil:
        till()
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)
    plant(Entities.Carrot)

    move(East)
    if get_pos_x() == get_world_size() - 1:
        move(North)

Example 7: Function Encapsulation

Use functions to make code clearer and more readable.

# Define function to move to next tile
def move_to_next():
    x = get_pos_x()
    move(East)
    if x == get_world_size() - 1:
        move(North)

# Define function to plant carrots
def plant_carrot():
    if get_ground_type() != Grounds.Soil:
        till()
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)
    plant(Entities.Carrot)

# Main program
while True:
    if can_harvest():
        harvest()

    if num_items(Items.Carrot) < 100:
        plant_carrot()

    move_to_next()

Example 8: Sunflower Energy Optimization

Sunflower optimization strategy, only harvest high-energy flowers.

# Measure and harvest highest energy sunflowers
sunflowers = []
world_size = get_world_size()

# Plant and measure all sunflowers
for i in range(world_size * world_size):
    if get_ground_type() == Grounds.Turf:
        till()
    if num_items(Items.Sunflower_Seed) > 0 or trade(Items.Sunflower_Seed):
        plant(Entities.Sunflower)

    if get_entity_type() == Entities.Sunflower:
        sunflowers.append(measure())

    move(East)
    if get_pos_x() == 0:
        move(North)

# Find highest energy value
max_val = 0
max_index = 0
for i in range(len(sunflowers)):
    if sunflowers[i] > max_val:
        max_val = sunflowers[i]
        max_index = i

# Move to highest energy sunflower and harvest
# (Movement logic omitted)
if measure() == max_val:
    if can_harvest():
        harvest()