Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Bot for The Farmer
- # http://pelikone.fi/pelit/muut/The-Farmer/85
- # Make sure the top-left of the game's main menu is visible.
- # Ensure there's no brownish yellow visible (32, 186, 165 to be exact)
- #
- # Download associated bitmaps from
- # https://dl.dropboxusercontent.com/u/22984741/python/autopy/farmer/farmer.zip
- import autopy
- import time
- import itertools
- import sys
- screen_x, screen_y = 0, 0
- screen_width, screen_height = 600, 420
- x_offset, y_offset = 0, 0 #-230, -130
- product_order = ['grape', 'watermelon', 'strawberry',
- 'corn', 'tomato', 'potato', 'carrot']#, 'turnip']
- worm_color = autopy.color.rgb_to_hex(32, 186, 165)
- class Globals:
- day = 0
- screen = 'farm'
- money = 400
- owned = {'turnip': 0,
- 'carrot': 0,
- 'potato': 0,
- 'tomato': 0,
- 'corn': 0,
- 'strawberry': 0,
- 'grape': 0,
- 'watermelon': 0,
- 'fertilizer': 0,
- 'bugkiller': 0}
- planted = [None, None, None, None]
- plant_amount = [0, 0, 0, 0]
- harvests_left = [0, 0, 0, 0]
- growtime = [0, 0, 0, 0]
- def find_top_left(screen):
- top_left_bmp = autopy.bitmap.Bitmap.open("top_left.png")
- return screen.find_bitmap(top_left_bmp)
- def find_bottom_right(screen):
- bottom_right_bmp = autopy.bitmap.Bitmap.open("bottom_right.png")
- pos = screen.find_bitmap(bottom_right_bmp)
- if pos:
- x, y = pos
- return x + 91, y + 24
- return None
- def move(x, y):
- autopy.mouse.move(screen_x + x_offset + int(x*screen_width),
- screen_y + y_offset + int(y*screen_height))
- def click(x=None, y=None, sleep=.1):
- if x is not None:
- if not (0 <= x < 1 and 0 <= y < 1):
- print 'Click outside of the game window at', (x, y)
- sys.exit()
- move(x, y)
- autopy.mouse.click()
- time.sleep(sleep)
- def type(text):
- for k in text:
- autopy.key.tap(k)
- time.sleep(.1)
- def start():
- click(.8, .75)
- click(1/2., 4/5.)
- type("master")
- click(6/7., 6/7.)
- click(6/7., 6/7.)
- def go_town():
- if Globals.screen == 'farm':
- click(.9, .9)
- elif Globals.screen == 'shop':
- click(.9, .1)
- else:
- go_farm()
- click(.9, .9)
- Globals.screen = 'town'
- def go_shop():
- if Globals.screen != 'town':
- go_town()
- click(1/5., 1/2.)
- Globals.screen = 'shop'
- def go_farm():
- if Globals.screen == 'shop':
- go_town()
- if Globals.screen == 'town':
- click(.1, .9)
- else:
- click(.92, .85)
- Globals.screen = 'farm'
- product_position = {'turnip': (.35, .5),
- 'carrot': (.45, .5),
- 'potato': (.53, .5),
- 'tomato': (.6, .5),
- 'corn': (.35, .6),
- 'strawberry': (.45, .6),
- 'grape': (.53, .6),
- 'watermelon': (.6, .6),
- 'fertilizer': (.75, .55),
- 'bugkiller': (.85, .55)}
- product_cost = {'turnip': 100,
- 'carrot': 200,
- 'potato': 300,
- 'tomato': 500,
- 'corn': 600,
- 'strawberry': 800,
- 'grape': 1200,
- 'watermelon': 900,
- 'fertilizer': 200,
- 'bugkiller': 200}
- product_amount = {'turnip': 1,
- 'carrot': 1,
- 'potato': 1,
- 'tomato': 1,
- 'corn': 1,
- 'strawberry': 1,
- 'grape': 1,
- 'watermelon': 1,
- 'fertilizer': 50,
- 'bugkiller': 20}
- def buy(product):
- if Globals.money >= product_cost[product]:
- click(*product_position[product])
- Globals.owned[product] += product_amount[product]
- Globals.money -= product_cost[product]
- print 'Bought', product, '(', Globals.owned[product], ') - money left', Globals.money
- def buy_required():
- go_shop()
- if Globals.owned['fertilizer'] < 12 and Globals.money >= 400:
- buy('fertilizer')
- if Globals.owned['bugkiller'] < 10 and Globals.money >= 400:
- buy('bugkiller')
- for x in product_order:
- if Globals.money >= product_cost[x]:
- buy(x)
- break
- go_farm()
- field_positions = [(.3, .6), (.7, .4), (.55, .9), (.9, .6)]
- def open_field(n):
- if Globals.screen == n:
- return
- if Globals.screen != 'farm':
- go_farm()
- click(*field_positions[n])
- Globals.screen = n
- tile_positions = [(.4, .8), (.6, .7), (.7, .5), (.85, .4),
- (.2, .65), (.4, .5), (.5, .4), (.7, .3),
- (.1, .5), (.2, .4), (.4, .3), (.6, .15)]
- def click_tiles():
- for x, y in tile_positions:
- click(x, y)
- def fertilize():
- click(.5, .85)
- click_tiles()
- click(.65, .85)
- Globals.owned['fertilizer'] -= 12
- def water():
- click(.4, .85)
- click_tiles()
- click(.65, .85)
- plant_position = {'turnip': (.2, .45),
- 'carrot': (.3, .45),
- 'potato': (.45, .45),
- 'tomato': (.6, .45),
- 'corn': (.2, .6),
- 'strawberry': (.3, .6),
- 'grape': (.45, .6),
- 'watermelon': (.6, .6)}
- def plant():
- fertilize()
- click(.1, .85)
- for x in product_order:
- if Globals.owned[x]:
- click(*plant_position[x])
- Globals.planted[Globals.screen] = x
- Globals.plant_amount[Globals.screen] = 12
- Globals.harvests_left[Globals.screen] = plant_harvests[x]
- Globals.growtime[Globals.screen] = plant_growtime[x]
- Globals.owned[x] -= 1
- break
- def water_all():
- for field in [0, 1, 2, 3]:
- if Globals.planted[field]:
- open_field(field)
- water()
- go_farm()
- def plant_all(n=2):
- if any(Globals.owned[x] for x in product_order):
- for x in xrange(4):
- if not Globals.planted[x]:
- open_field(x)
- plant()
- break
- if any(not Globals.planted[x] for x in xrange(4)):
- plant_all()
- elif (any(not Globals.planted[x] for x in xrange(4))
- and Globals.money >= 200 and n>0):
- buy_required()
- plant_all(n-1)
- def find_worms():
- rect = ((screen_x, screen_y), (screen_width, screen_height))
- screen = autopy.bitmap.capture_screen()
- worm_bmp = autopy.bitmap.Bitmap.open("worm.png")
- worm2_bmp = autopy.bitmap.Bitmap.open("worm2.png")
- point = screen.find_bitmap(worm_bmp)
- if not point:
- point = screen.find_bitmap(worm2_bmp)
- if not point:
- time.sleep(.1)
- screen = autopy.bitmap.capture_screen()
- point = screen.find_bitmap(worm_bmp)
- if not point:
- point = screen.find_bitmap(worm2_bmp)
- if point:
- x, y = point
- x = (x - screen_x) / float(screen_width)
- y = (y - screen_y) / float(screen_height)
- return x, y
- return None
- def kill_worms():
- if Globals.owned['bugkiller'] < 5 and Globals.money >= 400:
- field = Globals.screen
- go_shop()
- buy('bugkiller')
- open_field(field)
- if Globals.owned['bugkiller']:
- for _ in xrange(8):
- move(.1, .1)
- time.sleep(.3)
- point = find_worms()
- if point and Globals.owned['bugkiller']:
- click(.65, .85, .5)
- click(*point)
- Globals.owned['bugkiller'] -= 1
- click(.65, .85, .5)
- else:
- break
- def kill_all_worms():
- for field in [0, 1, 2, 3]:
- if Globals.planted[field]:
- open_field(field)
- kill_worms()
- plant_value = {'turnip': 50,
- 'carrot': 60,
- 'potato': 75,
- 'tomato': 150,
- 'corn': None,
- 'strawberry': None,
- 'grape': None,
- 'watermelon': 500}
- plant_harvests = {'turnip': 1,
- 'carrot': 1,
- 'potato': 1,
- 'tomato': 10,
- 'corn': 10,
- 'strawberry': 1,
- 'grape': 10,
- 'watermelon': 1}
- plant_growtime = {'turnip': 3,
- 'carrot': 3,
- 'potato': 7,
- 'tomato': 11,
- 'corn': 15,
- 'strawberry': 100,
- 'grape': 23,
- 'watermelon': 19}
- plant_regrowtime = {'tomato': 3,
- 'corn': 3,
- 'strawberry': 10,
- 'grape': 4}
- def collect_plants():
- field = Globals.screen
- plant = Globals.planted[field]
- time = Globals.growtime[field]
- if time == 0:
- click(.3, .85)
- for x, y in tile_positions:
- click(x, y, .4)
- click(.1, .75, .4)
- click(.65, .85)
- Globals.money += plant_value[plant] * Globals.plant_amount[field]
- Globals.harvests_left[field] -= 1
- if Globals.harvests_left[field] == 0:
- Globals.planted[field] = None
- Globals.plant_amount[field] = 0
- else:
- Globals.growtime[field] = plant_regrowtime[plant]
- cut_plants()
- print 'Collected', plant,' - money:', Globals.money
- def collect_all_plants():
- for field in [0, 1, 2, 3]:
- if Globals.planted[field]:
- open_field(field)
- collect_plants()
- def find_dead_plant():
- rect = ((screen_x, screen_y), (screen_width, screen_height))
- screen = autopy.bitmap.capture_screen()
- plant_dead_bmp = autopy.bitmap.Bitmap.open("dead.png")
- point = screen.find_bitmap(plant_dead_bmp, .15)
- if point:
- print 'Dead plant in', point
- x, y = point
- x = (x - screen_x) / float(screen_width)
- y = (y - screen_y) / float(screen_height)
- return x, y
- return None
- def cut_plants():
- pos = find_dead_plant()
- if pos:
- field = Globals.screen
- click(.75, .85)
- click(*pos)
- removed = 1
- for i in xrange(11):
- pos = find_dead_plant()
- if pos:
- click(*pos)
- removed += 1
- else:
- break
- click(.65, .85)
- Globals.plant_amount[field] -= removed
- if Globals.plant_amount[field] == 0:
- Globals.planted[field] = None
- Globals.growtime[field] = 0
- print 'Cut', removed, 'plants.'
- move(.1, .1)
- time.sleep(.1)
- def cut_all_plants():
- for field in [0, 1, 2, 3]:
- if Globals.planted[field]:
- open_field(field)
- cut_plants()
- def new_day():
- click(.3, .3)
- click(.1, .1)
- time.sleep(3)
- def day():
- Globals.day += 1
- for i, x in enumerate(Globals.growtime):
- if x:
- Globals.growtime[i] -= 1
- print '-- Day', Globals.day, '--'
- print 'Grow timers:', Globals.growtime
- cut_all_plants()
- collect_all_plants()
- kill_all_worms()
- plant_all()
- water_all()
- if all(x == None for x in Globals.planted):
- print "Dead."
- return
- new_day()
- def days(n):
- for x in xrange(n):
- day()
- def play(d):
- start()
- days(d)
- def find_screen():
- global screen_x, screen_y, screen_width, screen_height
- screen = autopy.bitmap.capture_screen()
- pos = find_top_left(screen)
- if not pos:
- print "No game found."
- return False
- screen_x, screen_y = pos
- #screen_width, screen_height = find_bottom_right(screen)
- #screen_width -= screen_x
- #screen_height -= screen_y
- print "Game found in ", pos, "with size", (screen_width, screen_height)
- return True
- def main(days=4):
- if find_screen():
- click(.1, .1)
- play(days)
- if __name__ == '__main__':
- screen_x, screen_y = 743, 236
- #play()
- main(int(raw_input("How many days (under 20 for your sanity)? ")))
- #open_field(0)
Advertisement
Add Comment
Please, Sign In to add comment