Skip to content

Commit 42d46c4

Browse files
authored
Merge pull request #203 from shenef/main
Add pre-commit config Closes #141
2 parents a23c8fb + 5461165 commit 42d46c4

File tree

6 files changed

+121
-49
lines changed

6 files changed

+121
-49
lines changed

Diff for: .pre-commit-config.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 22.10.0
4+
hooks:
5+
- id: black
6+
exclude: migrations
7+
- repo: https://github.com/pycqa/isort
8+
rev: 5.10.1
9+
hooks:
10+
- id: isort
11+
args: ["--profile", "black"]
12+
name: isort (python)
13+
14+
# uncomment once the repo complies with flake8
15+
#- repo: https://github.com/pycqa/flake8
16+
# rev: 5.0.4
17+
# hooks:
18+
# - id: flake8
19+
# args: ["--ignore=E501, W503"]

Diff for: config.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# TODO: Make this be set by an argument instead
1010
CONFIG_FILE_PATH = "config.yaml"
1111

12+
1213
# TODO: It's not ideal to open this file many times over, but also not a huge problem
1314
def open_config():
1415
# Open the config file and parse the yaml contents
@@ -20,5 +21,7 @@ def open_config():
2021
print(f"Error: Failed to parse config file {CONFIG_FILE_PATH}: {E}")
2122
return {}
2223
except Exception as E:
23-
print(f"Didn't find config file {CONFIG_FILE_PATH}, using default values for run.")
24+
print(
25+
f"Didn't find config file {CONFIG_FILE_PATH}, using default values for run."
26+
)
2427
return {}

Diff for: gamestate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
class GameState:
32
def __init__(self):
43
self.state = "none"
54
self.step = 1
65
self.rng_seed_num = 160
76
self.start_time = 0
87

8+
99
# Global
1010
game = GameState()

Diff for: main.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# This needs to be before the other imports in case they decide to log things when imported
77
import log_init
8-
import logging
8+
99
# This sets up console and file logging (should only be called once)
1010
log_init.initialize_logging()
1111

@@ -49,6 +49,7 @@
4949

5050
FFXC = xbox.controller_handle()
5151

52+
5253
def configuration_setup():
5354
game_vars = vars.vars_handle()
5455
# Open the config file and parse game configuration
@@ -118,6 +119,7 @@ def rng_seed_setup():
118119
logs.write_stats("RNG seed:")
119120
logs.write_stats(rng_seed)
120121

122+
121123
def load_game_state():
122124
# loading from a save file
123125
load_game.load_into_game(gamestate=game.state, step_counter=game.step)
@@ -137,7 +139,7 @@ def perform_TAS():
137139
# Blitzball testing logic
138140
if game.state == "Luca" and game.step == 3:
139141
area.dream_zan.new_game(game.state)
140-
load_game.load_save_num(37) # TODO: Magic number
142+
load_game.load_save_num(37) # TODO: Magic number
141143

142144
if game.rng_seed_num >= 256:
143145
game.state = "End"
@@ -780,6 +782,7 @@ def perform_TAS():
780782

781783
logger.info("Time! The game is now over.")
782784

785+
783786
def write_final_logs():
784787
if memory.main.get_story_progress() > 3210:
785788
end_time = logs.time_stamp()
@@ -810,6 +813,7 @@ def write_final_logs():
810813

811814
logger.info("Automation complete. Shutting down. Have a great day!")
812815

816+
813817
# Main entry point of TAS
814818
if __name__ == "__main__":
815819
# Load up vars.py

Diff for: save_sphere.py

+60-36
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import memory
22
import xbox
3+
34
FFXC = xbox.controller_handle()
4-
import math
55
import json
6+
import math
7+
68
import pathing
79
import vars
10+
811
game_vars = vars.vars_handle()
912

13+
1014
def distance(save_index) -> int:
1115
tidus_coords = memory.main.get_coords()
1216
target_coords = memory.main.get_actor_coords(save_index)
13-
14-
return int(math.sqrt((target_coords[1] - tidus_coords[1])**2 + (target_coords[0] - tidus_coords[0])**2))
17+
18+
return int(
19+
math.sqrt(
20+
(target_coords[1] - tidus_coords[1]) ** 2
21+
+ (target_coords[0] - tidus_coords[0]) ** 2
22+
)
23+
)
24+
1525

1626
def nearest_save_actor() -> int:
1727
save_actors = []
@@ -20,7 +30,7 @@ def nearest_save_actor() -> int:
2030
actorMem = memory.main.get_actor_id(x)
2131
if actorMem != 52685:
2232
print(actorMem, " | ", x)
23-
if actorMem in [20481,20482,20651]:
33+
if actorMem in [20481, 20482, 20651]:
2434
save_actors.append(x)
2535
print(save_actors)
2636
if len(save_actors) == 0:
@@ -39,6 +49,7 @@ def nearest_save_actor() -> int:
3949
best_dist = distance(save_actors[j])
4050
return best_actor
4151

52+
4253
def approach_save_sphere():
4354
memory.main.clear_save_menu_cursor()
4455
memory.main.clear_save_menu_cursor_2()
@@ -48,8 +59,9 @@ def approach_save_sphere():
4859
print("Approaching actor: ", target_actor)
4960
FFXC.set_neutral()
5061
while not (
51-
memory.main.diag_progress_flag() == target_details[2] and
52-
memory.main.diag_skip_possible()):
62+
memory.main.diag_progress_flag() == target_details[2]
63+
and memory.main.diag_skip_possible()
64+
):
5365
if memory.main.user_control():
5466
pathing.set_movement([target_coords[0], target_coords[1]])
5567
xbox.tap_b()
@@ -68,7 +80,7 @@ def approach_save_sphere():
6880
x_val=target_coords[0],
6981
y_val=target_coords[1],
7082
diag_prog=memory.main.diag_progress_flag(),
71-
actor=target_actor
83+
actor=target_actor,
7284
)
7385
while not memory.main.user_control():
7486
xbox.tap_a()
@@ -91,31 +103,35 @@ def approach_save_sphere():
91103
print("Mark 3", memory.main.diag_progress_flag())
92104
xbox.tap_b()
93105

106+
94107
def disengage_save_sphere():
95108
while memory.main.save_menu_cursor() == 0 and memory.main.save_menu_cursor_2() == 0:
96109
print("Cursor")
97110
xbox.tap_a()
98111
while not memory.main.user_control():
99112
xbox.tap_b()
100113

114+
101115
def touch_and_go():
102116
approach_save_sphere()
103117
print("Now touching save sphere.")
104118
disengage_save_sphere()
105119

106-
def touch_and_save(save_num:int=999):
120+
121+
def touch_and_save(save_num: int = 999):
107122
if save_num >= 200:
108123
print("Cannot save number ", save_num, ", out of bounds error")
109124
save_num = 999
110125
save_pos = 999
111126
if save_num != 999:
112127
import loadGame
128+
113129
saveFiles = loadGame.get_saved_files()
114130
testString = "ffx_" + str(save_num).zfill(3)
115131
for x in range(len(saveFiles)):
116132
if saveFiles[x] == testString:
117133
save_pos = x
118-
134+
119135
approach_save_sphere()
120136
memory.main.wait_frames(2)
121137
while not memory.main.save_menu_open():
@@ -138,37 +154,46 @@ def touch_and_save(save_num:int=999):
138154
xbox.tap_b()
139155
while not memory.main.user_control():
140156
xbox.tap_a()
141-
157+
142158
if save_num != 999 and save_pos == 999:
143159
saveFiles = loadGame.get_saved_files()
144-
print("File was expected as save number ", save_num, ", but could not find this file.")
160+
print(
161+
"File was expected as save number ",
162+
save_num,
163+
", but could not find this file.",
164+
)
145165
print("Actual save file: ", saveFiles[0])
146166
file_orig = game_vars.game_save_path() + saveFiles[0]
147167
print(file_orig)
148168
file_dest = game_vars.game_save_path() + "ffx_" + str(save_num).zfill(3)
149169
print(file_dest)
150170
import shutil
171+
151172
shutil.move(src=file_orig, dst=file_dest)
152173

153-
def get_save_sphere_settings(actor_index:int):
174+
175+
def get_save_sphere_settings(actor_index: int):
154176
filepath = "json_ai_files\\save_sphere_details.json"
155177
with open(filepath, "r") as fp:
156178
results = json.load(fp)
157-
179+
158180
map_num = str(memory.main.get_map())
159181
diag_num = str(memory.main.get_story_progress())
160182
actor_num = str(actor_index)
161-
ret_array = [999,999,999]
183+
ret_array = [999, 999, 999]
162184
if map_num in results:
163185
if diag_num in results[map_num]:
164186
if actor_num in results[map_num][diag_num]:
165-
ret_array = [results[map_num][diag_num][actor_num]["x"],
187+
ret_array = [
188+
results[map_num][diag_num][actor_num]["x"],
166189
results[map_num][diag_num][actor_num]["y"],
167-
results[map_num][diag_num][actor_num]["diag"]]
168-
190+
results[map_num][diag_num][actor_num]["diag"],
191+
]
192+
169193
return ret_array
170194

171-
def record_save_sphere(x_val:int, y_val:int, diag_prog:int, actor:int):
195+
196+
def record_save_sphere(x_val: int, y_val: int, diag_prog: int, actor: int):
172197
filepath = "json_ai_files\\save_sphere_details.json"
173198
with open(filepath, "r") as fp:
174199
records = json.load(fp)
@@ -177,32 +202,31 @@ def record_save_sphere(x_val:int, y_val:int, diag_prog:int, actor:int):
177202
actor_num = str(actor)
178203
print("========================")
179204
newVal = {
180-
map_num: {
181-
diag_num: {
182-
actor_num: {
183-
"x": x_val,
184-
"y": y_val,
185-
"diag": diag_prog
186-
}
187-
}
188-
}
205+
map_num: {diag_num: {actor_num: {"x": x_val, "y": y_val, "diag": diag_prog}}}
189206
}
190207
if map_num in records.keys():
191-
if diag_num in (
192-
records[map_num].keys()
193-
):
208+
if diag_num in (records[map_num].keys()):
194209
if actor_num in records[map_num][diag_num]:
195-
if records[map_num][diag_num][actor_num]["diag"] != newVal[map_num][diag_num][actor_num]["diag"]:
196-
197-
records[map_num][diag_num][actor_num]["x"] = newVal[map_num][diag_num][actor_num]["x"]
198-
records[map_num][diag_num][actor_num]["y"] = newVal[map_num][diag_num][actor_num]["y"]
199-
records[map_num][diag_num][actor_num]["diag"] = newVal[map_num][diag_num][actor_num]["diag"]
210+
if (
211+
records[map_num][diag_num][actor_num]["diag"]
212+
!= newVal[map_num][diag_num][actor_num]["diag"]
213+
):
214+
215+
records[map_num][diag_num][actor_num]["x"] = newVal[map_num][
216+
diag_num
217+
][actor_num]["x"]
218+
records[map_num][diag_num][actor_num]["y"] = newVal[map_num][
219+
diag_num
220+
][actor_num]["y"]
221+
records[map_num][diag_num][actor_num]["diag"] = newVal[map_num][
222+
diag_num
223+
][actor_num]["diag"]
200224
else:
201225
records[map_num][diag_num].update(newVal[map_num][diag_num])
202226
else:
203227
records[map_num].update(newVal[map_num])
204228
else:
205229
records.update(newVal)
206-
230+
207231
with open(filepath, "w") as fp:
208232
json.dump(records, fp, indent=4)

Diff for: tools/move_saves.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
from absl import app
2-
from absl import flags
31
import os
42
import shutil
53
import sys
64
import time
75

6+
from absl import app, flags
7+
88
FLAGS = flags.FLAGS
99

10-
flags.DEFINE_bool("setup_saves", False, "Will set up the TAS saves and store your saves in a new folder called 'Save Backups'")
11-
flags.DEFINE_bool("restore_saves", False, "Will restore your saves and remove the TAS saves.")
10+
flags.DEFINE_bool(
11+
"setup_saves",
12+
False,
13+
"Will set up the TAS saves and store your saves in a new folder called 'Save Backups'",
14+
)
15+
flags.DEFINE_bool(
16+
"restore_saves", False, "Will restore your saves and remove the TAS saves."
17+
)
18+
19+
flags.DEFINE_string(
20+
"user_path",
21+
"",
22+
"Set this if your Documents folder, and thus your 'SQUARE ENIX\\FINAL FANTASY X&X-2 HD Remaster folder' is in a different location.",
23+
)
1224

13-
flags.DEFINE_string("user_path", "", "Set this if your Documents folder, and thus your 'SQUARE ENIX\\FINAL FANTASY X&X-2 HD Remaster folder' is in a different location.")
1425

1526
def _move_file(name, src_dir, dest_dir, mod_time):
1627
src_full_path = os.path.join(src_dir, name)
@@ -33,14 +44,24 @@ def setup_saves():
3344
tas_saves = ".\\tas_saves"
3445
print(f"Copying save files from '{square_saves}' to '.\\Save Backups'")
3546
shutil.copytree(square_saves, ".\\Save Backups")
36-
print(f"Copying TAS save files from '.\\tas_saves' to {square_saves}' and modifying timestamps.")
47+
print(
48+
f"Copying TAS save files from '.\\tas_saves' to {square_saves}' and modifying timestamps."
49+
)
3750
shutil.rmtree(square_saves)
3851
os.mkdir(square_saves)
3952
current_time = time.time()
40-
for cur_file in sorted([f for f in os.listdir(tas_saves) if os.path.isfile(os.path.join(tas_saves, f))], reverse=True):
53+
for cur_file in sorted(
54+
[
55+
f
56+
for f in os.listdir(tas_saves)
57+
if os.path.isfile(os.path.join(tas_saves, f))
58+
],
59+
reverse=True,
60+
):
4161
_move_file(cur_file, tas_saves, square_saves, current_time)
4262
current_time += 60
4363

64+
4465
def restore_saves():
4566
"""Restores the backed up saves."""
4667
square_saves = f"{FLAGS.user_path or os.path.expanduser('~')}\\Documents\\SQUARE ENIX\\FINAL FANTASY X&X-2 HD Remaster\\FINAL FANTASY X\\"
@@ -50,7 +71,9 @@ def restore_saves():
5071

5172

5273
def main(argv):
53-
print("TAS Save fixer utility. Ensure that the script is run from the main folder of the repo.")
74+
print(
75+
"TAS Save fixer utility. Ensure that the script is run from the main folder of the repo."
76+
)
5477
check_flags()
5578
if FLAGS.setup_saves:
5679
print("Setting TAS Saves Up.")
@@ -61,6 +84,5 @@ def main(argv):
6184
print("Done")
6285

6386

64-
6587
if __name__ == "__main__":
6688
app.run(main)

0 commit comments

Comments
 (0)