forked from twig33/ynoclient
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame_interpreter_battle.cpp
568 lines (486 loc) · 15.4 KB
/
game_interpreter_battle.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
/*
* 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 "game_actors.h"
#include "game_battle.h"
#include "game_enemyparty.h"
#include "game_interpreter_battle.h"
#include "game_party.h"
#include "game_switches.h"
#include "game_system.h"
#include "game_variables.h"
#include <lcf/reader_util.h>
#include "output.h"
#include "player.h"
#include "game_map.h"
#include "spriteset_battle.h"
#include <cassert>
enum BranchBattleSubcommand {
eOptionBranchBattleElse = 1
};
Game_Interpreter_Battle::Game_Interpreter_Battle(Span<const lcf::rpg::TroopPage> pages)
: Game_Interpreter(true), pages(pages), executed(pages.size(), false)
{
}
bool Game_Interpreter_Battle::AreConditionsMet(const lcf::rpg::TroopPageCondition& condition, Game_Battler* source) {
if (!condition.flags.switch_a &&
!condition.flags.switch_b &&
!condition.flags.variable &&
!condition.flags.turn &&
!condition.flags.turn_enemy &&
!condition.flags.turn_actor &&
!condition.flags.fatigue &&
!condition.flags.enemy_hp &&
!condition.flags.actor_hp &&
!condition.flags.command_actor
) {
// Pages without trigger are never run
return false;
}
if (condition.flags.switch_a && !Main_Data::game_switches->Get(condition.switch_a_id))
return false;
if (condition.flags.switch_b && !Main_Data::game_switches->Get(condition.switch_b_id))
return false;
if (condition.flags.variable && !(Main_Data::game_variables->Get(condition.variable_id) >= condition.variable_value))
return false;
if (condition.flags.turn && !Game_Battle::CheckTurns(Game_Battle::GetTurn(), condition.turn_b, condition.turn_a))
return false;
if (condition.flags.turn_enemy) {
const auto& enemy = (*Main_Data::game_enemyparty)[condition.turn_enemy_id];
if (source && source != &enemy)
return false;
if (!Game_Battle::CheckTurns(enemy.GetBattleTurn(), condition.turn_enemy_b, condition.turn_enemy_a))
return false;
}
if (condition.flags.turn_actor) {
const auto* actor = Main_Data::game_actors->GetActor(condition.turn_actor_id);
if (source && source != actor)
return false;
if (!Game_Battle::CheckTurns(actor->GetBattleTurn(), condition.turn_actor_b, condition.turn_actor_a))
return false;
}
if (condition.flags.fatigue) {
int fatigue = Main_Data::game_party->GetFatigue();
if (fatigue < condition.fatigue_min || fatigue > condition.fatigue_max)
return false;
}
if (condition.flags.enemy_hp) {
Game_Battler& enemy = (*Main_Data::game_enemyparty)[condition.enemy_id];
int hp = enemy.GetHp();
int hpmin = enemy.GetMaxHp() * condition.enemy_hp_min / 100;
int hpmax = enemy.GetMaxHp() * condition.enemy_hp_max / 100;
if (hp < hpmin || hp > hpmax)
return false;
}
if (condition.flags.actor_hp) {
Game_Actor* actor = Main_Data::game_actors->GetActor(condition.actor_id);
int hp = actor->GetHp();
int hpmin = actor->GetMaxHp() * condition.actor_hp_min / 100;
int hpmax = actor->GetMaxHp() * condition.actor_hp_max / 100;
if (hp < hpmin || hp > hpmax)
return false;
}
if (condition.flags.command_actor) {
if (!source)
return false;
const auto* actor = Main_Data::game_actors->GetActor(condition.command_actor_id);
if (source != actor)
return false;
if (condition.command_id != actor->GetLastBattleAction())
return false;
}
return true;
}
int Game_Interpreter_Battle::ScheduleNextPage(Game_Battler* source) {
lcf::rpg::TroopPageCondition::Flags f;
for (auto& ff: f.flags) ff = true;
return ScheduleNextPage(f, source);
}
static bool HasRequiredCondition(lcf::rpg::TroopPageCondition::Flags page, lcf::rpg::TroopPageCondition::Flags required) {
for (size_t i = 0; i < page.flags.size(); ++i) {
if (required.flags[i] && page.flags[i]) {
return true;
}
}
return false;
}
int Game_Interpreter_Battle::ScheduleNextPage(lcf::rpg::TroopPageCondition::Flags required_conditions, Game_Battler* source) {
if (IsRunning()) {
return 0;
}
for (size_t i = 0; i < pages.size(); ++i) {
auto& page = pages[i];
if (executed[i]
|| !HasRequiredCondition(page.condition.flags, required_conditions)
|| !AreConditionsMet(page.condition, source)) {
continue;
}
Clear();
Push(page.event_commands, 0);
executed[i] = true;
return i + 1;
}
return 0;
}
// Execute Command.
bool Game_Interpreter_Battle::ExecuteCommand() {
auto& frame = GetFrame();
const auto& com = frame.commands[frame.current_command];
switch (static_cast<Cmd>(com.code)) {
case Cmd::CallCommonEvent:
return CommandCallCommonEvent(com);
case Cmd::ForceFlee:
return CommandForceFlee(com);
case Cmd::EnableCombo:
return CommandEnableCombo(com);
case Cmd::ChangeMonsterHP:
return CommandChangeMonsterHP(com);
case Cmd::ChangeMonsterMP:
return CommandChangeMonsterMP(com);
case Cmd::ChangeMonsterCondition:
return CommandChangeMonsterCondition(com);
case Cmd::ShowHiddenMonster:
return CommandShowHiddenMonster(com);
case Cmd::ChangeBattleBG:
return CommandChangeBattleBG(com);
case Cmd::ShowBattleAnimation_B:
return CommandShowBattleAnimation(com);
case Cmd::TerminateBattle:
return CommandTerminateBattle(com);
case Cmd::ConditionalBranch_B:
return CommandConditionalBranchBattle(com);
case Cmd::ElseBranch_B:
return CommandElseBranchBattle(com);
case Cmd::EndBranch_B:
return CommandEndBranchBattle(com);
case Cmd::Maniac_ControlBattle:
return CommandManiacControlBattle(com);
case Cmd::Maniac_ControlAtbGauge:
return CommandManiacControlAtbGauge(com);
case Cmd::Maniac_ChangeBattleCommandEx:
return CommandManiacChangeBattleCommandEx(com);
case Cmd::Maniac_GetBattleInfo:
return CommandManiacGetBattleInfo(com);
default:
return Game_Interpreter::ExecuteCommand();
}
}
// Commands
bool Game_Interpreter_Battle::CommandCallCommonEvent(lcf::rpg::EventCommand const& com) {
int evt_id = com.parameters[0];
Game_CommonEvent* common_event = lcf::ReaderUtil::GetElement(Game_Map::GetCommonEvents(), evt_id);
if (!common_event) {
Output::Warning("CallCommonEvent: Can't call invalid common event {}", evt_id);
return true;
}
Push(common_event);
return true;
}
bool Game_Interpreter_Battle::CommandForceFlee(lcf::rpg::EventCommand const& com) {
bool check = com.parameters[2] == 0;
switch (com.parameters[0]) {
case 0:
if (!check || Game_Battle::GetBattleCondition() != lcf::rpg::System::BattleCondition_pincers) {
this->force_flee_enabled = true;
}
break;
case 1:
if (!check || Game_Battle::GetBattleCondition() != lcf::rpg::System::BattleCondition_surround) {
int num_escaped = 0;
for (auto* enemy: Main_Data::game_enemyparty->GetEnemies()) {
if (enemy->Exists()) {
enemy->SetHidden(true);
enemy->SetDeathTimer();
++num_escaped;
}
}
if (num_escaped) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Game_System::SFX_Escape));
}
}
break;
case 2:
if (!check || Game_Battle::GetBattleCondition() != lcf::rpg::System::BattleCondition_surround) {
auto* enemy = Main_Data::game_enemyparty->GetEnemy(com.parameters[1]);
if (enemy->Exists()) {
enemy->SetHidden(true);
enemy->SetDeathTimer();
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Game_System::SFX_Escape));
}
}
break;
}
return true;
}
bool Game_Interpreter_Battle::CommandEnableCombo(lcf::rpg::EventCommand const& com) {
int actor_id = com.parameters[0];
if (!Main_Data::game_party->IsActorInParty(actor_id)) {
return true;
}
int command_id = com.parameters[1];
int multiple = com.parameters[2];
Game_Actor* actor = Main_Data::game_actors->GetActor(actor_id);
if (!actor) {
Output::Warning("EnableCombo: Invalid actor ID {}", actor_id);
return true;
}
actor->SetBattleCombo(command_id, multiple);
return true;
}
bool Game_Interpreter_Battle::CommandChangeMonsterHP(lcf::rpg::EventCommand const& com) {
int id = com.parameters[0];
Game_Enemy& enemy = (*Main_Data::game_enemyparty)[id];
bool lose = com.parameters[1] > 0;
bool lethal = com.parameters[4] > 0;
int hp = enemy.GetHp();
if (enemy.IsDead())
return true;
int change = 0;
switch (com.parameters[2]) {
case 0:
change = com.parameters[3];
break;
case 1:
change = Main_Data::game_variables->Get(com.parameters[3]);
break;
case 2:
change = com.parameters[3] * hp / 100;
break;
}
if (lose) {
change = -change;
}
enemy.ChangeHp(change, lethal);
auto& scene = Scene::instance;
if (scene) {
scene->OnEventHpChanged(&enemy, change);
}
if (enemy.IsDead()) {
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_EnemyKill));
enemy.SetDeathTimer();
}
return true;
}
bool Game_Interpreter_Battle::CommandChangeMonsterMP(lcf::rpg::EventCommand const& com) {
int id = com.parameters[0];
Game_Enemy& enemy = (*Main_Data::game_enemyparty)[id];
bool lose = com.parameters[1] > 0;
int sp = enemy.GetSp();
int change = 0;
switch (com.parameters[2]) {
case 0:
change = com.parameters[3];
break;
case 1:
change = Main_Data::game_variables->Get(com.parameters[3]);
break;
}
if (lose)
change = -change;
sp += change;
enemy.SetSp(sp);
return true;
}
bool Game_Interpreter_Battle::CommandChangeMonsterCondition(lcf::rpg::EventCommand const& com) {
Game_Enemy& enemy = (*Main_Data::game_enemyparty)[com.parameters[0]];
bool remove = com.parameters[1] > 0;
int state_id = com.parameters[2];
if (remove) {
// RPG_RT BUG: Monster dissapears immediately and doesn't animate death
enemy.RemoveState(state_id, false);
} else {
enemy.AddState(state_id, true);
}
return true;
}
bool Game_Interpreter_Battle::CommandShowHiddenMonster(lcf::rpg::EventCommand const& com) {
Game_Enemy& enemy = (*Main_Data::game_enemyparty)[com.parameters[0]];
enemy.SetHidden(false);
return true;
}
bool Game_Interpreter_Battle::CommandChangeBattleBG(lcf::rpg::EventCommand const& com) {
Game_Battle::ChangeBackground(ToString(com.string));
return true;
}
bool Game_Interpreter_Battle::CommandShowBattleAnimation(lcf::rpg::EventCommand const& com) {
int animation_id = com.parameters[0];
int target = com.parameters[1];
bool waiting_battle_anim = com.parameters[2] != 0;
bool allies = false;
if (Player::IsRPG2k3()) {
allies = com.parameters[3] != 0;
}
int frames = 0;
if (target < 0) {
std::vector<Game_Battler*> v;
if (allies) {
Main_Data::game_party->GetBattlers(v);
} else {
Main_Data::game_enemyparty->GetBattlers(v);
}
auto iter = std::remove_if(v.begin(), v.end(),
[](auto* target) { return !(target->Exists() || (target->GetType() == Game_Battler::Type_Ally && target->IsDead())); });
v.erase(iter, v.end());
frames = Game_Battle::ShowBattleAnimation(animation_id, v, false);
}
else {
Game_Battler* battler_target = nullptr;
if (allies) {
// Allies counted from 1
target -= 1;
if (target >= 0 && target < Main_Data::game_party->GetBattlerCount()) {
battler_target = &(*Main_Data::game_party)[target];
}
}
else {
if (target < Main_Data::game_enemyparty->GetBattlerCount()) {
battler_target = &(*Main_Data::game_enemyparty)[target];
}
}
if (battler_target) {
frames = Game_Battle::ShowBattleAnimation(animation_id, { battler_target });
}
}
if (waiting_battle_anim) {
_state.wait_time = frames;
}
return true;
}
bool Game_Interpreter_Battle::CommandTerminateBattle(lcf::rpg::EventCommand const& /* com */) {
_async_op = AsyncOp::MakeTerminateBattle(static_cast<int>(BattleResult::Abort));
return false;
}
// Conditional branch.
bool Game_Interpreter_Battle::CommandConditionalBranchBattle(lcf::rpg::EventCommand const& com) {
bool result = false;
int value1, value2;
switch (com.parameters[0]) {
case 0:
// Switch
result = Main_Data::game_switches->Get(com.parameters[1]) == (com.parameters[2] == 0);
break;
case 1:
// Variable
value1 = Main_Data::game_variables->Get(com.parameters[1]);
if (com.parameters[2] == 0) {
value2 = com.parameters[3];
} else {
value2 = Main_Data::game_variables->Get(com.parameters[3]);
}
switch (com.parameters[4]) {
case 0:
// Equal to
result = (value1 == value2);
break;
case 1:
// Greater than or equal
result = (value1 >= value2);
break;
case 2:
// Less than or equal
result = (value1 <= value2);
break;
case 3:
// Greater than
result = (value1 > value2);
break;
case 4:
// Less than
result = (value1 < value2);
break;
case 5:
// Different
result = (value1 != value2);
break;
}
break;
case 2: {
// Hero can act
Game_Actor* actor = Main_Data::game_actors->GetActor(com.parameters[1]);
if (!actor) {
Output::Warning("ConditionalBranchBattle: Invalid actor ID {}", com.parameters[1]);
// Use Else Branch
SetSubcommandIndex(com.indent, 1);
SkipToNextConditional({Cmd::ElseBranch_B, Cmd::EndBranch_B}, com.indent);
return true;
}
result = actor->CanAct();
break;
}
case 3:
// Monster can act
if (com.parameters[1] < Main_Data::game_enemyparty->GetBattlerCount()) {
result = (*Main_Data::game_enemyparty)[com.parameters[1]].CanAct();
}
break;
case 4:
// Monster is the current target
result = (targets_single_enemy && target_enemy_index == com.parameters[1]);
break;
case 5: {
// Hero uses the ... command
if (current_actor_id == com.parameters[1]) {
auto *actor = Main_Data::game_actors->GetActor(current_actor_id);
if (actor) {
result = actor->GetLastBattleAction() == com.parameters[2];
}
}
break;
}
}
int sub_idx = subcommand_sentinel;
if (!result) {
sub_idx = eOptionBranchBattleElse;
SkipToNextConditional({Cmd::ElseBranch_B, Cmd::EndBranch_B}, com.indent);
}
SetSubcommandIndex(com.indent, sub_idx);
return true;
}
bool Game_Interpreter_Battle::CommandElseBranchBattle(lcf::rpg::EventCommand const& com) { //code 23310
return CommandOptionGeneric(com, eOptionBranchBattleElse, {Cmd::EndBranch_B});
}
bool Game_Interpreter_Battle::CommandEndBranchBattle(lcf::rpg::EventCommand const& /* com */) { //code 23311
return true;
}
bool Game_Interpreter_Battle::CommandManiacControlBattle(lcf::rpg::EventCommand const&) {
if (!Player::IsPatchManiac()) {
return true;
}
Output::Warning("Maniac Patch: Command ControlBattle not supported");
return true;
}
bool Game_Interpreter_Battle::CommandManiacControlAtbGauge(lcf::rpg::EventCommand const&) {
if (!Player::IsPatchManiac()) {
return true;
}
Output::Warning("Maniac Patch: Command ControlAtbGauge not supported");
return true;
}
bool Game_Interpreter_Battle::CommandManiacChangeBattleCommandEx(lcf::rpg::EventCommand const&) {
if (!Player::IsPatchManiac()) {
return true;
}
Output::Warning("Maniac Patch: Command ChangeBattleCommandEx not supported");
return true;
}
bool Game_Interpreter_Battle::CommandManiacGetBattleInfo(lcf::rpg::EventCommand const&) {
if (!Player::IsPatchManiac()) {
return true;
}
Output::Warning("Maniac Patch: Command GetBattleInfo not supported");
return true;
}