-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.cpp
538 lines (471 loc) · 15 KB
/
room.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
#include "room.h"
#include <qstringlist.h>
#include <QTextStream>
#include "utils.h"
#include "exception.h"
#include "constantname.h"
namespace ts
{
void Room::init()
{
m_description = "";
m_zone = ZoneVNumberInvalid;
m_flags = 0;
m_sectorType = ROOM_SECTOR_INSIDE;
m_tunnelLimit = 3;
/* teleport */
m_teleportTime = 10;
m_teleportToRoom = 0;
m_teleportFlags = 0;
m_teleportCount = 0;
m_teleportSectorType = ROOM_SECTOR_INSIDE;
/* river */
m_riverDir = 0;
m_riverSpeed = 0;
posX = 0;
posY = 0;
posZ = 0;
/* lists */
if( !m_extraDescriptions.empty() )
m_extraDescriptions.clear();
if( !m_exits.empty() )
m_exits.clear();
}
Room::Room()
: ObjectData( ObjectData::Object_Room )
{
init();
}
Room::Room( VNumber room_vnumber, const QString& room_name )
: ObjectData( ObjectData::Object_Room, room_vnumber, room_name )
{
init();
}
Room::Room(const Room& r) : ObjectData((ObjectData)r)
{
copyFromRoom(r, true);
}
void Room::copyFromRoom(const ts::Room& r, bool cloneExits)
{
m_type = r.m_type;
m_vnumber = r.m_vnumber;
m_name = r.m_name;
m_description = r.m_description;
m_zone = r.m_zone;
m_flags = r.m_flags;
m_sectorType = r.m_sectorType;
m_tunnelLimit = r.m_tunnelLimit;
m_teleportTime = r.m_teleportTime;
m_teleportToRoom = r.m_teleportToRoom;
m_teleportFlags = r.m_teleportFlags;
m_teleportCount = r.m_teleportCount;
m_teleportSectorType = r.m_teleportSectorType;
m_riverDir = r.m_riverDir;
m_riverSpeed = r.m_riverSpeed;
m_extraDescriptions = r.m_extraDescriptions;
posX = r.posX;
posY = r.posY;
posZ = r.posZ;
if (cloneExits)
{
m_exits = r.m_exits;
}
}
Room::~Room()
{}
Room& Room::operator=( const Room& r )
{
if( this != &r )
{
ObjectData::operator=( (ObjectData)r );
m_description = r.m_description;
m_zone = r.m_zone;
m_flags = r.m_flags;
m_sectorType = r.m_sectorType;
m_tunnelLimit = r.m_tunnelLimit;
m_teleportTime = r.m_teleportTime;
m_teleportToRoom = r.m_teleportToRoom;
m_teleportFlags = r.m_teleportFlags;
m_teleportCount = r.m_teleportCount;
m_teleportSectorType = r.m_teleportSectorType;
m_riverDir = r.m_riverDir;
m_riverSpeed = r.m_riverSpeed;
m_extraDescriptions = r.m_extraDescriptions;
m_exits = r.m_exits;
posX = r.posX;
posY = r.posY;
posZ = r.posZ;
}
return *this;
}
bool Room::hasExtraDescription( const QString& extra_keys ) const
{
try
{
(void) extraDescription( extra_keys );
return true;
}
catch( XObjectNotFound& )
{
return false;
}
}
bool Room::canFindExtraDescription(const QString& extra_keys) const
{
try
{
(void)findExtraDescription(extra_keys);
return true;
}
catch (XObjectNotFound&)
{
return false;
}
}
const ExtraDescription& Room::findExtraDescription(const QString& key) const
{
QString sKeysToFind = key.toLower();
extra_description_const_iterator it = m_extraDescriptions.begin();
while (it != m_extraDescriptions.end())
{
if ((*it).isname(sKeysToFind))
return *it;
++it;
}
QString sException = "";
sException.sprintf("ExtraDescription '%s' not found.", sKeysToFind.toUtf8().data());
throw XObjectNotFound(sException);
}
const ExtraDescription& Room::extraDescription( const QString& extra_keys ) const
{
QString sKeysToFind = extra_keys.toLower();
extra_description_const_iterator it = m_extraDescriptions.begin();
while( it != m_extraDescriptions.end() )
{
if( (*it).keys() == sKeysToFind.toLower() )
return *it;
++it;
}
QString sException = "";
sException.sprintf( "ExtraDescription '%s' not found.", sKeysToFind.toUtf8().data() );
throw XObjectNotFound( sException );
}
void Room::removeExtraDescription( const QString& extra_keys )
{
QString sKeysToFind = extra_keys.toLower();
extra_description_iterator it = m_extraDescriptions.begin();
while( it != m_extraDescriptions.end() )
{
if( (*it).keys() == sKeysToFind.toLower() )
{
qDebug( "%s removed from %s.", (*it).dumpObject().toUtf8().data(), dumpObject().toUtf8().data() );
m_extraDescriptions.erase( it );
return;
}
++it;
}
}
void Room::addExtraDescription( const ExtraDescription& new_extra_description )
{
removeExtraDescription( new_extra_description.keys() );
m_extraDescriptions.append( new_extra_description );
qDebug( "%s added to %s.", new_extra_description.dumpObject().toUtf8().data(), dumpObject().toUtf8().data() );
}
bool Room::hasExit( const QString& exit_name ) const
{
try
{
(void) exit( exit_name );
return true;
}
catch( XObjectNotFound& )
{
return false;
}
}
const Exit& Room::exit( const QString& exit_name ) const
{
QString sExitToFind = exit_name.toLower();
exits_const_iterator it = m_exits.begin();
while( it != m_exits.end() )
{
if( (*it).name() == sExitToFind )
return *it;
++it;
}
QString sException = "";
sException.sprintf( "Exit '%s' not found.", sExitToFind.toUtf8().data() );
throw XObjectNotFound( sException );
}
bool Room::hasExit( int dir ) const
{
return hasExit( ConstantName::exitDirection( dir ) );
}
const Exit& Room::exit( int dir ) const
{
return exit( ConstantName::exitDirection( dir ) );
}
void Room::removeExit( const QString& exit_name )
{
QString sExitToFind = exit_name.toLower();
exits_iterator it = m_exits.begin();
while( it != m_exits.end() )
{
if( (*it).name() == sExitToFind )
{
qDebug( "%s removed from %s.", (*it).dumpObject().toUtf8().data(), dumpObject().toUtf8().data() );
m_exits.erase( it );
return;
}
++it;
}
}
void Room::addExit( Exit new_exit )
{
removeExit( new_exit.name() );
new_exit.setFromRoom( m_vnumber );
m_exits.append( new_exit );
qDebug( "%s added to %s.", new_exit.dumpObject().toUtf8().data(), dumpObject().toUtf8().data() );
}
void Room::save( QTextStream& stream, bool map )
{
stream << "#" << m_vnumber << endl;
stream << m_name << "~" << endl;
stream << m_description << endl;
stream << "~" << endl;
stream << m_zone << " ";
Utils::saveBitVector( stream, m_flags );
stream << " " << flush;
if( isTeleport() )
{
stream << "-1 " << m_teleportTime << " " << m_teleportToRoom << " ";
Utils::saveBitVector( stream, m_teleportFlags );
stream << " ";
if( hasTeleportFlag( ROOM_TELEPORT_FLAG_COUNT ) )
stream << m_teleportCount << " ";
}
stream << realSectorType() << flush;
if( isRiver() )
{
stream << " " << m_riverSpeed << " " << m_riverDir << flush;
}
if( hasFlag( ROOM_FLAG_TUNNEL ) )
{
stream << " " << m_tunnelLimit << flush;
}
stream << endl << flush;
/* Exit */
qSort(m_exits.begin(), m_exits.end());
exits_iterator ite = m_exits.begin();
while( ite != m_exits.end() )
{
(*ite).save( stream );
++ite;
}
/* Extras */
extra_description_iterator itd = m_extraDescriptions.begin();
while( itd != m_extraDescriptions.end() )
{
(*itd).save( stream );
++itd;
}
if (map) {
stream << "L" << endl;
stream << posX << " " << posY << " " << posZ << endl << flush;
}
else {
posX = posY = posZ = 0;
}
/* Closing fields */
stream << "S" << endl << flush;
qDebug( "%s saving completed.", dumpObject().toUtf8().data() );
}
void Room::load( FILE* pFile )
{
setName( Utils::readString( pFile ) );
QString sErrorMessage = "";
sErrorMessage = dumpObject();
setDescription( Utils::readString( pFile ) );
m_zone = Utils::readNumber( pFile, sErrorMessage + " (ZoneVNumber)" );
m_flags = Utils::readNumber( pFile, sErrorMessage + " (Flags)" );
int iTmp = Utils::readNumber( pFile, sErrorMessage + " (SectorType)" );
if( iTmp == -1 ) // Room is Teleport
m_sectorType = ROOM_SECTOR_TELEPORT;
else if( iTmp < 0 || iTmp >= ROOM_SECTOR_END )
{
qWarning( "%s has an invalid sector type %d (fixed).", sErrorMessage.toUtf8().data(), iTmp );
m_sectorType = ROOM_SECTOR_INSIDE;
}
else
m_sectorType = iTmp;
if( isTeleport() )
{
m_teleportTime = Utils::readNumber( pFile, sErrorMessage + " (TeleportTime)" );
m_teleportToRoom = Utils::readNumber( pFile, sErrorMessage + " (TeleportToRoom)" );
m_teleportFlags = Utils::readNumber( pFile, sErrorMessage + " (TeleportFlags)" );
if( hasTeleportFlag( ROOM_TELEPORT_FLAG_COUNT ) ) {
m_teleportCount = Utils::readNumber( pFile, sErrorMessage + " (TeleportCount)" );
}
iTmp = Utils::readNumberInLine( pFile, sErrorMessage + " (RealSectorType)" );
if( iTmp < 0 || iTmp >= ROOM_SECTOR_END || iTmp == ROOM_SECTOR_TELEPORT )
{
qWarning( "%s has an invalid teleport sector type %d (fixed).", sErrorMessage.toUtf8().data(), iTmp );
m_teleportSectorType = ROOM_SECTOR_INSIDE;
if( iTmp == -1L) {
m_teleportSectorType = m_teleportCount;
m_teleportCount = 0;
}
}
else
m_teleportSectorType = iTmp;
}
if( isRiver() )
{
m_riverSpeed = Utils::readNumberInLine( pFile, sErrorMessage + " (RiverSpeed)" );
if(m_riverSpeed != -1L) {
iTmp = Utils::readNumberInLine( pFile, sErrorMessage + " (RiverDir)" );
if( iTmp >= EXIT_DIRECTION_NORTH && iTmp < EXIT_DIRECTION_END )
m_riverDir = iTmp;
else
m_riverDir = EXIT_DIRECTION_INVALID;
}
}
if( hasFlag( ROOM_FLAG_TUNNEL ) )
{
m_tunnelLimit = Utils::readNumber( pFile, sErrorMessage + " (TunnelLimit)" );
}
char chk[161];
while( !feof( pFile ) && fgets( chk, 160, pFile ) )
{
switch( *chk )
{
case 'D': /* exit field */
loadExit( pFile, QString( chk ) );
break;
case 'E': /* extra description field */
loadExtraDescription( pFile );
break;
case 'L': /* location field */
{
posX = Utils::readNumber(pFile, "Position X", 0);
posY = Utils::readNumber(pFile, "Position Y", 0);
posZ = Utils::readNumber(pFile, "Position Z", 0);
}
break;
case 'S': /* end of room */
qDebug( "%s loading completed.", dumpObject().toUtf8().data() );
validate();
return;
#if defined( TS_DEBUG_PANIC )
default:
qDebug( "Skip line: '%s'.", chk );
#endif
}
}
qWarning( "%s loading completed. End of room not found.", dumpObject().toUtf8().data() );
validate();
}
void Room::loadExtraDescription( FILE* pFile )
{
ExtraDescription new_extra;
new_extra.load( pFile );
addExtraDescription( new_extra );
}
void Room::loadExit( FILE* pFile, const QString& head )
{
QChar c = head.at(1);
int eDirection = EXIT_DIRECTION_INVALID;
if( c.isDigit() )
{
int iDir = c.digitValue();
if( iDir >= EXIT_DIRECTION_NORTH && iDir < EXIT_DIRECTION_END )
{
eDirection = iDir;
Exit new_exit( eDirection, m_vnumber );
new_exit.load( pFile );
addExit( new_exit );
}
else
{
qWarning( "%s has an invalid exit direction in line '%s'.", dumpObject().toUtf8().data(), head.toUtf8().data() );
}
}
else
{
QString sTmp = head;
sTmp.remove( 0, 1 ); // remove 'D'
QStringList sList = sTmp.split( "~" );
QString name = sList.count() >= 1 ? sList[ 0 ] : "";
QString listName = sList.count() >= 2 ? sList[ 1 ] : "";
QString toName = sList.count() >= 3 ? sList[ 2 ] : "";
QString fromName = sList.count() >= 4 ? sList[ 3 ] : "";
QString revDirName = sList.count() >= 5 ? sList[ 4 ] : "";
Exit new_exit( name, m_vnumber );
new_exit.setListName( listName );
new_exit.setToName( toName );
new_exit.setFromName( fromName );
new_exit.setRevDirName( revDirName );
new_exit.load( pFile );
addExit( new_exit );
}
}
void Room::validate()
{
if( m_name.isEmpty() )
qWarning( "%s has an empty name.", dumpObject().toUtf8().data() );
if( m_description.isEmpty() )
qWarning( "%s has an empty description.", dumpObject().toUtf8().data() );
if( m_zone < 1 && m_zone != VNumberInvalid )
qDebug( "%s has an invalid zone number #%ld.", dumpObject().toUtf8().data(), m_zone );
if( hasFlag( ROOM_FLAG_DEATH ) )
qWarning( "%s is a death trap.", dumpObject().toUtf8().data() );
if( isTeleport() )
{
if( m_teleportTime <= 0 || m_teleportTime > 100 || m_teleportTime % 10 > 0 )
{
qWarning( "%s has an invalid teleport time %d, fixed to 10.", dumpObject().toUtf8().data(), m_teleportTime );
m_teleportTime = 10;
}
if( m_teleportToRoom <= 0 )
qWarning( "%s has an invalid teleport target room #%ld.", dumpObject().toUtf8().data(), m_teleportToRoom );
if( hasTeleportFlag( ROOM_TELEPORT_FLAG_COUNT ) && m_teleportCount <= 0 )
{
qWarning( "%s has an invalid teleport count %d, fixed to 1.", dumpObject().toUtf8().data(), m_teleportCount );
m_teleportCount = 1;
}
}
if( isRiver() )
{
if( m_riverSpeed < 0 || m_riverSpeed % 15 > 0 )
{
qWarning( "%s has an invalid river speed %d, fixed to 15.", dumpObject().toUtf8().data(), m_riverSpeed );
m_riverSpeed = 15;
}
if( m_riverDir == EXIT_DIRECTION_INVALID )
{
qWarning( "%s has an invalid river direction, fixed to North.", dumpObject().toUtf8().data() );
m_riverDir = EXIT_DIRECTION_NORTH;
}
}
if( hasFlag( ROOM_FLAG_TUNNEL ) && m_tunnelLimit < 1 )
{
qWarning( "%s has an invalid tunnel limit %d, fixed to 3.", dumpObject().toUtf8().data(), m_tunnelLimit );
m_tunnelLimit = 3;
}
if( !m_exits.empty() )
{
exits_iterator ite = m_exits.begin();
while( ite != m_exits.end() )
{
if( (*ite).name().isEmpty() )
qWarning( "%s has an empty exit name.", dumpObject().toUtf8().data() );
if( (*ite).fromRoom() != m_vnumber )
{
qWarning( "%s has an exit of another room, fixed." , dumpObject().toUtf8().data() );
(*ite).setFromRoom( m_vnumber );
}
++ite;
}
}
}
} // namespace ts