[CONTEXT] FARSPACE 4X game, need to understand GameState, engine turn processing, and event system.
[GOAL] Understand the full GameState struct, how turns are processed, and how events are emitted.
[DOWNSTREAM] Add trade network state to GameState, compute trade in turn processing, emit trade events.
[REQUEST] Very thorough exploration of:
crates/game_core/src/state/ - GameState struct, what fields it has, how initialized
Location is indirect: Planet.colony: Option<ColonyId> + colony points back to star/planet_index.
Related derived state in GameState: colony_supply, colony_blockade, colony_unrest, colony_unrest_causes, colony_rebellion_risk_bp, colony_recent_conquest_turn, colony_automation, last_colony_yields.
Trade-network state:
No dedicated trade_network field yet.
Closest existing system is recompute_colony_supply() + colony_supply.
Supply graph uses empire_trade_hub_star() and stars_have_trade_link().
Trade-link rules:
same-sector distance threshold: 550^2
cross-sector threshold: 325^2
hyperspace lane can count if empire has HYPERSPACE_CARTOGRAPHY, and player must have lane in known_hyperspace_lanes
This is likely insertion point for new trade-network state + trade turn computation.
Turn pipeline / apply_turn:
apply_turn(commands) processes commands in order.
Each command maps to a process_* helper; invalid input usually pushes Event::Error and returns.
EndTurn triggers full sim tick immediately at that point in command list, not only at end.
If no EndTurn seen, engine still refreshes supply/unrest/resource-access caches after command processing, but does not advance turn.
After all events collected, each event is appended to state.event_log as text; log trimmed to 50.
state exports include GameState, Colony, Empire, Fleet, Star, Planet, Sector, IDs, tech/content types, treaty/diplomacy types, yield types, etc
also exports dispatch API and YieldContext types
Pseudocode:
text
apply_turn(commands):
events = []
processed_end_turn = false
for cmd in commands:
match cmd:
EndTurn => processed_end_turn = true; process_end_turn(events)
else => process_* helper; helper may mutate state and push events/errors
if !processed_end_turn:
refresh_colony_supply()
refresh_unrest_statuses()
recompute_empire_resource_access()
append event.to_log_message() to event_log
trim event_log to 50
return events
Best insertion points for trade work:
GameState new fields in state.rs
Engine::process_end_turn near supply/blockade/resource-access refresh
recompute_colony_supply() / stars_have_trade_link() if trade graph should reuse connectivity rules
new events in events.rs
<next_steps>
Use state.rs + engine.rs as slice boundary.
Add trade state beside colony_supply/colony_blockade.
Hook trade compute into process_end_turn before economy summary.
Emit new trade events in same push-after-mutation style as supply/blockade.
</next_steps>