forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame_actor.cpp
1525 lines (1279 loc) · 41.2 KB
/
game_actor.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/>.
*/
// Headers
#include <algorithm>
#include <sstream>
#include <iterator>
#include "game_actor.h"
#include "game_battle.h"
#include "game_message.h"
#include "game_party.h"
#include "sprite_actor.h"
#include "main_data.h"
#include "output.h"
#include "player.h"
#include <lcf/reader_util.h>
#include <lcf/rpg/skill.h>
#include "util_macro.h"
#include "utils.h"
#include "pending_message.h"
#include "compiler.h"
#include "attribute.h"
#include "rand.h"
#include "algo.h"
constexpr int max_level_2k = 50;
constexpr int max_level_2k3 = 99;
int Game_Actor::MaxHpValue() const {
auto& val = lcf::Data::system.easyrpg_max_actor_hp;
if (val == -1) {
return Player::IsRPG2k() ? 999 : 9999;
}
return val;
}
int Game_Actor::MaxSpValue() const {
auto& val = lcf::Data::system.easyrpg_max_actor_sp;
if (val == -1) {
return 999;
}
return val;
}
int Game_Actor::MaxStatBattleValue() const {
auto& val = lcf::Data::system.easyrpg_max_stat_battle_value;
if (val == -1) {
return 9999;
}
return val;
}
int Game_Actor::MaxStatBaseValue() const {
auto& val = lcf::Data::system.easyrpg_max_stat_base_value;
if (val == -1) {
return 999;
}
return val;
}
int Game_Actor::MaxExpValue() const {
auto& val = lcf::Data::system.easyrpg_max_exp;
if (val == -1) {
return Player::IsRPG2k() ? 999999 : 9999999;
}
return val;
}
Game_Actor::Game_Actor(int actor_id) {
data.ID = actor_id;
if (actor_id == 0) {
return;
}
dbActor = lcf::ReaderUtil::GetElement(lcf::Data::actors, GetId());
data.two_weapon = dbActor->two_weapon;
data.lock_equipment = dbActor->lock_equipment;
data.auto_battle = dbActor->auto_battle;
data.super_guard = dbActor->super_guard;
data.hp_mod = 0;
data.sp_mod = 0;
data.attack_mod = 0;
data.defense_mod = 0;
data.spirit_mod = 0;
data.agility_mod = 0;
MakeExpList();
SetBattlePosition(GetOriginalPosition());
data.level = 0;
if (dbActor->initial_level > 0) {
// For games like COLORS: Lost Memories which use level 0, don't change level because it'll clamp to 1.
ChangeLevel(dbActor->initial_level, nullptr);
}
SetHp(GetMaxHp());
SetSp(GetMaxSp());
// Remove items that do not exist in the database anymore
std::array<int, 5> ids = {{
dbActor->initial_equipment.weapon_id,
dbActor->initial_equipment.shield_id,
dbActor->initial_equipment.armor_id,
dbActor->initial_equipment.helmet_id,
dbActor->initial_equipment.accessory_id }};
std::replace_if(ids.begin(), ids.end(), [] (const int& item_id) {
return lcf::ReaderUtil::GetElement(lcf::Data::items, item_id) == nullptr;
}, 0);
for (int i = 0; i <= 4; i++) {
SetEquipment(i + 1, ids[i]);
}
data.status.resize(lcf::Data::states.size(), 0);
Fixup();
}
void Game_Actor::SetSaveData(lcf::rpg::SaveActor save) {
data = std::move(save);
if (Player::IsRPG2k()) {
data.two_weapon = dbActor->two_weapon;
data.lock_equipment = dbActor->lock_equipment;
data.auto_battle = dbActor->auto_battle;
data.super_guard = dbActor->super_guard;
}
MakeExpList();
Fixup();
}
lcf::rpg::SaveActor Game_Actor::GetSaveData() const {
auto save = data;
if (Player::IsRPG2k()) {
// RPG_RT doesn't save these chunks in rm2k as they are meaningless
save.two_weapon = false;
save.lock_equipment = false;
save.auto_battle = false;
save.super_guard = false;
}
return save;
}
void Game_Actor::Fixup() {
RemoveInvalidData();
ResetEquipmentStates(false);
}
bool Game_Actor::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 invalid item {}", item_id);
return false;
}
if (!IsDead()) {
if (item->type == lcf::rpg::Item::Type_book) {
return LearnSkill(item->skill_id, nullptr);
}
if (item->type == lcf::rpg::Item::Type_material) {
SetBaseMaxHp(GetBaseMaxHp() + item->max_hp_points);
SetBaseMaxSp(GetBaseMaxSp() + item->max_sp_points);
SetBaseAtk(GetBaseAtk() + item->atk_points2);
SetBaseDef(GetBaseDef() + item->def_points2);
SetBaseAgi(GetBaseAgi() + item->agi_points2);
SetBaseSpi(GetBaseSpi() + item->spi_points2);
return true;
}
}
return Game_Battler::UseItem(item_id, source);
}
bool Game_Actor::IsItemUsable(int item_id) const {
const lcf::rpg::Item* item = lcf::ReaderUtil::GetElement(lcf::Data::items, item_id);
if (!item) {
Output::Warning("IsItemUsable: Invalid item ID {}", item_id);
return false;
}
int query_idx = GetId() - 1;
auto* query_set = &item->actor_set;
if (Player::IsRPG2k3() && lcf::Data::system.equipment_setting == lcf::rpg::System::EquipmentSetting_class) {
auto* cls = GetClass();
// Class index. If there's no class, in the "class_set" it's equal to 0. The first class is 1, not 0
query_idx = cls ? cls->ID : 0;
query_set = &item->class_set;
}
// If the actor or class ID is out of range this is an optimization in the ldb file
// (all actors or classes missing can equip the item)
if (query_set->size() <= (unsigned)(query_idx)) {
return true;
}
return (*query_set)[query_idx];
}
bool Game_Actor::IsSkillLearned(int skill_id) const {
return std::find(data.skills.begin(), data.skills.end(), skill_id) != data.skills.end();
}
bool Game_Actor::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 (!skill->affect_attr_defence) {
// Actor must have all attributes of the skill equipped as weapons
const auto* w1 = GetWeapon();
const auto* w2 = Get2ndWeapon();
for (size_t i = 0; i < skill->attribute_effects.size(); ++i) {
bool required = skill->attribute_effects[i] && lcf::Data::attributes[i].type == lcf::rpg::Attribute::Type_physical;
if (required) {
if (w1 && i < w1->attribute_set.size() && w1->attribute_set[i]) {
continue;
}
if (w2 && i < w2->attribute_set.size() && w2->attribute_set[i]) {
continue;
}
return false;
}
}
}
return Game_Battler::IsSkillUsable(skill_id);
}
int Game_Actor::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(), HasHalfSpCost());
}
bool Game_Actor::LearnSkill(int skill_id, PendingMessage* pm) {
if (skill_id > 0 && !IsSkillLearned(skill_id)) {
const lcf::rpg::Skill* skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (!skill) {
Output::Warning("Actor {}: Can't learn invalid skill {}", GetId(), skill_id);
return false;
}
data.skills.push_back((int16_t)skill_id);
std::sort(data.skills.begin(), data.skills.end());
if (pm) {
pm->PushLine(GetLearningMessage(*skill));
}
return true;
}
return false;
}
int Game_Actor::LearnLevelSkills(int min_level, int max_level, PendingMessage* pm) {
auto& skills = data.class_id > 0 ? GetClass()->skills : dbActor->skills;
int count = 0;
// Learn new skills
for (const lcf::rpg::Learning& learn : skills) {
// Skill learning, up to current level
if (learn.level >= min_level && learn.level <= max_level) {
count += LearnSkill(learn.skill_id, pm);
}
}
return count;
}
bool Game_Actor::UnlearnSkill(int skill_id) {
std::vector<int16_t>::iterator it = std::find(data.skills.begin(), data.skills.end(), skill_id);
if (it != data.skills.end()) {
data.skills.erase(it);
return true;
}
return false;
}
void Game_Actor::UnlearnAllSkills() {
data.skills.clear();
}
void Game_Actor::SetFace(const std::string& file_name, int index) {
if (file_name == dbActor->face_name && index == dbActor->face_index) {
data.face_name = "";
data.face_id = 0;
} else {
data.face_name.assign(file_name);
data.face_id = index;
}
}
const lcf::rpg::Item* Game_Actor::GetEquipment(int equip_type) const {
if (equip_type <= 0 || equip_type > (int)data.equipped.size())
return nullptr;
int item_id = data.equipped[equip_type - 1];
return lcf::ReaderUtil::GetElement(lcf::Data::items, item_id);
}
int Game_Actor::SetEquipment(int equip_type, int new_item_id) {
if (equip_type <= 0 || equip_type > (int) data.equipped.size())
return -1;
int old_item_id = data.equipped[equip_type - 1];
const lcf::rpg::Item* old_item = lcf::ReaderUtil::GetElement(lcf::Data::items, old_item_id);
const lcf::rpg::Item* new_item = lcf::ReaderUtil::GetElement(lcf::Data::items, new_item_id);
if (new_item_id != 0 && !new_item) {
Output::Warning("SetEquipment: Can't equip item with invalid ID {}", new_item_id);
new_item_id = 0;
}
data.equipped[equip_type - 1] = (short)new_item_id;
AdjustEquipmentStates(old_item, false, false);
AdjustEquipmentStates(new_item, true, false);
return old_item_id;
}
void Game_Actor::ChangeEquipment(int equip_type, int item_id) {
int prev_item = SetEquipment(equip_type, item_id);
if (prev_item != 0) {
Main_Data::game_party->AddItem(prev_item, 1);
}
if (item_id != 0) {
Main_Data::game_party->RemoveItem(item_id, 1);
}
// In case you have a two_handed weapon equipped, the other weapon is removed.
const lcf::rpg::Item* item = GetWeapon();
const lcf::rpg::Item* item2 = Get2ndWeapon();
if (item2 == nullptr) {
item2 = GetShield();
}
if (item && item2 && ((item->type == lcf::rpg::Item::Type_weapon && item->two_handed) || (item2->type == lcf::rpg::Item::Type_weapon && item2->two_handed))) {
ChangeEquipment(equip_type == lcf::rpg::Item::Type_weapon ? equip_type + 1 : equip_type - 1, 0);
}
}
bool Game_Actor::IsEquipped(int equip_id) const {
for (auto equip : GetWholeEquipment()) {
if (equip == equip_id) {
return true;
}
}
return false;
}
void Game_Actor::RemoveWholeEquipment() {
for (int i = 1; i <= 5; ++i) {
ChangeEquipment(i, 0);
}
}
int Game_Actor::GetItemCount(int item_id) {
int number = 0;
if (item_id > 0) {
for (int16_t i : GetWholeEquipment()) {
if (item_id == i) {
++number;
}
}
}
return number;
}
void Game_Actor::FullHeal() {
RemoveAllStates();
SetHp(GetMaxHp());
SetSp(GetMaxSp());
// Emulates RPG_RT behavior of resetting even battle equipment states on full heal.
ResetEquipmentStates(true);
}
int Game_Actor::GetBaseMaxHp(bool mod) const {
int n = 0;
// Special handling for games that use a level of 0 -> Return 0 Hp
// Same applies for other stats
if (GetLevel() > 0) {
// Looks like RPG_RT only applies Class changes (class_id > 0 - 20kdc)
// when the class was changed by the ChangeClass event, otherwise it uses
// the normal actor attributes.
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.maxhp, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.maxhp, GetLevel());
}
if (mod)
n += data.hp_mod;
return Utils::Clamp(n, 1, MaxHpValue());
}
int Game_Actor::GetBaseMaxHp() const {
return GetBaseMaxHp(true);
}
int Game_Actor::GetBaseMaxSp(bool mod) const {
int n = 0;
if (GetLevel() > 0) {
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.maxsp, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.maxsp, GetLevel());
}
if (mod)
n += data.sp_mod;
return Utils::Clamp(n, 0, MaxSpValue());
}
int Game_Actor::GetBaseMaxSp() const {
return GetBaseMaxSp(true);
}
static bool IsArmorType(const lcf::rpg::Item* item) {
return 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;
}
template <bool allow_weapon, bool allow_armor, typename F>
void ForEachEquipment(Span<const short> equipped, F&& f, Game_Battler::Weapon weapon = Game_Battler::WeaponAll) {
for (int slot = 0; slot < static_cast<int>(equipped.size()); ++slot) {
const auto item_id = equipped[slot];
if (item_id <= 0) {
continue;
}
auto* item = lcf::ReaderUtil::GetElement(lcf::Data::items, item_id);
// Invalid equipment was removed
assert(item != nullptr);
if (item->type == lcf::rpg::Item::Type_weapon) {
if (!allow_weapon || (weapon != Game_Battler::WeaponAll && weapon != slot + 1)) {
continue;
}
} else if (IsArmorType(item)) {
if (!allow_armor) {
continue;
}
} else {
assert(false && "Invalid item type equipped!");
continue;
}
f(*item);
}
}
int Game_Actor::GetBaseAtk(Weapon weapon, bool mod, bool equip) const {
int n = 0;
if (GetLevel() > 0) {
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.attack, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.attack, GetLevel());
}
if (mod) {
n += data.attack_mod;
}
if (equip) {
ForEachEquipment<true,true>(GetWholeEquipment(), [&](auto& item) { n += item.atk_points1; }, weapon);
}
return Utils::Clamp(n, 1, MaxStatBaseValue());
}
int Game_Actor::GetBaseAtk(Weapon weapon) const {
return GetBaseAtk(weapon, true, true);
}
int Game_Actor::GetBaseDef(Weapon weapon, bool mod, bool equip) const {
int n = 0;
if (GetLevel() > 0) {
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.defense, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.defense, GetLevel());
}
if (mod) {
n += data.defense_mod;
}
if (equip) {
ForEachEquipment<true,true>(GetWholeEquipment(), [&](auto& item) { n += item.def_points1; }, weapon);
}
return Utils::Clamp(n, 1, MaxStatBaseValue());
}
int Game_Actor::GetBaseDef(Weapon weapon) const {
return GetBaseDef(weapon, true, true);
}
int Game_Actor::GetBaseSpi(Weapon weapon, bool mod, bool equip) const {
int n = 0;
if (GetLevel() > 0) {
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.spirit, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.spirit, GetLevel());
}
if (mod) {
n += data.spirit_mod;
}
if (equip) {
ForEachEquipment<true,true>(GetWholeEquipment(), [&](auto& item) { n += item.spi_points1; }, weapon);
}
return Utils::Clamp(n, 1, MaxStatBaseValue());
}
int Game_Actor::GetBaseSpi(Weapon weapon) const {
return GetBaseSpi(weapon, true, true);
}
int Game_Actor::GetBaseAgi(Weapon weapon, bool mod, bool equip) const {
int n = 0;
if (GetLevel() > 0) {
n = data.class_id > 0
? *lcf::ReaderUtil::GetElement(GetClass()->parameters.agility, GetLevel())
: *lcf::ReaderUtil::GetElement(dbActor->parameters.agility, GetLevel());
}
if (mod) {
n += data.agility_mod;
}
if (equip) {
ForEachEquipment<true,true>(GetWholeEquipment(), [&](auto& item) { n += item.agi_points1; }, weapon);
}
return Utils::Clamp(n, 1, MaxStatBaseValue());
}
int Game_Actor::GetBaseAgi(Weapon weapon) const {
return GetBaseAgi(weapon, true, true);
}
int Game_Actor::CalculateExp(int level) const {
const lcf::rpg::Class* klass = lcf::ReaderUtil::GetElement(lcf::Data::classes, data.class_id);
int exp_curve = Player::IsRPG2k() ? 1 : 2;
if (lcf::Data::system.easyrpg_alternative_exp > 0) {
exp_curve = lcf::Data::system.easyrpg_alternative_exp;
}
double base, inflation, correction;
if (klass) {
base = klass->exp_base;
inflation = klass->exp_inflation;
correction = klass->exp_correction;
}
else {
const lcf::rpg::Actor& actor = *lcf::ReaderUtil::GetElement(lcf::Data::actors, GetId());
base = actor.exp_base;
inflation = actor.exp_inflation;
correction = actor.exp_correction;
}
int result = 0;
if (exp_curve == 1) {
inflation = 1.5 + (inflation * 0.01);
for (int i = level; i >= 1; i--)
{
result = result + (int)(correction + base);
base = base * inflation;
inflation = ((level+1) * 0.002 + 0.8) * (inflation - 1) + 1;
}
} else /*Rpg2k3*/ {
for (int i = 1; i <= level; i++)
{
result += (int)base;
result += i * (int)inflation;
result += (int)correction;
}
}
return min(result, MaxExpValue());
}
void Game_Actor::MakeExpList() {
exp_list.resize((size_t)GetMaxLevel());
for (int i = 1; i < (int)exp_list.size(); ++i) {
exp_list[i] = CalculateExp(i);
}
}
std::string Game_Actor::GetExpString(bool status_scene) const {
// RPG_RT displays dashes for max level. As a customization
// we always display the amount of EXP.
// if (GetNextExp() == -1) { return (MaxExpValue() >= 1000000 || status_scene) ? "-------" : "------"; }
return std::to_string(GetExp());
}
std::string Game_Actor::GetNextExpString(bool status_scene) const {
if (GetNextExp() == -1) {
return (MaxExpValue() >= 1000000 || status_scene) ? "-------" : "------";
}
return std::to_string(GetNextExp());
}
int Game_Actor::GetBaseExp() const {
return GetBaseExp(GetLevel());
}
int Game_Actor::GetBaseExp(int level) const {
return GetNextExp(level - 1);
}
int Game_Actor::GetNextExp() const {
return GetNextExp(GetLevel());
}
int Game_Actor::GetNextExp(int level) const {
if (level >= GetMaxLevel() || level <= -1) {
return -1;
} else if (level == 0) {
return 0;
} else {
return exp_list[level];
}
}
int Game_Actor::GetStateProbability(int state_id) const {
int rate = 2, mul = 100; // C - default
const uint8_t* r = lcf::ReaderUtil::GetElement(dbActor->state_ranks, state_id);
if (r) {
rate = *r;
}
// This takes the armor of the character with the most resistance for that particular state
for (const auto equipment : GetWholeEquipment()) {
lcf::rpg::Item* item = lcf::ReaderUtil::GetElement(lcf::Data::items, equipment);
if (item != nullptr
&& !(Player::IsRPG2k3() && item->reverse_state_effect)
&& (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)
&& state_id <= static_cast<int>(item->state_set.size()) && item->state_set[state_id - 1]) {
mul = std::min<int>(mul, 100 - item->state_chance);
}
}
// GetStateRate verifies the state_id
return GetStateRate(state_id, rate) * mul / 100;
}
int Game_Actor::GetBaseAttributeRate(int attribute_id) const {
int rate = 2; // C - default
const auto* r = lcf::ReaderUtil::GetElement(dbActor->attribute_ranks, attribute_id);
if (r) {
rate = *r;
}
bool boost = false;
ForEachEquipment<false,true>(GetWholeEquipment(), [&](auto& item) {
boost |= attribute_id >= 1 && attribute_id <= static_cast<int>(item.attribute_set.size()) && item.attribute_set[attribute_id - 1];
});
rate += boost;
return Utils::Clamp(rate, 0, 4);
}
int Game_Actor::GetWeaponId() const {
int item_id = GetWholeEquipment()[0];
return item_id <= (int)lcf::Data::items.size() ? item_id : 0;
}
int Game_Actor::GetShieldId() const {
int item_id = GetWholeEquipment()[1];
return item_id <= (int)lcf::Data::items.size() ? item_id : 0;
}
int Game_Actor::GetArmorId() const {
int item_id = GetWholeEquipment()[2];
return item_id <= (int)lcf::Data::items.size() ? item_id : 0;
}
int Game_Actor::GetHelmetId() const {
int item_id = GetWholeEquipment()[3];
return item_id <= (int)lcf::Data::items.size() ? item_id : 0;
}
int Game_Actor::GetAccessoryId() const {
int item_id = GetWholeEquipment()[4];
return item_id <= (int)lcf::Data::items.size() ? item_id : 0;
}
int Game_Actor::GetMaxLevel() const {
int max_level = Player::IsRPG2k() ? max_level_2k : max_level_2k3;
if (lcf::Data::system.easyrpg_max_level > -1) {
max_level = lcf::Data::system.easyrpg_max_level;
}
return Utils::Clamp<int32_t>(max_level, 1, dbActor->final_level);
}
void Game_Actor::SetExp(int _exp) {
data.exp = Utils::Clamp<int32_t>(_exp, 0, MaxExpValue());
}
void Game_Actor::ChangeExp(int exp, PendingMessage* pm) {
int new_level = GetLevel();
int new_exp = Utils::Clamp<int>(exp, 0, MaxExpValue());
if (new_exp > GetExp()) {
for (int i = GetLevel() + 1; i <= GetMaxLevel(); ++i) {
if (GetNextExp(new_level) != -1 && GetNextExp(new_level) > new_exp) {
break;
}
new_level++;
}
} else if (new_exp < GetExp()) {
for (int i = GetLevel(); i > 1; --i) {
if (new_exp >= GetNextExp(i - 1)) {
break;
}
new_level--;
}
}
SetExp(new_exp);
if (new_level != GetLevel()) {
ChangeLevel(new_level, pm);
}
}
void Game_Actor::SetLevel(int _level) {
data.level = Utils::Clamp(_level, 1, GetMaxLevel());
// Ensure current HP/SP remain clamped if new Max HP/SP is less.
SetHp(GetHp());
SetSp(GetSp());
}
std::string Game_Actor::GetLevelUpMessage(int new_level) const {
std::stringstream ss;
if (Player::IsRPG2k3E()) {
ss << GetName();
ss << " " << lcf::Data::terms.level_up << " ";
ss << " " << lcf::Data::terms.level << " " << new_level;
return ss.str();
} else if (Player::IsRPG2kE()) {
ss << new_level;
return Utils::ReplacePlaceholders(
lcf::Data::terms.level_up,
Utils::MakeArray('S', 'V', 'U'),
Utils::MakeSvArray(GetName(), ss.str(), lcf::Data::terms.level)
);
} else {
std::string particle, space = "";
if (Player::IsCP932()) {
particle = "は";
space += " ";
}
else {
particle = " ";
}
ss << GetName();
ss << particle << lcf::Data::terms.level << " ";
ss << new_level << space << lcf::Data::terms.level_up;
return ss.str();
}
}
std::string Game_Actor::GetLearningMessage(const lcf::rpg::Skill& skill) const {
if (Player::IsRPG2kE()) {
return Utils::ReplacePlaceholders(
lcf::Data::terms.skill_learned,
Utils::MakeArray('S', 'O'),
Utils::MakeSvArray(GetName(), skill.name)
);
}
return ToString(skill.name) + (Player::IsRPG2k3E() ? " " : "") + ToString(lcf::Data::terms.skill_learned);
}
void Game_Actor::ChangeLevel(int new_level, PendingMessage* pm) {
int old_level = GetLevel();
SetLevel(new_level);
new_level = GetLevel(); // Level adjusted to max
if (new_level > old_level) {
if (pm) {
pm->PushLine(GetLevelUpMessage(new_level));
}
// Learn new skills
LearnLevelSkills(old_level + 1, new_level, pm);
if (pm) {
pm->PushPageEnd();
}
// Experience adjustment:
// At least level minimum
SetExp(max(GetBaseExp(), GetExp()));
} else if (new_level < old_level) {
// Experience adjustment:
// Level minimum if higher then Level maximum
if (GetExp() >= GetNextExp()) {
SetExp(GetBaseExp());
}
}
}
bool Game_Actor::IsEquippable(int item_id) const {
const lcf::rpg::Item* item = lcf::ReaderUtil::GetElement(lcf::Data::items, item_id);
if (!item) {
Output::Warning("IsEquippable: Invalid item ID {}", item_id);
return false;
}
if (HasTwoWeapons() &&
item->type == lcf::rpg::Item::Type_shield) {
return false;
}
return IsItemUsable(item_id);
}
bool Game_Actor::IsEquipmentFixed(bool check_states) const {
if (data.lock_equipment) {
return true;
}
if (check_states) {
for (auto state_id: GetInflictedStates()) {
auto* state = lcf::ReaderUtil::GetElement(lcf::Data::states, state_id);
if (state && state->cursed) {
return true;
}
}
}
return false;
}
const lcf::rpg::Skill* Game_Actor::GetRandomSkill() const {
const std::vector<int16_t>& skills = GetSkills();
if (skills.empty()) {
return nullptr;
}
// Skills are guaranteed to be valid
return lcf::ReaderUtil::GetElement(lcf::Data::skills, skills[Rand::GetRandomNumber(0, skills.size() - 1)]);
}
Point Game_Actor::GetOriginalPosition() const {
return { dbActor->battle_x, dbActor->battle_y };
}
StringView Game_Actor::GetSkillName() const {
return dbActor->rename_skill ? StringView(dbActor->skill_name) : StringView(lcf::Data::terms.command_skill);
}
void Game_Actor::SetSprite(const std::string &file, int index, bool transparent) {
if (file == dbActor->character_name
&& index == dbActor->character_index
&& transparent == dbActor->transparent) {
data.sprite_name = "";
data.sprite_id = 0;
data.transparency = 0;
} else {
data.sprite_name = file;
data.sprite_id = index;
data.transparency = transparent ? 3 : 0;
}
}
void Game_Actor::ChangeBattleCommands(bool add, int id) {
auto& cmds = data.battle_commands;
// If changing battle commands, that is when RPG_RT will replace the -1 list with a 'true' list.
// Fetch original command array.
if (!data.changed_battle_commands) {
cmds = lcf::Data::actors[GetId() - 1].battle_commands;
data.changed_battle_commands = true;
}
// The battle commands array always has a size of 7 padded with -1. The last element before the padding is 0 which
// stands for the Row command
if (add) {
const lcf::rpg::BattleCommand* cmd = lcf::ReaderUtil::GetElement(lcf::Data::battlecommands.commands, id);
if (!cmd) {
Output::Warning("ChangeBattleCommands: Can't add invalid battle command {}", id);
return;
}
if (std::find(cmds.begin(), cmds.end(), id) == cmds.end()) {
std::vector<int32_t> new_cmds;
std::copy_if(cmds.begin(), cmds.end(),
std::back_inserter(new_cmds), [](int32_t i) { return i != 0 && i != -1; });
// Needs space for at least 2 more commands (new command and row)
if (new_cmds.size() >= 6) {
return;
}
new_cmds.push_back(id);
new_cmds.push_back(0);
cmds = new_cmds;
}
} else if (id == 0) {
cmds.clear();
cmds.push_back(0);
} else {
std::vector<int32_t>::iterator it;
it = std::find(cmds.begin(), cmds.end(), id);
if (it != cmds.end())
cmds.erase(it);
}
cmds.resize(7, -1);
}
const lcf::rpg::BattleCommand* Game_Actor::GetBattleCommand(int idx) const {
Span<const int32_t> commands;
if (data.changed_battle_commands) {
commands = data.battle_commands;
} else if (dbActor) {
commands = dbActor->battle_commands;
}
int cmd_id = 0;
if (idx >= 0 && idx < static_cast<int>(commands.size())) {
cmd_id = commands[idx];
}
return lcf::ReaderUtil::GetElement(lcf::Data::battlecommands.commands, cmd_id);
}
const std::vector<const lcf::rpg::BattleCommand*> Game_Actor::GetBattleCommands() const {
std::vector<const lcf::rpg::BattleCommand*> commands;
std::vector<int32_t> obc = data.battle_commands;
if (!data.changed_battle_commands) {
// In this case, get it straight from the LDB.
obc = lcf::Data::actors[GetId() - 1].battle_commands;
}
for (int command_index : obc) {
if (command_index == 0) {
// Row command -> not impl
continue;
}
if (command_index == -1) {
// Empty slot
continue;
}
const lcf::rpg::BattleCommand* cmd = lcf::ReaderUtil::GetElement(lcf::Data::battlecommands.commands, command_index);
if (!cmd) {
Output::Warning("GetBattleCommands: Invalid battle command ID {}", command_index);
continue;
}
commands.push_back(cmd);
}
return commands;
}
const lcf::rpg::Class* Game_Actor::GetClass() const {
int id = data.class_id;
if (id < 0) {
// This means class ID hasn't been changed yet.
id = dbActor->class_id;
}