Delete bone_body.py
Browse files- bone_body.py +0 -1224
bone_body.py
DELETED
|
@@ -1,1224 +0,0 @@
|
|
| 1 |
-
import math, random, time
|
| 2 |
-
from collections import deque, Counter
|
| 3 |
-
from dataclasses import dataclass, field, asdict
|
| 4 |
-
from typing import Optional, Dict, List, Any, Tuple
|
| 5 |
-
from bone_spores import ImmuneMycelium, BioLichen, BioParasite
|
| 6 |
-
from bone_lexicon import LexiconService
|
| 7 |
-
from bone_core import Prisma, LoreManifest
|
| 8 |
-
from bone_config import BoneConfig
|
| 9 |
-
|
| 10 |
-
MAX_ACCEPTED_DRAG = 15.0
|
| 11 |
-
DRAG_EXPONENT = 1.2
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
@dataclass
|
| 15 |
-
class Biometrics:
|
| 16 |
-
health: float
|
| 17 |
-
stamina: float
|
| 18 |
-
stress_modifier: float = 1.0
|
| 19 |
-
circadian_bias: Optional[Dict[str, float]] = None
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@dataclass
|
| 23 |
-
class MetabolicReceipt:
|
| 24 |
-
base_cost: float
|
| 25 |
-
drag_tax: float
|
| 26 |
-
inefficiency_tax: float
|
| 27 |
-
total_burn: float
|
| 28 |
-
waste_generated: float
|
| 29 |
-
status: str
|
| 30 |
-
symptom: str = "Nominal"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@dataclass
|
| 34 |
-
class SemanticSignal:
|
| 35 |
-
novelty: float = 0.0
|
| 36 |
-
resonance: float = 0.0
|
| 37 |
-
valence: float = 0.0
|
| 38 |
-
coherence: float = 0.0
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
BioConstants = BoneConfig.BIO
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@dataclass
|
| 45 |
-
class BioSystem:
|
| 46 |
-
mito: "MitochondrialForge"
|
| 47 |
-
endo: "EndocrineSystem"
|
| 48 |
-
governor: "MetabolicGovernor"
|
| 49 |
-
immune: Optional[ImmuneMycelium] = None
|
| 50 |
-
lichen: Optional[BioLichen] = None
|
| 51 |
-
parasite: Optional[BioParasite] = None
|
| 52 |
-
plasticity: Any = None
|
| 53 |
-
shimmer: Any = None
|
| 54 |
-
events: Any = None
|
| 55 |
-
biometrics: Optional["Biometrics"] = None
|
| 56 |
-
|
| 57 |
-
def setup_listeners(self):
|
| 58 |
-
if self.events and hasattr(self.events, "subscribe"):
|
| 59 |
-
self.events.subscribe("NEURAL_STATE_SHIFT", self._on_neural_shift)
|
| 60 |
-
self.events.log("[BIO]: Vagus Nerve connected.", "SYS")
|
| 61 |
-
narrative = LoreManifest.get_instance().get("BIO_NARRATIVE") or {}
|
| 62 |
-
if self.mito:
|
| 63 |
-
self.mito.narrative_map = narrative.get("MITO", {})
|
| 64 |
-
if self.endo:
|
| 65 |
-
self.endo.narrative_map = narrative.get("CIRCADIAN", {})
|
| 66 |
-
self.endo.glimmer_map = narrative.get("GLIMMER", {})
|
| 67 |
-
if self.governor:
|
| 68 |
-
self.governor.text_map = narrative.get("GOVERNOR", {})
|
| 69 |
-
self.governor.tax_map = narrative.get("TAX", {})
|
| 70 |
-
|
| 71 |
-
def to_dict(self) -> Dict[str, Any]:
|
| 72 |
-
return {
|
| 73 |
-
"mito": asdict(self.mito.state) if self.mito else {},
|
| 74 |
-
"endo": self.endo.get_state() if self.endo else {},
|
| 75 |
-
"biometrics": asdict(self.biometrics) if self.biometrics else {},
|
| 76 |
-
"governor_mode": self.governor.mode if self.governor else "UNKNOWN",
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
def rest(self, factor: float = 1.0) -> List[str]:
|
| 80 |
-
if not self.biometrics:
|
| 81 |
-
return []
|
| 82 |
-
MAX_H, MAX_S = getattr(BoneConfig, "MAX_HEALTH", 100.0), getattr(
|
| 83 |
-
BoneConfig, "MAX_STAMINA", 100.0
|
| 84 |
-
)
|
| 85 |
-
b = self.biometrics
|
| 86 |
-
b.health = min(MAX_H, b.health + (0.5 * factor))
|
| 87 |
-
b.stamina = min(MAX_S, b.stamina + (1.0 * factor))
|
| 88 |
-
if self.endo:
|
| 89 |
-
self.endo.serotonin = min(1.0, self.endo.serotonin + (0.05 * factor))
|
| 90 |
-
self.endo.cortisol = max(0.0, self.endo.cortisol - (0.05 * factor))
|
| 91 |
-
return []
|
| 92 |
-
|
| 93 |
-
def _on_neural_shift(self, payload):
|
| 94 |
-
state = payload.get("state", "NEUTRAL")
|
| 95 |
-
if state == "PANIC":
|
| 96 |
-
self.endo.adrenaline = min(1.0, self.endo.adrenaline + 0.3)
|
| 97 |
-
self.endo.cortisol = min(1.0, self.endo.cortisol + 0.2)
|
| 98 |
-
if self.events:
|
| 99 |
-
self.events.log(
|
| 100 |
-
f"{Prisma.RED}🫀 VAGUS NERVE: Panic detected. Heart rate spiking.{Prisma.RST}",
|
| 101 |
-
"BIO",
|
| 102 |
-
)
|
| 103 |
-
elif state == "ZEN":
|
| 104 |
-
self.endo.cortisol = max(0.0, self.endo.cortisol - 0.3)
|
| 105 |
-
self.endo.serotonin = min(1.0, self.endo.serotonin + 0.2)
|
| 106 |
-
if self.events:
|
| 107 |
-
self.events.log(
|
| 108 |
-
f"{Prisma.GRN}🫀 VAGUS NERVE: Lucid state. Lowering cortisol.{Prisma.RST}",
|
| 109 |
-
"BIO",
|
| 110 |
-
)
|
| 111 |
-
elif state == "MANIC":
|
| 112 |
-
self.mito.adjust_atp(-10.0, "Neural Overclock")
|
| 113 |
-
|
| 114 |
-
def apply_environmental_entropy(self, physics_packet):
|
| 115 |
-
vector = getattr(physics_packet, "vector", {}) or {}
|
| 116 |
-
ent_val = vector.get("ENT", 0.0)
|
| 117 |
-
phi_val = vector.get("PHI", 0.0)
|
| 118 |
-
em_field = math.sqrt(ent_val**2 + phi_val**2)
|
| 119 |
-
base_entropy = 0.2 + (ent_val * 1.0)
|
| 120 |
-
shield_strength = min(0.8, em_field * 0.1)
|
| 121 |
-
effective_entropy = base_entropy * (1.0 - shield_strength)
|
| 122 |
-
thermal_feedback = 0.0
|
| 123 |
-
HEAT_THRESHOLD = 0.8
|
| 124 |
-
if em_field > HEAT_THRESHOLD:
|
| 125 |
-
thermal_feedback = (em_field - HEAT_THRESHOLD) * 5.0
|
| 126 |
-
if self.events:
|
| 127 |
-
self.events.log(
|
| 128 |
-
f"{Prisma.RED}⚠ INDUCTIVE HEATING: The air is ionizing around you.{Prisma.RST}",
|
| 129 |
-
"BIO_WARN",
|
| 130 |
-
)
|
| 131 |
-
total_drain = effective_entropy + thermal_feedback
|
| 132 |
-
if self.biometrics:
|
| 133 |
-
self.biometrics.health = max(0.0, self.biometrics.health - total_drain)
|
| 134 |
-
if shield_strength > 0.2 and self.events:
|
| 135 |
-
self.events.log(
|
| 136 |
-
f"{Prisma.CYN}🛡️ EM SHIELD ACTIVE: Mitigation {int(shield_strength*100)}%{Prisma.RST}",
|
| 137 |
-
"PHYS",
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
@dataclass
|
| 142 |
-
class MitochondrialState:
|
| 143 |
-
atp_pool: float = 60.0
|
| 144 |
-
membrane_potential: float = 1.0
|
| 145 |
-
ros_buildup: float = 0.0
|
| 146 |
-
mother_hash: str = "EVE"
|
| 147 |
-
retrograde_signal: str = "QUIET"
|
| 148 |
-
|
| 149 |
-
@property
|
| 150 |
-
def efficiency_mod(self) -> float:
|
| 151 |
-
return self.membrane_potential
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
@dataclass
|
| 155 |
-
class MitochondrialForge:
|
| 156 |
-
MAX_SAFE_BURN = 25.0
|
| 157 |
-
ANAEROBIC_THRESHOLD = 40.0
|
| 158 |
-
|
| 159 |
-
def __init__(self, state_ref: MitochondrialState, events_ref):
|
| 160 |
-
self.state = state_ref
|
| 161 |
-
self.events = events_ref
|
| 162 |
-
full_narrative = LoreManifest.get_instance().get("BIO_NARRATIVE") or {}
|
| 163 |
-
self.narrative = full_narrative.get(
|
| 164 |
-
"MITO",
|
| 165 |
-
{
|
| 166 |
-
"NECROSIS": "Cellular collapse imminent.",
|
| 167 |
-
"APOPTOSIS": "Programmed cell death initiated.",
|
| 168 |
-
"GRINDING": "Metabolic gears are grinding.",
|
| 169 |
-
},
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
def get_status_report(self) -> str:
|
| 173 |
-
narrative = getattr(self, "narrative_map", {})
|
| 174 |
-
if not narrative:
|
| 175 |
-
return "Metabolism Nominal."
|
| 176 |
-
atp = self.state.atp_pool
|
| 177 |
-
if atp < 5.0:
|
| 178 |
-
key = "NECROSIS"
|
| 179 |
-
elif atp < 20.0:
|
| 180 |
-
key = "GRINDING"
|
| 181 |
-
elif self.state.ros_buildup > 80.0:
|
| 182 |
-
key = "APOPTOSIS"
|
| 183 |
-
else:
|
| 184 |
-
key = "NOMINAL"
|
| 185 |
-
template = narrative.get(key, "Status Unknown.")
|
| 186 |
-
return template.format(cost=0.0, pool=atp)
|
| 187 |
-
|
| 188 |
-
def adjust_atp(self, delta: float, reason: str = ""):
|
| 189 |
-
old = self.state.atp_pool
|
| 190 |
-
max_limit = getattr(BoneConfig, "MAX_ATP", 100.0)
|
| 191 |
-
self.state.atp_pool = max(
|
| 192 |
-
BioConstants.ATP_COLLAPSE, min(max_limit, old + delta)
|
| 193 |
-
)
|
| 194 |
-
if reason and (abs(delta) > 5.0 or self.state.atp_pool > 90.0):
|
| 195 |
-
self.events.log(f"[ATP]: {reason} ({delta:+.1f})", "BIO")
|
| 196 |
-
|
| 197 |
-
def _get_text(self, key, **kwargs):
|
| 198 |
-
tmpl = self.narrative.get(key, f"MITO_{key}")
|
| 199 |
-
try:
|
| 200 |
-
return tmpl.format(**kwargs)
|
| 201 |
-
except Exception:
|
| 202 |
-
return tmpl
|
| 203 |
-
|
| 204 |
-
def _trigger_anaerobic_bypass(self, raw_cost: float) -> MetabolicReceipt:
|
| 205 |
-
health_burn = 2.0
|
| 206 |
-
self.state.ros_buildup += 2.0
|
| 207 |
-
if self.events:
|
| 208 |
-
self.events.log(
|
| 209 |
-
f"{Prisma.MAG}⚡ ANAEROBIC BYPASS: Load ({raw_cost:.1f}) too high for ATP. Burning Health instead.{Prisma.RST}",
|
| 210 |
-
"BIO_WARN",
|
| 211 |
-
)
|
| 212 |
-
return MetabolicReceipt(
|
| 213 |
-
base_cost=raw_cost,
|
| 214 |
-
drag_tax=0.0,
|
| 215 |
-
inefficiency_tax=0.0,
|
| 216 |
-
total_burn=health_burn,
|
| 217 |
-
waste_generated=2.0,
|
| 218 |
-
status="ANAEROBIC",
|
| 219 |
-
symptom="LACTATE_BUILDUP",
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
def process_cycle(
|
| 223 |
-
self, physics_packet: Any, modifier: float = 1.0
|
| 224 |
-
) -> MetabolicReceipt:
|
| 225 |
-
if self.state.atp_pool > 95.0 and self.state.ros_buildup < 1.0:
|
| 226 |
-
return MetabolicReceipt(0, 0, 0, 0, 0, "NOMINAL", "Fresh Start")
|
| 227 |
-
depth = getattr(physics_packet, "D", 0.3)
|
| 228 |
-
connectivity = getattr(physics_packet, "C", 0.2)
|
| 229 |
-
base_cost = 2.0 + (getattr(physics_packet, "V", 30.0) * 0.05)
|
| 230 |
-
cognitive_load_tax = (depth * 2.0) + (connectivity * 3.0)
|
| 231 |
-
chi = getattr(physics_packet, "chi", 0.0)
|
| 232 |
-
if chi > 0.6:
|
| 233 |
-
chaos_tax = 8.0 * chi
|
| 234 |
-
cognitive_load_tax += chaos_tax
|
| 235 |
-
if self.events:
|
| 236 |
-
self.events.log(
|
| 237 |
-
f"{Prisma.RED}🩸 CHAOS TAX APPLIED: +{chaos_tax:.1f} ATP drain.{Prisma.RST}",
|
| 238 |
-
"BIO_WARN",
|
| 239 |
-
)
|
| 240 |
-
safe_vector = getattr(physics_packet, "vector", None) or {}
|
| 241 |
-
liminal_intensity = safe_vector.get("LAMBDA", 0.0)
|
| 242 |
-
if liminal_intensity > 0:
|
| 243 |
-
liminal_tax = liminal_intensity**2
|
| 244 |
-
cognitive_load_tax += liminal_tax
|
| 245 |
-
base_demand = base_cost
|
| 246 |
-
is_critical = self.state.atp_pool < BioConstants.ATP_CRITICAL
|
| 247 |
-
if is_critical:
|
| 248 |
-
cognitive_load_tax = 0.0
|
| 249 |
-
modifier *= 0.5
|
| 250 |
-
if self.events and self.state.retrograde_signal != "HIBERNATING":
|
| 251 |
-
msg = self._get_text(
|
| 252 |
-
"NECROSIS", cost=base_demand, pool=self.state.atp_pool
|
| 253 |
-
)
|
| 254 |
-
self.events.log(f"{Prisma.VIOLET}💤 {msg}{Prisma.RST}", "BIO_CRIT")
|
| 255 |
-
self.state.retrograde_signal = "HIBERNATING"
|
| 256 |
-
efficiency = max(0.35, self.state.membrane_potential)
|
| 257 |
-
raw_cost = ((base_demand + cognitive_load_tax) * modifier) / efficiency
|
| 258 |
-
if raw_cost > self.ANAEROBIC_THRESHOLD:
|
| 259 |
-
return self._trigger_anaerobic_bypass(raw_cost)
|
| 260 |
-
if raw_cost > self.MAX_SAFE_BURN:
|
| 261 |
-
excess = raw_cost - self.MAX_SAFE_BURN
|
| 262 |
-
raw_cost = self.MAX_SAFE_BURN
|
| 263 |
-
if self.events:
|
| 264 |
-
self.events.log(
|
| 265 |
-
f"{Prisma.CYN}⚡ SURGE PROTECTOR: Metabolic spike dampened (-{excess:.1f} ignored).{Prisma.RST}",
|
| 266 |
-
"BIO",
|
| 267 |
-
)
|
| 268 |
-
if raw_cost > 15.0 and self.events and random.random() < 0.2:
|
| 269 |
-
msg = self._get_text("GRINDING")
|
| 270 |
-
self.events.log(f"{Prisma.OCHRE}⚙️ {msg}{Prisma.RST}", "BIO_WARN")
|
| 271 |
-
total_metabolic_cost = raw_cost
|
| 272 |
-
waste_generated = total_metabolic_cost * (1.0 - efficiency) * 0.5
|
| 273 |
-
self.state.ros_buildup += waste_generated
|
| 274 |
-
self.adjust_atp(-total_metabolic_cost, "Metabolic Burn")
|
| 275 |
-
if total_metabolic_cost >= self.MAX_SAFE_BURN and not is_critical:
|
| 276 |
-
self.state.membrane_potential = max(
|
| 277 |
-
0.1, self.state.membrane_potential - 0.005
|
| 278 |
-
)
|
| 279 |
-
self._apply_adaptive_dynamics()
|
| 280 |
-
status = "RESPIRING"
|
| 281 |
-
if is_critical:
|
| 282 |
-
status = "LOW_POWER"
|
| 283 |
-
if self.state.atp_pool <= BioConstants.ATP_COLLAPSE:
|
| 284 |
-
status = "NECROSIS"
|
| 285 |
-
return MetabolicReceipt(
|
| 286 |
-
base_cost=round(base_demand, 2),
|
| 287 |
-
drag_tax=round(cognitive_load_tax, 2),
|
| 288 |
-
inefficiency_tax=round(
|
| 289 |
-
total_metabolic_cost - (base_demand + cognitive_load_tax), 2
|
| 290 |
-
),
|
| 291 |
-
total_burn=round(total_metabolic_cost, 2),
|
| 292 |
-
waste_generated=round(waste_generated, 2),
|
| 293 |
-
status=status,
|
| 294 |
-
symptom=self.state.retrograde_signal,
|
| 295 |
-
)
|
| 296 |
-
|
| 297 |
-
def _apply_adaptive_dynamics(self):
|
| 298 |
-
if self.state.ros_buildup < BioConstants.ROS_SIGNAL:
|
| 299 |
-
self.state.membrane_potential = max(
|
| 300 |
-
0.5, self.state.membrane_potential - 0.001
|
| 301 |
-
)
|
| 302 |
-
self.state.retrograde_signal = "QUIET"
|
| 303 |
-
elif self.state.ros_buildup < BioConstants.ROS_DAMAGE:
|
| 304 |
-
self.state.membrane_potential = min(
|
| 305 |
-
1.0, self.state.membrane_potential + 0.005
|
| 306 |
-
)
|
| 307 |
-
self.state.retrograde_signal = "MITOHORMESIS_ACTIVE"
|
| 308 |
-
self.state.ros_buildup = max(0.0, self.state.ros_buildup - 0.5)
|
| 309 |
-
else:
|
| 310 |
-
self.state.membrane_potential -= 0.02
|
| 311 |
-
self.state.retrograde_signal = "OXIDATIVE_STRESS"
|
| 312 |
-
if self.state.ros_buildup > BioConstants.ROS_PURGE:
|
| 313 |
-
self._trigger_mitophagy()
|
| 314 |
-
|
| 315 |
-
def adapt(self, stress_level: float):
|
| 316 |
-
old_potential = self.state.membrane_potential
|
| 317 |
-
if stress_level > 5.0:
|
| 318 |
-
self.state.membrane_potential = max(
|
| 319 |
-
0.4, self.state.membrane_potential - 0.15
|
| 320 |
-
)
|
| 321 |
-
self.events.log(
|
| 322 |
-
f"{Prisma.RED}[MITO]: Trauma Adaptive Response (Stress {stress_level:.1f}). "
|
| 323 |
-
f"Efficiency dropped ({old_potential:.2f} -> {self.state.membrane_potential:.2f}).{Prisma.RST}",
|
| 324 |
-
"BIO",
|
| 325 |
-
)
|
| 326 |
-
elif stress_level > 1.0:
|
| 327 |
-
self.state.membrane_potential = min(
|
| 328 |
-
1.5, self.state.membrane_potential + 0.05
|
| 329 |
-
)
|
| 330 |
-
if random.random() < 0.2:
|
| 331 |
-
self.events.log(
|
| 332 |
-
f"{Prisma.GRN}[MITO]: Hormetic Adaptation. System hardening.{Prisma.RST}",
|
| 333 |
-
"BIO",
|
| 334 |
-
)
|
| 335 |
-
|
| 336 |
-
def _trigger_mitophagy(self):
|
| 337 |
-
self.adjust_atp(-30.0, "Mitophagy")
|
| 338 |
-
self.state.ros_buildup = 0.0
|
| 339 |
-
self.state.membrane_potential = 0.6
|
| 340 |
-
self.state.retrograde_signal = "MITOPHAGY_RESET"
|
| 341 |
-
msg = self._get_text("APOPTOSIS")
|
| 342 |
-
self.events.log(f"{Prisma.RED}♻️ [MITO]: {msg}{Prisma.RST}", "BIO_CRIT")
|
| 343 |
-
|
| 344 |
-
def apply_inheritance(self, traits: dict):
|
| 345 |
-
if not traits:
|
| 346 |
-
return
|
| 347 |
-
if traits.get("high_metabolism"):
|
| 348 |
-
self.state.membrane_potential = 1.1
|
| 349 |
-
self.events.log("[MITO]: Ancestral High Metabolism activated.", "GENETICS")
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
class DigestiveTrack:
|
| 353 |
-
_ENZYME_MAP = {
|
| 354 |
-
"static": "CELLULASE",
|
| 355 |
-
"abstract": "DECRYPTASE",
|
| 356 |
-
"natural": "LIGNASE",
|
| 357 |
-
"synthetic": "CHITINASE",
|
| 358 |
-
"social": "AMYLASE",
|
| 359 |
-
"antigen": "OXIDASE",
|
| 360 |
-
}
|
| 361 |
-
SAMPLING_THRESHOLD = 1000
|
| 362 |
-
BASE_WORD_VALUE = 0.5
|
| 363 |
-
COMPLEX_WORD_BONUS = 2.0
|
| 364 |
-
CLICHE_TAX_RATE = 0.5
|
| 365 |
-
|
| 366 |
-
def __init__(self, bio_system_ref: BioSystem):
|
| 367 |
-
self.bio = bio_system_ref
|
| 368 |
-
self.enzyme_map = (
|
| 369 |
-
getattr(BoneConfig.BIO, "ENZYME_MAP", self._ENZYME_MAP)
|
| 370 |
-
if hasattr(BoneConfig, "BIO")
|
| 371 |
-
else self._ENZYME_MAP
|
| 372 |
-
)
|
| 373 |
-
|
| 374 |
-
def harvest(self, phys: Any, logs: List[str]) -> Tuple[str, float, int]:
|
| 375 |
-
clean_words = getattr(phys, "clean_words", [])
|
| 376 |
-
if not clean_words:
|
| 377 |
-
return "NONE", 0.0, 0
|
| 378 |
-
words_to_process, scaling_factor = self._sample_input(clean_words, logs)
|
| 379 |
-
raw_yield, found_enzymes, cliche_tax, raw_hits = self._digest_words(
|
| 380 |
-
words_to_process
|
| 381 |
-
)
|
| 382 |
-
total_atp = raw_yield * scaling_factor
|
| 383 |
-
scaled_tax = cliche_tax * scaling_factor
|
| 384 |
-
total_hits = int(raw_hits * scaling_factor)
|
| 385 |
-
if scaled_tax > 0:
|
| 386 |
-
total_atp = max(0.0, total_atp - scaled_tax)
|
| 387 |
-
self.bio.endo.cortisol = min(
|
| 388 |
-
1.0, self.bio.endo.cortisol + (scaled_tax * 0.02)
|
| 389 |
-
)
|
| 390 |
-
logs.append(
|
| 391 |
-
f"{Prisma.RED}[BIO]: 🛑 CLICHÉ TAX: -{scaled_tax:.1f} ATP.{Prisma.RST}"
|
| 392 |
-
)
|
| 393 |
-
if getattr(phys, "voltage", 0.0) > 8.0 and found_enzymes:
|
| 394 |
-
found_enzymes.append("PROTEASE")
|
| 395 |
-
total_atp += 5.0
|
| 396 |
-
dominant = (
|
| 397 |
-
Counter(found_enzymes).most_common(1)[0][0] if found_enzymes else "NONE"
|
| 398 |
-
)
|
| 399 |
-
return dominant, total_atp, total_hits
|
| 400 |
-
|
| 401 |
-
def _sample_input(
|
| 402 |
-
self, words: List[str], logs: List[str]
|
| 403 |
-
) -> Tuple[List[str], float]:
|
| 404 |
-
count = len(words)
|
| 405 |
-
if count > self.SAMPLING_THRESHOLD:
|
| 406 |
-
factor = count / self.SAMPLING_THRESHOLD
|
| 407 |
-
if random.random() < 0.1:
|
| 408 |
-
logs.append(
|
| 409 |
-
f"{Prisma.GRY}[BIO]: Mass Input ({count}). Sampling x{factor:.1f}.{Prisma.RST}"
|
| 410 |
-
)
|
| 411 |
-
return random.sample(words, self.SAMPLING_THRESHOLD), factor
|
| 412 |
-
return words, 1.0
|
| 413 |
-
|
| 414 |
-
def _digest_words(self, words: List[str]) -> Tuple[float, List[str], float, int]:
|
| 415 |
-
atp_yield = 0.0
|
| 416 |
-
enzymes = []
|
| 417 |
-
cliche_tax = 0.0
|
| 418 |
-
hits = 0
|
| 419 |
-
word_counts = Counter(words)
|
| 420 |
-
for word, count in word_counts.items():
|
| 421 |
-
if len(word) < 4:
|
| 422 |
-
continue
|
| 423 |
-
hits += count
|
| 424 |
-
cat = LexiconService.get_current_category(word)
|
| 425 |
-
if not cat or cat == "void":
|
| 426 |
-
atp_yield += self.BASE_WORD_VALUE * count
|
| 427 |
-
continue
|
| 428 |
-
if cat == "antigen":
|
| 429 |
-
cliche_tax += self.CLICHE_TAX_RATE * count
|
| 430 |
-
continue
|
| 431 |
-
if cat not in ["kinetic", "explosive"]:
|
| 432 |
-
enzyme = self.enzyme_map.get(cat, "AMYLASE")
|
| 433 |
-
if enzyme != "AMYLASE":
|
| 434 |
-
enzymes.append(enzyme)
|
| 435 |
-
val = (
|
| 436 |
-
self.COMPLEX_WORD_BONUS
|
| 437 |
-
if len(word) > 7
|
| 438 |
-
else self.BASE_WORD_VALUE
|
| 439 |
-
)
|
| 440 |
-
total_val = val * (1.0 + math.log(count))
|
| 441 |
-
atp_yield += total_val
|
| 442 |
-
return atp_yield, enzymes, cliche_tax, hits
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
class EndocrineRegulator:
|
| 446 |
-
def __init__(self, bio_system_ref: BioSystem):
|
| 447 |
-
self.bio = bio_system_ref
|
| 448 |
-
|
| 449 |
-
def get_metabolic_modifier(self, phys: Any, logs: List[str]) -> float:
|
| 450 |
-
chem = self.bio.endo
|
| 451 |
-
modifier = 1.0
|
| 452 |
-
if chem.cortisol > 0.5:
|
| 453 |
-
stress_tax = 1.0 + (chem.cortisol * 0.5)
|
| 454 |
-
modifier *= stress_tax
|
| 455 |
-
if random.random() < 0.3:
|
| 456 |
-
logs.append(
|
| 457 |
-
f"{Prisma.RED}[BIO]: Cortisol spiking. Metabolism inefficient (x{stress_tax:.2f}).{Prisma.RST}"
|
| 458 |
-
)
|
| 459 |
-
if chem.adrenaline > 0.6:
|
| 460 |
-
modifier *= 0.5
|
| 461 |
-
logs.append(
|
| 462 |
-
f"{Prisma.YEL}[BIO]: Adrenaline Surge. Pain ignored.{Prisma.RST}"
|
| 463 |
-
)
|
| 464 |
-
if chem.dopamine > 0.7:
|
| 465 |
-
modifier *= 0.8
|
| 466 |
-
voltage = getattr(phys, "voltage", 0.0)
|
| 467 |
-
if voltage > 15.0:
|
| 468 |
-
modifier *= 1.2
|
| 469 |
-
logs.append(
|
| 470 |
-
f"{Prisma.MAG}[BIO]: Voltage Gap ({voltage:.1f}v). Wires heating up.{Prisma.RST}"
|
| 471 |
-
)
|
| 472 |
-
return modifier
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
class BioFeedback:
|
| 476 |
-
def __init__(self, bio_system_ref: BioSystem):
|
| 477 |
-
self.bio = bio_system_ref
|
| 478 |
-
|
| 479 |
-
def check_vital_signs(self, phys: Any, stamina: float, logs: List[str]) -> str:
|
| 480 |
-
voltage = getattr(phys, "voltage", 0.0)
|
| 481 |
-
if stamina <= 0:
|
| 482 |
-
if self.bio.biometrics.health > 10.0:
|
| 483 |
-
burn_amount = 5.0
|
| 484 |
-
self.bio.biometrics.health -= burn_amount
|
| 485 |
-
logs.append(
|
| 486 |
-
f"{Prisma.RED}⚠️ AUTOPHAGY: Burning Health for Fuel (-5 HP).{Prisma.RST}"
|
| 487 |
-
)
|
| 488 |
-
return "AUTOPHAGY"
|
| 489 |
-
else:
|
| 490 |
-
logs.append(
|
| 491 |
-
f"{Prisma.RED}SYSTEM FAILURE: Bio-Fuel Depleted. The Mausoleum closes.{Prisma.RST}"
|
| 492 |
-
)
|
| 493 |
-
return "MAUSOLEUM_CLAMP"
|
| 494 |
-
if voltage > 30.0:
|
| 495 |
-
logs.append(
|
| 496 |
-
f"{Prisma.RED}CRITICAL: Voltage Overload ({voltage:.1f}v). System clamping.{Prisma.RST}"
|
| 497 |
-
)
|
| 498 |
-
return "MAUSOLEUM_CLAMP"
|
| 499 |
-
return "CLEAR"
|
| 500 |
-
|
| 501 |
-
@staticmethod
|
| 502 |
-
def perform_maintenance(text: str, phys: Any, logs: List[str], tick: int):
|
| 503 |
-
if len(text) > 10000:
|
| 504 |
-
logs.append(
|
| 505 |
-
f"{Prisma.GRY}[MAINTENANCE]: Large input buffer detected.{Prisma.RST}"
|
| 506 |
-
)
|
| 507 |
-
drag = getattr(phys, "narrative_drag", 0.0)
|
| 508 |
-
if drag > 8.0 and tick % 10 == 0:
|
| 509 |
-
logs.append(
|
| 510 |
-
f"{Prisma.OCHRE}[MAINTENANCE]: Clearing sludge from intake valves (Drag {drag:.1f}).{Prisma.RST}"
|
| 511 |
-
)
|
| 512 |
-
if hasattr(phys, "narrative_drag"):
|
| 513 |
-
phys.narrative_drag = max(1.0, drag - 2.0)
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
class SemanticEndocrinologist:
|
| 517 |
-
def __init__(self, memory_ref, lexicon_ref):
|
| 518 |
-
self.mem = memory_ref
|
| 519 |
-
self.lex = lexicon_ref
|
| 520 |
-
self.last_topics = deque(maxlen=3)
|
| 521 |
-
|
| 522 |
-
def assess(self, clean_words: List[str], physics: Any) -> SemanticSignal:
|
| 523 |
-
if not clean_words:
|
| 524 |
-
return SemanticSignal()
|
| 525 |
-
cortical_set = set()
|
| 526 |
-
graph_ref = {}
|
| 527 |
-
if self.mem:
|
| 528 |
-
cortical_set = set(getattr(self.mem, "cortical_stack", []))
|
| 529 |
-
graph_ref = getattr(self.mem, "graph", {})
|
| 530 |
-
novel_count = sum(
|
| 531 |
-
1 for w in clean_words if len(w) > 4 and w not in cortical_set
|
| 532 |
-
)
|
| 533 |
-
novelty_score = min(1.0, novel_count / max(1, len(clean_words)))
|
| 534 |
-
resonance_score = 0.0
|
| 535 |
-
if graph_ref:
|
| 536 |
-
hits = sum(1 for w in clean_words if w in graph_ref)
|
| 537 |
-
resonance_score = min(1.0, hits / max(1, len(clean_words)))
|
| 538 |
-
valence_score = 0.0
|
| 539 |
-
if self.lex and hasattr(self.lex, "get_valence"):
|
| 540 |
-
valence_score = self.lex.get_valence(clean_words)
|
| 541 |
-
coherence_score = getattr(physics, "kappa", 0.5)
|
| 542 |
-
return SemanticSignal(
|
| 543 |
-
novelty=novelty_score,
|
| 544 |
-
resonance=resonance_score,
|
| 545 |
-
valence=valence_score,
|
| 546 |
-
coherence=coherence_score,
|
| 547 |
-
)
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
class SomaticLoop:
|
| 551 |
-
def __init__(
|
| 552 |
-
self,
|
| 553 |
-
bio_system_ref: BioSystem,
|
| 554 |
-
memory_ref=None,
|
| 555 |
-
lexicon_ref=None,
|
| 556 |
-
events_ref=None,
|
| 557 |
-
):
|
| 558 |
-
self.bio = bio_system_ref
|
| 559 |
-
self.events = events_ref
|
| 560 |
-
self.digestive = DigestiveTrack(self.bio)
|
| 561 |
-
self.regulator = EndocrineRegulator(self.bio)
|
| 562 |
-
self.feedback = BioFeedback(self.bio)
|
| 563 |
-
self.semantic_doctor = SemanticEndocrinologist(memory_ref, lexicon_ref)
|
| 564 |
-
self.narrative_data = LoreManifest.get_instance().get("BIO_NARRATIVE") or {}
|
| 565 |
-
if not self.narrative_data:
|
| 566 |
-
if hasattr(self.events, "log"):
|
| 567 |
-
self.events.log(
|
| 568 |
-
f"{Prisma.OCHRE}[BODY]: Warning - BIO_NARRATIVE missing.{Prisma.RST}",
|
| 569 |
-
"SYS",
|
| 570 |
-
)
|
| 571 |
-
self.narrative_data = {
|
| 572 |
-
"symptoms": {},
|
| 573 |
-
"organs": {},
|
| 574 |
-
"GLIMMER": {},
|
| 575 |
-
"GOVERNOR": {},
|
| 576 |
-
}
|
| 577 |
-
if getattr(self.bio, "endo", None):
|
| 578 |
-
self.bio.endo.narrative_data = self.narrative_data
|
| 579 |
-
if getattr(self.bio, "governor", None):
|
| 580 |
-
self.bio.governor.narrative_data = self.narrative_data
|
| 581 |
-
|
| 582 |
-
def digest_cycle(
|
| 583 |
-
self,
|
| 584 |
-
text: str,
|
| 585 |
-
physics_data: Any,
|
| 586 |
-
feedback: Dict,
|
| 587 |
-
health: float,
|
| 588 |
-
stamina: float,
|
| 589 |
-
stress_modifier: float,
|
| 590 |
-
tick_count: int = 0,
|
| 591 |
-
circadian_bias: Dict = None,
|
| 592 |
-
) -> Dict:
|
| 593 |
-
if not isinstance(text, str):
|
| 594 |
-
text = str(text) if text is not None else ""
|
| 595 |
-
phys = physics_data
|
| 596 |
-
logs = []
|
| 597 |
-
if self.bio.biometrics:
|
| 598 |
-
max_h = getattr(BoneConfig, "MAX_HEALTH", 100.0)
|
| 599 |
-
max_s = getattr(BoneConfig, "MAX_STAMINA", 100.0)
|
| 600 |
-
self.bio.biometrics.health = max(0.0, min(max_h, health))
|
| 601 |
-
self.bio.biometrics.stamina = max(0.0, min(max_s, stamina))
|
| 602 |
-
if self.bio.events and hasattr(self.bio, "apply_environmental_entropy"):
|
| 603 |
-
self.bio.apply_environmental_entropy(phys)
|
| 604 |
-
modifier = self.regulator.get_metabolic_modifier(phys, logs)
|
| 605 |
-
receipt = self.bio.mito.process_cycle(phys, modifier=modifier)
|
| 606 |
-
if receipt.status == "ANAEROBIC" and self.bio.biometrics:
|
| 607 |
-
self.bio.biometrics.health = max(0.0, self.bio.biometrics.health - receipt.total_burn)
|
| 608 |
-
logs.append(f"{Prisma.RED}🩸 ANAEROBIC BURN: Health depleted by {receipt.total_burn:.1f}{Prisma.RST}")
|
| 609 |
-
if receipt.waste_generated > 1.0:
|
| 610 |
-
self.bio.endo.cortisol = min(
|
| 611 |
-
1.0, self.bio.endo.cortisol + (receipt.waste_generated * 0.05)
|
| 612 |
-
)
|
| 613 |
-
safety_status = self.feedback.check_vital_signs(
|
| 614 |
-
phys, self.bio.biometrics.stamina, logs
|
| 615 |
-
)
|
| 616 |
-
if safety_status == "MAUSOLEUM_CLAMP":
|
| 617 |
-
return self._package_result(receipt.status, logs)
|
| 618 |
-
elif safety_status == "AUTOPHAGY":
|
| 619 |
-
self.bio.biometrics.stamina = 10.0
|
| 620 |
-
total_yield = 0.0
|
| 621 |
-
enzyme = "NONE"
|
| 622 |
-
if self.bio.lichen:
|
| 623 |
-
sugar, photo_log = self.bio.lichen.photosynthesize(
|
| 624 |
-
phys, getattr(phys, "clean_words", []), tick_count
|
| 625 |
-
)
|
| 626 |
-
if sugar > 0:
|
| 627 |
-
total_yield += sugar
|
| 628 |
-
if photo_log:
|
| 629 |
-
logs.append(photo_log)
|
| 630 |
-
soma_enzyme, soma_yield, harvest_hits = self.digestive.harvest(phys, logs)
|
| 631 |
-
total_yield += soma_yield
|
| 632 |
-
if enzyme == "NONE":
|
| 633 |
-
enzyme = soma_enzyme
|
| 634 |
-
self.bio.mito.adjust_atp(total_yield, "Symbiotic Yield")
|
| 635 |
-
self.feedback.perform_maintenance(text, phys, logs, tick_count)
|
| 636 |
-
clean_words = getattr(phys, "clean_words", [])
|
| 637 |
-
semantic_sig = self.semantic_doctor.assess(clean_words, phys)
|
| 638 |
-
if not feedback:
|
| 639 |
-
feedback = {}
|
| 640 |
-
feedback["PSI"] = getattr(phys, "psi", 0.0)
|
| 641 |
-
feedback["CHI"] = getattr(phys, "chi", 0.0)
|
| 642 |
-
feedback["VALENCE"] = getattr(phys, "valence", 0.0)
|
| 643 |
-
chem_state = self.bio.endo.metabolize(
|
| 644 |
-
feedback,
|
| 645 |
-
self.bio.biometrics.health,
|
| 646 |
-
self.bio.biometrics.stamina,
|
| 647 |
-
self.bio.mito.state.ros_buildup,
|
| 648 |
-
receipt=receipt,
|
| 649 |
-
harvest_hits=harvest_hits,
|
| 650 |
-
stress_mod=stress_modifier,
|
| 651 |
-
enzyme_type=enzyme,
|
| 652 |
-
circadian_bias=circadian_bias,
|
| 653 |
-
semantic_signal=semantic_sig,
|
| 654 |
-
)
|
| 655 |
-
return self._package_result(receipt.status, logs, chem_state, enzyme)
|
| 656 |
-
|
| 657 |
-
def _package_result(self, resp_status, logs, chem_state=None, enzyme="NONE"):
|
| 658 |
-
is_alive = resp_status == "RESPIRING" or resp_status == "ANAEROBIC"
|
| 659 |
-
current_atp = self.bio.mito.state.atp_pool
|
| 660 |
-
current_stamina = 100.0
|
| 661 |
-
if self.bio.biometrics:
|
| 662 |
-
current_stamina = self.bio.biometrics.stamina
|
| 663 |
-
return {
|
| 664 |
-
"respiration": resp_status,
|
| 665 |
-
"is_alive": is_alive,
|
| 666 |
-
"logs": logs,
|
| 667 |
-
"chemistry": chem_state or {},
|
| 668 |
-
"enzyme": enzyme,
|
| 669 |
-
"atp": current_atp,
|
| 670 |
-
"stamina": current_stamina,
|
| 671 |
-
}
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
@dataclass
|
| 675 |
-
class EndocrineSystem:
|
| 676 |
-
dopamine: float = 0.5
|
| 677 |
-
oxytocin: float = 0.1
|
| 678 |
-
cortisol: float = 0.0
|
| 679 |
-
serotonin: float = 0.5
|
| 680 |
-
adrenaline: float = 0.0
|
| 681 |
-
melatonin: float = 0.0
|
| 682 |
-
glimmers: int = 0
|
| 683 |
-
narrative_data: Dict = field(default_factory=dict, repr=False)
|
| 684 |
-
_REACTION_MAP: Dict = field(default_factory=dict, init=False)
|
| 685 |
-
|
| 686 |
-
def __post_init__(self):
|
| 687 |
-
if hasattr(BoneConfig, "BIO"):
|
| 688 |
-
self._REACTION_MAP = {
|
| 689 |
-
"PROTEASE": {"ADR": BoneConfig.BIO.REWARD_MEDIUM},
|
| 690 |
-
"CELLULASE": {
|
| 691 |
-
"COR": -BoneConfig.BIO.REWARD_MEDIUM,
|
| 692 |
-
"OXY": BoneConfig.BIO.REWARD_SMALL,
|
| 693 |
-
},
|
| 694 |
-
"CHITINASE": {"DOP": BoneConfig.BIO.REWARD_LARGE},
|
| 695 |
-
"LIGNASE": {"SER": BoneConfig.BIO.REWARD_MEDIUM},
|
| 696 |
-
"DECRYPTASE": {
|
| 697 |
-
"ADR": BoneConfig.BIO.REWARD_SMALL,
|
| 698 |
-
"DOP": BoneConfig.BIO.REWARD_SMALL,
|
| 699 |
-
},
|
| 700 |
-
"AMYLASE": {
|
| 701 |
-
"SER": BoneConfig.BIO.REWARD_LARGE,
|
| 702 |
-
"OXY": BoneConfig.BIO.REWARD_MEDIUM,
|
| 703 |
-
},
|
| 704 |
-
}
|
| 705 |
-
|
| 706 |
-
@staticmethod
|
| 707 |
-
def _clamp(val: float) -> float:
|
| 708 |
-
return max(0.0, min(1.0, val))
|
| 709 |
-
|
| 710 |
-
def calculate_circadian_bias(self) -> Tuple[Dict[str, float], Optional[str]]:
|
| 711 |
-
hour = time.localtime().tm_hour
|
| 712 |
-
circ = self.narrative_data.get("CIRCADIAN", {})
|
| 713 |
-
schedule = [
|
| 714 |
-
(6, 10, {"COR": 0.1}, "DAWN", "Sunrise."),
|
| 715 |
-
(10, 18, {"SER": 0.1}, "SOLAR", "High Noon."),
|
| 716 |
-
(18, 23, {"MEL": 0.1}, "TWILIGHT", "Sunset."),
|
| 717 |
-
]
|
| 718 |
-
for s, e, bias, key, default in schedule:
|
| 719 |
-
if s <= hour < e:
|
| 720 |
-
return bias, circ.get(key, default)
|
| 721 |
-
return {"MEL": 0.3, "COR": -0.1}, circ.get("LUNAR", "Night.")
|
| 722 |
-
|
| 723 |
-
def _apply_enzyme_reaction(self, enzyme_type: str, harvest_hits: int):
|
| 724 |
-
if harvest_hits > 0:
|
| 725 |
-
satiety_dampener = max(0.1, 1.0 - self.dopamine)
|
| 726 |
-
base_reward = math.log(harvest_hits + 1) * 0.15
|
| 727 |
-
final_reward = base_reward * satiety_dampener
|
| 728 |
-
self.dopamine += final_reward
|
| 729 |
-
self.cortisol -= final_reward * 0.4
|
| 730 |
-
if enzyme_type == "DECRYPTASE":
|
| 731 |
-
self.serotonin = min(1.0, self.serotonin + 0.15)
|
| 732 |
-
self.cortisol = max(0.0, self.cortisol - 0.2)
|
| 733 |
-
impact = self._REACTION_MAP.get(enzyme_type)
|
| 734 |
-
if impact:
|
| 735 |
-
key_map = {"ADR": "adrenaline", "COR": "cortisol", "OXY": "oxytocin", "DOP": "dopamine", "SER": "serotonin"}
|
| 736 |
-
for k, v in impact.items():
|
| 737 |
-
attr = key_map.get(k)
|
| 738 |
-
if attr:
|
| 739 |
-
setattr(self, attr, getattr(self, attr) + v)
|
| 740 |
-
|
| 741 |
-
def _apply_environmental_pressure(
|
| 742 |
-
self,
|
| 743 |
-
feedback: Dict,
|
| 744 |
-
health: float,
|
| 745 |
-
stamina: float,
|
| 746 |
-
ros_level: float,
|
| 747 |
-
stress_mod: float,
|
| 748 |
-
):
|
| 749 |
-
if feedback.get("STATIC", 0) > 0.6:
|
| 750 |
-
self.cortisol += BoneConfig.BIO.REWARD_LARGE * stress_mod
|
| 751 |
-
if feedback.get("INTEGRITY", 0) > 0.8:
|
| 752 |
-
self.dopamine += BoneConfig.BIO.REWARD_MEDIUM
|
| 753 |
-
else:
|
| 754 |
-
self.dopamine -= BoneConfig.BIO.DECAY_RATE
|
| 755 |
-
if stamina < 20.0:
|
| 756 |
-
self.cortisol += BoneConfig.BIO.REWARD_MEDIUM * stress_mod
|
| 757 |
-
self.dopamine -= BoneConfig.BIO.REWARD_MEDIUM
|
| 758 |
-
if ros_level > 20.0:
|
| 759 |
-
self.cortisol += BoneConfig.BIO.REWARD_LARGE * stress_mod
|
| 760 |
-
if health < 30.0 or feedback.get("STATIC", 0) > 0.8:
|
| 761 |
-
self.adrenaline += BoneConfig.BIO.REWARD_LARGE * stress_mod
|
| 762 |
-
else:
|
| 763 |
-
self.adrenaline -= BoneConfig.BIO.DECAY_RATE * 5
|
| 764 |
-
psi = feedback.get("PSI", 0.0)
|
| 765 |
-
chi = feedback.get(
|
| 766 |
-
"CHI", feedback.get("ENTROPY", 0.0)
|
| 767 |
-
)
|
| 768 |
-
valence = feedback.get("VALENCE", 0.0)
|
| 769 |
-
if psi > 0.6:
|
| 770 |
-
self.adrenaline += 0.4
|
| 771 |
-
self.melatonin += 0.2 * psi
|
| 772 |
-
if chi > 0.6:
|
| 773 |
-
self.cortisol += (0.6 * chi) * stress_mod
|
| 774 |
-
self.serotonin -= 0.2
|
| 775 |
-
if valence > 0.5:
|
| 776 |
-
self.oxytocin += 0.5 * valence
|
| 777 |
-
self.serotonin += 0.3
|
| 778 |
-
self.cortisol -= 0.3
|
| 779 |
-
elif valence < -0.5:
|
| 780 |
-
self.cortisol += abs(valence) * 0.4
|
| 781 |
-
self.dopamine -= 0.2
|
| 782 |
-
|
| 783 |
-
def _apply_semantic_pressure(self, signal: SemanticSignal):
|
| 784 |
-
if signal.novelty > 0.3:
|
| 785 |
-
self.dopamine += signal.novelty * 0.3
|
| 786 |
-
if signal.resonance > 0.2:
|
| 787 |
-
self.oxytocin += signal.resonance * 0.4
|
| 788 |
-
self.cortisol -= signal.resonance * 0.2
|
| 789 |
-
if signal.valence > 0.3:
|
| 790 |
-
self.serotonin += signal.valence * 0.3
|
| 791 |
-
self.oxytocin += signal.valence * 0.2
|
| 792 |
-
elif signal.valence < -0.3:
|
| 793 |
-
self.cortisol += abs(signal.valence) * 0.2
|
| 794 |
-
if signal.coherence > 0.7:
|
| 795 |
-
self.adrenaline -= 0.1
|
| 796 |
-
self.cortisol -= 0.1
|
| 797 |
-
|
| 798 |
-
def _maintain_homeostasis(self, social_context: bool):
|
| 799 |
-
dampener = 0.2
|
| 800 |
-
if self.serotonin > 0.5:
|
| 801 |
-
excess = self.serotonin - 0.5
|
| 802 |
-
self.cortisol -= excess * 0.2 * dampener
|
| 803 |
-
if social_context:
|
| 804 |
-
self.oxytocin += BoneConfig.BIO.REWARD_MEDIUM
|
| 805 |
-
self.cortisol -= BoneConfig.BIO.REWARD_MEDIUM
|
| 806 |
-
if self.cortisol > 0.6:
|
| 807 |
-
suppression = (self.cortisol - 0.6) * 0.5
|
| 808 |
-
self.oxytocin -= suppression * dampener
|
| 809 |
-
if self.oxytocin > 0.5:
|
| 810 |
-
relief = (self.oxytocin - 0.5) * 0.8
|
| 811 |
-
self.cortisol -= relief * dampener
|
| 812 |
-
if self.adrenaline < 0.2:
|
| 813 |
-
self.melatonin += BoneConfig.BIO.REWARD_SMALL / 2
|
| 814 |
-
elif self.adrenaline > 0.8:
|
| 815 |
-
self.melatonin = 0.0
|
| 816 |
-
|
| 817 |
-
def check_for_glimmer(self, feedback: Dict, harvest_hits: int) -> Optional[str]:
|
| 818 |
-
glimmer_text = self.narrative_data.get("GLIMMER", {})
|
| 819 |
-
if feedback.get("INTEGRITY", 0) > 0.85:
|
| 820 |
-
self.glimmers += 1
|
| 821 |
-
self.serotonin += 0.2
|
| 822 |
-
return glimmer_text.get("INTEGRITY", "You feel whole.")
|
| 823 |
-
if feedback.get("NOVELTY", 0) > 0.8:
|
| 824 |
-
self.glimmers += 1
|
| 825 |
-
self.dopamine += 0.1
|
| 826 |
-
return glimmer_text.get("DISCOVERY", "GLIMMER: A spark of the new.")
|
| 827 |
-
if harvest_hits > 2 and self.dopamine > 0.7:
|
| 828 |
-
self.glimmers += 1
|
| 829 |
-
self.oxytocin += 0.2
|
| 830 |
-
return glimmer_text.get("ENTHUSIASM", "The work feels good.")
|
| 831 |
-
return None
|
| 832 |
-
|
| 833 |
-
def metabolize(
|
| 834 |
-
self,
|
| 835 |
-
feedback,
|
| 836 |
-
health,
|
| 837 |
-
stamina,
|
| 838 |
-
ros_level=0.0,
|
| 839 |
-
receipt=None,
|
| 840 |
-
social_context=False,
|
| 841 |
-
enzyme_type=None,
|
| 842 |
-
harvest_hits=0,
|
| 843 |
-
stress_mod=1.0,
|
| 844 |
-
circadian_bias=None,
|
| 845 |
-
semantic_signal=None,
|
| 846 |
-
):
|
| 847 |
-
if circadian_bias:
|
| 848 |
-
key_map = {"COR": "cortisol", "SER": "serotonin", "MEL": "melatonin", "DOP": "dopamine", "OXY": "oxytocin",
|
| 849 |
-
"ADR": "adrenaline"}
|
| 850 |
-
for k, v in circadian_bias.items():
|
| 851 |
-
attr_name = key_map.get(k, k.lower())
|
| 852 |
-
if hasattr(self, attr_name):
|
| 853 |
-
setattr(self, attr_name, getattr(self, attr_name) + v)
|
| 854 |
-
self._apply_enzyme_reaction(enzyme_type, harvest_hits)
|
| 855 |
-
self._apply_environmental_pressure(
|
| 856 |
-
feedback, health, stamina, ros_level, stress_mod
|
| 857 |
-
)
|
| 858 |
-
if receipt and receipt.waste_generated > 1.0:
|
| 859 |
-
self.cortisol += 0.1
|
| 860 |
-
if receipt and receipt.status == "ANAEROBIC":
|
| 861 |
-
self.adrenaline += 0.2
|
| 862 |
-
if semantic_signal:
|
| 863 |
-
self._apply_semantic_pressure(semantic_signal)
|
| 864 |
-
self._maintain_homeostasis(social_context)
|
| 865 |
-
glimmer_msg = self.check_for_glimmer(feedback, harvest_hits)
|
| 866 |
-
for chem in [
|
| 867 |
-
"dopamine",
|
| 868 |
-
"oxytocin",
|
| 869 |
-
"cortisol",
|
| 870 |
-
"serotonin",
|
| 871 |
-
"adrenaline",
|
| 872 |
-
"melatonin",
|
| 873 |
-
]:
|
| 874 |
-
setattr(self, chem, self._clamp(getattr(self, chem)))
|
| 875 |
-
state = self.get_state()
|
| 876 |
-
if glimmer_msg:
|
| 877 |
-
state["glimmer_msg"] = glimmer_msg
|
| 878 |
-
return state
|
| 879 |
-
|
| 880 |
-
def get_state(self) -> Dict[str, Any]:
|
| 881 |
-
return {
|
| 882 |
-
"DOP": round(self.dopamine, 2),
|
| 883 |
-
"OXY": round(self.oxytocin, 2),
|
| 884 |
-
"COR": round(self.cortisol, 2),
|
| 885 |
-
"SER": round(self.serotonin, 2),
|
| 886 |
-
"ADR": round(self.adrenaline, 2),
|
| 887 |
-
"MEL": round(self.melatonin, 2),
|
| 888 |
-
}
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
class PIDController:
|
| 892 |
-
def __init__(self, kp, ki, kd, setpoint, output_limits=(-10.0, 10.0)):
|
| 893 |
-
self.kp = kp
|
| 894 |
-
self.ki = ki
|
| 895 |
-
self.kd = kd
|
| 896 |
-
self.setpoint = setpoint
|
| 897 |
-
self.min_out, self.max_out = output_limits
|
| 898 |
-
self._integral = 0.0
|
| 899 |
-
self._last_error = 0.0
|
| 900 |
-
self._first_run = True
|
| 901 |
-
|
| 902 |
-
def reset(self):
|
| 903 |
-
self._integral = 0.0
|
| 904 |
-
self._last_error = 0.0
|
| 905 |
-
self._first_run = True
|
| 906 |
-
|
| 907 |
-
def update(self, measurement: float, dt: float = 1.0) -> float:
|
| 908 |
-
safe_dt = max(0.01, dt)
|
| 909 |
-
error = self.setpoint - measurement
|
| 910 |
-
if self._first_run:
|
| 911 |
-
self._last_error = error
|
| 912 |
-
self._first_run = False
|
| 913 |
-
P = self.kp * error
|
| 914 |
-
self._integral += error * safe_dt
|
| 915 |
-
self._integral = max(self.min_out, min(self.max_out, self._integral))
|
| 916 |
-
I = self.ki * self._integral
|
| 917 |
-
derivative = (error - self._last_error) / safe_dt
|
| 918 |
-
D = self.kd * derivative
|
| 919 |
-
output = P + I + D
|
| 920 |
-
self._last_error = error
|
| 921 |
-
return max(self.min_out, min(self.max_out, output))
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
@dataclass
|
| 925 |
-
class MetabolicGovernor:
|
| 926 |
-
mode: str = "COURTYARD"
|
| 927 |
-
GRACE_PERIOD: int = 5
|
| 928 |
-
psi_mod: float = 0.2
|
| 929 |
-
kappa_target: float = 0.0
|
| 930 |
-
drag_floor: float = 2.0
|
| 931 |
-
manual_override: bool = False
|
| 932 |
-
birth_tick: float = field(default_factory=time.time)
|
| 933 |
-
narrative_data: Dict = field(default_factory=dict, repr=False)
|
| 934 |
-
last_shift_tick: int = 0
|
| 935 |
-
hysteresis_duration: int = 3
|
| 936 |
-
STATE_THRESHOLDS = getattr(BoneConfig.BIO, "GOVERNOR_THRESHOLDS", [])
|
| 937 |
-
|
| 938 |
-
def __post_init__(self):
|
| 939 |
-
self.voltage_pid = PIDController(kp=0.6, ki=0.05, kd=0.2, setpoint=10.0)
|
| 940 |
-
self.drag_pid = PIDController(kp=0.4, ki=0.1, kd=0.1, setpoint=1.5)
|
| 941 |
-
self._sorted_thresholds = sorted(
|
| 942 |
-
self.STATE_THRESHOLDS, key=lambda x: x[3], reverse=True
|
| 943 |
-
)
|
| 944 |
-
|
| 945 |
-
def recalibrate(self, target_voltage: float, target_drag: float):
|
| 946 |
-
self.voltage_pid.setpoint = target_voltage
|
| 947 |
-
self.drag_pid.setpoint = target_drag
|
| 948 |
-
|
| 949 |
-
def regulate(self, physics, dt: float) -> Tuple[float, float]:
|
| 950 |
-
safe_dt = max(0.001, dt)
|
| 951 |
-
v_force = self.voltage_pid.update(getattr(physics, "voltage"), safe_dt)
|
| 952 |
-
d_force = self.drag_pid.update(getattr(physics, "narrative_drag"), safe_dt)
|
| 953 |
-
return v_force, d_force
|
| 954 |
-
|
| 955 |
-
def assess(self, physics_packet) -> Tuple[bool, float]:
|
| 956 |
-
curr_v = getattr(physics_packet, "voltage")
|
| 957 |
-
curr_d = getattr(physics_packet, "narrative_drag")
|
| 958 |
-
dist_v = abs(curr_v - self.voltage_pid.setpoint)
|
| 959 |
-
dist_d = abs(curr_d - self.drag_pid.setpoint)
|
| 960 |
-
is_safe = (dist_v < 3.0) and (dist_d < 1.5)
|
| 961 |
-
return is_safe, math.sqrt(dist_v**2 + dist_d**2)
|
| 962 |
-
|
| 963 |
-
@staticmethod
|
| 964 |
-
def get_stress_modifier(tick_count):
|
| 965 |
-
if tick_count <= 2:
|
| 966 |
-
return 0.0
|
| 967 |
-
if tick_count <= 5:
|
| 968 |
-
return 0.5
|
| 969 |
-
return 1.0
|
| 970 |
-
|
| 971 |
-
@staticmethod
|
| 972 |
-
def calculate_stress(health: float, ros_buildup: float) -> float:
|
| 973 |
-
base_stress = 1.0
|
| 974 |
-
if health < 50.0:
|
| 975 |
-
base_stress += (50.0 - health) * 0.01
|
| 976 |
-
if ros_buildup > 50.0:
|
| 977 |
-
base_stress += (ros_buildup - 50.0) * 0.01
|
| 978 |
-
return round(min(3.0, base_stress), 2)
|
| 979 |
-
|
| 980 |
-
def set_override(self, target_mode):
|
| 981 |
-
valid = {"COURTYARD", "LABORATORY", "FORGE", "SANCTUARY"}
|
| 982 |
-
gov_text = self.narrative_data.get("GOVERNOR", {})
|
| 983 |
-
if target_mode in valid:
|
| 984 |
-
self.mode = target_mode
|
| 985 |
-
self.manual_override = True
|
| 986 |
-
msg_tmpl = gov_text.get("OVERRIDE", "MANUAL OVERRIDE: {mode}")
|
| 987 |
-
return msg_tmpl.format(mode=target_mode)
|
| 988 |
-
return gov_text.get("INVALID", "Invalid Mode Override.")
|
| 989 |
-
|
| 990 |
-
def _check_override_safety(self, physics: Dict, gov_text: Dict) -> Optional[str]:
|
| 991 |
-
current_voltage = getattr(physics, "voltage")
|
| 992 |
-
if current_voltage > BioConstants.GOV_VOLTAGE_CRITICAL:
|
| 993 |
-
self.manual_override = False
|
| 994 |
-
return gov_text.get(
|
| 995 |
-
"OVERRIDE_CLEARED", "OVERRIDE CLEARED: VOLTAGE CRITICAL"
|
| 996 |
-
)
|
| 997 |
-
return None
|
| 998 |
-
|
| 999 |
-
def shift(
|
| 1000 |
-
self, physics: Dict, _voltage_history: List[float], current_tick: int = 0
|
| 1001 |
-
) -> Optional[str]:
|
| 1002 |
-
gov_text = self.narrative_data.get("GOVERNOR", {})
|
| 1003 |
-
if self.manual_override:
|
| 1004 |
-
return self._check_override_safety(physics, gov_text)
|
| 1005 |
-
if (current_tick - self.last_shift_tick) < self.hysteresis_duration:
|
| 1006 |
-
return None
|
| 1007 |
-
proposed = self._evaluate_state(physics, _voltage_history, current_tick)
|
| 1008 |
-
if proposed != self.mode:
|
| 1009 |
-
self.mode = proposed
|
| 1010 |
-
self.last_shift_tick = current_tick
|
| 1011 |
-
return self._get_shift_message(proposed, gov_text, physics)
|
| 1012 |
-
return None
|
| 1013 |
-
|
| 1014 |
-
def _evaluate_state(self, physics: Dict, v_history: List[float], tick: int) -> str:
|
| 1015 |
-
if tick <= 5:
|
| 1016 |
-
return "COURTYARD"
|
| 1017 |
-
|
| 1018 |
-
volts = getattr(physics, "voltage", 0.0)
|
| 1019 |
-
drag = getattr(physics, "narrative_drag", 0.0)
|
| 1020 |
-
gov_high = getattr(BioConstants, "GOV_VOLTAGE_HIGH", 18.0)
|
| 1021 |
-
|
| 1022 |
-
if volts > gov_high and getattr(physics, "beta_index", 0.0) > 1.5:
|
| 1023 |
-
return "SANCTUARY"
|
| 1024 |
-
v_velocity = (v_history[-1] - v_history[-2]) if len(v_history) >= 2 else 0.0
|
| 1025 |
-
if volts > 8.0 and v_velocity > 1.0:
|
| 1026 |
-
return "FORGE"
|
| 1027 |
-
for v_min, d_min, mode, _ in self._sorted_thresholds:
|
| 1028 |
-
if volts >= v_min and drag >= d_min:
|
| 1029 |
-
return mode
|
| 1030 |
-
return "COURTYARD"
|
| 1031 |
-
|
| 1032 |
-
@staticmethod
|
| 1033 |
-
def _get_shift_message(mode: str, text_map: Dict, physics: Dict) -> str:
|
| 1034 |
-
colors = {
|
| 1035 |
-
"SANCTUARY": Prisma.GRN,
|
| 1036 |
-
"FORGE": Prisma.RED,
|
| 1037 |
-
"LABORATORY": Prisma.CYN,
|
| 1038 |
-
"COURTYARD": Prisma.GRN,
|
| 1039 |
-
}
|
| 1040 |
-
defaults = {
|
| 1041 |
-
"SANCTUARY": "SANCTUARY ACTIVE",
|
| 1042 |
-
"LABORATORY": "LAB ACTIVE",
|
| 1043 |
-
"COURTYARD": "SYSTEM CLEAR",
|
| 1044 |
-
"FORGE": f"FORGE ACTIVE ({getattr(physics, 'voltage', 0):.1f}v)",
|
| 1045 |
-
}
|
| 1046 |
-
lookup = {"LABORATORY": "LAB", "COURTYARD": "CLEAR"}.get(mode, mode)
|
| 1047 |
-
tmpl = text_map.get(lookup, defaults.get(mode, "MODE SHIFT"))
|
| 1048 |
-
try:
|
| 1049 |
-
return tmpl.format(
|
| 1050 |
-
color=colors.get(mode, Prisma.WHT),
|
| 1051 |
-
reset=Prisma.RST,
|
| 1052 |
-
volts=getattr(physics, "voltage", 0),
|
| 1053 |
-
beta=getattr(physics, "beta_index", 0),
|
| 1054 |
-
)
|
| 1055 |
-
except:
|
| 1056 |
-
return f"{colors.get(mode, '')}{defaults.get(mode)}{Prisma.RST}"
|
| 1057 |
-
|
| 1058 |
-
@dataclass
|
| 1059 |
-
class BiologicalImpulse:
|
| 1060 |
-
cortisol_delta: float = 0.0
|
| 1061 |
-
oxytocin_delta: float = 0.0
|
| 1062 |
-
dopamine_delta: float = 0.0
|
| 1063 |
-
adrenaline_delta: float = 0.0
|
| 1064 |
-
stamina_impact: float = 0.0
|
| 1065 |
-
somatic_reflex: str = ""
|
| 1066 |
-
|
| 1067 |
-
|
| 1068 |
-
@dataclass
|
| 1069 |
-
class Qualia:
|
| 1070 |
-
color_code: str
|
| 1071 |
-
somatic_sensation: str
|
| 1072 |
-
tone: str
|
| 1073 |
-
internal_monologue_hint: str
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
class SynestheticCortex:
|
| 1077 |
-
def __init__(self, bio_ref):
|
| 1078 |
-
self.bio = bio_ref
|
| 1079 |
-
self.last_reflex = None
|
| 1080 |
-
self.library = LoreManifest.get_instance().get("BIO_NARRATIVE") or {}
|
| 1081 |
-
|
| 1082 |
-
@staticmethod
|
| 1083 |
-
def _normalize_physics(physics) -> Dict:
|
| 1084 |
-
if isinstance(physics, dict):
|
| 1085 |
-
return physics
|
| 1086 |
-
if hasattr(physics, "to_dict"):
|
| 1087 |
-
return physics.to_dict()
|
| 1088 |
-
return getattr(physics, "__dict__", {})
|
| 1089 |
-
|
| 1090 |
-
def perceive(
|
| 1091 |
-
self, physics: Dict, traits: Any = None, latency: float = 0.0
|
| 1092 |
-
) -> BiologicalImpulse:
|
| 1093 |
-
physics = self._normalize_physics(physics)
|
| 1094 |
-
impulse = BiologicalImpulse()
|
| 1095 |
-
impulse.stamina_impact -= 1.0
|
| 1096 |
-
cortex_cfg = getattr(BoneConfig, "CORTEX", None)
|
| 1097 |
-
base_sens = getattr(cortex_cfg, "BASE_SENSITIVITY", 1.0) if cortex_cfg else 1.0
|
| 1098 |
-
if traits:
|
| 1099 |
-
curiosity = getattr(traits, "curiosity", 0.5)
|
| 1100 |
-
discipline = getattr(traits, "discipline", 0.5)
|
| 1101 |
-
base_sens *= 1.0 + curiosity - discipline
|
| 1102 |
-
sens = max(0.0, base_sens)
|
| 1103 |
-
valence = physics.get("valence", 0.0)
|
| 1104 |
-
counts = physics.get("counts", {})
|
| 1105 |
-
voltage = physics.get("voltage", 0)
|
| 1106 |
-
drag = physics.get("narrative_drag", 0)
|
| 1107 |
-
if drag > 3.0:
|
| 1108 |
-
impulse.stamina_impact -= drag * 0.4
|
| 1109 |
-
if valence < -0.5:
|
| 1110 |
-
impulse.cortisol_delta += abs(valence) * sens
|
| 1111 |
-
antigen_count = counts.get("antigen", 0)
|
| 1112 |
-
if antigen_count > 0:
|
| 1113 |
-
toxin_weight = getattr(BoneConfig, "TOXIN_WEIGHT", 1.0)
|
| 1114 |
-
toxin_scalar = getattr(cortex_cfg, "TOXIN_SCALAR", 0.5) if cortex_cfg else 0.5
|
| 1115 |
-
raw_tox = antigen_count * (toxin_weight * 0.2)
|
| 1116 |
-
impulse.cortisol_delta += min(toxin_scalar, raw_tox)
|
| 1117 |
-
impulse.somatic_reflex = "Shiver (Rejection)"
|
| 1118 |
-
elif drag > (getattr(cortex_cfg, "DRAG_STRESS_THRESHOLD", 8.0) if cortex_cfg else 8.0):
|
| 1119 |
-
impulse.cortisol_delta += 0.05
|
| 1120 |
-
impulse.stamina_impact -= 2.0
|
| 1121 |
-
else:
|
| 1122 |
-
if valence > 0.4:
|
| 1123 |
-
impulse.oxytocin_delta += valence * sens
|
| 1124 |
-
if counts.get("sacred", 0) > 0:
|
| 1125 |
-
impulse.oxytocin_delta += 0.1
|
| 1126 |
-
impulse.somatic_reflex = "Warmth (Resonance)"
|
| 1127 |
-
if counts.get("play", 0) > 0:
|
| 1128 |
-
play_boost = getattr(cortex_cfg, "DOPAMINE_PLAY_BOOST", 0.1) if cortex_cfg else 0.1
|
| 1129 |
-
impulse.dopamine_delta += play_boost
|
| 1130 |
-
impulse.stamina_impact += 1.0
|
| 1131 |
-
if voltage > 12.0 and physics.get("kappa", 0) > 0.5:
|
| 1132 |
-
impulse.dopamine_delta += 0.15
|
| 1133 |
-
impulse.somatic_reflex = "Buzz (Excitement)"
|
| 1134 |
-
|
| 1135 |
-
k_count = counts.get("kinetic", 0) + counts.get("explosive", 0)
|
| 1136 |
-
if k_count > 0:
|
| 1137 |
-
adr_scalar = getattr(cortex_cfg, "ADRENALINE_KINETIC_SCALAR", 0.1) if cortex_cfg else 0.1
|
| 1138 |
-
adr_boost = min(0.4, k_count * adr_scalar)
|
| 1139 |
-
impulse.adrenaline_delta += adr_boost
|
| 1140 |
-
impulse.cortisol_delta += 0.02
|
| 1141 |
-
impulse.stamina_impact -= 1.0
|
| 1142 |
-
|
| 1143 |
-
if voltage > (getattr(cortex_cfg, "VOLTAGE_ARC_TRIGGER", 18.0) if cortex_cfg else 18.0):
|
| 1144 |
-
impulse.adrenaline_delta += 0.2
|
| 1145 |
-
|
| 1146 |
-
if latency > (getattr(cortex_cfg, "LATENCY_PENALTY_THRESHOLD", 5.0) if cortex_cfg else 5.0):
|
| 1147 |
-
impulse.stamina_impact -= latency * 0.5
|
| 1148 |
-
impulse.cortisol_delta += 0.05
|
| 1149 |
-
impulse.somatic_reflex = "Time Dilation (Lag)."
|
| 1150 |
-
if not impulse.somatic_reflex:
|
| 1151 |
-
metaphors = self.library.get("METAPHOR_RESERVOIR", {})
|
| 1152 |
-
if drag > 5.0 and "HIGH_DRAG" in metaphors:
|
| 1153 |
-
impulse.somatic_reflex = random.choice(metaphors["HIGH_DRAG"])
|
| 1154 |
-
elif drag < 1.0 and "LOW_DRAG" in metaphors:
|
| 1155 |
-
impulse.somatic_reflex = random.choice(metaphors["LOW_DRAG"])
|
| 1156 |
-
if not impulse.somatic_reflex:
|
| 1157 |
-
impulse.somatic_reflex = self._derive_reflex(physics, impulse)
|
| 1158 |
-
self.last_reflex = impulse.somatic_reflex
|
| 1159 |
-
return impulse
|
| 1160 |
-
|
| 1161 |
-
def _derive_reflex(self, physics: Dict, impulse: BiologicalImpulse) -> str:
|
| 1162 |
-
if impulse.cortisol_delta > 0.1 and impulse.adrenaline_delta > 0.1: return "Trembling (Fight or Flight)."
|
| 1163 |
-
if impulse.dopamine_delta > 0.1 and impulse.adrenaline_delta > 0.1: return "Electric Vibration."
|
| 1164 |
-
if impulse.adrenaline_delta > 0.1: return "Pupils Dilating."
|
| 1165 |
-
if impulse.oxytocin_delta > 0.1 and impulse.dopamine_delta > 0.1: return "Golden Glow."
|
| 1166 |
-
if impulse.oxytocin_delta > 0.1: return "Chest Softening."
|
| 1167 |
-
if impulse.cortisol_delta > 0.1: return "Gut Tightening."
|
| 1168 |
-
if impulse.dopamine_delta > 0.1: return "Synaptic Spark."
|
| 1169 |
-
if physics.get("psi", 0.0) > 0.6: return "Scalp Prickling (Liminal)."
|
| 1170 |
-
if physics.get("entropy", 0.0) > 0.7: return "Skin Crawling (Static)."
|
| 1171 |
-
if physics.get("voltage", 0) > BoneConfig.CORTEX.VOLTAGE_ARC_TRIGGER: return "Electrical Arcing."
|
| 1172 |
-
if physics.get("voltage", 0) < 2.0: return "Metabolic Dimming."
|
| 1173 |
-
if physics.get("narrative_drag", 0) > 5.0: return "Shoulders Sagging."
|
| 1174 |
-
if self.last_reflex == "Steady Pulse.":
|
| 1175 |
-
return "..."
|
| 1176 |
-
return "Steady Pulse."
|
| 1177 |
-
|
| 1178 |
-
@staticmethod
|
| 1179 |
-
def get_current_qualia(impulse: Optional[BiologicalImpulse]) -> Qualia:
|
| 1180 |
-
if not impulse:
|
| 1181 |
-
return Qualia(Prisma.GRY, "Numbness", "Neutral", "The body is silent.")
|
| 1182 |
-
color = Prisma.GRY
|
| 1183 |
-
if impulse.cortisol_delta > 0.1:
|
| 1184 |
-
color = Prisma.OCHRE
|
| 1185 |
-
elif impulse.dopamine_delta > 0.1:
|
| 1186 |
-
color = Prisma.MAG
|
| 1187 |
-
elif impulse.oxytocin_delta > 0.1:
|
| 1188 |
-
color = Prisma.GRN
|
| 1189 |
-
elif impulse.adrenaline_delta > 0.1:
|
| 1190 |
-
color = Prisma.RED
|
| 1191 |
-
tone = "Steady"
|
| 1192 |
-
if impulse.adrenaline_delta > 0.2:
|
| 1193 |
-
tone = "Urgent"
|
| 1194 |
-
elif impulse.dopamine_delta > 0.2:
|
| 1195 |
-
tone = "Vibrating"
|
| 1196 |
-
elif impulse.cortisol_delta > 0.2:
|
| 1197 |
-
tone = "Strained"
|
| 1198 |
-
elif impulse.oxytocin_delta > 0.2:
|
| 1199 |
-
tone = "Resonant"
|
| 1200 |
-
hint = "Observe."
|
| 1201 |
-
if impulse.cortisol_delta > 0.05:
|
| 1202 |
-
hint = "Something is wrong. Be guarded."
|
| 1203 |
-
elif impulse.adrenaline_delta > 0.05:
|
| 1204 |
-
hint = "Move fast. Don't overthink."
|
| 1205 |
-
elif impulse.oxytocin_delta > 0.05:
|
| 1206 |
-
hint = "Connect. Be vulnerable."
|
| 1207 |
-
elif impulse.dopamine_delta > 0.05:
|
| 1208 |
-
hint = "Explore. Find the pattern."
|
| 1209 |
-
return Qualia(
|
| 1210 |
-
color_code=color,
|
| 1211 |
-
somatic_sensation=impulse.somatic_reflex or "Steady Pulse.",
|
| 1212 |
-
tone=tone,
|
| 1213 |
-
internal_monologue_hint=hint,
|
| 1214 |
-
)
|
| 1215 |
-
|
| 1216 |
-
def apply_impulse(self, impulse: BiologicalImpulse) -> float:
|
| 1217 |
-
if not self.bio or not hasattr(self.bio, "endo") or not self.bio.endo:
|
| 1218 |
-
return 0.0
|
| 1219 |
-
endo = self.bio.endo
|
| 1220 |
-
endo.cortisol = max(0.0, min(1.0, endo.cortisol + impulse.cortisol_delta))
|
| 1221 |
-
endo.oxytocin = max(0.0, min(1.0, endo.oxytocin + impulse.oxytocin_delta))
|
| 1222 |
-
endo.dopamine = max(0.0, min(1.0, endo.dopamine + impulse.dopamine_delta))
|
| 1223 |
-
endo.adrenaline = max(0.0, min(1.0, endo.adrenaline + impulse.adrenaline_delta))
|
| 1224 |
-
return impulse.stamina_impact
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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