The Farmer Was Replaced

The Farmer Was Replaced

Code Share
Not meant to be a cheat/shortcut, just a place to share quirky or useful basic codes, such as:

-----------------------------

def RETURNHOME(): while get_pos_x() > 0: move(West) while get_pos_y() > 0: move(South)
-----------------------------

Simply puts the drone at the start point (0,0)

NOTE: Use

(code)

(/code)
Using square brackets instead, to keep the indented format (Thank you Ryan for showing me how to do that :D)
< >
Showing 1-15 of 42 comments
oh interesting! I did something similar;
I made 2 functions, one that allows multiple moves called mv:

def mv(str, int): x = 0 while x != int: move(str) x += 1

then made this to reset your position to 0, 0:

def retToOrigin(): mv(South, get_pos_y()) mv(West, get_pos_x())

edited out my abbreviated code
Originally posted by Ryan:
oh interesting! I did something similar;
I made 2 functions, one that allows multiple moves called mv:

def mv(str, int): x = 0 while x != int: move(str) x += 1

then made this to reset your position to 0, 0:

def retToOrigin(): mv(South, get_pos_y()) mv(West, get_pos_x())

edited out my abbreviated code

I am in no way shape or form a coder (I've dabbled in Unity C++, but then who hasn't :P lol), so I have no idea how robust the code is beyond the help window :P. Sometimes I feel like I'm doing surgery with a brick...but somehow its working :P
Originally posted by Duros001:

I am in no way shape or form a coder (I've dabbled in Unity C++, but then who hasn't :P lol), so I have no idea how robust the code is beyond the help window :P. Sometimes I feel like I'm doing surgery with a brick...but somehow its working :P

You're not the only one, I know really basic stuff and can make small tools but everything else is pure trial and error lol, I'm struggling so badly with making a maze solver, I feel like you've got to be a genius to get do that!
Originally posted by Ryan:
Originally posted by Duros001:

I am in no way shape or form a coder (I've dabbled in Unity C++, but then who hasn't :P lol), so I have no idea how robust the code is beyond the help window :P. Sometimes I feel like I'm doing surgery with a brick...but somehow its working :P

You're not the only one, I know really basic stuff and can make small tools but everything else is pure trial and error lol, I'm struggling so badly with making a maze solver, I feel like you've got to be a genius to get do that!

IKR! I'm just thinking of a way to brute-force it! like maybe try to move one space in each direction clockwise, then same anti-clockwise, then increase the number of L,U,U,R,D etc, lol, and between each line have
if is_over(Entities.Treasure): harvest() else: pass
lol
Haha that's exactly what I tried to do as well!


def mazeSolver(): x = get_pos_x() y = get_pos_y() z = listxy() # Returns a list of [x, y] dir = [North, West, East, South] while True: if is_over(Entities.Treasure): harvest() break else: z = listxy() move(North) if z[1] == y: z = listxy() move(East) if z[0] == x: z = listxy() move(South) if z[1] == y(): break


The way I code is awful as well, I hardly ever rename things so I end up with loads of f1(), f2() in my code.

I tried to write this in such a way that it'll check what moves it can make but I really have no idea what I'm doing lol
Originally posted by Ryan:
Haha that's exactly what I tried to do as well!


def mazeSolver(): x = get_pos_x() y = get_pos_y() z = listxy() # Returns a list of [x, y] dir = [North, West, East, South] while True: if is_over(Entities.Treasure): harvest() break else: z = listxy() move(North) if z[1] == y: z = listxy() move(East) if z[0] == x: z = listxy() move(South) if z[1] == y(): break


The way I code is awful as well, I hardly ever rename things so I end up with loads of f1(), f2() in my code.

I tried to write this in such a way that it'll check what moves it can make but I really have no idea what I'm doing lol
Haha, all my code made it do was dance in concentric circles :P
Originally posted by Duros001:
Haha, all my code made it do was dance in concentric circles :P

It's a start! Mine usually gets stuck in corridors so it constantly goes up and down forever lol, I wonder if we just keep hammering at this enough if it'll just work one time haha!
Brute forcing it for the cash you initially need for speed upgrades before you can figure out a way to solve it also works
def money(): while(True): plant(Entities.Bush) trade(Items.Fertilizer) use_item(Items.Fertilizer) harvest()
this costs a lot of pumpkins but it does work
Originally posted by Dr.Smush:
Brute forcing it for the cash you initially need for speed upgrades before you can figure out a way to solve it also works
def money(): while(True): plant(Entities.Bush) trade(Items.Fertilizer) use_item(Items.Fertilizer) harvest()
this costs a lot of pumpkins but it does work

Haha! The ultimate brute force, just throw enough bushes and fertiliser into the ether until it lands under the drone, I genuinely hadn't even considered that a possibility, I guess if you have a 6x6 grid the chanced are 1 in 36 for it to land right under the drone which isn't terrible odds!
def inqItem(ent): for i in get_inventory(): if i[0] == ent: return i[1]

I wrote a little tool to check how much you own of a given item, so if you run inqItem(Items.Wood) it'll tell you how much wood you have, good for automating buying seeds if you need it :)
i wrote an automation on farming;

Main loop
Set farm plots
bottom row plants a row of wheat, tree, wheat, tree, etc
left column is all carrots
the rest is pumpkin

def main(): while True: for i in range(get_world_size()): for j in range(get_world_size()): if j > 0: if i > 0: plant_all(3) else: plant_all(2) else: plant_all((i + j) % 2) move(North) move(East)

I have two lists, with wheat, tree, carrots, and pumpkins and the other list is their seeds.
It then checks if the soil's hydrated, and then waters it. If there's no water tank, buy an empty tank. Eventually you should get a net neutral water gain.
Next is to check if the ground needs to be tilled for seeds or not for wheat.
It then harvests if it can. smaller farms means your drone cycles back before some things can grow back.
Now it loops. This is mostly for pumpkins.
It checks if we have seeds in our inventory. if not, we buy new ones.
It then uses Fertilizer. If the pumpkin dies, it tries again until it grows.
This allows for a max sized pumpkin for net gain.

def plant_all(type): plants = [Entities.Grass, Entities.Tree, Entities.Carrots, Entities.Pumpkin] seed = [0, 0, Items.Carrot_Seed, Items.Pumpkin_Seed] if get_water() < 0.25: if not check_inventory(Items.Water_Tank): trade(Items.Empty_Tank) use_item(Items.Water_Tank) if type > 1: if is_over(Grounds.Turf): till() else: if is_over(Grounds.Soil): till() if can_harvest(): harvest() while True: if type > 1: if not check_inventory(seed[type]): trade(seed[type]) plant(plants[type]) if not check_inventory(Items.Fertilizer): trade(Items.Fertilizer) use_item(Items.Fertilizer) if can_harvest(): break

Inventory checker. It basically checks if you have the item in your inventory. Any item at 0 basically doesn't show up under get_inventory().

def check_inventory(item): check = False inventory = get_inventory() for i in range(len(inventory)): inventory1 = inventory
if inventory1[0] == item:
check = True
break

return(check)[/code]
Here's what I did:
def maze(): clear() plant(Entities.Bush) if get_item_amt(Items.Fertilizer) == 0: trade(Items.Fertilizer) use_item(Items.Fertilizer) facing = North while not is_over(Entities.Treasure): prevX = get_pos_x() prevY = get_pos_y() for dir in get_directions(facing): move(dir) if get_pos_x() != prevX or get_pos_y() != prevY: facing = dir break harvest()

And the helper function:
def get_directions(facing): if facing == North: return [West, North, East, South] elif facing == East: return [North, East, South, West] elif facing == South: return [East, South, West, North] else: return [South, West, North, East]

And:
def get_item_amt(item): for i in get_inventory(): if i[0] == item: return i[1] return 0

Works every time, and never misses the treasure.
Originally posted by Radial:
i wrote an automation on farming;

Main loop
Set farm plots
bottom row plants a row of wheat, tree, wheat, tree, etc
left column is all carrots
the rest is pumpkin

def main(): while True: for i in range(get_world_size()): for j in range(get_world_size()): if j > 0: if i > 0: plant_all(3) else: plant_all(2) else: plant_all((i + j) % 2) move(North) move(East)

I have two lists, with wheat, tree, carrots, and pumpkins and the other list is their seeds.
It then checks if the soil's hydrated, and then waters it. If there's no water tank, buy an empty tank. Eventually you should get a net neutral water gain.
Next is to check if the ground needs to be tilled for seeds or not for wheat.
It then harvests if it can. smaller farms means your drone cycles back before some things can grow back.
Now it loops. This is mostly for pumpkins.
It checks if we have seeds in our inventory. if not, we buy new ones.
It then uses Fertilizer. If the pumpkin dies, it tries again until it grows.
This allows for a max sized pumpkin for net gain.

def plant_all(type): plants = [Entities.Grass, Entities.Tree, Entities.Carrots, Entities.Pumpkin] seed = [0, 0, Items.Carrot_Seed, Items.Pumpkin_Seed] if get_water() < 0.25: if not check_inventory(Items.Water_Tank): trade(Items.Empty_Tank) use_item(Items.Water_Tank) if type > 1: if is_over(Grounds.Turf): till() else: if is_over(Grounds.Soil): till() if can_harvest(): harvest() while True: if type > 1: if not check_inventory(seed[type]): trade(seed[type]) plant(plants[type]) if not check_inventory(Items.Fertilizer): trade(Items.Fertilizer) use_item(Items.Fertilizer) if can_harvest(): break

Inventory checker. It basically checks if you have the item in your inventory. Any item at 0 basically doesn't show up under get_inventory().

def check_inventory(item): check = False inventory = get_inventory() for i in range(len(inventory)): inventory1 = inventory
if inventory1[0] == item:
check = True
break

return(check)[/code] [/quote]

Please share your get_inventory() funtion. Thank you.
As a beginner coder, I went to hell and back with the maze code. Here is what my current solution looks like. The idea is to create a dictionary whose key is a set of x/y coordinates already visited, and for each key, another dictionary whose keys are the four directions and the number of times they've been tried.

visited = {(6,7), {North:0, East:0, West:0, South:0}}

Next direction is decided by comparing the numbers in the nested dictionary, and choosing the direction with the least attempts. Try a direction, worked, add 1 to the current direction, as well as add 1 to the next position in the opposite direction.

Example: current position is (6,7), try moving East to (7,7), so it will look like this:

visited = {(6,7):{North: 0, East: 1, West: 0, South: 0}, (7,7):{North: 0, East: 0, West: 1, South: 0}}

If a direction doesn't work, I just add 20 to this direction, which is arbitrary but has been working so far. This is useful to avoid immediate backtracking, but it will backtrack if there's no other option (deadend is found), and will attempt a new direction if available. I haven't seen it get stuck in circuit paths with this solution.

def find_treasure(): direction = North visited = {} curr_pos = (get_pos_x(), get_pos_y()) update_visited(visited, direction, curr_pos) visited[curr_pos][direction] -= 1 while not get_entity_type() == Entities.Treasure: while True: curr_pos = (get_pos_x(), get_pos_y()) move(direction) new_pos = (get_pos_x(), get_pos_y()) if get_entity_type() == Entities.Treasure: break if curr_pos == new_pos: visited = update_visited(visited, direction, curr_pos) visited[curr_pos][direction] += 20 (visited, direction, curr_pos) = try_least_visited(visited, direction, curr_pos) else: visited = update_visited(visited,direction, curr_pos) visited = update_visited(visited,opposite_direction(direction), new_pos) (visited, direction, curr_pos) = try_least_visited(visited, direction, curr_pos) harvest()

def update_visited(visited,direction, curr_pos): if curr_pos not in visited: visited[curr_pos] = {North:0,South:0,East:0,West:0} visited[curr_pos][direction] += 1 else: visited[curr_pos][direction] += 1 return visited

def opposite_direction(direction): if direction == North: return South if direction == East: return West if direction == South: return North if direction == West: return East

def try_least_visited(visited,direction,curr_pos): curr_pos = (get_pos_x(), get_pos_y()) least_tried = North for dir in [South, East, West]: if visited[curr_pos][least_tried] == 0: return (visited, least_tried, curr_pos) elif visited[curr_pos][dir] < visited[curr_pos][least_tried]: least_tried = dir direction = least_tried return (visited, direction, curr_pos)
my little overnight carrot farm
also works with bushes :D
while True: start = 0 worldsize = get_world_size() while start < worldsize: for i in range(worldsize): planted=get_entity_type() if planted==None: trade(Items.Carrot_Seed) plant(Entities.Carrots) if can_harvest: harvest() trade(Items.Carrot_Seed) plant(Entities.Carrots) move(North) move(East) start = start+1

Heres my "semi automatic pumpkin farmer)
start = 0 worldsize = get_world_size() while start < worldsize: for i in range(worldsize): planted=get_entity_type() if planted==None: trade(Items.Pumpkin_Seed) plant(Entities.Pumpkin) move(North) move(East) start = start+1
< >
Showing 1-15 of 42 comments
Per page: 1530 50

Date Posted: Feb 14, 2023 @ 1:11am
Posts: 42