-
Notifications
You must be signed in to change notification settings - Fork 224
/
map.cpp
684 lines (568 loc) · 16.9 KB
/
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
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor 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.
//
// Remere's Map Editor 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 this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "gui.h" // loadbar
#include "map.h"
#include <sstream>
Map::Map() : BaseMap(),
width(512),
height(512),
houses(*this),
has_changed(false),
unnamed(false),
waypoints(*this)
{
// Earliest version possible
// Caller is responsible for converting us to proper version
mapVersion.otbm = MAP_OTBM_1;
mapVersion.client = CLIENT_VERSION_NONE;
}
Map::~Map()
{
////
}
bool Map::open(const std::string file)
{
if(file == filename)
return true; // Do not reopen ourselves!
tilecount = 0;
IOMapOTBM maploader(getVersion());
bool success = maploader.loadMap(*this, wxstr(file));
mapVersion = maploader.version;
warnings = maploader.getWarnings();
if(!success) {
error = maploader.getError();
return false;
}
has_changed = false;
wxFileName fn = wxstr(file);
filename = fn.GetFullPath().mb_str(wxConvUTF8);
name = fn.GetFullName().mb_str(wxConvUTF8);
// convert(getReplacementMapClassic(), true);
#if 0 // This will create a replacement map out of one of SO's template files
std::ofstream out("templateshit.cpp");
out << "\tConversionMap replacement_map;\n";
int count = 0;
out << "\tstd::vector<uint16_t> veckey, vecval;\n\n";
for(int x = 20; ; x += 2) {
int y = 22;
Tile* old = getTile(x, y, rme::MapGroundLayer);
if(old) {
y -= 2;
Tile* new_ = getTile(x, y, rme::MapGroundLayer);
if(new_) {
if(old->ground || old->items.size()) {
out << "\tvecval.clear();\n";
if(new_->ground)
out << "\tvecval.push_back(" << new_->ground->getID() << ");\n";
for(ItemVector::iterator iter = new_->items.begin(); iter != new_->items.end(); ++iter)
out << "\tvecval.push_back(" << (*iter)->getID() << ");\n";
if(old->ground && old->items.empty()) // Single item
out << "\treplacement_map.stm[" << old->ground->getID() << "] = vecval;\n\n";
else if(old->ground == nullptr && old->items.size() == 1) // Single item
out << "\treplacement_map.stm[" << old->items.front()->getID() << "] = vecval;\n\n";
else {
// Many items
out << "\tveckey.clear();\n";
if(old->ground)
out << "\tveckey.push_back(" << old->ground->getID() << ");\n";
for(ItemVector::iterator iter = old->items.begin(); iter != old->items.end(); ++iter)
out << "\tveckey.push_back(" << (*iter)->getID() << ");\n";
out << "\tstd::sort(veckey.begin(), veckey.end());\n";
out << "\treplacement_map.mtm[veckey] = vecval;\n\n";
}
}
}
} else {
break;
}
}
out.close();
#endif
return true;
}
bool Map::convert(MapVersion to, bool showdialog)
{
if(mapVersion.client == to.client) {
// Only OTBM version differs
// No changes necessary
mapVersion = to;
return true;
}
/* TODO
if(to.otbm == MAP_OTBM_4 && to.client < CLIENT_VERSION_850)
return false;
if(mapVersion.client >= CLIENT_VERSION_760 && to.client < CLIENT_VERSION_760)
convert(getReplacementMapFrom760To740(), showdialog);
if(mapVersion.client < CLIENT_VERSION_810 && to.client >= CLIENT_VERSION_810)
convert(getReplacementMapFrom800To810(), showdialog);
if(mapVersion.client == CLIENT_VERSION_854_BAD && to.client >= CLIENT_VERSION_854)
convert(getReplacementMapFrom854To854(), showdialog);
*/
mapVersion = to;
return true;
}
bool Map::convert(const ConversionMap& rm, bool showdialog)
{
if(showdialog)
g_gui.CreateLoadBar("Converting map ...");
uint64_t tiles_done = 0;
std::vector<uint16_t> id_list;
//std::ofstream conversions("converted_items.txt");
for(MapIterator miter = begin(); miter != end(); ++miter) {
Tile* tile = (*miter)->get();
ASSERT(tile);
if(tile->size() == 0)
continue;
// id_list try MTM conversion
id_list.clear();
if(tile->ground)
id_list.push_back(tile->ground->getID());
for(ItemVector::const_iterator item_iter = tile->items.begin(); item_iter != tile->items.end(); ++item_iter)
if((*item_iter)->isBorder())
id_list.push_back((*item_iter)->getID());
std::sort(id_list.begin(), id_list.end());
ConversionMap::MTM::const_iterator cfmtm = rm.mtm.end();
while(id_list.size()) {
cfmtm = rm.mtm.find(id_list);
if(cfmtm != rm.mtm.end())
break;
id_list.pop_back();
}
// Keep track of how many items have been inserted at the bottom
size_t inserted_items = 0;
if(cfmtm != rm.mtm.end()) {
const std::vector<uint16_t>& v = cfmtm->first;
if(tile->ground && std::find(v.begin(), v.end(), tile->ground->getID()) != v.end()) {
delete tile->ground;
tile->ground = nullptr;
}
for(ItemVector::iterator item_iter = tile->items.begin(); item_iter != tile->items.end(); ) {
if(std::find(v.begin(), v.end(), (*item_iter)->getID()) != v.end()) {
delete *item_iter;
item_iter = tile->items.erase(item_iter);
}
else
++item_iter;
}
const std::vector<uint16_t>& new_items = cfmtm->second;
for(std::vector<uint16_t>::const_iterator iit = new_items.begin(); iit != new_items.end(); ++iit) {
Item* item = Item::Create(*iit);
if(item->isGroundTile())
tile->ground = item;
else {
tile->items.insert(tile->items.begin(), item);
++inserted_items;
}
}
}
if(tile->ground) {
ConversionMap::STM::const_iterator cfstm = rm.stm.find(tile->ground->getID());
if(cfstm != rm.stm.end()) {
uint16_t aid = tile->ground->getActionID();
uint16_t uid = tile->ground->getUniqueID();
delete tile->ground;
tile->ground = nullptr;
const std::vector<uint16_t>& v = cfstm->second;
//conversions << "Converted " << tile->getX() << ":" << tile->getY() << ":" << tile->getZ() << " " << id << " -> ";
for(std::vector<uint16_t>::const_iterator iit = v.begin(); iit != v.end(); ++iit) {
Item* item = Item::Create(*iit);
//conversions << *iit << " ";
if(item->isGroundTile()) {
item->setActionID(aid);
item->setUniqueID(uid);
tile->addItem(item);
} else {
tile->items.insert(tile->items.begin(), item);
++inserted_items;
}
}
//conversions << std::endl;
}
}
for(ItemVector::iterator replace_item_iter = tile->items.begin() + inserted_items; replace_item_iter != tile->items.end(); ) {
uint16_t id = (*replace_item_iter)->getID();
ConversionMap::STM::const_iterator cf = rm.stm.find(id);
if(cf != rm.stm.end()) {
//uint16_t aid = (*replace_item_iter)->getActionID();
//uint16_t uid = (*replace_item_iter)->getUniqueID();
delete *replace_item_iter;
replace_item_iter = tile->items.erase(replace_item_iter);
const std::vector<uint16_t>& v = cf->second;
for(std::vector<uint16_t>::const_iterator iit = v.begin(); iit != v.end(); ++iit) {
replace_item_iter = tile->items.insert(replace_item_iter, Item::Create(*iit));
//conversions << "Converted " << tile->getX() << ":" << tile->getY() << ":" << tile->getZ() << " " << id << " -> " << *iit << std::endl;
++replace_item_iter;
}
}
else
++replace_item_iter;
}
++tiles_done;
if(showdialog && tiles_done % 0x10000 == 0) {
g_gui.SetLoadDone(int(tiles_done / double(getTileCount()) * 100.0));
}
}
if(showdialog)
g_gui.DestroyLoadBar();
return true;
}
void Map::cleanInvalidTiles(bool showdialog)
{
if(showdialog)
g_gui.CreateLoadBar("Removing invalid tiles...");
uint64_t tiles_done = 0;
for(MapIterator miter = begin(); miter != end(); ++miter) {
Tile* tile = (*miter)->get();
ASSERT(tile);
if(tile->size() == 0)
continue;
for(ItemVector::iterator item_iter = tile->items.begin(); item_iter != tile->items.end();) {
if(g_items.isValidID((*item_iter)->getID()))
++item_iter;
else {
delete *item_iter;
item_iter = tile->items.erase(item_iter);
}
}
++tiles_done;
if(showdialog && tiles_done % 0x10000 == 0) {
g_gui.SetLoadDone(int(tiles_done / double(getTileCount()) * 100.0));
}
}
if(showdialog)
g_gui.DestroyLoadBar();
}
bool Map::doChange()
{
bool doupdate = !has_changed;
has_changed = true;
return doupdate;
}
bool Map::clearChanges()
{
bool doupdate = has_changed;
has_changed = false;
return doupdate;
}
void Map::setWidth(int new_width)
{
if(new_width > 65000)
width = 65000;
else if(new_width < 64)
width = 64;
else
width = new_width;
}
void Map::setHeight(int new_height)
{
if(new_height > 65000)
height = 65000;
else if(new_height < 64)
height = 64;
else
height = new_height;
}
void Map::setMapDescription(const std::string& new_description)
{
description = new_description;
}
void Map::setHouseFilename(const std::string& new_housefile)
{
housefile = new_housefile;
unnamed = false;
}
void Map::setSpawnFilename(const std::string& new_spawnfile)
{
spawnfile = new_spawnfile;
unnamed = false;
}
bool Map::addSpawn(Tile* tile)
{
Spawn* spawn = tile->spawn;
if(spawn) {
int z = tile->getZ();
int start_x = tile->getX() - spawn->getSize();
int start_y = tile->getY() - spawn->getSize();
int end_x = tile->getX() + spawn->getSize();
int end_y = tile->getY() + spawn->getSize();
for(int y = start_y; y <= end_y; ++y) {
for(int x = start_x; x <= end_x; ++x) {
TileLocation* ctile_loc = createTileL(x, y, z);
ctile_loc->increaseSpawnCount();
}
}
spawns.addSpawn(tile);
return true;
}
return false;
}
void Map::removeSpawnInternal(Tile* tile)
{
Spawn* spawn = tile->spawn;
ASSERT(spawn);
int z = tile->getZ();
int start_x = tile->getX() - spawn->getSize();
int start_y = tile->getY() - spawn->getSize();
int end_x = tile->getX() + spawn->getSize();
int end_y = tile->getY() + spawn->getSize();
for(int y = start_y; y <= end_y; ++y) {
for(int x = start_x; x <= end_x; ++x) {
TileLocation* ctile_loc = getTileL(x, y, z);
if(ctile_loc != nullptr && ctile_loc->getSpawnCount() > 0)
ctile_loc->decreaseSpawnCount();
}
}
}
void Map::removeSpawn(Tile* tile)
{
if(tile->spawn) {
removeSpawnInternal(tile);
spawns.removeSpawn(tile);
}
}
SpawnList Map::getSpawnList(const Tile* tile) const
{
SpawnList list;
if(!tile) return list;
const TileLocation* location = tile->getLocation();
if(!location || location->getSpawnCount() == 0)
return list;
uint32_t found = 0;
if(tile->spawn) {
++found;
list.push_back(tile->spawn);
}
// Scans the border tiles in an expanding square around the original spawn
const Position& position = tile->getPosition();
int start_x = position.x - 1;
int end_x = position.x + 1;
int start_y = position.y - 1;
int end_y = position.y + 1;
while(found != location->getSpawnCount()) {
for(int x = start_x; x <= end_x; ++x) {
const Tile* start_tile = getTile(x, start_y, position.z);
if(start_tile && start_tile->spawn) {
list.push_back(start_tile->spawn);
++found;
}
const Tile* end_tile = getTile(x, end_y, position.z);
if(end_tile && end_tile->spawn) {
list.push_back(end_tile->spawn);
++found;
}
}
for(int y = start_y + 1; y < end_y; ++y) {
const Tile* start_tile = getTile(start_x, y, position.z);
if(start_tile && start_tile->spawn) {
list.push_back(start_tile->spawn);
++found;
}
const Tile* end_tile = getTile(end_x, y, position.z);
if(end_tile && end_tile->spawn) {
list.push_back(end_tile->spawn);
++found;
}
}
--start_x;
--start_y;
++end_x;
++end_y;
}
return list;
}
SpawnList Map::getSpawnList(const Position& position) const
{
const Tile* tile = getTile(position);
return getSpawnList(tile);
}
SpawnList Map::getSpawnList(int x, int y, int z) const
{
const Tile* tile = getTile(x, y, z);
return getSpawnList(tile);
}
bool Map::exportMinimap(FileName filename, int floor /*= rme::MapGroundLayer*/, bool displaydialog)
{
uint8_t* pic = nullptr;
try
{
int min_x = 0x10000, min_y = 0x10000;
int max_x = 0x00000, max_y = 0x00000;
if(size() == 0)
return true;
uint32_t minimap_colors[256];
for(int i = 0; i < 256; ++i)
minimap_colors[i] = colorFromEightBit(i).GetRGB();
for(MapIterator mit = begin(); mit != end(); ++mit) {
if((*mit)->get() == nullptr || (*mit)->empty())
continue;
Position pos = (*mit)->getPosition();
if(pos.x < min_x)
min_x = pos.x;
if(pos.y < min_y)
min_y = pos.y;
if(pos.x > max_x)
max_x = pos.x;
if(pos.y > max_y)
max_y = pos.y;
}
int minimap_width = max_x - min_x+1;
int minimap_height = max_y - min_y+1;
pic = newd uint8_t[minimap_width*minimap_height]; // 1 byte per pixel
memset(pic, 0, minimap_width*minimap_height);
int tiles_iterated = 0;
for(MapIterator mit = begin(); mit != end(); ++mit) {
Tile* tile = (*mit)->get();
++tiles_iterated;
if(tiles_iterated % 8192 == 0 && displaydialog)
g_gui.SetLoadDone(int(tiles_iterated / double(tilecount) * 90.0));
if(tile->empty() || tile->getZ() != floor)
continue;
//std::cout << "Pixel : " << (tile->getY() - min_y) * width + (tile->getX() - min_x) << std::endl;
uint32_t pixelpos = (tile->getY() - min_y) * minimap_width + (tile->getX() - min_x);
uint8_t& pixel = pic[pixelpos];
for(ItemVector::const_reverse_iterator item_iter = tile->items.rbegin(); item_iter != tile->items.rend(); ++item_iter) {
if((*item_iter)->getMiniMapColor()) {
pixel = (*item_iter)->getMiniMapColor();
break;
}
}
if(pixel == 0)
// check ground too
if(tile->hasGround())
pixel = tile->ground->getMiniMapColor();
}
// Create a file for writing
FileWriteHandle fh(nstr(filename.GetFullPath()));
if(!fh.isOpen()) {
delete[] pic;
return false;
}
// Store the magic number
fh.addRAW("BM");
// Store the file size
// We need to predict how large it will be
uint32_t file_size =
14 // header
+40 // image data header
+256*4 // color palette
+((minimap_width + 3) / 4 * 4) * height; // pixels
fh.addU32(file_size);
// Two values reserved, must always be 0.
fh.addU16(0);
fh.addU16(0);
// Bitmapdata offset
fh.addU32(14 + 40 + 256*4);
// Header size
fh.addU32(40);
// Header width/height
fh.addU32(minimap_width);
fh.addU32(minimap_height);
// Color planes
fh.addU16(1);
// bits per pixel, OT map format is 8
fh.addU16(8);
// compression type, 0 is no compression
fh.addU32(0);
// image size, 0 is valid if we use no compression
fh.addU32(0);
// horizontal/vertical resolution in pixels / meter
fh.addU32(4000);
fh.addU32(4000);
// Number of colors
fh.addU32(256);
// Important colors, 0 is all
fh.addU32(0);
// Write the color palette
for(int i = 0; i < 256; ++i)
fh.addU32(minimap_colors[i]);
// Bitmap width must be divisible by four, calculate how much padding we need
int padding = ((minimap_width & 3) != 0? 4-(minimap_width & 3) : 0);
// Bitmap rows are saved in reverse order
for(int y = minimap_height-1; y >= 0; --y) {
fh.addRAW(pic + y*minimap_width, minimap_width);
for(int i = 0; i < padding; ++i) {
fh.addU8(0);
}
if(y % 100 == 0 && displaydialog) {
g_gui.SetLoadDone(90 + int((minimap_height-y) / double(minimap_height) * 10.0));
}
}
delete[] pic;
//fclose(file);
fh.close();
}
catch(...)
{
delete[] pic;
}
return true;
}
void Map::updateUniqueIds(Tile* old_tile, Tile* new_tile)
{
if(old_tile && old_tile->hasUniqueItem()) {
if(old_tile->ground) {
uint16_t uid = old_tile->ground->getUniqueID();
if(uid != 0)
removeUniqueId(uid);
}
for(const Item* item : old_tile->items) {
if(item) {
uint16_t uid = item->getUniqueID();
if(uid != 0) {
removeUniqueId(uid);
}
}
}
}
if(new_tile && new_tile->hasUniqueItem()) {
if(new_tile->ground) {
uint16_t uid = new_tile->ground->getUniqueID();
if(uid != 0)
addUniqueId(uid);
}
for(const Item* item : new_tile->items) {
if(item) {
uint16_t uid = item->getUniqueID();
if(uid != 0) {
addUniqueId(uid);
}
}
}
}
}
void Map::addUniqueId(uint16_t uid)
{
auto it = std::find(uniqueIds.begin(), uniqueIds.end(), uid);
if (it == uniqueIds.end()) {
uniqueIds.push_back(uid);
}
}
void Map::removeUniqueId(uint16_t uid)
{
auto it = std::find(uniqueIds.begin(), uniqueIds.end(), uid);
if (it != uniqueIds.end()) {
uniqueIds.erase(it);
}
}
bool Map::hasUniqueId(uint16_t uid) const
{
if (uid < rme::MinUniqueId || uniqueIds.empty())
return false;
auto it = std::find(uniqueIds.begin(), uniqueIds.end(), uid);
return it != uniqueIds.end();
}