Skip to content

Commit c16dbc3

Browse files
committed
fix: issue with ability cooldown
1 parent 75f0a32 commit c16dbc3

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

test/battle-nads/BattleNadsCombatTest.t.sol

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ contract BattleNadsCombatTest is BattleNadsBaseTest, Constants {
240240
// Execute the ability task
241241
_rollForward(1);
242242

243-
BattleNad memory afterExecution = _battleNad(1);
244243
// Task execution completed
245244
assertTrue(true, "Ability task execution completed");
246245
} else {
@@ -384,19 +383,10 @@ contract BattleNadsCombatTest is BattleNadsBaseTest, Constants {
384383
bool combatStarted = _triggerRandomCombat(fighter);
385384
assertTrue(combatStarted, "Should enter combat");
386385

387-
// Use abilities with proper targeting and check for logs
388-
bool abilityUsed = _useAppropriateAbility(fighter);
386+
// Use ability with proper execution and cooldown handling
387+
bool abilityUsed = _useAbilityAndExecute(fighter, 0, 1); // Use first ability
389388

390389
if (abilityUsed) {
391-
// Wait for ability to execute
392-
BattleNad memory currentState = battleNads.getBattleNad(fighter);
393-
if (currentState.activeAbility.taskAddress != address(0)) {
394-
uint256 targetBlock = uint256(currentState.activeAbility.targetBlock);
395-
if (targetBlock > block.number) {
396-
_rollForward(targetBlock - block.number + 1);
397-
}
398-
}
399-
400390
Log[] memory combatLogs = _getCombatLogs(user1);
401391

402392
// Should have some combat-related logs

test/battle-nads/helpers/BattleNadsBaseTest.sol

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ contract BattleNadsBaseTest is BaseTest {
257257
BattleNad memory currentState = battleNads.getBattleNad(charId);
258258
require(currentState.stats.combatants > 0, "Character must be in combat");
259259

260+
uint256 lastAbilityBlock = 0; // Track when last ability was used
261+
260262
for (uint256 i = 0; i < maxRounds; i++) {
261263
currentState = battleNads.getBattleNad(charId);
262264

@@ -269,10 +271,13 @@ contract BattleNadsBaseTest is BaseTest {
269271
if (currentState.activeAbility.taskAddress != address(0)) {
270272
uint256 targetBlock = uint256(currentState.activeAbility.targetBlock);
271273
if (targetBlock > block.number) {
272-
// Roll forward to the target block to execute the ability task
274+
// Roll forward to execute the ability task
273275
uint256 blocksToRoll = targetBlock - block.number + 1;
274276
_rollForward(blocksToRoll);
275277

278+
// Track when this ability executed for cooldown calculation
279+
lastAbilityBlock = block.number;
280+
276281
// Check if combat ended after ability execution
277282
BattleNad memory afterExecution = battleNads.getBattleNad(charId);
278283
if (afterExecution.stats.combatants == 0) {
@@ -282,15 +287,22 @@ contract BattleNadsBaseTest is BaseTest {
282287
}
283288
}
284289

290+
// Check if we need to wait for ability cooldown (200 blocks)
291+
if (lastAbilityBlock > 0 && block.number < lastAbilityBlock + 200) {
292+
uint256 cooldownRemaining = (lastAbilityBlock + 200) - block.number;
293+
vm.roll(block.number + cooldownRemaining + 1);
294+
}
295+
285296
// Use appropriate ability to progress combat
286297
bool abilityUsed = _useAppropriateAbility(charId);
287298

288299
if (abilityUsed) {
289300
// Ability was scheduled, will be handled in next iteration
301+
lastAbilityBlock = block.number; // Update ability usage time
290302
continue;
291303
} else {
292-
// If we can't use abilities, try to wait for existing tasks to complete
293-
_rollForward(5);
304+
// If we can't use abilities, wait a bit for combat to progress naturally
305+
_rollForward(10);
294306
}
295307
}
296308

@@ -496,10 +508,10 @@ contract BattleNadsBaseTest is BaseTest {
496508
vm.store(address(battleNads), statSlot, bytes32(packedData));
497509
}
498510

499-
/// @notice Enhanced ability execution helper with proper targeting
511+
/// @notice Helper to use an ability and execute it, handling cooldowns properly
500512
/// @param charId Character using ability
501-
/// @param targetIndex Target index (0 for non-targeted abilities, or specific index for targeted)
502-
/// @param abilityIndex Ability index (1-based)
513+
/// @param targetIndex Target index for targeted abilities
514+
/// @param abilityIndex Ability index to use
503515
/// @return success Whether ability was successfully used and executed
504516
function _useAbilityAndExecute(
505517
bytes32 charId,
@@ -514,16 +526,26 @@ contract BattleNadsBaseTest is BaseTest {
514526
// Use ability
515527
vm.prank(userSessionKey1);
516528
try battleNads.useAbility(charId, targetIndex, abilityIndex) {
517-
// Roll forward to execute the ability task
518-
_rollForward(1);
529+
BattleNad memory afterAbility = battleNads.getBattleNad(charId);
530+
531+
// If ability was scheduled as task, wait for it to execute
532+
if (afterAbility.activeAbility.taskAddress != address(0)) {
533+
uint256 targetBlock = uint256(afterAbility.activeAbility.targetBlock);
534+
if (targetBlock > block.number) {
535+
_rollForward(targetBlock - block.number + 1);
536+
}
537+
538+
// Skip ahead for cooldown (200 blocks as per _checkAbilityTimeout)
539+
vm.roll(block.number + 200);
540+
}
519541

520-
BattleNad memory afterState = battleNads.getBattleNad(charId);
542+
BattleNad memory finalState = battleNads.getBattleNad(charId);
521543

522-
// Check if ability had some effect (combat state changed or ability task cleared)
544+
// Check if ability had some effect
523545
return (
524-
afterState.stats.combatants != before.stats.combatants
525-
|| afterState.stats.combatantBitMap != before.stats.combatantBitMap
526-
|| afterState.stats.health != before.stats.health || afterState.activeAbility.taskAddress == address(0)
546+
finalState.stats.combatants != before.stats.combatants
547+
|| finalState.stats.combatantBitMap != before.stats.combatantBitMap
548+
|| finalState.stats.health != before.stats.health || finalState.activeAbility.taskAddress == address(0)
527549
);
528550
} catch {
529551
return false;

0 commit comments

Comments
 (0)