forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTRoomDB.cpp
1272 lines (1183 loc) · 56.2 KB
/
TRoomDB.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
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2014-2019 by Stephen Lyons - slysven@virginmedia.com *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "TRoomDB.h"
#include "Host.h"
#include "TArea.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QElapsedTimer>
#include <QRegularExpression>
#include "post_guard.h"
TRoomDB::TRoomDB(TMap* pMap)
: mpMap(pMap)
, mpTempRoomDeletionSet(nullptr)
, mUnnamedAreaName(tr("Unnamed Area"))
, mDefaultAreaName(tr("Default Area"))
{
// Ensure the default area is created, the area/areaName items that get
// created here will get blown away when a map is loaded but that is expected...
addArea(-1, mDefaultAreaName);
}
TRoom* TRoomDB::getRoom(int id)
{
if (id < 0) {
return nullptr;
}
const auto i = rooms.constFind(id);
if (i != rooms.constEnd() && i.key() == id) {
return i.value();
}
return nullptr;
}
bool TRoomDB::addRoom(int id)
{
if (!rooms.contains(id) && id > 0) {
rooms[id] = new TRoom(this);
rooms[id]->setId(id);
// there is no point in updating the entranceMap here, as the room has no exit information
return true;
} else {
if (id <= 0) {
QString error = QStringLiteral("addRoom: illegal room id=%1. roomID must be > 0").arg(id);
mpMap->logError(error);
}
return false;
}
}
bool TRoomDB::addRoom(int id, TRoom* pR, bool isMapLoading)
{
if (!rooms.contains(id) && id > 0 && pR) {
rooms[id] = pR;
pR->setId(id);
updateEntranceMap(pR, isMapLoading);
return true;
} else {
return false;
}
}
void TRoomDB::deleteValuesFromEntranceMap(int value)
{
QList<int> keyList = entranceMap.keys();
QList<int> valueList = entranceMap.values();
QList<uint> deleteEntries;
int index = valueList.indexOf(value);
while (index != -1) {
deleteEntries.append(index);
index = valueList.indexOf(value, index + 1);
}
for (int i = deleteEntries.size() - 1; i >= 0; --i) {
entranceMap.remove(keyList.at(deleteEntries.at(i)), valueList.at(deleteEntries.at(i)));
}
}
void TRoomDB::deleteValuesFromEntranceMap(QSet<int>& valueSet)
{
QElapsedTimer timer;
timer.start();
QList<int> keyList = entranceMap.keys();
QList<int> valueList = entranceMap.values();
QList<uint> deleteEntries;
foreach (int roomId, valueSet) {
int index = valueList.indexOf(roomId);
while (index >= 0) {
deleteEntries.append(index);
index = valueList.indexOf(roomId, index + 1);
}
}
for (unsigned int entry : deleteEntries) {
entranceMap.remove(keyList.at(entry), valueList.at(entry));
}
qDebug() << "TRoomDB::deleteValuesFromEntranceMap() with a list of:" << valueSet.size() << "items, run time:" << timer.nsecsElapsed() * 1.0e-9 << "sec.";
}
void TRoomDB::updateEntranceMap(int id)
{
TRoom* pR = getRoom(id);
updateEntranceMap(pR);
}
void TRoomDB::updateEntranceMap(TRoom* pR, bool isMapLoading)
{
static bool showDebug = false; // Enable this at runtime (set a breakpoint on it) for debugging!
// entranceMap maps the room to rooms it has a viable exit to. So if room b and c both have
// an exit to room a, upon deleting room a we want a map that allows us to find
// room b and c efficiently.
// So we create a mapping like: {room_a: room_b, room_a: room_c}. This allows us to delete
// rooms and know which other rooms are impacted by this change in a single lookup.
if (pR) {
int id = pR->getId();
QHash<int, int> exits = pR->getExits();
QList<int> toExits = exits.keys();
QString values;
// to update this we need to iterate the entire entranceMap and remove invalid
// connections. I'm not sure if this is efficient for every update, and given
// that we check for rooms existance when the map is used, we'll deal with
// possible spurious exits for now.
// entranceMap.remove(id); // <== not what is wanted
// We need to remove all values == id NOT keys == id, so try and do that
// now. SlySven
if (!isMapLoading) {
deleteValuesFromEntranceMap(id); // When LOADING a map, will never need to do this
}
for (int toExit : toExits) {
if (showDebug) {
values.append(QStringLiteral("%1,").arg(toExit));
}
if (!entranceMap.contains(toExit, id)) {
// entranceMap is a QMultiHash, so multiple, identical entries is
// more than possible - it was actually happening and making
// entranceMap get larger than needed...!
entranceMap.insert(toExit, id);
}
}
if (showDebug) {
if (!values.isEmpty()) {
values.chop(1);
}
if (values.isEmpty()) {
qDebug() << "TRoomDB::updateEntranceMap(TRoom * pR) called for room with id:" << id << ", it is not an Entrance for any Rooms.";
} else {
qDebug() << "TRoomDB::updateEntranceMap(TRoom * pR) called for room with id:" << id << ", it is an Entrance for Room(s):" << values << ".";
}
}
}
}
// this is call by TRoom destructor only
bool TRoomDB::__removeRoom(int id)
{
static QMultiHash<int, int> _entranceMap; // Make it persistant - for multiple room deletions
static bool isBulkDelete = false;
// Gets set / reset by mpTempRoomDeletionSet being non-null, used to setup
// _entranceMap the first time around for multi-room deletions
TRoom* pR = getRoom(id);
// This will FAIL during map deletion as TRoomDB::rooms has already been
// zapped, so can use to skip everything...
if (pR) {
if (mpTempRoomDeletionSet && mpTempRoomDeletionSet->size() > 1) { // We are deleting multiple rooms
if (!isBulkDelete) {
_entranceMap = entranceMap;
_entranceMap.detach(); // MUST take a deep copy of the data
isBulkDelete = true; // But only do it the first time for a bulk delete
}
} else { // We are deleting a single room
if (isBulkDelete) { // Last time was a bulk delete but it isn't one now
isBulkDelete = false;
}
_entranceMap.clear();
_entranceMap = entranceMap; // Refresh our local copy
_entranceMap.detach(); // MUST take a deep copy of the data
}
// FIXME: make a proper exit controller so we don't need to do all these if statements
// Remove the links from the rooms entering this room
QMultiHash<int, int>::const_iterator i = _entranceMap.constFind(id);
// The removeAllSpecialExitsToRoom below modifies the entranceMap - and
// it is unsafe to modify (use copy operations on) something that an STL
// iterator is active on - see "Implicit sharing iterator problem" in
// "Container Class | Qt 5.x Core" - this is now avoid by taking a deep
// copy and iterating through that instead whilst modifying the original
while (i != _entranceMap.cend() && i.key() == id) {
if (i.value() == id || (mpTempRoomDeletionSet && mpTempRoomDeletionSet->size() > 1 && mpTempRoomDeletionSet->contains(i.value()))) {
++i;
continue; // Bypass rooms we know are also to be deleted
}
TRoom* r = getRoom(i.value());
if (r) {
if (r->getNorth() == id) {
r->setNorth(-1);
}
if (r->getNortheast() == id) {
r->setNortheast(-1);
}
if (r->getNorthwest() == id) {
r->setNorthwest(-1);
}
if (r->getEast() == id) {
r->setEast(-1);
}
if (r->getWest() == id) {
r->setWest(-1);
}
if (r->getSouth() == id) {
r->setSouth(-1);
}
if (r->getSoutheast() == id) {
r->setSoutheast(-1);
}
if (r->getSouthwest() == id) {
r->setSouthwest(-1);
}
if (r->getUp() == id) {
r->setUp(-1);
}
if (r->getDown() == id) {
r->setDown(-1);
}
if (r->getIn() == id) {
r->setIn(-1);
}
if (r->getOut() == id) {
r->setOut(-1);
}
r->removeAllSpecialExitsToRoom(id);
}
++i;
}
rooms.remove(id);
if (roomIDToHash.contains(id)) {
QString hash = roomIDToHash[id];
roomIDToHash.remove(id);
hashToRoomID.remove(hash);
}
int areaID = pR->getArea();
TArea* pA = getArea(areaID);
if (pA) {
pA->removeRoom(id);
}
if ((!mpTempRoomDeletionSet) || mpTempRoomDeletionSet->size() == 1) { // if NOT deleting multiple rooms
entranceMap.remove(id); // Only removes matching keys
deleteValuesFromEntranceMap(id); // Needed to remove matching values
}
// Because we clear the graph in initGraph which will be called
// if mMapGraphNeedsUpdate is true -- we don't need to
// remove the vertex using clear_vertex and remove_vertex here
mpMap->mMapGraphNeedsUpdate = true;
return true;
}
return false;
}
bool TRoomDB::removeRoom(int id)
{
if (rooms.contains(id) && id > 0) {
if (mpMap->mRoomIdHash.value(mpMap->mProfileName) == id) {
// Now we store mRoomId for each profile, we must remove any where
// this room was used
QList<QString> profilesWithUserInThisRoom = mpMap->mRoomIdHash.keys(id);
foreach (QString key, profilesWithUserInThisRoom) {
mpMap->mRoomIdHash[key] = 0;
}
}
if (mpMap->mTargetID == id) {
mpMap->mTargetID = 0;
}
TRoom* pR = getRoom(id);
delete pR;
return true;
}
return false;
}
void TRoomDB::removeRoom(QSet<int>& ids)
{
QElapsedTimer timer;
timer.start();
QSet<int> deletedRoomIds;
mpTempRoomDeletionSet = &ids; // Will activate "bulk room deletion" code
// When used by TLuaInterpreter::deleteArea()
// via removeArea(int) the list of rooms to
// delete - as suppplied by the reference
// type argument IS NOT CONSTANT - it is
// ALTERED by TArea::removeRoom( int room )
// for each room that is removed
quint64 roomcount = mpTempRoomDeletionSet->size();
while (!mpTempRoomDeletionSet->isEmpty()) {
int deleteRoomId = *(mpTempRoomDeletionSet->constBegin());
TRoom* pR = getRoom(deleteRoomId);
if (pR) {
deletedRoomIds.insert(deleteRoomId);
delete pR;
}
mpTempRoomDeletionSet->remove(deleteRoomId);
}
foreach (int deleteRoomId, deletedRoomIds) {
entranceMap.remove(deleteRoomId); // This has been deferred from __removeRoom()
}
deleteValuesFromEntranceMap(deletedRoomIds);
mpTempRoomDeletionSet->clear();
mpTempRoomDeletionSet = nullptr;
qDebug() << "TRoomDB::removeRoom(QList<int>) run time for" << roomcount << "rooms:" << timer.nsecsElapsed() * 1.0e-9 << "sec.";
}
bool TRoomDB::removeArea(int id)
{
if (TArea* pA = areas.value(id)) {
if (!rooms.isEmpty()) {
// During map deletion rooms will already
// have been cleared so this would not
// be wanted to be done in that case.
removeRoom(pA->rooms);
}
// During map deletion areaNamesMap will
// already have been cleared !!!
areaNamesMap.remove(id);
// This means areas.clear() is not needed during map
// deletion
areas.remove(id);
mpMap->mMapGraphNeedsUpdate = true;
return true;
} else if (areaNamesMap.contains(id)) {
// Handle corner case where the area name was created but not used
areaNamesMap.remove(id);
return true;
}
return false;
}
bool TRoomDB::removeArea(const QString& name)
{
if (areaNamesMap.values().contains(name)) {
return removeArea(areaNamesMap.key(name)); // i.e. call the removeArea(int) method
} else {
return false;
}
}
void TRoomDB::removeArea(TArea* pA)
{
if (!pA) {
qWarning() << "TRoomDB::removeArea(TArea *) Warning - attempt to remove an area with a NULL TArea pointer!";
return;
}
int areaId = areas.key(pA, 0);
if (areaId == areas.key(pA, -1)) {
// By testing twice with different default keys to return if value NOT
// found, we can be certain we have an actual valid value
removeArea(areaId);
} else {
qWarning() << "TRoomDB::removeArea(TArea *) Warning - attempt to remove an area NOT in TRoomDB::areas!";
}
}
int TRoomDB::getAreaID(TArea* pA)
{
return areas.key(pA);
}
void TRoomDB::buildAreas()
{
QElapsedTimer timer;
timer.start();
QHashIterator<int, TRoom*> it(rooms);
while (it.hasNext()) {
it.next();
int id = it.key();
TRoom* pR = getRoom(id);
if (!pR) {
continue;
}
TArea* pA = getArea(pR->getArea());
if (!pA) {
areas[pR->getArea()] = new TArea(mpMap, this);
}
}
// if the area has been created without any rooms add the area ID
QMapIterator<int, QString> it2(areaNamesMap);
while (it2.hasNext()) {
it2.next();
int id = it2.key();
if (!areas.contains(id)) {
areas[id] = new TArea(mpMap, this);
}
}
qDebug() << "TRoomDB::buildAreas() run time:" << timer.nsecsElapsed() * 1.0e-9 << "sec.";
}
const QList<TRoom*> TRoomDB::getRoomPtrList() const
{
return rooms.values();
}
QList<int> TRoomDB::getRoomIDList()
{
return rooms.keys();
}
TArea* TRoomDB::getArea(int id)
{
//area id of -1 is a room in the "void", 0 is a failure
if (id > 0 || id == -1) {
return areas.value(id, nullptr);
} else {
return nullptr;
}
}
// Used by TMap::audit() - can detect and return areas with normally invalids Id (less than -1 or zero)!
TArea* TRoomDB::getRawArea(int id, bool* isValid = nullptr)
{
if (areas.contains(id)) {
if (isValid) {
*isValid = true;
}
return areas.value(id);
} else {
if (isValid) {
*isValid = false;
}
return nullptr;
}
}
bool TRoomDB::setAreaName(int areaID, QString name)
{
if (areaID < 1) {
qWarning() << "TRoomDB::setAreaName((int)areaID, (QString)name): WARNING: Suspect area id:" << areaID << "supplied.";
return false;
}
if (name.isEmpty()) {
qWarning() << "TRoomDB::setAreaName((int)areaID, (QString)name): WARNING: Empty name supplied.";
return false;
} else if (areaNamesMap.values().count(name) > 0) {
// That name is already IN the areaNamesMap
if (areaNamesMap.value(areaID) == name) {
// The trivial case, the given areaID already IS that name
return true;
} else {
qWarning() << "TRoomDB::setAreaName((int)areaID, (QString)name): WARNING: Duplicate name supplied" << name << "- that is not permitted any longer!";
return false;
}
}
areaNamesMap[areaID] = name;
// This creates a NEW area name with given areaID if the ID was not
// previously used - but the TArea only gets created if the user manually
// creates it with TLuaInterpreter::addArea(areaId), OR moves a room to the
// area with either a Lua command or GUI action.
return true;
}
bool TRoomDB::addArea(int id)
{
if (!areas.contains(id)) {
areas[id] = new TArea(mpMap, this);
if (!areaNamesMap.contains(id)) {
// Must provide a name for this new area
QString newAreaName = mUnnamedAreaName;
if (areaNamesMap.values().contains(newAreaName)) {
// We already have an "unnamed area"
uint deduplicateSuffix = 0;
do {
newAreaName = QStringLiteral("%1_%2").arg(mUnnamedAreaName).arg(++deduplicateSuffix, 3, 10, QLatin1Char('0'));
} while (areaNamesMap.values().contains(newAreaName));
}
areaNamesMap.insert(id, newAreaName);
}
return true;
} else {
QString error = tr("Area with ID=%1 already exists!").arg(id);
mpMap->logError(error);
return false;
}
}
int TRoomDB::createNewAreaID()
{
int id = 1;
while (areas.contains(id)) {
id++;
}
return id;
}
int TRoomDB::addArea(QString name)
{
// reject it if area name already exists or is empty
if (name.isEmpty()) {
QString error = tr("An Unnamed Area is (no longer) permitted!");
mpMap->logError(error);
return 0;
} else if (areaNamesMap.values().contains(name)) {
QString error = tr("An area called %1 already exists!").arg(name);
mpMap->logError(error);
return 0;
}
int areaID = createNewAreaID();
if (addArea(areaID)) {
areaNamesMap[areaID] = name;
// This will overwrite the "Unnamed Area_###" that addArea( areaID )
// will generate - but that is fine.
return areaID;
} else {
return 0; //fail
}
}
// this func is called by the xml map importer
// NOTE: we no longer accept duplicate IDs or duplicate area names
// duplicate definitions are ignored
// Unless the area name is empty, in which case we provide one!
bool TRoomDB::addArea(int id, QString name)
{
if (((!name.isEmpty()) && areaNamesMap.values().contains(name)) || areaNamesMap.keys().contains(id)) {
return false;
} else if (addArea(id)) {
// This will generate an "Unnamed Area_###" area name which we should
// overwrite only if we have a name!
if (!name.isEmpty()) {
areaNamesMap[id] = name;
}
return true;
} else {
return false;
}
}
const QList<TArea*> TRoomDB::getAreaPtrList() const
{
return areas.values();
}
QList<int> TRoomDB::getAreaIDList()
{
return areas.keys();
}
/*
* Tasks to perform
* 1) Validate all room Ids and remap any problem ones ( <1 ), retaining
* remappings for fixups later on
* 2) Find all areas that the rooms demand
* 3) Find all areas that the areanames demand
* 4) Validate all existing area Ids and remap any problem one
* 5) Merge in area Ids that rooms and areanames demand, remapping any problem
* ones
* 6) If rooms have been remapped, fixup rooms' exits' to use the new room Ids
* 7) If areas have been remapped, fixup rooms to use new area Ids
* 8) Validate area::rooms - adding or removing roomId based on the rooms' idea
* of the area they should be in - not forgetting any remappings of area/room
* Ids..
* 9) Validate TRoom elements: exits, stubs (beware of duplicate QList<T>
* elements), doors (must have a real or stub in that direction), custom
* lines (no key for an normal or special exit that isn't there), locks
* (beware of duplicate QList<T> elements), room and exit weights (can only
* have a weight on an actual exit).
* 10) Validate TArea elements: zLevels (beware of duplicate QList<T> elements)
*/
void TRoomDB::auditRooms(QHash<int, int>& roomRemapping, QHash<int, int>& areaRemapping)
{
QSet<int> validUsedRoomIds; // Used good ids (>= 1)
QSet<int> validUsedAreaIds; // As rooms
// START OF TASK 1 & 2:
// Scan through all rooms and record their idea of which area they
// should be in. Note invalid room Ids those will be fixed up later...
QHash<int, int> roomAreaHash; // Key: room Id, Value: area it believes it should be in
QMultiHash<int, int> areaRoomMultiHash; // Key: areaId, ValueS: rooms that believe they belong there
{ // Block this code to limit scope of iterator
QMutableHashIterator<int, TRoom*> itRoom(rooms);
while (itRoom.hasNext()) {
itRoom.next();
TRoom* pR = itRoom.value();
if (!pR) {
if (mudlet::self()->showMapAuditErrors()) {
QString warnMsg = tr("[ WARN ] - Problem with data structure associated with room id: %1 - that\n"
"room's data has been lost so the id is now being deleted. This\n"
"suggests serious problems with the currently running version of\n"
"Mudlet - is your system running out of memory?")
.arg(itRoom.key());
mpMap->postMessage(warnMsg);
}
mpMap->appendRoomErrorMsg(itRoom.key(),
tr("[ WARN ] - Problem with data structure associated with this room."
" The room's data has been lost so the id is now being deleted."
" This suggests serious problems with the currently running version of Mudlet"
" - is your system running out of memory?"),
true);
itRoom.remove();
continue;
}
if (itRoom.key() >= 1) {
validUsedRoomIds.insert(itRoom.key());
} else {
roomRemapping.insert(itRoom.key(), itRoom.key());
//Store them for now, will assign new values when we have the
// set of good ones already used
}
int areaId = pR->getArea();
areaRoomMultiHash.insert(areaId, itRoom.key());
roomAreaHash.insert(itRoom.key(), areaId);
}
}
// Check for existance of all areas needed by rooms
QSet<int> areaIdSet = areaRoomMultiHash.keys().toSet();
// START OF TASK 3
// Throw in the area Ids from the areaNamesMap:
areaIdSet.unite(areaNamesMap.keys().toSet());
// And the area Ids used by the map labels:
areaIdSet.unite(mpMap->mapLabels.keys().toSet());
// Check the set of area Ids against the ones we actually have:
QSetIterator<int> itUsedArea(areaIdSet);
// This is a later fix, as I forgot to handle wanted area ids that are +ve
// (and thus valid) but absent:
QList<int> missingAreasNeeded;
while (itUsedArea.hasNext()) {
int usedAreaId = itUsedArea.next();
if (usedAreaId < -1 || !usedAreaId) {
areaRemapping.insert(usedAreaId, usedAreaId); // Will find new value to use later
} else {
validUsedAreaIds.insert(usedAreaId);
}
if (!areas.contains(usedAreaId)) {
if (mudlet::self()->showMapAuditErrors()) {
QString warnMsg = tr("[ ALERT ] - Area with id: %1 expected but not found, will be created.").arg(usedAreaId);
mpMap->postMessage(warnMsg);
}
mpMap->appendAreaErrorMsg(usedAreaId, tr("[ ALERT ] - Area with this id expected but not found, will be created."), true);
missingAreasNeeded.append(usedAreaId);
}
}
// START OF TASK 4
// Check for any problem Id in original areas
QMapIterator<int, TArea*> itArea(areas);
while (itArea.hasNext()) {
itArea.next();
int areaId = itArea.key();
if (areaId < -1 || !areaId) {
areaRemapping.insert(areaId, areaId); // Will find new value to use later
} else {
validUsedAreaIds.insert(areaId);
}
}
// START OF TASK 5.0 (previously omitted) - add in areas that we have an id
// for but no actual TArea, at this point:
// * id WILL be in validUsedAreaIds
// * id WILL NOT be in areaRemapping, but represents a id number that cannot be
// remapped as a new area id
// * id MAY be in TRoomDB::areaNamesMap
// * id MAY be used by rooms which will have to be added to it at some point
// * WILL NOT be in TRoomDB::areas - as that is the source of the error this
// bit of the task being addressed here is fixing
if (!missingAreasNeeded.isEmpty()) {
if (mudlet::self()->showMapAuditErrors()) {
QString alertMsg = tr("[ ALERT ] - %n area(s) detected as missing in map: adding it/them in.\n"
" Look for further messsages related to the rooms that are supposed\n"
" to be in this/these area(s)...",
"Making use of %n to allow quantity dependent message form 8-) !",
missingAreasNeeded.count());
mpMap->postMessage(alertMsg);
}
mpMap->appendErrorMsgWithNoLf(tr("[ ALERT ] - %n area(s) detected as missing in map: adding it/them in.\n"
" Look for further messsages related to the rooms that is/are supposed to\n"
" be in this/these area(s)...",
"Making use of %n to allow quantity dependent message form 8-) !",
missingAreasNeeded.count()),
true);
QString infoMsg;
if (mudlet::self()->showMapAuditErrors()) {
infoMsg = tr("[ INFO ] - The missing area(s) are now called:\n"
"(ID) ==> \"name\"",
"Making use of %n to allow quantity dependent message form 8-) !",
missingAreasNeeded.count());
}
if (missingAreasNeeded.count() > 1) {
// Sort the ids so that the reporting is ordered, which could be
// helpful if there is a large number of faults
std::sort(missingAreasNeeded.begin(), missingAreasNeeded.end());
}
for (int newAreaId : missingAreasNeeded) {
// This will create a new "Default" area name if there is not one
// already for this id - and we do not anticipate that it could ever
// fail and return false...
addArea(newAreaId);
infoMsg.append(QStringLiteral("\n%1 ==> \"%2\"").arg(QString::number(newAreaId), areaNamesMap.value(newAreaId)));
}
// Didn't really needed to be done, but as we have finished with it
// now, clearing it may make tracking the overall processes going on
// in the debugger a little clearer...
missingAreasNeeded.clear();
if (mudlet::self()->showMapAuditErrors()) {
mpMap->postMessage(infoMsg);
}
mpMap->appendErrorMsg(infoMsg, true);
}
// START OF TASK 5.1
// Now process problem areaIds
if (!areaRemapping.isEmpty()) {
if (mudlet::self()->showMapAuditErrors()) {
QString alertMsg = tr("[ ALERT ] - Bad, (less than +1 and not the reserved -1) area ids found (count: %1)\n"
"in map, now working out what new id numbers to use...")
.arg(areaRemapping.count());
mpMap->postMessage(alertMsg);
}
mpMap->appendErrorMsg(tr("[ ALERT ] - Bad, (less than +1 and not the reserved -1) area ids found (count: %1) in map!"
" Look for further messsages related to this for each affected area ...")
.arg(areaRemapping.count()),
true);
QString infoMsg;
if (mudlet::self()->showMapAuditErrors()) {
infoMsg = tr("[ INFO ] - The renumbered area ids will be:\n"
"Old ==> New");
}
QMutableHashIterator<int, int> itRemappedArea(areaRemapping);
while (itRemappedArea.hasNext()) {
itRemappedArea.next();
int faultyAreaId = itRemappedArea.key();
int replacementAreaId = 0;
do {
; // No-op, increment done in test
} while (areas.contains(++replacementAreaId));
// Insert replacement value into hash
itRemappedArea.setValue(replacementAreaId);
if (mudlet::self()->showMapAuditErrors()) {
infoMsg.append(QStringLiteral("\n%1 ==> %2").arg(QString::number(faultyAreaId), QString::number(replacementAreaId)));
}
mpMap->appendAreaErrorMsg(faultyAreaId, tr("[ INFO ] - The area with this bad id was renumbered to: %1.").arg(replacementAreaId), true);
mpMap->appendAreaErrorMsg(replacementAreaId, tr("[ INFO ] - This area was renumbered from the bad id: %1.").arg(faultyAreaId), true);
TArea* pA = nullptr;
if (areas.contains(faultyAreaId)) {
pA = areas.take(faultyAreaId);
} else {
pA = new TArea(mpMap, this);
}
if (areaNamesMap.contains(faultyAreaId)) {
QString areaName = areaNamesMap.value(faultyAreaId);
areaNamesMap.remove(faultyAreaId);
areaNamesMap.insert(replacementAreaId, areaName);
} else {
// I think this is unlikely but better provide code to cover it
// if it does arise that we need a new area but do not have a
// provided name
QString newAreaName = mUnnamedAreaName;
if (areaNamesMap.values().contains(newAreaName)) {
// We already have an "unnamed area"
uint deduplicateSuffix = 0;
do {
newAreaName = QStringLiteral("%1_%2").arg(mUnnamedAreaName).arg(++deduplicateSuffix, 3, 10, QLatin1Char('0'));
} while (areaNamesMap.values().contains(newAreaName));
}
areaNamesMap.insert(replacementAreaId, newAreaName);
}
pA->mUserData.insert(QStringLiteral("audit.remapped_id"), QString::number(faultyAreaId));
validUsedAreaIds.insert(replacementAreaId);
areas.insert(replacementAreaId, pA);
// Fixup map labels as well
if (mpMap->mapLabels.contains(faultyAreaId)) {
QMap<qint32, TMapLabel> areaMapLabels = mpMap->mapLabels.take(faultyAreaId);
mpMap->mapLabels.insert(replacementAreaId, areaMapLabels);
}
pA->mIsDirty = true;
}
if (mudlet::self()->showMapAuditErrors()) {
mpMap->postMessage(infoMsg);
}
} else {
if (mudlet::self()->showMapAuditErrors()) {
QString infoMsg = tr("[ INFO ] - Area id numbering is satisfactory.");
mpMap->postMessage(infoMsg);
}
mpMap->appendErrorMsg(tr("[ INFO ] - Area id numbering is satisfactory."), false);
}
// END OF TASK 2,3,4,5 - all needed areas exist and remap details are in
// areaRemapping - still need to update rooms and areaNames and mapLabels
// Now complete TASK 1 - find the new room Ids to use
if (!roomRemapping.isEmpty()) {
if (mudlet::self()->showMapAuditErrors()) {
QString alertMsg = tr("[ ALERT ] - Bad, (less than +1) room ids found (count: %1) in map, now working\n"
"out what new id numbers to use.")
.arg(roomRemapping.count());
mpMap->postMessage(alertMsg);
}
mpMap->appendErrorMsg(tr("[ ALERT ] - Bad, (less than +1) room ids found (count: %1) in map!"
" Look for further messsages related to this for each affected room ...")
.arg(roomRemapping.count()),
true);
QString infoMsg;
if (mudlet::self()->showMapAuditErrors()) {
infoMsg = tr("[ INFO ] - The renumbered rooms will be:\n");
}
QMutableHashIterator<int, int> itRenumberedRoomId(roomRemapping);
while (itRenumberedRoomId.hasNext()) {
itRenumberedRoomId.next();
unsigned int newRoomId = 0;
do {
; // Noop - needed increment is done in test condition!
} while (validUsedRoomIds.contains(++newRoomId));
itRenumberedRoomId.setValue(newRoomId); // Update the QHash
validUsedRoomIds.insert(newRoomId);
if (mudlet::self()->showMapAuditErrors()) {
infoMsg.append(QStringLiteral("%1 ==> %2").arg(QString::number(itRenumberedRoomId.key()), QString::number(itRenumberedRoomId.value())));
}
mpMap->appendRoomErrorMsg(itRenumberedRoomId.key(), tr("[ INFO ] - This room with the bad id was renumbered to: %1.").arg(itRenumberedRoomId.value()), true);
mpMap->appendRoomErrorMsg(itRenumberedRoomId.value(), tr("[ INFO ] - This room was renumbered from the bad id: %1.").arg(itRenumberedRoomId.key()), true);
}
if (mudlet::self()->showMapAuditErrors()) {
mpMap->postMessage(infoMsg);
}
QSet<TRoom*> holdingSet;
{ // Block this code to limit scope of iterator
QMutableHashIterator<int, TRoom*> itRoom(rooms);
// Although this is "mutable" we can only changed the VALUE not the KEY
// Also we cannot directly change the collection being iterated over -
// though we can REMOVE items via the iterator - so pull the
// affected TRoom instances out of the Hash, renumber them and then
// hold onto them before reinserting them after we have finished
// this iteration
while (itRoom.hasNext()) {
itRoom.next();
TRoom* pR = itRoom.value();
if (roomRemapping.contains(itRoom.key())) {
pR->userData.insert(QStringLiteral("audit.remapped_id"), QString::number(itRoom.key()));
pR->setId(roomRemapping.value(itRoom.key()));
itRoom.remove();
holdingSet.insert(pR);
}
}
}
// Now stuff the modified values back in under the new key values
QSetIterator<TRoom*> itModifiedRoom(holdingSet);
while (itModifiedRoom.hasNext()) {
TRoom* pR = itModifiedRoom.next();
int newRoomId = pR->getId();
rooms.insert(newRoomId, pR);
}
} else {
if (mudlet::self()->showMapAuditErrors()) {
QString infoMsg = tr("[ INFO ] - Room id numbering is satisfactory.");
mpMap->postMessage(infoMsg);
}
mpMap->appendErrorMsg(tr("[ INFO ] - Room id numbering is satisfactory."), false);
}
// END OF TASK 1
// START OF TASK 6,7 & 9
{ // Block the following code to limit the scope of the itRoom iterator
QMutableHashIterator<int, TRoom*> itRoom(rooms);
while (itRoom.hasNext()) {
itRoom.next();
TRoom* pR = itRoom.value();
// Purges any duplicates that a QList structure DOES permit, but a QSet does NOT:
// Exit stubs:
unsigned int _listCount = pR->exitStubs.count();
QSet<int> _set = pR->exitStubs.toSet();
if (_set.count() < _listCount) {
if (mudlet::self()->showMapAuditErrors()) {
QString infoMsg = tr("[ INFO ] - Duplicate exit stub identifiers found in room id: %1, this is an\n"
"anomaly but has been cleaned up easily.")
.arg(itRoom.key());
mpMap->postMessage(infoMsg);
}
mpMap->appendRoomErrorMsg(itRoom.key(), tr("[ INFO ] - Duplicate exit stub identifiers found in room, this is an anomaly but has been cleaned up easily."), false);
}
pR->exitStubs = _set.toList();
// Exit locks:
_listCount = pR->exitLocks.count();
_set = pR->exitLocks.toSet();
if (_set.count() < _listCount) {
if (mudlet::self()->showMapAuditErrors()) {
QString infoMsg = tr("[ INFO ] - Duplicate exit lock identifiers found in room id: %1, this is an\n"
"anomaly but has been cleaned up easily.")
.arg(itRoom.key());
mpMap->postMessage(infoMsg);
}
mpMap->appendRoomErrorMsg(itRoom.key(), tr("[ INFO ] - Duplicate exit lock identifiers found in room, this is an anomaly but has been cleaned up easily."), false);
}
pR->exitLocks = _set.toList();
// TASK 9 IS DONE INSIDE THIS METHOD:
pR->audit(roomRemapping, areaRemapping);
}
}
// END OF TASK 6,7 & 9
// START TASK 8
{
QMapIterator<int, TArea*> itArea(areas);
while (itArea.hasNext()) {
itArea.next();
TArea* pA = itArea.value();
QSet<int> replacementRoomsSet;
{ // Block code to limit scope of iterator, find and pull out renumbered rooms
QMutableSetIterator<int> itAreaRoom(pA->rooms);
if (!roomRemapping.isEmpty()) {
while (itAreaRoom.hasNext()) {
int originalRoomId = itAreaRoom.next();
if (roomRemapping.contains(originalRoomId)) {
itAreaRoom.remove();
replacementRoomsSet.insert(roomRemapping.value(originalRoomId));
}
}
}
}
// Merge back in the renumbered rooms
if (!replacementRoomsSet.isEmpty()) {
pA->rooms.unite(replacementRoomsSet);
}
// Now compare pA->rooms to areaRoomMultiHash.values( itArea.key() )
QSet<int> foundRooms = areaRoomMultiHash.values(itArea.key()).toSet();
QSetIterator<int> itFoundRoom(foundRooms);
// Original form of code which was slower because the two sets of rooms were
// compared TWICE:
// extraRooms = pA->rooms;
// extraRooms.subtract( foundRooms );
// missingRooms = foundRooms;
// missingRooms.subtract( pA->rooms );
// Revised code that only makes one pass
QSet<int> extraRooms(pA->rooms); //Take common rooms from here so that any left are the "extras"
extraRooms.detach(); // We'll need a deep copy so might as well explicitly say so to make the copy
QSet<int> missingRooms; // Add rooms not in pA->room to here
int checkRoom;
while (itFoundRoom.hasNext()) {
checkRoom = itFoundRoom.next();
if (pA->rooms.contains(checkRoom)) {
extraRooms.remove(checkRoom);
} else {
missingRooms.insert(checkRoom);
}
}
// Report differences:
if (!missingRooms.isEmpty()) {
QStringList roomList;
QList<int> missingRoomsList = missingRooms.toList();
if (missingRoomsList.size() > 1) {
// The on-screen listing are clearer if we sort the rooms