codis 1.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. codis-1.1.1.dist-info/METADATA +194 -0
  2. codis-1.1.1.dist-info/RECORD +329 -0
  3. codis-1.1.1.dist-info/WHEEL +5 -0
  4. codis-1.1.1.dist-info/entry_points.txt +5 -0
  5. codis-1.1.1.dist-info/licenses/LICENSE +21 -0
  6. codis-1.1.1.dist-info/top_level.txt +2 -0
  7. run.py +190 -0
  8. src/__init__.py +1 -0
  9. src/__main__.py +81 -0
  10. src/_app_build.py +1507 -0
  11. src/_app_editor.py +1274 -0
  12. src/_app_kui.py +825 -0
  13. src/_app_lsp.py +625 -0
  14. src/_app_recovery.py +189 -0
  15. src/_app_settings.py +1385 -0
  16. src/_app_tools.py +2471 -0
  17. src/_app_webpreview.py +126 -0
  18. src/_font_safety.py +127 -0
  19. src/_shells.py +172 -0
  20. src/app.py +1488 -0
  21. src/chat/__init__.py +1 -0
  22. src/chat/adapters/__init__.py +33 -0
  23. src/chat/adapters/base.py +209 -0
  24. src/chat/adapters/discord.py +190 -0
  25. src/chat/adapters/http.py +78 -0
  26. src/chat/adapters/irc.py +360 -0
  27. src/chat/adapters/matrix.py +219 -0
  28. src/chat/adapters/mattermost.py +178 -0
  29. src/chat/adapters/mqtt.py +165 -0
  30. src/chat/adapters/slack.py +204 -0
  31. src/chat/adapters/telegram.py +117 -0
  32. src/chat/adapters/ws.py +154 -0
  33. src/chat/adapters/xmpp.py +337 -0
  34. src/chat/history.py +76 -0
  35. src/chat/models.py +207 -0
  36. src/chat/probe.py +79 -0
  37. src/chat/session.py +446 -0
  38. src/chat/util.py +125 -0
  39. src/collab/__init__.py +1 -0
  40. src/collab/net.py +411 -0
  41. src/collab/ot.py +311 -0
  42. src/collab/session.py +141 -0
  43. src/collab/tk_adapter.py +134 -0
  44. src/config.py +335 -0
  45. src/constants.py +2552 -0
  46. src/dep_catalog.py +446 -0
  47. src/diff/__init__.py +1 -0
  48. src/diff/engine.py +247 -0
  49. src/dpi.py +30 -0
  50. src/editor/__init__.py +1 -0
  51. src/editor/_pane_lsp.py +2937 -0
  52. src/editor/_pane_utils.py +171 -0
  53. src/editor/autocomplete.py +607 -0
  54. src/editor/code_folding.py +836 -0
  55. src/editor/color_preview.py +382 -0
  56. src/editor/complexity_lens.py +281 -0
  57. src/editor/diff_pane.py +623 -0
  58. src/editor/file_watcher.py +144 -0
  59. src/editor/focus_block.py +236 -0
  60. src/editor/heatmap.py +141 -0
  61. src/editor/inlay_hints.py +366 -0
  62. src/editor/inline_blame.py +429 -0
  63. src/editor/kdocs.py +3376 -0
  64. src/editor/large_file.py +488 -0
  65. src/editor/ligatures.py +180 -0
  66. src/editor/line_numbers.py +598 -0
  67. src/editor/minimap.py +691 -0
  68. src/editor/multi_cursor.py +269 -0
  69. src/editor/pane.py +2082 -0
  70. src/editor/rhythm.py +206 -0
  71. src/editor/save_hooks.py +43 -0
  72. src/editor/smart_selection.py +425 -0
  73. src/editor/smart_tips.py +719 -0
  74. src/editor/snippets.py +411 -0
  75. src/editor/sticky_note_gutter.py +410 -0
  76. src/editor/sticky_scroll.py +385 -0
  77. src/editor/suggest.py +464 -0
  78. src/editor/text_transforms.py +310 -0
  79. src/editor/vim_mode.py +1074 -0
  80. src/events/__init__.py +84 -0
  81. src/events/bus.py +427 -0
  82. src/events/correlator.py +244 -0
  83. src/events/kinds.py +125 -0
  84. src/events/pipeline.py +264 -0
  85. src/events/stream.py +416 -0
  86. src/events/window.py +204 -0
  87. src/extcompat/__init__.py +19 -0
  88. src/extcompat/code/__init__.py +13 -0
  89. src/extcompat/code/consent.py +162 -0
  90. src/extcompat/code/host.py +353 -0
  91. src/extcompat/code/hosts/py_host.py +763 -0
  92. src/extcompat/code/locate.py +105 -0
  93. src/extcompat/code/manager.py +325 -0
  94. src/extcompat/code/permissions.py +154 -0
  95. src/extcompat/code/sandbox.py +60 -0
  96. src/extcompat/colors.py +131 -0
  97. src/extcompat/common.py +184 -0
  98. src/extcompat/cson.py +269 -0
  99. src/extcompat/formats/__init__.py +22 -0
  100. src/extcompat/formats/_tm.py +189 -0
  101. src/extcompat/formats/_util.py +31 -0
  102. src/extcompat/formats/base16.py +66 -0
  103. src/extcompat/formats/brackets.py +172 -0
  104. src/extcompat/formats/eclipse.py +193 -0
  105. src/extcompat/formats/emacs.py +361 -0
  106. src/extcompat/formats/geany.py +147 -0
  107. src/extcompat/formats/generic.py +184 -0
  108. src/extcompat/formats/gtksource.py +117 -0
  109. src/extcompat/formats/helix.py +91 -0
  110. src/extcompat/formats/jetbrains.py +343 -0
  111. src/extcompat/formats/kate.py +130 -0
  112. src/extcompat/formats/micro.py +131 -0
  113. src/extcompat/formats/nano.py +111 -0
  114. src/extcompat/formats/notepadpp.py +140 -0
  115. src/extcompat/formats/pulsar.py +165 -0
  116. src/extcompat/formats/pyides.py +163 -0
  117. src/extcompat/formats/sublime.py +340 -0
  118. src/extcompat/jsonc.py +65 -0
  119. src/extcompat/lessish.py +128 -0
  120. src/extcompat/manager.py +122 -0
  121. src/extcompat/market/__init__.py +63 -0
  122. src/extcompat/market/sources.py +211 -0
  123. src/extcompat/model.py +88 -0
  124. src/extcompat/net.py +228 -0
  125. src/extcompat/openvsx.py +63 -0
  126. src/extcompat/readers.py +362 -0
  127. src/extcompat/registry.py +73 -0
  128. src/extcompat/safexml.py +63 -0
  129. src/extcompat/sexp.py +187 -0
  130. src/extcompat/sources.py +105 -0
  131. src/extcompat/store.py +276 -0
  132. src/extcompat/textmate.py +214 -0
  133. src/extcompat/themes.py +487 -0
  134. src/extcompat/yamlite.py +294 -0
  135. src/fileio.py +94 -0
  136. src/fs_ops.py +106 -0
  137. src/i18n.py +233 -0
  138. src/ia/__init__.py +1 -0
  139. src/ia/actions.py +295 -0
  140. src/ia/approval_gate.py +672 -0
  141. src/ia/checkpoints.py +308 -0
  142. src/ia/context_builder.py +921 -0
  143. src/ia/dialdiff.py +408 -0
  144. src/ia/iaspace.py +2078 -0
  145. src/ia/pair.py +296 -0
  146. src/ia/providers.py +954 -0
  147. src/ia/session.py +335 -0
  148. src/ia/toolbox.py +2524 -0
  149. src/launcher_actions.py +204 -0
  150. src/lint/__init__.py +1 -0
  151. src/lint/manager.py +1058 -0
  152. src/locales/.translate_manifest.json +64170 -0
  153. src/locales/de.json +4936 -0
  154. src/locales/en.json +4936 -0
  155. src/locales/es.json +4936 -0
  156. src/locales/fr.json +4936 -0
  157. src/locales/it.json +4936 -0
  158. src/locales/ja.json +4936 -0
  159. src/locales/ko.json +4936 -0
  160. src/locales/nl.json +4936 -0
  161. src/locales/pl.json +4936 -0
  162. src/locales/pt-BR.json +4936 -0
  163. src/locales/ru.json +4936 -0
  164. src/locales/tr.json +4936 -0
  165. src/locales/uk.json +4936 -0
  166. src/locales/zh-CN.json +4936 -0
  167. src/lsp/__init__.py +26 -0
  168. src/lsp/client.py +2300 -0
  169. src/lsp/dap_client.py +480 -0
  170. src/lsp/edits.py +84 -0
  171. src/lsp/framing.py +73 -0
  172. src/lsp/protocol.py +931 -0
  173. src/main.py +260 -0
  174. src/memtrim.py +91 -0
  175. src/plugins/__init__.py +1 -0
  176. src/plugins/api.py +1761 -0
  177. src/plugins/loader.py +571 -0
  178. src/plugins/process_runner.py +519 -0
  179. src/plugins/registry.py +342 -0
  180. src/plugins/safety.py +367 -0
  181. src/plugins/startup.py +195 -0
  182. src/plugins/store.py +1178 -0
  183. src/proc_registry.py +278 -0
  184. src/pyexec.py +394 -0
  185. src/recovery.py +259 -0
  186. src/stall_trace.py +92 -0
  187. src/standalone.py +1070 -0
  188. src/startup_profile.py +35 -0
  189. src/syntax/__init__.py +1 -0
  190. src/syntax/highlighter.py +776 -0
  191. src/syntax/patterns.py +584 -0
  192. src/system_profile.py +430 -0
  193. src/terminal/backends.py +658 -0
  194. src/terminal/internal.py +346 -0
  195. src/terminal/keys.py +138 -0
  196. src/terminal/palette.py +67 -0
  197. src/terminal/session.py +283 -0
  198. src/terminal/shells.py +79 -0
  199. src/terminal/view.py +489 -0
  200. src/terminal/vt.py +870 -0
  201. src/textfmt.py +22 -0
  202. src/theme/__init__.py +1 -0
  203. src/theme/base.py +1281 -0
  204. src/theme/colors.py +49 -0
  205. src/theme/manager.py +602 -0
  206. src/theme/utils.py +312 -0
  207. src/toolbox.py +329 -0
  208. src/trash_core.py +531 -0
  209. src/ui/__init__.py +1 -0
  210. src/ui/_perf_utils.py +285 -0
  211. src/ui/_persistence_utils.py +93 -0
  212. src/ui/accessibility.py +843 -0
  213. src/ui/activity_bar.py +860 -0
  214. src/ui/app_embed.py +434 -0
  215. src/ui/archive_panel.py +608 -0
  216. src/ui/bookmarks.py +314 -0
  217. src/ui/breadcrumb.py +383 -0
  218. src/ui/calculator_panel.py +982 -0
  219. src/ui/calendar_panel.py +1218 -0
  220. src/ui/chat_panel.py +1271 -0
  221. src/ui/chat_profile_dialog.py +278 -0
  222. src/ui/chat_render.py +128 -0
  223. src/ui/clock_panel.py +810 -0
  224. src/ui/code_outline.py +364 -0
  225. src/ui/code_runner.py +729 -0
  226. src/ui/code_stats.py +546 -0
  227. src/ui/collab_panel.py +1009 -0
  228. src/ui/color_picker.py +547 -0
  229. src/ui/command_palette.py +452 -0
  230. src/ui/cursor_manager.py +750 -0
  231. src/ui/custom_keywords_panel.py +413 -0
  232. src/ui/debug_panel.py +1037 -0
  233. src/ui/dependency_graph.py +692 -0
  234. src/ui/diff_panel.py +381 -0
  235. src/ui/docs_panel.py +514 -0
  236. src/ui/doom_embed.py +349 -0
  237. src/ui/dropdown.py +321 -0
  238. src/ui/easter_eggs.py +979 -0
  239. src/ui/env_manager.py +666 -0
  240. src/ui/extcode_bridge.py +215 -0
  241. src/ui/extcode_dialogs.py +412 -0
  242. src/ui/extcompat_dialog.py +583 -0
  243. src/ui/file_timeline.py +319 -0
  244. src/ui/file_tree.py +1208 -0
  245. src/ui/find_in_files.py +1355 -0
  246. src/ui/flowchart_panel.py +727 -0
  247. src/ui/formatter_panel.py +490 -0
  248. src/ui/git_auth.py +763 -0
  249. src/ui/git_clone_dialog.py +487 -0
  250. src/ui/git_panel.py +1483 -0
  251. src/ui/grab_safe.py +84 -0
  252. src/ui/internal_python_dialog.py +98 -0
  253. src/ui/k_ui/__init__.py +1305 -0
  254. src/ui/k_ui/_bgrenderer.py +317 -0
  255. src/ui/k_ui/_colors.py +213 -0
  256. src/ui/k_ui/_dock.py +545 -0
  257. src/ui/k_ui/_easter.py +290 -0
  258. src/ui/k_ui/_engine.py +5448 -0
  259. src/ui/k_ui/_perf.py +333 -0
  260. src/ui/k_ui/_tkutil.py +7 -0
  261. src/ui/k_ui/_tooltip.py +365 -0
  262. src/ui/k_ui/_widgets.py +1204 -0
  263. src/ui/k_ui/_world.py +837 -0
  264. src/ui/key_remapper.py +856 -0
  265. src/ui/launcher.py +1252 -0
  266. src/ui/lazy_panel.py +73 -0
  267. src/ui/macro_recorder.py +610 -0
  268. src/ui/menu_bar.py +898 -0
  269. src/ui/output_panel.py +321 -0
  270. src/ui/paint_canvas.py +1000 -0
  271. src/ui/paint_engine.py +535 -0
  272. src/ui/paint_panel.py +888 -0
  273. src/ui/panel_detach.py +112 -0
  274. src/ui/performance_panel.py +861 -0
  275. src/ui/pip_manager.py +1758 -0
  276. src/ui/plugin_panel.py +1586 -0
  277. src/ui/pomodoro_panel.py +447 -0
  278. src/ui/problems.py +566 -0
  279. src/ui/process_manager.py +1657 -0
  280. src/ui/project_templates.py +775 -0
  281. src/ui/recovery_dialog.py +122 -0
  282. src/ui/regex_playground.py +304 -0
  283. src/ui/remote_adapters.py +1398 -0
  284. src/ui/remote_browser.py +1224 -0
  285. src/ui/remote_profile_dialog.py +342 -0
  286. src/ui/render_panel.py +1293 -0
  287. src/ui/rest_client.py +549 -0
  288. src/ui/scratchpad.py +548 -0
  289. src/ui/search_bar.py +1274 -0
  290. src/ui/settings_editor.py +3349 -0
  291. src/ui/settings_window.py +1554 -0
  292. src/ui/snippets_panel.py +597 -0
  293. src/ui/sql_studio.py +1815 -0
  294. src/ui/status_bar.py +438 -0
  295. src/ui/tab_bar.py +654 -0
  296. src/ui/task_manager.py +1773 -0
  297. src/ui/terminal.py +525 -0
  298. src/ui/theme_preview_panel.py +851 -0
  299. src/ui/thread_safe_tk.py +152 -0
  300. src/ui/time_tracker.py +574 -0
  301. src/ui/tkbind.py +43 -0
  302. src/ui/todo_panel.py +358 -0
  303. src/ui/toolbox_panels.py +658 -0
  304. src/ui/trash_manager.py +657 -0
  305. src/ui/ui_scale.py +294 -0
  306. src/ui/ux_enhancements.py +41 -0
  307. src/ui/web_preview.py +397 -0
  308. src/ui/webdev.py +630 -0
  309. src/ui/welcome.py +567 -0
  310. src/ui/window_embed.py +1500 -0
  311. src/ui/wizard.py +1550 -0
  312. src/ui/workspace_switcher.py +418 -0
  313. src/ui/workspace_trust.py +370 -0
  314. src/ui/zen_audio.py +354 -0
  315. src/ui/zen_media.py +2271 -0
  316. src/ui/zen_media_client.py +303 -0
  317. src/ui/zen_media_host.py +329 -0
  318. src/ui/zen_player.py +907 -0
  319. src/ui/zen_player_widgets.py +700 -0
  320. src/updater/__init__.py +11 -0
  321. src/updater/assets.py +145 -0
  322. src/updater/backup.py +101 -0
  323. src/updater/channel.py +221 -0
  324. src/updater/dialog.py +492 -0
  325. src/updater/integrity.py +111 -0
  326. src/updater/manager.py +727 -0
  327. src/updater/result.py +114 -0
  328. src/venv_runtime.py +531 -0
  329. src/workspace_state.py +25 -0
src/ui/k_ui/_world.py ADDED
@@ -0,0 +1,837 @@
1
+ """
2
+ K-UI - CosmosWorld: o "sistema nervoso" do cosmos.
3
+
4
+ Cada fenômeno do cosmos (meteoros, cometas, buracos negros, supernovas,
5
+ pulsares, aliens...) nasce por timer e se move sozinho no seu próprio loop de
6
+ desenho. Este módulo é a camada que faltava: ele roda **antes** do desenho de
7
+ cada frame, lê o estado de todas as entidades e faz com que elas se
8
+ influenciem de forma causal e coerente:
9
+
10
+ * **Gravidade unificada** - Sol, buracos negros, binárias e pulsares curvam
11
+ cometas, meteoros, detritos e satélites (cometas ganham órbitas e
12
+ estilingues gravitacionais).
13
+ * **Colisões com consequência** - impacto no Sol/planeta gera faíscas,
14
+ fragmentos, anel de choque e erupção solar; buracos negros acretam massa
15
+ (e crescem) com jatos e espirais de acreção.
16
+ * **Ondas de choque** - supernovas, ondas gravitacionais, impactos e o salvar
17
+ de um arquivo propagam frentes que empurram partículas, detritos, cometas e
18
+ nebulosas, assustam aliens e fazem as estrelas cintilarem.
19
+ * **Ciclo de vida estelar** - supernova -> remanescente (pulsar ou nebulosa)
20
+ -> compressão da nuvem -> nascimento de estrela.
21
+ * **Farol de pulsar** - o feixe ilumina o que atravessa (planetas, aliens,
22
+ cometas), e aliens "recebem o sinal".
23
+ * **Cometas realistas** - a cauda aponta para longe do Sol e cresce com a
24
+ proximidade; perto demais, o cometa sublima e se desintegra.
25
+ * **Personalidade** - aliens ficam curiosos com o cursor quando você está
26
+ calmo e fogem de supernovas; o Sol reage à digitação (vento solar) e aos
27
+ avisos/erros do editor (erupções e tempestades).
28
+
29
+ O kernel não conhece Tk: opera sobre as listas de dicts do engine, o que o
30
+ torna testável sem display. O desenho dos efeitos próprios fica em
31
+ :meth:`CosmosWorld.draw`.
32
+ """
33
+
34
+ from __future__ import annotations
35
+
36
+ import math
37
+ import random
38
+ from collections import deque
39
+ from collections.abc import Callable
40
+ from typing import Any
41
+
42
+ __all__ = ["CosmosWorld", "Fx", "Front"]
43
+
44
+ _TAU = math.tau
45
+
46
+ SUN_GM = 675.0
47
+ _SOFT2 = 625.0
48
+ _A_CAP = 0.5
49
+
50
+
51
+ class Fx:
52
+ """Efeito visual leve pertencente ao mundo (faísca, anel, jato...)."""
53
+
54
+ __slots__ = ("kind", "x", "y", "vx", "vy", "r", "life", "decay", "col", "a", "b", "c")
55
+
56
+ def __init__(
57
+ self, kind: str, x: float, y: float, *, vx: float = 0.0, vy: float = 0.0,
58
+ r: float = 1.0, life: float = 1.0, decay: float = 0.03, col: str = "accent2",
59
+ a: float = 0.0, b: float = 0.0, c: float = 0.0,
60
+ ) -> None:
61
+ self.kind = kind
62
+ self.x, self.y, self.vx, self.vy = x, y, vx, vy
63
+ self.r, self.life, self.decay, self.col = r, life, decay, col
64
+ self.a, self.b, self.c = a, b, c
65
+
66
+
67
+ class Front:
68
+ """Frente de onda expansiva (impacto, nova, tempestade solar, clique)."""
69
+
70
+ __slots__ = ("x", "y", "r", "vr", "width", "strength", "max_r", "kind", "col")
71
+
72
+ def __init__(
73
+ self, x: float, y: float, strength: float, max_r: float, *,
74
+ vr: float = 3.2, width: float = 26.0, kind: str = "wave", col: str = "accent2",
75
+ ) -> None:
76
+ self.x, self.y = x, y
77
+ self.r = 4.0
78
+ self.vr, self.width, self.strength = vr, width, strength
79
+ self.max_r, self.kind, self.col = max_r, kind, col
80
+
81
+
82
+ class _Well:
83
+ """Poço gravitacional / corpo de colisão."""
84
+
85
+ __slots__ = ("x", "y", "gm", "kill", "kind", "ref")
86
+
87
+ def __init__(self, x: float, y: float, gm: float, kill: float, kind: str, ref: Any) -> None:
88
+ self.x, self.y, self.gm, self.kill, self.kind, self.ref = x, y, gm, kill, kind, ref
89
+
90
+
91
+ class CosmosWorld:
92
+ """Simulação de interações do cosmos (ver docstring do módulo)."""
93
+
94
+ MAX_FX = 240
95
+ MAX_FRONTS = 12
96
+
97
+ def __init__(self, seed: int | None = None) -> None:
98
+ self.rng = random.Random(seed)
99
+ self.t = 0.0
100
+ self.W = 1200
101
+ self.H = 800
102
+ self.fx: list[Fx] = []
103
+ self.fronts: list[Front] = []
104
+ self.excite: list[tuple[float, float, float, float, float]] = []
105
+ self.mood = 0.0
106
+ self.stress = 0.0
107
+ self.log: deque[tuple[str, dict[str, Any]]] = deque(maxlen=96)
108
+ self.stats: dict[str, int] = {}
109
+ self._listeners: dict[str, list[Callable[..., None]]] = {}
110
+
111
+ self.cursor = {"x": 0.0, "y": 0.0, "vx": 0.0, "vy": 0.0, "on": False,
112
+ "moved": -1e9, "still": 0.0}
113
+ self._sn_seen: dict[int, dict] = {}
114
+ self._sb_seen: dict[int, dict] = {}
115
+ self._pending_births: list[list[Any]] = []
116
+ self._lit: dict[tuple[int, int], float] = {}
117
+ self._flare_cd = 120.0
118
+ self._open_comet_t = -1e9
119
+ self._key_t = -1e9
120
+ self._curious: set[int] = set()
121
+ self._want_comet = False
122
+
123
+ def on(self, name: str, fn: Callable[..., None]) -> None:
124
+ """Registra um listener para um evento do cosmos (ex.: ``"impact"``)."""
125
+ self._listeners.setdefault(name, []).append(fn)
126
+
127
+ def emit(self, name: str, **data: Any) -> None:
128
+ """Registra um evento no log, conta nas estatísticas e notifica listeners."""
129
+ self.stats[name] = self.stats.get(name, 0) + 1
130
+ self.log.append((name, data))
131
+ for fn in self._listeners.get(name, ()):
132
+ fn(**data)
133
+
134
+ def reset(self) -> None:
135
+ """Esquece todo o estado transitório (ex.: após um Big Bang)."""
136
+ self.fx.clear()
137
+ self.fronts.clear()
138
+ self.excite = []
139
+ self._sn_seen.clear()
140
+ self._sb_seen.clear()
141
+ self._pending_births.clear()
142
+ self._lit.clear()
143
+ self._curious.clear()
144
+ self._want_comet = False
145
+ self.emit("reset")
146
+
147
+ def cursor_move(self, x: float, y: float) -> None:
148
+ """Informa a posição do mouse sobre o cosmos."""
149
+ c = self.cursor
150
+ if c["on"]:
151
+ c["vx"] = c["vx"] * 0.6 + (x - c["x"]) * 0.4
152
+ c["vy"] = c["vy"] * 0.6 + (y - c["y"]) * 0.4
153
+ c["x"], c["y"], c["on"] = float(x), float(y), True
154
+ c["moved"] = self.t
155
+ c["still"] = 0.0
156
+
157
+ def cursor_leave(self) -> None:
158
+ """O mouse saiu do cosmos."""
159
+ self.cursor["on"] = False
160
+
161
+ def cursor_click(self, x: float, y: float) -> None:
162
+ """Clique no espaço vazio: ondulação que empurra poeira e partículas."""
163
+ self._add_front(Front(x, y, 0.9, 260, vr=4.5, width=22, kind="ripple", col="accent2"))
164
+ self._sparks(x, y, 7, 2.4, ("accent2", "aur_a", "star3"))
165
+ self.emit("ripple", x=x, y=y)
166
+
167
+ def on_event(self, name: str, **kw: Any) -> None:
168
+ """Eventos do editor que viram acontecimentos no cosmos.
169
+
170
+ ``save`` -> nova solar; ``open`` -> um cometa chega; ``close`` ->
171
+ pequena onda; ``keystroke`` -> vento solar.
172
+ """
173
+ sun = self._sun_pos(kw.get("eng"))
174
+ if name == "save":
175
+ sx, sy = sun
176
+ self._add_front(Front(sx, sy, 0.45, 680, vr=6.0, width=34, kind="nova", col="accent"))
177
+ self._ring(sx, sy, 10, 2.8, "accent", life=1.0, decay=0.018, w=2)
178
+ self._sparks(sx, sy, 14, 3.2, ("accent", "accent2", "star3"))
179
+ self.mood *= 0.7
180
+ self.emit("nova")
181
+ elif name == "open":
182
+ if self.t - self._open_comet_t > 160:
183
+ self._open_comet_t = self.t
184
+ self.emit("comet_arrival")
185
+ self._want_comet = True
186
+ elif name == "close":
187
+ sx, sy = sun
188
+ self._ring(sx, sy, 8, 1.8, "quantum", life=0.8, decay=0.03)
189
+ self.emit("dim")
190
+ elif name == "keystroke" and self.t - self._key_t > 2.5:
191
+ self._key_t = self.t
192
+ sx, sy = sun
193
+ for _ in range(self.rng.randint(1, 2)):
194
+ a = self.rng.uniform(0, _TAU)
195
+ sp = self.rng.uniform(1.6, 2.8)
196
+ self._add(Fx("wind", sx + math.cos(a) * 10, sy + math.sin(a) * 10,
197
+ vx=math.cos(a) * sp, vy=math.sin(a) * sp, r=1.6,
198
+ life=1.0, decay=0.014, col="accent2"))
199
+
200
+ def step(self, eng: Any, dt: float, W: int, H: int) -> None:
201
+ """Avança as interações um frame (``dt`` em frames de referência)."""
202
+ dt = max(0.1, min(float(dt), 3.0))
203
+ self.t += dt
204
+ self.W, self.H = W, H
205
+ self.excite = []
206
+ self._update_mood(eng, dt)
207
+ sun = self._sun(eng, W, H)
208
+ wells = self._wells(eng, sun)
209
+ fronts = self._collect_fronts(eng)
210
+ self._gravity(eng, wells, dt)
211
+ self._solar_wind(eng, sun, dt)
212
+ self._push_by_fronts(eng, fronts, dt)
213
+ self._cursor_forces(eng, dt)
214
+ self._collisions(eng, sun, wells)
215
+ self._comet_physiology(eng, sun, dt)
216
+ self._lifecycle(eng, fronts, dt)
217
+ self._lighthouse(eng, dt)
218
+ self._alien_psychology(eng, fronts, dt)
219
+ self._flares(eng, sun, dt)
220
+ self._spawn_requested_comet(eng, sun)
221
+ self._advance(dt)
222
+
223
+ def _update_mood(self, eng: Any, dt: float) -> None:
224
+ kpm = float(getattr(eng, "_metric_kpm", 0.0))
225
+ target = min(1.0, kpm / 260.0)
226
+ self.mood += (target - self.mood) * min(1.0, 0.02 * dt)
227
+ warns = int(getattr(eng, "_metric_warnings", 0))
228
+ errs = int(getattr(eng, "_metric_errors", 0))
229
+ st = min(1.0, warns * 0.12 + errs * 0.25)
230
+ self.stress += (st - self.stress) * min(1.0, 0.02 * dt)
231
+
232
+ def _sun_pos(self, eng: Any) -> tuple[float, float]:
233
+ if eng is not None and getattr(eng, "_cfg", {}).get("solar_system"):
234
+ return self.W * eng._solar_cx, self.H * eng._solar_cy
235
+ return self.W * 0.5, self.H * 0.5
236
+
237
+ def _sun(self, eng: Any, W: int, H: int) -> _Well | None:
238
+ if not getattr(eng, "_cfg", {}).get("solar_system"):
239
+ return None
240
+ return _Well(W * eng._solar_cx, H * eng._solar_cy, SUN_GM, 11.0, "sun", None)
241
+
242
+ def _wells(self, eng: Any, sun: _Well | None) -> list[_Well]:
243
+ wells: list[_Well] = [sun] if sun else []
244
+ for bh in getattr(eng, "_black_holes", ()):
245
+ if bh.get("alpha", 0.0) < 0.25 or bh.get("death_mode"):
246
+ continue
247
+ wells.append(_Well(bh["x"], bh["y"], 55.0 * bh["r"] * bh["mass"],
248
+ bh["r"] * 1.3, "bh", bh))
249
+ for b in getattr(eng, "_binary_stars", ()):
250
+ wells.append(_Well(b["cx"], b["cy"], 45.0 * (b["r_a"] + b["r_b"]), 0.0, "binary", b))
251
+ for ps in getattr(eng, "_pulsars", ()):
252
+ wells.append(_Well(ps["x"], ps["y"], 70.0, 0.0, "pulsar", ps))
253
+ return wells
254
+
255
+ def _accel(self, e: dict, wells: list[_Well], dt: float, k: float, vx: str, vy: str,
256
+ kinds: tuple[str, ...]) -> None:
257
+ ex, ey = e["x"], e["y"]
258
+ for w in wells:
259
+ if w.kind not in kinds:
260
+ continue
261
+ dx, dy = w.x - ex, w.y - ey
262
+ d2 = dx * dx + dy * dy
263
+ if d2 > 640_000:
264
+ continue
265
+ d = math.sqrt(d2) or 1.0
266
+ a = min(_A_CAP, w.gm * k / (d2 + _SOFT2)) * dt
267
+ e[vx] += dx / d * a
268
+ e[vy] += dy / d * a
269
+
270
+ def _gravity(self, eng: Any, wells: list[_Well], dt: float) -> None:
271
+ if not wells:
272
+ return
273
+ every = ("sun", "bh", "binary", "pulsar")
274
+ for c in getattr(eng, "_comets", ()):
275
+ self._accel(c, wells, dt, 1.0, "vx", "vy", every)
276
+ sp = math.hypot(c["vx"], c["vy"])
277
+ if sp > 9.0:
278
+ c["vx"] *= 9.0 / sp
279
+ c["vy"] *= 9.0 / sp
280
+ for d in getattr(eng, "_debris", ()):
281
+ self._accel(d, wells, dt, 1.0, "vx", "vy", every)
282
+ sp = math.hypot(d["vx"], d["vy"])
283
+ if sp > 3.0:
284
+ d["vx"] *= 3.0 / sp
285
+ d["vy"] *= 3.0 / sp
286
+ for m in getattr(eng, "_meteors", ()):
287
+ self._accel(m, wells, dt, 0.8, "vx_extra", "vy_extra", ("sun", "binary", "pulsar"))
288
+ for s in getattr(eng, "_satellites", ()):
289
+ self._accel(s, wells, dt, 0.5, "vx", "vy", ("sun", "binary", "pulsar"))
290
+ for n in getattr(eng, "_nebula_clouds", ()):
291
+ self._accel(n, wells, dt, 0.04, "vx", "vy", every)
292
+ for p in getattr(eng, "_particles", ()):
293
+ self._accel(p, wells, dt, 0.5, "vx", "vy", ("bh",))
294
+
295
+ def _solar_wind(self, eng: Any, sun: _Well | None, dt: float) -> None:
296
+ if sun is None:
297
+ return
298
+ gust = (0.5 + 1.3 * self.mood + 0.8 * self.stress) * 0.006 * dt
299
+ for p in getattr(eng, "_particles", ()):
300
+ dx, dy = p["x"] - sun.x, p["y"] - sun.y
301
+ d2 = dx * dx + dy * dy
302
+ if d2 > 90_000 or d2 < 1:
303
+ continue
304
+ d = math.sqrt(d2)
305
+ f = gust * (1.0 - d / 300.0)
306
+ p["vx"] += dx / d * f
307
+ p["vy"] += dy / d * f
308
+ for n in getattr(eng, "_nebula_clouds", ()):
309
+ dx, dy = n["x"] - sun.x, n["y"] - sun.y
310
+ d = math.hypot(dx, dy) or 1.0
311
+ if d < 420:
312
+ n["vx"] += dx / d * gust * 0.04
313
+ n["vy"] += dy / d * gust * 0.04
314
+
315
+ def _add_front(self, f: Front) -> None:
316
+ if len(self.fronts) >= self.MAX_FRONTS:
317
+ self.fronts.pop(0)
318
+ self.fronts.append(f)
319
+
320
+ def _collect_fronts(self, eng: Any) -> list[tuple[float, float, float, float, float, str]]:
321
+ out: list[tuple[float, float, float, float, float, str]] = []
322
+ for sn in getattr(eng, "_supernovas", ()):
323
+ r, mx = sn.get("shockwave_r", 0.0), sn.get("max_shock", 1.0)
324
+ if 0 < r < mx:
325
+ s = 2.4 * (1.0 - r / mx) ** 1.2
326
+ out.append((sn["x"], sn["y"], r, 30.0, s, "supernova"))
327
+ self.excite.append((sn["x"], sn["y"], r, 42.0, min(1.0, s * 0.45)))
328
+ for w in getattr(eng, "_grav_waves", ()):
329
+ a = w.get("alpha", 0.0)
330
+ if a > 0.05:
331
+ out.append((w["x"], w["y"], w["r"], 22.0, 0.35 * a, "gw"))
332
+ for f in self.fronts:
333
+ s = f.strength * (1.0 - f.r / f.max_r)
334
+ if s > 0.02:
335
+ out.append((f.x, f.y, f.r, f.width, s, f.kind))
336
+ self.excite.append((f.x, f.y, f.r, f.width * 1.4, min(1.0, s * 0.6)))
337
+ return out
338
+
339
+ def _push(self, ents: Any, fronts: list, k: float, dt: float, vx: str = "vx",
340
+ vy: str = "vy") -> None:
341
+ for e in ents:
342
+ ex, ey = e["x"], e["y"]
343
+ for fx, fy, fr, fw, fs, _kind in fronts:
344
+ dx, dy = ex - fx, ey - fy
345
+ if abs(dx) > fr + fw or abs(dy) > fr + fw:
346
+ continue
347
+ d = math.hypot(dx, dy) or 1.0
348
+ off = abs(d - fr)
349
+ if off >= fw:
350
+ continue
351
+ imp = fs * k * (1.0 - off / fw) * dt
352
+ e[vx] += dx / d * imp
353
+ e[vy] += dy / d * imp
354
+
355
+ def _push_by_fronts(self, eng: Any, fronts: list, dt: float) -> None:
356
+ if not fronts:
357
+ return
358
+ self._push(getattr(eng, "_particles", ()), fronts, 1.0, dt)
359
+ self._push(getattr(eng, "_debris", ()), fronts, 0.6, dt)
360
+ self._push(getattr(eng, "_comets", ()), fronts, 0.25, dt)
361
+ self._push(getattr(eng, "_meteors", ()), fronts, 0.35, dt, "vx_extra", "vy_extra")
362
+ self._push(getattr(eng, "_nebula_clouds", ()), fronts, 0.5, dt)
363
+ self._push(getattr(eng, "_satellites", ()), fronts, 0.15, dt)
364
+ for n in getattr(eng, "_nebula_clouds", ()):
365
+ for fx, fy, fr, fw, _fs, kind in fronts:
366
+ if kind != "supernova":
367
+ continue
368
+ d = math.hypot(n["x"] - fx, n["y"] - fy)
369
+ if abs(d - fr) < fw and n.get("_sf", 0.0) <= self.t:
370
+ n["_sf"] = self.t + 900
371
+ n["life"] = min(1.0, n["life"] + 0.35)
372
+ self._pending_births.append([110.0, n["x"], n["y"], n.get("col1", "aur_a")])
373
+ self.emit("compression", x=n["x"], y=n["y"])
374
+
375
+ def _cursor_forces(self, eng: Any, dt: float) -> None:
376
+ c = self.cursor
377
+ if not c["on"] or self.t - c["moved"] > 360:
378
+ return
379
+ c["still"] = self.t - c["moved"]
380
+ cx, cy = c["x"], c["y"]
381
+ self.excite.append((cx, cy, 0.0, 70.0, 0.55))
382
+ hover = c["still"] > 50
383
+ for p in getattr(eng, "_particles", ()):
384
+ dx, dy = p["x"] - cx, p["y"] - cy
385
+ d2 = dx * dx + dy * dy
386
+ if d2 > 22_500 or d2 < 1:
387
+ continue
388
+ d = math.sqrt(d2)
389
+ f = 1.0 - d / 150.0
390
+ p["vx"] += c["vx"] * 0.02 * f * dt
391
+ p["vy"] += c["vy"] * 0.02 * f * dt
392
+ if hover:
393
+ a = min(0.04, 140.0 / (d2 + 900.0)) * dt
394
+ p["vx"] -= dx / d * a
395
+ p["vy"] -= dy / d * a
396
+ p["vx"] *= 0.985
397
+ p["vy"] *= 0.985
398
+ c["vx"] *= 0.9
399
+ c["vy"] *= 0.9
400
+
401
+ def _add(self, fx: Fx) -> None:
402
+ if len(self.fx) >= self.MAX_FX and fx.kind not in ("ring", "flare"):
403
+ return
404
+ self.fx.append(fx)
405
+
406
+ def _sparks(self, x: float, y: float, n: int, speed: float, cols: tuple[str, ...]) -> None:
407
+ for _ in range(n):
408
+ a = self.rng.uniform(0, _TAU)
409
+ sp = self.rng.uniform(0.35, 1.0) * speed
410
+ self._add(Fx("spark", x, y, vx=math.cos(a) * sp, vy=math.sin(a) * sp,
411
+ r=self.rng.uniform(0.8, 1.8), life=self.rng.uniform(0.6, 1.0),
412
+ decay=self.rng.uniform(0.02, 0.045), col=self.rng.choice(cols)))
413
+
414
+ def _ring(self, x: float, y: float, r0: float, grow: float, col: str, *,
415
+ life: float = 1.0, decay: float = 0.035, w: int = 1) -> None:
416
+ self._add(Fx("ring", x, y, r=r0, life=life, decay=decay, col=col, a=grow, b=float(w)))
417
+
418
+ def _streaks(self, x: float, y: float, n: int, rad: float, col: str) -> None:
419
+ for _ in range(n):
420
+ self._add(Fx("streak", x, y, r=rad * self.rng.uniform(0.7, 1.2),
421
+ life=1.0, decay=0.04, col=col, a=self.rng.uniform(0, _TAU),
422
+ b=self.rng.choice((-1.0, 1.0)) * self.rng.uniform(0.18, 0.32)))
423
+
424
+ def _fragments(self, eng: Any, x: float, y: float, n: int, speed: float) -> None:
425
+ deb = getattr(eng, "_debris", None)
426
+ if deb is None:
427
+ return
428
+ for _ in range(n):
429
+ if len(deb) >= 12:
430
+ break
431
+ a = self.rng.uniform(0, _TAU)
432
+ sp = self.rng.uniform(0.2, 1.0) * speed
433
+ deb.append({
434
+ "x": x, "y": y, "vx": math.cos(a) * sp, "vy": math.sin(a) * sp,
435
+ "rot": self.rng.uniform(0, _TAU), "rot_spd": self.rng.uniform(-0.05, 0.05),
436
+ "size": self.rng.uniform(1.5, 4.0),
437
+ "dtype": self.rng.choice(("bolt", "chip", "strut")),
438
+ "col": self.rng.choice(("fg_dim", "accent2", "star3")),
439
+ "life": self.rng.uniform(0.5, 0.9), "decay": 0.0006,
440
+ })
441
+
442
+ def _feed_bh(self, bh: dict, x: float, y: float, gain: float) -> None:
443
+ r0 = bh.setdefault("_r0", bh["r"])
444
+ bh["mass"] = min(4.0, bh["mass"] + gain)
445
+ bh["r"] = min(r0 * 1.25, bh["r"] + gain * 1.2)
446
+ bh["_fed"] = bh.get("_fed", 0) + 1
447
+ self._streaks(bh["x"], bh["y"], 5, bh["r"] * 3.2, "plasma")
448
+ self._ring(bh["x"], bh["y"], bh["r"], 1.1, "warn", life=0.7, decay=0.05)
449
+ self.emit("accrete", x=bh["x"], y=bh["y"], mass=bh["mass"])
450
+ if bh["_fed"] % 6 == 0:
451
+ self._add_front(Front(bh["x"], bh["y"], 0.5, 360, vr=4.0, width=24, kind="gw",
452
+ col="quantum"))
453
+
454
+ def _impact(self, eng: Any, x: float, y: float, what: str, power: float, col: str,
455
+ fragments: bool = True) -> None:
456
+ self._sparks(x, y, int(8 + 8 * power), 2.6 + power, (col, "warn", "accent2"))
457
+ self._ring(x, y, 3, 1.5 + power, col, life=1.0, decay=0.04, w=2)
458
+ self._add_front(Front(x, y, 0.35 + 0.35 * power, 90 + 80 * power, vr=3.2, width=18,
459
+ kind="impact", col=col))
460
+ if fragments:
461
+ self._fragments(eng, x, y, 1 + int(2 * power), 1.2)
462
+ self.emit("impact", x=x, y=y, target=what, power=power)
463
+
464
+ def _hit_test(self, eng: Any, e: dict, sun: _Well | None, wells: list[_Well],
465
+ planets: list[tuple[float, float, float, dict]], power: float,
466
+ gain: float) -> bool:
467
+ """True se ``e`` colidiu com algo (e deve ser removida)."""
468
+ ex, ey = e["x"], e["y"]
469
+ for w in wells:
470
+ if (w.kind == "bh" and w.kill > 0
471
+ and (ex - w.x) ** 2 + (ey - w.y) ** 2 < w.kill * w.kill):
472
+ self._feed_bh(w.ref, ex, ey, gain)
473
+ return True
474
+ if sun is not None and (ex - sun.x) ** 2 + (ey - sun.y) ** 2 < (sun.kill + 2) ** 2:
475
+ self._impact(eng, ex, ey, "sun", power, "warn", fragments=False)
476
+ self._prominence(sun.x, sun.y, math.atan2(ey - sun.y, ex - sun.x), 1.4)
477
+ return True
478
+ for px, py, pr, pl in planets:
479
+ if (ex - px) ** 2 + (ey - py) ** 2 < pr * pr:
480
+ self._impact(eng, ex, ey, pl.get("name", "planet"), power,
481
+ pl.get("col", "accent"))
482
+ return True
483
+ return False
484
+
485
+ def _collisions(self, eng: Any, sun: _Well | None, wells: list[_Well]) -> None:
486
+ planets = [(pl["_px"], pl["_py"], pl.get("radius", 4) + 2.0, pl)
487
+ for pl in getattr(eng, "_solar_planets", ())
488
+ if pl.get("_px", 0) or pl.get("_py", 0)]
489
+ for m in getattr(eng, "_meteors", ()):
490
+ if m.get("alive", True) and self._hit_test(eng, m, sun, wells, planets, 0.6, 0.03):
491
+ m["alive"] = False
492
+ comets = getattr(eng, "_comets", None)
493
+ if comets:
494
+ for c in comets:
495
+ if self._hit_test(eng, c, sun, wells, planets, 1.0, 0.08):
496
+ c["_dead"] = True
497
+ comets[:] = [c for c in comets if not c.get("_dead")]
498
+ deb = getattr(eng, "_debris", None)
499
+ if deb:
500
+ for d in deb:
501
+ if self._hit_test(eng, d, sun, wells, [], 0.3, 0.015):
502
+ d["_dead"] = True
503
+ deb[:] = [d for d in deb if not d.get("_dead")]
504
+ sats = getattr(eng, "_satellites", None)
505
+ if sats:
506
+ for s in sats:
507
+ if self._hit_test(eng, s, None, wells, [], 0.5, 0.02):
508
+ s["_dead"] = True
509
+ sats[:] = [s for s in sats if not s.get("_dead")]
510
+ for p in getattr(eng, "_particles", ()):
511
+ for w in wells:
512
+ if w.kind == "bh" and (p["x"] - w.x) ** 2 + (p["y"] - w.y) ** 2 < w.kill ** 2:
513
+ p["x"] = self.rng.choice((-3.0, self.W + 3.0))
514
+ p["y"] = self.rng.uniform(0, self.H)
515
+ p["vx"], p["vy"] = self.rng.uniform(-0.25, 0.25), self.rng.uniform(-0.5, -0.02)
516
+ if self.rng.random() < 0.15:
517
+ self._streaks(w.x, w.y, 1, w.ref["r"] * 2.5, "aur_a")
518
+ break
519
+
520
+ def _comet_physiology(self, eng: Any, sun: _Well | None, dt: float) -> None:
521
+ wind = 0.35 + 0.9 * self.mood + 0.7 * self.stress
522
+ for c in getattr(eng, "_comets", ()):
523
+ c["age"] = c.get("age", 0.0) + dt
524
+ c.setdefault("mass", 1.0)
525
+ vx, vy = c["vx"], c["vy"]
526
+ vn = math.hypot(vx, vy) or 1.0
527
+ tdx, tdy, scale = -vx / vn, -vy / vn, 1.0
528
+ if sun is not None:
529
+ dx, dy = c["x"] - sun.x, c["y"] - sun.y
530
+ d = math.hypot(dx, dy) or 1.0
531
+ w = max(0.0, min(1.0, 1.4 - d / 420.0))
532
+ tdx = (dx / d) * w + tdx * (1.0 - w)
533
+ tdy = (dy / d) * w + tdy * (1.0 - w)
534
+ tn = math.hypot(tdx, tdy) or 1.0
535
+ tdx, tdy = tdx / tn, tdy / tn
536
+ scale = max(0.45, min(2.3, 300.0 / max(d, 70.0))) * (0.7 + 0.5 * wind)
537
+ if d < 230:
538
+ c["mass"] -= 0.0007 * dt * (1.0 + (230.0 - d) / 60.0)
539
+ if self.rng.random() < 0.25 * dt and len(self.fx) < self.MAX_FX - 20:
540
+ self._add(Fx("dust", c["x"], c["y"], vx=tdx * 0.9 + self.rng.uniform(-.2, .2),
541
+ vy=tdy * 0.9 + self.rng.uniform(-.2, .2), r=1.2,
542
+ life=0.9, decay=0.03, col=c.get("col", "aur_a")))
543
+ if c["age"] > 6000:
544
+ c["mass"] -= 0.0004 * dt
545
+ c["tdx"], c["tdy"] = tdx, tdy
546
+ c["tlen"] = c.get("tail_len", 120) * scale * max(0.25, c["mass"])
547
+ c["flare"] = 1.0 + max(0.0, 1.0 - c["mass"]) * 0.8 + 0.25 * max(0.0, scale - 1.0)
548
+ if c["mass"] <= 0:
549
+ c["_dead"] = True
550
+ self._sparks(c["x"], c["y"], 10, 2.0, (c.get("col", "aur_a"), "star3"))
551
+ self._ring(c["x"], c["y"], 4, 1.6, c.get("col", "aur_a"), life=0.9, decay=0.035)
552
+ self._fragments(eng, c["x"], c["y"], 2, 0.8)
553
+ self.emit("comet_sublimated", x=c["x"], y=c["y"])
554
+ comets = getattr(eng, "_comets", None)
555
+ if comets:
556
+ comets[:] = [c for c in comets if not c.get("_dead")]
557
+
558
+ def _spawn_requested_comet(self, eng: Any, sun: _Well | None) -> None:
559
+ if not self._want_comet:
560
+ return
561
+ self._want_comet = False
562
+ comets = getattr(eng, "_comets", None)
563
+ if comets is None or len(comets) >= 3:
564
+ return
565
+ W, H = self.W, self.H
566
+ side = self.rng.randint(0, 3)
567
+ x = (-40.0, W + 40.0, self.rng.uniform(0, W), self.rng.uniform(0, W))[side]
568
+ y = (self.rng.uniform(0, H), self.rng.uniform(0, H), -40.0, H + 40.0)[side]
569
+ tx, ty = (sun.x, sun.y) if sun else (W / 2, H / 2)
570
+ tx += self.rng.uniform(-90, 90)
571
+ ty += self.rng.uniform(-90, 90)
572
+ d = math.hypot(tx - x, ty - y) or 1.0
573
+ sp = self.rng.uniform(1.8, 3.2)
574
+ comets.append({
575
+ "x": x, "y": y, "vx": (tx - x) / d * sp, "vy": (ty - y) / d * sp,
576
+ "tail_len": self.rng.randint(90, 190),
577
+ "col": self.rng.choice(("aur_a", "accent2", "star3", "quantum")),
578
+ "core_r": self.rng.uniform(2.8, 4.8), "ph": self.rng.uniform(0, _TAU),
579
+ })
580
+
581
+ def _new_pulsar(self, x: float, y: float) -> dict:
582
+ return {
583
+ "x": x, "y": y, "ph": self.rng.uniform(0, _TAU),
584
+ "period": self.rng.uniform(18, 40), "timer": 0.0,
585
+ "jet_ang": self.rng.uniform(0, _TAU), "jet_spd": self.rng.uniform(0.05, 0.12),
586
+ "radius": self.rng.uniform(2.8, 4.5),
587
+ "col": self.rng.choice(("quantum", "aur_a", "accent2", "star3")),
588
+ "life": 1.0, "decay": self.rng.uniform(0.0003, 0.0006), "pulse_alpha": 0.0,
589
+ }
590
+
591
+ def _new_nebula(self, x: float, y: float, col: str) -> dict:
592
+ return {
593
+ "x": x, "y": y, "rx": self.rng.uniform(70, 120), "ry": self.rng.uniform(45, 80),
594
+ "ph": self.rng.uniform(0, _TAU), "speed": self.rng.uniform(0.003, 0.008),
595
+ "vx": 0.0, "vy": 0.0, "col1": col, "col2": "accent2",
596
+ "life": 1.0, "decay": 0.0006,
597
+ }
598
+
599
+ def _new_birth(self, x: float, y: float, col: str) -> dict:
600
+ return {
601
+ "x": x, "y": y, "r": float(self.rng.uniform(9, 18)), "phase": 0.0,
602
+ "max_phase": self.rng.randint(200, 320), "col": col,
603
+ "cloud_r": float(self.rng.uniform(55, 95)), "_w": True,
604
+ }
605
+
606
+ def _lifecycle(self, eng: Any, fronts: list, dt: float) -> None:
607
+ sne = {id(s): s for s in getattr(eng, "_supernovas", ())}
608
+ for sid, s in sne.items():
609
+ if sid not in self._sn_seen:
610
+ self._sn_seen[sid] = s
611
+ self.emit("supernova", x=s["x"], y=s["y"])
612
+ for sid in [k for k in self._sn_seen if k not in sne]:
613
+ self._remnant(eng, self._sn_seen.pop(sid))
614
+
615
+ births = getattr(eng, "_star_birth", None)
616
+ if births is not None:
617
+ neb = [n for n in getattr(eng, "_nebula_clouds", ()) if n["life"] > 0.25]
618
+ for sb in births:
619
+ if sb.get("_w"):
620
+ continue
621
+ sb["_w"] = True
622
+ if neb:
623
+ n = self.rng.choice(neb)
624
+ sb["x"] = n["x"] + self.rng.uniform(-0.3, 0.3) * n["rx"]
625
+ sb["y"] = n["y"] + self.rng.uniform(-0.3, 0.3) * n["ry"]
626
+ sb["cloud_r"] = max(sb.get("cloud_r", 50.0), n["rx"] * 0.6)
627
+ cur = {id(b): b for b in births}
628
+ for bid, b in cur.items():
629
+ self._sb_seen.setdefault(bid, b)
630
+ for bid in [k for k in self._sb_seen if k not in cur]:
631
+ b = self._sb_seen.pop(bid)
632
+ self._ring(b["x"], b["y"], 3, 2.2, b.get("col", "star"), life=1.0, decay=0.03)
633
+ self._sparks(b["x"], b["y"], 9, 1.8, (b.get("col", "star"), "star3"))
634
+ self.excite.append((b["x"], b["y"], 0.0, 90.0, 0.8))
635
+ self.emit("star_born", x=b["x"], y=b["y"])
636
+ for pend in self._pending_births[:]:
637
+ pend[0] -= dt
638
+ if pend[0] <= 0:
639
+ self._pending_births.remove(pend)
640
+ if len(births) < 4:
641
+ births.append(self._new_birth(pend[1], pend[2], "star3"))
642
+
643
+ def _remnant(self, eng: Any, sn: dict) -> None:
644
+ x, y = sn["x"], sn["y"]
645
+ self._ring(x, y, 6, 2.4, sn.get("ejecta_col", "accent2"), life=1.0, decay=0.02, w=2)
646
+ pulsars = getattr(eng, "_pulsars", None)
647
+ neb = getattr(eng, "_nebula_clouds", None)
648
+ if self.rng.random() < 0.55 and pulsars is not None and len(pulsars) < 4:
649
+ pulsars.append(self._new_pulsar(x, y))
650
+ self.emit("remnant", kind="pulsar", x=x, y=y)
651
+ elif neb is not None and len(neb) < 6:
652
+ neb.append(self._new_nebula(x, y, sn.get("ejecta_col", "aur_a")))
653
+ self._pending_births.append([self.rng.uniform(220, 360), x, y, "star3"])
654
+ self.emit("remnant", kind="nebula", x=x, y=y)
655
+
656
+ def _lighthouse(self, eng: Any, dt: float) -> None:
657
+ pulsars = getattr(eng, "_pulsars", ())
658
+ if not pulsars:
659
+ return
660
+ targets: list[tuple[str, dict, float, float]] = []
661
+ for pl in getattr(eng, "_solar_planets", ()):
662
+ if pl.get("_px", 0) or pl.get("_py", 0):
663
+ targets.append(("planet", pl, pl["_px"], pl["_py"]))
664
+ for lst, tag in (("_aliens", "alien"), ("_comets", "comet"), ("_satellites", "sat")):
665
+ for e in getattr(eng, lst, ()):
666
+ targets.append((tag, e, e["x"], e["y"]))
667
+ for key in [k for k, v in self._lit.items() if v < self.t]:
668
+ del self._lit[key]
669
+ for ps in pulsars:
670
+ for tag, e, tx, ty in targets:
671
+ dx, dy = tx - ps["x"], ty - ps["y"]
672
+ d = math.hypot(dx, dy)
673
+ if d < 30 or d > 420:
674
+ continue
675
+ ang = math.atan2(dy, dx)
676
+ for beam in (ps["jet_ang"], ps["jet_ang"] + math.pi):
677
+ diff = abs((ang - beam + math.pi) % _TAU - math.pi)
678
+ if diff < 0.07:
679
+ key = (id(ps), id(e))
680
+ if key in self._lit:
681
+ break
682
+ self._lit[key] = self.t + 70
683
+ self._add(Fx("beam", ps["x"], ps["y"], r=0.0, life=1.0, decay=0.12,
684
+ col=ps.get("col", "quantum"), a=tx, b=ty))
685
+ self._ring(tx, ty, 3, 1.3, ps.get("col", "quantum"), life=0.9, decay=0.05)
686
+ if tag == "alien":
687
+ e["scanning"] = True
688
+ e["scan_timer"] = max(e.get("scan_timer", 0), 180)
689
+ self.emit("lit", target=tag)
690
+ break
691
+
692
+ def _alien_psychology(self, eng: Any, fronts: list, dt: float) -> None:
693
+ aliens = getattr(eng, "_aliens", ())
694
+ if not aliens:
695
+ return
696
+ c = self.cursor
697
+ curious = c["on"] and self.t - c["moved"] < 300 and self.mood < 0.5
698
+ for a in aliens:
699
+ for fx, fy, fr, fw, fs, kind in fronts:
700
+ if kind not in ("supernova", "cme", "impact") or fs < 0.5:
701
+ continue
702
+ dx, dy = a["x"] - fx, a["y"] - fy
703
+ d = math.hypot(dx, dy) or 1.0
704
+ if abs(d - fr) < fw * 2.5 and d < 520:
705
+ a["state"] = "flee"
706
+ a["flee_timer"] = 120
707
+ a["scanning"] = False
708
+ a["vx"] = a["vx"] * 0.6 + dx / d * 1.1
709
+ a["vy"] = a["vy"] * 0.6 + dy / d * 1.1
710
+ if curious and a.get("state", "travel") == "travel" and not a.get("scanning"):
711
+ dx, dy = c["x"] - a["x"], c["y"] - a["y"]
712
+ d = math.hypot(dx, dy) or 1.0
713
+ if 40 < d < 440:
714
+ k = 0.012 * dt if d > 150 else -0.004 * dt
715
+ a["vx"] += dx / d * k
716
+ a["vy"] += dy / d * k
717
+ if d < 160:
718
+ a["vx"] *= 0.97
719
+ a["vy"] *= 0.97
720
+ if id(a) not in self._curious:
721
+ self._curious.add(id(a))
722
+ self.emit("alien_curious")
723
+ sp = math.hypot(a["vx"], a["vy"])
724
+ if sp > 1.4:
725
+ a["vx"] *= 1.4 / sp
726
+ a["vy"] *= 1.4 / sp
727
+
728
+ def _prominence(self, sx: float, sy: float, ang: float, size: float) -> None:
729
+ col = "warn" if self.stress > 0.25 else "accent2"
730
+ self._add(Fx("flare", sx, sy, r=11.0, life=1.0, decay=0.018, col=col,
731
+ a=ang, b=size * self.rng.uniform(16, 30), c=self.rng.uniform(0.5, 0.9)))
732
+
733
+ def _flares(self, eng: Any, sun: _Well | None, dt: float) -> None:
734
+ if sun is None:
735
+ return
736
+ self._flare_cd -= dt
737
+ if self._flare_cd > 0:
738
+ return
739
+ self._flare_cd = max(25.0, 230.0 - 170.0 * self.mood - 130.0 * self.stress) * \
740
+ self.rng.uniform(0.6, 1.4)
741
+ ang = self.rng.uniform(0, _TAU)
742
+ self._prominence(sun.x, sun.y, ang, 1.0 + self.mood + self.stress)
743
+ for _ in range(3):
744
+ sp = self.rng.uniform(1.2, 2.4)
745
+ a2 = ang + self.rng.uniform(-0.25, 0.25)
746
+ self._add(Fx("wind", sun.x + math.cos(ang) * 11, sun.y + math.sin(ang) * 11,
747
+ vx=math.cos(a2) * sp, vy=math.sin(a2) * sp, r=1.5, life=1.0,
748
+ decay=0.012, col="warn" if self.stress > 0.25 else "accent2"))
749
+ self.emit("flare", stress=round(self.stress, 2))
750
+ if self.stress > 0.4 and self.rng.random() < 0.18:
751
+ self._add_front(Front(sun.x, sun.y, 0.9, 560, vr=4.2, width=30, kind="cme",
752
+ col="warn"))
753
+ self.emit("cme")
754
+
755
+ def _advance(self, dt: float) -> None:
756
+ for f in self.fronts:
757
+ f.r += f.vr * dt
758
+ self.fronts = [f for f in self.fronts if f.r < f.max_r]
759
+ drag = 0.96 ** dt
760
+ alive: list[Fx] = []
761
+ for fx in self.fx:
762
+ fx.life -= fx.decay * dt
763
+ if fx.life <= 0:
764
+ continue
765
+ k = fx.kind
766
+ if k in ("spark", "wind", "dust"):
767
+ fx.x += fx.vx * dt
768
+ fx.y += fx.vy * dt
769
+ if k == "spark":
770
+ fx.vx *= drag
771
+ fx.vy *= drag
772
+ elif k == "ring":
773
+ fx.r += fx.a * dt
774
+ elif k == "streak":
775
+ fx.a += fx.b * dt
776
+ fx.r *= 0.93 ** dt
777
+ if fx.r < 3:
778
+ continue
779
+ alive.append(fx)
780
+ self.fx = alive
781
+
782
+ def draw(self, eng: Any, W: int, H: int) -> None:
783
+ """Desenha efeitos do mundo (usa o canvas e a paleta do ``eng``)."""
784
+ from ._colors import _lc_q
785
+
786
+ p = eng._p
787
+ void = p["void"]
788
+ oval, line = eng.create_oval, eng.create_line
789
+ for f in self.fronts:
790
+ s = f.strength * (1.0 - f.r / f.max_r)
791
+ if s > 0.06:
792
+ r = int(f.r)
793
+ oval(int(f.x) - r, int(f.y) - r, int(f.x) + r, int(f.y) + r, fill="",
794
+ outline=_lc_q(void, p.get(f.col, p["accent2"]), min(0.5, s * 0.55)), width=1)
795
+ cos, sin = math.cos, math.sin
796
+ for fx in self.fx:
797
+ col = p.get(fx.col, p["accent2"])
798
+ al = max(0.0, min(1.0, fx.life))
799
+ x, y = fx.x, fx.y
800
+ k = fx.kind
801
+ if k == "spark":
802
+ line(int(x), int(y), int(x - fx.vx * 2.2), int(y - fx.vy * 2.2),
803
+ fill=_lc_q(void, col, al), width=1)
804
+ elif k in ("wind", "dust"):
805
+ r = max(1, int(fx.r * (0.5 + al * 0.5)))
806
+ oval(int(x) - r, int(y) - r, int(x) + r, int(y) + r,
807
+ fill=_lc_q(void, col, al * (0.7 if k == "wind" else 0.4)), outline="")
808
+ elif k == "ring":
809
+ r = int(fx.r)
810
+ oval(int(x) - r, int(y) - r, int(x) + r, int(y) + r, fill="",
811
+ outline=_lc_q(void, col, al * 0.8), width=int(fx.b))
812
+ elif k == "streak":
813
+ px = x + cos(fx.a) * fx.r
814
+ py = y + sin(fx.a) * fx.r
815
+ qa = fx.a - fx.b * 2.2
816
+ qr = fx.r / 0.93 ** 2.2
817
+ line(int(px), int(py), int(x + cos(qa) * qr), int(y + sin(qa) * qr),
818
+ fill=_lc_q(void, col, al), width=2 if al > 0.5 else 1)
819
+ elif k == "beam":
820
+ line(int(x), int(y), int(fx.a), int(fx.b),
821
+ fill=_lc_q(void, col, al * 0.7), width=2)
822
+ elif k == "flare":
823
+ grow = math.sin(min(1.0, (1.0 - al) * 2.2) * math.pi * 0.5) if al > 0.5 else 1.0
824
+ span = 0.55
825
+ pts: list[int] = []
826
+ for i in range(9):
827
+ t = i / 8
828
+ rad = fx.r + fx.b * grow * math.sin(t * math.pi)
829
+ a = fx.a + (t - 0.5) * span
830
+ pts += [int(x + cos(a) * rad), int(y + sin(a) * rad)]
831
+ line(*pts, fill=_lc_q(void, col, al * 0.85), width=2, smooth=True)
832
+
833
+ def snapshot(self) -> dict[str, Any]:
834
+ """Resumo do estado (para testes e depuração)."""
835
+ return {"t": round(self.t, 1), "mood": round(self.mood, 3),
836
+ "stress": round(self.stress, 3), "fx": len(self.fx),
837
+ "fronts": len(self.fronts), "stats": dict(self.stats)}