forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_map.cpp
2039 lines (1691 loc) · 52.9 KB
/
game_map.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 <cassert>
#include <initializer_list>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <climits>
#include <numeric>
#include <unordered_set>
#include "async_handler.h"
#include "options.h"
#include "system.h"
#include "game_battle.h"
#include "game_battler.h"
#include "game_map.h"
#include "game_interpreter_map.h"
#include "game_switches.h"
#include "game_player.h"
#include "game_party.h"
#include "game_message.h"
#include "game_screen.h"
#include "game_pictures.h"
#include "scene_battle.h"
#include "scene_map.h"
#include <lcf/lmu/reader.h>
#include <lcf/reader_lcf.h>
#include "map_data.h"
#include "main_data.h"
#include "output.h"
#include "util_macro.h"
#include "game_system.h"
#include "filefinder.h"
#include "player.h"
#include "input.h"
#include "utils.h"
#include "rand.h"
#include <lcf/scope_guard.h>
#include <lcf/rpg/save.h>
#include "scene_gameover.h"
#include "feature.h"
namespace {
// Intended bad value, Game_Map::Init sets them correctly
int screen_width = -1;
int screen_height = -1;
lcf::rpg::SaveMapInfo map_info;
lcf::rpg::SavePanorama panorama;
bool need_refresh;
int animation_type;
bool animation_fast;
std::vector<unsigned char> passages_down;
std::vector<unsigned char> passages_up;
std::vector<Game_Event> events;
std::vector<Game_CommonEvent> common_events;
std::unordered_map<int, MapEventCache> events_cache_by_switch;
std::unordered_map<int, MapEventCache> events_cache_by_variable;
std::unique_ptr<lcf::rpg::Map> map;
std::unique_ptr<Game_Interpreter_Map> interpreter;
std::vector<Game_Vehicle> vehicles;
lcf::rpg::Chipset* chipset;
//FIXME: Find a better way to do this.
bool panorama_on_map_init = true;
bool reset_panorama_x_on_next_init = true;
bool reset_panorama_y_on_next_init = true;
bool translation_changed = false;
// Used when the current map is not in the maptree
const lcf::rpg::MapInfo empty_map_info;
}
namespace Game_Map {
void SetupCommon();
}
void Game_Map::OnContinueFromBattle() {
Main_Data::game_system->BgmPlay(Main_Data::game_system->GetBeforeBattleMusic());
}
static Game_Map::Parallax::Params GetParallaxParams();
void Game_Map::Init() {
screen_width = (Player::screen_width / 16) * SCREEN_TILE_SIZE;
screen_height = (Player::screen_height / 16) * SCREEN_TILE_SIZE;
Dispose();
map_info = {};
panorama = {};
SetNeedRefresh(true);
interpreter.reset(new Game_Interpreter_Map(true));
InitCommonEvents();
vehicles.clear();
vehicles.emplace_back(Game_Vehicle::Boat);
vehicles.emplace_back(Game_Vehicle::Ship);
vehicles.emplace_back(Game_Vehicle::Airship);
}
void Game_Map::InitCommonEvents() {
common_events.clear();
common_events.reserve(lcf::Data::commonevents.size());
for (const lcf::rpg::CommonEvent& ev : lcf::Data::commonevents) {
common_events.emplace_back(ev.ID);
}
translation_changed = false;
}
void Game_Map::Dispose() {
events.clear();
events_cache_by_switch.clear();
events_cache_by_variable.clear();
map.reset();
map_info = {};
panorama = {};
}
void Game_Map::Quit() {
Dispose();
common_events.clear();
interpreter.reset();
}
int Game_Map::GetMapSaveCount() {
return (Player::IsRPG2k3() && map->save_count_2k3e > 0)
? map->save_count_2k3e
: map->save_count;
}
void Game_Map::Setup(std::unique_ptr<lcf::rpg::Map> map_in) {
Dispose();
screen_width = (Player::screen_width / 16) * SCREEN_TILE_SIZE;
screen_height = (Player::screen_height / 16) * SCREEN_TILE_SIZE;
map = std::move(map_in);
SetupCommon();
panorama_on_map_init = true;
Parallax::ClearChangedBG();
SetEncounterSteps(GetMapInfo().encounter_steps);
SetChipset(map->chipset_id);
std::iota(map_info.lower_tiles.begin(), map_info.lower_tiles.end(), 0);
std::iota(map_info.upper_tiles.begin(), map_info.upper_tiles.end(), 0);
// Save allowed
const auto* current_info = &GetMapInfo();
int current_index = current_info->ID;
int can_save = current_info->save;
int can_escape = current_info->escape;
int can_teleport = current_info->teleport;
while (can_save == lcf::rpg::MapInfo::TriState_parent
|| can_escape == lcf::rpg::MapInfo::TriState_parent
|| can_teleport == lcf::rpg::MapInfo::TriState_parent)
{
const auto* parent_info = &GetParentMapInfo(*current_info);
int parent_index = parent_info->ID;
if (parent_index == 0) {
// If parent is 0 and flag is parent, it's implicitly enabled.
break;
}
if (parent_index == current_index) {
Output::Warning("Map {} has parent pointing to itself!", current_index);
break;
}
current_info = parent_info;
if (can_save == lcf::rpg::MapInfo::TriState_parent) {
can_save = current_info->save;
}
if (can_escape == lcf::rpg::MapInfo::TriState_parent) {
can_escape = current_info->escape;
}
if (can_teleport == lcf::rpg::MapInfo::TriState_parent) {
can_teleport = current_info->teleport;
}
}
Main_Data::game_system->SetAllowSave(can_save != lcf::rpg::MapInfo::TriState_forbid);
Main_Data::game_system->SetAllowEscape(can_escape != lcf::rpg::MapInfo::TriState_forbid);
Main_Data::game_system->SetAllowTeleport(can_teleport != lcf::rpg::MapInfo::TriState_forbid);
auto& player = *Main_Data::game_player;
SetPositionX(player.GetX() * SCREEN_TILE_SIZE - player.GetPanX());
SetPositionY(player.GetY() * SCREEN_TILE_SIZE - player.GetPanY());
// Update the save counts so that if the player saves the game
// events will properly resume upon loading.
Main_Data::game_player->UpdateSaveCounts(lcf::Data::system.save_count, GetMapSaveCount());
}
void Game_Map::SetupFromSave(
std::unique_ptr<lcf::rpg::Map> map_in,
lcf::rpg::SaveMapInfo save_map,
lcf::rpg::SaveVehicleLocation save_boat,
lcf::rpg::SaveVehicleLocation save_ship,
lcf::rpg::SaveVehicleLocation save_airship,
lcf::rpg::SaveEventExecState save_fg_exec,
lcf::rpg::SavePanorama save_pan,
std::vector<lcf::rpg::SaveCommonEvent> save_ce) {
map = std::move(map_in);
map_info = std::move(save_map);
panorama = std::move(save_pan);
SetupCommon();
const bool is_db_save_compat = Main_Data::game_player->IsDatabaseCompatibleWithSave(lcf::Data::system.save_count);
const bool is_map_save_compat = Main_Data::game_player->IsMapCompatibleWithSave(GetMapSaveCount());
InitCommonEvents();
if (is_db_save_compat && is_map_save_compat) {
for (size_t i = 0; i < std::min(save_ce.size(), common_events.size()); ++i) {
common_events[i].SetSaveData(save_ce[i].parallel_event_execstate);
}
}
if (is_map_save_compat) {
for (size_t i = 0; i < std::min(map->events.size(), map_info.events.size()); ++i) {
auto& ev = events[i];
ev.SetSaveData(map_info.events[i]);
}
}
map_info.events.clear();
interpreter->Clear();
GetVehicle(Game_Vehicle::Boat)->SetSaveData(std::move(save_boat));
GetVehicle(Game_Vehicle::Ship)->SetSaveData(std::move(save_ship));
GetVehicle(Game_Vehicle::Airship)->SetSaveData(std::move(save_airship));
if (is_map_save_compat) {
// Make main interpreter "busy" if save contained events to prevent auto-events from starting
interpreter->SetState(std::move(save_fg_exec));
}
SetEncounterSteps(map_info.encounter_steps);
// RPG_RT bug: Chipset is not loaded. Fixed in 2k3E
if (Player::IsRPG2k3E()) {
SetChipset(map_info.chipset_id);
} else {
SetChipset(0);
}
if (!is_map_save_compat) {
panorama = {};
}
// We want to support loading rm2k3e panning chunks
// but also not break other saves which don't have them.
// To solve this problem, we reuse the scrolling methods
// which always reset the position anyways when scroll_horz/vert
// is false.
// This produces compatible behavior for old RPG_RT saves, namely
// the pan_x/y is always forced to 0.
// If the later async code will load panorama, set the flag to not clear the offsets.
// FIXME: RPG_RT compatibility bug: Everytime we load a savegame with default panorama chunks,
// this causes them to get overwritten
// FIXME: RPG_RT compatibility bug: On async platforms, panorama async loading can
// cause panorama chunks to be out of sync.
Game_Map::Parallax::ChangeBG(GetParallaxParams());
}
std::unique_ptr<lcf::rpg::Map> Game_Map::loadMapFile(int map_id) {
std::unique_ptr<lcf::rpg::Map> map;
// Try loading EasyRPG map files first, then fallback to normal RPG Maker
// FIXME: Assert map was cached for async platforms
std::string map_name = Game_Map::ConstructMapName(map_id, true);
std::string map_file = FileFinder::Game().FindFile(map_name);
if (map_file.empty()) {
map_name = Game_Map::ConstructMapName(map_id, false);
map_file = FileFinder::Game().FindFile(map_name);
if (map_file.empty()) {
Output::Error("Loading of Map {} failed.\nThe map was not found.", map_name);
return nullptr;
}
auto map_stream = FileFinder::Game().OpenInputStream(map_file);
if (!map_stream) {
Output::Error("Loading of Map {} failed.\nMap not readable.", map_name);
return nullptr;
}
map = lcf::LMU_Reader::Load(map_stream, Player::encoding);
if (Input::IsRecording()) {
map_stream.clear();
map_stream.seekg(0);
Input::AddRecordingData(Input::RecordingData::Hash,
fmt::format("map{:04} {:#08x}", map_id, Utils::CRC32(map_stream)));
}
} else {
auto map_stream = FileFinder::Game().OpenInputStream(map_file);
if (!map_stream) {
Output::Error("Loading of Map {} failed.\nMap not readable.", map_name);
return nullptr;
}
map = lcf::LMU_Reader::LoadXml(map_stream);
}
Output::Debug("Loaded Map {}", map_name);
if (map.get() == NULL) {
Output::ErrorStr(lcf::LcfReader::GetError());
}
return map;
}
void Game_Map::SetupCommon() {
if (!Tr::GetCurrentTranslationId().empty()) {
// Build our map translation id.
std::stringstream ss;
ss << "map" << std::setfill('0') << std::setw(4) << GetMapId() << ".po";
// Translate all messages for this map
Player::translation.RewriteMapMessages(ss.str(), *map);
}
SetNeedRefresh(true);
PrintPathToMap();
// Restart all common events after translation change
// Otherwise new strings are not applied
if (translation_changed) {
InitCommonEvents();
}
// Create the map events
events.reserve(map->events.size());
for (auto& ev : map->events) {
events.emplace_back(GetMapId(), &ev);
for (const auto& pg : ev.pages) {
if (pg.condition.flags.switch_a) {
AddEventToSwitchCache(ev, pg.condition.switch_a_id);
}
if (pg.condition.flags.switch_b) {
AddEventToSwitchCache(ev, pg.condition.switch_b_id);
}
if (pg.condition.flags.variable) {
AddEventToVariableCache(ev, pg.condition.variable_id);
}
}
}
}
void Game_Map::AddEventToSwitchCache(lcf::rpg::Event& ev, int switch_id) {
events_cache_by_switch[switch_id].AddEvent(ev);
}
void Game_Map::AddEventToVariableCache(lcf::rpg::Event& ev, int var_id) {
events_cache_by_variable[var_id].AddEvent(ev);
}
void Game_Map::PrepareSave(lcf::rpg::Save& save) {
save.foreground_event_execstate = interpreter->GetSaveState();
save.airship_location = GetVehicle(Game_Vehicle::Airship)->GetSaveData();
save.ship_location = GetVehicle(Game_Vehicle::Ship)->GetSaveData();
save.boat_location = GetVehicle(Game_Vehicle::Boat)->GetSaveData();
save.map_info = map_info;
save.map_info.chipset_id = GetChipset();
if (save.map_info.chipset_id == GetOriginalChipset()) {
// This emulates RPG_RT behavior, where chipset id == 0 means use the default map chipset.
save.map_info.chipset_id = 0;
}
if (save.map_info.encounter_steps == GetOriginalEncounterSteps()) {
save.map_info.encounter_steps = -1;
}
// Note: RPG_RT does not use a sentinel for parallax parameters. Once the parallax BG is changed, it stays that way forever.
save.map_info.events.clear();
save.map_info.events.reserve(events.size());
for (Game_Event& ev : events) {
save.map_info.events.push_back(ev.GetSaveData());
}
save.panorama = panorama;
save.common_events.clear();
save.common_events.reserve(common_events.size());
for (Game_CommonEvent& ev : common_events) {
save.common_events.push_back(lcf::rpg::SaveCommonEvent());
save.common_events.back().ID = ev.GetIndex();
save.common_events.back().parallel_event_execstate = ev.GetSaveData();
}
}
void Game_Map::PlayBgm() {
const auto* current_info = &GetMapInfo();
while (current_info->music_type == 0 && GetParentMapInfo(*current_info).ID != current_info->ID) {
current_info = &GetParentMapInfo(*current_info);
}
if ((current_info->ID > 0) && !current_info->music.name.empty()) {
if (current_info->music_type == 1) {
return;
}
auto& music = current_info->music;
if (!Main_Data::game_player->IsAboard()) {
Main_Data::game_system->BgmPlay(music);
} else {
Main_Data::game_system->SetBeforeVehicleMusic(music);
}
}
}
std::vector<uint8_t> Game_Map::GetTilesLayer(int layer) {
return layer >= 1 ? map_info.upper_tiles : map_info.lower_tiles;
}
void Game_Map::Refresh() {
if (GetMapId() > 0) {
for (Game_Event& ev : events) {
ev.RefreshPage();
}
}
need_refresh = false;
}
Game_Interpreter_Map& Game_Map::GetInterpreter() {
assert(interpreter);
return *interpreter;
}
void Game_Map::Scroll(int dx, int dy) {
int x = map_info.position_x;
AddScreenX(x, dx);
map_info.position_x = x;
int y = map_info.position_y;
AddScreenY(y, dy);
map_info.position_y = y;
if (dx == 0 && dy == 0) {
return;
}
Main_Data::game_screen->OnMapScrolled(dx, dy);
Main_Data::game_pictures->OnMapScrolled(dx, dy);
Game_Map::Parallax::ScrollRight(dx);
Game_Map::Parallax::ScrollDown(dy);
}
// Add inc to acc, clamping the result into the range [low, high].
// If the result is clamped, inc is also modified to be actual amount
// that acc changed by.
static void ClampingAdd(int low, int high, int& acc, int& inc) {
int original_acc = acc;
acc = std::max(low, std::min(high, acc + inc));
inc = acc - original_acc;
}
void Game_Map::AddScreenX(int& screen_x, int& inc) {
int map_width = GetTilesX() * SCREEN_TILE_SIZE;
if (LoopHorizontal()) {
screen_x = (screen_x + inc) % map_width;
} else {
ClampingAdd(0, map_width - screen_width, screen_x, inc);
}
}
void Game_Map::AddScreenY(int& screen_y, int& inc) {
int map_height = GetTilesY() * SCREEN_TILE_SIZE;
if (LoopVertical()) {
screen_y = (screen_y + inc) % map_height;
} else {
ClampingAdd(0, map_height - screen_height, screen_y, inc);
}
}
bool Game_Map::IsValid(int x, int y) {
return (x >= 0 && x < GetTilesX() && y >= 0 && y < GetTilesY());
}
static int GetPassableMask(int old_x, int old_y, int new_x, int new_y) {
int bit = 0;
if (new_x > old_x) { bit |= Passable::Right; }
if (new_x < old_x) { bit |= Passable::Left; }
if (new_y > old_y) { bit |= Passable::Down; }
if (new_y < old_y) { bit |= Passable::Up; }
return bit;
}
static bool WouldCollide(const Game_Character& self, const Game_Character& other, bool self_conflict) {
if (self.GetThrough() || other.GetThrough()) {
return false;
}
if (self.IsFlying() || other.IsFlying()) {
return false;
}
if (!self.IsActive() || !other.IsActive()) {
return false;
}
if (self.GetType() == Game_Character::Event
&& other.GetType() == Game_Character::Event
&& (self.IsOverlapForbidden() || other.IsOverlapForbidden())) {
return true;
}
if (other.GetLayer() == lcf::rpg::EventPage::Layers_same && self_conflict) {
return true;
}
if (self.GetLayer() == other.GetLayer()) {
return true;
}
return false;
}
template <typename T>
static void MakeWayUpdate(T& other) {
other.Update();
}
static void MakeWayUpdate(Game_Event& other) {
other.Update(false);
}
template <typename T>
static bool CheckWayTestCollideEvent(int x, int y, const Game_Character& self, T& other, bool self_conflict) {
if (&self == &other) {
return false;
}
if (!other.IsInPosition(x, y)) {
return false;
}
return WouldCollide(self, other, self_conflict);
}
template <typename T>
static bool MakeWayCollideEvent(int x, int y, const Game_Character& self, T& other, bool self_conflict) {
if (&self == &other) {
return false;
}
if (!other.IsInPosition(x, y)) {
return false;
}
// Force the other event to update, allowing them to possibly move out of the way.
MakeWayUpdate(other);
if (!other.IsInPosition(x, y)) {
return false;
}
return WouldCollide(self, other, self_conflict);
}
static Game_Vehicle::Type GetCollisionVehicleType(const Game_Character* ch) {
if (ch && ch->GetType() == Game_Character::Vehicle) {
return static_cast<Game_Vehicle::Type>(static_cast<const Game_Vehicle*>(ch)->GetVehicleType());
}
return Game_Vehicle::None;
}
bool Game_Map::CheckWay(const Game_Character& self,
int from_x, int from_y,
int to_x, int to_y
)
{
return CheckOrMakeWayEx(
self, from_x, from_y, to_x, to_y, true, nullptr, false
);
}
bool Game_Map::CheckWay(const Game_Character& self,
int from_x, int from_y,
int to_x, int to_y,
bool check_events_and_vehicles,
std::unordered_set<int> *ignore_some_events_by_id) {
return CheckOrMakeWayEx(
self, from_x, from_y, to_x, to_y,
check_events_and_vehicles,
ignore_some_events_by_id, false
);
}
bool Game_Map::CheckOrMakeWayEx(const Game_Character& self,
int from_x, int from_y,
int to_x, int to_y,
bool check_events_and_vehicles,
std::unordered_set<int> *ignore_some_events_by_id,
bool make_way
)
{
// Infer directions before we do any rounding.
const int bit_from = GetPassableMask(from_x, from_y, to_x, to_y);
const int bit_to = GetPassableMask(to_x, to_y, from_x, from_y);
// Now round for looping maps.
to_x = Game_Map::RoundX(to_x);
to_y = Game_Map::RoundY(to_y);
// Note, even for diagonal, if the tile is invalid we still check vertical/horizontal first!
if (!Game_Map::IsValid(to_x, to_y)) {
return false;
}
if (self.GetThrough()) {
return true;
}
const auto vehicle_type = GetCollisionVehicleType(&self);
bool self_conflict = false;
// Depending on whether we're supposed to call MakeWayCollideEvent
// (which might change the map) or not, choose what to call:
auto CheckOrMakeCollideEvent = [&](auto& other) {
if (make_way) {
return MakeWayCollideEvent(to_x, to_y, self, other, self_conflict);
} else {
return CheckWayTestCollideEvent(
to_x, to_y, self, other, self_conflict
);
}
};
if (!self.IsJumping()) {
// Check for self conflict.
// If this event has a tile graphic and the tile itself has passage blocked in the direction
// we want to move, flag it as "self conflicting" for use later.
if (self.GetLayer() == lcf::rpg::EventPage::Layers_below && self.GetTileId() != 0) {
int tile_id = self.GetTileId();
if ((passages_up[tile_id] & bit_from) == 0) {
self_conflict = true;
}
}
if (vehicle_type == Game_Vehicle::None) {
// Check that we are allowed to step off of the current tile.
// Note: Vehicles can always step off a tile.
// The current coordinate can be invalid due to an out-of-bounds teleport or a "Set Location" event.
// Round it for looping maps to ensure the check passes
// This is not fully bug compatible to RPG_RT. Assuming the Y-Coordinate is out-of-bounds: When moving
// left or right the invalid Y will stay in RPG_RT preventing events from being triggered, but we wrap it
// inbounds after the first move.
from_x = Game_Map::RoundX(from_x);
from_y = Game_Map::RoundY(from_y);
if (!IsPassableTile(&self, bit_from, from_x, from_y)) {
return false;
}
}
}
if (vehicle_type != Game_Vehicle::Airship && check_events_and_vehicles) {
// Check for collision with events on the target tile.
for (auto& other: GetEvents()) {
if (ignore_some_events_by_id != NULL &&
ignore_some_events_by_id->find(other.GetId()) !=
ignore_some_events_by_id->end())
continue;
if (CheckOrMakeCollideEvent(other)) {
return false;
}
}
auto& player = Main_Data::game_player;
if (player->GetVehicleType() == Game_Vehicle::None) {
if (CheckOrMakeCollideEvent(*Main_Data::game_player)) {
return false;
}
}
for (auto vid: { Game_Vehicle::Boat, Game_Vehicle::Ship}) {
auto& other = vehicles[vid - 1];
if (other.IsInCurrentMap()) {
if (CheckOrMakeCollideEvent(other)) {
return false;
}
}
}
auto& airship = vehicles[Game_Vehicle::Airship - 1];
if (airship.IsInCurrentMap() && self.GetType() != Game_Character::Player) {
if (CheckOrMakeCollideEvent(airship)) {
return false;
}
}
}
int bit = bit_to;
if (self.IsJumping()) {
bit = Passable::Down | Passable::Up | Passable::Left | Passable::Right;
}
return IsPassableTile(
&self, bit, to_x, to_y, check_events_and_vehicles, true
);
}
bool Game_Map::MakeWay(const Game_Character& self,
int from_x, int from_y,
int to_x, int to_y
)
{
return CheckOrMakeWayEx(
self, from_x, from_y, to_x, to_y, true, NULL, true
);
}
bool Game_Map::CanLandAirship(int x, int y) {
if (!Game_Map::IsValid(x, y)) return false;
const auto* terrain = lcf::ReaderUtil::GetElement(lcf::Data::terrains, GetTerrainTag(x, y));
if (!terrain) {
Output::Warning("CanLandAirship: Invalid terrain at ({}, {})", x, y);
return false;
}
if (!terrain->airship_land) {
return false;
}
for (auto& ev: events) {
if (ev.IsInPosition(x, y)
&& ev.IsActive()
&& ev.GetActivePage() != nullptr) {
return false;
}
}
for (auto vid: { Game_Vehicle::Boat, Game_Vehicle::Ship }) {
auto& vehicle = vehicles[vid - 1];
if (vehicle.IsInCurrentMap() && vehicle.IsInPosition(x, y)) {
return false;
}
}
const int bit = Passable::Down | Passable::Right | Passable::Left | Passable::Up;
int tile_index = x + y * GetTilesX();
if (!IsPassableLowerTile(bit, tile_index)) {
return false;
}
int tile_id = map->upper_layer[tile_index] - BLOCK_F;
tile_id = map_info.upper_tiles[tile_id];
return (passages_up[tile_id] & bit) != 0;
}
bool Game_Map::CanEmbarkShip(Game_Player& player, int x, int y) {
auto bit = GetPassableMask(player.GetX(), player.GetY(), x, y);
return IsPassableTile(&player, bit, player.GetX(), player.GetY());
}
bool Game_Map::CanDisembarkShip(Game_Player& player, int x, int y) {
if (!Game_Map::IsValid(x, y)) {
return false;
}
for (auto& ev: GetEvents()) {
if (ev.IsInPosition(x, y)
&& ev.GetLayer() == lcf::rpg::EventPage::Layers_same
&& ev.IsActive()
&& ev.GetActivePage() != nullptr) {
return false;
}
}
int bit = GetPassableMask(x, y, player.GetX(), player.GetY());
return IsPassableTile(nullptr, bit, x, y);
}
bool Game_Map::IsPassableLowerTile(int bit, int tile_index) {
int tile_raw_id = map->lower_layer[tile_index];
int tile_id = 0;
if (tile_raw_id >= BLOCK_E) {
tile_id = tile_raw_id - BLOCK_E;
tile_id = map_info.lower_tiles[tile_id] + BLOCK_E_INDEX;
} else if (tile_raw_id >= BLOCK_D) {
tile_id = (tile_raw_id - BLOCK_D) / BLOCK_D_STRIDE + BLOCK_D_INDEX;
int autotile_id = (tile_raw_id - BLOCK_D) % BLOCK_D_STRIDE;
if (((passages_down[tile_id] & Passable::Wall) != 0) && (
(autotile_id >= 20 && autotile_id <= 23) ||
(autotile_id >= 33 && autotile_id <= 37) ||
autotile_id == 42 || autotile_id == 43 ||
autotile_id == 45 || autotile_id == 46))
return true;
} else if (tile_raw_id >= BLOCK_C) {
tile_id = (tile_raw_id - BLOCK_C) / BLOCK_C_STRIDE + BLOCK_C_INDEX;
} else if (map->lower_layer[tile_index] < BLOCK_C) {
tile_id = tile_raw_id / BLOCK_B_STRIDE;
}
return (passages_down[tile_id] & bit) != 0;
}
bool Game_Map::IsPassableTile(
const Game_Character* self, int bit, int x, int y
) {
return IsPassableTile(
self, bit, x, y, true, true
);
}
bool Game_Map::IsPassableTile(
const Game_Character* self, int bit, int x, int y,
bool check_events_and_vehicles, bool check_map_geometry
) {
if (!IsValid(x, y)) return false;
const auto vehicle_type = GetCollisionVehicleType(self);
if (check_events_and_vehicles) {
if (vehicle_type != Game_Vehicle::None) {
const auto* terrain = lcf::ReaderUtil::GetElement(lcf::Data::terrains, GetTerrainTag(x, y));
if (!terrain) {
Output::Warning("IsPassableTile: Invalid terrain at ({}, {})", x, y);
return false;
}
if (vehicle_type == Game_Vehicle::Boat && !terrain->boat_pass) {
return false;
}
if (vehicle_type == Game_Vehicle::Ship && !terrain->ship_pass) {
return false;
}
if (vehicle_type == Game_Vehicle::Airship) {
return terrain->airship_pass;
}
}
// Highest ID event with layer=below, not through, and a tile graphic wins.
int event_tile_id = 0;
for (auto& ev: events) {
if (self == &ev) {
continue;
}
if (!ev.IsActive() || ev.GetActivePage() == nullptr || ev.GetThrough()) {
continue;
}
if (ev.IsInPosition(x, y) && ev.GetLayer() == lcf::rpg::EventPage::Layers_below) {
int tile_id = ev.GetTileId();
if (tile_id > 0) {
event_tile_id = tile_id;
}
}
}
// If there was a below tile event, and the tile is not above
// Override the chipset with event tile behavior.
if (event_tile_id > 0
&& ((passages_up[event_tile_id] & Passable::Above) == 0)) {
switch (vehicle_type) {
case Game_Vehicle::None:
return ((passages_up[event_tile_id] & bit) != 0);
case Game_Vehicle::Boat:
case Game_Vehicle::Ship:
return false;
case Game_Vehicle::Airship:
break;
};
}
}
if (check_map_geometry) {
int tile_index = x + y * GetTilesX();
int tile_id = map->upper_layer[tile_index] - BLOCK_F;
tile_id = map_info.upper_tiles[tile_id];
if (vehicle_type == Game_Vehicle::Boat || vehicle_type == Game_Vehicle::Ship) {
if ((passages_up[tile_id] & Passable::Above) == 0)
return false;
return true;
}
if ((passages_up[tile_id] & bit) == 0)
return false;
if ((passages_up[tile_id] & Passable::Above) == 0)
return true;
return IsPassableLowerTile(bit, tile_index);
} else {
return true;
}
}
int Game_Map::GetBushDepth(int x, int y) {
if (!Game_Map::IsValid(x, y)) return 0;
const lcf::rpg::Terrain* terrain = lcf::ReaderUtil::GetElement(lcf::Data::terrains, GetTerrainTag(x,y));
if (!terrain) {
Output::Warning("GetBushDepth: Invalid terrain at ({}, {})", x, y);
return 0;
}
return terrain->bush_depth;
}
bool Game_Map::IsCounter(int x, int y) {
if (!Game_Map::IsValid(x, y)) return false;
int const tile_id = map->upper_layer[x + y * GetTilesX()];
if (tile_id < BLOCK_F) return false;
int const index = map_info.upper_tiles[tile_id - BLOCK_F];
return !!(passages_up[index] & Passable::Counter);
}
int Game_Map::GetTerrainTag(int x, int y) {
if (!chipset) {
// FIXME: Is this ever possible?
return 1;
}
auto& terrain_data = chipset->terrain_data;
if (terrain_data.empty()) {
// RPG_RT optimisation: When the terrain is all 1, no terrain data is stored
return 1;
}
// Terrain tag wraps on looping maps
if (Game_Map::LoopHorizontal()) {
x = RoundX(x);
}
if (Game_Map::LoopVertical()) {
y = RoundY(y);
}
// RPG_RT always uses the terrain of the first lower tile
// for out of bounds coordinates.
unsigned chip_index = 0;
if (Game_Map::IsValid(x, y)) {
const auto chip_id = map->lower_layer[x + y * GetTilesX()];
chip_index = ChipIdToIndex(chip_id);
// Apply tile substitution
if (chip_index >= BLOCK_E_INDEX && chip_index < NUM_LOWER_TILES) {
chip_index = map_info.lower_tiles[chip_index - BLOCK_E_INDEX] + BLOCK_E_INDEX;
}
}
assert(chip_index < terrain_data.size());
return terrain_data[chip_index];
}
void Game_Map::GetEventsXY(std::vector<Game_Event*>& events, int x, int y) {
for (Game_Event& ev : GetEvents()) {
if (ev.IsInPosition(x, y) && ev.IsActive()) {
events.push_back(&ev);
}
}
}
Game_Event* Game_Map::GetEventAt(int x, int y, bool require_active) {
auto& events = GetEvents();
for (auto iter = events.rbegin(); iter != events.rend(); ++iter) {
auto& ev = *iter;
if (ev.IsInPosition(x, y) && (!require_active || ev.IsActive())) {
return &ev;
}
}
return nullptr;