Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FarmLoops file:
- import FarmerManager
- import FarmFunctions
- import PumpkinUtils
- import CactusUtils
- import FarmPlan
- import Utils
- dead_patches = {}
- ### Multi drone ###
- def MultiFarmerPumpkinPatchLoop():
- for y in range(get_world_size()):
- if spawn_drone(HorizontalFarm):
- move(North)
- return
- def spawn_drone_wrapper(function, companion_x, companion_y, companion_type):
- def func():
- return function(companion_x, companion_y, companion_type)
- return func
- def CompanionFarmingLoop():
- for x in range(get_world_size()):
- for y in range(get_world_size()):
- # plant within our normal FarmPlan
- CheckeredFarm()
- if get_companion() != None:
- companion_type, pos = get_companion()
- companion_x, companion_y = pos
- #print("Spawning companion drone for: ", companion_type, " at ", pos)
- if spawn_drone(spawn_drone_wrapper(CompanionFarm, companion_x, companion_y, companion_type)):
- move(North)
- continue
- move(North)
- move(East)
- def CompanionFarm(companion_x, companion_y, companion_type):
- FarmerManager.MoveFarmer(companion_x, companion_y)
- FarmFunctions.Farm()
- if companion_type != Entities.Grass:
- FarmFunctions.TillableFarm(companion_type)
- def HorizontalFarm():
- basic_farm = True
- for x in range(get_world_size()):
- x, y = Utils.get_pos()
- if basic_farm == True:
- if Utils.is_even(x):
- if Utils.is_even(y):
- #FarmFunctions.TillableFarm(Entities.Carrot)
- FarmFunctions.Farm()
- else:
- FarmFunctions.TillableFarm(Entities.Sunflower)
- else:
- if Utils.is_even(y):
- FarmFunctions.TillableFarm(Entities.Tree)
- else:
- if y % 3 == 1:
- FarmFunctions.TillableFarm(Entities.Carrot)
- else:
- FarmFunctions.Farm()
- #FarmFunctions.TillableFarm(Entities.Sunflower)
- else:
- FarmFunctions.PumpkinFlowerFarm(x, y)
- #FarmFunctions.TillableFarm(Entities.Tree, True)
- move(East)
- #spawn_drone(ReverseHorizontalFarm)
- return
- def ReverseHorizontalFarm():
- basic_farm = True
- for x in range(get_world_size()):
- x, y = Utils.get_pos()
- if basic_farm == True:
- if Utils.is_even(x):
- if Utils.is_even(y):
- FarmFunctions.TillableFarm(Entities.Carrot)
- else:
- FarmFunctions.TillableFarm(Entities.Sunflower)
- else:
- if Utils.is_even(y):
- FarmFunctions.TillableFarm(Entities.Tree)
- else:
- if y % 3 == 1:
- FarmFunctions.TillableFarm(Entities.Carrot)
- else:
- FarmFunctions.Farm()
- else:
- FarmFunctions.PumpkinFlowerFarm(x, y)
- move(West)
- return
- def CheckeredFarm():
- x, y = Utils.get_pos()
- if Utils.is_even(x):
- if Utils.is_even(y):
- FarmFunctions.TillableFarm(Entities.Carrot)
- else:
- FarmFunctions.TillableFarm(Entities.Sunflower)
- else:
- if Utils.is_even(y):
- FarmFunctions.TillableFarm(Entities.Tree)
- else:
- if y % 3 == 1:
- FarmFunctions.TillableFarm(Entities.Carrot)
- else:
- FarmFunctions.Farm()
- ### single drone
- def PumpkinPatchLoop():
- FarmerManager.MoveFarmer(0,0)
- for x in range(get_world_size()):
- for y in range(get_world_size()):
- FarmingUtils.PumpkinFlowerFarm(x, y)
- move(North)
- move(East)
- for x in range(get_world_size()):
- for y in range(get_world_size()):
- if get_entity_type() == Entities.Sunflower:
- FarmFunctions.TillableFarm(Entities.Sunflower)
- if get_entity_type() == Entities.Dead_Pumpkin:
- FarmFunctions.PumpkinFlowerFarm(x, y)
- dead_patches[(x,y)] = 1
- move(North)
- move(East)
- while dead_patches:
- patches = []
- for patch in dead_patches:
- patches.append(patch)
- for patch in patches:
- dead_patches.pop(patch)
- x, y = patch
- FarmerManager.MoveFarmer(x, y)
- if get_entity_type() == Entities.Dead_Pumpkin:
- FarmFunctions.PumpkinFlowerFarm(x, y)
- dead_patches[(x,y)] = 1
- CactusUtils.sort_cactus()
- for name in FarmPlan.pumpkin_patches:
- (x0, y0, x1, y1) = FarmPlan.pumpkin_patches[name]
- FarmerManager.MoveFarmer(x0, y0)
- FarmFunctions.Farm()
- def FarmLoop():
- for x in range(get_world_size()):
- for y in range(get_world_size()):
- if Utils.is_even(y) and Utils.is_even(x):
- #FarmFunctions.Farm()
- FarmFunctions.TillableFarm(Entities.Carrot)
- elif Utils.is_odd(y) and Utils.is_odd(x):
- if y % 3 == 1 or y % 5 == 1:
- FarmFunctions.TillableFarm(Entities.Carrot)
- #FarmFunctions.Farm()
- else:
- FarmFunctions.TillableFarm(Entities.Carrot)
- #FarmFunctions.Farm()
- else:
- FarmFunctions.TillableFarm(Entities.Carrot)
- move(North)
- move(East)
- CactusUtils File:
- cactus_sizes = {}
- def sort_cactus():
- for (type, x0, y0, x1, y1) in FarmPlan.cactus_highways:
- cactuses = []
- for x in range(x0, x1 + 1):
- cactuses.append((x, y0))
- def size_at(key):
- return cactus_sizes[key]
- n = len(cactuses)
- if n <= 1:
- return
- FarmerManager.MoveFarmer(x0 + 1, y0)
- while True:
- swapped = False
- comparisons = max(0, n - 1)
- for i in range(1, comparisons + 1):
- left_key = cactuses[i - 1]
- right_key = cactuses[i]
- if size_at(left_key) > size_at(right_key):
- swap(West)
- swapped = True
- swap_val = Utils.swap_values(cactuses[i - 1], cactuses[i])
- cactuses[i - 1], cactuses[i] = swap_val
- move(East)
- else:
- move(East)
- n -= 1
- if not swapped:
- break
- for _ in range(comparisons):
- move(West)
- def record_sizes(x, y):
- cactus_sizes[(x,y)] = measure()
- PumpkinUtils File:
- import FarmPlan
- import Utils
- patch_2_pumpkinIds = {}
- def end_of_patch(x,y, patch):
- _, x0, y0, x1,y1 = patch
- if x == x1:
- return True
- return False
- def check_pumpkin_patch(x, y):
- global patch_2_pumpkinIds
- if get_entity_type() == Entities.Pumpkin:
- if FarmPlan.in_farm_plan(x, y, FarmPlan.pumpkin_patches):
- patch = FarmPlan.get_patch(x, y)
- if patch != None:
- id = measure()
- if patch not in patch_2_pumpkinIds:
- patch_2_pumpkinIds[patch] = set()
- patch_2_pumpkinIds[patch].add(id)
- else:
- patch_2_pumpkinIds[patch].add(id)
- if end_of_patch(x, y, patch):
- if len(patch_2_pumpkinIds[patch]) == 1:
- FarmingUtils.Farm()
- FarmerManager File:
- import Utils
- HATS = [Hats.Brown_Hat,
- Hats.Green_Hat, Hats.Purple_Hat]
- def ChangeHat():
- change_hat(HATS[random() * len(HATS)])
- def determine_move_xtype(diff_x):
- if diff_x > 0:
- return West
- elif diff_x < 0:
- return East
- def determine_move_ytype(diff_y):
- if diff_y > 0:
- return South
- elif diff_y < 0:
- return North
- def MoveFarmer(target_x, target_y):
- farmer_x, farmer_y = Utils.get_pos()
- diff_x = farmer_x - target_x
- diff_y = farmer_y - target_y
- x_dir = determine_move_xtype(diff_x)
- y_dir = determine_move_ytype(diff_y)
- if x_dir != None:
- for _ in range(abs(diff_x)):
- move(x_dir)
- if y_dir != None:
- for _ in range(abs(diff_y)):
- move(y_dir)
- FarmFunctions File:
- import FarmingUtils
- import PumpkinUtils
- import FarmPlan
- def Farm():
- if can_harvest():
- harvest()
- def TillableFarm(entity, fertilize=False):
- Farm()
- FarmingUtils.till_water_plant(entity, fertilize)
- def BushFarm():
- Farm()
- if get_entity_type() not in FarmPlan.farmed_entities:
- plant(Entities.Bush)
- def PumpkinFarm():
- FarmingUtils.till_water_plant(Entities.Pumpkin)
- def PumpkinFlowerFarm(x, y):
- if PumpkinPatchFarm(x, y):
- PumpkinUtils.check_pumpkin_patch(x, y)
- return
- if CactusHighwayFarm(x, y):
- return
- if SunflowerHighwayFarm(x, y):
- return
- TillableFarm(Entities.Carrot)
- def PumpkinPatchFarm(x, y):
- if FarmPlan.in_farm_plan(x,y, FarmPlan.pumpkin_patches):
- TillableFarm(Entities.Pumpkin, True)
- return True
- return False
- def SunflowerHighwayFarm(x, y):
- if FarmPlan.in_farm_plan(x,y, FarmPlan.sunflower_highways):
- TillableFarm(Entities.Sunflower)
- return True
- return False
- def CactusHighwayFarm(x, y):
- if FarmPlan.in_farm_plan(x,y, FarmPlan.cactus_highways):
- TillableFarm(Entities.Cactus)
- return True
- return False
- FarmingUtils File:
- import FarmPlan
- import PumpkinUtils
- import CactusUtils
- import FarmerManager
- import Utils
- def water():
- if get_water() <= 0.5:
- use_item(Items.Water)
- def till_water_plant(entity, fertilize):
- if get_ground_type() != Grounds.Soil:
- till()
- if get_ground_type() == Grounds.Soil:
- water()
- plant(entity)
- if fertilize:
- use_item(Items.Fertilizer)
- if get_entity_type() == Entities.Cactus:
- x, y = Utils.get_pos()
- CactusUtils.record_sizes(x,y)
- main file:
- import FarmLoops
- import FarmerManager
- import MazeSolver
- import Utils
- clear()
- def analyze_runs(runs):
- total_time = 0
- total_chests_solved = 0
- for run in runs:
- total_time += runs[run]["time"]
- total_chests_solved += runs[run]["chests_solved"]
- avg_time = total_time / len(runs)
- avg_chests = total_chests_solved / len(runs)
- move(South)
- move(South)
- print("Over the course of ", len(runs), " runs,")
- move(South)
- move(South)
- print("the drones found ", total_chests_solved, " chests.")
- move(South)
- move(South)
- print("It took an average", avg_time, " seconds")
- move(South)
- move(South)
- print("to find ", avg_chests , " chests per run.")
- def spawn_drone_wrapper(function, x, y):
- def func():
- return function(x, y)
- return func
- def maze_loop_multi(x, y):
- FarmerManager.MoveFarmer(x, y)
- first_flag = False
- while True:
- if not first_flag:
- if x == 24 and y == 24:
- MazeSolver.SpawnMaze()
- first_flag = True
- else:
- do_a_flip()
- MazeSolver.SpawnMaze()
- if get_entity_type() != Entities.Hedge:
- continue
- MazeSolver.SolveMaze()
- DRONE_LOCATIONS = [
- (7, 24),
- (7, 7),
- (24, 7),
- (24, 24),
- (16, 16)
- ]
- def better_maze_loop():
- for location in DRONE_LOCATIONS:
- x,y = location
- spawn_drone(spawn_drone_wrapper(maze_loop_multi, x,y))
- def MazeLoop():
- #set_execution_speed(1)
- runs = {}
- i = 0
- while True:
- start_time = get_time()
- MazeSolver.SpawnMaze()
- chests_solved = MazeSolver.SolveMaze()
- total_time = get_time() - start_time
- print("total time: ", total_time)
- #move(South)
- #move(South)
- print("chests found: ", chests_solved)
- runs[i] = {"time": total_time, "chests_solved": chests_solved}
- i+=1
- analyze_runs(runs)
- def CompanionLoop():
- farmer_count = 6
- while True:
- for farmer in range(farmer_count):
- spawn_drone(FarmLoops.CompanionFarmingLoop)
- for i in range(farmer):
- move(West)
- better_maze_loop()
- #MazeLoop()
- #while True:
- # FarmLoops.MultiFarmerPumpkinPatchLoop()
- Maze Solver file:
- import FarmerManager
- import Utils
- import MazeUtils
- maze_map = {}
- path_taken = []
- chest_positions = set()
- last_seen_chest_count = len(chest_positions)
- current_generation = 0
- last_hint = None
- def get_hint_cached():
- global last_hint
- hint = safe_treasure_hint()
- if hint != None:
- last_hint = hint
- return last_hint
- def reset_state():
- global maze_map
- global path_taken
- global current_generation
- global last_hint
- maze_map = {}
- path_taken = []
- current_generation += 1
- last_hint = None
- def is_deadend(x,y):
- if (x,y) in maze_map:
- return maze_map[(x,y)]["dead_end"] == True or maze_map[(x,y)]["dead_path"] == True
- return False
- def was_visited(x, y):
- if (x, y) in maze_map and maze_map[(x, y)]["visited"] == True :
- return True
- return False
- def collect_treasure():
- global chest_positions
- if get_entity_type() == Entities.Treasure:
- if len(chest_positions) >= MazeUtils.NUM_CHESTS:
- harvest()
- chest_positions = set()
- return True
- else:
- if MazeUtils.CUSTOM_MAZE_SIZE == 0:
- substance = get_world_size() * 2**(num_unlocked(Unlocks.Mazes) - 1)
- else:
- substance = MazeUtils.CUSTOM_MAZE_SIZE
- if not use_item(Items.Weird_Substance, substance):
- harvest()
- chest_positions = set()
- return True
- return False
- def is_hedge():
- return get_entity_type() == Entities.Hedge
- def safe_treasure_hint():
- if not is_hedge():
- return None
- hint = measure()
- if hint == None:
- return None
- if len(hint) == 2:
- return (hint[0], hint[1])
- return None
- def evaluate_position():
- if not is_hedge():
- return None
- hint = get_hint_cached()
- if hint == None:
- return None
- chest_x, chest_y = hint
- x, y = Utils.get_pos()
- score = Utils.manhattan(x, y, chest_x, chest_y)
- return score
- def spawn_in_direction(directions):
- global maze_map
- global chest_positions
- global current_generation
- best_dir = choose_direction(directions)
- other_dirs = []
- for dir in directions:
- if dir != best_dir:
- other_dirs.append(dir)
- drone_ids = []
- for dir in other_dirs:
- current_generation += 1
- drone_id = spawn_drone(spawn_drone_wrapper(SolveMazeShadowClone, maze_map, dir, chest_positions, current_generation))
- drone_ids.append(drone_id)
- return best_dir, drone_ids
- def log_dir_and_move(direction):
- global path_taken
- if direction == None:
- return
- path_taken.append(direction)
- move(direction)
- global last_hint
- last_hint = None
- def filter_visited(available_directions):
- unvisited = []
- for dir in available_directions:
- x, y = Utils.dir_increment(dir)
- if not was_visited(x,y) and not is_deadend(x, y):
- unvisited.append(dir)
- return unvisited
- def choose_n_move(unvisited):
- direction = choose_direction(unvisited)
- if direction == None:
- return True
- log_dir_and_move(direction)
- return True
- def spawn_n_move(unvisited):
- direction, drone_ids = spawn_in_direction(unvisited)
- if direction == None:
- return True
- log_dir_and_move(direction)
- return True
- def chest_moved():
- global last_seen_chest_count
- global chest_positions
- temp_chest_count = last_seen_chest_count
- last_seen_chest_count = len(chest_positions)
- return temp_chest_count != last_seen_chest_count
- def spawn_drone_wrapper(function, arg_map, direction, chest_positions, gen):
- def func():
- return function(arg_map, direction, chest_positions, gen)
- return func
- def map_sourroundings():
- global chest_positions
- global maze_map
- if not is_hedge():
- return [], Utils.get_pos()
- hint = safe_treasure_hint()
- if hint != None and hint not in chest_positions:
- chest_positions.add(hint)
- coordinates = Utils.get_pos()
- connections = []
- for d in Utils.DIRS:
- if can_move(d):
- connections.append(d)
- if coordinates not in maze_map:
- maze_map[coordinates] = {
- "connections": connections,
- "dead_end": (len(connections) == 1),
- "visited": True,
- "dead_path": False
- }
- else:
- maze_map[coordinates]["connections"] = connections
- return connections, coordinates
- def choose_direction(directions):
- if not is_hedge():
- return directions[0]
- hint = get_hint_cached()
- if hint == None:
- return directions[0]
- chest_x, chest_y = hint
- best_score = 1000000
- best_dir = directions[0]
- for dir in directions:
- x, y = Utils.dir_increment(dir)
- score = Utils.manhattan(x, y, chest_x, chest_y)
- if score < best_score:
- best_score = score
- best_dir = dir
- return best_dir
- def SolveMaze():
- global maze_map
- global path_taken
- global chest_positions
- global last_hint
- reset_state()
- while True:
- chests_collected = len(chest_positions)
- if chests_collected >= MazeUtils.NUM_CHESTS:
- chest_positions = set()
- return chests_collected
- if chest_moved():
- reset_state()
- if collect_treasure():
- reset_state()
- if outside_maze():
- return len(chest_positions)
- available_directions, coordinates = map_sourroundings()
- unvisited = filter_visited(available_directions)
- if len(unvisited) >= 2:
- if spawn_n_move(unvisited):
- continue
- if len(unvisited) > 0:
- if choose_n_move(unvisited):
- continue
- if len(path_taken) == 0:
- return len(chest_positions)
- if outside_maze():
- return len(chest_positions)
- maze_map[coordinates]["dead_path"] = True
- last_dir = path_taken.pop()
- move(Utils.reverse_dir(last_dir))
- def outside_maze():
- if get_entity_type() != Entities.Hedge and get_entity_type() != Entities.Treasure:
- return True
- return False
- def SolveMazeShadowClone(map, direction, chest_poses, my_generation):
- global maze_map
- global chest_positions
- global path_taken
- global current_generation
- global last_hint
- chest_positions = chest_poses
- maze_map = map
- path_taken = []
- last_hint = None
- change_hat(FarmerManager.HATS[my_generation % len(FarmerManager.HATS)])
- log_dir_and_move(direction)
- allowed_moves = MazeUtils.ALLOWED_MOVES
- while True:
- if outside_maze():
- return
- if collect_treasure():
- return
- if allowed_moves <= 0:
- return
- available_directions, coordinates = map_sourroundings()
- unvisited = filter_visited(available_directions)
- allowed_moves -= 1
- score = evaluate_position()
- if score != None and score <= 14:
- allowed_moves += 50
- if outside_maze():
- return
- if len(unvisited) >= 2:
- if spawn_n_move(unvisited):
- continue
- if len(unvisited) > 0:
- if choose_n_move(unvisited):
- continue
- if len(path_taken) == 0:
- return
- if score > 10:
- return
- maze_map[coordinates]["dead_path"] = True
- last_dir = path_taken.pop()
- move(Utils.reverse_dir(last_dir))
- def SpawnMaze():
- use_item(Items.Water)
- plant(Entities.Bush)
- use_item(Items.Fertilizer)
- while not can_harvest():
- FarmerManager.ChangeHat()
- if MazeUtils.CUSTOM_MAZE_SIZE == 0:
- substance = get_world_size() * 2**(num_unlocked(Unlocks.Mazes) - 1)
- else:
- substance = MazeUtils.CUSTOM_MAZE_SIZE
- use_item(Items.Weird_Substance, substance)
- Utils File:
- DIRS = [North, East, South, West]
- def get_pos():
- return(get_pos_x(), get_pos_y())
- def is_even(n):
- return n % 2 == 0
- def is_odd(n):
- return n % 2 != 0
- def in_rect(x, y, x0, y0, x1, y1):
- return (x0 <= x <= x1) and (y0 <= y <= y1)
- def in_vertical(x, y, x_const, y0, y1):
- return (x == x_const) and (y0 <= y <= y1)
- def in_horizontal(x, y, y_const, x0, x1):
- return (y == y_const) and (x0 <= x <= x1)
- def swap_values(x, y):
- return y, x
- def reverse_dir(dir):
- if dir == North:
- return South
- if dir == South:
- return North
- if dir == East:
- return West
- if dir == West:
- return East
- def dir_increment(dir, coordinates=None, x=None, y=None):
- if coordinates != None:
- x, y = coordinates
- if coordinates == None and x == None:
- x, y = get_pos()
- if dir == North:
- y += 1
- elif dir == South:
- y -= 1
- elif dir == East:
- x += 1
- elif dir == West:
- x -= 1
- return x, y
- def manhattan(x1, y1, x2, y2):
- return abs(x1 - x2) + abs(y1 - y2)
Advertisement
Add Comment
Please, Sign In to add comment