Загрузить файлы в «/»
This commit is contained in:
202
ai_decision.py
Normal file
202
ai_decision.py
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
|
||||
import random
|
||||
|
||||
from typing import List, Dict
|
||||
|
||||
|
||||
|
||||
class AIDecision:
|
||||
|
||||
def __init__(self, difficulty: str = "medium"):
|
||||
|
||||
self.difficulty = difficulty
|
||||
|
||||
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
|
||||
|
||||
if self.last_decision_time < self.decision_cooldown:
|
||||
|
||||
return []
|
||||
|
||||
|
||||
|
||||
commands = []
|
||||
|
||||
commands.extend(self.plan_expand(snapshot))
|
||||
|
||||
commands.extend(self.plan_attack(snapshot))
|
||||
|
||||
commands.extend(self.plan_build(snapshot))
|
||||
|
||||
|
||||
|
||||
self.last_decision_time = 0
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
|
||||
def plan_expand(self, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
commands = []
|
||||
|
||||
resources = snapshot.get('resources', {})
|
||||
|
||||
minerals = resources.get('minerals', 0)
|
||||
|
||||
|
||||
|
||||
if minerals >= 400 and random.random() < 0.3:
|
||||
|
||||
commands.append({
|
||||
|
||||
'type': 'create_entity',
|
||||
|
||||
'entity_type': 'supply_depot',
|
||||
|
||||
'position': self._find_expansion_position(snapshot)
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
return commands
|
||||
|
||||
|
||||
|
||||
def plan_attack(self, snapshot: Dict) -> List[Dict]:
|
||||
|
||||
commands = []
|
||||
|
||||
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']
|
||||
|
||||
|
||||
|
||||
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 unit.get('type') == 'unit' and unit.get('state') == 'idle':
|
||||
|
||||
commands.append({
|
||||
|
||||
'type': 'attack',
|
||||
|
||||
'entity_id': unit['id'],
|
||||
|
||||
'target_id': target_unit['id']
|
||||
|
||||
})
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user