Теперь он хотя бы запускается

This commit is contained in:
2025-11-05 02:50:11 +03:00
parent c1029bdd4f
commit dbb3b4b478
2 changed files with 150 additions and 294 deletions

View File

@@ -1,202 +1,102 @@
import random import random
from typing import List, Dict from typing import List, Dict
class AIDecision:
def __init__(self, difficulty: str = "medium"): class AIDecision:
self.difficulty = difficulty def __init__(self, difficulty: str = "medium"):
self.last_decision_time = 0 self.difficulty = difficulty
self.decision_cooldown = self._get_decision_cooldown() self.last_decision_time = 0
self.decision_cooldown = self._get_decision_cooldown()
def decide(self, snapshot: Dict, dt: float) -> List[Dict]:
self.last_decision_time += dt def decide(self, game_state, dt: float) -> List[Dict]:
if self.last_decision_time < self.decision_cooldown: self.last_decision_time += dt
return [] if self.last_decision_time < self.decision_cooldown:
return []
commands = []
commands.extend(self.plan_expand(snapshot)) commands = []
commands.extend(self.plan_attack(snapshot)) commands.extend(self.plan_expand(game_state))
commands.extend(self.plan_build(snapshot)) commands.extend(self.plan_attack(game_state))
commands.extend(self.plan_build(game_state))
self.last_decision_time = 0
return commands self.last_decision_time = 0
return commands
def plan_expand(self, snapshot: Dict) -> List[Dict]:
commands = [] def plan_expand(self, game_state) -> List[Dict]:
resources = snapshot.get('resources', {}) commands = []
minerals = resources.get('minerals', 0) resources = getattr(game_state, 'resources', {})
minerals = getattr(resources, 'minerals', 0) if hasattr(resources, 'minerals') else 0
if minerals >= 400 and random.random() < 0.3:
commands.append({ if minerals >= 400 and random.random() < 0.3:
'type': 'create_entity', commands.append({
'entity_type': 'supply_depot', 'type': 'create_entity',
'position': self._find_expansion_position(snapshot) 'entity_type': 'supply_depot',
}) 'position': self._find_expansion_position(game_state)
})
return commands
return commands
def plan_attack(self, snapshot: Dict) -> List[Dict]:
commands = [] def plan_attack(self, game_state) -> List[Dict]:
entities = snapshot.get('entities', []) commands = []
ai_units = [e for e in entities if e.get('owner') == 'ai'] entities = getattr(game_state, 'entities', [])
player_units = [e for e in entities if e.get('owner') == 'player'] ai_units = [e for e in entities if getattr(e, 'owner', None) == 'ai']
player_units = [e for e in entities if getattr(e, 'owner', None) == 'player']
if len(ai_units) >= 5 and player_units and random.random() < 0.4:
target_unit = random.choice(player_units) if len(ai_units) >= 5 and player_units and random.random() < 0.4:
for unit in ai_units: target_unit = random.choice(player_units)
if unit.get('type') == 'unit' and unit.get('state') == 'idle': for unit in ai_units:
commands.append({ if getattr(unit, 'type', None) == 'unit' and getattr(unit, 'state', None) == 'idle':
'type': 'attack', commands.append({
'entity_id': unit['id'], 'type': 'attack',
'target_id': target_unit['id'] 'entity_id': getattr(unit, 'id', None),
})
return commands
def plan_build(self, snapshot: Dict) -> List[Dict]:
commands = []
resources = snapshot.get('resources', {})
minerals = resources.get('minerals', 0)
entities = snapshot.get('entities', [])
ai_units = [e for e in entities if e.get('owner') == 'ai']
if minerals >= 150 and len(ai_units) < 8 and random.random() < 0.5:
commands.append({
'type': 'create_entity',
'entity_type': 'worker',
'position': self._find_build_position(snapshot)
})
if minerals >= 300 and random.random() < 0.2:
commands.append({
'type': 'create_entity',
'entity_type': 'barracks',
'position': self._find_build_position(snapshot)
})
return commands
def _find_expansion_position(self, snapshot: Dict) -> tuple:
base_x = snapshot.get('ai_base_x', 100)
base_y = snapshot.get('ai_base_y', 100)
offset_x = random.randint(-200, 200)
offset_y = random.randint(-200, 200)
return (base_x + offset_x, base_y + offset_y)
def _find_build_position(self, snapshot: Dict) -> tuple:
base_x = snapshot.get('ai_base_x', 100)
base_y = snapshot.get('ai_base_y', 100)
offset_x = random.randint(-50, 50)
offset_y = random.randint(-50, 50)
return (base_x + offset_x, base_y + offset_y)
def _get_decision_cooldown(self) -> float:
if self.difficulty == "easy":
return 3.0
elif self.difficulty == "medium":
return 2.0
elif self.difficulty == "hard":
return 1.0
else:
return 2.0

View File

@@ -1,92 +1,48 @@
from typing import List, Dict, Tuple from typing import List, Dict, Tuple
import pygame
class InputCommands:
def __init__(self):
pass class InputCommands:
def __init__(self):
def generate_commands(self, events: List, snapshot: Dict) -> List[Dict]: pass
commands = []
camera = snapshot.get('camera', {}) def generate_commands(self, events: List, game_state) -> List[Dict]:
commands = []
for event in events: camera = getattr(game_state, 'camera', {})
command = self._process_event(event, camera)
if command: for event in events:
commands.append(command) command = self._process_event(event, camera)
if command:
return commands commands.append(command)
def _process_event(self, event, camera: Dict) -> Dict: return commands
if event.type == event.MOUSEBUTTONDOWN:
if event.button == 1: # Left click def _process_event(self, event, camera: Dict) -> Dict:
world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera) if event.type == pygame.MOUSEBUTTONDOWN:
return {'type': 'select', 'world_pos': (world_x, world_y)} if event.button == 1: # Left click
elif event.button == 3: # Right click world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera)
world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera)
return {'type': 'move', 'world_pos': (world_x, world_y)}
elif event.type == event.KEYDOWN:
if event.key == event.K_b:
return {'type': 'build', 'building_type': 'barracks'}
elif event.key == event.K_s:
return {'type': 'build', 'building_type': 'supply_depot'}
elif event.key == event.K_g:
return {'type': 'gather'}
return None
def screen_to_world(self, x: int, y: int, camera: Dict) -> Tuple[float, float]:
camera_x = camera.get('x', 0)
camera_y = camera.get('y', 0)
zoom = camera.get('zoom', 1.0)
world_x = (x / zoom) + camera_x
world_y = (y / zoom) + camera_y
return world_x, world_y