Last active
April 7, 2017 06:53
-
-
Save Raniz85/491dc37c222120476c9515872a95fc83 to your computer and use it in GitHub Desktop.
Simple Python script for grinding Trust Master Rewards in FFBE. Requires Python 3 and ADB. Don't forget to enable debug mode on your device. Future improvements is to determine state from screenshots instead of waiting a specific time and support completion of the daily quest. USE AT YOUR OWN RISK!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright 2017 Raniz | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import subprocess, time, argparse, sys, itertools, atexit | |
locations = { | |
(16 / 9): { | |
'center': (0.5, 0.5), | |
'entrance': (0.5, 0.644), | |
'missions_next': (0.5, 0.871), | |
'depart': (0.5, 0.848), | |
'auto': (0.156, 0.957), | |
'char1': (0.25, 0.666), | |
'char2': (0.75, 0.666), | |
'results_next': (0.5, 0.879), | |
'friend_dont_request': (0.25, 0.732), | |
'refresh_lapis': (0.75, 0.564), | |
}, | |
16 / 10: { | |
'center': (0.5, 0.5), | |
'entrance': (0.5, 0.694), | |
'missions_next': (0.5, 0.922), | |
'depart': (0.5, 0.901), | |
'auto': (0.156, 0.960), | |
'char1': (0.25, 0.630), | |
'char2': (0.75, 0.630), | |
'results_next': (0.5, 0.901), | |
'friend_dont_request': (0.25, 0.737), | |
'refresh_lapis': (0.75, 0.564), | |
} | |
} | |
class FfBeController: | |
def __init__(self, width, height, lag=0): | |
self.width = width | |
self.height = height | |
self.lag = lag | |
self.positions = locations[height / width] | |
self.extra_delay = 0 | |
self.process = None | |
def connect_adb(self): | |
status = self.process.poll() if self.process is not None else -1 | |
if status is not None: | |
if self.process: | |
print('ADB exited with status {}'.format(status)) | |
self.process.stdin.close() | |
self.process.stdout.close() | |
self.process = subprocess.Popen(['adb', 'shell'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
self.process.stdin.write('true\n'.encode('utf-8')) | |
atexit.register(self.process.terminate) | |
print('Started ADB as {}'.format(self.process.pid)) | |
def adb_command(self, *args): | |
command = ' '.join(args) | |
#print("Executing {}".format(command)) | |
self.connect_adb() | |
self.process.stdin.write(command.encode('utf-8')) | |
self.process.stdin.write(b'&& echo done\n') | |
self.process.stdin.flush() | |
# Wait for the command prompt to appear | |
while self.process.poll() is None: | |
line = self.process.stdout.readline().strip() | |
if line == b'done': | |
return | |
def wait(self, message, sleep_time, tap=None): | |
if not message.endswith('...'): | |
message = message + '...' | |
end = time.time() + sleep_time + self.lag | |
pad_length = 0 | |
if tap: | |
locations = itertools.cycle(tap) | |
while time.time() < end: | |
remaining = end - time.time() | |
text = message.format(remaining) | |
pad_length = max(len(text), pad_length) | |
sys.stdout.write('\r') | |
sys.stdout.write(text.ljust(pad_length)) | |
sys.stdout.flush() | |
if tap: | |
if isinstance(tap, str): | |
self.tap(tap) | |
else: | |
self.tap(*tap) | |
else: | |
time.sleep(min(0.1, remaining)) | |
print('\r' + message.format(sleep_time) + ' Done') | |
def get_collected_energy(self, time_started): | |
# One energy every 5 minutes | |
return int((time.time() - time_started) / 300) | |
def wait_for_energy(self, time_started): | |
sleep_time = 300 - (time.time() - time_started) % 300 | |
self.wait('Waiting {:.0f}s for new energy', sleep_time) | |
def tap(self, *locations): | |
command = [] | |
for location in locations: | |
x, y = self.positions[location] | |
command.append('input tap {:.0f} {:.0f}'.format(x * self.width, y * self.height)) | |
self.adb_command(' && '.join(command)) | |
def input(self, *args): | |
command = ['input'] + list(args) | |
self.adb_command(*command) | |
def run_es(self, select_party=False, lapis_refresh=False): | |
print('Selecting Earth Shrine - Entrance') | |
self.tap('entrance') | |
if lapis_refresh: | |
self.wait('Waiting {:.1f}s for lapis refresh screen', 1) | |
print('Potentially refreshing with lapis') | |
self.tap('refresh_lapis') | |
self.wait('Waiting {:.1f}s for energy to be refreshed', 2) | |
self.wait('Waiting {:.1f}s for missions screen', 1.5) | |
print('Advancing past missions') | |
self.tap('missions_next') | |
self.wait('Waiting {:0.1f}s for companion screen', 2.5) | |
print('Selecting companion') | |
self.tap('entrance') | |
self.wait('Waiting {:0.1f}s for party selection screen', 2.5) | |
if select_party: | |
input('Please select the party you want to grind with, then press enter') | |
print('Departing') | |
self.tap('depart') | |
self.wait('Waiting {:0.1f}s for quest to start loading', 2) | |
print('Attacking') | |
self.wait('Killing monsters ({:0.1f}s)', 35, tap=('char1', 'char2')) | |
self.wait('Waiting {:0.1f}s for quest to finish', 5) | |
print('Moving past results') | |
self.tap('center') | |
time.sleep(1) | |
self.tap('results_next') | |
self.wait('Waiting {:0.1f}s for character screen', 2) | |
print('Advancing to loot screen') | |
self.tap('center') | |
time.sleep(1) | |
self.tap('results_next') | |
self.wait('Waiting {:0.1f}s for loot screen', 1) | |
print('Ending quest') | |
self.tap('center') | |
time.sleep(2) | |
self.tap('results_next') | |
self.wait('Waiting {:0.1f}s for friend request screen to show up', 2) | |
print('Possibly not requesting friend') | |
self.tap('friend_dont_request') | |
def loop_es(self, initial_energy, rank, lapis_refresh=False, max_rounds=-1): | |
time_started = time.time() | |
energy_spent = 0 | |
if rank > 100: | |
max_energy = 110 + int(rank % 100 / 2) | |
else: | |
max_energy = rank + 10 | |
times_refreshed = 0 | |
while True: | |
energy_remaining = initial_energy + self.get_collected_energy(time_started) - energy_spent + times_refreshed * max_energy | |
print('{} energy remains'.format(energy_remaining)) | |
if not lapis_refresh: | |
# Only wait for energy if we're not refreshing | |
if energy_remaining == 0: | |
self.wait_for_energy(time_started) | |
elif energy_remaining == 0: | |
# We refresh now | |
times_refreshed += 1 | |
energy_remaining += max_energy | |
# Only refresh if we're below 5 energy or above max energy minus 5 (offsets to account for us being out of sync) | |
self.run_es(energy_spent == 0, lapis_refresh and (energy_remaining < 5 or energy_remaining > max_energy - 5)) | |
energy_spent += 1 | |
print('Earth Shrine completed {} times'.format(energy_spent)) | |
if max_rounds > 0 and energy_spent == max_rounds: | |
print('Maximum number of clears reached, exiting') | |
return | |
print(''.ljust(40, '=')) | |
self.wait('Waiting {:.1f}s for Earth Shrine to show up', 4) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--width', default=1440, type=int, help='Width of your screen in pixels') | |
parser.add_argument('--height', default=2560, type=int, help='Height of your screen in pixels') | |
parser.add_argument('--lag', default=0, type=float, help='Extra time, in seconds, to add to all wait times') | |
parser.add_argument('--lapis-refresh', action='store_true', help='Use lapis to refresh energy when empty') | |
parser.add_argument('--stop-after', default=-1, type=int, help='The maximum number of rounds to run, leave off to run indefinitely') | |
parser.add_argument('energy', type=int, help='The amount of energy you have left') | |
parser.add_argument('rank', type=int, help='Your current rank') | |
args = parser.parse_args() | |
input("Navigate to the Earth Shrine quest selection screen and ensure that you've\n" + | |
"already completed the 'Adventurer' daily quest, then press enter") | |
controller = FfBeController(args.width, args.height, args.lag) | |
controller.loop_es(args.energy, args.rank, lapis_refresh=args.lapis_refresh, max_rounds=args.stop_after) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment