Skip to content

Commit

Permalink
Use Rand library in Player code
Browse files Browse the repository at this point in the history
  • Loading branch information
mateofio committed Sep 21, 2020
1 parent f5a453c commit 0b47f37
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 66 deletions.
3 changes: 2 additions & 1 deletion src/algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "game_enemy.h"
#include "attribute.h"
#include "player.h"
#include "rand.h"
#include <lcf/rpg/skill.h>

#include <algorithm>
Expand Down Expand Up @@ -153,7 +154,7 @@ int VarianceAdjustEffect(int base, int var) {
// FIXME: RPG_RT 2k3 doesn't apply variance if negative attribute flips damage
if (var > 0 && (base > 0 || Player::IsLegacy())) {
int adj = std::max(1, var * base / 10);
return base + Utils::GetRandomNumber(0, adj) - adj / 2;
return base + Rand::GetRandomNumber(0, adj) - adj / 2;
}
return base;
}
Expand Down
3 changes: 2 additions & 1 deletion src/async_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <fstream>
#include "utils.h"
#include "transition.h"
#include "rand.h"

// When this option is enabled async requests are randomly delayed.
// This allows testing some aspects of async file fetching locally.
Expand Down Expand Up @@ -231,7 +232,7 @@ void FileRequestAsync::UpdateProgress() {
#ifndef EMSCRIPTEN
// Fake download for testing event handlers

if (!IsReady() && Utils::ChanceOf(1, 100)) {
if (!IsReady() && Rand::ChanceOf(1, 100)) {
DownloadDone(true);
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/game_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "pending_message.h"
#include "compiler.h"
#include "attribute.h"
#include "rand.h"

constexpr int max_level_2k = 50;
constexpr int max_level_2k3 = 99;
Expand Down Expand Up @@ -809,7 +810,7 @@ const lcf::rpg::Skill* Game_Actor::GetRandomSkill() const {
}

// Skills are guaranteed to be valid
return lcf::ReaderUtil::GetElement(lcf::Data::skills, skills[Utils::GetRandomNumber(0, skills.size() - 1)]);
return lcf::ReaderUtil::GetElement(lcf::Data::skills, skills[Rand::GetRandomNumber(0, skills.size() - 1)]);
}

Point Game_Actor::GetOriginalPosition() const {
Expand Down
2 changes: 2 additions & 0 deletions src/game_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "spriteset_battle.h"
#include "output.h"
#include "utils.h"
#include "rand.h"

namespace Game_Battle {
const lcf::rpg::Troop* troop = nullptr;
Expand Down Expand Up @@ -709,3 +710,4 @@ Point Game_Battle::Calculate2k3BattlePosition(const Game_Actor& actor) {

return position;
}

35 changes: 18 additions & 17 deletions src/game_battlealgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <lcf/rpg/item.h>
#include "sprite_battler.h"
#include "utils.h"
#include "rand.h"
#include "state.h"
#include "algo.h"
#include "attribute.h"
Expand All @@ -66,7 +67,7 @@ static void BattlePhysicalStateHeal(int physical_rate, std::vector<int16_t>& tar
if (state->release_by_damage > 0) {
int release_chance = state->release_by_damage * physical_rate / 100;

if (!Utils::ChanceOf(release_chance, 100)) {
if (!Rand::ChanceOf(release_chance, 100)) {
continue;
}

Expand Down Expand Up @@ -932,8 +933,8 @@ bool Game_BattleAlgorithm::Normal::Execute() {
auto crit_chance = Algo::CalcCriticalHitChance(source, target, Game_Battler::WeaponAll);

// Damage calculation
if (Utils::PercentChance(to_hit)) {
if (Utils::PercentChance(crit_chance)) {
if (Rand::PercentChance(to_hit)) {
if (Rand::PercentChance(crit_chance)) {
critical_hit = true;
}

Expand Down Expand Up @@ -985,7 +986,7 @@ bool Game_BattleAlgorithm::Normal::Execute() {
if (!weapon_heals_states) {
pct = pct * GetTarget()->GetStateProbability(state_id) / 100;
}
if (!Utils::PercentChance(pct)) {
if (!Rand::PercentChance(pct)) {
return false;
}
if (weapon_heals_states) {
Expand Down Expand Up @@ -1161,17 +1162,17 @@ bool Game_BattleAlgorithm::Skill::Execute() {
this->hp = std::max<int>(0, std::min<int>(effect, GetTarget()->GetMaxHp() - GetTarget()->GetHp()));
}
}
if (skill.affect_sp && Utils::PercentChance(to_hit)) {
if (skill.affect_sp && Rand::PercentChance(to_hit)) {
int sp_cost = GetSource() == GetTarget() ? source->CalculateSkillCost(skill.ID) : 0;
this->sp = std::max<int>(0, std::min<int>(effect, GetTarget()->GetMaxSp() - GetTarget()->GetSp() + sp_cost));
}
if (skill.affect_attack && Utils::PercentChance(to_hit))
if (skill.affect_attack && Rand::PercentChance(to_hit))
this->attack = std::max<int>(0, std::min<int>(effect, std::min<int>(GetTarget()->MaxStatBattleValue(), GetTarget()->GetBaseAtk() * 2) - GetTarget()->GetAtk()));
if (skill.affect_defense && Utils::PercentChance(to_hit))
if (skill.affect_defense && Rand::PercentChance(to_hit))
this->defense = std::max<int>(0, std::min<int>(effect, std::min<int>(GetTarget()->MaxStatBattleValue(), GetTarget()->GetBaseDef() * 2) - GetTarget()->GetDef()));
if (skill.affect_spirit && Utils::PercentChance(to_hit))
if (skill.affect_spirit && Rand::PercentChance(to_hit))
this->spirit = std::max<int>(0, std::min<int>(effect, std::min<int>(GetTarget()->MaxStatBattleValue(), GetTarget()->GetBaseSpi() * 2) - GetTarget()->GetSpi()));
if (skill.affect_agility && Utils::PercentChance(to_hit))
if (skill.affect_agility && Rand::PercentChance(to_hit))
this->agility = std::max<int>(0, std::min<int>(effect, std::min<int>(GetTarget()->MaxStatBattleValue(), GetTarget()->GetBaseAgi() * 2) - GetTarget()->GetAgi()));

this->success = GetAffectedHp() != -1 || GetAffectedSp() != -1 || GetAffectedAttack() > 0
Expand Down Expand Up @@ -1199,17 +1200,17 @@ bool Game_BattleAlgorithm::Skill::Execute() {
}
}

if (skill.affect_sp && Utils::PercentChance(to_hit)) {
if (skill.affect_sp && Rand::PercentChance(to_hit)) {
this->sp = std::min<int>(effect, GetTarget()->GetSp());
}

if (skill.affect_attack && Utils::PercentChance(to_hit))
if (skill.affect_attack && Rand::PercentChance(to_hit))
this->attack = std::max<int>(0, std::min<int>(effect, GetTarget()->GetAtk() - (GetTarget()->GetBaseAtk() + 1) / 2));
if (skill.affect_defense && Utils::PercentChance(to_hit))
if (skill.affect_defense && Rand::PercentChance(to_hit))
this->defense = std::max<int>(0, std::min<int>(effect, GetTarget()->GetDef() - (GetTarget()->GetBaseDef() + 1) / 2));
if (skill.affect_spirit && Utils::PercentChance(to_hit))
if (skill.affect_spirit && Rand::PercentChance(to_hit))
this->spirit = std::max<int>(0, std::min<int>(effect, GetTarget()->GetSpi() - (GetTarget()->GetBaseSpi() + 1) / 2));
if (skill.affect_agility && Utils::PercentChance(to_hit))
if (skill.affect_agility && Rand::PercentChance(to_hit))
this->agility = std::max<int>(0, std::min<int>(effect, GetTarget()->GetAgi() - (GetTarget()->GetBaseAgi() + 1) / 2));

if (skill.affect_hp) {
Expand Down Expand Up @@ -1268,7 +1269,7 @@ bool Game_BattleAlgorithm::Skill::Execute() {
if (heals_states && !target_has_state) {
continue;
}
if (!Utils::PercentChance(to_hit)) {
if (!Rand::PercentChance(to_hit)) {
continue;
}

Expand All @@ -1278,7 +1279,7 @@ bool Game_BattleAlgorithm::Skill::Execute() {
if (State::Remove(state_id, target_states, target_perm_states)) {
states.push_back({state_id, StateEffect::Healed});
}
} else if (Utils::PercentChance(GetTarget()->GetStateProbability(state_id))) {
} else if (Rand::PercentChance(GetTarget()->GetStateProbability(state_id))) {
if (State::Add(state_id, target_states, target_perm_states, true)) {
this->success = true;
states.push_back({state_id, StateEffect::Inflicted});
Expand All @@ -1294,7 +1295,7 @@ bool Game_BattleAlgorithm::Skill::Execute() {
if (skill.affect_attr_defence) {
for (int i = 0; i < static_cast<int>(skill.attribute_effects.size()); i++) {
if (skill.attribute_effects[i] && GetTarget()->CanShiftAttributeRate(i + 1, IsPositive() ? 1 : -1)) {
if (!Utils::PercentChance(to_hit))
if (!Rand::PercentChance(to_hit))
continue;
shift_attributes.push_back(i + 1);
this->success = true;
Expand Down
3 changes: 2 additions & 1 deletion src/game_battler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "shake.h"
#include "attribute.h"
#include "algo.h"
#include "rand.h"

Game_Battler::Game_Battler() {
ResetBattle();
Expand Down Expand Up @@ -541,7 +542,7 @@ std::vector<int16_t> Game_Battler::BattleStateHeal() {
for (size_t i = 0; i < states.size(); ++i) {
if (HasState(i + 1)) {
if (states[i] > lcf::Data::states[i].hold_turn
&& Utils::ChanceOf(lcf::Data::states[i].auto_release_prob, 100)
&& Rand::ChanceOf(lcf::Data::states[i].auto_release_prob, 100)
&& RemoveState(i + 1, false)
) {
healed_states.push_back(i + 1);
Expand Down
5 changes: 3 additions & 2 deletions src/game_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "utils.h"
#include "util_macro.h"
#include "output.h"
#include "rand.h"
#include <cmath>
#include <cassert>

Expand Down Expand Up @@ -508,7 +509,7 @@ void Game_Character::Turn180Degree() {
}

void Game_Character::Turn90DegreeLeftOrRight() {
if (Utils::ChanceOf(1,2)) {
if (Rand::ChanceOf(1,2)) {
Turn90DegreeLeft();
} else {
Turn90DegreeRight();
Expand Down Expand Up @@ -546,7 +547,7 @@ void Game_Character::TurnAwayFromHero() {
}

void Game_Character::TurnRandom() {
SetDirection(Utils::GetRandomNumber(0, 3));
SetDirection(Rand::GetRandomNumber(0, 3));
}

void Game_Character::Wait() {
Expand Down
5 changes: 3 additions & 2 deletions src/game_enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "utils.h"
#include "player.h"
#include "attribute.h"
#include "rand.h"

namespace {
constexpr int levitation_frame_count = 14;
Expand All @@ -45,7 +46,7 @@ Game_Enemy::Game_Enemy(const lcf::rpg::TroopMember* member)
hp = GetMaxHp();
sp = GetMaxSp();
SetHidden(troop_member->invisible);
cycle = Utils::GetRandomNumber(0, levitation_frame_count - 1) * levitation_frame_cycle;
cycle = Rand::GetRandomNumber(0, levitation_frame_count - 1) * levitation_frame_cycle;

SetBattlePosition(GetOriginalPosition());
}
Expand Down Expand Up @@ -227,7 +228,7 @@ const lcf::rpg::EnemyAction* Game_Enemy::ChooseRandomAction() {
return nullptr;
}

int which = Utils::GetRandomNumber(0, total - 1);
int which = Rand::GetRandomNumber(0, total - 1);
for (std::vector<int>::const_iterator it = valid.begin(); it != valid.end(); ++it) {
const lcf::rpg::EnemyAction& action = actions[*it];
if (which >= action.rating) {
Expand Down
5 changes: 3 additions & 2 deletions src/game_enemyparty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <lcf/reader_util.h>
#include "utils.h"
#include "output.h"
#include "rand.h"

Game_EnemyParty::Game_EnemyParty() {
}
Expand Down Expand Up @@ -72,7 +73,7 @@ void Game_EnemyParty::ResetBattle(int battle_troop_id) {
continue;
}

if (Utils::PercentChance(40)) {
if (Rand::PercentChance(40)) {
enemy.SetHidden(true);
--non_hidden;
}
Expand Down Expand Up @@ -115,7 +116,7 @@ void Game_EnemyParty::GenerateDrops(std::vector<int>& out) const {
if (enemy.IsDead()) {
// Only roll if the enemy has something to drop
if (enemy.GetDropId() != 0) {
if (Utils::ChanceOf(enemy.GetDropProbability(), 100)) {
if (Rand::ChanceOf(enemy.GetDropProbability(), 100)) {
out.push_back(enemy.GetDropId());
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/game_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "main_data.h"
#include "player.h"
#include "utils.h"
#include "rand.h"
#include "output.h"
#include <cmath>
#include <cassert>
Expand Down Expand Up @@ -464,12 +465,12 @@ void Game_Event::UpdateNextMovementAction() {

void Game_Event::SetMaxStopCountForRandom() {
auto st = GetMaxStopCountForStep(GetMoveFrequency());
st *= (Utils::GetRandomNumber(0, 3) + 3) / 5;
st *= (Rand::GetRandomNumber(0, 3) + 3) / 5;
SetMaxStopCount(st);
}

void Game_Event::MoveTypeRandom() {
int draw = Utils::GetRandomNumber(0, 9);
int draw = Rand::GetRandomNumber(0, 9);

const auto prev_dir = GetDirection();

Expand All @@ -481,7 +482,7 @@ void Game_Event::MoveTypeRandom() {
} else if (draw < 8) {
Turn180Degree();
} else {
SetStopCount(Utils::GetRandomNumber(0, GetMaxStopCount()));
SetStopCount(Rand::GetRandomNumber(0, GetMaxStopCount()));
return;
}

Expand Down Expand Up @@ -553,13 +554,13 @@ void Game_Event::MoveTypeTowardsOrAwayPlayer(bool towards) {

int dir = 0;
if (!in_sight) {
dir = Utils::GetRandomNumber(0, 3);
dir = Rand::GetRandomNumber(0, 3);
} else {
int draw = Utils::GetRandomNumber(0, 9);
int draw = Rand::GetRandomNumber(0, 9);
if (draw == 0) {
dir = GetDirection();
} else if(draw == 1) {
dir = Utils::GetRandomNumber(0, 3);
dir = Rand::GetRandomNumber(0, 3);
} else {
dir = towards
? GetDirectionToHero()
Expand Down
3 changes: 2 additions & 1 deletion src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "transition.h"
#include "baseui.h"
#include "algo.h"
#include "rand.h"

enum BranchSubcommand {
eOptionBranchElse = 1
Expand Down Expand Up @@ -980,7 +981,7 @@ bool Game_Interpreter::CommandControlVariables(lcf::rpg::EventCommand const& com
// Random between range
int rmax = max(com.parameters[5], com.parameters[6]);
int rmin = min(com.parameters[5], com.parameters[6]);
value = Utils::GetRandomNumber(rmin, rmax);
value = Rand::GetRandomNumber(rmin, rmax);
break;
}
case 4:
Expand Down
5 changes: 3 additions & 2 deletions src/game_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "player.h"
#include "input.h"
#include "utils.h"
#include "rand.h"
#include <lcf/scope_guard.h>
#include "scene_gameover.h"

Expand Down Expand Up @@ -1219,9 +1220,9 @@ bool Game_Map::PrepareEncounter(BattleArgs& args) {
return false;
}

args.troop_id = encounters[Utils::GetRandomNumber(0, encounters.size() - 1)];
args.troop_id = encounters[Rand::GetRandomNumber(0, encounters.size() - 1)];

if (Utils::GetRandomNumber(1, 32) == 1) {
if (Rand::GetRandomNumber(1, 32) == 1) {
args.first_strike = true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/game_party_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <list>
#include "game_party_base.h"
#include "utils.h"
#include "rand.h"

Game_Party_Base::~Game_Party_Base() {
}
Expand Down Expand Up @@ -86,7 +87,7 @@ Game_Battler* Game_Party_Base::GetRandomActiveBattler() {
if (battlers.empty()) {
return NULL;
}
return battlers[Utils::GetRandomNumber(0, battlers.size() - 1)];
return battlers[Rand::GetRandomNumber(0, battlers.size() - 1)];
}

Game_Battler* Game_Party_Base::GetRandomDeadBattler() {
Expand All @@ -96,7 +97,7 @@ Game_Battler* Game_Party_Base::GetRandomDeadBattler() {
return NULL;
}

return battlers[Utils::GetRandomNumber(0, battlers.size() - 1)];
return battlers[Rand::GetRandomNumber(0, battlers.size() - 1)];
}

bool Game_Party_Base::IsAnyActive() {
Expand Down
3 changes: 2 additions & 1 deletion src/game_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "util_macro.h"
#include "game_switches.h"
#include "output.h"
#include "rand.h"
#include "utils.h"
#include <lcf/reader_util.h>
#include <lcf/scope_guard.h>
Expand Down Expand Up @@ -705,7 +706,7 @@ void Game_Player::UpdateEncounterSteps() {
const auto pmod = row.pmod;
const auto p = (1.0f / float(encounter_rate)) * pmod * (float(terrain->encounter_rate) / 100.0f);

if (!Utils::PercentChance(p)) {
if (!Rand::PercentChance(p)) {
return;
}

Expand Down
Loading

0 comments on commit 0b47f37

Please sign in to comment.