forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
autobattle.cpp
380 lines (343 loc) · 13.7 KB
/
autobattle.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
/*
* 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 "autobattle.h"
#include "game_actor.h"
#include "game_enemy.h"
#include "game_enemyparty.h"
#include "game_party.h"
#include "game_battlealgorithm.h"
#include "game_battle.h"
#include "algo.h"
#include "player.h"
#include "output.h"
#include "rand.h"
#include <lcf/reader_util.h>
#include <lcf/data.h>
namespace AutoBattle {
#ifdef EP_DEBUG_AUTOBATTLE
template <typename... Args>
static void DebugLog(const char* fmt, Args&&... args) {
Output::Debug(fmt, std::forward<Args>(args)...);
}
#else
template <typename... Args>
static void DebugLog(const char*, Args&&...) {}
#endif
constexpr decltype(RpgRtCompat::name) RpgRtCompat::name;
constexpr decltype(AttackOnly::name) AttackOnly::name;
constexpr decltype(RpgRtImproved::name) RpgRtImproved::name;
std::unique_ptr<AlgorithmBase> CreateAlgorithm(StringView name) {
if (Utils::StrICmp(name, RpgRtImproved::name) == 0) {
return std::make_unique<RpgRtImproved>();
}
if (Utils::StrICmp(name, AttackOnly::name) == 0) {
return std::make_unique<AttackOnly>();
}
if (Utils::StrICmp(name, RpgRtCompat::name) != 0) {
static bool warned = false;
if (!warned) {
Output::Debug("Invalid AutoBattle algo name `{}' falling back to {} ...", name, RpgRtCompat::name);
warned = true;
}
}
return std::make_unique<RpgRtCompat>();
}
void AlgorithmBase::SetAutoBattleAction(Game_Actor& source) {
vSetAutoBattleAction(source);
if (source.GetBattleAlgorithm() == nullptr) {
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::None>(&source));
}
}
void RpgRtCompat::vSetAutoBattleAction(Game_Actor& source) {
SelectAutoBattleActionRpgRtCompat(source, Game_Battle::GetBattleCondition());
}
void AttackOnly::vSetAutoBattleAction(Game_Actor& source) {
SelectAutoBattleAction(source, Game_Battler::WeaponAll, Game_Battle::GetBattleCondition(), false, false, false, false);
}
void RpgRtImproved::vSetAutoBattleAction(Game_Actor& source) {
SelectAutoBattleAction(source, Game_Battler::WeaponAll, Game_Battle::GetBattleCondition(), true, false, false, false);
}
static int CalcSkillCostAutoBattle(const Game_Actor& source, const lcf::rpg::Skill& skill, bool emulate_bugs) {
// RPG_RT autobattle ignores half sp cost modifier
return emulate_bugs
? Algo::CalcSkillCost(skill, source.GetMaxSp(), false)
: source.CalculateSkillCost(skill.ID);
}
double CalcSkillHealAutoBattleTargetRank(const Game_Actor& source, const Game_Battler& target, const lcf::rpg::Skill& skill, bool apply_variance, bool emulate_bugs) {
assert(Algo::IsNormalOrSubskill(skill));
assert(Algo::SkillTargetsAllies(skill));
const double src_max_sp = source.GetMaxSp();
const double tgt_max_hp = target.GetMaxHp();
const double tgt_hp = target.GetHp();
if (target.GetHp() > 0) {
// Can the skill heal the target?
if (!skill.affect_hp) {
return 0.0;
}
const double base_effect = Algo::CalcSkillEffect(source, target, skill, apply_variance);
const double max_effect = std::min(base_effect, tgt_max_hp - tgt_hp);
auto rank = static_cast<double>(max_effect) / static_cast<double>(tgt_max_hp);
if (src_max_sp > 0) {
const double cost = CalcSkillCostAutoBattle(source, skill, emulate_bugs);
rank -= cost / src_max_sp / 8.0;
rank = std::max(rank, 0.0);
}
return rank;
}
// Can the skill revive the target?
if (skill.state_effects.size() > 1 && skill.state_effects[0]) {
// BUG: RPG_RT does not check the reverse_state_effect flag to skip skills which would kill party members
if (emulate_bugs || !skill.reverse_state_effect) {
return static_cast<double>(skill.power) / 1000.0 + 1.0;
}
}
return 0.0;
}
double CalcSkillDmgAutoBattleTargetRank(const Game_Actor& source, const Game_Battler& target, const lcf::rpg::Skill& skill, bool apply_variance, bool emulate_bugs) {
assert(Algo::IsNormalOrSubskill(skill));
assert(Algo::SkillTargetsEnemies(skill));
(void)emulate_bugs;
if (!(skill.affect_hp && target.Exists())) {
return 0.0;
}
double rank = 0.0;
const double src_max_sp = source.GetMaxSp();
const double tgt_hp = target.GetHp();
const double base_effect = Algo::CalcSkillEffect(source, target, skill, apply_variance);
rank = std::min(base_effect, tgt_hp) / tgt_hp;
if (rank == 1.0) {
rank = 1.5;
}
if (src_max_sp > 0) {
const double cost = CalcSkillCostAutoBattle(source, skill, emulate_bugs);
rank -= cost / src_max_sp / 4.0;
rank = std::max(rank, 0.0);
}
// Bonus if the target is the first existing enemy?
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
if (enemy->Exists()) {
if (enemy == &target) {
rank = rank * 1.5 + 0.5;
}
break;
}
}
return rank;
}
double CalcSkillAutoBattleRank(const Game_Actor& source, const lcf::rpg::Skill& skill, bool apply_variance, bool emulate_bugs) {
if (!source.IsSkillUsable(skill.ID)) {
return 0.0;
}
if (!Algo::IsNormalOrSubskill(skill)) {
return 0.0;
}
double rank = 0.0;
switch (skill.scope) {
case lcf::rpg::Skill::Scope_ally:
for (auto* target: Main_Data::game_party->GetActors()) {
auto target_rank = CalcSkillHealAutoBattleTargetRank(source, *target, skill, apply_variance, emulate_bugs);
rank = std::max(rank, target_rank);
DebugLog("AUTOBATTLE: Actor {} Check Skill Single Ally {} Rank : {}({}): {} -> {}", source.GetName(), target->GetName(), skill.name, skill.ID, rank, target_rank);
}
break;
case lcf::rpg::Skill::Scope_party:
for (auto* target: Main_Data::game_party->GetActors()) {
auto target_rank = CalcSkillHealAutoBattleTargetRank(source, *target, skill, apply_variance, emulate_bugs);
rank += target_rank;
DebugLog("AUTOBATTLE: Actor {} Check Skill Party Ally {} Rank : {}({}): {} -> {}", source.GetName(), target->GetName(), skill.name, skill.ID, rank, target_rank);
}
break;
case lcf::rpg::Skill::Scope_enemy:
for (auto* target: Main_Data::game_enemyparty->GetEnemies()) {
auto target_rank = CalcSkillDmgAutoBattleTargetRank(source, *target, skill, apply_variance, emulate_bugs);
rank = std::max(rank, target_rank);
DebugLog("AUTOBATTLE: Actor {} Check Skill Single Enemy {} Rank : {}({}): {} -> {}", source.GetName(), target->GetName(), skill.name, skill.ID, rank, target_rank);
}
break;
case lcf::rpg::Skill::Scope_enemies:
for (auto* target: Main_Data::game_enemyparty->GetEnemies()) {
auto target_rank = CalcSkillDmgAutoBattleTargetRank(source, *target, skill, apply_variance, emulate_bugs);
rank += target_rank;
DebugLog("AUTOBATTLE: Actor {} Check Skill Party Enemy {} Rank : {}({}): {} -> {}", source.GetName(), target->GetName(), skill.name, skill.ID, rank, target_rank);
}
break;
case lcf::rpg::Skill::Scope_self:
rank = CalcSkillHealAutoBattleTargetRank(source, source, skill, apply_variance, emulate_bugs);
DebugLog("AUTOBATTLE: Actor {} Check Skill Self Rank : {}({}): {}", source.GetName(), skill.name, skill.ID, rank);
break;
}
if (rank > 0.0) {
rank += Rand::GetRandomNumber(0, 99) / 100.0;
}
return rank;
}
double CalcNormalAttackAutoBattleTargetRank(const Game_Actor& source,
const Game_Battler& target,
Game_Battler::Weapon weapon,
lcf::rpg::System::BattleCondition cond,
bool apply_variance,
bool emulate_bugs)
{
if (!target.Exists()) {
return 0.0;
}
const bool is_critical_hit = false;
const bool is_charged = false;
// RPG_RT BUG: Normal damage variance is not used
// Note: RPG_RT does not do the "2k3_enemy_row_bug" when computing autobattle ranks.
double base_effect = Algo::CalcNormalAttackEffect(source, target, weapon, is_critical_hit, is_charged, apply_variance, cond, false);
// RPG_RT BUG: Dual Attack is ignored
if (!emulate_bugs) {
base_effect *= source.GetNumberOfAttacks(weapon);
}
const double tgt_hp = target.GetHp();
auto rank = std::min(base_effect, tgt_hp) / tgt_hp;
if (rank == 1.0) {
rank = 1.5;
}
if (!emulate_bugs) {
// EasyRPG customization - include sp cost of weapon attack using same logic as skill attack
const auto cost = std::min(source.CalculateWeaponSpCost(weapon), source.GetSp());
if (cost > 0) {
const double src_max_sp = source.GetMaxSp();
rank -= static_cast<double>(cost) / src_max_sp / 4.0;
rank = std::max(rank, 0.0);
}
}
// Bonus if the target is the first existing enemy?
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
if (enemy->Exists()) {
if (enemy == &target) {
rank = rank * 1.5 + 0.5;
}
break;
}
}
if (rank > 0.0) {
rank = Rand::GetRandomNumber(0, 99) / 100.0 + rank * 1.5;
}
return rank;
}
double CalcNormalAttackAutoBattleRank(const Game_Actor& source, Game_Battler::Weapon weapon, const lcf::rpg::System::BattleCondition cond, bool apply_variance, bool emulate_bugs) {
double rank = 0.0;
std::vector<Game_Battler*> targets;
Main_Data::game_enemyparty->GetBattlers(targets);
if (!emulate_bugs && source.HasAttackAll(weapon)) {
for (auto* target: targets) {
auto target_rank = CalcNormalAttackAutoBattleTargetRank(source, *target, weapon, cond, apply_variance, emulate_bugs);
rank += target_rank;
DebugLog("AUTOBATTLE: Actor {} Check Attack Party Enemy {} Rank : {} -> {}", source.GetName(), target->GetName(), rank, target_rank);
}
} else {
for (auto* target: targets) {
auto target_rank = CalcNormalAttackAutoBattleTargetRank(source, *target, weapon, cond, apply_variance, emulate_bugs);
rank = std::max(rank, target_rank);
DebugLog("AUTOBATTLE: Actor {} Check Attack Single Enemy {} Rank : {} -> {}", source.GetName(), target->GetName(), rank, target_rank);
}
}
return rank;
}
void SelectAutoBattleAction(Game_Actor& source,
Game_Battler::Weapon weapon,
lcf::rpg::System::BattleCondition cond,
bool do_skills,
bool attack_variance,
bool skill_variance,
bool emulate_bugs)
{
double skill_rank = 0.0;
lcf::rpg::Skill* skill = nullptr;
// Find the highest ranking skill
if (do_skills) {
for (auto& skill_id: source.GetSkills()) {
auto* candidate_skill = lcf::ReaderUtil::GetElement(lcf::Data::skills, skill_id);
if (candidate_skill) {
const auto rank = CalcSkillAutoBattleRank(source, *candidate_skill, skill_variance, emulate_bugs);
DebugLog("AUTOBATTLE: Actor {} Check Skill Rank : {}({}): {}", source.GetName(), candidate_skill->name, candidate_skill->ID, rank);
if (rank > skill_rank) {
skill_rank = rank;
skill = candidate_skill;
}
}
}
DebugLog("AUTOBATTLE: Actor {} Best Skill Rank : {}({}): {}", source.GetName(), skill->name, skill->ID, skill_rank);
}
double normal_attack_rank = CalcNormalAttackAutoBattleRank(source, weapon, cond, attack_variance, emulate_bugs);
DebugLog("AUTOBATTLE: Actor {} Normal Attack Rank : {}", source.GetName(), normal_attack_rank);
auto best_target_rank = 0.0;
Game_Battler* best_target = nullptr;
std::vector<Game_Battler*> targets;
if (skill != nullptr && normal_attack_rank < skill_rank) {
// Choose Skill Target
switch (skill->scope) {
case lcf::rpg::Skill::Scope_enemies:
DebugLog("AUTOBATTLE: Actor {} Select Skill Target : ALL ENEMIES", source.GetName());
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Skill>(&source, Main_Data::game_enemyparty.get(), *skill));
return;
case lcf::rpg::Skill::Scope_party:
DebugLog("AUTOBATTLE: Actor {} Select Skill Target : ALL ALLIES", source.GetName());
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Skill>(&source, Main_Data::game_party.get(), *skill));
return;
case lcf::rpg::Skill::Scope_enemy:
for (auto* target: Main_Data::game_enemyparty->GetEnemies()) {
const auto target_rank = CalcSkillDmgAutoBattleTargetRank(source, *target, *skill, skill_variance, emulate_bugs);
if (target_rank > best_target_rank) {
best_target_rank = target_rank;
best_target = target;
}
}
break;
case lcf::rpg::Skill::Scope_ally:
for (auto* target: Main_Data::game_party->GetActors()) {
const auto target_rank = CalcSkillHealAutoBattleTargetRank(source, *target, *skill, skill_variance, emulate_bugs);
if (target_rank > best_target_rank) {
best_target_rank = target_rank;
best_target = target;
}
}
break;
case lcf::rpg::Skill::Scope_self:
best_target = &source;
break;
}
if (best_target) {
DebugLog("AUTOBATTLE: Actor {} Select Skill Target : {}", source.GetName(), best_target->GetName());
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Skill>(&source, best_target, *skill));
}
return;
}
// Choose normal attack
if (source.HasAttackAll(weapon)) {
DebugLog("AUTOBATTLE: Actor {} Select Attack Target : ALL ENEMIES", source.GetName());
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Normal>(&source, Main_Data::game_enemyparty.get()));
return;
}
for (auto* target: Main_Data::game_enemyparty->GetEnemies()) {
const auto target_rank = CalcNormalAttackAutoBattleTargetRank(source, *target, weapon, cond, attack_variance, emulate_bugs);
// On case of ==, prefer the first enemy
if (target_rank > best_target_rank) {
best_target_rank = target_rank;
best_target = target;
}
}
if (best_target != nullptr) {
DebugLog("AUTOBATTLE: Actor {} Select Attack Target : {}", source.GetName(), best_target->GetName());
source.SetBattleAlgorithm(std::make_shared<Game_BattleAlgorithm::Normal>(&source, best_target));
return;
}
}
} // namespace AutoBattle