forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_battler.cpp
764 lines (659 loc) · 21.6 KB
/
game_battler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include "player.h"
#include "game_battler.h"
#include "game_actor.h"
#include "game_enemyparty.h"
#include "game_battle.h"
#include "game_party.h"
#include "game_party_base.h"
#include "game_player.h"
#include "game_switches.h"
#include "game_system.h"
#include "game_targets.h"
#include "game_screen.h"
#include "util_macro.h"
#include "main_data.h"
#include "utils.h"
#include "output.h"
#include <lcf/reader_util.h>
#include "game_battlealgorithm.h"
#include "state.h"
#include "shake.h"
#include "attribute.h"
#include "algo.h"
#include "rand.h"
Game_Battler::Game_Battler() {
}
bool Game_Battler::HasState(int state_id) const {
return State::Has(state_id, GetStates());
}
std::vector<int16_t> Game_Battler::GetInflictedStates() const {
auto& states = GetStates();
std::vector<int16_t> inf_states;
for (size_t i = 0; i < states.size(); ++i) {
if (states[i] > 0) {
inf_states.push_back(i + 1);
}
}
return inf_states;
}
PermanentStates Game_Battler::GetPermanentStates() const {
return PermanentStates();
}
bool Game_Battler::EvadesAllPhysicalAttacks() const {
for (auto state_id: GetInflictedStates()) {
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, state_id);
if (state && state->avoid_attacks) {
return true;
}
}
return false;
}
lcf::rpg::State::Restriction Game_Battler::GetSignificantRestriction() const {
return State::GetSignificantRestriction(GetStates());
}
bool Game_Battler::CanAct() const {
const auto& states = GetStates();
for (size_t i = 0; i < states.size(); ++i) {
if (states[i] > 0) {
const auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, i + 1);
assert(state);
if (state->restriction == lcf::rpg::State::Restriction_do_nothing) {
return false;
}
}
}
return true;
}
bool Game_Battler::CanActOrRecoverable() const {
const auto& states = GetStates();
for (size_t i = 0; i < states.size(); ++i) {
if (states[i] > 0) {
const auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, i + 1);
assert(state);
if (state->restriction == lcf::rpg::State::Restriction_do_nothing && state->auto_release_prob == 0) {
return false;
}
}
}
return true;
}
const lcf::rpg::State* Game_Battler::GetSignificantState() const {
return State::GetSignificantState(GetStates());
}
int Game_Battler::GetStateRate(int state_id, int rate) const {
return State::GetStateRate(state_id, rate);
}
bool Game_Battler::IsSkillUsable(int skill_id) const {
const lcf::rpg::Skill* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (!skill) {
Output::Warning("IsSkillUsable: Invalid skill ID {}", skill_id);
return false;
}
if (CalculateSkillCost(skill_id) > GetSp() || CalculateSkillHpCost(skill_id) >= GetHp()) {
return false;
}
for (auto state_id: GetInflictedStates()) {
const auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, state_id);
if (state) {
if (state->restrict_skill && skill->physical_rate >= state->restrict_skill_level && !skill->easyrpg_ignore_restrict_skill) {
return false;
}
if (state->restrict_magic && skill->magical_rate >= state->restrict_magic_level && !skill->easyrpg_ignore_restrict_magic) {
return false;
}
}
}
return Algo::IsSkillUsable(*skill, true);
}
bool Game_Battler::UseItem(int item_id, const Game_Battler* source) {
const lcf::rpg::Item* item = lcf::ReaderUtil::GetElement(lcf::Data::items, item_id);
if (!item) {
Output::Warning("UseItem: Can't use item with invalid ID {}", item_id);
return false;
}
if (item->type == lcf::rpg::Item::Type_medicine) {
bool was_used = false;
int revived = 0;
int hp_change = item->recover_hp_rate * GetMaxHp() / 100 + item->recover_hp;
int sp_change = item->recover_sp_rate * GetMaxSp() / 100 + item->recover_sp;
if (IsDead()) {
// Check if item can revive
if (item->state_set.empty() || !item->state_set[0]) {
return false;
}
} else if (item->ko_only) {
// Must be dead
return false;
}
for (int i = 0; i < (int)item->state_set.size(); i++) {
if (item->state_set[i]) {
was_used |= HasState(lcf::Data::states[i].ID);
if (i == 0 && HasState(i + 1))
revived = 1;
RemoveState(lcf::Data::states[i].ID, false);
}
}
if (hp_change > 0 && !HasFullHp()) {
ChangeHp(hp_change - revived, false);
was_used = true;
}
if (sp_change > 0 && !HasFullSp()) {
ChangeSp(sp_change);
was_used = true;
}
return was_used;
}
if (item->type == lcf::rpg::Item::Type_switch) {
return true;
}
bool do_skill = (item->type == lcf::rpg::Item::Type_special)
|| (item->use_skill && (
item->type == lcf::rpg::Item::Type_weapon
|| item->type == lcf::rpg::Item::Type_shield
|| item->type == lcf::rpg::Item::Type_armor
|| item->type == lcf::rpg::Item::Type_helmet
|| item->type == lcf::rpg::Item::Type_accessory
)
);
if (do_skill) {
auto* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, item->skill_id);
if (skill == nullptr) {
Output::Warning("UseItem: Can't use item {} skill with invalid ID {}", item->ID, item->skill_id);
return false;
}
return UseSkill(item->skill_id, source);
}
return false;
}
bool Game_Battler::UseSkill(int skill_id, const Game_Battler* source) {
const lcf::rpg::Skill* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (!skill) {
Output::Warning("UseSkill: Can't use skill with invalid ID {}", skill_id);
return false;
}
bool cure_hp_percentage = false;
bool was_used = false;
int revived = 0;
if (Algo::IsNormalOrSubskill(*skill)) {
// Only takes care of healing skills outside of battle,
// the other skill logic is in Game_BattleAlgorithm
if (!(skill->scope == lcf::rpg::Skill::Scope_ally ||
skill->scope == lcf::rpg::Skill::Scope_party ||
skill->scope == lcf::rpg::Skill::Scope_self)) {
return false;
}
// Calculate effect:
auto effect = Algo::CalcSkillEffect(*source, *this, *skill, true, false, lcf::rpg::System::BattleCondition_none, false);
// Negative attributes do damage but cannot kill
bool negative_effect = false;
if (effect < 0) {
negative_effect = true;
effect = -effect;
}
// Cure states
for (int i = 0; i < (int)skill->state_effects.size(); i++) {
if (skill->state_effects[i]) {
if (skill->reverse_state_effect) {
was_used |= !HasState(lcf::Data::states[i].ID);
AddState(lcf::Data::states[i].ID, true);
}
else {
if (i == 0 && IsDead()) {
revived = 1;
}
was_used |= HasState(lcf::Data::states[i].ID);
RemoveState(lcf::Data::states[i].ID, false);
// If Death is cured and HP is not selected, we set a bool so it later heals HP percentage
if (i == 0 && !skill->affect_hp && revived) {
cure_hp_percentage = true;
}
}
}
}
// Skills only increase hp and sp outside of battle
if (!negative_effect) {
if (effect > 0 && skill->affect_hp && !HasFullHp() && !IsDead()) {
was_used = true;
ChangeHp(effect - revived, false);
}
else if (effect > 0 && cure_hp_percentage) {
was_used = true;
ChangeHp(GetMaxHp() * effect / 100 - revived, false);
}
if (effect > 0 && skill->affect_sp && !HasFullSp() && !IsDead()) {
was_used = true;
ChangeSp(effect);
}
} else {
if (effect > 0 && skill->affect_hp && !IsDead()) {
was_used = true;
ChangeHp(-effect, false);
}
if (effect > 0 && skill->affect_sp && !IsDead()) {
was_used = true;
ChangeSp(-effect);
}
}
} else if (skill->type == lcf::rpg::Skill::Type_teleport || skill->type == lcf::rpg::Skill::Type_escape) {
Main_Data::game_system->SePlay(skill->sound_effect);
was_used = true;
} else if (skill->type == lcf::rpg::Skill::Type_switch) {
Main_Data::game_system->SePlay(skill->sound_effect);
Main_Data::game_switches->Set(skill->switch_id, true);
was_used = true;
}
return was_used;
}
int Game_Battler::CalculateSkillCost(int skill_id) const {
const lcf::rpg::Skill* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (!skill) {
Output::Warning("CalculateSkillCost: Invalid skill ID {}", skill_id);
return 0;
}
return Algo::CalcSkillCost(*skill, GetMaxSp(), false);
}
int Game_Battler::CalculateSkillHpCost(int skill_id) const {
const lcf::rpg::Skill* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (!skill) {
Output::Warning("CalculateSkillHpCost: Invalid skill ID {}", skill_id);
return 0;
}
return Algo::CalcSkillHpCost(*skill, GetMaxHp());
}
bool Game_Battler::AddState(int state_id, bool allow_battle_states) {
auto was_added = State::Add(state_id, GetStates(), GetPermanentStates(), allow_battle_states);
if (!was_added) {
return was_added;
}
if (state_id == lcf::rpg::State::kDeathID) {
SetAtbGauge(0);
SetHp(0);
SetAtkModifier(0);
SetDefModifier(0);
SetSpiModifier(0);
SetAgiModifier(0);
SetIsDefending(false);
SetCharged(false);
attribute_shift.clear();
}
if (!Game_Battle::IsBattleRunning()) {
return was_added;
}
if (GetSignificantRestriction() != lcf::rpg::State::Restriction_normal) {
SetIsDefending(false);
SetCharged(false);
if (GetBattleAlgorithm() != nullptr
&& GetBattleAlgorithm()->GetType() != Game_BattleAlgorithm::Type::None) {
this->SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::None>(this));
}
}
if (GetBattleAlgorithm() != nullptr && GetBattleAlgorithm()->GetType() == Game_BattleAlgorithm::Type::Skill) {
auto* algo = static_cast<Game_BattleAlgorithm::Skill*>(GetBattleAlgorithm().get());
auto& skill = algo->GetSkill();
// Only remove the action when it is a now invalid skill. Skills triggered through items are not affected.
if (!algo->GetItem() && !IsSkillUsable(skill.ID)) {
SetCharged(false);
this->SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::None>(this));
}
}
return was_added;
}
template<typename F>
bool RemoveStates(Game_Battler& battler, F&& f) {
auto prev_restriction = battler.GetSignificantRestriction();
auto check_dead = [&]() {
// Cannot use IsDead here, as it checks for HP == 0
return State::Has(lcf::rpg::State::kDeathID, battler.GetStates());
};
bool is_dead = check_dead();
bool was_removed = f();
if (was_removed) {
if (is_dead != check_dead()) {
// Was revived
battler.SetHp(1);
}
auto cur_restriction = battler.GetSignificantRestriction();
if (battler.GetBattleAlgorithm() != nullptr
&& battler.GetBattleAlgorithm()->GetType() != Game_BattleAlgorithm::Type::None
&& cur_restriction != prev_restriction) {
battler.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::None>(&battler));
}
}
return was_removed;
}
void Game_Battler::RemoveBattleStates() {
RemoveStates(*this, [&]() {
return State::RemoveAllBattle(GetStates(), GetPermanentStates());
});
}
void Game_Battler::RemoveAllStates() {
RemoveStates(*this, [&]() {
return State::RemoveAll(GetStates(), GetPermanentStates());
});
}
bool Game_Battler::RemoveState(int state_id, bool always_remove_battle_states) {
return RemoveStates(*this, [&]() {
PermanentStates ps;
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, state_id);
if (!(always_remove_battle_states && state && state->type == lcf::rpg::State::Persistence_ends)) {
ps = GetPermanentStates();
}
return State::Remove(state_id, GetStates(), ps);
});
}
int Game_Battler::ApplyConditions() {
int damageTaken = 0;
for (int16_t inflicted : GetInflictedStates()) {
// States are guaranteed to be valid
lcf::rpg::State& state = *lcf::ReaderUtil::GetElement(lcf::Data::states, inflicted);
int hp = state.hp_change_val + (GetMaxHp() * state.hp_change_max / 100);
int sp = state.sp_change_val + (GetMaxSp() * state.sp_change_max / 100);
int src_hp = 0;
int src_sp = 0;
if (state.hp_change_type == state.ChangeType_lose) {
src_hp = -hp;
if(src_hp > 0) {
src_hp = 0;
}
}
else if(state.hp_change_type == state.ChangeType_gain) {
src_hp = hp;
if(src_hp < 0) {
src_hp = 0;
}
}
else {
src_hp = 0;
}
if (state.sp_change_type == state.ChangeType_lose) {
src_sp = -sp;
if(src_sp > 0) {
src_sp = 0;
}
}
else if (state.sp_change_type == state.ChangeType_gain) {
src_sp = sp;
if(src_sp < 0) {
src_sp = 0;
}
}
else {
src_sp = 0;
}
this->ChangeHp(src_hp, false);
this->ChangeSp(src_sp);
damageTaken += src_hp;
}
return damageTaken;
}
int Game_Battler::ChangeHp(int hp, bool lethal) {
if (IsDead()) {
return 0;
}
const auto prev_hp = GetHp();
auto req_new_hp = prev_hp + hp;
if (!lethal) {
req_new_hp = std::max(1, req_new_hp);
}
auto new_hp = SetHp(req_new_hp);
// Death
if (new_hp <= 0) {
AddState(lcf::rpg::State::kDeathID, true);
}
return new_hp - prev_hp;
}
int Game_Battler::GetMaxHp() const {
return GetBaseMaxHp();
}
bool Game_Battler::HasFullHp() const {
return GetMaxHp() == GetHp();
}
int Game_Battler::ChangeSp(int sp) {
const auto prev_sp = GetSp();
const auto new_sp = SetSp(prev_sp + sp);
return new_sp - prev_sp;
}
int Game_Battler::GetMaxSp() const {
return GetBaseMaxSp();
}
bool Game_Battler::HasFullSp() const {
return GetMaxSp() == GetSp();
}
static int AdjustParam(int base, int mod, int maxval, Span<const int16_t> states, bool lcf::rpg::State::*adj) {
auto value = Utils::Clamp(base + mod, 1, maxval);
bool half = false;
bool dbl = false;
for (auto i: states) {
const auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, i);
assert(state);
if (state->*adj) {
half |= (state->affect_type == lcf::rpg::State::AffectType_half);
dbl |= (state->affect_type == lcf::rpg::State::AffectType_double);
}
}
if (dbl != half) {
if (dbl) {
value *= 2;
} else {
value = std::max(1, value / 2);
}
}
// NOTE: RPG_RT does not clamp these values to the upper range!
// Exceptions:
// * 2k3 special function which computes atk for individual weapons / dual wield dmg does clamp at the end
// * 2k3 special function which computes agi for individual weapons / dual wield hit ratio does clamp, but also has a bug where it ignores states which modify agi!
return value;
}
int Game_Battler::CalcValueAfterAtkStates(int value) const {
return AdjustParam(value, 0, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_attack);
}
int Game_Battler::CalcValueAfterDefStates(int value) const {
return AdjustParam(value, 0, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_defense);
}
int Game_Battler::CalcValueAfterSpiStates(int value) const {
return AdjustParam(value, 0, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_spirit);
}
int Game_Battler::CalcValueAfterAgiStates(int value) const {
return AdjustParam(value, 0, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_agility);
}
int Game_Battler::GetAtk(Weapon weapon) const {
return AdjustParam(GetBaseAtk(weapon), atk_modifier, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_attack);
}
int Game_Battler::GetDef(Weapon weapon) const {
return AdjustParam(GetBaseDef(weapon), def_modifier, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_defense);
}
int Game_Battler::GetSpi(Weapon weapon) const {
return AdjustParam(GetBaseSpi(weapon), spi_modifier, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_spirit);
}
int Game_Battler::GetAgi(Weapon weapon) const {
return AdjustParam(GetBaseAgi(weapon), agi_modifier, MaxStatBattleValue(), GetInflictedStates(), &lcf::rpg::State::affect_agility);
}
int Game_Battler::GetDisplayX() const {
int shake_pos = Main_Data::game_screen->GetShakeOffsetX() + shake.position;
return Player::menu_offset_x + ((GetBattlePosition().x + shake_pos) * MENU_WIDTH / MENU_WIDTH);
}
int Game_Battler::GetDisplayY() const {
int shake_pos = Main_Data::game_screen->GetShakeOffsetY();
return Player::menu_offset_y + ((GetBattlePosition().y + GetFlyingOffset() + shake_pos) * MENU_HEIGHT / MENU_HEIGHT);
}
Game_Party_Base& Game_Battler::GetParty() const {
if (GetType() == Type_Ally) {
return *Main_Data::game_party;
} else {
return *Main_Data::game_enemyparty;
}
}
void Game_Battler::UpdateBattle() {
Shake::Update(shake.position, shake.time_left, shake.strength, shake.speed, false);
Flash::Update(flash.current_level, flash.time_left);
++frame_counter;
}
std::vector<int16_t> Game_Battler::BattleStateHeal() {
std::vector<int16_t> healed_states;
std::vector<int16_t>& states = GetStates();
for (size_t i = 0; i < states.size(); ++i) {
if (HasState(i + 1)) {
if (states[i] > lcf::Data::states[i].hold_turn
&& Rand::ChanceOf(lcf::Data::states[i].auto_release_prob, 100)
&& RemoveState(i + 1, false)
) {
healed_states.push_back(i + 1);
} else {
++states[i];
}
}
}
return healed_states;
}
bool Game_Battler::HasReflectState() const {
for (int16_t i : GetInflictedStates()) {
// States are guaranteed to be valid
if (lcf::ReaderUtil::GetElement(lcf::Data::states, i)->reflect_magic) {
return true;
}
}
return false;
}
void Game_Battler::ResetBattle() {
// Note: ATB gauge is not reset here. This is on purpose because RPG_RT will freeze
// the gauge and carry it between battles if !CanActOrRecoverable().
SetCharged(false);
SetIsDefending(false);
SetHidden(false);
SetDirectionFlipped(false);
battle_turn = 0;
last_battle_action = -1;
atk_modifier = 0;
def_modifier = 0;
spi_modifier = 0;
agi_modifier = 0;
frame_counter = Rand::GetRandomNumber(0, 63);
battle_combo_command_id = -1;
battle_combo_times = 1;
attribute_shift.clear();
SetBattleAlgorithm(nullptr);
SetBattleSprite(nullptr);
SetWeaponSprite(nullptr);
}
int Game_Battler::GetAttributeRate(int attribute_id) const {
auto rate = GetBaseAttributeRate(attribute_id);
rate += GetAttributeRateShift(attribute_id);
return Utils::Clamp(rate, 0, 4);
}
int Game_Battler::ShiftAttributeRate(int attribute_id, int shift) {
auto delta = CanShiftAttributeRate(attribute_id, shift);
if (delta) {
if (attribute_id > static_cast<int>(attribute_shift.size())) {
attribute_shift.resize(attribute_id);
}
attribute_shift[attribute_id - 1] += delta;
}
return delta;
}
int Game_Battler::GetAttributeRateShift(int attribute_id) const {
if (attribute_id < 1 || attribute_id > static_cast<int>(attribute_shift.size())) {
return 0;
}
return attribute_shift[attribute_id - 1];
}
int Game_Battler::CanShiftAttributeRate(int attribute_id, int shift) const {
if (attribute_id < 1 || attribute_id > static_cast<int>(lcf::Data::attributes.size())) {
return 0;
}
const auto prev_shift = GetAttributeRateShift(attribute_id);
const auto new_shift = Utils::Clamp(prev_shift + shift, -1, 1);
return new_shift - prev_shift;
}
int Game_Battler::GetHitChanceModifierFromStates() const {
int modifier = 100;
// Modify hit chance for each state the source has
for (const auto id : GetInflictedStates()) {
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, id);
if (state) {
modifier = std::min<int>(modifier, state->reduce_hit_ratio);
}
}
return modifier;
}
void Game_Battler::ShakeOnce(int strength, int speed, int frames) {
shake.strength = strength;
shake.speed = speed;
shake.time_left = frames;
// FIXME: RPG_RT doesn't reset position for screen shake. So we guess? it doesn't do so here either.
}
void Game_Battler::Flash(int r, int g, int b, int power, int frames) {
flash.red = r;
flash.green = g;
flash.blue = b;
flash.current_level = power;
flash.time_left = frames;
}
const std::vector<lcf::rpg::State*> Game_Battler::GetInflictedStatesOrderedByPriority() const {
std::vector<lcf::rpg::State*> state_list = State::GetObjects(GetStates());
return State::SortedByPriority(state_list);
}
int Game_Battler::CanChangeAtkModifier(int modifier) const {
const auto prev = atk_modifier;
const auto base = GetBaseAtk();
const auto new_mod = (Utils::Clamp(atk_modifier + modifier, -base / 2, base));
return new_mod - prev;
}
int Game_Battler::CanChangeDefModifier(int modifier) const {
const auto prev = def_modifier;
const auto base = GetBaseDef();
const auto new_mod = (Utils::Clamp(def_modifier + modifier, -base / 2, base));
return new_mod - prev;
}
int Game_Battler::CanChangeSpiModifier(int modifier) const {
const auto prev = spi_modifier;
const auto base = GetBaseSpi();
const auto new_mod = (Utils::Clamp(spi_modifier + modifier, -base / 2, base));
return new_mod - prev;
}
int Game_Battler::CanChangeAgiModifier(int modifier) const {
const auto prev = agi_modifier;
const auto base = GetBaseAgi();
const auto new_mod = (Utils::Clamp(agi_modifier + modifier, -base / 2, base));
return new_mod - prev;
}
int Game_Battler::ChangeAtkModifier(int modifier) {
auto delta = CanChangeAtkModifier(modifier);
SetAtkModifier(atk_modifier + delta);
return delta;
}
int Game_Battler::ChangeDefModifier(int modifier) {
auto delta = CanChangeDefModifier(modifier);
SetDefModifier(def_modifier + delta);
return delta;
}
int Game_Battler::ChangeSpiModifier(int modifier) {
auto delta = CanChangeSpiModifier(modifier);
SetSpiModifier(spi_modifier + delta);
return delta;
}
int Game_Battler::ChangeAgiModifier(int modifier) {
auto delta = CanChangeAgiModifier(modifier);
SetAgiModifier(agi_modifier + delta);
return delta;
}