RareUI

The Farmer Bot

Mar 29th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.09 KB | None | 0 0
  1. # Bot for The Farmer
  2. # http://pelikone.fi/pelit/muut/The-Farmer/85
  3. # Make sure the top-left of the game's main menu is visible.
  4. # Ensure there's no brownish yellow visible (32, 186, 165 to be exact)
  5. #
  6. # Download associated bitmaps from
  7. # https://dl.dropboxusercontent.com/u/22984741/python/autopy/farmer/farmer.zip
  8.  
  9. import autopy
  10. import time
  11. import itertools
  12. import sys
  13.  
  14. screen_x, screen_y = 0, 0
  15. screen_width, screen_height = 600, 420
  16. x_offset, y_offset = 0, 0 #-230, -130
  17.  
  18. product_order = ['grape', 'watermelon', 'strawberry',
  19.                  'corn', 'tomato', 'potato', 'carrot']#, 'turnip']
  20.  
  21. worm_color = autopy.color.rgb_to_hex(32, 186, 165)
  22.  
  23. class Globals:
  24.     day = 0
  25.     screen = 'farm'
  26.     money = 400
  27.     owned = {'turnip': 0,
  28.              'carrot': 0,
  29.              'potato': 0,
  30.              'tomato': 0,
  31.              'corn': 0,
  32.              'strawberry': 0,
  33.              'grape': 0,
  34.              'watermelon': 0,
  35.              'fertilizer': 0,
  36.              'bugkiller': 0}
  37.     planted = [None, None, None, None]
  38.     plant_amount = [0, 0, 0, 0]
  39.     harvests_left = [0, 0, 0, 0]
  40.     growtime = [0, 0, 0, 0]
  41.  
  42. def find_top_left(screen):
  43.     top_left_bmp = autopy.bitmap.Bitmap.open("top_left.png")
  44.     return screen.find_bitmap(top_left_bmp)
  45.  
  46. def find_bottom_right(screen):
  47.     bottom_right_bmp = autopy.bitmap.Bitmap.open("bottom_right.png")
  48.     pos = screen.find_bitmap(bottom_right_bmp)
  49.     if pos:
  50.         x, y = pos
  51.         return x + 91, y + 24
  52.     return None
  53.  
  54. def move(x, y):
  55.     autopy.mouse.move(screen_x + x_offset + int(x*screen_width),
  56.                       screen_y + y_offset + int(y*screen_height))
  57.  
  58. def click(x=None, y=None, sleep=.1):
  59.     if x is not None:
  60.         if not (0 <= x < 1 and 0 <= y < 1):
  61.             print 'Click outside of the game window at', (x, y)
  62.             sys.exit()
  63.         move(x, y)
  64.     autopy.mouse.click()
  65.     time.sleep(sleep)
  66.  
  67. def type(text):
  68.     for k in text:
  69.         autopy.key.tap(k)
  70.         time.sleep(.1)
  71.  
  72. def start():
  73.     click(.8, .75)
  74.     click(1/2., 4/5.)
  75.     type("master")
  76.     click(6/7., 6/7.)
  77.     click(6/7., 6/7.)
  78.  
  79. def go_town():
  80.     if Globals.screen == 'farm':
  81.         click(.9, .9)
  82.     elif Globals.screen == 'shop':
  83.         click(.9, .1)
  84.     else:
  85.         go_farm()
  86.         click(.9, .9)
  87.     Globals.screen = 'town'
  88.  
  89. def go_shop():
  90.     if Globals.screen != 'town':
  91.         go_town()
  92.     click(1/5., 1/2.)
  93.     Globals.screen = 'shop'
  94.  
  95. def go_farm():
  96.     if Globals.screen == 'shop':
  97.         go_town()
  98.     if Globals.screen == 'town':
  99.         click(.1, .9)
  100.     else:
  101.         click(.92, .85)
  102.     Globals.screen = 'farm'
  103.  
  104. product_position = {'turnip': (.35, .5),
  105.                     'carrot': (.45, .5),
  106.                     'potato': (.53, .5),
  107.                     'tomato': (.6, .5),
  108.                     'corn': (.35, .6),
  109.                     'strawberry': (.45, .6),
  110.                     'grape': (.53, .6),
  111.                     'watermelon': (.6, .6),
  112.                     'fertilizer': (.75, .55),
  113.                     'bugkiller': (.85, .55)}
  114. product_cost = {'turnip': 100,
  115.                 'carrot': 200,
  116.                 'potato': 300,
  117.                 'tomato': 500,
  118.                 'corn': 600,
  119.                 'strawberry': 800,
  120.                 'grape': 1200,
  121.                 'watermelon': 900,
  122.                 'fertilizer': 200,
  123.                 'bugkiller': 200}
  124. product_amount = {'turnip': 1,
  125.                  'carrot': 1,
  126.                  'potato': 1,
  127.                  'tomato': 1,
  128.                  'corn': 1,
  129.                  'strawberry': 1,
  130.                  'grape': 1,
  131.                  'watermelon': 1,
  132.                  'fertilizer': 50,
  133.                  'bugkiller': 20}
  134. def buy(product):
  135.     if Globals.money >= product_cost[product]:
  136.         click(*product_position[product])
  137.         Globals.owned[product] += product_amount[product]
  138.         Globals.money -= product_cost[product]
  139.         print 'Bought', product, '(', Globals.owned[product], ') - money left', Globals.money
  140.  
  141. def buy_required():
  142.     go_shop()
  143.     if Globals.owned['fertilizer'] < 12 and Globals.money >= 400:
  144.         buy('fertilizer')
  145.     if Globals.owned['bugkiller'] < 10 and Globals.money >= 400:
  146.         buy('bugkiller')
  147.     for x in product_order:
  148.         if Globals.money >= product_cost[x]:
  149.             buy(x)
  150.             break
  151.     go_farm()
  152.  
  153. field_positions = [(.3, .6), (.7, .4), (.55, .9), (.9, .6)]
  154. def open_field(n):
  155.     if Globals.screen == n:
  156.         return
  157.     if Globals.screen != 'farm':
  158.         go_farm()
  159.     click(*field_positions[n])
  160.     Globals.screen = n
  161.  
  162. tile_positions = [(.4, .8), (.6, .7), (.7, .5), (.85, .4),
  163.                   (.2, .65), (.4, .5), (.5, .4), (.7, .3),
  164.                   (.1, .5), (.2, .4), (.4, .3), (.6, .15)]
  165.  
  166. def click_tiles():
  167.     for x, y in tile_positions:
  168.         click(x, y)
  169.  
  170. def fertilize():
  171.     click(.5, .85)
  172.     click_tiles()
  173.     click(.65, .85)
  174.     Globals.owned['fertilizer'] -= 12
  175.  
  176. def water():
  177.     click(.4, .85)
  178.     click_tiles()
  179.     click(.65, .85)
  180.  
  181. plant_position = {'turnip': (.2, .45),
  182.                   'carrot': (.3, .45),
  183.                   'potato': (.45, .45),
  184.                   'tomato': (.6, .45),
  185.                   'corn': (.2, .6),
  186.                   'strawberry': (.3, .6),
  187.                   'grape': (.45, .6),
  188.                   'watermelon': (.6, .6)}
  189. def plant():
  190.     fertilize()
  191.     click(.1, .85)
  192.     for x in product_order:
  193.         if Globals.owned[x]:
  194.             click(*plant_position[x])
  195.             Globals.planted[Globals.screen] = x
  196.             Globals.plant_amount[Globals.screen] = 12
  197.             Globals.harvests_left[Globals.screen] = plant_harvests[x]
  198.             Globals.growtime[Globals.screen] = plant_growtime[x]
  199.             Globals.owned[x] -= 1
  200.             break
  201.  
  202. def water_all():
  203.     for field in [0, 1, 2, 3]:
  204.         if Globals.planted[field]:
  205.             open_field(field)
  206.             water()
  207.     go_farm()
  208.  
  209. def plant_all(n=2):
  210.     if any(Globals.owned[x] for x in product_order):
  211.         for x in xrange(4):
  212.             if not Globals.planted[x]:
  213.                 open_field(x)
  214.                 plant()
  215.                 break
  216.         if any(not Globals.planted[x] for x in xrange(4)):
  217.             plant_all()
  218.     elif (any(not Globals.planted[x] for x in xrange(4))
  219.           and Globals.money >= 200 and n>0):
  220.         buy_required()
  221.         plant_all(n-1)
  222.  
  223. def find_worms():
  224.     rect = ((screen_x, screen_y), (screen_width, screen_height))
  225.     screen = autopy.bitmap.capture_screen()
  226.     worm_bmp = autopy.bitmap.Bitmap.open("worm.png")
  227.     worm2_bmp = autopy.bitmap.Bitmap.open("worm2.png")
  228.     point = screen.find_bitmap(worm_bmp)
  229.     if not point:
  230.         point = screen.find_bitmap(worm2_bmp)
  231.     if not point:
  232.         time.sleep(.1)
  233.         screen = autopy.bitmap.capture_screen()
  234.         point = screen.find_bitmap(worm_bmp)
  235.     if not point:
  236.         point = screen.find_bitmap(worm2_bmp)
  237.     if point:
  238.         x, y = point
  239.         x = (x - screen_x) / float(screen_width)
  240.         y = (y - screen_y) / float(screen_height)
  241.         return x, y
  242.     return None
  243.  
  244. def kill_worms():
  245.     if Globals.owned['bugkiller'] < 5 and Globals.money >= 400:
  246.         field = Globals.screen
  247.         go_shop()
  248.         buy('bugkiller')
  249.         open_field(field)
  250.     if Globals.owned['bugkiller']:
  251.         for _ in xrange(8):
  252.             move(.1, .1)
  253.             time.sleep(.3)
  254.             point = find_worms()
  255.             if point and Globals.owned['bugkiller']:
  256.                 click(.65, .85, .5)
  257.                 click(*point)
  258.                 Globals.owned['bugkiller'] -= 1
  259.                 click(.65, .85, .5)
  260.             else:
  261.                 break
  262.  
  263. def kill_all_worms():
  264.     for field in [0, 1, 2, 3]:
  265.         if Globals.planted[field]:
  266.             open_field(field)
  267.             kill_worms()
  268.  
  269. plant_value = {'turnip': 50,
  270.                'carrot': 60,
  271.                'potato': 75,
  272.                'tomato': 150,
  273.                'corn': None,
  274.                'strawberry': None,
  275.                'grape': None,
  276.                'watermelon': 500}
  277.  
  278. plant_harvests = {'turnip': 1,
  279.                   'carrot': 1,
  280.                   'potato': 1,
  281.                   'tomato': 10,
  282.                   'corn': 10,
  283.                   'strawberry': 1,
  284.                   'grape': 10,
  285.                   'watermelon': 1}
  286.  
  287. plant_growtime = {'turnip': 3,
  288.                   'carrot': 3,
  289.                   'potato': 7,
  290.                   'tomato': 11,
  291.                   'corn': 15,
  292.                   'strawberry': 100,
  293.                   'grape': 23,
  294.                   'watermelon': 19}
  295.  
  296. plant_regrowtime = {'tomato': 3,
  297.                     'corn': 3,
  298.                     'strawberry': 10,
  299.                     'grape': 4}
  300.  
  301. def collect_plants():
  302.     field = Globals.screen
  303.     plant = Globals.planted[field]
  304.     time = Globals.growtime[field]
  305.     if time == 0:
  306.         click(.3, .85)
  307.         for x, y in tile_positions:
  308.             click(x, y, .4)
  309.             click(.1, .75, .4)
  310.         click(.65, .85)
  311.         Globals.money += plant_value[plant] * Globals.plant_amount[field]
  312.         Globals.harvests_left[field] -= 1
  313.         if Globals.harvests_left[field] == 0:
  314.             Globals.planted[field] = None
  315.             Globals.plant_amount[field] = 0
  316.         else:
  317.             Globals.growtime[field] = plant_regrowtime[plant]
  318.             cut_plants()
  319.         print 'Collected', plant,' - money:', Globals.money
  320.  
  321. def collect_all_plants():
  322.     for field in [0, 1, 2, 3]:
  323.         if Globals.planted[field]:
  324.             open_field(field)
  325.             collect_plants()
  326.  
  327. def find_dead_plant():
  328.     rect = ((screen_x, screen_y), (screen_width, screen_height))
  329.     screen = autopy.bitmap.capture_screen()
  330.     plant_dead_bmp = autopy.bitmap.Bitmap.open("dead.png")
  331.     point = screen.find_bitmap(plant_dead_bmp, .15)
  332.     if point:
  333.         print 'Dead plant in', point
  334.         x, y = point
  335.         x = (x - screen_x) / float(screen_width)
  336.         y = (y - screen_y) / float(screen_height)
  337.         return x, y
  338.     return None
  339.  
  340. def cut_plants():
  341.     pos = find_dead_plant()
  342.     if pos:
  343.         field = Globals.screen
  344.         click(.75, .85)
  345.         click(*pos)
  346.         removed = 1
  347.         for i in xrange(11):
  348.             pos = find_dead_plant()
  349.             if pos:
  350.                 click(*pos)
  351.                 removed += 1
  352.             else:
  353.                 break
  354.         click(.65, .85)
  355.         Globals.plant_amount[field] -= removed
  356.         if Globals.plant_amount[field] == 0:
  357.             Globals.planted[field] = None
  358.             Globals.growtime[field] = 0
  359.         print 'Cut', removed, 'plants.'
  360.         move(.1, .1)
  361.         time.sleep(.1)
  362.  
  363. def cut_all_plants():
  364.     for field in [0, 1, 2, 3]:
  365.         if Globals.planted[field]:
  366.             open_field(field)
  367.             cut_plants()
  368.  
  369. def new_day():
  370.     click(.3, .3)
  371.     click(.1, .1)
  372.     time.sleep(3)
  373.  
  374. def day():
  375.     Globals.day += 1
  376.     for i, x in enumerate(Globals.growtime):
  377.         if x:
  378.             Globals.growtime[i] -= 1
  379.     print '-- Day', Globals.day, '--'
  380.     print 'Grow timers:', Globals.growtime
  381.     cut_all_plants()
  382.     collect_all_plants()
  383.     kill_all_worms()
  384.     plant_all()
  385.     water_all()
  386.     if all(x == None for x in Globals.planted):
  387.         print "Dead."
  388.         return
  389.     new_day()
  390.  
  391. def days(n):
  392.     for x in xrange(n):
  393.         day()
  394.  
  395. def play(d):
  396.     start()
  397.     days(d)
  398.  
  399. def find_screen():
  400.     global screen_x, screen_y, screen_width, screen_height
  401.     screen = autopy.bitmap.capture_screen()
  402.     pos = find_top_left(screen)
  403.     if not pos:
  404.         print "No game found."
  405.         return False
  406.     screen_x, screen_y = pos
  407.     #screen_width, screen_height = find_bottom_right(screen)
  408.     #screen_width -= screen_x
  409.     #screen_height -= screen_y
  410.     print "Game found in ", pos, "with size", (screen_width, screen_height)
  411.     return True
  412.  
  413. def main(days=4):
  414.     if find_screen():
  415.         click(.1, .1)
  416.         play(days)
  417.  
  418. if __name__ == '__main__':
  419.     screen_x, screen_y = 743, 236
  420.     #play()
  421.     main(int(raw_input("How many days (under 20 for your sanity)? ")))
  422.     #open_field(0)
Advertisement
Add Comment
Please, Sign In to add comment