Skip to content

Commit

Permalink
fixes flee outcome not changing during battle
Browse files Browse the repository at this point in the history
  • Loading branch information
loothero committed Sep 16, 2023
1 parent 7e3a71e commit 6069cf2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
22 changes: 20 additions & 2 deletions contracts/adventurer/src/adventurer_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ impl AdventurerUtils of IAdventurerUtils {
// @param adventurer_entropy: adventurer entropy
// @param global_entropy: global entropy
// @return (u128, u128): tuple of randomness
fn get_randomness(adventurer_xp: u16,
adventurer_entropy: u128, global_entropy: u128
fn get_randomness(
adventurer_xp: u16, adventurer_entropy: u128, global_entropy: u128
) -> (u128, u128) {
let mut hash_span = ArrayTrait::<felt252>::new();
hash_span.append(adventurer_xp.into());
Expand All @@ -191,6 +191,24 @@ impl AdventurerUtils of IAdventurerUtils {
AdventurerUtils::split_hash(poseidon)
}

// @notice gets randomness for adventurer with health included in entropy
// @param adventurer_xp: adventurer xp
// @param adventurer_entropy: adventurer entropy
// @param adventurer_health: adventurer health
// @param global_entropy: global entropy
// @return (u128, u128): tuple of randomness
fn get_randomness_with_health(
adventurer_xp: u16, adventurer_health: u16, adventurer_entropy: u128, global_entropy: u128
) -> (u128, u128) {
let mut hash_span = ArrayTrait::<felt252>::new();
hash_span.append(adventurer_xp.into());
hash_span.append(adventurer_health.into());
hash_span.append(adventurer_entropy.into());
hash_span.append(global_entropy.into());
let poseidon = poseidon_hash_span(hash_span.span());
AdventurerUtils::split_hash(poseidon)
}

// @notice splits hash into two u128s
// @param felt_to_split: felt to split
// @return (u128, u128): tuple of u128s
Expand Down
12 changes: 7 additions & 5 deletions contracts/game/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ mod Game {
_adventurer: LegacyMap::<u256, felt252>,
_owner: LegacyMap::<u256, ContractAddress>,
_adventurer_meta: LegacyMap::<u256, felt252>,
_loot: LegacyMap::<u256, felt252>,
_loot_special_names: LegacyMap::<(u256, u256), felt252>,
_bag: LegacyMap::<u256, felt252>,
_counter: u256,
Expand Down Expand Up @@ -945,6 +944,8 @@ mod Game {
contract_address: self._collectible_beasts.read()
};

let allow_list = collectible_beasts_contract.getWhitelist();

let is_beast_minted = collectible_beasts_contract
.isMinted(
beast.id, beast.combat_spec.specials.special2, beast.combat_spec.specials.special3
Expand Down Expand Up @@ -1615,8 +1616,8 @@ mod Game {
// When generating the beast, we need to ensure entropy remains fixed for the battle
// for attacking however, we should change the entropy during battle so we use adventurer and beast health
// to accomplish this
let (attack_rnd_1, attack_rnd_2) = AdventurerUtils::get_randomness(
adventurer.xp, adventurer_entropy, global_entropy
let (attack_rnd_1, attack_rnd_2) = AdventurerUtils::get_randomness_with_health(
adventurer.xp, adventurer.health, adventurer_entropy, global_entropy
);

// get the damage dealt to the beast
Expand Down Expand Up @@ -1825,8 +1826,8 @@ mod Game {
flee_to_the_death: bool
) {
// get flee and ambush entropy seeds
let (flee_entropy, ambush_entropy) = AdventurerUtils::get_randomness(
adventurer.xp, adventurer_entropy, global_entropy
let (flee_entropy, ambush_entropy) = AdventurerUtils::get_randomness_with_health(
adventurer.xp, adventurer.health, adventurer_entropy, global_entropy
);

// attempt to flee
Expand Down Expand Up @@ -3177,5 +3178,6 @@ mod Game {
ref self: T, to: ContractAddress, beast: u8, prefix: u8, suffix: u8, level: felt252
);
fn isMinted(ref self: T, beast: u8, prefix: u8, suffix: u8) -> bool;
fn getWhitelist(self: @T) -> ContractAddress;
}
}

0 comments on commit 6069cf2

Please sign in to comment.