forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_battlealgorithm.cpp
1511 lines (1281 loc) · 43.7 KB
/
game_battlealgorithm.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* 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/>.
*/
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <sstream>
#include "game_actor.h"
#include "game_battle.h"
#include "game_battlealgorithm.h"
#include "game_battler.h"
#include "game_enemy.h"
#include "game_enemyparty.h"
#include "game_party.h"
#include "game_party_base.h"
#include "game_switches.h"
#include "game_system.h"
#include "main_data.h"
#include "battle_message.h"
#include "output.h"
#include "player.h"
#include <lcf/reader_util.h>
#include <lcf/rpg/animation.h>
#include <lcf/rpg/state.h>
#include <lcf/rpg/skill.h>
#include <lcf/rpg/item.h>
#include <lcf/rpg/battleranimation.h>
#include "sprite_battler.h"
#include "utils.h"
#include "rand.h"
#include "state.h"
#include "enemyai.h"
#include "algo.h"
#include "attribute.h"
#include "spriteset_battle.h"
#include "feature.h"
static inline int MaxDamageValue() {
return lcf::Data::system.easyrpg_max_damage == -1 ? (Player::IsRPG2k() ? 999 : 9999) : lcf::Data::system.easyrpg_max_damage;
}
Game_BattleAlgorithm::AlgorithmBase::AlgorithmBase(Type ty, Game_Battler* source, Game_Battler* target) :
AlgorithmBase(ty, source, std::vector<Game_Battler*>{ target }) {}
Game_BattleAlgorithm::AlgorithmBase::AlgorithmBase(Type ty, Game_Battler* source, std::vector<Game_Battler*> in_targets) :
type(ty), source(source), targets(std::move(in_targets))
{
assert(source != nullptr);
for (auto* t: targets) {
assert(t != nullptr);
}
Reset();
source->SetIsDefending(false);
num_original_targets = targets.size();
current_target = targets.end();
}
Game_BattleAlgorithm::AlgorithmBase::AlgorithmBase(Type ty, Game_Battler* source, Game_Party_Base* target) :
type(ty), source(source)
{
assert(source != nullptr);
assert(target != nullptr);
Reset();
source->SetIsDefending(false);
current_target = targets.end();
party_target = target;
}
void Game_BattleAlgorithm::AlgorithmBase::Reset() {
hp = 0;
sp = 0;
attack = 0;
defense = 0;
spirit = 0;
agility = 0;
switch_id = 0;
flags = {};
states.clear();
attributes.clear();
}
int Game_BattleAlgorithm::AlgorithmBase::PlayAnimation(int anim_id, bool only_sound, int cutoff, bool invert) {
if (anim_id == 0) {
return 0;
}
auto anim_iter = targets.begin();
auto anim_end = targets.end();
// If targets were added, skip the originals and use the added one
// Cases: reflect, retargeting, etc..
if (num_original_targets < static_cast<int>(targets.size())) {
anim_iter += num_original_targets;
}
std::vector<Game_Battler*> anim_targets;
for (; anim_iter != anim_end; ++anim_iter) {
auto* target = *anim_iter;
if (!target->IsHidden() && (IsTargetValid(*target) || (target->GetType() == Game_Battler::Type_Ally && target->IsDead()))) {
anim_targets.push_back(target);
}
}
return Game_Battle::ShowBattleAnimation(anim_id, anim_targets, only_sound, cutoff, invert);
}
std::string Game_BattleAlgorithm::AlgorithmBase::GetFailureMessage() const {
return BattleMessage::GetPhysicalFailureMessage(*GetSource(), *GetTarget());
}
Game_Battler* Game_BattleAlgorithm::AlgorithmBase::GetTarget() const {
if (current_target == targets.end()) {
return NULL;
}
return *current_target;
}
bool Game_BattleAlgorithm::AlgorithmBase::Execute() {
Reset();
return vExecute();
}
bool Game_BattleAlgorithm::AlgorithmBase::vExecute() {
return SetIsSuccess();
}
void Game_BattleAlgorithm::AlgorithmBase::ApplyCustomEffect() {
}
int Game_BattleAlgorithm::AlgorithmBase::ApplySwitchEffect() {
const auto sw = GetAffectedSwitch();
if (sw > 0) {
Main_Data::game_switches->Set(sw, true);
}
return sw;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplyHpEffect() {
auto* target = GetTarget();
assert(target);
if (target->IsDead()) {
return 0;
}
int hp = GetAffectedHp();
if (hp != 0) {
hp = target->ChangeHp(hp, true);
if (IsAbsorbHp()) {
// Only absorb the hp that were left
source->ChangeHp(-hp, true);
}
}
return hp;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplySpEffect() {
auto* target = GetTarget();
assert(target);
auto sp = GetAffectedSp();
if (sp != 0) {
sp = target->ChangeSp(sp);
if (IsAbsorbSp()) {
// Only absorb the sp that were left
source->ChangeSp(-sp);
}
}
return sp;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplyAtkEffect() {
auto* target = GetTarget();
assert(target);
auto atk = GetAffectedAtk();
if (atk != 0) {
atk = target->ChangeAtkModifier(atk);
if (IsAbsorbAtk()) {
source->ChangeAtkModifier(-atk);
}
}
return atk;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplyDefEffect() {
auto* target = GetTarget();
assert(target);
auto def = GetAffectedDef();
if (def != 0) {
def = target->ChangeDefModifier(def);
if (IsAbsorbDef()) {
source->ChangeDefModifier(-def);
}
}
return def;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplySpiEffect() {
auto* target = GetTarget();
assert(target);
auto spi = GetAffectedSpi();
if (spi) {
spi = target->ChangeSpiModifier(spi);
if (IsAbsorbSpi()) {
source->ChangeSpiModifier(-spi);
}
}
return spi;
}
int Game_BattleAlgorithm::AlgorithmBase::ApplyAgiEffect() {
auto* target = GetTarget();
assert(target);
auto agi = GetAffectedAgi();
if (agi) {
agi = target->ChangeAgiModifier(agi);
if (IsAbsorbAgi()) {
source->ChangeAgiModifier(-agi);
}
}
return agi;
}
bool Game_BattleAlgorithm::AlgorithmBase::ApplyStateEffect(StateEffect se) {
auto* target = GetTarget();
if (!target) {
return false;
}
bool rc = false;
bool was_dead = target->IsDead();
// Apply states
switch (se.effect) {
case StateEffect::Inflicted:
rc = target->AddState(se.state_id, true);
break;
case StateEffect::Healed:
case StateEffect::HealedByAttack:
rc = target->RemoveState(se.state_id, false);
break;
default:
break;
}
// Apply revived hp healing
if (was_dead && !target->IsDead()) {
auto hp = GetAffectedHp();
target->ChangeHp(hp - 1, false);
}
return rc;
}
void Game_BattleAlgorithm::AlgorithmBase::ApplyStateEffects() {
// Apply states
for (auto& se: states) {
ApplyStateEffect(se);
}
}
int Game_BattleAlgorithm::AlgorithmBase::ApplyAttributeShiftEffect(AttributeEffect ae) {
auto* target = GetTarget();
if (target) {
return target->ShiftAttributeRate(ae.attr_id, ae.shift);
}
return 0;
}
void Game_BattleAlgorithm::AlgorithmBase::ApplyAttributeShiftEffects() {
for (auto& ae: attributes) {
ApplyAttributeShiftEffect(ae);
}
}
void Game_BattleAlgorithm::AlgorithmBase::ApplyAll() {
ApplyCustomEffect();
ApplySwitchEffect();
ApplyHpEffect();
ApplySpEffect();
ApplyAtkEffect();
ApplyDefEffect();
ApplySpiEffect();
ApplyAgiEffect();
ApplyStateEffects();
ApplyAttributeShiftEffects();
}
void Game_BattleAlgorithm::AlgorithmBase::ProcessPostActionSwitches() {
for (int s : switch_on) {
Main_Data::game_switches->Set(s, true);
}
for (int s : switch_off) {
Main_Data::game_switches->Set(s, false);
}
}
bool Game_BattleAlgorithm::AlgorithmBase::IsTargetValid(const Game_Battler& target) const {
return target.Exists();
}
int Game_BattleAlgorithm::AlgorithmBase::GetSourcePose() const {
return lcf::rpg::BattlerAnimation::Pose_Idle;
}
int Game_BattleAlgorithm::AlgorithmBase::GetCBAMovement() const {
return lcf::rpg::BattlerAnimationItemSkill::Movement_none;
}
int Game_BattleAlgorithm::AlgorithmBase::GetCBAAfterimage() const {
return lcf::rpg::BattlerAnimationItemSkill::Afterimage_none;
}
const lcf::rpg::BattlerAnimationItemSkill* Game_BattleAlgorithm::AlgorithmBase::GetWeaponAnimationData() const {
return nullptr;
}
const lcf::rpg::Item* Game_BattleAlgorithm::AlgorithmBase::GetWeaponData() const {
return nullptr;
}
void Game_BattleAlgorithm::AlgorithmBase::Start() {
reflect_target = nullptr;
if (party_target) {
targets.clear();
party_target->GetBattlers(targets);
num_original_targets = targets.size();
} else {
// Remove any previously set reflect targets
targets.resize(num_original_targets);
}
current_target = targets.begin();
// Call any custom start logic, then check if we need to retarget
bool allow_retarget = vStart();
if (!IsCurrentTargetValid()) {
if (!TargetNext() && allow_retarget && !party_target && !targets.empty()) {
auto* last_target = targets.back();
auto* next_target = last_target->GetParty().GetNextActiveBattler(last_target);
if (next_target) {
current_target = targets.insert(targets.end(), next_target);
}
if (!IsCurrentTargetValid()) {
TargetNext();
}
}
}
// This case must be true before returning.
assert(current_target == targets.end() || IsCurrentTargetValid());
source->SetCharged(false);
}
bool Game_BattleAlgorithm::AlgorithmBase::vStart() {
return true;
}
void Game_BattleAlgorithm::AlgorithmBase::AddTarget(Game_Battler* target, bool set_current) {
assert(target != nullptr);
const auto idx = std::distance(targets.begin(), current_target);
const auto size = targets.size();
targets.push_back(target);
current_target = targets.begin() + (set_current ? size : idx);
}
void Game_BattleAlgorithm::AlgorithmBase::AddTargets(Game_Party_Base* party, bool set_current) {
assert(party != nullptr);
const auto idx = std::distance(targets.begin(), current_target);
const auto size = targets.size();
party->GetBattlers(targets);
current_target = targets.begin() + (set_current ? size : idx);
}
bool Game_BattleAlgorithm::AlgorithmBase::ReflectTargets() {
auto iter = std::find_if(current_target, targets.end(), [this](auto* target) { return IsReflected(*target); });
// No reflect
if (iter == targets.end()) {
return false;
}
reflect_target = *iter;
if (party_target) {
// Reflect back on source party
AddTargets(&source->GetParty(), true);
} else {
// Reflect back on source
AddTarget(source, true);
}
if (!IsCurrentTargetValid()) {
TargetNext();
}
return true;
}
bool Game_BattleAlgorithm::AlgorithmBase::TargetNext() {
return TargetNextInternal();
}
bool Game_BattleAlgorithm::AlgorithmBase::RepeatNext(bool require_valid) {
++cur_repeat;
if (cur_repeat >= repeat || (require_valid && !IsCurrentTargetValid())) {
cur_repeat = 0;
return false;
}
return true;
}
bool Game_BattleAlgorithm::AlgorithmBase::IsCurrentTargetValid() const {
if (current_target == targets.end()) {
return false;
}
return IsTargetValid(**current_target);
}
bool Game_BattleAlgorithm::AlgorithmBase::TargetNextInternal() {
do {
if (current_target == targets.end() || ++current_target == targets.end()) {
return false;
}
} while (!IsCurrentTargetValid());
return true;
}
void Game_BattleAlgorithm::AlgorithmBase::SetRepeat(int repeat) {
this->repeat = std::max(1, repeat);
}
void Game_BattleAlgorithm::AlgorithmBase::SetSwitchEnable(int switch_id) {
switch_on.push_back(switch_id);
}
void Game_BattleAlgorithm::AlgorithmBase::SetSwitchDisable(int switch_id) {
switch_off.push_back(switch_id);
}
const lcf::rpg::Sound* Game_BattleAlgorithm::AlgorithmBase::GetStartSe() const {
return NULL;
}
std::string Game_BattleAlgorithm::AlgorithmBase::GetStartMessage(int) const {
return "";
}
const lcf::rpg::Sound* Game_BattleAlgorithm::AlgorithmBase::GetFailureSe() const {
return &Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Evasion);
}
bool Game_BattleAlgorithm::AlgorithmBase::IsReflected(const Game_Battler&) const {
return false;
}
void Game_BattleAlgorithm::AlgorithmBase::ApplyComboHitsMultiplier(int hits) {
repeat *= hits;
}
void Game_BattleAlgorithm::AlgorithmBase::AddAffectedState(StateEffect se) {
auto* target = GetTarget();
if (se.state_id == lcf::rpg::State::kDeathID
&& (se.effect == StateEffect::Healed || se.effect == StateEffect::HealedByAttack)
&& target && target->IsDead()) {
SetFlag(eRevived, true);
}
states.push_back(std::move(se));
}
void Game_BattleAlgorithm::AlgorithmBase::AddAffectedAttribute(AttributeEffect ae) {
attributes.push_back(std::move(ae));
}
void Game_BattleAlgorithm::AlgorithmBase::BattlePhysicalStateHeal(int physical_rate, std::vector<int16_t>& target_states, const PermanentStates& ps) {
if (physical_rate <= 0) {
return;
}
for (int i = 0; i < (int)target_states.size(); ++i) {
auto state_id = i + 1;
if (!State::Has(state_id, target_states)) {
continue;
}
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, state_id);
if (state == nullptr) {
continue;
}
if (state->release_by_damage > 0) {
int release_chance = state->release_by_damage * physical_rate / 100;
if (!Rand::ChanceOf(release_chance, 100)) {
continue;
}
if (State::Remove(state_id, target_states, ps)) {
AddAffectedState(StateEffect{state_id, Game_BattleAlgorithm::StateEffect::HealedByAttack});
}
}
}
}
Game_BattleAlgorithm::None::None(Game_Battler* source) :
AlgorithmBase(Type::None, source, source) {
// no-op
}
Game_BattleAlgorithm::Normal::Normal(Game_Battler* source, Game_Battler* target, int hits_multiplier, Style style) :
AlgorithmBase(Type::Normal, source, target), hits_multiplier(hits_multiplier)
{
Init(style);
}
Game_BattleAlgorithm::Normal::Normal(Game_Battler* source, Game_Party_Base* target, int hits_multiplier, Style style) :
AlgorithmBase(Type::Normal, source, target), hits_multiplier(hits_multiplier)
{
Init(style);
}
Game_BattleAlgorithm::Normal::Style Game_BattleAlgorithm::Normal::GetDefaultStyle() {
return Feature::HasRpg2k3BattleSystem() ? Style_MultiHit : Style_Combined;
}
Game_Battler::Weapon Game_BattleAlgorithm::Normal::GetWeapon() const {
if (weapon_style < 0) {
return Game_Battler::WeaponAll;
}
return GetCurrentRepeat() >= weapon_style ? Game_Battler::WeaponSecondary : Game_Battler::WeaponPrimary;
}
void Game_BattleAlgorithm::Normal::Init(Style style) {
auto* source = GetSource();
charged_attack = source->IsCharged();
weapon_style = -1;
if (source->GetType() == Game_Battler::Type_Ally && style == Style_MultiHit) {
auto* ally = static_cast<Game_Actor*>(source);
if (ally->GetWeapon() && ally->Get2ndWeapon()) {
auto hits = hits_multiplier * ally->GetNumberOfAttacks(Game_Battler::WeaponPrimary);
weapon_style = hits;
hits += hits_multiplier * ally->GetNumberOfAttacks(Game_Battler::WeaponSecondary);
SetRepeat(hits);
return;
}
}
SetRepeat(hits_multiplier * source->GetNumberOfAttacks(GetWeapon()));
}
void Game_BattleAlgorithm::Normal::ApplyComboHitsMultiplier(int hits) {
AlgorithmBase::ApplyComboHitsMultiplier(hits);
if (weapon_style > 0) {
// For dual wield normal attack, the first weapon gets combo'd then the second weapon.
weapon_style *= hits;
}
}
bool Game_BattleAlgorithm::Normal::vStart() {
// If this weapon attacks all, then attack all enemies regardless of original targetting.
const auto weapon = GetWeapon();
auto* source = GetSource();
if (GetOriginalPartyTarget() == nullptr && source->HasAttackAll(weapon)) {
auto* target = GetOriginalTargets().back();
AddTargets(&target->GetParty(), true);
}
source->ChangeSp(-source->CalculateWeaponSpCost(weapon));
return true;
}
int Game_BattleAlgorithm::Normal::GetAnimationId(int idx) const {
const auto weapon = GetWeapon();
auto* source = GetSource();
if (source->GetType() == Game_Battler::Type_Ally) {
Game_Actor* ally = static_cast<Game_Actor*>(source);
auto weapons = ally->GetWeapons(weapon);
auto* item = (idx >= 0 && idx < static_cast<int>(weapons.size()))
? weapons[idx] : nullptr;
if (item) {
return item->animation_id;
} else if (idx == 0) {
return ally->GetUnarmedBattleAnimationId();
}
return 0;
}
if (source->GetType() == Game_Battler::Type_Enemy
&& Feature::HasRpg2k3BattleSystem()
&& !lcf::Data::animations.empty()) {
Game_Enemy* enemy = static_cast<Game_Enemy*>(source);
return enemy->GetUnarmedBattleAnimationId();
}
return 0;
}
bool Game_BattleAlgorithm::Normal::vExecute() {
const auto weapon = GetWeapon();
auto& source = *GetSource();
auto& target = *GetTarget();
auto to_hit = Algo::CalcNormalAttackToHit(source, target, weapon, Game_Battle::GetBattleCondition(), true);
auto crit_chance = Algo::CalcCriticalHitChance(source, target, weapon, -1);
// Damage calculation
if (!Rand::PercentChance(to_hit)) {
return SetIsFailure();
}
if (Rand::PercentChance(crit_chance)) {
SetIsCriticalHit(true);
}
auto effect = Algo::CalcNormalAttackEffect(source, target, weapon, IsCriticalHit(), charged_attack, true, Game_Battle::GetBattleCondition(), treat_enemies_asif_in_front_row);
effect = Algo::AdjustDamageForDefend(effect, target);
effect = Utils::Clamp(effect, -MaxDamageValue(), MaxDamageValue());
this->SetAffectedHp(-effect);
// If target is killed, states not applied
if (target.GetHp() + GetAffectedHp() <= 0) {
return SetIsSuccess();
}
// Make a copy of the target's state set and see what we can apply.
auto target_states = target.GetStates();
auto target_perm_states = target.GetPermanentStates();
// Conditions healed by physical attack:
BattlePhysicalStateHeal(100, target_states, target_perm_states);
auto easyrpg_state_set = [&](const auto& state_set, const auto& state_chance) {
int num_states = static_cast<int>(state_set.size());
for (int i = 0; i < num_states; ++i) {
int inflict_pct = 0;
if (i < static_cast<int>(num_states) && state_set[i]) {
inflict_pct = static_cast<int>(state_chance);
}
auto state_id = (i + 1);
if (inflict_pct > 0) {
inflict_pct = inflict_pct * target.GetStateProbability(state_id) / 100;
if (Rand::PercentChance(inflict_pct)) {
if (!State::Has(state_id, target_states) && State::Add(state_id, target_states, target_perm_states, true)) {
AddAffectedState(StateEffect{state_id, StateEffect::Inflicted});
}
}
inflict_pct = 0;
}
}
};
// Conditions caused / healed by weapon.
if (source.GetType() == Game_Battler::Type_Ally) {
auto& ally = static_cast<Game_Actor&>(source);
const bool is2k3 = Player::IsRPG2k3();
auto weapons = ally.GetWeapons(weapon);
if (weapons[0]) {
int num_states = 0;
for (auto* w: weapons) {
if (w) {
num_states = std::max(num_states, static_cast<int>(w->state_set.size()));
}
}
for (int i = 0; i < num_states; ++i) {
// EasyRPG extension: This logic allows heal/inflict to work properly with a combined weapon attack.
// If the first weapon heals and the second inflicts, then this will do then in the right order.
// If both heal or both inflict, we take the max probability as RPG_RT does.
int heal_pct = 0;
int inflict_pct = 0;
for (auto* w: weapons) {
if (w && i < static_cast<int>(w->state_set.size()) && w->state_set[i]) {
if (is2k3 && w->reverse_state_effect) {
heal_pct = std::max(heal_pct, static_cast<int>(w->state_chance));
} else {
inflict_pct = std::max(inflict_pct, static_cast<int>(w->state_chance));
}
}
}
auto state_id = (i + 1);
for (auto* w: weapons) {
if (is2k3 && w && w->reverse_state_effect) {
if (heal_pct > 0 && Rand::PercentChance(heal_pct)) {
if (State::Remove(state_id, target_states, target_perm_states)) {
AddAffectedState(StateEffect{state_id, StateEffect::Healed});
}
}
heal_pct = 0;
} else if (inflict_pct > 0) {
inflict_pct = inflict_pct * target.GetStateProbability(state_id) / 100;
if (Rand::PercentChance(inflict_pct)) {
// Unlike skills, weapons do not try to reinflict states already present
if (!State::Has(state_id, target_states) && State::Add(state_id, target_states, target_perm_states, true)) {
AddAffectedState(StateEffect{state_id, StateEffect::Inflicted});
}
}
inflict_pct = 0;
}
}
}
} else {
lcf::rpg::Actor* allydata = lcf::ReaderUtil::GetElement(lcf::Data::actors, ally.GetId());
easyrpg_state_set(allydata->easyrpg_unarmed_state_set, allydata->easyrpg_unarmed_state_chance);
}
} else if (source.GetType() == Game_Battler::Type_Enemy) {
auto& enemy = static_cast<Game_Enemy&>(source);
lcf::rpg::Enemy* enemydata = lcf::ReaderUtil::GetElement(lcf::Data::enemies, enemy.GetId());
easyrpg_state_set(enemydata->easyrpg_state_set, enemydata->easyrpg_state_chance);
}
return SetIsSuccess();
}
std::string Game_BattleAlgorithm::Normal::GetStartMessage(int line) const {
if (line == 0) {
if (Feature::HasRpg2kBattleSystem()) {
return BattleMessage::GetNormalAttackStartMessage2k(*GetSource());
}
if (GetSource()->GetType() == Game_Battler::Type_Enemy && hits_multiplier == 2) {
return BattleMessage::GetDoubleAttackStartMessage2k3(*GetSource());
}
}
return "";
}
int Game_BattleAlgorithm::Normal::GetSourcePose() const {
auto weapon = GetWeapon();
return weapon == Game_Battler::WeaponSecondary
? lcf::rpg::BattlerAnimation::Pose_AttackLeft
: lcf::rpg::BattlerAnimation::Pose_AttackRight;
}
int Game_BattleAlgorithm::Normal::GetCBAMovement() const {
const auto weapon = GetWeapon();
auto* source = GetSource();
if (source->GetType() == Game_Battler::Type_Ally) {
auto* ally = static_cast<Game_Actor*>(source);
auto weapons = ally->GetWeapons(weapon);
auto* item = weapons[0];
if (item) {
if (static_cast<int>(item->animation_data.size()) > source->GetId() - 1) {
return item->animation_data[source->GetId() - 1].movement;
}
}
}
return lcf::rpg::BattlerAnimationItemSkill::Movement_none;
}
int Game_BattleAlgorithm::Normal::GetCBAAfterimage() const {
const auto weapon = GetWeapon();
auto* source = GetSource();
if (source->GetType() == Game_Battler::Type_Ally) {
auto* ally = static_cast<Game_Actor*>(source);
auto weapons = ally->GetWeapons(weapon);
auto* item = weapons[0];
if (item) {
if (static_cast<int>(item->animation_data.size()) > source->GetId() - 1) {
return item->animation_data[source->GetId() - 1].after_image;
}
}
}
return lcf::rpg::BattlerAnimationItemSkill::Afterimage_none;
}
const lcf::rpg::BattlerAnimationItemSkill* Game_BattleAlgorithm::Normal::GetWeaponAnimationData() const {
const auto weapon = GetWeapon();
auto* source = GetSource();
if (source->GetType() == Game_Battler::Type_Ally) {
auto* ally = static_cast<Game_Actor*>(source);
auto weapons = ally->GetWeapons(weapon);
auto* item = weapons[0];
if (item) {
if (static_cast<int>(item->animation_data.size()) > source->GetId() - 1) {
return &item->animation_data[source->GetId() - 1];
}
}
}
return nullptr;
}
const lcf::rpg::Item* Game_BattleAlgorithm::Normal::GetWeaponData() const {
const auto weapon = GetWeapon();
auto* source = GetSource();
if (source->GetType() == Game_Battler::Type_Ally) {
auto* ally = static_cast<Game_Actor*>(source);
auto weapons = ally->GetWeapons(weapon);
auto* item = weapons[0];
if (item) {
return item;
}
}
return nullptr;
}
const lcf::rpg::Sound* Game_BattleAlgorithm::Normal::GetStartSe() const {
if (Feature::HasRpg2kBattleSystem() && GetSource()->GetType() == Game_Battler::Type_Enemy) {
return &Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_EnemyAttacks);
}
return nullptr;
}
Game_BattleAlgorithm::Skill::Skill(Game_Battler* source, Game_Battler* target, const lcf::rpg::Skill& skill, const lcf::rpg::Item* item) :
AlgorithmBase(Type::Skill, source, target), skill(skill), item(item)
{
Init();
}
Game_BattleAlgorithm::Skill::Skill(Game_Battler* source, Game_Party_Base* target, const lcf::rpg::Skill& skill, const lcf::rpg::Item* item) :
AlgorithmBase(Type::Skill, source, target), skill(skill), item(item)
{
Init();
}
Game_BattleAlgorithm::Skill::Skill(Game_Battler* source, const lcf::rpg::Skill& skill, const lcf::rpg::Item* item) :
Skill(source, source, skill, item)
{
}
void Game_BattleAlgorithm::Skill::Init() {
}
bool Game_BattleAlgorithm::Skill::vStart() {
auto* source = GetSource();
if (item) {
Main_Data::game_party->ConsumeItemUse(item->ID);
} else {
source->ChangeSp(-source->CalculateSkillCost(skill.ID));
source->ChangeHp(-source->CalculateSkillHpCost(skill.ID), false);
}
return true;
}
int Game_BattleAlgorithm::Skill::GetAnimationId(int idx) const {
return idx == 0 && Algo::IsNormalOrSubskill(skill) ? skill.animation_id : 0;
}
bool Game_BattleAlgorithm::Skill::IsTargetValid(const Game_Battler& target) const {
if (target.IsHidden()) {
return false;
}
if (target.IsDead()) {
// Cures death
// NOTE: RPG_RT 2k3 also allows this targetting if reverse_state_effect.
return Algo::SkillTargetsAllies(skill) && !skill.state_effects.empty() && skill.state_effects[0];
}
return true;
}
bool Game_BattleAlgorithm::Skill::vExecute() {
if (item && item->skill_id != skill.ID) {
assert(false && "Item skill mismatch");
}
auto* source = GetSource();
assert(source);
auto* target = GetTarget();
assert(target);
if (skill.type == lcf::rpg::Skill::Type_switch) {
SetAffectedSwitch(skill.switch_id);
return SetIsSuccess();
}
if (!Algo::IsNormalOrSubskill(skill)) {
// Possible to trigger in RPG_RT by using a weapon which invokes a teleport or escape skills.
// Silently does nothing.
return SetIsSuccess();
}
SetIsPositive(Algo::SkillTargetsAllies(skill));
auto to_hit = Algo::CalcSkillToHit(*source, *target, skill, Game_Battle::GetBattleCondition(), true);
auto crit_chance = Algo::CalcCriticalHitChance(*source, *target, Game_Battler::WeaponAll, skill.easyrpg_critical_hit_chance);
if (Rand::PercentChance(crit_chance)) {
SetIsCriticalHit(true);
}
auto effect = Algo::CalcSkillEffect(*source, *target, skill, true, IsCriticalHit(), Game_Battle::GetBattleCondition(), treat_enemies_asif_in_front_row);
effect = Utils::Clamp(effect, -MaxDamageValue(), MaxDamageValue());
if (!IsPositive()) {
effect = -effect;
}
const bool is_dead = target->IsDead();
const bool cures_death = IsPositive()
&& !skill.state_effects.empty()
&& skill.state_effects[lcf::rpg::State::kDeathID - 1]
&& is_dead;
// Dead targets only allowed if this skill revives later
if (is_dead && (!IsPositive() || !cures_death)) {
return SetIsFailure();
}
// Absorb only works on offensive skills.
const auto absorb = (skill.absorb_damage && !IsPositive());
// Make a copy of the target's state set and see what we can apply.
auto target_states = target->GetStates();
auto target_perm_states = target->GetPermanentStates();
if (skill.affect_hp && Rand::PercentChance(to_hit)) {
const auto hp_effect = IsPositive()
? effect
: Algo::AdjustDamageForDefend(effect, *target);
const auto hp_cost = (source == target) ? source->CalculateSkillHpCost(skill.ID) : 0;
const auto cur_hp = target->GetHp() - hp_cost;
if (absorb) {
// Cannot aborb more hp than the target has.
auto hp = std::max<int>(hp_effect, -cur_hp);
if (hp != 0) {
SetAffectedHp(hp);
SetIsAbsorbHp(true);
// Absorb requires damage to be successful
SetIsSuccess();
}
} else {
if (IsPositive()) {
// RPG_RT attribute inverted healing effects are non-lethal
auto hp = std::max(-(cur_hp - 1), hp_effect);
if (hp != 0) {
// HP recovery is sucessful if the effect is non-zero, even at full hp.
SetAffectedHp(hp);
SetIsSuccess();
}
} else {
SetAffectedHp(hp_effect);
if (cur_hp + GetAffectedHp() > 0) {
// Conditions healed by physical attack, but only if target not killed.
BattlePhysicalStateHeal(skill.physical_rate * 10, target_states, target_perm_states);
}
// Hp damage always successful, even if 0
SetIsSuccess();
}
}
}
// If target will be killed, no further affects are applied.
if (!is_dead && GetTarget()->GetHp() + this->GetAffectedHp() <= 0) {
return IsSuccess();
}
if (skill.affect_sp && Rand::PercentChance(to_hit)) {
const auto sp_cost = (source == target) ? source->CalculateSkillCost(skill.ID) : 0;
const auto max_sp = target->GetMaxSp();
const auto cur_sp = target->GetSp() - sp_cost;
int sp = 0;
if (absorb) {
// Cannot aborb more sp than the target has.
sp = std::max<int>(effect, -cur_sp);
} else {
sp = Utils::Clamp(cur_sp + effect, 0, max_sp) - cur_sp;
}
if (sp != 0) {
SetAffectedSp(sp);
SetIsAbsorbSp(absorb);