Refactor: less direct access to game struct#4673
Refactor: less direct access to game struct#4673PieterVdc wants to merge 4 commits intodkfans:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR reduces direct access to the global game struct by routing common reads through accessor functions (eg get_gameturn(), creature_stats_get(), get_room_kind_stats(), get_trap_model_stats()), as groundwork for making the game state more serializable and encapsulated.
Changes:
- Replaced many
game.play_gameturnreads withget_gameturn()across gameplay, UI, networking, and tests. - Replaced direct config array indexing (
game.conf.*) with helper accessors (eg creature/room/trap model lookups). - Updated assorted timers/cooldowns/turn-based logic to use the new accessors.
Reviewed changes
Copilot reviewed 92 out of 92 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/actionpt.c | Use get_gameturn() for periodic action-point processing. |
| src/ariadne.c | Use get_gameturn() in navigation debug logging. |
| src/bflib_datetm.cpp | Use get_gameturn() for frame-skip / delta-time logic. |
| src/bflib_network_exchange.cpp | Use get_gameturn() when computing historical turn for networking. |
| src/config_spritecolors.c | Use get_gameturn() for neutral flashing color selection. |
| src/console_cmd.c | Use get_gameturn() in console time/turn commands and random creature selection helper. |
| src/creature_graphics.c | Use get_gameturn() for periodic tint updates. |
| src/creature_groups.c | Use get_gameturn() for follower recomputation and party wait timing. |
| src/creature_instances.c | Use get_gameturn() for instance cooldowns and teleport/heal delays. |
| src/creature_jobs.c | Use get_gameturn() for secondary-job check throttling. |
| src/creature_states.c | Use get_gameturn() across many creature state timers and throttles. |
| src/creature_states_combt.c | Use get_gameturn() for combat visibility caching and flee timers; use trap stats accessor. |
| src/creature_states_hero.c | Use get_gameturn() for hero idle/wait/retarget timings. |
| src/creature_states_lair.c | Use get_gameturn() for lair enemy scanning and sleep recovery cadence. |
| src/creature_states_mood.c | Use get_gameturn() for mood sound and “postals” cadence timers. |
| src/creature_states_pray.c | Use get_gameturn() to stamp temple cure protection time. |
| src/creature_states_prisn.c | Use get_gameturn() for prison timers and break checks. |
| src/creature_states_rsrch.c | Use get_gameturn() for post-research happiness timing. |
| src/creature_states_scavn.c | Use get_gameturn() for scavenging counters and protection windows. |
| src/creature_states_spdig.c | Use get_gameturn() for imp birth visuals and task-check throttles. |
| src/creature_states_tortr.c | Use get_gameturn() for torture timers, sounds, and convert/break computations. |
| src/creature_states_wrshp.c | Use get_gameturn() for workshop celebration timing. |
| src/dungeon_data.c | Use get_gameturn() for script timer restart. |
| src/engine_redraw.c | Use get_gameturn() for cursor animation timing and Armageddon overlay timing. |
| src/engine_render.c | Use get_gameturn() for interpolation resets, blink highlights, flame animation frame selection, and room stats accessor. |
| src/front_easter.c | Use get_gameturn() for easter egg animation positions. |
| src/front_input.c | Use get_gameturn() in debug reporting, level-finish time logging, and cheat selection creature stats access. |
| src/front_lvlstats.c | Use get_gameturn() when capturing end-of-level stat turn. |
| src/frontend.cpp | Use get_room_kind_stats() and get_gameturn() for UI state and objective scheduling. |
| src/frontmenu_ingame_evnt.c | Use get_gameturn() for UI flashing, timers, and network stats display. |
| src/frontmenu_ingame_map.c | Use get_gameturn() for minimap interpolation bookkeeping and blink cadence. |
| src/frontmenu_ingame_tabs.c | Use get_gameturn() for UI blinking and room kind stats accessor usage. |
| src/frontmenu_specials.c | Use creature stats accessor for specials UI rendering. |
| src/ftests/ftest.c | Use get_gameturn() for seeding and action scheduling timestamps. |
| src/ftests/ftest_util.c | Use creature stats accessor during random creature selection. |
| src/ftests/tests/ftest_bug_ai_bridge.c | Use get_gameturn() in functional test timing/log output. |
| src/ftests/tests/ftest_bug_invisible_units_cant_select.c | Use get_gameturn() for test timestamps. |
| src/ftests/tests/ftest_bug_pathing_pillar_circling.c | Use get_gameturn() for test timing gate. |
| src/ftests/tests/ftest_bug_pathing_stair_treasury.c | Use get_gameturn() for test timing gate. |
| src/ftests/tests/ftest_template.c | Use get_gameturn() for sample test delays/counters. |
| src/game_saves.c | Use get_gameturn() for “start-of-level” save conditional. |
| src/gui_frontbtns.c | Use get_gameturn() for UI blink cycle. |
| src/gui_msgs.c | Use get_gameturn() for message animation and expiration timing. |
| src/gui_parchment.c | Use get_gameturn() for parchment flame animation and map blink/flash logic. |
| src/gui_soundmsgs.cpp | Use get_gameturn() for “played recently” suppression windows. |
| src/gui_tooltips.c | Use creature stats accessor for tooltip content. |
| src/gui_topmsg.c | Use get_gameturn() for stat checking cadence and desync logging. |
| src/light_data.c | Use get_gameturn() for light interpolation lifecycle. |
| src/local_camera.c | Use get_gameturn() for local camera packet selection turn. |
| src/lua_api.c | Use get_gameturn() for bonus timer and message expiration updates. |
| src/lvl_script_commands.c | Use room kind stats accessor and get_gameturn() for message clearing. |
| src/lvl_script_conditions.c | Use get_gameturn() for script “game turn” and timers. |
| src/lvl_script_value.c | Use creature stats accessor and get_gameturn() for bonus time. |
| src/magic_powers.c | Use get_gameturn() for cooldowns/timers; use trap stats accessor. |
| src/main.cpp | Use get_gameturn() across many core-loop timers/logging and input-lag packet selection. |
| src/map_events.c | Use get_gameturn() for event cooldowns and last-run bookkeeping; use creature stats accessor. |
| src/net_checksums.c | Use get_gameturn() in checksum snapshotting and logging. |
| src/net_input_lag.c | Use get_gameturn() for local lag queue slot selection. |
| src/net_resync.cpp | Use get_gameturn() for resync progress bar early exit. |
| src/packets.c | Use get_gameturn() for lag-compensation packet selection and logging. |
| src/packets_cheats.c | Use creature stats accessor for cheat UI strings and random selection filtering. |
| src/packets_misc.c | Use get_gameturn() in packet save/load logging. |
| src/player_compchecks.c | Use get_gameturn() for AI timing and creature stats accessor in messages. |
| src/player_compevents.c | Use room kind stats accessor. |
| src/player_complookup.c | Use get_gameturn() when recording gold-map refresh turn. |
| src/player_compprocs.c | Use get_gameturn() across AI process timing and distance scaling. |
| src/player_comptask.c | Use get_gameturn() for AI task lifecycle timers; use room kind stats accessor; use creature stats accessor for chat. |
| src/player_computer.c | Use get_gameturn() for AI scheduling and periodic processing. |
| src/player_utils.c | Use get_gameturn() for objective display scheduling. |
| src/power_hand.c | Use get_gameturn() for Armageddon gating, hand busy timers, and hand rule checks; use creature stats accessor. |
| src/power_process.c | Use get_gameturn() for Armageddon and disease tick timing. |
| src/power_specials.c | Use get_gameturn() for turn collision protection in box activation. |
| src/room_data.c | Use get_gameturn() to stamp room creation turn. |
| src/room_entrance.c | Use get_gameturn() for entrance generation and Armageddon blocks. |
| src/room_garden.c | Use get_gameturn() for food growth cadence. |
| src/room_library.c | Use get_gameturn() for research completion and startup integration logic. |
| src/room_util.c | Use get_gameturn() in flames and room assimilation timing checks. |
| src/room_workshop.c | Use get_gameturn() for manufacture completion bookkeeping. |
| src/sounds.c | Use room kind stats accessor and get_gameturn() for bonus timer / ambient scheduling. |
| src/spdigger_stack.c | Use get_gameturn() for digger-stack and task-check throttling. |
| src/thing_corpses.c | Use get_gameturn() for corpse aging/cleanup and creation stamping. |
| src/thing_creature.c | Use get_gameturn() for many creature timers (flee/disease/unsummon/etc) and creature stats accessor in UI. |
| src/thing_data.c | Use get_gameturn() in thing allocation random seed derivation. |
| src/thing_doors.c | Use get_gameturn() to stamp door creation turn. |
| src/thing_effects.c | Use get_gameturn() for effect creation and timing-based visuals/subeffects. |
| src/thing_factory.c | Use get_gameturn() to stamp cave-in creation turn. |
| src/thing_list.c | Use creature stats accessor for “lord of the land” checks and model-flag counting. |
| src/thing_objects.c | Use get_gameturn() for object creation and cached nearby creature lookup timing. |
| src/thing_shots.c | Use get_gameturn() for periodic shot detonation, lightning cadence, and creation stamping. |
| src/thing_stats.c | Use get_gameturn() for creature age statistic. |
| src/thing_traps.c | Use get_gameturn() for trap rearm/shoot timing and trap stats accessor usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| static TbBool hand_rule_age_higher(struct HandRule *hand_rule, const struct Thing *thing) | ||
| { | ||
| return (game.play_gameturn - thing->creation_turn < hand_rule->param) ? !hand_rule->allow : !!hand_rule->allow; | ||
| return (get_gameturn() - thing->creation_turn < hand_rule->param) ? !hand_rule->allow : !!hand_rule->allow; | ||
| } |
There was a problem hiding this comment.
hand_rule_age_higher() currently uses the same < hand_rule->param comparison as hand_rule_age_lower(), so the AGE_HIGHER scripting rule cannot ever behave differently from AGE_LOWER. This looks like a logic bug; AGE_HIGHER should compare the thing age against param in the opposite direction (eg >=/>), while keeping the same allow/deny inversion semantics used by the other rules.
| i = eestats->subeffect_delay; | ||
| if (i > 0) | ||
| { | ||
| if (((elemtng->creation_turn - game.play_gameturn) % i) == 0) | ||
| if (((elemtng->creation_turn - get_gameturn()) % i) == 0) | ||
| { | ||
| struct Thing *subeff = create_effect_element(&elemtng->mappos, eestats->subeffect_model, elemtng->owner); |
There was a problem hiding this comment.
The subeffect spawning interval uses ((elemtng->creation_turn - get_gameturn()) % i) == 0. Since GameTurn is uint32_t, this subtraction underflows for most of the effect lifetime, making the modulo condition depend on wraparound rather than the effect age. This should likely be based on elapsed turns, eg (get_gameturn() - elemtng->creation_turn) % i, to spawn subeffects at the intended cadence.
for later making the game struct better serializable, hiding it's access more helps