forked from wT-/TekkenBot
-
Notifications
You must be signed in to change notification settings - Fork 45
/
GameInputter.py
298 lines (230 loc) · 8.49 KB
/
GameInputter.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
A layer of abstraction over ArtificialKeyboard, GameInputter.py takes basic Tekken commands (forward, tap light punch,
hold back) and turns them into the actual keypresses.
"""
from enum import Enum
from ArtificialKeyboard import ArtificalKeyboard
#directinput scan codes
#https://gist.github.com/tracend/912308
class Keys_P2:
UP = 0xc8
DOWN = 0xd0
LEFT = 0xCB
RIGHT = 0xCD
A = 0x4b
B = 0x4c
X = 0x47
Y = 0x48
START = 0xc7
SELECT = 0xd2
LB = 0x49
RB = 0x4d
LT = 0x4a
RT = 0x4e
class Keys_P1:
UP = 0x11
DOWN = 0x1F
LEFT = 0x1E
RIGHT = 0x20
A = 0x24
B = 0x25
X = 0x16
Y = 0x17
START = 0x30
SELECT = 0x2F
LB = 0x19
RB = 0x18
LT = 0x27
RT = 0x26
class Buttons(Enum):
BUTTON_1 = 1
BUTTON_2 = 2
BUTTON_3 = 3
BUTTON_4 = 4
BUTTON_LEFT = 5
BUTTON_RIGHT = 6
BUTTON_UP = 7
BUTTON_DOWN = 8
BUTTON_RB = 9
BUTTON_ACCEPT = 10
BUTTON_SELECT = 11
class GameControllerInputter:
p2_controls = {
Buttons.BUTTON_1 : Keys_P2.X,
Buttons.BUTTON_2 : Keys_P2.Y,
Buttons.BUTTON_3 : Keys_P2.A,
Buttons.BUTTON_4 : Keys_P2.B,
Buttons.BUTTON_LEFT : Keys_P2.LEFT,
Buttons.BUTTON_RIGHT : Keys_P2.RIGHT,
Buttons.BUTTON_UP : Keys_P2.UP,
Buttons.BUTTON_DOWN : Keys_P2.DOWN,
Buttons.BUTTON_RB : Keys_P2.RB,
Buttons.BUTTON_ACCEPT : Keys_P2.A,
Buttons.BUTTON_SELECT : Keys_P2.SELECT
}
p1_controls = {
Buttons.BUTTON_1: Keys_P1.X,
Buttons.BUTTON_2: Keys_P1.Y,
Buttons.BUTTON_3: Keys_P1.A,
Buttons.BUTTON_4: Keys_P1.B,
Buttons.BUTTON_LEFT: Keys_P1.LEFT,
Buttons.BUTTON_RIGHT: Keys_P1.RIGHT,
Buttons.BUTTON_UP: Keys_P1.UP,
Buttons.BUTTON_DOWN: Keys_P1.DOWN,
Buttons.BUTTON_RB: Keys_P1.RB,
Buttons.BUTTON_ACCEPT: Keys_P1.A,
Buttons.BUTTON_SELECT: Keys_P1.SELECT
}
def __init__(self, usePlayer2Controls = True):
if usePlayer2Controls:
self.controls = GameControllerInputter.p2_controls
else:
self.controls = GameControllerInputter.p1_controls
self.heldKeys = []
self.tappedKeys = []
self.releasedKeys = []
self.heldKeys = []
self.performedInitialKeyRelease = False
self.wasOnLeft = True
self.isOnLeft = True
self.releasedKeyJailTime = 0
self.isTekkenActiveWindow = False
self.SetControlsOnLeft()
def checkFacing(self):
isBotOnLeft = self.isOnLeft
if(self.wasOnLeft != isBotOnLeft):
if isBotOnLeft:
print("Bot on Left")
self.SetControlsOnLeft()
else: #bot is on the right
print("Bot on Right")
self.SetControlsOnRight()
self.Release()
self.wasOnLeft = isBotOnLeft
def SetControlsOnLeft(self):
self.back = self.controls[Buttons.BUTTON_LEFT]
self.forward = self.controls[Buttons.BUTTON_RIGHT]
self.right = self.controls[Buttons.BUTTON_DOWN]
self.left = self.controls[Buttons.BUTTON_UP]
def SetControlsOnRight(self):
self.back = self.controls[Buttons.BUTTON_RIGHT]
self.forward = self.controls[Buttons.BUTTON_LEFT]
self.right = self.controls[Buttons.BUTTON_UP]
self.left = self.controls[Buttons.BUTTON_DOWN]
def TapBack(self):
self.TapButton(self.back)
def TapForward(self):
self.TapButton(self.forward)
def TapDown(self):
self.TapButton(self.controls[Buttons.BUTTON_DOWN])
def TapUp(self):
self.TapButton(self.controls[Buttons.BUTTON_UP])
def TapRight(self):
self.TapButton(self.right)
def TapLeft(self):
self.TapButton(self.left)
def Tap1(self):
self.TapButton(self.controls[Buttons.BUTTON_1])
def Tap2(self):
self.TapButton(self.controls[Buttons.BUTTON_2])
def Tap3(self):
self.TapButton(self.controls[Buttons.BUTTON_3])
def Tap4(self):
self.TapButton(self.controls[Buttons.BUTTON_4])
def TapAccept(self):
self.TapButton(self.controls[Buttons.BUTTON_ACCEPT])
def TapSelect(self):
self.TapButton(self.controls[Buttons.BUTTON_SELECT])
def TapRageArt(self):
self.TapButton(self.controls[Buttons.BUTTON_RB])
def HoldBack(self):
self.HoldButton(self.back)
def HoldDown(self):
self.HoldButton(self.controls[Buttons.BUTTON_DOWN])
def HoldUp(self):
self.HoldButton(self.controls[Buttons.BUTTON_UP])
def HoldForward(self):
self.HoldButton(self.forward)
def Hold1(self):
self.HoldButton(self.controls[Buttons.BUTTON_1])
def Hold2(self):
self.HoldButton(self.controls[Buttons.BUTTON_2])
def Hold3(self):
self.HoldButton(self.controls[Buttons.BUTTON_3])
def Hold4(self):
self.HoldButton(self.controls[Buttons.BUTTON_4])
def HoldRage(self):
self.HoldButton(self.controls[Buttons.BUTTON_RB])
def ReleaseForward(self):
self.ReleaseButton(self.forward)
def ReleaseUp(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_UP])
def ReleaseBack(self):
self.ReleaseButton(self.back)
def ReleaseDown(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_DOWN])
def Release1(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_1])
def Release2(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_2])
def Release3(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_3])
def Release4(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_4])
def ReleaseRage(self):
self.ReleaseButton(self.controls[Buttons.BUTTON_RB])
def ResetPractice(self):
self.TapAccept()
self.TapSelect()
def ReleaseButton(self, button):
self.ReleaseKeyIfActive(button)
if button in self.heldKeys:
self.heldKeys.remove(button)
def TapButton(self, button):
if not button in self.releasedKeys:
self.PressKeyIfActive(button)
self.tappedKeys.append(button)
self.releasedKeyJailTime = 0
def HoldButton(self, button):
if not button in self.heldKeys:
self.PressKeyIfActive(button)
self.heldKeys.append(button)
def Update(self, isTekkenActiveWindow, isOnLeft):
self.isTekkenActiveWindow = isTekkenActiveWindow
self.isOnLeft = isOnLeft
print("Bot on: " + str(self.isOnLeft))
self.checkFacing()
if isTekkenActiveWindow and not self.performedInitialKeyRelease:
self.Release()
self.performedInitialKeyRelease = True
else:
self.releasedKeyJailTime -= 1
if self.releasedKeyJailTime <= 0:
self.releasedKeys = []
for button in self.tappedKeys:
self.ReleaseKeyIfActive(button)
self.releasedKeys.append(button)
if button in self.heldKeys:
self.heldKeys.remove(button)
self.tappedKeys = []
def Release(self):
if (self.isTekkenActiveWindow):
self.heldKeys = []
self.ReleaseAllButtons()
def PressKeyIfActive(self, hexKeyCode):
if (self.isTekkenActiveWindow):
ArtificalKeyboard.PressKey(hexKeyCode)
def ReleaseKeyIfActive(self, hexKeyCode):
if (self.isTekkenActiveWindow):
ArtificalKeyboard.ReleaseKey(hexKeyCode)
def ReleaseAllButtons(self):
#print("releasing all buttons")
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_1])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_2])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_3])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_4])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_UP])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_DOWN])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_LEFT])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_RIGHT])
ArtificalKeyboard.ReleaseKey(self.controls[Buttons.BUTTON_RB])