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