Advertisement
juh9870

Untitled

May 14th, 2024 (edited)
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. def fertilize():
  2.     if not use_item(Items.Fertilizer):
  3.         if get_entity_type() == None:
  4.             return True
  5.         if num_items(Items.Fertilizer) == 0 and not trade(Items.Fertilizer, 100):
  6.             return False
  7.         if not use_item(Items.Fertilizer):
  8.             return False
  9.     return True
  10.  
  11. def xy_to_idx(x, y):
  12.         return y * get_world_size() + x
  13.  
  14. def idx_to_xy(idx):
  15.     s = get_world_size()
  16.     return idx % s, idx // s
  17.  
  18. # Assuming we start somewhere in the middle for more efficient pathing
  19. def maze():
  20.     SIZE = get_world_size()
  21.     SIZE_M_1 = SIZE - 1
  22.    
  23.     U = 0
  24.     R = 1
  25.     D = 2
  26.     L = 3
  27.     DIRS = [U, R, D, L]
  28.     BACK = [D, L, U, R]
  29.     MOVES = [North, East, South, West]
  30.     SYMBOLS = ["^", ">", "v", "<"]
  31.     OFFSETS = [SIZE, 1, -SIZE, -1]
  32.    
  33.     # Tuples of (back, distance, exhausted)
  34.     flow = []
  35.     for i in range(SIZE ** 2):
  36.         flow.append(None)
  37.    
  38.     ox = get_pos_x()
  39.     oy = get_pos_y()
  40.     oi = xy_to_idx(ox, oy)
  41.    
  42.     flow[oi] = [None, 0, False]
  43.  
  44.     def navigate_to(target):
  45.         back_path = {-1}
  46.         moves_back = []
  47.         moves = []
  48.         pos = xy_to_idx(get_pos_x(), get_pos_y())
  49.         if pos == target:
  50.             return
  51.        
  52.         intersection = oi
  53.        
  54.         while pos != oi:
  55.             back_path.add(pos)
  56.             moves_back.append(flow[pos][0])
  57.             pos += OFFSETS[flow[pos][0]]
  58.         while target != oi:
  59.             if target in back_path:
  60.                 intersection = target
  61.                 break
  62.             dir = flow[target][0]
  63.             moves.insert(0, BACK[dir])
  64.             target += OFFSETS[dir]
  65.        
  66.         for dir in moves_back:
  67.             if xy_to_idx(get_pos_x(), get_pos_y()) == intersection:
  68.                 break
  69.             if not move(MOVES[dir]):
  70.                 print("Path: ")
  71.                 print(moves)
  72.                 print("Attempted to make a bad move" * 5)
  73.         for dir in moves:
  74.             if not move(MOVES[dir]):
  75.                 print("Path: ")
  76.                 print(moves)
  77.                 print("Attempted to make a bad move" * 5)
  78.  
  79.     def can_explore_dir(pos, dir):
  80.         from = flow[pos]
  81.         # Do not go back
  82.         if dir == from[0]:
  83.             return False
  84.            
  85.         x, y = idx_to_xy(pos)
  86.        
  87.         # Do not go OOB
  88.         if dir == U and y == SIZE_M_1:
  89.             return False
  90.         if dir == D and y == 0:
  91.             return False
  92.         if dir == L and x == 0:
  93.             return False
  94.         if dir == R and x == SIZE_M_1:
  95.             return False
  96.        
  97.         return True
  98.    
  99.     treasure = [None]
  100.  
  101.     def explore(max_line):
  102.         for i in range(SIZE ** 2):
  103.             flow[i] = None
  104.         flow[oi] = (None, 0)
  105.    
  106.         to_explore = {oi: 0}
  107.         linear = []
  108.         while len(to_explore) > 0 or len(linear) > 0:
  109.             #quick_print("= STEP =")
  110.             if get_entity_type() == Entities.Treasure:
  111.                 treasure[0] = pos
  112.                
  113.             pos = oi
  114.             if len(linear) > 0:
  115.                 pos = linear.pop()
  116.             else:
  117.                 min_distance = 999999
  118.                 for p in to_explore:
  119.                     dist = to_explore[p]
  120.                     if dist < min_distance:
  121.                         min_distance = dist
  122.                         pos = p
  123.             navigate_to(pos)
  124.            
  125.             data = flow[pos]
  126.            
  127.             end_linear = False
  128.            
  129.             next_distance = data[1] + 1
  130.        
  131.             viable_dirs = []
  132.            
  133.             for dir in DIRS:
  134.                 if not can_explore_dir(pos, dir):
  135.                     continue
  136.                    
  137.                 other_pos = pos + OFFSETS[dir]
  138.                 other = flow[other_pos]
  139.                 # Explored cell is distance 1 away, ignore it
  140.                 if other != None and abs(other[1] - data[1]) <= 1:
  141.                     continue
  142.  
  143.                 # Can't move to that cell, ignore it
  144.                 if not move(MOVES[dir]):
  145.                     continue
  146.                 move(MOVES[BACK[dir]])
  147.                
  148.                 linear.append(other_pos)
  149.                 if other == None:
  150.                     flow[other_pos] = (BACK[dir], next_distance)
  151.                 else:
  152.                     if other[1] > next_distance:
  153.                         flow[other_pos] = (BACK[dir], next_distance)
  154.                     end_linear = True
  155.                     linear.pop()
  156.                     continue
  157.            
  158.             if pos in to_explore:
  159.                 to_explore.pop(pos)
  160.            
  161.             if end_linear or len(linear) > max_line:
  162.                 for pos in linear:
  163.                     to_explore[pos] = flow[pos][1]
  164.                 linear = []
  165.            
  166.             continue # old code below
  167.             if not move(MOVES[dir]):
  168.                 continue
  169.             pos += OFFSETS[dir]
  170.             flow[pos] = (BACK[dir], data[1] + 1)
  171.             for dir in DIRS:
  172.                 if can_explore_dir(pos, dir):
  173.                     to_explore.append((pos, dir))
  174.  
  175.     # Will break with cycles
  176.     def dumb_explore():
  177.         def try_explore_dir(pos, dir):
  178.             from = flow[pos]
  179.             # Do not go back
  180.             if dir == from[0]:
  181.                 return False
  182.    
  183.             x, y = idx_to_xy(pos)
  184.    
  185.             # Do not go OOB
  186.             if dir == U and y == SIZE_M_1:
  187.                 return False
  188.             if dir == D and y == 0:
  189.                 return False
  190.             if dir == L and x == 0:
  191.                 return False
  192.             if dir == R and x == SIZE_M_1:
  193.                 return False
  194.    
  195.             cell = flow[pos + OFFSETS[dir]]
  196.             if cell == None:
  197.                 return True
  198.    
  199.             # Do not go to exhausted cells
  200.             return not cell[2]
  201.  
  202.         for i in range(SIZE ** 2):
  203.             flow[i] = None
  204.         flow[oi] = [None, 0, False]
  205.  
  206.         pos = oi
  207.         while True:
  208.             if get_entity_type() == Entities.Treasure:
  209.                 treasure[0] = pos
  210.             moved = False
  211.             for dir in DIRS:
  212.                 try_explore_dir(pos, dir)
  213.             for dir in DIRS:
  214.                 if try_explore_dir(pos, dir) and move(MOVES[dir]):
  215.                     moved = True
  216.                     distance = flow[pos][1] + 1
  217.                     pos += OFFSETS[dir]
  218.                     flow[pos] = [BACK[dir], distance, False]
  219.                     break
  220.             if not moved:
  221.                 if pos == oi:
  222.                     return
  223.                 flow[pos][2] = True
  224.                 dir = flow[pos][0]
  225.                 move(MOVES[dir])
  226.                 pos += OFFSETS[dir]
  227.    
  228.     dumb_explore()
  229.    
  230.     iter = 0
  231.     while True:
  232.         iter += 1
  233.         if iter == 100 or iter == 200:
  234.             navigate_to(oi)
  235.             explore(35 - (iter // 10))
  236.  
  237.         navigate_to(treasure[0])
  238.         next = measure()
  239.         if next == None:
  240.             harvest()
  241.             return
  242.         nx, ny = next
  243.         treasure = [xy_to_idx(nx,ny)]
  244.         while get_entity_type() == Entities.Treasure:
  245.             fertilize()
  246.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement