Skip to content

Commit

Permalink
Refactored compute_creature_max_health(#3792)
Browse files Browse the repository at this point in the history
no functional changes
  • Loading branch information
walt253 authored Jan 10, 2025
1 parent a591ad5 commit 6f613a3
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 119 deletions.
2 changes: 1 addition & 1 deletion src/config_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct CreatureRulesConfig {
unsigned long flee_zone_radius;
GameTurnDelta game_turns_in_flee;
unsigned short game_turns_unconscious;
long critical_health_permil;
HitPoints critical_health_permil;
unsigned char stun_enemy_chance_evil;
unsigned char stun_enemy_chance_good;
};
Expand Down
28 changes: 17 additions & 11 deletions src/creature_states_lair.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,42 +512,48 @@ long room_has_slab_adjacent(const struct Room *room, long slbkind)

short creature_sleep(struct Thing *thing)
{
struct CreatureControl* cctrl = creature_control_get_from_thing(thing);
if (creature_affected_by_slap(thing) || !creature_will_sleep(thing)) {
struct CreatureControl *cctrl = creature_control_get_from_thing(thing);
if (creature_affected_by_slap(thing) || !creature_will_sleep(thing))
{
set_start_state(thing);
return 0;
}
struct Room* room = get_room_thing_is_on(thing);
if (room_is_invalid(room) || (!room_role_matches(room->kind,get_room_role_for_job(Job_TAKE_SLEEP)))
|| (cctrl->lair_room_id != room->index) || (room->owner != thing->owner)) {
struct Room *room = get_room_thing_is_on(thing);
if (room_is_invalid(room)
|| (!room_role_matches(room->kind, get_room_role_for_job(Job_TAKE_SLEEP)))
|| (cctrl->lair_room_id != room->index)
|| (room->owner != thing->owner))
{
set_start_state(thing);
return 0;
}
thing->movement_flags &= ~0x0020;
struct CreatureStats* crstat = creature_stats_get_from_thing(thing);
struct CreatureStats *crstat = creature_stats_get_from_thing(thing);
// Recovery is disabled if frequency is set to 0 on rules.cfg.
if (game.conf.rules.creature.recovery_frequency > 0)
{
if (((game.play_gameturn + thing->index) % game.conf.rules.creature.recovery_frequency) == 0)
{
HitPoints recover = compute_creature_max_health(crstat->sleep_recovery, cctrl->explevel, thing->owner);
HitPoints recover = compute_creature_max_health(crstat->sleep_recovery, cctrl->explevel);
apply_health_to_thing_and_display_health(thing, recover);
}
}
anger_set_creature_anger(thing, 0, AngR_NoLair);
anger_apply_anger_to_creature(thing, crstat->annoy_sleeping, AngR_Other, 1);
if (cctrl->turns_at_job > 0) {
if (cctrl->turns_at_job > 0)
{
cctrl->turns_at_job--;
}
if (((game.play_gameturn + thing->index) & 0x3F) == 0)
{
if (CREATURE_RANDOM(thing, 100) < 5) {
struct Dungeon* dungeon = get_dungeon(thing->owner);
if (CREATURE_RANDOM(thing, 100) < 5)
{
struct Dungeon *dungeon = get_dungeon(thing->owner);
dungeon->lvstats.backs_stabbed++;
}
}
if (crstat->sleep_exp_slab != SlbT_ROCK)
{
{ // To think about: Should SlbT_ROCK be ignored? Settings the experience gain to 0 is enough to disable the feature.
if (creature_can_gain_experience(thing) && room_has_slab_adjacent(room, crstat->sleep_exp_slab))
{
cctrl->exp_points += crstat->sleep_experience;
Expand Down
12 changes: 8 additions & 4 deletions src/creature_states_spdig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1502,17 +1502,21 @@ short imp_toking(struct Thing *creatng)
}
if (cctrl->instance_id == CrInst_NULL)
{
if ( CREATURE_RANDOM(creatng, 8) )
if (CREATURE_RANDOM(creatng, 8))
{
set_creature_instance(creatng, CrInst_RELAXING, 0, 0);
}
else
{
set_creature_instance(creatng, CrInst_TOKING, 0, 0);
}
}

if ((cctrl->instance_id == CrInst_TOKING) && (cctrl->inst_turn == cctrl->inst_action_turns))
{
struct CreatureStats* crstat = creature_stats_get_from_thing(creatng);
if (crstat->toking_recovery != 0) {
HitPoints recover = compute_creature_max_health(crstat->toking_recovery, cctrl->explevel, creatng->owner);
if (crstat->toking_recovery != 0)
{
HitPoints recover = compute_creature_max_health(crstat->toking_recovery, cctrl->explevel);
apply_health_to_thing_and_display_health(creatng, recover);
}
}
Expand Down
38 changes: 21 additions & 17 deletions src/creature_states_tortr.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,10 @@ long compute_torture_broke_chance(const struct Thing *thing)
CrCheckRet process_torture_function(struct Thing *creatng)
{
long i;
struct Room* room = get_room_creature_works_in(creatng);
if ( !room_still_valid_as_type_for_thing(room,RoRoF_Torture,creatng) )
struct Room *room = get_room_creature_works_in(creatng);
if (!room_still_valid_as_type_for_thing(room, RoRoF_Torture, creatng))
{
WARNLOG("Room %s owned by player %d is bad work place for %s owned by played %d",room_code_name(room->kind),(int)room->owner,thing_model_name(creatng),(int)creatng->owner);
WARNLOG("Room %s owned by player %d is bad work place for %s owned by played %d", room_code_name(room->kind), (int)room->owner, thing_model_name(creatng), (int)creatng->owner);
set_start_state(creatng);
return CrCkRet_Continue;
}
Expand All @@ -516,16 +516,16 @@ CrCheckRet process_torture_function(struct Thing *creatng)
return CrCkRet_Available;
}
}
struct CreatureStats* crstat = creature_stats_get_from_thing(creatng);
struct CreatureControl* cctrl = creature_control_get_from_thing(creatng);
struct CreatureStats *crstat = creature_stats_get_from_thing(creatng);
struct CreatureControl *cctrl = creature_control_get_from_thing(creatng);
anger_apply_anger_to_creature(creatng, crstat->annoy_in_torture, AngR_Other, 1);
if ((long)game.play_gameturn >= cctrl->turns_at_job + game.conf.rules.health.turns_per_torture_health_loss)
{
i = compute_creature_max_health(game.conf.rules.health.torture_health_loss,cctrl->explevel,creatng->owner);
remove_health_from_thing_and_display_health(creatng, i);
HitPoints torture_damage = compute_creature_max_health(game.conf.rules.health.torture_health_loss, cctrl->explevel);
remove_health_from_thing_and_display_health(creatng, torture_damage);
cctrl->turns_at_job = (long)game.play_gameturn;
}
// Check if we should convert the creature into ghost
// Check if we should convert the creature into ghost.
if ((creatng->health < 0) && (game.conf.rules.rooms.ghost_convert_chance > 0))
{
if (CREATURE_RANDOM(creatng, 100) < game.conf.rules.rooms.ghost_convert_chance)
Expand All @@ -534,32 +534,36 @@ CrCheckRet process_torture_function(struct Thing *creatng)
return CrCkRet_Deleted;
}
}
// Other torture functions are available only when torturing enemies
// Other torture functions are available only when torturing enemies.
if (room->owner == creatng->owner)
{
return CrCkRet_Available;
// Torture must take some time before it has any affect
i = compute_torture_convert_time(creatng,room);
if ( (i < crstat->torture_break_time) || (cctrl->tortured.assigned_torturer == 0) )
}
// Torture must take some time before it has any affect.
i = compute_torture_convert_time(creatng, room);
if ((i < crstat->torture_break_time) || (cctrl->tortured.assigned_torturer == 0))
{
return CrCkRet_Available;
// After that, every time broke chance is hit, do something
}
// After that, every time broke chance is hit, do something.
if (CREATURE_RANDOM(creatng, 100) < compute_torture_broke_chance(creatng))
{
if (CREATURE_RANDOM(creatng, 100) >= (int)game.conf.rules.rooms.torture_death_chance)
{
SYNCDBG(4, "The %s has been broken", thing_model_name(creatng));

if (CREATURE_RANDOM(creatng, 100) < (int)game.conf.rules.rooms.torture_convert_chance)
{ // converting creature and ending the torture
{ // Converting creature and ending the torture.
convert_tortured_creature_owner(creatng, room->owner);
return CrCkRet_Continue;
}
else
{ // revealing information about enemy and continuing the torture
{ // Revealing information about enemy and continuing the torture.
cctrl->tortured.start_gameturn = (long)game.play_gameturn - (long)crstat->torture_break_time / 2;
reveal_players_map_to_player(creatng, room->owner);
return CrCkRet_Available;
}
} else
}
else
{
SYNCDBG(4, "The %s died from torture", thing_model_name(creatng));
if (CREATURE_RANDOM(creatng, 100) < game.conf.rules.rooms.ghost_convert_chance)
Expand Down
21 changes: 11 additions & 10 deletions src/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,38 +584,39 @@ void slap_creature(struct PlayerInfo *player, struct Thing *thing)
long i;
crstat = creature_stats_get_from_thing(thing);
cctrl = creature_control_get_from_thing(thing);

anger_apply_anger_to_creature(thing, crstat->annoy_slapped, AngR_Other, 1);
if (crstat->slaps_to_kill > 0)
{
i = compute_creature_max_health(crstat->health,cctrl->explevel,thing->owner) / crstat->slaps_to_kill;
apply_damage_to_thing_and_display_health(thing, i, player->id_number);
HitPoints slap_damage = calculate_correct_creature_max_health(thing) / crstat->slaps_to_kill;
apply_damage_to_thing_and_display_health(thing, slap_damage, player->id_number);
}
pwrdynst = get_power_dynamic_stats(PwrK_SLAP);
i = cctrl->slap_turns;
cctrl->slap_turns = pwrdynst->duration;
if (i == 0)
cctrl->max_speed = calculate_correct_creature_maxspeed(thing);
{
cctrl->max_speed = calculate_correct_creature_maxspeed(thing);
}
if (thing->active_state != CrSt_CreatureSlapCowers)
{
clear_creature_instance(thing);
if (thing->active_state != CrSt_CreatureCastingPreparation)
{
// If the creature was in CreatureCastingPreparation state, its active and continue
// states have been assigned to those bkp states, so we don't need to assign them again.
// If the creature was in CreatureCastingPreparation state,
// its active and continue states have been assigned to those bkp states,
// so we don't need to assign them again.
cctrl->active_state_bkp = thing->active_state;
cctrl->continue_state_bkp = thing->continue_state;
}
creature_mark_if_woken_up(thing);
external_set_thing_state(thing, CrSt_CreatureSlapCowers);
}
cctrl->frozen_on_hit = 6;
cctrl->cowers_from_slap_turns = 18;
cctrl->frozen_on_hit = 6; // Could be configurable.
cctrl->cowers_from_slap_turns = 18; // Could be configurable.
play_creature_sound(thing, CrSnd_Slap, 3, 0);
}

TbBool can_cast_power_at_xy(PlayerNumber plyr_idx, PowerKind pwkind,
MapSubtlCoord stl_x, MapSubtlCoord stl_y, unsigned long allow_flags)
TbBool can_cast_power_at_xy(PlayerNumber plyr_idx, PowerKind pwkind, MapSubtlCoord stl_x, MapSubtlCoord stl_y, unsigned long allow_flags)
{
struct Map *mapblk;
struct SlabMap *slb;
Expand Down
9 changes: 3 additions & 6 deletions src/player_comptask.c
Original file line number Diff line number Diff line change
Expand Up @@ -2838,12 +2838,9 @@ struct Thing *find_creature_for_defend_pickup(struct Computer2 *comp)
{
struct PerExpLevelValues* expvalues;
expvalues = &game.creature_scores[thing->model];
long expval;
long healthprm;
long new_factor;
expval = expvalues->value[cctrl->explevel];
healthprm = get_creature_health_permil(thing);
new_factor = healthprm * expval / 1000;
long expval = expvalues->value[cctrl->explevel];
HitPoints healthprm = get_creature_health_permil(thing);
HitPoints new_factor = healthprm * expval / 1000;
if ((new_factor > best_factor) && (healthprm > 20))
{
best_factor = new_factor;
Expand Down
Loading

0 comments on commit 6f613a3

Please sign in to comment.