-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpigas.py
More file actions
200 lines (157 loc) · 7.76 KB
/
Copy pathpigas.py
File metadata and controls
200 lines (157 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import traceback
from library.entity_attributes import *
from library.errors import *
from modules.gingham_processing import GinghamProcessor
from modules.game_window import GameWindow
from modules.navigation import Navigator
from modules.companion import CompanionControlLoop
from modules.workflow_handler import ScriptWorkflowHandler
from library.miscellaneous import is_debug, setup_logging
from library.miscellaneous import unexpected_finish
if __name__ == '__main__':
config_file = 'config.yaml'
try:
# log level according to config
debug = is_debug(config_file)
setup_logging(debug=debug,
loggers=[
'game_window',
'script_control',
'hardware_input',
'navigation',
'companion'
])
# script workflow handler
workflow_handler = ScriptWorkflowHandler(config_file=config_file)
# game window
game_window = GameWindow()
game_window.set_window_parameters(config_data=workflow_handler.config_data)
# gingham analyzer
gingham = GinghamProcessor()
# geometry and navigation
navigator = Navigator(config_data=workflow_handler.config_data)
# companion actions
companion = CompanionControlLoop()
companion.initialize_companion(game_window=game_window,
expansion=workflow_handler.expansion,
config_data=workflow_handler.config_data,
debug=debug)
workflow_handler.set_modules(companion, gingham, navigator, game_window)
while True:
### preparations
# check if a game window still exists
game_window.ensure_window_exists()
# set frame
workflow_handler.set_frame()
# activate a window
if not workflow_handler.pause_command:
game_window.ensure_window_active()
# gingham calibration at first frame run
workflow_handler.calibration_workflow(debug=debug)
# set session data
session_data = workflow_handler.session_data
# extend session data with gingham data
screenshot = game_window.take_screenshot(savefig=debug)
gingham_pixels = gingham.pixels_analysis(data=screenshot,
n_monitoring_pixels=game_window.n_pixels['y'],
pixel_height=game_window.pixel_size['x'],
pixel_width=game_window.pixel_size['y'])
session_data.update(gingham.to_dictionary(gingham_pixels))
# extend session data with calculated geometry
session_geometry = navigator.game_state_geometry(session_data)
session_data.update(session_geometry)
# update companion with new session data
companion.session_data.update(session_data)
# check for context for comments if streaming
if workflow_handler.streaming[0]:
companion.set_comment_as_player_message(comment_file=workflow_handler.streaming[1])
### script workflow control
# update script workflow controller
workflow_handler.set_workflow_commands(session_data)
# workflow control
should_disable = workflow_handler.script_workflow_control()
companion.workflow_report(report_disable=should_disable)
if workflow_handler.pause_command:
continue
### companion workflow
# check if a companion is moving somewhere
companion.check_movement_keys()
# companion behaviors defined by player commands
companion.define_behaviour()
# companion duties defined by commands, statuses and navigation (positions and angles)
companion.clear_duties()
companion.define_duties()
# define state based on duties
companion.define_state()
# logging
companion.get_profile()
###### companion logic
### movement
# doesn't move if:
# actions: INITIALIZING, RESPONDING, CHANGING_SPEED, MOUNTING, UNMOUNTING,
# stay: STAYING, WAITING_FOR_PLAYER,
# combat: ATTACKING_TO_DEFEND, HEALING_YOURSELF
if not companion.state_is_one_of(companion.movement_restricted_states):
companion.rotate_to(Duty.ROTATE_TO_PLAYER)
companion.move_to(Duty.NEARING_WITH_PLAYER)
if companion.has_duty(Duty.AVOID_LOW_OBSTACLE):
companion.jump()
## some actions at entering the game (buffing will be the other state)
if companion.state_is(State.INITIALIZING):
companion.entering_the_game()
companion.apply_buffing_rotation(ally_target='companion')
### respond to message
if companion.state_is(State.RESPONDING):
companion.respond_to_player()
if companion.state_is(State.CHANGING_SPEED):
companion.changing_speed()
### mounting
if companion.state_is(State.MOUNTING):
companion.mounting()
### unmounting
if companion.state_is(State.UNMOUNTING):
companion.unmounting()
### announce if should to wait for the player
if companion.state_is(State.WAITING_FOR_PLAYER):
companion.waiting_for_player()
### looting
# performs prelooting actions
if companion.state_is(State.NEARING_FOR_LOOTING):
companion.perform_prelooting_actions()
# perform looting
if companion.state_is(State.LOOTING):
# values - part of screen around the character
companion.do_loot_actions(looting_area={'x_length': 0.3, 'y_length': 0.3,
'x_step': 0.025, 'y_step': 0.055})
### healing
# player
if companion.state_is(State.HEALING_PLAYER) and not companion.has_duty(Duty.ROTATE_TO_PLAYER):
companion.apply_healing_rotation(target='player')
# yourself
if companion.state_is(State.HEALING_YOURSELF):
companion.apply_healing_rotation(target='companion')
### combat
# help to player
if companion.state_is(State.ATTACKING_TO_HELP):
companion.rotate_to(Duty.ROTATE_TO_PLAYER_FACING)
if not companion.has_one_of_duties([Duty.ROTATE_TO_PLAYER_FACING, Duty.ROTATE_TO_PLAYER]):
companion.apply_combat_rotation(ally_target=None,
enemy_is_target_of='player')
# defend
if companion.state_is(State.ATTACKING_TO_DEFEND):
companion.apply_combat_rotation(ally_target='companion',
enemy_is_target_of='companion')
companion.check_spellbook_cooldowns()
workflow_handler.set_loop_control_execution_time()
except KeyboardInterrupt:
ScriptWorkflowHandler(config_file=config_file).finish_script()
except (GameWindowError,
CompanionControlError,
WorkflowHandlerError,
GinghamProcessorError,
CommonError) as e:
unexpected_finish(e)
except Exception as e:
unexpected_finish(e,
title="Unhandled exception occurs",
traceback=str(traceback.format_exc()))