Skip to content

refactor(events): split events into separate files#177

Open
ramon-bernardo wants to merge 8 commits intodevfrom
refactor/split-events
Open

refactor(events): split events into separate files#177
ramon-bernardo wants to merge 8 commits intodevfrom
refactor/split-events

Conversation

@ramon-bernardo
Copy link
Copy Markdown
Member

@ramon-bernardo ramon-bernardo commented Feb 9, 2026

Pull Request Prelude

Changes Proposed

This change reorganizes the event-related classes into a dedicated events module to improve code structure, maintainability, and separation of concerns.

With this refactor, these classes were moved into a new src/events directory, and each event was split into its own header and source file. The build configuration and module references were updated accordingly to reflect the new structure.

This does not introduce new functionality but improves the overall organization of the codebase, making the event system easier to navigate, extend and maintain.

Summary by CodeRabbit

  • Refactor

    • Event system split into separate modules (creature, game, monster, party, player) with targeted load/reload for each.
    • Consolidated and reorganized event declarations for clearer module boundaries.
  • Chores

    • Updated build and project layout and adjusted include paths to match the modularized event subsystem.
  • User impact

    • No gameplay or public API changes; behavior remains unchanged.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 9, 2026

📝 Walkthrough

Walkthrough

Centralized the event system into a new src/events object library: removed the monolithic src/events.cpp, added per-subsystem event modules (creature, game, party, player, monster, events), updated CMake and project files, and migrated callers to use the new per-module headers and explicit load/reload calls.

Changes

Cohort / File(s) Summary
Build / project
src/CMakeLists.txt, src/events/CMakeLists.txt, vc18/atlas.vcxproj, vc18/atlas.vcxproj.filters
Made events an OBJECT library, removed events.cpp/events.h from top-level lists, added add_subdirectory(events), and updated project filters and sources to include new src/events/* files.
Removed monolith
src/events.cpp
Deleted the previous central event implementation that handled all Lua event loading and dispatch.
New events modules
src/events/events.h, src/events/events.cpp, src/events/creature.h, src/events/creature.cpp, src/events/game.h, src/events/game.cpp, src/events/party.h, src/events/party.cpp, src/events/monster.h, src/events/monster.cpp, src/events/player.h, src/events/player.cpp
Added per-subsystem event headers and implementations. Each module provides load/reload and per-event entrypoints; events/events.cpp exposes getScriptId(EventInfoId).
Call-site includes & usage
src/game.cpp, src/scriptmanager.cpp, src/signals.cpp, src/combat.cpp, src/creature.cpp, src/monster.cpp, src/party.cpp, src/player.cpp, src/protocolgame.cpp, src/spawn.cpp, src/spells.cpp, src/lua/..., src/script.cpp
Replaced include of the old aggregated events.h with specific events/* headers and replaced bulk tfs::events::load()/reload() calls with per-module ...::load() / ...::reload() invocations.
Lua modules adjustments
src/lua/modules/*, src/lua/register.h (project file changes)
Updated Lua module include paths to reference new events headers where applicable.

Sequence Diagram(s)

sequenceDiagram
    participant ScriptManager
    participant EventsAggregator as "tfs::events (caller)"
    participant Module as "tfs::events::creature / game / party / player / monster"
    participant Lua as "LuaScriptInterface"
    participant FS as "data/scripts/events/*.lua"
    rect rgba(200,200,255,0.5)
    ScriptManager->>EventsAggregator: call load() (was single tfs::events::load)
    EventsAggregator->>Module: call Module::load() for each subsystem
    Module->>Lua: Lua.init() / load script file
    Lua->>FS: read specific .lua (e.g., creature.lua)
    FS-->>Lua: file contents
    Lua-->>Module: register meta-event IDs
    Module-->>EventsAggregator: return loaded
    EventsAggregator-->>ScriptManager: all modules loaded
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • ramon-bernardo

Poem

"I hopped through code with nimble paws,
New event rooms I built with claws.
Lua scripts now sit in rows,
Each creature, party, player knows.
A carrot cheer for modular cause! 🥕"

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: refactoring events into separate files with improved code organization.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/split-events

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/CMakeLists.txt (1)

18-18: ⚠️ Potential issue | 🔴 Critical

Remove stale reference to deleted src/events.cpp on line 18.

The file src/events.cpp no longer exists and should not be listed in the main CMakeLists.txt. The new src/events/events.cpp is properly compiled as part of the events OBJECT library (included via add_subdirectory(events) on line 180). Keeping the stale reference on line 18 will cause the build to fail when CMake attempts to compile a non-existent source file.

🤖 Fix all issues with AI agents
In `@src/CMakeLists.txt`:
- Around line 180-181: The new OBJECT library target events is added but not
wired into the tfslib target; add a target_sources(tfslib PRIVATE
$<TARGET_OBJECTS:events>) (mirroring the existing target_sources call that wires
luascript) so the events object files are linked into the final binary; locate
the existing target_sources call for luascript and add an equivalent line
referencing events and tfslib.

In `@src/events/CMakeLists.txt`:
- Line 19: Remove the stale reference to ${CMAKE_CURRENT_LIST_DIR}/events.cpp
from the tfs_SRC list and ensure the events OBJECT library produced by
add_subdirectory(events) is actually consumed by tfslib: delete the old
events.cpp entry from tfs_SRC, update src/CMakeLists.txt to add
target_sources(tfslib PRIVATE $<TARGET_OBJECTS:events>) after the existing
add_subdirectory(events) line, and remove or wire the events_HDR variable so
headers are either used or the variable deleted; this ensures add_library(events
OBJECT ${events_SRC}) object files are linked into tfslib and no dead
source/header variables remain.

In `@vc18/atlas.vcxproj.filters`:
- Around line 697-699: The ClInclude entry for the header register.h was moved
to the root "Header Files" filter; update the <ClInclude
Include="..\src\lua\modules\register.h"> block in the vcxproj.filters so its
<Filter> value matches the lua sub-filter (e.g. "Header Files\lua" or the
existing lua subfolder filter used by other headers), i.e. change the <Filter>
element back to the expected sub-filter for lua headers and verify other entries
in the file (other src\lua\modules\*.h) use the same filter for consistency.
🧹 Nitpick comments (6)
src/events/CMakeLists.txt (1)

10-17: events_HDR is defined but unused.

The header list variable is declared but never referenced in add_library or any other CMake command. If it's intended for IDE integration or the format target, it should be included; otherwise it's dead configuration.

Option: include headers in the library target for IDE visibility
-add_library(events OBJECT ${events_SRC})
+add_library(events OBJECT ${events_SRC} ${events_HDR})
src/events/party.h (1)

6-19: Consider adding a forward declaration for Player.

Party is forward-declared on Line 6, but Player (used in multiple function signatures) is not. For consistency and to avoid reliance on transitive includes or PCH for this header's self-containment, a forward declaration for Player would be appropriate.

Proposed fix
 class Party;
+class Player;
 
 namespace tfs::events::party {
src/events/monster.h (1)

1-15: Add forward declarations for Monster, Container, and Position for consistency and header hygiene.

Unlike src/events/creature.h which forward-declares Monster and Container, this header uses these types in function signatures without forward declarations. Although the code currently compiles because translation units include necessary headers before calling these functions, explicitly forward-declaring these types improves header self-sufficiency and consistency with the codebase pattern shown in creature.h.

Suggested fix
 `#pragma` once
 
+class Container;
+class Monster;
+struct Position;
+
 namespace tfs::events::monster {
src/events/creature.cpp (1)

6-6: Include of "../item.h" — verify it's still needed.

item.h is included but Item is only used as a shared_ptr<Item> parameter in onDeath (line 415). Depending on what the header for creature.h already pulls in (e.g., forward declarations vs full definitions), this include might be redundant, or it might be needed for CombatDamage. Low priority — just a note.

Also applies to: 8-10

src/events/player.cpp (1)

534-559: Intentional parameter reorder for Lua — just a note for maintainers.

The C++ signature has (player, item, outfit, podiumVisible, direction) but the Lua push order is (player, item, outfit, direction, podiumVisible) (lines 553-557), matching the Lua comment Player:onPodiumEdit(item, outfit, direction, isVisible). This deliberate reorder is easy to miss; a brief inline comment would help future readers.

src/events/party.cpp (1)

8-8: Remove unused #include "../item.h" on line 8.

The include is unused—no Item-related types appear anywhere in this file. Party event handlers only use Party, Player, and uint64_t types.

Proposed fix
-#include "../item.h"
 `#include` "../lua/env.h"

Comment thread src/CMakeLists.txt
Comment thread src/events/CMakeLists.txt
Comment thread vc18/atlas.vcxproj.filters Outdated
Comment thread src/events/CMakeLists.txt
Comment thread src/events/party.h
Comment thread src/events/monster.h
ramon-bernardo and others added 3 commits February 9, 2026 19:44
Co-authored-by: Ranieri Althoff <1993083+ranisalt@users.noreply.github.com>
Co-authored-by: Ranieri Althoff <1993083+ranisalt@users.noreply.github.com>
Comment thread src/events/creature.cpp Outdated
Comment thread src/events/player.cpp
Comment on lines +534 to +535
void onPodiumEdit(const std::shared_ptr<Player>& player, const std::shared_ptr<Item>& item, const Outfit_t& outfit,
bool podiumVisible, Direction direction)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should reorder these to match the Lua signature?

Not important

ranisalt and others added 2 commits February 9, 2026 23:46
Co-authored-by: Ranieri Althoff <1993083+ranisalt@users.noreply.github.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/events/creature.cpp`:
- Around line 307-310: The current block negates damage.secondary.value whenever
damage.primary.type != COMBAT_HEALING, which can flip healing into damage;
update the logic to mirror the pattern used in src/game.cpp:3936 by checking
each damage component's type independently — i.e., keep the existing check for
damage.primary.type before negating damage.primary.value, and add a separate
conditional that verifies damage.secondary.type != COMBAT_HEALING before
negating damage.secondary.value (refer to the symbols damage.primary.type,
damage.primary.value, damage.secondary.type, damage.secondary.value, and
COMBAT_HEALING).

Comment thread src/events/creature.cpp
Comment on lines +307 to +310
if (damage.primary.type != COMBAT_HEALING) {
damage.primary.value = -damage.primary.value;
damage.secondary.value = -damage.secondary.value;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# First, examine the specific file and lines mentioned
head -320 src/events/creature.cpp | tail -25

Repository: atlas-kit/atlas

Length of output: 1079


🏁 Script executed:

#!/bin/bash
# Check the damage structure to understand primary and secondary fields
rg -n 'struct.*damage|class.*damage' --type cpp -A 10 -i | head -50

Repository: atlas-kit/atlas

Length of output: 1886


🏁 Script executed:

#!/bin/bash
# Look for where damage.secondary is used
rg -n 'damage\.secondary' --type cpp -B 2 -A 2 | head -80

Repository: atlas-kit/atlas

Length of output: 5088


🏁 Script executed:

#!/bin/bash
# Check if secondary.type exists and is checked elsewhere
rg -n 'secondary\.type' --type cpp -B 2 -A 2

Repository: atlas-kit/atlas

Length of output: 7246


Secondary damage negation should check secondary type independently — correctness issue.

The negation block only checks damage.primary.type != COMBAT_HEALING but unconditionally negates damage.secondary.value. If secondary damage type is COMBAT_HEALING while primary is not, the healing value will be incorrectly negated to damage. This violates the established pattern in src/game.cpp:3936 where secondary type is always checked before negating secondary value.

Suggested fix
 		if (damage.primary.type != COMBAT_HEALING) {
 			damage.primary.value = -damage.primary.value;
-			damage.secondary.value = -damage.secondary.value;
 		}
+		if (damage.secondary.type != COMBAT_HEALING) {
+			damage.secondary.value = -damage.secondary.value;
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (damage.primary.type != COMBAT_HEALING) {
damage.primary.value = -damage.primary.value;
damage.secondary.value = -damage.secondary.value;
}
if (damage.primary.type != COMBAT_HEALING) {
damage.primary.value = -damage.primary.value;
}
if (damage.secondary.type != COMBAT_HEALING) {
damage.secondary.value = -damage.secondary.value;
}
🤖 Prompt for AI Agents
In `@src/events/creature.cpp` around lines 307 - 310, The current block negates
damage.secondary.value whenever damage.primary.type != COMBAT_HEALING, which can
flip healing into damage; update the logic to mirror the pattern used in
src/game.cpp:3936 by checking each damage component's type independently — i.e.,
keep the existing check for damage.primary.type before negating
damage.primary.value, and add a separate conditional that verifies
damage.secondary.type != COMBAT_HEALING before negating damage.secondary.value
(refer to the symbols damage.primary.type, damage.primary.value,
damage.secondary.type, damage.secondary.value, and COMBAT_HEALING).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants