-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameMechanic.py
520 lines (423 loc) · 16.2 KB
/
GameMechanic.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import os, time
from GUIParser import InGameAoE3GUIParser, GuiItem
import Hotkeys
import pyautogui
import util
class GameEnvironment:
def __init__(self, botInstance):
self._iteration: int = 0
self._bot = botInstance
self._bot.setGameEnv(self)
self.InGameGui = None
@property
def Iteration(self) -> int:
return self._iteration
def run(self):
time.sleep(3)
self._bot.onStart()
while self.Iteration <= 1_000:
self._bot.onStep()
self._iteration += 1
self._bot.onEnd()
class AoE3Environment(GameEnvironment):
ResourceToUpgradeAge: dict = {
1: {
"Food": 800,
"Wood": 0,
"Gold": 0
},
2: {
"Food": 1_200,
"Wood": 0,
"Gold": 1_000
},
3: {
"Food": 2_000,
"Wood": 0,
"Gold": 1_200
},
4: {
"Food": 4_000,
"Wood": 0,
"Gold": 4_000
},
}
def __init__(self, botInstance):
super(AoE3Environment, self).__init__(botInstance)
self.InGameGui = InGameAoE3GUIParser()
self.GuiItems_updates: dict = {name: -1 for name, _ in self.InGameGui.items()}
self._Age: int = 0
self._food: int = 0
self._wood: int = 0
self._gold: int = 0
self._hmVillager: int = 0
self._foodCollectors: int = 0
self._woodCollectors: int = 0
self._goldCollectors: int = 0
self._hmIdleVillager: int = 0
@property
def Food(self) -> int:
if self.GuiItems_updates["FoodSlot"] < self.Iteration:
self.updateFoodSlot()
return self._food
@property
def Wood(self) -> int:
if self.GuiItems_updates["WoodSlot"] < self.Iteration:
self.updateWoodSlot()
return self._wood
@property
def Gold(self) -> int:
if self.GuiItems_updates["GoldSlot"] < self.Iteration:
self.updateGoldSlot()
return self._gold
@property
def HmVillager(self) -> int:
if self.GuiItems_updates["HmVillagerSlot"] < self.Iteration:
self.updateHmVillagerSlot()
return self._hmVillager
@property
def FoodCollectors(self) -> int:
if self.GuiItems_updates["FoodCollectorsSlot"] < self.Iteration:
self.updateFoodCollectorsSlot()
return self._foodCollectors
@property
def WoodCollectors(self) -> int:
if self.GuiItems_updates["WoodCollectorsSlot"] < self.Iteration:
self.updateWoodCollectorsSlot()
return self._woodCollectors
@property
def GoldCollectors(self) -> int:
if self.GuiItems_updates["GoldCollectorsSlot"] < self.Iteration:
self.updateGoldCollectorsSlot()
return self._goldCollectors
@property
def HmIdleVillager(self) -> int:
if self.GuiItems_updates["HmIdleVillagerSlot"] < self.Iteration:
self.updateHmIdleVillagerSlot()
return self._hmIdleVillager
@property
def Age(self) -> int:
if self.GuiItems_updates["AgeSlot"] < self.Iteration:
self.updateAgeSlot()
return self._Age
def updateAgeSlot(self):
AgeByPath: dict = {
1: "GameMessages/1_Discovery_Age.PNG",
2: "GameMessages/2_Colonial_Age.png",
3: "GameMessages/3_Fortress_Age.png",
4: "GameMessages/4_Industrial_Age.png",
5: "GameMessages/5_Imperial_Age.png"
}
for age, path in AgeByPath.items():
if self.InGameGui.imgIn(path):
self._Age = age
print(f"age: {age}")
break
self.GuiItems_updates["AgeSlot"] = self.Iteration
def updateFoodSlot(self):
foodStr = repr(self.InGameGui["FoodSlot"])
# self.InGameGui["FoodSlot"].show()
if foodStr:
self._food = int(foodStr)
self.GuiItems_updates["FoodSlot"] = self.Iteration
def updateWoodSlot(self):
woodStr = repr(self.InGameGui["WoodSlot"])
# self.InGameGui["WoodSlot"].show()
if woodStr:
self._wood = int(woodStr)
self.GuiItems_updates["WoodSlot"] = self.Iteration
def updateGoldSlot(self):
goldStr = repr(self.InGameGui["GoldSlot"])
# self.InGameGui["GoldSlot"].show()
if goldStr:
self._gold = int(goldStr)
self.GuiItems_updates["GoldSlot"] = self.Iteration
def updateHmVillagerSlot(self):
hmVillagerStr = repr(self.InGameGui["HmVillagerSlot"])
# self.InGameGui["HmVillagerSlot"].show()
if hmVillagerStr:
self._hmVillager = int(hmVillagerStr)
self.GuiItems_updates["HmVillagerSlot"] = self.Iteration
def updateFoodCollectorsSlot(self):
foodCollectorsStr = repr(self.InGameGui["FoodCollectorsSlot"])
if foodCollectorsStr:
self._foodCollectors = int(foodCollectorsStr)
self.GuiItems_updates["FoodCollectorsSlot"] = self.Iteration
def updateWoodCollectorsSlot(self):
woodCollectorsStr = repr(self.InGameGui["WoodCollectorsSlot"])
if woodCollectorsStr:
self._woodCollectors = int(woodCollectorsStr)
self.GuiItems_updates["WoodCollectorsSlot"] = self.Iteration
def updateGoldCollectorsSlot(self):
foodCollectorsStr = repr(self.InGameGui["GoldCollectorsSlot"])
if foodCollectorsStr:
self._goldCollectors = int(foodCollectorsStr)
self.GuiItems_updates["GoldCollectorsSlot"] = self.Iteration
def updateHmIdleVillagerSlot(self):
HmIdleVillagerSlotStr = repr(self.InGameGui["HmIdleVillagerSlot"])
if HmIdleVillagerSlotStr:
self._hmIdleVillager = int(HmIdleVillagerSlotStr)
self.GuiItems_updates["HmIdleVillagerSlot"] = self.Iteration
def getDeckCard(self, CardName: str):
Hotkeys.SendHotKeys("Find Home City")
if CardName not in self.InGameGui.items():
try:
self.InGameGui.addItem(GuiItem(f"{CardName}", None, IconPath=os.getcwd()+f"/DeckCards/{CardName}.PNG"))
except TypeError:
time.sleep(1)
self.InGameGui.addItem(GuiItem(f"{CardName}", None, IconPath=os.getcwd()+f"/DeckCards/{CardName}.PNG"))
card = self.InGameGui[CardName]
card.click()
pyautogui.hotkey("esc")
return 60
def selectFoodCollector(self):
from Units import Unit, Villager
self.InGameGui["FoodCollectorsSlot"].click()
unitIcon = Unit.UnitIconsDirectory + self._bot.civ + "/Villager.PNG"
return Villager(unitIcon, self) if self.InGameGui.imgIn(unitIcon) else None
def selectWoodCollector(self):
self.InGameGui["WoodCollectorsSlot"].click()
def selectGoldCollector(self):
self.InGameGui["GoldCollectorsSlot"].click()
def selectIdleVillager(self):
from Units import Unit, Villager
Hotkeys.Find_idle_Villager()
unitIcon = Unit.UnitIconsDirectory+self._bot.civ+"/Villager.PNG"
return Villager(unitIcon, self) if self.InGameGui.imgIn(unitIcon) else None
def selectMarket(self):
from Units import Unit, Market
Hotkeys.SendHotKeys("Find Market")
unitIcon = Unit.UnitIconsDirectory+self._bot.civ+"/Market.PNG"
return Market(unitIcon, self) if self.InGameGui.imgIn(unitIcon) else None
def selectMill(self):
from Units import Unit, Mill
Hotkeys.SendHotKeys("Find Mill")
unitIcon = Unit.UnitIconsDirectory + self._bot.civ + "/Mill.PNG"
return Mill(unitIcon, self) if self.InGameGui.imgIn(unitIcon) else None
def selectTownCenter(self):
from Units import Unit, TownCenter
Hotkeys.SendHotKeys("Find Town Center")
unitIcon = Unit.UnitIconsDirectory + self._bot.civ + "/TownCenter.png"
return TownCenter(unitIcon, self) if self.InGameGui.imgIn(unitIcon) else None
def selectHero(self):
return
@staticmethod
def build():
from GUIParser import GUIParser
center = pyautogui.size()[0] // 2, pyautogui.size()[1] // 2
# center = self.InGameGui.activeWindow.center
pyautogui.moveTo(*center)
errorMess_paths = [fr"GameMessages/Other_objects_prevent_you_from_placing_this_here.PNG",
fr"GameMessages/You_must_explore_this_area_before_placing_items_there.PNG",
fr"GameMessages/This_building_has_corps_around_it_You_need_to_allow_more_space.PNG",
fr"GameMessages/You_may_not_obstruct_a_Trade_Route.PNG"]
search = AoE3Env.PerformCrossSearch(errorMess_paths, func=pyautogui.leftClick, sleep=0.7, inv=True)
if search is not None:
return None
while any([GUIParser.imgIn(mess) for mess in errorMess_paths]):
direction: str = util.randomDirection(("left", "right", "up", "down"))
pyautogui.keyDown(direction)
util.randomSleep(b=0.2)
pyautogui.keyUp(direction)
pyautogui.press(direction)
pyautogui.leftClick()
pyautogui.hotkey("esc")
@staticmethod
def PerformCrossSearch(pathsToFind: list, variances: tuple = (50, 100, 150), func=pyautogui.rightClick,
sleep: float = 1.0, **kwargs):
"""
Perform a cross search to find the given paths in the screen.
:param pathsToFind: The path we want to find. (list[str])
:param variances: The variances points (tuple[int])
:param func: The function to perform when a path is find.
:param sleep: The time to sleep between each point [s] (float)
:param kwargs:
:inv: To perform a invert search. i.e We don't want to see any of the path.
:return: The found position (int, int) if found else None
"""
import time
from GUIParser import GUIParser
inv: bool = kwargs.get("inv", False)
center = pyautogui.size()[0] // 2, pyautogui.size()[1] // 2
pyautogui.moveTo(*center)
searchPositions: list = list()
for var in variances:
searchPositions.extend([(center[0], center[1]+var),
(center[0]+var, center[1]),
(center[0], center[1]-var),
(center[0]-var, center[1])])
for searchPos in searchPositions:
pyautogui.moveTo(searchPos)
time.sleep(sleep)
if inv:
if not any([GUIParser.imgIn(path) for path in pathsToFind]):
func()
pyautogui.hotkey("esc")
return searchPos
else:
if any([GUIParser.imgIn(path) for path in pathsToFind]):
func()
pyautogui.hotkey("esc")
return searchPos
return None
@staticmethod
def deselectAll():
pyautogui.hotkey("esc")
def findSilverMine(self):
region = self.InGameGui.locate("MapItems/SilverMineTiny.png",
grayscale=False, region=self.InGameGui["Map"].box,
confidence=0.5)
if region is not None:
SilverMine = GuiItem("SilverMineMapItem", region=region, IconPath="MapItems/SilverMineTiny.png")
self.InGameGui.addItem(SilverMine)
SilverMine.click()
return region
GameEnv = GameEnvironment
AoE3Env = AoE3Environment
class BotAi:
def __init__(self, name: str, civ: str):
self.GameEnv = None
self._name: str = name
self._civ: str = civ
@property
def name(self) -> str:
return self._name
@property
def civ(self) -> str:
return self._civ
@property
def Age(self) -> int:
return self.GameEnv.Age
def setGameEnv(self, gameEnv):
self.GameEnv: GameEnv = gameEnv
def onStart(self):
raise NotImplementedError()
def onStep(self):
raise NotImplementedError()
def onEnd(self):
raise NotImplementedError()
class AoE3yBotAi(BotAi):
CollectorsRatioByAge: dict = {
1: {
"Food": 0.4,
"Wood": 0.5,
"Gold": 0.1
},
2: {
"Food": 0.5,
"Wood": 0.3,
"Gold": 0.2
},
3: {
"Food": 0.5,
"Wood": 0.3,
"Gold": 0.2
},
4: {
"Food": 0.5,
"Wood": 0.3,
"Gold": 0.2
},
5: {
"Food": 0.5,
"Wood": 0.1,
"Gold": 0.4
},
}
@property
def Food(self) -> int:
return self.GameEnv.Food
@property
def Wood(self) -> int:
return self.GameEnv.Wood
@property
def Gold(self) -> int:
return self.GameEnv.Gold
@property
def HmVillager(self) -> int:
return self.GameEnv.HmVillager
@property
def Iteration(self) -> int:
return self.GameEnv.Iteration
@property
def FoodCollectors(self) -> int:
return self.GameEnv.FoodCollectors
@property
def WoodCollectors(self) -> int:
return self.GameEnv.WoodCollectors
@property
def GoldCollectors(self) -> int:
return self.GameEnv.GoldCollectors
@property
def HmIdleVillager(self) -> int:
return self.GameEnv.HmIdleVillager
def distributeWorkers(self):
from Units import Villager
HmIdealWorkersByResource: dict = {
resource: self.CollectorsRatioByAge[self.Age][resource]*self.HmVillager
for resource in {"Food", "Wood", "Gold"}
}
HmCurrentWorkersByResource: dict = {
"Food": self.FoodCollectors,
"Wood": self.WoodCollectors,
"Gold": self.GoldCollectors
}
DeltaWorkersByResource: dict = {
resource: HmIdealWorkersByResource[resource] - HmCurrentWorkersByResource[resource]
for resource in {"Food", "Wood", "Gold"}
}
idleVillager = self.GameEnv.selectIdleVillager()
if idleVillager is not None:
idleVillager.WorkOnSilverMine()
idleVillager = self.GameEnv.selectIdleVillager()
if idleVillager is not None:
idleVillager.WorkOnMill()
print(HmIdealWorkersByResource, HmCurrentWorkersByResource, DeltaWorkersByResource, sep='\n')
if self.GoldCollectors < HmIdealWorkersByResource["Gold"] and self.FoodCollectors > HmIdealWorkersByResource["Food"]:
villager = self.GameEnv.selectFoodCollector()
if villager is not None:
print(f"{villager} -> {villager.WorkOnSilverMine}")
villager.WorkOnSilverMine()
def CanImproveAge(self):
resources = AoE3Env.ResourceToUpgradeAge[self.Age]
print(resources, all([self.Food >= resources["Food"], self.Wood >= resources["Wood"], self.Gold >= resources["Gold"]]))
return all([self.Food >= resources["Food"], self.Wood >= resources["Wood"], self.Gold >= resources["Gold"]])
def ImproveAge(self):
tc = self.GameEnv.selectTownCenter()
if tc is not None and self.CanImproveAge():
tc.ImproveAge()
def onStart(self):
pass
def onStep(self):
from Units import Wagon, TownCenter
# if self.Iteration == 1:
# # construction of the first town
# time.sleep(Wagon.BuildTownCenter())
# time.sleep(30)
#
# elif self.Iteration == 2:
# idlv = self.GameEnv.selectIdleVillager()
# if idlv is not None:
# time.sleep(idlv.BuildMarket())
#
# elif self.Iteration == 3:
# [time.sleep(TownCenter.TrainVillager()) for _ in range(2)]
# time.sleep(30)
#
# elif self.Iteration == 4:
# time.sleep(self.GameEnv.getDeckCard("300Woods"))
#
# elif self.Iteration == 5:
# idlv = self.GameEnv.selectIdleVillager()
# if idlv is not None:
# time.sleep(idlv.BuildMill())
#
# else:
# self.distributeWorkers()
# self.ImproveAge()
self.distributeWorkers()
self.ImproveAge()
def onEnd(self):
pass
if __name__ == '__main__':
pass