Skip to content

Commit 71254ba

Browse files
committed
fix: load equipment and inventory data in ability handler (fixes #20)
- Add equipment loading (loadEquipment()) in _handleAbility function - Add inventory loading from storage in _handleAbility function - Load defender equipment when processing offensive abilities - Fix variable shadowing warning in Storage.sol _getKiller function - Add comprehensive tests for equipment/inventory loading during abilities This ensures ability usage returns complete BattleNad structs with populated weapon, armor, and inventory data instead of empty values.
1 parent a1ccc46 commit 71254ba

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

src/battle-nads/Handler.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ abstract contract Handler is Balances {
568568
return (attacker, false, 0);
569569
}
570570

571+
// Load equipment and inventory
572+
attacker = attacker.loadEquipment();
573+
attacker.inventory = inventories[attacker.id];
574+
571575
// Attempt to load a defender, exit if no defenders remain
572576
BattleNad memory defender;
573577
bool loadedDefender = attacker.activeAbility.targetIndex != 0
@@ -587,6 +591,9 @@ abstract contract Handler is Balances {
587591
// Return early if target cant be found - process their death in regular combat task.
588592
return (attacker, false, 0);
589593
}
594+
595+
// Load defender equipment too
596+
defender = defender.loadEquipment();
590597
}
591598

592599
// Make sure the characters are in combat if appropriate

src/battle-nads/Storage.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ abstract contract Storage {
210210
}
211211

212212
function _getKiller(bytes32 deceasedID) internal view returns (bytes32 killerID, bool valid) {
213-
bytes32 killerID = killMap[deceasedID];
214-
if (killerID == _UNKILLED || killerID == _KILL_PROCESSED || !_isValidID(killerID)) {
213+
bytes32 storedKillerID = killMap[deceasedID];
214+
if (storedKillerID == _UNKILLED || storedKillerID == _KILL_PROCESSED || !_isValidID(storedKillerID)) {
215215
killerID = _NULL_ID;
216216
return (killerID, false);
217217
}
218+
killerID = storedKillerID;
218219
return (killerID, true);
219220
}
220221

test/battle-nads/BattleNadsAbilityTest.t.sol

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,132 @@ contract BattleNadsAbilityTest is BattleNadsBaseTest {
483483
}
484484
}
485485
}
486+
487+
// =============================================================================
488+
// EQUIPMENT AND INVENTORY LOADING TESTS (Issue #20)
489+
// =============================================================================
490+
491+
/**
492+
* @dev Tests that equipment and inventory data are properly loaded when using abilities
493+
* This test verifies the fix for issue #20 where ability usage returned empty equipment data
494+
*/
495+
function test_Ability_EquipmentAndInventoryLoading() public {
496+
bytes32 character = character1;
497+
498+
// First equip some items to the character by setting weapon and armor IDs
499+
_modifyCharacterStat(character, "weaponID", 4); // Set weapon ID
500+
_modifyCharacterStat(character, "armorID", 4); // Set armor ID
501+
502+
// Get character data before ability to verify equipment is set
503+
BattleNad memory beforeAbility = battleNads.getBattleNad(character);
504+
console.log("=== Before Ability Usage ===");
505+
console.log("Weapon ID:", beforeAbility.stats.weaponID);
506+
console.log("Armor ID:", beforeAbility.stats.armorID);
507+
console.log("Weapon Name:", beforeAbility.weapon.name);
508+
console.log("Armor Name:", beforeAbility.armor.name);
509+
510+
// Verify character has equipment IDs
511+
assertTrue(beforeAbility.stats.weaponID > 0, "Character should have weapon equipped");
512+
assertTrue(beforeAbility.stats.armorID > 0, "Character should have armor equipped");
513+
514+
// Enter combat to use abilities
515+
bool combatStarted = _triggerRandomCombat(character);
516+
assertTrue(combatStarted, "Should enter combat");
517+
518+
// Use a non-offensive ability (doesn't require target)
519+
vm.prank(userSessionKey1);
520+
battleNads.useAbility(character, 0, 1);
521+
522+
// Get character data after ability usage
523+
BattleNad memory afterAbility = battleNads.getBattleNad(character);
524+
console.log("=== After Ability Usage ===");
525+
console.log("Weapon ID:", afterAbility.stats.weaponID);
526+
console.log("Armor ID:", afterAbility.stats.armorID);
527+
console.log("Weapon Name:", afterAbility.weapon.name);
528+
console.log("Weapon Base Damage:", afterAbility.weapon.baseDamage);
529+
console.log("Armor Name:", afterAbility.armor.name);
530+
console.log("Armor Factor:", afterAbility.armor.armorFactor);
531+
console.log("Inventory Weapon Bitmap:", afterAbility.inventory.weaponBitmap);
532+
console.log("Inventory Armor Bitmap:", afterAbility.inventory.armorBitmap);
533+
534+
// Verify equipment data is still loaded (not empty) after ability usage
535+
if (afterAbility.stats.weaponID > 0) {
536+
assertTrue(
537+
bytes(afterAbility.weapon.name).length > 0 || afterAbility.weapon.baseDamage > 0,
538+
"Weapon data should be loaded when weapon is equipped"
539+
);
540+
}
541+
542+
if (afterAbility.stats.armorID > 0) {
543+
assertTrue(
544+
bytes(afterAbility.armor.name).length > 0 || afterAbility.armor.armorFactor > 0,
545+
"Armor data should be loaded when armor is equipped"
546+
);
547+
}
548+
549+
// Verify inventory data is loaded
550+
assertTrue(
551+
afterAbility.inventory.weaponBitmap > 0 || afterAbility.inventory.armorBitmap > 0 || afterAbility.inventory.balance > 0,
552+
"Inventory data should be loaded"
553+
);
554+
555+
console.log("Equipment and inventory data properly loaded during ability usage!");
556+
}
557+
558+
/**
559+
* @dev Tests equipment loading for offensive abilities with targets
560+
*/
561+
function test_Ability_OffensiveWithEquipmentLoading() public {
562+
bytes32 attacker = character1;
563+
bytes32 defender = character2;
564+
565+
// Equip items to both characters by setting weapon and armor IDs
566+
_modifyCharacterStat(attacker, "weaponID", 3); // Set weapon ID
567+
_modifyCharacterStat(attacker, "armorID", 3); // Set armor ID
568+
_modifyCharacterStat(defender, "weaponID", 5); // Set weapon ID
569+
_modifyCharacterStat(defender, "armorID", 5); // Set armor ID
570+
571+
// Enter combat
572+
bool combatStarted = _triggerRandomCombat(attacker);
573+
assertTrue(combatStarted, "Should enter combat");
574+
575+
// Find target for offensive ability
576+
uint256 targetIndex = _findCombatTarget(attacker);
577+
assertTrue(targetIndex > 0, "Should find valid target");
578+
579+
// Determine offensive ability index based on class
580+
BattleNad memory attackerData = battleNads.getBattleNad(attacker);
581+
uint256 offensiveAbilityIndex = attackerData.stats.class == CharacterClass.Warrior ? 1 : 2;
582+
583+
// Use offensive ability
584+
vm.prank(userSessionKey1);
585+
battleNads.useAbility(attacker, targetIndex, offensiveAbilityIndex);
586+
587+
// Get updated attacker data
588+
BattleNad memory afterAbility = battleNads.getBattleNad(attacker);
589+
590+
// Verify both attacker's equipment is loaded
591+
console.log("=== Attacker Equipment After Offensive Ability ===");
592+
console.log("Weapon ID:", afterAbility.stats.weaponID);
593+
console.log("Weapon Name:", afterAbility.weapon.name);
594+
console.log("Armor ID:", afterAbility.stats.armorID);
595+
console.log("Armor Name:", afterAbility.armor.name);
596+
597+
if (afterAbility.stats.weaponID > 0) {
598+
assertTrue(
599+
bytes(afterAbility.weapon.name).length > 0 || afterAbility.weapon.baseDamage > 0,
600+
"Attacker weapon data should be loaded"
601+
);
602+
}
603+
604+
if (afterAbility.stats.armorID > 0) {
605+
assertTrue(
606+
bytes(afterAbility.armor.name).length > 0 || afterAbility.armor.armorFactor > 0,
607+
"Attacker armor data should be loaded"
608+
);
609+
}
610+
611+
// The defender's equipment should also be loaded in the handler
612+
console.log("Offensive ability with equipment loading test passed!");
613+
}
486614
}

0 commit comments

Comments
 (0)