Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/contracts/out/CombatSystem.sol/CombatSystem.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@
"solhint-config-mud": "2.0.11",
"solhint-plugin-mud": "2.0.11"
}
}
}
25 changes: 12 additions & 13 deletions packages/contracts/src/systems/CombatSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,12 @@ contract CombatSystem is System {
int256 _unroundedDamage = baseDifference + baseDamage;

_totalDamage = Math.roundInt(_unroundedDamage, int256(WAD)) / int256(WAD);
} else if (baseDifference < 0 && Math.absolute(baseDifference / int256(WAD)) >= uint256(attackerStat)) {
} else if (
baseDamage > int256(0) && baseDifference < int256(0)
&& Math.absolute(baseDifference / int256(WAD)) >= uint256(attackerStat)
) {
// if the stat difference is equal to or greater than the attackers base stat subtract difference from damage
if (baseDamage + baseDifference > 0) {
if (baseDamage + baseDifference > int256(0)) {
_totalDamage = (baseDamage + baseDifference) / int256(WAD);
} else {
// if damage is negative minimu damage is 1
Expand Down Expand Up @@ -370,23 +373,19 @@ contract CombatSystem is System {
baseDamage = (
attackStats.bonusDamage
+ int256(
uint256(rnChunk) % uint256(equippedSpell.maxDamage) <= uint256(equippedSpell.minDamage)
uint256(rnChunk) % uint256(equippedSpell.maxDamage) + 1 <= uint256(equippedSpell.minDamage)
? equippedSpell.minDamage
: int256(
uint256(rnChunk)
% uint256(
equippedSpell.maxDamage < 0 ? equippedSpell.maxDamage - 1 : equippedSpell.maxDamage + 1
)
)
: int256(uint256(rnChunk) % uint256(equippedSpell.maxDamage) + 1)
)
) * int256(ATTACK_MODIFIER);
} else {
baseDamage = (equippedSpell.maxDamage + attackStats.bonusDamage) * int256(ATTACK_MODIFIER);
}
_damage = (
_addStatBonus(attackerIntelligence, defenderIntelligence, baseDamage)
- int256(_addStatBonus(defenderIntelligence, defenderIntelligence, int256(DEFENSE_MODIFIER)))
);
_damage = _addStatBonus(attackerIntelligence, defenderIntelligence, baseDamage);

if (_damage < int256(0) && equippedSpell.maxDamage > int256(0)) {
_damage = int256(0);
}
}

function _calculateStatusEffect(
Expand Down
47 changes: 33 additions & 14 deletions packages/contracts/test/CombatSystem.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
TOKEN_URI,
ITEMS_NAMESPACE
} from "../constants.sol";
import {CombatEncounterData, StarterItemsData} from "@codegen/index.sol";
import {CombatEncounterData, StarterItemsData, SpellStatsData, SpellStats} from "@codegen/index.sol";
import {GasReporter} from "@latticexyz/gas-report/src/GasReporter.sol";
import "forge-std/console.sol";

Expand Down Expand Up @@ -189,26 +189,45 @@ contract Test_CombatSystem is SetUp, GasReporter {
world.UD__endEncounter(encounterId, 1000000000, true);
}

function test_MagicDamage() public {
function test_MagicDamage(uint256 attackerInt, uint256 defenderInt) public {
attackerInt = bound(attackerInt, 6, 100);
defenderInt = bound(defenderInt, 0, 100);
// bob has higher agi and int to go first
StatsData memory BobStats = world.UD__getStats(bobCharacterId);
BobStats.intelligence = 10;
BobStats.agility = 10;
world.UD__adminSetStats(bobCharacterId, BobStats);

world.UD__adminDropItem(bobCharacterId, 11, 1);
uint256[] memory itemIds = new uint256[](1);
itemIds[0] = 11;
vm.prank(bob);
world.UD__equipItems(bobCharacterId, itemIds);
StatsData memory bobStats = world.UD__getStats(bobCharacterId);
bobStats.intelligence = int256(attackerInt);
bobStats.agility = 10;
world.UD__adminSetStats(bobCharacterId, bobStats);

// assert that spell is correct
SpellStatsData memory spellStats = SpellStats.get(startingSpellId);
assert(spellStats.maxDamage == 5);
assert(spellStats.minDamage == 1);

// set defender stats
StatsData memory entityStats = world.UD__getStats(entityId);
entityStats.intelligence = int256(defenderInt);
entityStats.agility = 9;
world.UD__adminSetStats(entityId, entityStats);

// world.UD__adminDropItem(bobCharacterId, startingSpellId, 1);
// uint256[] memory itemIds = new uint256[](1);
// itemIds[0] = startingSpellId;
// vm.prank(bob);
// world.UD__equipItems(bobCharacterId, itemIds);

vm.prank(bob);
bytes32 encounterId = world.UD__createEncounter(EncounterType.PvE, attackers, defenders);
Action[] memory actions = new Action[](1);
actions[0] = Action({attackerEntityId: bobCharacterId, defenderEntityId: entityId, itemId: 11});
uint256 fees = 0; // entropy.getFee(address(1));
actions[0] = Action({attackerEntityId: bobCharacterId, defenderEntityId: entityId, itemId: startingSpellId});

vm.prank(bob);
world.UD__endTurn(encounterId, bobCharacterId, actions);

StatsData memory bobAfterTurn = world.UD__getStats(bobCharacterId);
StatsData memory entAfterTurn = world.UD__getStats(entityId);

assertTrue(bobAfterTurn.currentHp <= bobStats.currentHp);
assertTrue(entAfterTurn.currentHp <= entityStats.currentHp);
}

// function test_MagicHeals() public {
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/worlds.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"17069": {
"address": "0x5eb9ce99fa842af4be34f9319532dd847b27350c",
"blockNumber": 8864941
"address": "0xc85ee40fbe19f55416e356db98af9e67a97175ee",
"blockNumber": 8949955
},
"31337": {
"address": "0xd3c22974e1eddfd1cd4181c7023f6bca79eeba03"
"address": "0x64972f2e920106cc8b4402d42b921b009ec169a0"
},
"84532": {
"address": "0x3cf01199a18fd3900c49710563177505085371ca",
Expand Down