Skip to content

Commit

Permalink
fixes starting stats not applied to adventurer (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
loothero authored Oct 12, 2023
1 parent e7b4a29 commit 1e5453e
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions contracts/game/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ mod Game {
_rotate_game_entropy(ref self);
}

// if the adventurer is on level 1, this is their first action of the game
if adventurer.get_level() == 1 {
// so we reveal their starting stats and store them in Adventurer Meta
let adventurer_meta = _handle_stat_reveal(@self, ref adventurer, adventurer_id);
// update adventurer meta data (this is the last time this storage slot is updated)
_pack_adventurer_meta(ref self, adventurer_id, adventurer_meta);
}

// get number of blocks between actions
let (idle, num_blocks) = _is_idle(immutable_adventurer, game_entropy);

Expand Down Expand Up @@ -1063,12 +1071,6 @@ mod Game {
ref self, ref adventurer, adventurer_id, xp_earned_items, attack_rnd_2
);

// if adventurer is on level 1, they are defeating their first beast
if (adventurer.get_level() == 1) {
// which means we can reveal their starting stats
_handle_stat_reveal(ref self, ref adventurer, adventurer_id);
}

// emit slayed beast event
__event_SlayedBeast(
ref self,
Expand Down Expand Up @@ -1109,13 +1111,14 @@ mod Game {
/// @param self A reference to the ContractState object.
/// @param adventurer A reference to the Adventurer object whose stats are to be revealed and set.
/// @param adventurer_id The unique identifier of the adventurer.
/// @return The adventurer's metadata.
fn _handle_stat_reveal(
ref self: ContractState, ref adventurer: Adventurer, adventurer_id: felt252
) {
self: @ContractState, ref adventurer: Adventurer, adventurer_id: felt252
) -> AdventurerMetadata {
// generate starting stats using the adventurer entropy which is based on the block hash of the block after
// the player committed to playing the game
let starting_stats = AdventurerUtils::generate_starting_stats(
_get_adventurer_entropy(@self, adventurer_id).into(), NUM_STARTING_STATS
_get_adventurer_entropy(self, adventurer_id).into(), NUM_STARTING_STATS
);

// adventurer shouldn't have any stats so save gas and overwrite
Expand All @@ -1126,9 +1129,9 @@ mod Game {
- STARTING_HEALTH;

// update adventurer meta with starting stats, this is last time we need to update adventurer meta data
let mut adventurer_meta = _unpack_adventurer_meta(@self, adventurer_id);
let mut adventurer_meta = _unpack_adventurer_meta(self, adventurer_id);
adventurer_meta.starting_stats = starting_stats;
_pack_adventurer_meta(ref self, adventurer_id, adventurer_meta);
adventurer_meta
}

fn _mint_beast(self: @ContractState, beast: Beast) {
Expand Down Expand Up @@ -2212,26 +2215,29 @@ mod Game {
let mut adventurer: Adventurer = self._adventurer.read(adventurer_id);

// start with no stat boosts
let mut stat_boosts = StatUtils::new();
let mut item_stat_boosts = StatUtils::new();

// if adventurer has item specials
if adventurer.has_item_specials() {
// get specials from storage
let (name_storage1, name_storage2) = _get_special_storages(self, adventurer_id);
// get resulting stat boosts
stat_boosts = adventurer.get_stat_boosts(name_storage1, name_storage2);
item_stat_boosts = adventurer.get_stat_boosts(name_storage1, name_storage2);
// apply stat boosts
adventurer.apply_stat_boosts(stat_boosts);
adventurer.apply_stat_boosts(item_stat_boosts);
}

let starting_stats = _unpack_adventurer_meta(self, adventurer_id).starting_stats;
adventurer.apply_stat_boosts(starting_stats);

// get bag from storage
let bag = _unpacked_bag(self, adventurer_id);

// luck isn't stored, it is calculated dynamically
adventurer.set_luck(bag);

// return adventurer with stat boosts
(adventurer, stat_boosts, bag)
// return adventurer, item stat boots, and bag
(adventurer, item_stat_boosts, bag)
}

#[inline(always)]
Expand Down Expand Up @@ -2470,17 +2476,6 @@ mod Game {
ImplLoot::is_starting_weapon(starting_weapon) == true, messages::INVALID_STARTING_WEAPON
);
}
fn _assert_starting_stats(starting_stats: Stats) {
let total_stats = starting_stats.strength
+ starting_stats.dexterity
+ starting_stats.vitality
+ starting_stats.intelligence
+ starting_stats.wisdom
+ starting_stats.charisma;
assert(total_stats == NUM_STARTING_STATS, messages::WRONG_NUM_STARTING_STATS);
_assert_zero_luck(starting_stats);
}

fn _assert_zero_luck(stats: Stats) {
assert(stats.luck == 0, messages::NON_ZERO_STARTING_LUCK);
}
Expand Down

1 comment on commit 1e5453e

@vercel
Copy link

@vercel vercel bot commented on 1e5453e Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.