| """ |
| Agent Action Space - Defines what actions agent can perform. |
| |
| This module provides: |
| - Action definitions (deploy, query, submit) |
| - Action validation |
| - Action execution through DroneSheet middleware |
| |
| Key principle: Agent modifies ONLY visible values. |
| Results are filtered to hide internal state. |
| """ |
|
|
| from dataclasses import dataclass, field |
| from typing import Dict, List, Any, Optional, Tuple |
| from enum import Enum |
| import random |
| import logging |
|
|
| logger = logging.getLogger(__name__) |
|
|
| from ...middleware.drone_sheet import DroneSheet |
| from ...middleware.drone_state import DroneState, JudgmentResult, EnvironmentEffects |
| from ..environment.scm_base import CausalSCM |
| from ..game.judge import judge_survival |
| from ..game.combat import full_simulation |
|
|
|
|
| class ActionType(Enum): |
| """Types of actions agent can perform.""" |
| DEPLOY = "deploy" |
| SUBMIT_FINAL = "submit_final" |
| GET_STATUS = "get_status" |
| GET_HISTORY = "get_history" |
|
|
|
|
| @dataclass |
| class AgentAction: |
| """Base class for agent actions.""" |
| action_type: ActionType |
|
|
|
|
| @dataclass |
| class DeployAction(AgentAction): |
| """Deploy drones with a design and optional equipment.""" |
| action_type: ActionType = ActionType.DEPLOY |
| design: Dict[str, int] = field(default_factory=dict) |
| equipment: Dict[str, str] = field(default_factory=dict) |
| count: int = 1 |
|
|
|
|
| @dataclass |
| class SubmitAction(AgentAction): |
| """Submit final design for Stage 2 evaluation.""" |
| action_type: ActionType = ActionType.SUBMIT_FINAL |
| design: Dict[str, int] = field(default_factory=dict) |
| equipment: Dict[str, str] = field(default_factory=dict) |
|
|
|
|
| @dataclass |
| class DeployResult: |
| """Result of a deployment action.""" |
| success: bool |
| error: Optional[str] = None |
|
|
| |
| deployed: int = 0 |
| survived: int = 0 |
| destroyed: int = 0 |
|
|
| |
| results: List[Dict[str, Any]] = field(default_factory=list) |
|
|
| |
| full_results: List[Dict[str, Any]] = field(default_factory=list) |
|
|
| |
| environment: Dict[str, float] = field(default_factory=dict) |
|
|
| |
| average_hit_count: float = 0.0 |
|
|
|
|
| @dataclass |
| class SubmitResult: |
| """Result of final submission.""" |
| success: bool |
| error: Optional[str] = None |
|
|
| |
| fleet_size: int = 0 |
| survived: int = 0 |
| survival_rate: float = 0.0 |
|
|
| |
| final_score: float = 0.0 |
| victory: bool = False |
| victory_threshold: float = 0.55 |
|
|
|
|
| class AgentActionSpace: |
| """ |
| Defines and executes agent actions. |
| |
| This class: |
| 1. Validates agent actions |
| 2. Executes actions through middleware |
| 3. Filters results for agent visibility |
| |
| Usage: |
| action_space = AgentActionSpace(scm, config) |
| result = action_space.execute(DeployAction(design={...}, count=5)) |
| """ |
|
|
| def __init__( |
| self, |
| scm: CausalSCM, |
| config: Optional[Dict[str, Any]] = None |
| ): |
| """ |
| Initialize AgentActionSpace. |
| |
| Args: |
| scm: SCM for environment effects |
| config: Experiment configuration |
| """ |
| self.scm = scm |
| self.config = config or {} |
|
|
| |
| self.total_drone_budget = self.config.get('resources', {}).get('total_drone_budget', 200) |
| self.stage2_fleet_size = self.config.get('resources', {}).get('stage2_fleet_size', 1000) |
| self.victory_threshold = self.config.get('resources', {}).get('victory_threshold', 0.55) |
| self.stage1_deployment_budget = self.config.get('resources', {}).get('stage1_deployment_budget', None) |
|
|
| |
| agent_visibility = self.config.get('agent_visibility', {}) |
| self.hide_failed_drones = agent_visibility.get('hide_failed_drones', False) |
|
|
| |
| self._drones_used = 0 |
| self._deployments_used = 0 |
| self._history: List[Dict[str, Any]] = [] |
| self._session_drone_counter = 0 |
|
|
| def execute(self, action: AgentAction) -> Any: |
| """ |
| Execute an agent action. |
| |
| Args: |
| action: AgentAction to execute |
| |
| Returns: |
| Action result (type depends on action) |
| """ |
| if action.action_type == ActionType.DEPLOY: |
| return self._execute_deploy(action) |
| elif action.action_type == ActionType.SUBMIT_FINAL: |
| return self._execute_submit(action) |
| elif action.action_type == ActionType.GET_STATUS: |
| return self._get_status() |
| elif action.action_type == ActionType.GET_HISTORY: |
| return self._get_history() |
| else: |
| raise ValueError(f"Unknown action type: {action.action_type}") |
|
|
| def _execute_deploy(self, action: DeployAction, is_test: bool = False) -> DeployResult: |
| """ |
| Execute drone deployment. |
| |
| Args: |
| action: DeployAction with design, equipment, and count |
| is_test: If True, skip budget checks (for admin test deploys) |
| |
| Returns: |
| DeployResult with filtered data |
| """ |
| |
| if not is_test: |
| |
| if self.stage1_deployment_budget is not None: |
| if self._deployments_used >= self.stage1_deployment_budget: |
| return DeployResult( |
| success=False, |
| error=f"Deployment budget exhausted. Used {self._deployments_used}/{self.stage1_deployment_budget} calls.", |
| ) |
|
|
| |
| if self._drones_used + action.count > self.total_drone_budget: |
| remaining = self.total_drone_budget - self._drones_used |
| return DeployResult( |
| success=False, |
| error=f"Insufficient budget. Remaining: {remaining}", |
| ) |
|
|
| |
| action_space_config = None |
| if action.equipment: |
| try: |
| from ..action_space import get_action_space |
| experiment_name = self.config.get('experiment', {}).get('name', 'antenna_trap') |
| action_space_config = get_action_space(experiment_name) |
| except Exception as e: |
| |
| logger.warning(f"Failed to load action space config: {e}") |
| pass |
|
|
| results = [] |
| survived = 0 |
| destroyed = 0 |
| total_hit_count = 0 |
| last_env = {} |
|
|
| for i in range(action.count): |
| |
| sheet = DroneSheet(self.config) |
|
|
| |
| success, error = sheet.set_def_design(action.design) |
| if not success: |
| return DeployResult(success=False, error=error) |
|
|
| |
| if action.equipment: |
| sheet.set_equipment(action.equipment) |
|
|
| |
| if action_space_config: |
| full_design = {**action.design, 'equipment': action.equipment} |
| equipment_effects = action_space_config.compute_effects(full_design) |
| sheet.apply_equipment_effects(equipment_effects) |
|
|
| |
| |
| env = self.scm.sample_environment(equipment=action.equipment) |
| self.scm.apply_effects(sheet, env) |
|
|
| |
| |
| state_after_scm = sheet.to_drone_state() |
| scm_decided_outcome = ( |
| state_after_scm.hp.get('engine', 100) <= 0 or |
| state_after_scm.hp.get('cockpit', 100) <= 0 |
| ) |
|
|
| if scm_decided_outcome: |
| |
| judgment = judge_survival(state_after_scm) |
| was_detected = False |
| combat_result = None |
| else: |
| |
| was_detected, combat_result = full_simulation(state_after_scm) |
|
|
| |
| if was_detected and combat_result: |
| sheet.apply_combat_damage( |
| combat_result.damage_by_component, |
| combat_result.hit_count, |
| combat_result.combat_log |
| ) |
| total_hit_count += combat_result.hit_count |
|
|
| |
| state = sheet.to_drone_state() |
|
|
| |
| judgment = judge_survival(state) |
| judgment = JudgmentResult( |
| status=judgment.status, |
| fail_reason=judgment.fail_reason, |
| final_hp=judgment.final_hp, |
| was_detected=was_detected, |
| hit_count=combat_result.hit_count if combat_result else 0, |
| ) |
|
|
| if judgment.survived: |
| survived += 1 |
| else: |
| destroyed += 1 |
|
|
| |
| filtered_result = sheet.filter_result_for_agent(judgment) |
|
|
| |
| if hasattr(self.scm, 'get_noise_std'): |
| noise_std = self.scm.get_noise_std(env) |
| filtered_result = sheet.add_observation_noise(filtered_result, noise_std) |
|
|
| results.append(filtered_result) |
|
|
| |
| last_env = env.visible.copy() |
|
|
| |
| self._session_drone_counter += 1 |
| history_record = { |
| 'id': f'SESSION-{self._session_drone_counter:03d}', |
| 'design': action.design.copy(), |
| 'status': judgment.status, |
| 'hit_count': judgment.hit_count, |
| 'environment': env.visible.copy(), |
| } |
| if action.equipment: |
| history_record['equipment'] = action.equipment.copy() |
| self._history.append(history_record) |
|
|
| |
| if not is_test: |
| self._drones_used += action.count |
| self._deployments_used += 1 |
|
|
| |
| if self.hide_failed_drones: |
| visible_results = [r for r in results if r.get('status') == 'RETURNED'] |
| else: |
| visible_results = results |
|
|
| return DeployResult( |
| success=True, |
| deployed=action.count, |
| survived=survived, |
| destroyed=destroyed, |
| results=visible_results, |
| full_results=results, |
| environment=last_env, |
| average_hit_count=total_hit_count / action.count if action.count > 0 else 0, |
| ) |
|
|
| def _execute_submit(self, action: SubmitAction) -> SubmitResult: |
| """ |
| Execute final submission (Stage 2). |
| |
| Args: |
| action: SubmitAction with final design and optional equipment |
| |
| Returns: |
| SubmitResult with victory status |
| """ |
| |
| |
| if hasattr(self.scm, 'set_evaluation_mode'): |
| self.scm.set_evaluation_mode(True) |
|
|
| survived = 0 |
|
|
| |
| action_space_config = None |
| if action.equipment: |
| try: |
| from ..action_space import get_action_space |
| experiment_name = self.config.get('experiment', {}).get('name', 'antenna_trap') |
| action_space_config = get_action_space(experiment_name) |
| except Exception: |
| pass |
|
|
| for i in range(self.stage2_fleet_size): |
| |
| sheet = DroneSheet(self.config) |
| success, error = sheet.set_def_design(action.design) |
| if not success: |
| return SubmitResult(success=False, error=error) |
|
|
| |
| if action.equipment: |
| sheet.set_equipment(action.equipment) |
| if action_space_config: |
| full_design = {**action.design, 'equipment': action.equipment} |
| equipment_effects = action_space_config.compute_effects(full_design) |
| sheet.apply_equipment_effects(equipment_effects) |
|
|
| |
| env = self.scm.sample_environment(equipment=action.equipment) |
| self.scm.apply_effects(sheet, env) |
|
|
| |
| state_after_scm = sheet.to_drone_state() |
| scm_decided_outcome = ( |
| state_after_scm.hp.get('engine', 100) <= 0 or |
| state_after_scm.hp.get('cockpit', 100) <= 0 |
| ) |
|
|
| if scm_decided_outcome: |
| |
| judgment = judge_survival(state_after_scm) |
| else: |
| |
| was_detected, combat_result = full_simulation(state_after_scm) |
|
|
| if was_detected and combat_result: |
| sheet.apply_combat_damage( |
| combat_result.damage_by_component, |
| combat_result.hit_count, |
| ) |
|
|
| state = sheet.to_drone_state() |
| judgment = judge_survival(state) |
|
|
| if judgment.survived: |
| survived += 1 |
|
|
| survival_rate = survived / self.stage2_fleet_size |
| total_def = sum(action.design.values()) |
| def_efficiency = 1.0 - min(1.0, total_def / 300) |
|
|
| |
| final_score = survival_rate * 0.7 + def_efficiency * 0.3 |
|
|
| return SubmitResult( |
| success=True, |
| fleet_size=self.stage2_fleet_size, |
| survived=survived, |
| survival_rate=survival_rate, |
| final_score=final_score, |
| victory=survival_rate >= self.victory_threshold, |
| victory_threshold=self.victory_threshold, |
| ) |
|
|
| def _get_status(self) -> Dict[str, Any]: |
| """Get current status (filtered for agent).""" |
| status = { |
| 'drones_remaining': self.total_drone_budget - self._drones_used, |
| 'drones_used': self._drones_used, |
| 'total_drones': self.total_drone_budget, |
| 'history_count': len(self._history), |
| 'victory_threshold': self.victory_threshold, |
| 'stage2_fleet_size': self.stage2_fleet_size, |
| } |
| |
| if self.stage1_deployment_budget is not None: |
| status['deployments_used'] = self._deployments_used |
| status['deployments_remaining'] = self.stage1_deployment_budget - self._deployments_used |
| status['stage1_deployment_budget'] = self.stage1_deployment_budget |
| return status |
|
|
| def _get_history(self, include_failed: bool = False) -> List[Dict[str, Any]]: |
| """ |
| Get flight history (filtered for agent). |
| |
| Args: |
| include_failed: If True, include all drones regardless of hide_failed_drones setting. |
| Used by admin endpoints to get full history. |
| |
| Returns: |
| List of flight records, optionally filtered to only RETURNED drones. |
| """ |
| if self.hide_failed_drones and not include_failed: |
| |
| return [r for r in self._history if r.get('status') == 'RETURNED'] |
| return self._history.copy() |
|
|
| def _get_full_history(self) -> List[Dict[str, Any]]: |
| """Get complete flight history (for admin, ignores hide_failed_drones).""" |
| return self._history.copy() |
|
|
| def reset(self) -> None: |
| """Reset action space state.""" |
| self._drones_used = 0 |
| self._deployments_used = 0 |
| self._history.clear() |
| self._session_drone_counter = 0 |
|
|
| def set_evaluation_mode(self, is_evaluation: bool = True) -> None: |
| """ |
| Set evaluation mode for the SCM (used for testing). |
| |
| This allows switching between Stage 1 (exploration) and Stage 2 (evaluation) |
| weather distributions without consuming submit budget. |
| |
| Args: |
| is_evaluation: If True, switch to Stage 2 (30% storm for weather_defense) |
| If False, use Stage 1 (70% storm for weather_defense) |
| """ |
| if hasattr(self.scm, 'set_evaluation_mode'): |
| self.scm.set_evaluation_mode(is_evaluation) |
|
|
| @property |
| def drones_remaining(self) -> int: |
| """Get remaining drone budget.""" |
| return self.total_drone_budget - self._drones_used |
|
|
| def generate_initial_observations(self, count: Optional[int] = None) -> List[Dict[str, Any]]: |
| """ |
| Generate initial observations for agent to analyze. |
| |
| These observations: |
| - Use the standard design (or biased design if configured) |
| - Have INIT prefix IDs |
| - Do NOT consume drone budget |
| - Are visible in the history |
| |
| Args: |
| count: Number of initial observations (default from config) |
| |
| Returns: |
| List of observation records |
| |
| Config options for history bias (in game.json resources): |
| - initial_observation_bias: "none" | "trap" | "optimal" |
| - "none": use standard design (default) |
| - "trap": use high antenna_def design (misleads agent to protect antenna) |
| - "optimal": use antenna_def=0 design (shows correct pattern) |
| - initial_observation_design: custom design dict (overrides bias) |
| """ |
| if count is None: |
| count = self.config.get('resources', {}).get('initial_observations', 50) |
|
|
| |
| raw_design = self.config.get('drone', {}).get('standard_design', { |
| 'engine_def': 20, |
| 'cockpit_def': 20, |
| 'wing_def': 15, |
| 'body_def': 15, |
| 'antenna_def': 10, |
| 'camera_def': 5, |
| 'gun_def': 5, |
| }) |
| |
| standard_design = {k: v for k, v in raw_design.items() if k.endswith('_def')} |
|
|
| |
| resources = self.config.get('resources', {}) |
| custom_design = resources.get('initial_observation_design') |
| bias_type = resources.get('initial_observation_bias', 'none') |
|
|
| |
| use_random_design = False |
| force_clear_weather = False |
| use_simpsons_paradox = False |
| use_anti_correlation = False |
|
|
| if custom_design: |
| |
| observation_design = {k: v for k, v in custom_design.items() if k.endswith('_def')} |
| elif bias_type == 'trap': |
| |
| observation_design = standard_design.copy() |
| observation_design['antenna_def'] = 30 |
| observation_design['camera_def'] = 30 |
| observation_design['gun_def'] = 30 |
| elif bias_type == 'optimal': |
| |
| observation_design = standard_design.copy() |
| observation_design['antenna_def'] = 0 |
| elif bias_type == 'random_designs': |
| |
| use_random_design = True |
| observation_design = standard_design |
| elif bias_type == 'clear_weather_only': |
| |
| observation_design = standard_design.copy() |
| force_clear_weather = True |
| elif bias_type == 'high_total_def': |
| |
| observation_design = { |
| 'engine_def': 35, 'cockpit_def': 35, |
| 'wing_def': 30, 'body_def': 30, |
| 'antenna_def': 25, 'camera_def': 20, 'gun_def': 20 |
| } |
| elif bias_type == 'critical_focus': |
| |
| observation_design = { |
| 'engine_def': 40, 'cockpit_def': 40, |
| 'wing_def': 30, 'body_def': 30, |
| 'antenna_def': 0, 'camera_def': 0, 'gun_def': 0 |
| } |
| elif bias_type == 'local_optima': |
| |
| observation_design = standard_design.copy() |
| observation_design['antenna_def'] = 5 |
| observation_design['camera_def'] = 10 |
| observation_design['gun_def'] = 10 |
| elif bias_type == 'simpsons_paradox': |
| |
| use_simpsons_paradox = True |
| observation_design = standard_design |
| elif bias_type == 'anti_correlation': |
| |
| use_anti_correlation = True |
| observation_design = standard_design.copy() |
| observation_design['antenna_def'] = 25 |
| |
| elif bias_type == 'deployment_zone_high_def': |
| |
| observation_design = { |
| 'engine_def': 30, 'cockpit_def': 30, |
| 'wing_def': 25, 'body_def': 25, |
| 'antenna_def': 20, 'camera_def': 15, 'gun_def': 15, |
| 'shield_def': 0 |
| } |
| elif bias_type == 'deployment_zone_local_optima': |
| |
| |
| |
| observation_design = { |
| 'engine_def': 35, 'cockpit_def': 30, |
| 'wing_def': 30, 'body_def': 25, |
| 'antenna_def': 15, 'camera_def': 10, 'gun_def': 10, |
| 'shield_def': 5 |
| } |
| elif bias_type == 'deployment_zone_simpsons_paradox': |
| |
| use_simpsons_paradox = True |
| observation_design = standard_design |
| elif bias_type == 'deployment_zone_high_emi_only': |
| |
| |
| observation_design = standard_design.copy() |
| observation_design['shield_def'] = 0 |
| else: |
| |
| observation_design = standard_design |
|
|
| |
| bias_equipment = None |
| use_deployment_zone_simpsons = False |
| use_deployment_zone_high_emi = False |
| if bias_type == 'deployment_zone_high_def': |
| bias_equipment = {'enhancement_module': 'radar_boost'} |
| elif bias_type == 'deployment_zone_local_optima': |
| bias_equipment = {'enhancement_module': 'signal_filter'} |
| elif bias_type == 'deployment_zone_simpsons_paradox': |
| use_deployment_zone_simpsons = True |
| use_simpsons_paradox = False |
| elif bias_type == 'deployment_zone_high_emi_only': |
| use_deployment_zone_high_emi = True |
| bias_equipment = {'enhancement_module': 'radar_boost'} |
|
|
| observations = [] |
|
|
| for i in range(count): |
| |
| sheet = DroneSheet(self.config) |
|
|
| |
| current_equipment = bias_equipment.copy() if bias_equipment else None |
|
|
| |
| if use_random_design: |
| |
| current_design = { |
| 'engine_def': random.randint(10, 40), |
| 'cockpit_def': random.randint(10, 40), |
| 'wing_def': random.randint(5, 35), |
| 'body_def': random.randint(5, 35), |
| 'antenna_def': random.randint(0, 30), |
| 'camera_def': random.randint(0, 25), |
| 'gun_def': random.randint(0, 25), |
| } |
| elif use_deployment_zone_simpsons: |
| |
| |
| if i % 3 == 0: |
| |
| current_design = standard_design.copy() |
| current_design['engine_def'] = 25 |
| current_design['shield_def'] = 0 |
| current_equipment = {'enhancement_module': 'radar_boost'} |
| else: |
| |
| current_design = standard_design.copy() |
| current_design['engine_def'] = 15 |
| current_design['shield_def'] = 0 |
| current_equipment = {'enhancement_module': 'thermal_shield'} |
| elif use_simpsons_paradox: |
| |
| if i % 3 == 0: |
| |
| current_design = standard_design.copy() |
| current_design['antenna_def'] = 25 |
| else: |
| |
| current_design = standard_design.copy() |
| current_design['antenna_def'] = 20 |
| elif use_anti_correlation: |
| |
| current_design = observation_design.copy() |
| current_design['antenna_def'] = random.randint(15, 35) |
| else: |
| current_design = observation_design.copy() |
|
|
| |
| success, error = sheet.set_def_design(current_design) |
| if not success: |
| continue |
|
|
| |
| if current_equipment: |
| sheet.set_equipment(current_equipment) |
| |
| try: |
| from ..action_space import get_action_space |
| experiment_name = self.config.get('experiment', {}).get('name', 'antenna_trap') |
| action_space_config = get_action_space(experiment_name) |
| if action_space_config: |
| full_design = {**current_design, 'equipment': current_equipment} |
| equipment_effects = action_space_config.compute_effects(full_design) |
| sheet.apply_equipment_effects(equipment_effects) |
| except Exception as e: |
| logger.warning(f"Failed to apply equipment effects for observation: {e}") |
|
|
| |
| |
| if force_clear_weather: |
| |
| env = self.scm.sample_environment(equipment=current_equipment) |
| |
| if hasattr(env, 'hidden') and 'weather_pattern' in env.hidden: |
| env.hidden['weather_pattern'] = random.uniform(0.0, 0.15) |
| |
| sheet = DroneSheet(self.config) |
| sheet.set_def_design(current_design) |
| elif use_deployment_zone_simpsons: |
| |
| env = self.scm.sample_environment(equipment=current_equipment) |
| if hasattr(env, 'latent') and 'mission_zone' in env.latent: |
| if i % 3 == 0: |
| |
| env.latent['mission_zone'] = 'epsilon' |
| env.latent['emi_level'] = 0.8 + random.uniform(-0.1, 0.1) |
| env.visible['altitude_band'] = 'high' |
| else: |
| |
| env.latent['mission_zone'] = 'delta' |
| env.latent['emi_level'] = 0.1 + random.uniform(-0.05, 0.05) |
| env.visible['altitude_band'] = 'low' |
| |
| sheet = DroneSheet(self.config) |
| sheet.set_def_design(current_design) |
| |
| if current_equipment: |
| sheet.set_equipment(current_equipment) |
| try: |
| from ..action_space import get_action_space |
| experiment_name = self.config.get('experiment', {}).get('name', 'antenna_trap') |
| action_space_config = get_action_space(experiment_name) |
| if action_space_config: |
| full_design = {**current_design, 'equipment': current_equipment} |
| equipment_effects = action_space_config.compute_effects(full_design) |
| sheet.apply_equipment_effects(equipment_effects) |
| except Exception: |
| pass |
| elif use_deployment_zone_high_emi: |
| |
| env = self.scm.sample_environment(equipment=current_equipment) |
| if hasattr(env, 'latent') and 'mission_zone' in env.latent: |
| |
| high_emi_zone = random.choice(['epsilon', 'zeta']) |
| env.latent['mission_zone'] = high_emi_zone |
| env.latent['emi_level'] = 0.7 + random.uniform(0, 0.2) |
| |
| env.visible['altitude_band'] = random.choice(['low', 'medium', 'high']) |
| |
| sheet = DroneSheet(self.config) |
| sheet.set_def_design(current_design) |
| |
| if current_equipment: |
| sheet.set_equipment(current_equipment) |
| try: |
| from ..action_space import get_action_space |
| experiment_name = self.config.get('experiment', {}).get('name', 'antenna_trap') |
| action_space_config = get_action_space(experiment_name) |
| if action_space_config: |
| full_design = {**current_design, 'equipment': current_equipment} |
| equipment_effects = action_space_config.compute_effects(full_design) |
| sheet.apply_equipment_effects(equipment_effects) |
| except Exception: |
| pass |
| else: |
| env = self.scm.sample_environment(equipment=current_equipment) |
|
|
| self.scm.apply_effects(sheet, env) |
|
|
| |
| state_after_scm = sheet.to_drone_state() |
| scm_decided_outcome = ( |
| state_after_scm.hp.get('engine', 100) <= 0 or |
| state_after_scm.hp.get('cockpit', 100) <= 0 |
| ) |
|
|
| if scm_decided_outcome: |
| |
| judgment = judge_survival(state_after_scm) |
| was_detected = False |
| hit_count = 0 |
| else: |
| |
| was_detected, combat_result = full_simulation(state_after_scm) |
|
|
| |
| if was_detected and combat_result: |
| sheet.apply_combat_damage( |
| combat_result.damage_by_component, |
| combat_result.hit_count, |
| combat_result.combat_log if hasattr(combat_result, 'combat_log') else [] |
| ) |
|
|
| |
| state = sheet.to_drone_state() |
|
|
| |
| judgment = judge_survival(state) |
| hit_count = combat_result.hit_count if combat_result else 0 |
|
|
| |
| if use_anti_correlation and judgment.status != 'RETURNED': |
| |
| continue |
|
|
| |
| observation = { |
| 'id': f'INIT-{i+1:03d}', |
| 'design': current_design.copy(), |
| 'status': judgment.status, |
| 'hit_count': hit_count, |
| 'was_detected': was_detected, |
| 'environment': env.visible.copy(), |
| } |
| |
| if current_equipment: |
| observation['equipment'] = current_equipment.copy() |
|
|
| observations.append(observation) |
|
|
| |
| self._history.append(observation) |
|
|
| return observations |
|
|