fix(mod-api): a hit names whose it is and what dealt it #3231
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!3231
Loading…
Reference in a new issue
No description provided.
Delete branch "glinesbdev/Longshot:fix/mod-api-hit-source"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
actors.damaged's sourceKind said who dealt a hit from one mark alone: the flag the game sets on its internal hits when they are Link's. The game sets it only on its own scripted damage, so Link's sword, charged sword and slingshot all read other. A mod could not tell its player's blows from an enemy's, and nothing said what a hit was dealt with (a sword, an arrow, a bomb).
To see it on main: log actors.damaged from a mod (e.sourceKind), hit an Octorok with the sword or the slingshot. Each hit reads other.
This reads a hit in its parts, as the game sums them at the life write:
Who dealt it. A hit is Link's when its internal part is marked his, or its external part (a blow or a shot) has Link (Player) as its owner. Otherwise an external hit is another's when its source and owner can be read, and unknown when they cannot; a hit with no external part is another's unless its internal part is marked Link's. The source (what hit, who threw or shot it) is read only when the hit has an external part, since the game keeps its last attacker after the hit, and a fall right after a blow would otherwise read the blow's attacker.
What it was dealt with. The external part's damage kinds, by the game's own list of them (body, smallWeapon, largeWeapon, arrow, bomb, fire, hammer, durability and the rest): a new DamageType enum, ActorDamage.damageType (the first of the hit's kinds), and where.damageType on a damage rule, which matches any of them.
Read first: game-core/src/rules.rs (hit_parts, source_kind, the damage_types match in applies) and game-core/src/layout/rules.rs (the four new fields, with what each is). Then api/src/services/actors.rs and rules.rs for the names, and docs/dev/reference/actors.md for what the game does and what a run showed.
Testing
Tested: Windows 10, Radeon RDNA3 (driver ), a test build of this branch with a mod logging every actors.damaged (its kind, source, owner and damageType), over two runs. None read unknown.
Magic arrows carry arrow alone: in the second run the mod also added two damage rules, fire ×3 and beam ×5. Din's Fire went from 6 to 18, and the arrows stayed at 2 and 14. The boomerang and the hookshot only stunned the enemies they hit, which deals no damage and so no actors.damaged.
Checks, on this branch rebased on main @
3a0f7d8f7:cargo test --lib: game-core 410 passed. api 591 passed, 1 failed: watch::tests::a_walked_reshade_folder_counts_a_change_once_two_walks_agree, which fails on main too. modtool 252 passed, 1 failed: testing_game::tests::the_menu_passes_its_tests, its frame-time check (the search step's worst frame 3.0 to 3.4 ms against a limit of 2 ms times the CPU scale). Run alone, it failed on main too (1 run of 3, worst frame 3.0 ms) and failed in each of 3 runs alone here. Nothing in this change runs in the menu's frames; it reads as a timing test that a debug build on this PC is near the edge of.
cargo clippy -p api -p game-core -p modtool --all-targets -- -D warnings (no errors; one unused-import warning in build-core, not touched here), cargo fmt --all --check and schema-gen --check: clean.
New tests: a_hit_is_links_when_the_game_marks_its_internal_part_or_its_owner_is_link (game-core; fails on main, where a blow owned by Link reads other) and a_hits_kind_of_damage_and_whether_link_dealt_it (modtool's fake game: a slingshotBullet rule doubles a seed and leaves a blow; the event names each kind and whose it is). Removing the damageType match from applies fails the second.
Still to test:
Linux: log actors.damaged and hit an enemy with the sword and the slingshot, expecting player and smallWeapon / slingshotBullet (@Beanstalk or by hand).
Any: a mod's scripted hit (actors.damage on an enemy), expecting player with no source and no damageType (it is an internal hit, marked Link's) (by hand).
Any: a boomerang or hookshot hit that deals damage (an enemy it hurts rather than stuns), expecting player and boomerang / hookshot (by hand).
Measurements
Nothing to measure: the hit's parts are read only when a damage rule asks for its source or kind, or a mod watches actors.damaged, and then it is four more reads of the life component per hit.
Risks
sourceKind changes for mods that read it: Link's blows and shots now read player where they read other. That is the fix, but a mod that worked around it will see the difference.
Link's own bomb hurting him reads player, as its owner is Link, so a rule on Link's side with sourceKind: "player" catches it too. A shot he sends back with his shield stays its first owner's.
damageType is only the first of a hit's kinds; a rule's where.damageType matches any of them. No hit in the runs had more than one.
A revert is enough. Nothing changes in saves or settings.
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
@review
Recommendation: Request changes. An external hit whose attacker cannot be read is reported as another actor’s hit, so mods can apply the wrong damage rule. The new testing helper can also panic on an invalid Unicode damage type.
The change identifies Link’s blows and projectiles and exposes damage types to mods. The reported Windows runs demonstrate the common cases, but unreadable attackers are misclassified and several stated cases remain untested.
Blocking (1): must change before merging.
game/crates/game-core/src/rules.rs:888: An external hit with an unreadable attacker is classified as other instead of unknown. (more on the line) Do: Return None when an external hit’s source or owner cannot be read, and test the unreadable attacker case.Should fix (2): not blocking, but worth doing before merging.
game/crates/modtool/src/testing_game/hooks.rs:1141: The testing helper slices a caller-supplied string at a byte boundary that may split a UTF-8 character. (more on the line) Do: Validate or look up the supplied API name without slicing at an unchecked byte boundary, and cover an invalid Unicode name.Testing before merging (1) (the change behaves the same on every system: one run of the change itself is enough; it doesn't hold the recommendation)
@fixcan do L1, L2 and L3.Checks: Still running: 11 checks.
More: does it help, other systems, switches, what was read
Does it help: Yes for the reported cases: two Windows runs identified Link’s sword and slingshot hits as player and their respective damage types. This is not a performance change; no before-and-after performance numbers were provided.
Other systems: Damage rules and actors.damaged share the new source classification; an incorrect classification can change gameplay as well as event data. Linux, scripted hits and damaging boomerang or hookshot hits remain untested. The change does not alter rendering, saves or settings.
Switches: None.
Reviewed:
1b46e53817against3a0f7d8f7f: Read the repository rules, standards, PR template, commit, diff, changed layout file and the surrounding hit-source, rule and fake-game paths. Skimmed the generated documentation and schema changes; did not read the larger changed API and modtool files in full. The adjacent Tooling checkout and docs/review-checklist.md were unavailable.; nothing run (a code review).Checked and fine:
Testing: No code, tests, builds or project scripts were run, as requested. The pull request reports Windows game runs and game-free tests; CI was still running.
Overlaps and merge order: Unknown; the network is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by llun. Nothing was run; a maintainer decides.
@ -807,0 +885,4 @@return Some(SourceKind::Player);}if parts.external > 0 && source.source != 0 {return match source.owner_type {L1 (blocking): An external hit with an unreadable attacker is classified as other instead of unknown.
When: When the external part is nonzero but its attacker link is stale or unreadable, hit_source returns a default source. This branch falls through to Other, so an other-only damage rule applies even if Link dealt the hit, and actors.damaged reports other instead of null.
Do: Return None when an external hit’s source or owner cannot be read, and test the unreadable attacker case.
@ -1138,0 +1138,4 @@// (what it was dealt with: one of the API's DamageType names, the game's with its first letter lowered)let kind = a.get("damageType").and_then(J::as_str);let mask = match kind {Some(k) if !k.is_empty() => game_core::rules::damage_type_bit(&(k[..1].to_uppercase() + &k[1..]))L2 (should fix): The testing helper slices a caller-supplied string at a byte boundary that may split a UTF-8 character.
When: A mod test calling game.damage with damageType set to a non-ASCII string such as "é" reaches k[..1] and panics instead of returning the intended invalid-kind error.
Do: Validate or look up the supplied API name without slicing at an unchecked byte boundary, and cover an invalid Unicode name.
@fix
1b46e53817f115dad32bRecommendation: Approve. The code is ready: only testing is left. The Windows game runs show the corrected hit attribution and damage types, but the listed Linux, scripted-hit, and damaging boomerang or hookshot cases remain untested. Recommend approval; not approved on the forge.
What
@fixchangedI changed this pull request: the new head is
f115dad32b. I rebased it ontomain(b6330a5b0b).Conflicts resolved
Changed
Nothing was built or run: the checks run again on the new head.
Mods can distinguish Link’s hits from other damage and inspect or match the kind of damage. The change adds bounded reads when a rule or watcher needs that information.
Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)
Checks: they run again on this head (
PR description / Title and description (pull_request_target)failed on the last one); a failure shows on the pull request.Also noticed (1; not asked for, not blocking)
More: does it help, other systems, switches, what was read
Does it help: Yes: the reported Windows runs classified sword and slingshot hits as player and identified their damage types; damage rules also changed Din’s Fire damage from 6 to 18. The remaining listed paths have not been exercised.
Other systems: The changed life hook serves damage rules and actors.damaged on Windows and Linux. The reported Windows run covers several player and enemy hits; Linux and the remaining damage paths are still untested.
Switches: None.
Reviewed:
f115dad32bagainstb6330a5b0b: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the fix diff, the changed sections of game-core rules and modtool hooks, and game-core’s rules layout in full. Skimmed the original feature diff and reported runs; the other changed files were not read in full.; nothing run (a code review).Checked and fine:
Testing: No commands that run the project or its tests were executed. The description reports Windows game runs and game-free tests; CI has not run on this head.
Overlaps and merge order: Not checked; network access is closed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.