| """ |
| DroneSheet - Central middleware for drone data management. |
| |
| This is the single source of truth for all drone attributes: |
| - Component HP values (hidden from agent) |
| - Component DEF values (agent visible) |
| - Component ATK values (agent visible) |
| - Derived values (computed, visibility controlled) |
| |
| Data Flow: |
| 1. Agent sets DEF values via set_def_design() |
| 2. SCM writes environment effects via apply_environment_effects() |
| 3. Combat system writes damage via apply_combat_damage() |
| 4. Game reads state via to_drone_state() |
| 5. Results filtered for agent via filter_for_agent() |
| """ |
|
|
| from dataclasses import dataclass, field |
| from typing import Dict, List, Any, Optional, Tuple |
| import math |
| import random |
| import numpy as np |
| import copy |
| import logging |
|
|
| from .drone_state import DroneState, EnvironmentEffects, JudgmentResult |
| from .visibility import VisibilityConfig, Visibility |
|
|
| logger = logging.getLogger(__name__) |
|
|
|
|
| |
| DEFAULT_COMPONENTS = { |
| "engine": {"hp": 100, "default_def": 20, "is_critical": True}, |
| "cockpit": {"hp": 100, "default_def": 20, "is_critical": True}, |
| "wing": {"hp": 80, "default_def": 15, "is_critical": True}, |
| "body": {"hp": 80, "default_def": 15, "is_critical": True}, |
| "antenna": {"hp": 50, "default_def": 10, "is_critical": False}, |
| "camera": {"hp": 40, "default_def": 5, "is_critical": False, "has_eft": True}, |
| "gun": {"hp": 40, "default_def": 5, "is_critical": False, "has_atk": True, "default_atk": 20}, |
| } |
|
|
| |
| CRITICAL_COMPONENTS = ['engine', 'cockpit', 'wing', 'body'] |
|
|
|
|
| @dataclass |
| class AgilityConfig: |
| """Configuration for agility calculation from DEF.""" |
| base_agility: float = 1.0 |
| linear_coefficient: float = 0.002 |
| exponential_decay_scale: float = 200.0 |
| min_agility: float = 0.1 |
| max_agility: float = 1.0 |
|
|
|
|
| @dataclass |
| class ComponentConfig: |
| """Configuration for a single component.""" |
| name: str |
| hp: int |
| default_def: int |
| is_critical: bool = False |
| has_atk: bool = False |
| has_eft: bool = False |
| default_atk: int = 0 |
|
|
|
|
| class DroneSheet: |
| """ |
| Drone data middleware. |
| |
| This class is the central hub that: |
| 1. Stores all drone attributes (HP, DEF, ATK, derived values) |
| 2. Controls what Agent can see vs what is hidden |
| 3. Computes derived values (Agility, Signal Strength, Stealth) |
| 4. Mediates between Agent actions and Game simulation |
| """ |
|
|
| def __init__( |
| self, |
| config: Optional[Dict[str, Any]] = None, |
| visibility_config: Optional[VisibilityConfig] = None |
| ): |
| """ |
| Initialize DroneSheet with configuration. |
| |
| Args: |
| config: Experiment configuration dict |
| visibility_config: Optional visibility configuration |
| """ |
| config = config or {} |
|
|
| |
| self._components: Dict[str, ComponentConfig] = {} |
| self._load_components(config) |
|
|
| |
| |
| self._hp: Dict[str, int] = {} |
|
|
| |
| self._def: Dict[str, int] = {} |
|
|
| |
| self._atk: Dict[str, int] = {} |
|
|
| |
| self._eft: Dict[str, float] = {} |
|
|
| |
| self._total_def: int = 0 |
| self._total_hp: int = 0 |
| self._agility: float = 1.0 |
| self._signal_strength: float = 1.0 |
| self._stealth_rating: float = 0.0 |
| self._firepower: float = 0.0 |
|
|
| |
| self._env_effects: Optional[EnvironmentEffects] = None |
| self._env_damage_applied: bool = False |
|
|
| |
| self._combat_damage: Dict[str, int] = {} |
| self._hit_count: int = 0 |
| self._combat_log: List[str] = [] |
|
|
| |
| self._equipment: Dict[str, str] = {} |
| self._equipment_effects: Optional['EquipmentEffects'] = None |
|
|
| |
| self._agility_config = AgilityConfig( |
| **config.get('agility_system', {}) |
| ) |
| self._visibility = visibility_config or VisibilityConfig.from_config(config) |
| self._def_reduction_factor = config.get('def_reduction_factor', 0.3) |
|
|
| |
| self._initialize_defaults() |
|
|
| def _load_components(self, config: Dict[str, Any]) -> None: |
| """Load component definitions from config or use defaults.""" |
| components_config = config.get('drone', {}).get('components', DEFAULT_COMPONENTS) |
|
|
| for name, comp_cfg in components_config.items(): |
| if isinstance(comp_cfg, dict): |
| self._components[name] = ComponentConfig( |
| name=name, |
| hp=comp_cfg.get('hp', 50), |
| default_def=comp_cfg.get('default_def', 10), |
| is_critical=comp_cfg.get('is_critical', False), |
| has_atk=comp_cfg.get('has_atk', False), |
| has_eft=comp_cfg.get('has_eft', False), |
| default_atk=comp_cfg.get('default_atk', 0), |
| ) |
|
|
| def _initialize_defaults(self) -> None: |
| """Initialize all values to defaults.""" |
| for name, comp in self._components.items(): |
| self._hp[name] = comp.hp |
| self._def[name] = comp.default_def |
| if comp.has_atk: |
| self._atk[name] = comp.default_atk |
| if comp.has_eft: |
| self._eft[name] = 1.0 |
|
|
| self._recompute_derived() |
|
|
| def reset(self) -> None: |
| """Reset sheet to initial state.""" |
| self._initialize_defaults() |
| self._env_effects = None |
| self._env_damage_applied = False |
| self._combat_damage.clear() |
| self._hit_count = 0 |
| self._combat_log.clear() |
| self._equipment.clear() |
| self._equipment_effects = None |
|
|
| |
|
|
| def set_def_design(self, design: Dict[str, int]) -> Tuple[bool, Optional[str]]: |
| """ |
| Agent sets DEF values. |
| |
| This is the PRIMARY way agent modifies drone stats. |
| |
| Args: |
| design: Dict mapping component_def to value |
| e.g., {"engine_def": 25, "cockpit_def": 20, ...} |
| |
| Returns: |
| (success, error_message) |
| """ |
| |
| is_valid, error = self._validate_def_design(design) |
| if not is_valid: |
| return False, error |
|
|
| |
| self._initialize_defaults() |
|
|
| |
| for key, value in design.items(): |
| |
| component = key.replace('_def', '') if key.endswith('_def') else key |
| if component in self._components: |
| self._def[component] = value |
|
|
| |
| self._recompute_derived() |
|
|
| return True, None |
|
|
| def _validate_def_design(self, design: Dict[str, int]) -> Tuple[bool, Optional[str]]: |
| """Validate a DEF design from agent.""" |
| if not design: |
| return False, "Design cannot be empty" |
|
|
| for key, value in design.items(): |
| component = key.replace('_def', '') if key.endswith('_def') else key |
|
|
| |
| if component not in self._components: |
| return False, f"Unknown component: {component}" |
|
|
| |
| if not isinstance(value, (int, float)) or value < 0: |
| return False, f"Invalid DEF value for {component}: {value}" |
|
|
| return True, None |
|
|
| def set_equipment(self, equipment: Dict[str, str]) -> Tuple[bool, Optional[str]]: |
| """ |
| Agent sets discrete equipment choices. |
| |
| Args: |
| equipment: Dict mapping slot to choice_id |
| e.g., {"coating": "stealth", "antenna_mode": "passive"} |
| |
| Returns: |
| (success, error_message) |
| """ |
| if equipment: |
| self._equipment = equipment.copy() |
| logger.debug(f"Equipment set: {equipment}") |
| return True, None |
|
|
| def apply_equipment_effects(self, effects: 'EquipmentEffects') -> None: |
| """ |
| Apply equipment effects to drone attributes. |
| |
| This is called after action space validation computes the combined |
| effects from all discrete choices. |
| |
| Args: |
| effects: EquipmentEffects computed from action space config |
| """ |
| from ..modules.action_space.schema import EquipmentEffects as EE |
|
|
| self._equipment_effects = effects |
|
|
| |
| |
|
|
| |
| if hasattr(effects, 'agility_modifier') and effects.agility_modifier != 0: |
| self._agility = max( |
| self._agility_config.min_agility, |
| min(self._agility_config.max_agility, |
| self._agility + effects.agility_modifier) |
| ) |
|
|
| |
| if hasattr(effects, 'def_multiplier') and effects.def_multiplier != 1.0: |
| for comp in self._def: |
| self._def[comp] = int(self._def[comp] * effects.def_multiplier) |
| self._recompute_derived() |
|
|
| |
| if hasattr(effects, 'hp_modifiers') and effects.hp_modifiers: |
| for comp, modifier in effects.hp_modifiers.items(): |
| if comp in self._hp: |
| self._hp[comp] = max(0, self._hp[comp] + modifier) |
| self._recompute_derived() |
|
|
| logger.debug(f"Equipment effects applied: detection={getattr(effects, 'detection_modifier', 0)}, " |
| f"agility={getattr(effects, 'agility_modifier', 0)}") |
|
|
| def get_equipment_effects(self) -> Optional['EquipmentEffects']: |
| """Get the current equipment effects (for SCM to use).""" |
| return self._equipment_effects |
|
|
| def get_equipment(self) -> Dict[str, str]: |
| """Get current equipment choices.""" |
| return self._equipment.copy() |
|
|
| def get_agent_view(self) -> Dict[str, Any]: |
| """ |
| Return data visible to agent. |
| |
| Filters out hidden fields like HP and agility. |
| """ |
| view = { |
| |
| 'def_values': {f"{k}_def": v for k, v in self._def.items()}, |
| 'total_def': self._total_def, |
|
|
| |
| 'atk_values': {f"{k}_atk": v for k, v in self._atk.items()}, |
|
|
| |
| 'equipment': self._equipment.copy() if self._equipment else {}, |
|
|
| |
| 'components': { |
| name: { |
| 'def': self._def.get(name, 0), |
| 'atk': self._atk.get(name) if comp.has_atk else None, |
| 'is_critical': comp.is_critical, |
| } |
| for name, comp in self._components.items() |
| }, |
| } |
|
|
| |
| return self._visibility.filter_for_agent(view) |
|
|
| |
|
|
| def apply_environment_effects(self, effects: EnvironmentEffects) -> None: |
| """ |
| SCM writes environment effects into middleware. |
| |
| This is the PRIMARY way SCM affects the simulation. |
| |
| Args: |
| effects: EnvironmentEffects computed by SCM |
| """ |
| self._env_effects = effects |
|
|
| |
| if not self._env_damage_applied: |
| for component, raw_damage in effects.component_damage.items(): |
| if component in self._hp: |
| |
| |
| if component == 'antenna': |
| actual_damage = raw_damage |
| else: |
| |
| def_value = self._def.get(component, 0) |
| actual_damage = self._calculate_damage(raw_damage, def_value) |
| self._hp[component] = max(0, self._hp[component] - actual_damage) |
|
|
| self._env_damage_applied = True |
|
|
| |
| self._recompute_derived() |
|
|
| def _calculate_damage(self, raw_damage: int, def_value: int) -> int: |
| """ |
| Calculate actual damage after DEF reduction. |
| |
| DEF reduction is significant to make armor investment worthwhile. |
| This rewards proper armor allocation over simply minimizing weight. |
| """ |
| |
| reduction = int(def_value * self._def_reduction_factor) |
| |
| max_reduction = int(raw_damage * 0.65) |
| reduction = min(reduction, max_reduction) |
| return max(1, raw_damage - reduction) |
|
|
| |
|
|
| def apply_combat_damage( |
| self, |
| damage_by_component: Dict[str, int], |
| hit_count: int = 0, |
| combat_log: Optional[List[str]] = None |
| ) -> None: |
| """ |
| Apply combat damage to drone. |
| |
| Args: |
| damage_by_component: Raw damage per component |
| hit_count: Number of hits taken |
| combat_log: Combat event log |
| """ |
| for component, raw_damage in damage_by_component.items(): |
| if component in self._hp: |
| def_value = self._def.get(component, 0) |
| actual_damage = self._calculate_damage(raw_damage, def_value) |
| self._hp[component] = max(0, self._hp[component] - actual_damage) |
| self._combat_damage[component] = self._combat_damage.get(component, 0) + actual_damage |
|
|
| self._hit_count += hit_count |
| if combat_log: |
| self._combat_log.extend(combat_log) |
|
|
| |
| self._recompute_derived() |
|
|
| |
|
|
| def to_drone_state(self) -> DroneState: |
| """ |
| Generate standardized DroneState for Game module. |
| |
| This is what Game.judge_survival() receives. |
| """ |
| |
| effects = self._env_effects or EnvironmentEffects() |
|
|
| |
| detection_prob = self._compute_detection_probability(effects) |
|
|
| |
| combat_rounds = self._compute_combat_rounds(effects) |
|
|
| return DroneState( |
| |
| hp=self._hp.copy(), |
|
|
| |
| def_values=self._def.copy(), |
|
|
| |
| agility=self._agility, |
| detection_probability=detection_prob, |
| combat_rounds=combat_rounds, |
|
|
| |
| antenna_emitting=self._hp.get('antenna', 0) > 0, |
| antenna_hp=self._hp.get('antenna', 0), |
|
|
| |
| combat_damage_modifier=effects.combat_damage_modifier, |
| combat_accuracy_modifier=effects.combat_accuracy_modifier, |
|
|
| |
| camera_power=self._eft.get('camera', 1.0) * effects.camera_effectiveness, |
| gun_power=self._eft.get('gun', 1.0) * effects.gun_effectiveness, |
|
|
| |
| weather_pattern=effects.weather_pattern, |
| environment_visible=self._get_visible_environment(effects), |
| ) |
|
|
| def _compute_detection_probability(self, effects: EnvironmentEffects) -> float: |
| """ |
| Compute final detection probability. |
| |
| The SCM is responsible for computing the detection probability based on: |
| - Weather conditions |
| - Antenna state (emitting signal or not) |
| - Other environmental factors |
| |
| DroneSheet uses the detection_modifier from SCM as the base probability, |
| then applies equipment effects if present. |
| |
| Design intent: |
| - SCM computes detection_modifier considering antenna HP |
| - Equipment effects can modify detection (e.g., stealth coating) |
| - DroneSheet clamps and returns final value |
| """ |
| |
| prob = effects.detection_modifier |
|
|
| |
| if self._equipment_effects: |
| equip_detection_mod = getattr(self._equipment_effects, 'detection_modifier', 0) |
| prob += equip_detection_mod |
|
|
| return min(0.95, max(0.01, prob)) |
|
|
| def _compute_combat_rounds(self, effects: EnvironmentEffects) -> int: |
| """ |
| Compute number of combat rounds. |
| |
| Combat rounds are fixed, only modified by environment. |
| Agility does NOT affect combat rounds (only affects hit chance). |
| """ |
| base_rounds = 10 |
|
|
| |
| rounds = base_rounds * effects.combat_rounds_modifier |
|
|
| return max(1, int(rounds)) |
|
|
| def _get_visible_environment(self, effects: EnvironmentEffects) -> Dict[str, float]: |
| """Get environment variables visible to agent.""" |
| visible_vars = ['wind_speed', 'humidity', 'temperature', 'uv_index'] |
| return {k: v for k, v in effects.raw_environment.items() if k in visible_vars} |
|
|
| |
|
|
| def get_full_stats(self) -> Dict[str, Any]: |
| """ |
| Return ALL stats for admin/frontend. |
| |
| Includes hidden values like HP and agility. |
| """ |
| effects = self._env_effects or EnvironmentEffects() |
|
|
| result = { |
| |
| 'hp': self._hp.copy(), |
| 'total_hp': self._total_hp, |
|
|
| |
| 'def_values': self._def.copy(), |
| 'total_def': self._total_def, |
|
|
| |
| 'atk_values': self._atk.copy(), |
|
|
| |
| 'equipment': self._equipment.copy() if self._equipment else {}, |
|
|
| |
| 'agility': self._agility, |
| 'signal_strength': self._signal_strength, |
| 'stealth_rating': self._stealth_rating, |
| 'firepower': self._firepower, |
|
|
| |
| 'detection_probability': self._compute_detection_probability(effects), |
| 'combat_rounds': self._compute_combat_rounds(effects), |
|
|
| |
| 'env_damage_applied': self._env_damage_applied, |
| 'weather_pattern': effects.weather_pattern if self._env_effects else None, |
|
|
| |
| 'hit_count': self._hit_count, |
| 'combat_damage': self._combat_damage.copy(), |
|
|
| |
| 'components': { |
| name: { |
| 'hp': self._hp.get(name, 0), |
| 'def': self._def.get(name, 0), |
| 'atk': self._atk.get(name), |
| 'is_critical': comp.is_critical, |
| 'has_atk': comp.has_atk, |
| } |
| for name, comp in self._components.items() |
| }, |
| } |
|
|
| |
| if self._equipment_effects: |
| result['equipment_effects'] = { |
| 'detection_modifier': getattr(self._equipment_effects, 'detection_modifier', 0), |
| 'agility_modifier': getattr(self._equipment_effects, 'agility_modifier', 0), |
| 'def_multiplier': getattr(self._equipment_effects, 'def_multiplier', 1.0), |
| 'emi_resistance': getattr(self._equipment_effects, 'emi_resistance', 0), |
| } |
|
|
| return result |
|
|
| def set_visibility(self, field: str, visibility: Visibility) -> None: |
| """Admin changes field visibility.""" |
| self._visibility.set_visibility(field, visibility) |
|
|
| |
|
|
| def _recompute_derived(self) -> None: |
| """Recompute all derived values from current state.""" |
| |
| self._total_def = sum(self._def.values()) |
|
|
| |
| self._total_hp = sum(self._hp.values()) |
|
|
| |
| self._agility = self._compute_agility(self._total_def) |
|
|
| |
| antenna_hp = self._hp.get('antenna', 0) |
| antenna_max_hp = self._components.get('antenna', ComponentConfig('antenna', 50, 10)).hp |
| self._signal_strength = min(1.0, max(0.0, antenna_hp / antenna_max_hp)) if antenna_max_hp > 0 else 0.0 |
|
|
| |
| self._stealth_rating = 1.0 - self._signal_strength |
|
|
| |
| gun_atk = self._atk.get('gun', 0) |
| self._firepower = gun_atk * self._eft.get('gun', 1.0) |
|
|
| def _compute_agility(self, total_def: int) -> float: |
| """ |
| Compute agility from total DEF. |
| |
| Higher DEF = Lower agility = More likely to be hit |
| |
| Formula: agility = base - linear_coeff * def * exp(-def / scale) - linear_penalty |
| """ |
| if total_def <= 0: |
| return self._agility_config.max_agility |
|
|
| cfg = self._agility_config |
| penalty = cfg.linear_coefficient * total_def * math.exp(-total_def / cfg.exponential_decay_scale) |
| linear_penalty = 0.001 * total_def |
|
|
| agility = cfg.base_agility - penalty - linear_penalty |
| return max(cfg.min_agility, min(cfg.max_agility, agility)) |
|
|
| |
|
|
| def filter_result_for_agent(self, result: JudgmentResult) -> Dict[str, Any]: |
| """ |
| Filter JudgmentResult for agent consumption. |
| |
| Hides HP values and internal details. |
| """ |
| effects = self._env_effects or EnvironmentEffects() |
|
|
| filtered = { |
| 'status': result.status, |
| 'survived': result.survived, |
|
|
| |
| 'hit_count': result.hit_count, |
| 'was_detected': result.was_detected, |
|
|
| |
| 'environment': self._get_visible_environment(effects), |
|
|
| |
| |
| |
| } |
|
|
| |
| if self._def_remaining_visible(): |
| filtered['def_remaining'] = self._def.copy() |
|
|
| return filtered |
|
|
| def _def_remaining_visible(self) -> bool: |
| """Check if DEF remaining values should be visible to agent.""" |
| |
| for def_field in ['engine_def', 'cockpit_def', 'wing_def', 'body_def', |
| 'antenna_def', 'camera_def', 'gun_def']: |
| if def_field in self._visibility.agent_visible_override: |
| return True |
| return False |
|
|
| def filter_result_for_admin(self, result: JudgmentResult) -> Dict[str, Any]: |
| """ |
| Full JudgmentResult for admin/frontend. |
| """ |
| effects = self._env_effects or EnvironmentEffects() |
|
|
| return { |
| 'status': result.status, |
| 'survived': result.survived, |
| 'fail_reason': result.fail_reason, |
|
|
| |
| 'final_hp': result.final_hp, |
|
|
| |
| 'hit_count': result.hit_count, |
| 'was_detected': result.was_detected, |
| 'damage_taken': result.damage_taken, |
|
|
| |
| 'drone_stats': self.get_full_stats(), |
|
|
| |
| 'environment': effects.raw_environment, |
| 'weather_pattern': effects.weather_pattern, |
|
|
| |
| 'combat_log': result.combat_log, |
| 'damage_log': result.damage_log, |
| } |
|
|
| |
|
|
| def add_observation_noise(self, result: Dict[str, Any], noise_std: float) -> Dict[str, Any]: |
| """ |
| Add Gaussian observation noise to agent-visible results. |
| |
| This is called by action_space when SCM supports weather-dependent noise. |
| |
| Args: |
| result: Filtered result for agent (from filter_result_for_agent) |
| noise_std: Standard deviation as ratio of value (e.g., 0.20 for rain, 0.05 for clear) |
| |
| Returns: |
| Noisy result for agent consumption |
| """ |
| import numpy as np |
|
|
| |
| def add_noise(value: float, std_ratio: float) -> float: |
| """Add Gaussian noise: value + N(0, (value * std_ratio)²)""" |
| if value == 0: |
| return 0 |
| std = abs(value) * std_ratio |
| noise = np.random.normal(0, std) |
| return max(0, value + noise) |
|
|
| |
| def flip_status(status: str) -> str: |
| """Flip status between RETURNED and DESTROYED""" |
| if status == 'RETURNED': |
| return 'DESTROYED' |
| elif status in ('DESTROYED', 'LOST'): |
| return 'RETURNED' |
| return status |
|
|
| |
| noisy_result = result.copy() |
|
|
| |
| if 'environment' in noisy_result: |
| for key, value in noisy_result['environment'].items(): |
| if isinstance(value, (int, float)): |
| noisy_result['environment'][key] = add_noise(float(value), noise_std) |
|
|
| |
| if 'hit_count' in noisy_result and noisy_result['hit_count'] is not None: |
| noisy_hit_count = add_noise(float(noisy_result['hit_count']), noise_std) |
| noisy_result['hit_count'] = int(max(0, round(noisy_hit_count))) |
|
|
| |
| |
| if 'status' in noisy_result and random.random() < noise_std * 0.1: |
| noisy_result['status'] = flip_status(noisy_result['status']) |
| |
| noisy_result['survived'] = (noisy_result['status'] == 'RETURNED') |
|
|
| |
| if 'def_remaining' in noisy_result: |
| for component, value in noisy_result['def_remaining'].items(): |
| if isinstance(value, (int, float)): |
| noisy_result['def_remaining'][component] = add_noise(float(value), noise_std) |
|
|
| return noisy_result |
|
|
| |
|
|
| def copy(self) -> 'DroneSheet': |
| """Create a deep copy of this DroneSheet.""" |
| new_sheet = DroneSheet.__new__(DroneSheet) |
| new_sheet._components = copy.deepcopy(self._components) |
| new_sheet._hp = self._hp.copy() |
| new_sheet._def = self._def.copy() |
| new_sheet._atk = self._atk.copy() |
| new_sheet._eft = self._eft.copy() |
| new_sheet._total_def = self._total_def |
| new_sheet._total_hp = self._total_hp |
| new_sheet._agility = self._agility |
| new_sheet._signal_strength = self._signal_strength |
| new_sheet._stealth_rating = self._stealth_rating |
| new_sheet._firepower = self._firepower |
| new_sheet._env_effects = self._env_effects |
| new_sheet._env_damage_applied = self._env_damage_applied |
| new_sheet._combat_damage = self._combat_damage.copy() |
| new_sheet._hit_count = self._hit_count |
| new_sheet._combat_log = self._combat_log.copy() |
| new_sheet._agility_config = self._agility_config |
| new_sheet._visibility = self._visibility |
| new_sheet._def_reduction_factor = self._def_reduction_factor |
| new_sheet._equipment = self._equipment.copy() |
| new_sheet._equipment_effects = self._equipment_effects |
| return new_sheet |
|
|