-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_sim.py
More file actions
568 lines (478 loc) · 23.2 KB
/
main_sim.py
File metadata and controls
568 lines (478 loc) · 23.2 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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# --- Standard Libraries ---
import random
# --- Pygame ---
import pygame
# --- Globals / Shared State ---
from merge_sim.constants import BOARD_ROWS, BOARD_COLS, bombs, rn
# --- Cards ---
from merge_sim.cards import (
Card,
CARD_STATS
)
# --- Modifiers / Synergies ---
from merge_sim.modifiers import (
ClanSynergyManager,
BrawlerSynergyManager,
NobleSynergyManager,
GoblinSynergyManager,
ThrowerSynergyManager,
UndeadSynergyManager,
AvengerSynergyManager,
RangerSynergyManager,
AceSynergyManager,
AssassinSynergyManager,
JuggernautSynergyManager
)
# --- Visualisation / Graphics ---
from merge_sim.visualise import draw_grid, hex_to_pixel, PLAYER_COLOURS
# --- Combat / Player Units ---
from merge_sim.combat_unit import spawn_skeleton
from merge_sim.player import Player
# --- Board / Hex Utilities ---
from merge_sim.board_utils import (
get_occupied_positions,
combine_grids,
print_combined_grid
)
from merge_sim.hex_utils import (
hex_neighbors,
hex_distance,
find_path_bfs_to_range,
)
# --- Projectiles ---
from merge_sim.projectile import Projectile
# --- Bots ---
from merge_sim.player import get_player_colour
from merge_sim.bot import *
def simulate_and_visualize_combat_live(players):
"""
Simulates a live combat round between two players and visualizes it using pygame.
Restores all units to full HP before starting, places missing-position units,
and shows debug information throughout the battle.
Args:
players (list): A list containing the two players.
Returns:
tuple: ([], winner_player_object_or_None, remaining_units_count_or_None)
"""
if not players or len(players) < 2 or not players[0].opponent:
return [], None, None
# --- INITIAL UNIT RESET & PLACEMENT ---
for player in players:
for unit in player.field:
unit.restore_full_health()
if unit.row is None or unit.col is None:
player.place_on_grid_random(unit)
# --- COMBINE PLAYER GRIDS ---
p1, p2 = players[0], players[0].opponent
combined = combine_grids(p1, p2)
# Gather all units into a flat list
units = []
seen_units = set()
for r in range(BOARD_ROWS):
for c in range(BOARD_COLS):
unit = combined[r][c]
if unit and unit not in seen_units:
units.append(unit)
seen_units.add(unit)
if not units:
return [], None, None
p1.clan_manager = ClanSynergyManager(p1) # pass all units on the board
p2.clan_manager = ClanSynergyManager(p2) # pass all units on the board
p1.clan_manager.setup_round() # counts Clan cards at start of round
p2.clan_manager.setup_round() # counts Clan cards at start of round
p1.brawler_manager = BrawlerSynergyManager(p1)
p2.brawler_manager = BrawlerSynergyManager(p2)
p1.brawler_manager.setup_round()
p2.brawler_manager.setup_round()
p1.noble_manager = NobleSynergyManager(p1, False)
p2.noble_manager = NobleSynergyManager(p2, True)
p1.noble_manager.setup_round()
p2.noble_manager.setup_round()
p1.goblin_manager = GoblinSynergyManager(p1)
p2.goblin_manager = GoblinSynergyManager(p2)
p1.goblin_manager.setup_round()
p2.goblin_manager.setup_round()
p1.thrower_synergy = ThrowerSynergyManager(p1)
p2.thrower_synergy = ThrowerSynergyManager(p2)
p1.thrower_synergy.setup_round()
p2.thrower_synergy.setup_round()
p1.undead_manager = UndeadSynergyManager(p1)
p2.undead_manager = UndeadSynergyManager(p2)
p1.undead_manager.setup_round()
p2.undead_manager.setup_round()
p1.avenger_manager = AvengerSynergyManager(p1)
p2.avenger_manager = AvengerSynergyManager(p2)
p1.avenger_manager.setup_round()
p2.avenger_manager.setup_round()
p1.ranger_manager = RangerSynergyManager(p1)
p2.ranger_manager = RangerSynergyManager(p2)
p1.ranger_manager.setup_round()
p2.ranger_manager.setup_round()
p1.ace_manager = AceSynergyManager(p1)
p2.ace_manager = AceSynergyManager(p2)
p1.ace_manager.setup_round()
p2.ace_manager.setup_round()
p1.assassin_manager = AssassinSynergyManager(p1)
p2.assassin_manager = AssassinSynergyManager(p2)
p1.assassin_manager.setup_round(units, combined, False)
p2.assassin_manager.setup_round(units, combined, True)
p1.juggernaut_manager = JuggernautSynergyManager(p1)
p2.juggernaut_manager = JuggernautSynergyManager(p2)
p1.juggernaut_manager.setup_round(combined, False)
p2.juggernaut_manager.setup_round(combined, True)
# --- CLEAR ANY EXISTING BOMBS AT ROUND START ---
bombs.clear()
# --- PYGAME INITIALIZATION ---
pygame.init()
screen = pygame.display.set_mode((1200, 1000))
pygame.display.set_caption("MergeTacticsBot Combat Visualization (Live)")
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 30)
FPS = 60
projectiles = []
start_ticks = pygame.time.get_ticks()
total_paused_time = 0
paused = False
round_count = 0
winner = None
remaining_units = None
for unit in units:
if getattr(unit.card, "name", "").lower() == "prince":
unit.prince_combat_start_ability([u for u in units if u.alive], combined)
# --- MAIN SIMULATION LOOP ---
while True:
round_count += 1
# Update living units
living_units = [u for u in units if u.alive]
if not living_units:
break
# Check if both players still have alive units
p1_alive = any(u.alive and u.owner == p1 for u in units)
p2_alive = any(u.alive and u.owner == p2 for u in units)
if not p1_alive or not p2_alive:
break
# --- HANDLE PYGAME EVENTS ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return [], None, None
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
return [], None, None
elif event.key == pygame.K_SPACE:
paused = not paused
if paused:
pause_start_time = pygame.time.get_ticks()
else:
pause_end_time = pygame.time.get_ticks()
total_paused_time += (pause_end_time - pause_start_time)
# --- TIME CALCULATIONS ---
current_ticks = pygame.time.get_ticks()
if paused:
current_time = (pause_start_time - start_ticks - total_paused_time) / 1000.0
else:
current_time = (current_ticks - start_ticks - total_paused_time) / 1000.0
dt = clock.get_time() / 1000.0
# --- UNIT LOGIC LOOP (handle newly spawned units dynamically) ---
i = 0
while i < len(units):
unit = units[i]
if not unit.alive:
i += 1
continue
# ✅ Clan synergy check
unit.owner.clan_manager.trigger(unit)
unit.owner.avenger_manager.update_last_standing()
# Update status effects
time_step = current_time - getattr(unit, 'last_update_time', current_time)
unit.update_status_effects(time_step)
unit.last_update_time = current_time
if not unit.can_act():
i += 1
continue
# Target acquisition
if not unit.current_target or not unit.current_target.alive or getattr(unit.current_target, 'invisible', False):
# Only consider alive and visible enemies
visible_enemies = [u for u in units if u.alive and not getattr(u, 'invisible', False) and u.owner != unit.owner]
if visible_enemies:
closest_enemy, _ = unit.find_closest_enemy(visible_enemies)
unit.current_target = closest_enemy
unit.is_attacking = False
unit.last_attack_time = None
# Retargeting
else:
new_target = unit.should_retarget(units, combined)
if new_target and new_target != unit.current_target and not getattr(new_target, 'invisible', False):
print(f"🔄 {unit.card.name} is retargeting from {unit.current_target.card.name} to {new_target.card.name}")
unit.current_target = new_target
unit.last_attack_time = None
# ATTACK LOGIC
if unit.current_target is not None and unit.is_in_range_of(unit.current_target):
unit.is_attacking = True
if unit.last_attack_time is None:
unit.last_attack_time = current_time
elif unit.can_attack(current_time):
if unit is not None and unit.current_target is not None and unit.alive and unit.current_target.alive and not getattr(unit.current_target, 'invisible', False):
# safe to attack
# Perform unit-specific attack
try:
attacker_pos = hex_to_pixel(*unit.get_position())
target_pos = hex_to_pixel(*unit.current_target.get_position())
attack_result = unit.attack(unit.current_target, current_time, units, combined)
unit.owner.ranger_manager.on_attack(unit)
if attack_result:
unit.last_attack_time = current_time
print(f"Position of {unit.card.name} [{unit.owner.name}]: {unit.get_position()}")
colour = PLAYER_COLOURS.get(unit.owner.name, (255, 255, 255))
projectiles.append(Projectile(attacker_pos, target_pos, colour))
except Exception as e:
attacker_name = getattr(unit.card, 'name', 'Unknown')
target_name = getattr(unit.current_target.card, 'name', 'Unknown') if unit.current_target else 'None'
attacker_pos = unit.get_position() if hasattr(unit, 'get_position') else ('?', '?')
target_pos = unit.current_target.get_position() if unit.current_target and hasattr(unit.current_target, 'get_position') else ('?', '?')
attacker_owner = getattr(unit.owner, 'name', 'Unknown')
target_owner = getattr(unit.current_target.owner, 'name', 'Unknown') if unit.current_target else 'None'
attacker_hp = getattr(unit, 'current_hp', 'Unknown')
target_hp = getattr(unit.current_target, 'current_hp', 'Unknown') if unit.current_target else 'None'
print("⚠️ Attack error:", e)
print(f"Attacker: {attacker_name} (Owner: {attacker_owner}, HP: {attacker_hp}, Pos: {attacker_pos})")
print(f"Target: {target_name} (Owner: {target_owner}, HP: {target_hp}, Pos: {target_pos})")
# Optional: prevent further crashing by only calling take_damage if current_target is valid
if unit.current_target and getattr(unit.current_target, 'alive', False):
unit.current_target.take_damage(unit.get_damage(), combined, all_units=units, attacker=unit)
unit.last_attack_time = current_time
else:
unit.is_attacking = False
# MOVEMENT LOGIC
if unit.current_target and unit.current_target.alive and unit.alive:
target_pos = unit.current_target.get_position()
if target_pos is None:
continue
if not unit.is_in_range_of(unit.current_target) and unit.can_move(current_time):
current_pos = unit.get_position()
occupied = get_occupied_positions(units, reserved_positions=None, excluding_unit=unit)
best_move = None
best_dist = float('inf')
for move_pos in hex_neighbors(current_pos[0], current_pos[1]):
if move_pos not in occupied:
path = find_path_bfs_to_range(move_pos, target_pos, unit.card.range, occupied_positions=occupied)
if path:
dist = len(path) - 1
else:
dist = float('inf')
if dist < best_dist:
best_dist = dist
best_move = move_pos
if best_move:
unit.move_to(*best_move, combined)
unit.move_cooldown = current_time
unit.last_move_time = current_time
unit.last_position = best_move
# --- inside the main loop, after unit actions ---
for bomb in bombs[:]: # iterate over a copy
bomb["timer"] -= dt # dt = time step per frame
# Print countdown (rounded to 2 decimals for readability)
print(f"⏳ Bomb at {bomb['pos']} exploding in {bomb['timer']:.2f}s")
if bomb["timer"] <= 0:
# Trigger explosion
bomb_pos = bomb["pos"]
radius = bomb["radius"]
damage = bomb["damage"]
stun_duration = bomb["stun"]
for unit in units:
if unit.alive and unit.owner != bomb["owner"] and hex_distance(unit.get_position(), bomb_pos) <= radius:
unit.take_damage(damage, combined, units)
unit.status_effects["stunned"] = max(unit.status_effects.get("stunned", 0), stun_duration)
print(f"💥 Bomb hits {unit.card.name} (Owner: {unit.owner.name}) for {damage} damage and {stun_duration}s stun!")
bombs.remove(bomb)
# AFTER ATTACK/MOVE: newly spawned units are already in 'units', so they'll be processed in subsequent iterations
i += 1 # increment manually to include new units
# Spawn skeletons for positions recorded by Skeleton King
for unit in units:
if unit.card.name.lower() == "skeleton-king":
if hasattr(unit, "killed_enemy_this_round"):
for idx, killed_info in enumerate(unit.killed_enemy_this_round):
print("Entered loop for Skeleton King kills")
pos = killed_info.get("pos")
level = killed_info.get("level")
owner = killed_info.get("owner")
print(f"🔹 Attempting spawn {idx+1}: pos={pos}, level={level}, owner={owner.name if owner else 'None'}")
if pos is None or owner is None:
print(f"⚠️ Skipping spawn: invalid position or owner!")
continue
# Check if tile is already occupied
occupied_positions = {(u.row, u.col) for u in units if u.alive}
if pos in occupied_positions:
print(f"⚠️ Cannot spawn skeleton at {pos}, tile is occupied!")
continue
# Spawn skeleton
skeleton_unit = spawn_skeleton(pos, level, owner, units, combined)
if skeleton_unit:
print(f"✅ Spawned skeleton at {pos} for {owner.name} (level {level})")
else:
print(f"❌ Failed to spawn skeleton at {pos}")
# Clear after processing
unit.killed_enemy_this_round = []
else:
print(f"ℹ️ Skeleton King {unit.card.name} has no recorded kills")
# --- UPDATE PROJECTILES ---
for projectile in projectiles[:]:
projectile.update(dt)
if projectile.is_finished():
projectiles.remove(projectile)
# --- RENDER FRAME ---
screen.fill((30, 30, 30))
draw_grid(screen, combined, units=units)
for projectile in projectiles:
pos = projectile.get_position()
pygame.draw.circle(screen, projectile.colour, (int(pos[0]), int(pos[1])), 8)
pygame.display.flip()
clock.tick(FPS)
# --- CHECK FOR END CONDITION ---
p1_alive = any(u.alive and u.owner == p1 for u in units)
p2_alive = any(u.alive and u.owner == p2 for u in units)
if not p1_alive and not p2_alive:
winner = None
remaining_units = None
break
elif not p1_alive:
winner = p2
remaining_units = len([u for u in units if u.alive and u.owner == p2])
break
elif not p2_alive:
winner = p1
remaining_units = len([u for u in units if u.alive and u.owner == p1])
break
p1.goblin_manager.on_buy_phase_start(rn)
p2.goblin_manager.on_buy_phase_start(rn)
p1.thrower_synergy.reset_synergy()
p2.thrower_synergy.reset_synergy()
pygame.quit()
return [], winner, remaining_units
def assign_opponents(players):
alive_players = [p for p in players if p.hp > 0]
players_shuffled = alive_players[:]
random.shuffle(players_shuffled)
for p in players_shuffled:
p.opponent = None
for i in range(0, len(players_shuffled) - 1, 2):
p1 = players_shuffled[i]
p2 = players_shuffled[i + 1]
p1.opponent = p2
p2.opponent = p1
print(f"{p1.name} ❤️{p1.hp} will fight {p2.name} ❤️{p2.hp} in combat phase.")
if len(players_shuffled) % 2 == 1:
last_player = players_shuffled[-1]
last_player.opponent = None
print(f"{last_player.name} ❤️{last_player.hp} has no opponent this round.")
def play_round(players, round_number):
print(f"\n=== ROUND {round_number} ===")
alive_players = [p for p in players if p.hp > 0]
if len(alive_players) <= 1:
if len(alive_players) == 1:
print(f"🏆 GAME OVER: {alive_players[0].name} is the last player standing!")
else:
print("🏆 GAME OVER: No players remaining!")
return False
assign_opponents(alive_players)
for p in alive_players:
p.elixir += 4
turn_order = alive_players[:]
random.shuffle(turn_order)
passes_in_a_row = 0
total_players = len(alive_players)
while passes_in_a_row < total_players:
for player in turn_order:
if player.hp <= 0:
continue
acted = player.act(round_number)
if acted and player.has_space(round_number):
passes_in_a_row = 0
print(f"{player.name} acted and has {player.elixir}💧 left.")
else:
print(f"{player.name} passes.")
passes_in_a_row += 1
if passes_in_a_row >= total_players:
break
print(f"\n--- Round {round_number} Combat Phase ---")
matched_pairs = set()
for p in alive_players:
opponent = p.opponent
if opponent and opponent.hp > 0 and (p, opponent) not in matched_pairs and (opponent, p) not in matched_pairs:
combined = combine_grids(p, opponent)
p_colour = get_player_colour(p.name)
o_colour = get_player_colour(opponent.name)
print(f"\nMatchup: {p_colour}{p.name} ❤️{p.hp}\033[0m VS {o_colour}{opponent.name} ❤️{opponent.hp}\033[0m")
print_combined_grid(combined)
matched_pairs.add((p, opponent))
# Run combat simulation
combat_grids_and_arrows, winner, remaining_units = simulate_and_visualize_combat_live([p, opponent])
# Only count original units for end-of-round damage
original_units_remaining = [
u for u in (p.field + opponent.field)
if u.alive and u.card.name.lower() != "skeleton"
]
remaining_units_count = len(original_units_remaining)
# Apply damage based on original units only
if winner == p:
opponent.take_damage(remaining_units_count + 1)
elif winner == opponent:
p.take_damage(remaining_units_count + 1)
else: # Draw
print("🤝 No damage dealt due to draw!")
print(f"\n--- Round {round_number} Summary ---")
for player in alive_players:
if player.hp > 0:
player.display_zone(round_number)
return True
if __name__ == '__main__':
class DeckManager:
def __init__(self):
self.card_pool = [Card(name, cost) for name, cost in CARD_STATS.items() for _ in range(4)]
random.shuffle(self.card_pool)
def draw_hand(self, n=3):
hand = []
used_names = set()
i = 0
while len(hand) < n and i < len(self.card_pool):
card = self.card_pool[i]
if card.name not in used_names:
hand.append(card)
used_names.add(card.name)
self.card_pool.pop(i)
else:
i += 1
return hand
def return_cards(self, cards):
self.card_pool.extend(cards)
random.shuffle(self.card_pool)
def deal_hand(self, n=3):
return self.draw_hand(n)
deck = DeckManager()
players = [
Player("Greedy", deck, greedy_bot_logic),
Player("Efficient", deck, efficient_bot_logic),
Player("ComboSeeker", deck, combo_seeker_bot_logic),
Player("Random", deck, random_bot_logic)
]
for player in players:
player.give_starting_unit()
rn = 1
round_num = 1
while round_num <= 20: # Max 10 rounds
if not play_round(players, round_num):
break
round_num += 1
rn += 1
# Final standings
alive_players = [p for p in players if p.hp > 0]
alive_players.sort(key=lambda p: p.hp, reverse=True)
print(f"\n🏆 FINAL STANDINGS:")
for i, player in enumerate(alive_players, 1):
print(f"{i}. {player.name} - ❤️{player.hp} HP")
dead_players = [p for p in players if p.hp <= 0]
if dead_players:
print(f"\n💀 ELIMINATED:")
for player in dead_players:
print(f" {player.name} - ❤️{player.hp} HP")