Теперь он хотя бы запускается
This commit is contained in:
156
ai_decision.py
156
ai_decision.py
@@ -1,202 +1,102 @@
|
||||
|
||||
|
||||
|
||||
|
||||
import random
|
||||
|
||||
from typing import List, Dict
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class AIDecision:
|
||||
|
||||
|
||||
def decide(self, snapshot: Dict, dt: float) -> List[Dict]:
|
||||
|
||||
|
||||
def __init__(self, difficulty: str = "medium"):
|
||||
|
||||
|
||||
|
||||
self.difficulty = difficulty
|
||||
|
||||
|
||||
|
||||
self.last_decision_time = 0
|
||||
|
||||
commands.extend(self.plan_expand(snapshot))
|
||||
|
||||
commands.extend(self.plan_attack(snapshot))
|
||||
|
||||
commands.extend(self.plan_build(snapshot))
|
||||
|
||||
|
||||
|
||||
self.decision_cooldown = self._get_decision_cooldown()
|
||||
|
||||
|
||||
|
||||
|
||||
def decide(self, game_state, dt: float) -> List[Dict]:
|
||||
|
||||
|
||||
|
||||
def plan_expand(self, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
self.last_decision_time += dt
|
||||
|
||||
|
||||
resources = snapshot.get('resources', {})
|
||||
|
||||
minerals = resources.get('minerals', 0)
|
||||
|
||||
|
||||
if self.last_decision_time < self.decision_cooldown:
|
||||
|
||||
return []
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
commands = []
|
||||
|
||||
'position': self._find_expansion_position(snapshot)
|
||||
|
||||
|
||||
commands.extend(self.plan_expand(game_state))
|
||||
|
||||
|
||||
|
||||
commands.extend(self.plan_attack(game_state))
|
||||
|
||||
|
||||
|
||||
def plan_attack(self, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
commands.extend(self.plan_build(game_state))
|
||||
|
||||
|
||||
entities = snapshot.get('entities', [])
|
||||
|
||||
ai_units = [e for e in entities if e.get('owner') == 'ai']
|
||||
|
||||
player_units = [e for e in entities if e.get('owner') == 'player']
|
||||
|
||||
|
||||
|
||||
|
||||
self.last_decision_time = 0
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if unit.get('type') == 'unit' and unit.get('state') == 'idle':
|
||||
|
||||
|
||||
def plan_expand(self, game_state) -> List[Dict]:
|
||||
|
||||
|
||||
|
||||
'entity_id': unit['id'],
|
||||
|
||||
'target_id': target_unit['id']
|
||||
|
||||
commands = []
|
||||
|
||||
resources = getattr(game_state, 'resources', {})
|
||||
|
||||
|
||||
|
||||
minerals = getattr(resources, 'minerals', 0) if hasattr(resources, 'minerals') else 0
|
||||
|
||||
|
||||
|
||||
def plan_build(self, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
|
||||
|
||||
|
||||
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 >= 400 and random.random() < 0.3:
|
||||
|
||||
commands.append({
|
||||
|
||||
'type': 'create_entity',
|
||||
|
||||
|
||||
'entity_type': 'supply_depot',
|
||||
|
||||
|
||||
|
||||
'position': self._find_expansion_position(game_state)
|
||||
|
||||
'position': self._find_build_position(snapshot)
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
|
||||
'position': self._find_build_position(snapshot)
|
||||
|
||||
|
||||
|
||||
def plan_attack(self, game_state) -> List[Dict]:
|
||||
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
entities = getattr(game_state, 'entities', [])
|
||||
|
||||
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']
|
||||
|
||||
|
||||
|
||||
def _find_build_position(self, snapshot: Dict) -> tuple:
|
||||
|
||||
base_x = snapshot.get('ai_base_x', 100)
|
||||
|
||||
base_y = snapshot.get('ai_base_y', 100)
|
||||
|
||||
|
||||
|
||||
if len(ai_units) >= 5 and player_units and random.random() < 0.4:
|
||||
|
||||
|
||||
target_unit = random.choice(player_units)
|
||||
|
||||
|
||||
for unit in ai_units:
|
||||
|
||||
|
||||
|
||||
|
||||
if getattr(unit, 'type', None) == 'unit' and getattr(unit, 'state', None) == 'idle':
|
||||
|
||||
|
||||
|
||||
commands.append({
|
||||
|
||||
|
||||
|
||||
'type': 'attack',
|
||||
|
||||
|
||||
|
||||
'entity_id': getattr(unit, 'id', None),
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,92 +1,48 @@
|
||||
|
||||
|
||||
|
||||
|
||||
from typing import List, Dict, Tuple
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def generate_commands(self, events: List, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
|
||||
class InputCommands:
|
||||
|
||||
camera = snapshot.get('camera', {})
|
||||
|
||||
|
||||
|
||||
def __init__(self):
|
||||
|
||||
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def generate_commands(self, events: List, game_state) -> List[Dict]:
|
||||
|
||||
|
||||
|
||||
commands = []
|
||||
|
||||
if event.type == event.MOUSEBUTTONDOWN:
|
||||
|
||||
|
||||
camera = getattr(game_state, 'camera', {})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for event in events:
|
||||
|
||||
|
||||
command = self._process_event(event, camera)
|
||||
|
||||
|
||||
elif event.type == event.KEYDOWN:
|
||||
|
||||
if event.key == event.K_b:
|
||||
|
||||
|
||||
if command:
|
||||
|
||||
|
||||
elif event.key == event.K_s:
|
||||
|
||||
commands.append(command)
|
||||
|
||||
|
||||
elif event.key == event.K_g:
|
||||
|
||||
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def _process_event(self, event, camera: Dict) -> Dict:
|
||||
|
||||
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
|
||||
|
||||
|
||||
if event.button == 1: # Left click
|
||||
|
||||
|
||||
world_x, world_y = self.screen_to_world(event.pos[0], event.pos[1], camera)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user