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
22 changes: 16 additions & 6 deletions src/Engine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ contract Engine is IEngine, MappingAllocator {
uint256 numHooks = battle.engineHooks.length;
if (numHooks > 0) {
for (uint256 i; i < numHooks;) {
config.engineHooks[i] = battle.engineHooks[i];
IEngineHook hook = battle.engineHooks[i];
config.engineHooks[i].hook = hook;
config.engineHooks[i].stepsBitmap = hook.getStepsBitmap();
unchecked {
++i;
}
Expand Down Expand Up @@ -296,8 +298,10 @@ contract Engine is IEngine, MappingAllocator {
}
}

for (uint256 i = 0; i < battle.engineHooks.length;) {
battle.engineHooks[i].onBattleStart(battleKey);
for (uint256 i = 0; i < numHooks;) {
if ((config.engineHooks[i].stepsBitmap & (1 << uint8(EngineHookStep.OnBattleStart))) != 0) {
config.engineHooks[i].hook.onBattleStart(battleKey);
}
unchecked {
++i;
}
Expand Down Expand Up @@ -388,7 +392,9 @@ contract Engine is IEngine, MappingAllocator {

uint256 numHooks = config.engineHooksLength;
for (uint256 i = 0; i < numHooks;) {
config.engineHooks[i].onRoundStart(battleKey);
if ((config.engineHooks[i].stepsBitmap & (1 << uint8(EngineHookStep.OnRoundStart))) != 0) {
config.engineHooks[i].hook.onRoundStart(battleKey);
}
unchecked {
++i;
}
Expand Down Expand Up @@ -590,7 +596,9 @@ contract Engine is IEngine, MappingAllocator {

// Run the round end hooks
for (uint256 i = 0; i < numHooks;) {
config.engineHooks[i].onRoundEnd(battleKey);
if ((config.engineHooks[i].stepsBitmap & (1 << uint8(EngineHookStep.OnRoundEnd))) != 0) {
config.engineHooks[i].hook.onRoundEnd(battleKey);
}
unchecked {
++i;
}
Expand Down Expand Up @@ -737,7 +745,9 @@ contract Engine is IEngine, MappingAllocator {
}

for (uint256 i = 0; i < config.engineHooksLength;) {
config.engineHooks[i].onBattleEnd(battleKey);
if ((config.engineHooks[i].stepsBitmap & (1 << uint8(EngineHookStep.OnBattleEnd))) != 0) {
config.engineHooks[i].hook.onBattleEnd(battleKey);
}
unchecked {
++i;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Enums.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,10 @@ enum ExtraDataType {
SelfTeamIndex,
OpponentNonKOTeamIndex
}

enum EngineHookStep {
OnBattleStart,
OnRoundStart,
OnRoundEnd,
OnBattleEnd
}
4 changes: 4 additions & 0 deletions src/IEngineHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
pragma solidity ^0.8.0;

interface IEngineHook {
// Returns pre-computed bitmap of steps this hook runs at (set at deploy time)
// Bit layout: OnBattleStart=0x01, OnRoundStart=0x02, OnRoundEnd=0x04, OnBattleEnd=0x08
function getStepsBitmap() external view returns (uint16);

function onBattleStart(bytes32 battleKey) external;
function onRoundStart(bytes32 battleKey) external;
function onRoundEnd(bytes32 battleKey) external;
Expand Down
8 changes: 7 additions & 1 deletion src/Structs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct BattleConfig {
mapping(uint256 => EffectInstance) globalEffects;
mapping(uint256 => EffectInstance) p0Effects;
mapping(uint256 => EffectInstance) p1Effects;
mapping(uint256 => IEngineHook) engineHooks;
mapping(uint256 => EngineHookInstance) engineHooks;
}

struct EffectInstance {
Expand All @@ -101,6 +101,12 @@ struct EffectInstance {
bytes32 data; // 256 bits in slot 1
}

struct EngineHookInstance {
IEngineHook hook; // 160 bits (packed with stepsBitmap in slot 0)
uint16 stepsBitmap; // 16 bits - packs with hook in slot 0 (bit i = runs at EngineHookStep(i))
// 80 bits unused in slot 0
}

// View struct for getBattle - contains array instead of mapping for memory return
struct BattleConfigView {
IValidator validator;
Expand Down
7 changes: 7 additions & 0 deletions src/gacha/GachaRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {IMoveSet} from "../moves/IMoveSet.sol";
contract GachaRegistry is IMonRegistry, IEngineHook, IOwnableMon, IGachaRNG {
using EnumerableSetLib for EnumerableSetLib.Uint256Set;

// Only runs at OnBattleEnd (bit 3 = 0x08)
uint16 public constant STEPS_BITMAP = 0x08;

uint256 public constant INITIAL_ROLLS = 4;
uint256 public constant ROLL_COST = 7;
uint256 public constant POINTS_PER_WIN = 2;
Expand Down Expand Up @@ -118,6 +121,10 @@ contract GachaRegistry is IMonRegistry, IEngineHook, IOwnableMon, IGachaRNG {
}

// IEngineHook implementation
function getStepsBitmap() external pure override returns (uint16) {
return STEPS_BITMAP;
}

function onBattleStart(bytes32 battleKey) external override {}

function onRoundStart(bytes32 battleKey) external override {}
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/BattleHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {EnumerableSetLib} from "../lib/EnumerableSetLib.sol";
contract BattleHistory is IEngineHook {
using EnumerableSetLib for EnumerableSetLib.AddressSet;

// Only runs at OnBattleEnd (bit 3 = 0x08)
uint16 public constant STEPS_BITMAP = 0x08;

IEngine public immutable engine;

mapping(address => uint256) private _numBattles;

// Mapping from player address to set of all opponents fought
Expand All @@ -31,6 +34,10 @@ contract BattleHistory is IEngineHook {
engine = _engine;
}

function getStepsBitmap() external pure override returns (uint16) {
return STEPS_BITMAP;
}

function onBattleStart(bytes32 battleKey) external {}

function onRoundStart(bytes32 battleKey) external {}
Expand Down