forked from thoth-medievia/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRoom.cpp
1725 lines (1614 loc) · 68 KB
/
TRoom.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) 2012-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2014-2016, 2018 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 "TRoom.h"
#include "TArea.h"
#include "TRoomDB.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QString>
#include <QStringBuilder>
#include <QRegularExpression>
#include "post_guard.h"
// Helper needed to allow Qt::PenStyle enum to be unserialised (read from file)
// in Qt5 - the compilation errors that result in not having this are really
// confusing!
QDataStream &operator>>(QDataStream& ds, Qt::PenStyle& value)
{
int temporary;
ds >> temporary;
switch(temporary) {
case Qt::DotLine:
[[fallthrough]];
case Qt::DashLine:
[[fallthrough]];
case Qt::DashDotLine:
[[fallthrough]];
case Qt::DashDotDotLine:
value = static_cast<Qt::PenStyle>(temporary);
break;
case Qt::SolidLine:
[[fallthrough]];
default:
// Force anything else to be a solidline
value = Qt::SolidLine;
}
return ds;
}
TRoom::TRoom(TRoomDB* pRDB)
: x(0)
, y(0)
, z(0)
, environment(-1)
, isLocked(false)
, min_x(0)
, min_y(0)
, max_x(0)
, max_y(0)
, mSymbol(QString())
, highlight(false)
, highlightColor(QColor(255, 150, 0))
, rendered(false)
, id(0)
, area(-1)
, weight(1)
, north(-1)
, northeast(-1)
, east(-1)
, southeast(-1)
, south(-1)
, southwest(-1)
, west(-1)
, northwest(-1)
, up(-1)
, down(-1)
, in(-1)
, out(-1)
, mpRoomDB(pRDB)
, highlightRadius()
{
}
TRoom::~TRoom()
{
if (mpRoomDB) {
mpRoomDB->__removeRoom(id);
}
}
const QString TRoom::dirCodeToDisplayName(const int dirCode)
{
switch (dirCode) {
case DIR_NORTH: return tr("North"); break;
case DIR_NORTHEAST: return tr("North-east"); break;
case DIR_NORTHWEST: return tr("North-west"); break;
case DIR_SOUTH: return tr("South"); break;
case DIR_SOUTHEAST: return tr("South-east"); break;
case DIR_SOUTHWEST: return tr("South-west"); break;
case DIR_EAST: return tr("East"); break;
case DIR_WEST: return tr("West"); break;
case DIR_UP: return tr("Up"); break;
case DIR_DOWN: return tr("Down"); break;
case DIR_IN: return tr("In"); break;
case DIR_OUT: return tr("Out"); break;
case DIR_OTHER: return tr("Other"); break;
default:
return tr("Unknown");
}
}
bool TRoom::hasExitStub(int direction)
{
if (exitStubs.contains(direction)) {
return true;
} else {
return false;
}
}
void TRoom::setExitStub(int direction, bool status)
{
if (status) {
if (!hasExit(direction)) {
if (!exitStubs.contains(direction)) {
// Previous code did not check for an existing entry for given
// direction and QList container permits duplicates of same value!
exitStubs.append(direction);
}
} else {
QString error = QString("Set exit stub in given direction in RoomID(%1) - there is already an exit there!").arg(id);
mpRoomDB->mpMap->logError(error);
}
} else {
exitStubs.removeAll(direction);
}
}
int TRoom::getExitWeight(const QString& cmd)
{
if (exitWeights.contains(cmd)) {
return exitWeights[cmd];
} else {
return weight; // NOTE: if no exit weight has been set: exit weight = room weight
}
}
// NOTE: needed so dialogRoomExit code can tell if an exit weight has been set
// now that they are private!
bool TRoom::hasExitWeight(const QString& cmd)
{
if (exitWeights.contains(cmd)) {
if (exitWeights.value(cmd) > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
void TRoom::setWeight(int w)
{
if (w < 1) {
w = 1;
}
weight = w;
}
// Previous implementations did not allow for REMOVAL of an exit weight (by
// setting it to zero)
void TRoom::setExitWeight(const QString& cmd, int w)
{
if (w > 0) {
exitWeights[cmd] = w;
if (mpRoomDB && mpRoomDB->mpMap) {
mpRoomDB->mpMap->mMapGraphNeedsUpdate = true;
}
} else if (exitWeights.contains(cmd)) {
exitWeights.remove(cmd);
if (mpRoomDB && mpRoomDB->mpMap) {
mpRoomDB->mpMap->mMapGraphNeedsUpdate = true;
}
}
}
// Uses lower case initials: n,ne,e,se,s,sw,w,nw
//
// also: up, down, in, out or any unprefixed special exit command
// all of which can be stored but aren't (yet?) showable on the 2D mapper
bool TRoom::setDoor(const QString& cmd, const int doorStatus)
{
if (doorStatus > 0 && doorStatus <= 3) {
if (doors.value(cmd, 0) != doorStatus) {
// .value will return 0 if there ISN'T a door for this cmd
doors[cmd] = doorStatus;
return true; // As we have changed things
} else {
return false; // Valid but ineffective
}
} else if (doors.contains(cmd) && !doorStatus) {
doors.remove(cmd);
return true; // As we have changed things
} else {
return false; // As we have not changed things
}
}
int TRoom::getDoor(const QString& cmd)
{
return doors.value(cmd, 0);
// Second argument is the result if cmd is not in the doors QMap
}
void TRoom::setId(int _id)
{
id = _id;
}
// The second optional argument delays area related recaluclations when true
// until called with false (the default) - it records the "dirty" areas so that
// the affected areas can be identified.
// The caller, should set the argument true for all but the last when working
// through a list of rooms.
// There IS a theoretical risk that if the last called room "doesn't exist" then
// the area related recalculations won't get done - so had better provide an
// alternative means to do them as a fault recovery
bool TRoom::setArea(int areaID, bool isToDeferAreaRelatedRecalculations)
{
static QSet<TArea*> dirtyAreas;
TArea* pA = mpRoomDB->getArea(areaID);
if (!pA) {
// There is no TArea instance with that _areaID
// So try and make it
mpRoomDB->addArea(areaID);
pA = mpRoomDB->getArea(areaID);
if (!pA) { // Oh, dear THAT didn't work
QString error = qApp->translate("TRoom", "No area created! Requested area ID=%1. Note: Area IDs must be > 0").arg(areaID);
mpRoomDB->mpMap->logError(error);
return false;
}
}
//remove from the old area
TArea* pA2 = mpRoomDB->getArea(area);
if (pA2) {
pA2->removeRoom(id, isToDeferAreaRelatedRecalculations);
// Ah, all rooms in the OLD area that led to the room now become area
// exits for that OLD area {so must run determineAreaExits() for the
// old area after the room has moved to the new area see other
// "if( pA2 )" below} - other exits that led to the room from other
// areas are still "out of area exits" UNLESS the room moves to the SAME
// area that the other exits are in.
// Add to local store of dirty areas
dirtyAreas.insert(pA2);
// Flag the area itself in case something goes
// wrong on last room in a series
pA2->mIsDirty = true;
} else {
QString error = qApp->translate("TRoom", "Warning: When setting the Area for Room (Id: %1) it did not have a current area!").arg(id);
mpRoomDB->mpMap->logError(error);
}
area = areaID;
pA->addRoom(id);
dirtyAreas.insert(pA);
pA->mIsDirty = true;
if (!isToDeferAreaRelatedRecalculations) {
QSetIterator<TArea*> itpArea = dirtyAreas;
while (itpArea.hasNext()) {
TArea* pArea = itpArea.next();
pArea->calcSpan();
pArea->determineAreaExits();
pArea->mIsDirty = false;
}
dirtyAreas.clear();
}
return true;
}
bool TRoom::setExit(int to, int direction)
{
// FIXME: This along with TRoom->setExit need to be unified to a controller.
switch (direction) {
case DIR_NORTH: north = to; break;
case DIR_NORTHEAST: northeast = to; break;
case DIR_NORTHWEST: northwest = to; break;
case DIR_EAST: east = to; break;
case DIR_WEST: west = to; break;
case DIR_SOUTH: south = to; break;
case DIR_SOUTHEAST: southeast = to; break;
case DIR_SOUTHWEST: southwest = to; break;
case DIR_UP: up = to; break;
case DIR_DOWN: down = to; break;
case DIR_IN: in = to; break;
case DIR_OUT: out = to; break;
default:
return false;
}
mpRoomDB->updateEntranceMap(this);
return true;
}
bool TRoom::hasExit(int direction) const
{
switch (direction) {
case DIR_NORTH: if (north != -1) { return true; } break;
case DIR_NORTHEAST: if (northeast != -1) { return true; } break;
case DIR_NORTHWEST: if (northwest != -1) { return true; } break;
case DIR_EAST: if (east != -1) { return true; } break;
case DIR_WEST: if (west != -1) { return true; } break;
case DIR_SOUTH: if (south != -1) { return true; } break;
case DIR_SOUTHEAST: if (southeast != -1) { return true; } break;
case DIR_SOUTHWEST: if (southwest != -1) { return true; } break;
case DIR_UP: if (up != -1) { return true; } break;
case DIR_DOWN: if (down != -1) { return true; } break;
case DIR_IN: if (in != -1) { return true; } break;
case DIR_OUT: if (out != -1) { return true; } break;
}
return false;
}
// Confirms whether the text identifies a valid exit, given the context in which
// it is called it does not have to worry about the casing of "text" as that has
// already been handled in the current usages:
bool TRoom::hasExitOrSpecialExit(const QString& text) const
{
// First check the normal ones:
if (text == QLatin1String("n")) {
return hasExit(DIR_NORTH);
} else if (text == QLatin1String("ne")) {
return hasExit(DIR_NORTHEAST);
} else if (text == QLatin1String("e")) {
return hasExit(DIR_EAST);
} else if (text == QLatin1String("se")) {
return hasExit(DIR_SOUTHEAST);
} else if (text == QLatin1String("s")) {
return hasExit(DIR_SOUTH);
} else if (text == QLatin1String("sw")) {
return hasExit(DIR_SOUTHWEST);
} else if (text == QLatin1String("w")) {
return hasExit(DIR_WEST);
} else if (text == QLatin1String("nw")) {
return hasExit(DIR_NORTHWEST);
} else if (text == QLatin1String("up")) {
return hasExit(DIR_UP);
} else if (text == QLatin1String("down")) {
return hasExit(DIR_DOWN);
} else if (text == QLatin1String("in")) {
return hasExit(DIR_IN);
} else if (text == QLatin1String("out")) {
return hasExit(DIR_OUT);
} else {
// Then check the special exits:
QMapIterator<int, QString> itSpecialExit(other);
while (itSpecialExit.hasNext()) {
itSpecialExit.next();
QString exitCmd(itSpecialExit.value());
if (exitCmd.startsWith(QLatin1Char('0')) || exitCmd.startsWith(QLatin1Char('1'))) {
exitCmd.remove(0, 1);
}
if (exitCmd == text) {
// We have a special exit which matches the given text
return true;
}
}
}
return false;
}
int TRoom::getExit(int direction)
{
switch (direction) {
case DIR_NORTH:
return north;
case DIR_NORTHEAST:
return northeast;
case DIR_NORTHWEST:
return northwest;
case DIR_EAST:
return east;
case DIR_WEST:
return west;
case DIR_SOUTH:
return south;
case DIR_SOUTHEAST:
return southeast;
case DIR_SOUTHWEST:
return southwest;
case DIR_UP:
return up;
case DIR_DOWN:
return down;
case DIR_IN:
return in;
case DIR_OUT:
return out;
}
return -1;
}
QHash<int, int> TRoom::getExits()
{
// key is room id we exit to, value is type of exit. 0 is normal, 1 is special
QHash<int, int> exitList;
if (north != -1) {
exitList[north] = 0;
}
if (northeast != -1) {
exitList[northeast] = 0;
}
if (east != -1) {
exitList[east] = 0;
}
if (southeast != -1) {
exitList[southeast] = 0;
}
if (south != -1) {
exitList[south] = 0;
}
if (southwest != -1) {
exitList[southwest] = 0;
}
if (west != -1) {
exitList[west] = 0;
}
if (northwest != -1) {
exitList[northwest] = 0;
}
if (up != -1) {
exitList[up] = 0;
}
if (down != -1) {
exitList[down] = 0;
}
if (in != -1) {
exitList[in] = 0;
}
if (out != -1) {
exitList[out] = 0;
}
QMapIterator<int, QString> it(other);
while (it.hasNext()) {
it.next();
exitList[it.key()] = 1;
}
return exitList;
}
void TRoom::setExitLock(int exit, bool state)
{
if (state) {
if ((!exitLocks.contains(exit)) && (exit >= DIR_NORTH && exit <= DIR_OUT)) {
exitLocks.push_back(exit);
}
} else {
exitLocks.removeAll(exit);
}
}
// The need for "to" seems superfluous here, cmd is the decisive factor
void TRoom::setSpecialExitLock(int to, const QString& cmd, bool doLock)
{
QMapIterator<int, QString> it(other);
while (it.hasNext()) {
it.next();
if (it.key() != to) {
continue;
}
if (it.value().size() < 1) {
continue;
}
if (it.value().mid(1) != cmd) {
if (it.value() != cmd) {
continue;
}
}
if (doLock) {
QString _cmd = it.value();
_cmd.replace(0, 1, '1');
other.replace(to, _cmd);
} else {
QString _cmd = it.value();
_cmd.replace(0, 1, '0');
other.replace(to, _cmd);
}
return;
}
}
bool TRoom::setSpecialExitLock(const QString& cmd, bool doLock)
{
QMutableMapIterator<int, QString> it(other);
while (it.hasNext()) {
it.next();
if (!it.value().size()) {
continue;
}
if (it.value().mid(1) != cmd) { // This value doesn't match, just check the old (obsolete) form without a lock state prefix
if (it.value() != cmd) { // No match with or WITHOUT lock prefix, so move on to next value
continue;
} else { // Got a match WITHOUT a '0'|'1' prefix (used now to encode lock state) so add it on
QString _cmd = it.value();
if (doLock) {
_cmd.prepend('1');
} else {
_cmd.prepend('0');
}
it.setValue(_cmd); // We can change the value as we are using the Mutable iterator...
return true;
}
} else { // Found it!
QString _cmd = it.value();
if (doLock) {
_cmd.replace(0, 1, '1');
} else {
_cmd.replace(0, 1, '0');
}
it.setValue(_cmd);
return true;
}
}
return false;
}
bool TRoom::hasExitLock(int exit)
{
return exitLocks.contains(exit);
}
// 0=offen 1=zu
bool TRoom::hasSpecialExitLock(int to, const QString& cmd)
{
if (other.contains(to)) {
QMapIterator<int, QString> it(other);
while (it.hasNext()) {
it.next();
if (it.key() != to) {
continue;
}
if (it.value().size() < 2) {
continue;
}
return it.value().mid(0, 1) == "1";
}
return false;
} else {
return false;
}
}
// Original addSpecialExit...() code had limitation that it used the "to" room
// as part of the things to look for to identify a particular special exit
// indeed the use of the "to" room as the key for the "other" exit map does seem
// a poorer choice than the "command" which is currently the value item...
// FIXME: swap key/value items in (TRoom *)->other<int, QString> map?
// Changing to setSpecialExit(), "to" values less than 1 remove exit...
void TRoom::setSpecialExit(int to, const QString& cmd)
{
QString _strippedCmd;
QString _prefix = "";
if (cmd.startsWith('0') || cmd.startsWith('1')) {
_strippedCmd = cmd.mid(1);
_prefix = cmd.mid(0, 1);
} else {
_strippedCmd = cmd;
}
if (_strippedCmd.isEmpty()) {
return; // Refuse to create an unnamed special exit!!!
}
// replace if this special exit exists, otherwise add
QMutableMapIterator<int, QString> it(other);
while (it.hasNext()) {
it.next();
if (!it.value().size()) {
continue;
}
if (Q_LIKELY(it.value().startsWith('0') || it.value().startsWith('1'))) {
if (it.value().mid(1) != _strippedCmd) {
continue;
} else { // Found the matching command, preserve the existing lock state
// unless overriden in command and also the old destination to
// note which areas are affected
if (_prefix.isEmpty()) {
_prefix = it.value().mid(0, 1);
}
it.remove(); // Despite this being a "Mutable" iterator it does
// NOT allow us to change the KEY - we only can
// remove the entry to add-in a new one later.
break;
}
} else {
if (it.value() != _strippedCmd) {
continue;
} else { // Found the matching command, but this is an old one with no lock state prefix
if (_prefix.isEmpty()) {
_prefix = '0'; // Assume default unlock case if not set
}
it.remove();
break;
}
}
}
// Have definitely removed the existing case of this command
// Now add it to map if wanted
if (to > 0) {
if (_prefix.isEmpty()) {
_prefix = '0';
}
QString finalCmd = _prefix % _strippedCmd;
other.insertMulti(to, finalCmd);
} else { // Clean up related data:
customLinesArrow.remove(_strippedCmd);
customLinesColor.remove(_strippedCmd);
customLinesStyle.remove(_strippedCmd);
customLines.remove(_strippedCmd);
exitWeights.remove(_strippedCmd);
doors.remove(_strippedCmd);
}
TArea* pA = mpRoomDB->getArea(area);
if (pA) {
pA->determineAreaExitsOfRoom(id);
// This updates the (TArea *)->exits map even for exit REMOVALS
}
mpRoomDB->updateEntranceMap(this);
mpRoomDB->mpMap->mMapGraphNeedsUpdate = true;
}
void TRoom::clearSpecialExits()
{
other.clear();
mpRoomDB->updateEntranceMap(this);
mpRoomDB->mpMap->mMapGraphNeedsUpdate = true;
}
void TRoom::removeAllSpecialExitsToRoom(int _id)
{
QList<int> keyList = other.keys();
QList<QString> valList = other.values();
for (int i = 0; i < keyList.size(); i++) {
if (keyList[i] == _id) {
// guaranteed to be in synch according to Qt docs
other.remove(keyList[i], valList[i]);
}
}
TArea* pA = mpRoomDB->getArea(area);
if (pA) {
pA->determineAreaExitsOfRoom(id);
}
mpRoomDB->updateEntranceMap(this);
mpRoomDB->mpMap->mMapGraphNeedsUpdate = true;
}
void TRoom::calcRoomDimensions()
{
if (customLines.empty()) {
return;
}
min_x = 0.0;
min_y = 0.0;
max_x = 0.0;
max_y = 0.0;
bool needInit = true;
QMapIterator<QString, QList<QPointF>> it(customLines);
while (it.hasNext()) {
it.next();
const QList<QPointF>& _pL = it.value();
if (_pL.empty()) {
continue;
}
if (needInit) {
needInit = false;
min_x = _pL[0].x();
max_x = min_x;
min_y = _pL[0].y();
max_y = min_y;
}
for (auto point : _pL) {
qreal _x = point.x();
qreal _y = point.y();
if (_x < min_x) {
min_x = _x;
}
if (_x > max_x) {
max_x = _x;
}
if (_y < min_y) {
min_y = _y;
}
if (_y > max_y) {
max_y = _y;
}
}
}
}
void TRoom::restore(QDataStream& ifs, int roomID, int version)
{
id = roomID;
ifs >> area;
// Can be useful when analysing suspect map files!
// qDebug() << "TRoom::restore(...," << roomID << ",...) has AreaId:" << area;
ifs >> x;
ifs >> y;
ifs >> z;
ifs >> north;
ifs >> northeast;
ifs >> east;
ifs >> southeast;
ifs >> south;
ifs >> southwest;
ifs >> west;
ifs >> northwest;
ifs >> up;
ifs >> down;
ifs >> in;
ifs >> out;
ifs >> environment;
ifs >> weight;
// force room weight >= 1 otherwise pathfinding chooses random paths.
if (weight < 1) {
weight = 1;
}
if (version < 8) {
float f1, f2, f3, f4;
ifs >> f1; //rooms[i]->xRot;
ifs >> f2; //rooms[i]->yRot;
ifs >> f3; //rooms[i]->zRot;
ifs >> f4; //rooms[i]->zoom;
}
ifs >> name;
ifs >> isLocked;
if (version >= 6) {
ifs >> other;
}
qint8 oldCharacterCode = 0;
if (version >= 19) {
// From version 19 we use a QString for one or more (Unicode) Graphemes
ifs >> mSymbol;
} else if (version >= 9 && version < 19) {
// For older versions we note the prior unsigned short in case
// there is no fallback carried in the room user data
ifs >> oldCharacterCode;
}
if (version >= 10) {
ifs >> userData;
if (version < 19) {
// Recover and remove backup values from the user data
QString symbolString = userData.take(QLatin1String("system.fallback_symbol"));
if (!symbolString.isEmpty()) {
// There is a fallback in the user data
mSymbol = symbolString;
} else if (oldCharacterCode > 32) {
// There is an old format unsigned short represeting a printable
// ASCII or ISO 8859-1 (Latin1) character:
mSymbol = QChar(oldCharacterCode);
}
}
}
if (version >= 11) {
if (version >= 20) {
// In version 20 we stopped storing a QString form for the line
// style - and we also switched to lower case for the normal exit
// direction keys and using a QColor to store the color:
ifs >> customLines;
ifs >> customLinesArrow;
ifs >> customLinesColor;
ifs >> customLinesStyle;
} else {
QMap<QString, QList<QPointF>> oldLinesData;
ifs >> oldLinesData;
QMapIterator<QString, QList<QPointF>> itCustomLine(oldLinesData);
while (itCustomLine.hasNext()) {
itCustomLine.next();
QString direction(itCustomLine.key());
if (direction == QLatin1String("N") || direction == QLatin1String("E") || direction == QLatin1String("S") || direction == QLatin1String("W") || direction == QLatin1String("UP")
|| direction == QLatin1String("DOWN")
|| direction == QLatin1String("NE")
|| direction == QLatin1String("SE")
|| direction == QLatin1String("SW")
|| direction == QLatin1String("NW")
|| direction == QLatin1String("IN")
|| direction == QLatin1String("OUT")) {
customLines.insert(itCustomLine.key().toLower(), itCustomLine.value());
} else {
customLines.insert(itCustomLine.key(), itCustomLine.value());
}
}
QMap<QString, bool> oldLinesArrowData;
ifs >> oldLinesArrowData;
QMapIterator<QString, bool> itCustomLineArrow(oldLinesArrowData);
while (itCustomLineArrow.hasNext()) {
itCustomLineArrow.next();
QString direction(itCustomLineArrow.key());
if (direction == QLatin1String("N") || direction == QLatin1String("E") || direction == QLatin1String("S") || direction == QLatin1String("W") || direction == QLatin1String("UP")
|| direction == QLatin1String("DOWN")
|| direction == QLatin1String("NE")
|| direction == QLatin1String("SE")
|| direction == QLatin1String("SW")
|| direction == QLatin1String("NW")
|| direction == QLatin1String("IN")
|| direction == QLatin1String("OUT")) {
customLinesArrow.insert(itCustomLineArrow.key().toLower(), itCustomLineArrow.value());
} else {
customLinesArrow.insert(itCustomLineArrow.key(), itCustomLineArrow.value());
}
}
QMap<QString, QList<int>> oldLinesColorData;
ifs >> oldLinesColorData;
QMapIterator<QString, QList<int>> itCustomLineColor(oldLinesColorData);
while (itCustomLineColor.hasNext()) {
itCustomLineColor.next();
QString direction(itCustomLineColor.key());
if (direction == QLatin1String("N") || direction == QLatin1String("E") || direction == QLatin1String("S") || direction == QLatin1String("W") || direction == QLatin1String("UP")
|| direction == QLatin1String("DOWN")
|| direction == QLatin1String("NE")
|| direction == QLatin1String("SE")
|| direction == QLatin1String("SW")
|| direction == QLatin1String("NW")
|| direction == QLatin1String("IN")
|| direction == QLatin1String("OUT")) {
// Fixup broken custom lines caused by maps saved prior to
// https://github.com/Mudlet/Mudlet/pull/2559 going into the
// code by only adding the value if it contains enough
// colour components to be a valid colour:
if (itCustomLineColor.value().count() > 2) {
customLinesColor.insert(itCustomLineColor.key().toLower(), QColor(itCustomLineColor.value().at(0), itCustomLineColor.value().at(1), itCustomLineColor.value().at(2)));
}
// Otherwise we will fixup both empty
// itCustomLineColor.value() entites AND altogether missing
// ones outside of the while() {...}:
} else {
if (itCustomLineColor.value().count() > 2) {
customLinesColor.insert(itCustomLineColor.key(), QColor(itCustomLineColor.value().at(0), itCustomLineColor.value().at(1), itCustomLineColor.value().at(2)));
}
}
}
// Create new (RED) colour customLinesColor entities for any custom
// exit lines that does not have one or has an empty one as a result
// of https://github.com/Mudlet/Mudlet/issues/2558 - use a QSet
// rather than a QList when manipulating the present/absent
// direction keys as there won't be any duplicate keys and the
// operation we want to perform (obtain the directions of all custom
// exit lines and remove those which are already included in the
// colours) is much easier to perform on a QSet rather than a QList:
QSet<QString> missingKeys = customLines.keys().toSet().subtract(customLinesColor.keys().toSet());
QSetIterator<QString> itMissingCustomLineColourKey(missingKeys);
while (itMissingCustomLineColourKey.hasNext()) {
customLinesColor.insert(itMissingCustomLineColourKey.next(), QColor(Qt::red));
}
QMap<QString, QString> oldLineStyleData;
ifs >> oldLineStyleData;
QMapIterator<QString, QString> itCustomLineStyle(oldLineStyleData);
while (itCustomLineStyle.hasNext()) {
itCustomLineStyle.next();
QString direction(itCustomLineStyle.key());
if (direction == QLatin1String("N") || direction == QLatin1String("E") || direction == QLatin1String("S") || direction == QLatin1String("W") || direction == QLatin1String("UP")
|| direction == QLatin1String("DOWN")
|| direction == QLatin1String("NE")
|| direction == QLatin1String("SE")
|| direction == QLatin1String("SW")
|| direction == QLatin1String("NW")
|| direction == QLatin1String("IN")
|| direction == QLatin1String("OUT")) {
direction = direction.toLower();
}
if (itCustomLineStyle.value() == QLatin1String("dot line")) {
customLinesStyle.insert(direction, Qt::DotLine);
} else if (itCustomLineStyle.value() == QLatin1String("dash line")) {
customLinesStyle.insert(direction, Qt::DashLine);
} else if (itCustomLineStyle.value() == QLatin1String("dash dot line")) {
customLinesStyle.insert(direction, Qt::DashDotLine);
} else if (itCustomLineStyle.value() == QLatin1String("dash dot dot line")) {
customLinesStyle.insert(direction, Qt::DashDotDotLine);
} else {
customLinesStyle.insert(direction, Qt::SolidLine);
}
}
}
ifs >> exitLocks;
}
if (version >= 13) {
ifs >> exitStubs;
}
if (version >= 16) {
ifs >> exitWeights;
ifs >> doors;
}
calcRoomDimensions();
}
void TRoom::audit(const QHash<int, int> roomRemapping, const QHash<int, int> areaRemapping)
{
if (areaRemapping.contains(area)) {
userData.insert(QStringLiteral("audit.remapped_area"), QString::number(area));
area = areaRemapping.value(area);
}
auditExits(roomRemapping);
}
void TRoom::auditExits(const QHash<int, int> roomRemapping)
{
// Clone all the structures into working copies that we can eliminate valid
// members from to identify any rogue members before removing them:
QMap<QString, int> exitWeightsCopy = exitWeights;
QSet<int> exitStubsCopy = exitStubs.toSet();
QSet<int> exitLocksCopy = exitLocks.toSet();
QMap<QString, int> doorsCopy = doors;
QMap<QString, QList<QPointF>> customLinesCopy = customLines;
QMap<QString, QColor> customLinesColorCopy = customLinesColor;
QMap<QString, Qt::PenStyle> customLinesStyleCopy = customLinesStyle;
QMap<QString, bool> customLinesArrowCopy = customLinesArrow;
exitWeightsCopy.detach(); // Make deep copies now, this will happen anyhow once we start to remove valid members
exitStubsCopy.detach();
exitLocksCopy.detach();
doorsCopy.detach();
customLinesCopy.detach();
customLinesColorCopy.detach();
customLinesStyleCopy.detach();
customLinesArrowCopy.detach();
auditExit(north,
DIR_NORTH,
tr("North"),
QStringLiteral("n"),
exitWeightsCopy,
exitStubsCopy,
exitLocksCopy,
doorsCopy,
customLinesCopy,
customLinesColorCopy,
customLinesStyleCopy,
customLinesArrowCopy,
roomRemapping);
auditExit(northeast,
DIR_NORTHEAST,
tr("Northeast"),
QStringLiteral("ne"),
exitWeightsCopy,
exitStubsCopy,
exitLocksCopy,
doorsCopy,
customLinesCopy,
customLinesColorCopy,
customLinesStyleCopy,
customLinesArrowCopy,
roomRemapping);
auditExit(northwest,
DIR_NORTHWEST,
tr("Northwest"),
QStringLiteral("nw"),
exitWeightsCopy,
exitStubsCopy,
exitLocksCopy,
doorsCopy,
customLinesCopy,