-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata.cpp
More file actions
2163 lines (1932 loc) · 44.7 KB
/
data.cpp
File metadata and controls
2163 lines (1932 loc) · 44.7 KB
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
#include "stdafx.h"
#include <stdio.h>
#include "UI.h"
//#include "Objects.h"
#include "Dispatch.h"
#include "CSB.h"
#include "Data.h"
void MD5Init();
void MD5Update(ui8 *data, ui32 len);
void MD5Final(ui8 *digest);
void CleanupCustomPhrases();
void CleanupTranslations();
void DSAInstrumentation_Dump();
void FILETABLECleanup();
bool IsRecordFileRecording();
extern ui32 numRandomCalls;
extern char *PlayfileName;
extern char szCSBVersion[];
extern i32 VBLMultiplier;
extern bool fullscreenRequested;
//extern bool fullscreenActive;
extern bool RecordCommandOption;
extern bool NoRecordCommandOption;
extern char *finalEditText;
void AtariMemCleanup();
#define MaxTraceFiles 100
i16 TraceFile = -1;
i16 GraphicTraceFile = -1;
char *g_folderName = NULL;
std::string g_folderParentName;
std::string g_root;
VIDEOMODE videoMode;
std::unique_ptr<ui8[]> g_tempBitmap;
int g_tempBitmapSize = 0;
ui8 *dataTypeMap = NULL;
ui16 *dataIndexMap = NULL;
i32 dataMapLength = 0;
ui32 graphicSignature1=0, graphicSignature2=0;
ui32 expectedGraphicSignature1=0, expectedGraphicSignature2=0;
ui32 CSBgraphicSignature1=0, CSBgraphicSignature2=0;
ui32 expectedCSBgraphicSignature1=0, expectedCSBgraphicSignature2=0;
ui32 EDBT_CSBGraphicsSignature_data = 0xffffffff;
ui32 EDBT_GraphicsSignature_data = 0xffffffff;
ui32 EDBT_CSBversion_data = 0xffffffff;
ui32 EDBT_Debuging_data = 0xffffffff;
ui32 dungeonSignature1=0, dungeonSignature2=0;
ui32 versionSignature = 0;
ui32 spellFilterLocation = 0;
ui32 cellflagArraySize;
bool disableSaves = false;
bool bigActuators = false;
bool sequencedTimers = false;
bool extendedTimers = false;
bool DefaultDirectXOption = false;
bool extendedWallDecorations;
i32 deleteDuplicateTimers = -1;
OVERLAYDATA currentOverlay;
SOUNDDATA currentSound;
TEMPORARY_FILE CSBgraphicsFile;
bool overlayActive = false;
i32 xGraphicJitter=0;
i32 yGraphicJitter=0;
i32 xOverlayJitter=0;
i32 yOverlayJitter=0;
bool jitterChanged = false;
ui8 overlayPaletteRed[512];
ui8 overlayPaletteGreen[512];
ui8 overlayPaletteBlue[512];
bool invisibleMonsters = false;
bool drawAsSize4Monsters = false;
i32 g_objectListIndexSize = 0;
EXPOOL expool;
BACKGROUND_LIB backgroundLib;
i32 adjustSkillsParameters[5];
bool monsterMoveFilterActive = false;
i32 numGlobalVariables = 0;
ui32 *globalVariables = NULL;
ui32 parameterMessageSequence = 0;
bool playback_71 = false;
bool neophyteSkills = false;
MouseQueueEnt MouseQueue[MOUSEQLEN]; //Moved to global space to enlarge.//16852
MouseQueueEnt *pMouseQueue = MouseQueue;
ui8 monsterMoveInhibit[4]; //N, E, S, W
DSAINDEX DSAIndex;
ui16 DSALevelIndex[64][32];
void AllocateTempBitmap(int size)
{
if (g_tempBitmapSize < size)
{
g_tempBitmap = std::make_unique<ui8[]>(size);
g_tempBitmapSize = size;
}
}
void ClearOverlayPalette()
{
int i, intensity;
//ui16 result[512];
//Set overlay palette to the identity palette
// We translate the color intensities of the Atari palette (0 - 7)
// to 8-bit color intensities (0 - 255) and split the three
// colors components into three arrays.
// Now, for each color word ( rrrgggbbb ) we can do a table
// lookup to determine, for example, the red component.
for (i=0; i<512; i++)
{
intensity=((i>>6)&7);
intensity = (intensity<<5)|(intensity<<2)|(intensity>>1);
overlayPaletteRed[i] = (ui8)intensity;
intensity=((i>>3)&7);
intensity = (intensity<<5)|(intensity<<2)|(intensity>>1);
overlayPaletteGreen[i] = (ui8)intensity;
intensity=((i>>0)&7);
intensity = (intensity<<5)|(intensity<<2)|(intensity>>1);
overlayPaletteBlue[i] = (ui8)intensity;
//result[i] = ((overlayPaletteRed[i]>>3)<<10)
// | ((overlayPaletteGreen[i]>>3)<<5)
// | ((overlayPaletteBlue[i]>>3));
};
//FILE *f = fopen("identityPalette.bin","wb");
//fwrite(result,2,512,f);
//fclose(f);
}
void CloseTraceFile()
{
if (TraceFile >= 0) CLOSE(TraceFile);
TraceFile = -1;
}
void CloseGraphicTraceFile()
{
CLOSE(GraphicTraceFile);
GraphicTraceFile = -1;
}
void SmartDiscardTrace(FILE *f);
bool OpenTraceFile()
{//Return true if file successfully.
i32 i=0, f;
bool result = false;
const char *folder;
#ifdef _LINUX
for (f=0, folder="traces/"; f<2; f++, folder="")
#else
for (f=0, folder="\\traces\\"; f<2; f++, folder="")
#endif
{
for (i=0; i<MaxTraceFiles; i++)
{
char filename[100];
sprintf(filename,"%sTRACE%03d.txt",folder,i);
TraceFile = OPEN(filename,"r");
if (TraceFile < 0)
{
char msg[100];;
TraceFile = CREATE(filename,"w", false);
if (TraceFile < 0)
{
if (f==0) break;
sprintf(msg,"Cannot open file %s for Trace",filename);
UI_MessageBox(msg,
"",MESSAGE_OK);
}
else
{
result = true;
};
break;
}
else
{
CLOSE (TraceFile);
};
};
if (result) break;
};
if (i==MaxTraceFiles)
UI_MessageBox("Too many Trace files",
"",MESSAGE_OK);
return result;
};
bool OpenGraphicTraceFile()
{//Return true if file successfully.
GraphicTraceFile = OPEN("GraphicTrace.txt","w");
if (GraphicTraceFile < 0) return false;
GraphicTraceActive = true;
return true;
}
i32 millisecondsPerVBL = 17;
i32 VBLperTimer = 10;
bool TimerTraceActive=false;
#ifndef _MSVC_INTEL
bool DSATraceActive = false;
#endif
bool AttackTraceActive = false;
bool AITraceActive = false;
bool GraphicTraceActive = false;
i32 traceViewportDrawing = 0;
i32 GameMode=0;
//i32 clockSpeed = 10;
bool BeginRecordOK = false;
bool RepeatGame = false;
bool GameIsComplete;
bool ItemsRemainingOK = false;
bool gameFrozen = false;
i32 DrawViewportCount = 0;
DOESNOTEXIST DoesNotExist;
bool DiskMenuNeeded;
i32 MostRecentlyAdjustedSkills[2] = {0,0}; //Just for Player display
i32 LatestSkillValues[2] = {0,0}; //Just for Player display.
i32 AdjustedSkillNumber = 0;
bool AutoEnlarge = false;
bool ThreeDMovements = false; //For 3DMaze
bool GravityGame = false; //For 3DMaze
unsigned char cipherkey[64];
unsigned char *encipheredDataFile = NULL;//cipherkey;
bool simpleEncipher = false;
i32 nextGravityMoveTime;
char moveHistory[4] = {0,1,2,0};
bool gravityMove = false;
i32 ExtendedFeaturesSize = 0;
char ExtendedFeaturesVersion = '@';
bool NoSound = false;
bool NoSleep = false;
bool DM_rules = false;
bool indirectText = false;
i32 totalMoveCount;
bool version90Compatible = false;
i32 RememberToPutObjectInHand = -1;
i32 segSize[5] = {0x02,0x02,0x02,0x02,0x00};
i32 segSrcX[5] = {0x00,0x00,0xe0,0x00,0x00};
i32 segSrcY[5] = {0x21,0x00,0x21,0xa9,0x00};
i32 segWidth[5] = {0xe0, 320,0x60, 320,0x02};
i32 segHeight[5] = {0x88,0x21,0x88,0x1f,0x02};
i32 segX[5] = {0x00,0x00,0xe0,0x00,0x20};
i32 segY[5] = {0x21,0x00,0x21,0xa9,0x31};
i32 *videoSegSize = segSize;
i32 *videoSegSrcX = segSrcX;
i32 *videoSegSrcY = segSrcY;
i32 *videoSegWidth = segWidth;
i32 *videoSegHeight = segHeight;
i32 *videoSegX = segX;
i32 *videoSegY = segY;
char *gameInfo = NULL;
i32 gameInfoSize; //Binary information (includes the trailing NUL)
char hintKey[8];
SPEEDTABLE speedTable[SPEED_NUMSPEED];
SPEEDS gameSpeed = SPEED_NORMAL;
VOLUMETABLE volumeTable[VOLUME_NUMVOLUME];
VOLUMES gameVolume = VOLUME_FULL;
bool playerClock = false;
bool extraTicks = true;
bool usingDirectX = true;
//Locations in Atari screen pixel units.
// Viewport, Portraits, Spells&Commands, Text
void TranslateFullscreen(i32 a, i32 b, i32& x, i32& y)
{//Translate physical pixel units on actual screen.
// to pixel units on Atari screen
i32 i, left, top, right, bottom;
for (i=0; i<4; i++)
{
if (videoSegSize[i] == 0) continue;
i32 size = videoSegSize[i];
left = videoSegSrcX[i];
top = videoSegSrcY[i];
right = left + videoSegWidth[i]-1;
bottom = top + videoSegHeight[i]-1;
if (a < videoSegX[i]) continue;
if (a >= videoSegX[i] + size*(videoSegWidth[i])) continue;
if (b < videoSegY[i]) continue;
if (b >= videoSegY[i] + size*(videoSegHeight[i])) continue;
x = (a - videoSegX[i] + size/2)/size + left;
y = (b - videoSegY[i] + size/2)/size + top;
if (x > right-1) x = right-1;
if (y > bottom-1) y = bottom-1;
return;
};
}
bool GetVideoRectangle(i32 i, RECT *rect)
{
i32 j;
if (i>3) return false;
for (j=0; j<4; j++)
{
if (videoSegSize[j] == 0) continue;
if (i == 0)
{
rect->top = videoSegY[j];
rect->left = videoSegX[j];
rect->right = videoSegX[j] + videoSegSize[j]*videoSegWidth[j];
rect->bottom = videoSegY[j] + videoSegSize[j]*videoSegHeight[j];
return true;
};
i--;
};
return false;
}
/*
SDLIST::~SDLIST()
{
SD *temp;
while (psd != NULL)
{
temp = psd;
psd = psd->pnext;
delete temp;
};
}
SD *SDLIST::newSD(char *name,
i32 minimum,
float level,
float y_intercept,
float slope)
{
SD *temp;
if (strlen(name) > 15) return NULL;
temp = new SD;
temp->pnext = psd;
psd = temp;
strcpy(temp->name,name);
temp->minimum = minimum;
temp->level = level;
temp->y_intercept = y_intercept;
temp->slope = slope;
return temp;
}
SD *SDLIST::First()
{
return psd;
}
SD *SDLIST::Next(SD *ent)
{
return ent->pnext;
}
*/
/*
i32 AddSD(char *name,
i32 min,
float level,
float y_intercept,
float slope)
{
if(sdList.newSD(name,min,level,y_intercept,slope)==NULL) return 1;
return 0;
}
// The list of Smart Discards from the config.txt file
SDLIST sdList;
SDENT sdTable[199];
*/
DATABASES db;
// *********************************************************
// Object Database class member functions
// *********************************************************
i32 dbEntrySizes[16] =
{
sizeof (DB0),
sizeof (DB1),
sizeof (DB2),
sizeof (DB3),
sizeof (DB4),
sizeof (DB5),
sizeof (DB6),
sizeof (DB7),
sizeof (DB8),
sizeof (DB9),
sizeof (DB10),
sizeof (DB11),
sizeof (DB12),
sizeof (DB13),
sizeof (DB14),
sizeof (DB15)
};
const char *dbNames[16] =
{
"Doors", //0
"Teleporters", //1
"Scrolls", //2
"Actuators", //3
"Monsters", //4
"Weapons", //5
"Clothes", //6
"Scrolls", //7
"potions", //8
"chests", //9
"food, misc", //10
"??????", //11
"??????", //12
"??????", //13
"Missiles", //14
"Clouds" //15
};
DATABASES::DATABASES ()
{
i32 i;
for (i=0; i<16; i++)
{
m_Address[i] = NULL;
m_numEnt[i] = 0;
};
}
DATABASES::~DATABASES ()
{
i32 i;
for (i=0; i<16; i++) DeAllocate(i);
}
RN::RN(ui32,ui32,ui32)
{
NotImplemented(0x7cd0);
}
RN::RN(DBTYPE dbType, ui32 dbIndex)
{
i32 emptyIndexEntry;
i32 i, newDML;
DBCOMMON *pDBC;
emptyIndexEntry = -1;
for (i=dataMapLength-1; i>0; i--)
{
if (dataIndexMap[i] == 0xffff)
{
emptyIndexEntry = i;
continue;
};
if ( (dataTypeMap[i] & 15) != dbType) continue;
if (dataIndexMap[i] == dbIndex)
{
indirectIndex = (ui16)i;
return; //Already exists
};
pDBC = db.GetCommonAddress(dbType, dataIndexMap[i]);
if (pDBC->link() == RNnul)
{
emptyIndexEntry = i;
continue;
};
};
if (emptyIndexEntry == -1)
{
if (dataMapLength > 64000)
{
UI_MessageBox("Object Indirect Index Overflow","Error",MB_OK);
die(0x22dde5);
};
newDML = 32*(dataMapLength/32) + 33;
dataTypeMap = (ui8 *)UI_realloc(dataTypeMap, newDML, MALLOC062);
dataIndexMap = (ui16 *)UI_realloc(dataIndexMap, 2*newDML, MALLOC063);
if ( (dataTypeMap == NULL) || (dataIndexMap == NULL))
{
UI_MessageBox("Cannot allocate memory for object index",
"Error", MB_OK);
die(0xa1c445);
};
emptyIndexEntry = dataMapLength;
dataMapLength++;
};
indirectIndex = (ui16)emptyIndexEntry;
dataIndexMap[emptyIndexEntry] = (ui16)dbIndex;
dataTypeMap[emptyIndexEntry] = (ui8)dbType;
return;
}
RN::RN(RNVAL i)
{
if ( (i < 0xff80)
&& (i != 0))
{
UI_MessageBox("Illegal RNVAL","Sys Error",MB_OK);
die(0xace321);
};
indirectIndex = (ui16)i;
};
bool RN::checkIndirectIndex() const
{
if ( (indirectIndex < dataMapLength)
&& (indirectIndex > 0) )return true;
UI_MessageBox("Illegal object Indirect Index","error",MB_OK);
#ifdef _DEBUG
die(0x6ccee);
#endif
return false;
}
bool RN::checkIndirectIndex(ui32 i) const
{
if ( (i < (unsigned)dataMapLength)
&& (i > 0) )return true;
return false;
}
void RN::CreateSpell(i32 n)
{
ASSERT(n<128,"spell");
indirectIndex = (ui16)(0xff80 + n);
}
ui8 RN::GetSpellType()
{
if (indirectIndex < 0xff80)
{
UI_MessageBox("Illegal Spell object","Error",MB_OK);
die(0xc8d9a3);
};
return (ui8)(indirectIndex & 0x7f);
}
bool RN::NonMaterial()
{
DBTYPE dbType;
DB4 *pDB_4;
dbType = this->dbType();
ASSERT(dbType == dbMONSTER,"monster");
if (dbType != dbMONSTER) return false;
pDB_4 = GetRecordAddressDB4(*this);
ASSERT(pDB_4->monsterType() < 27,"monsterType");
return d.MonsterDescriptor[pDB_4->monsterType()].nonMaterial();
}
// TAG009308
OBJ_DESC_INDEX RN::DescIndex() const
{
//UNKNOWN *unkA3;
i32 DBType;
OBJ_DESC_INDEX result;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//unkA3 = GetRecordAddress(object);
DBType = -1;
if (*this != RNempty) DBType = dbType();
switch (DBType)
{
case dbSCROLL:
result = objDI_Scroll;
break;
case dbCHEST:
result = (OBJ_DESC_INDEX)(BITS1_2(GetRecordAddressDB9(*this)->word4())+objDI_Chest);
break;
case dbMISC:
result = (OBJ_DESC_INDEX)(GetRecordAddressDB10(*this)->miscType() + objDI_Compass);
break;
case dbWEAPON:
result = (OBJ_DESC_INDEX)(GetRecordAddressDB5(*this)->weaponType() + objDI_EyeOfTime);
break;
case dbCLOTHING:
//result is object index
result = (OBJ_DESC_INDEX)(GetRecordAddressDB6(*this)->clothingType() + objDI_Cape);
break;
case dbPOTION:
//result is object index.
result = (OBJ_DESC_INDEX)(GetRecordAddressDB8(*this)->potionType() + objDI_MonPotionA);
break;
default:
result = (OBJ_DESC_INDEX)(-1);
};
return result;
}
// *********************************************************
//
// *********************************************************
// TAG001188
OBJ_NAME_INDEX RN::NameIndex() const
{
dReg D0;
OBJ_NAME_INDEX objNID7;
DBCOMMON *dbA3;
objNID7 = GetBasicObjectType(*this);
if (objNID7 == objNI_NotAnObject) return objNID7;
// Firstly, if it is not a modifiable object or
// a flask of some sort, then return it as is.
if ( (objNID7 > objNI_LastModifiableObject)
|| (objNID7 < objNI_FirstModifiableObject) )
{
if ( (objNID7 < objNI_FirstFullFlask) || (objNID7 > objNI_LastFullFlask) )
{ // Not a filled flask
if (objNID7 != objNI_EmptyFlask) return objNID7;
};
};
dbA3 = GetCommonAddress(*this);
if (objNID7 == objNI_Compass_N)
{
objNID7 = (OBJ_NAME_INDEX)(objNID7 + d.partyFacing);
return objNID7;
};
if (objNID7 == objNI_Torch_a)
{
//D0W = dbA3->word(2);
//D0W = BITS15_15(D0W);
D0W = (i16)dbA3->CastToDB5()->litTorch();
if (D0W)
{
D0W = (i16)dbA3->CastToDB5()->charges();
D0W = (UI8)(d.Byte640[D0W]);
objNID7 = (OBJ_NAME_INDEX)(objNID7 + D0W);
};
return objNID7;
};
if (objNID7 == objNI_OpenScroll)
{
D0W = (i16)dbA3->CastToDB7()->open();
if (!D0W)
{
objNID7 = objNI_Scroll;
};
return objNID7;
};
if ( (objNID7==objNI_Waterskin)
|| (objNID7==objNI_Illumulet_a)
|| (objNID7==objNI_JewelSymal_a) )
{
D0W = (i16)dbA3->CastToDB10()->value();
if (D0W)
{
objNID7 = (OBJ_NAME_INDEX)(objNID7 + 1);
};
return objNID7;
};
if ( (objNID7 == objNI_Storm_a)
|| (objNID7 == objNI_Flamitt_a)
|| (objNID7 == objNI_StormRing_a)
|| (objNID7 == objNI_RABlade_a)
|| (objNID7 == objNI_EyeOfTime_a)
|| (objNID7 == objNI_StaffOfClaws_a) )
{
D0W = (i16)dbA3->CastToDB5()->charges();
if (D0W)
{
objNID7 = (OBJ_NAME_INDEX)(objNID7 + 1);
};
};
return objNID7;
}
bool RN::IsAKey() const
{
OBJ_NAME_INDEX objNI;
if ( dbType() != dbMISC ) return false;
objNI = NameIndex();
if (objNI < objNI_FirstKey) return false;
if (objNI > objNI_LastKey) return false;
return true;
}
bool RN::Levitating()
{
DBTYPE dbType;
DB4 *pDB_4;
dbType = this->dbType();
ASSERT(dbType == dbMONSTER,"levitate");
if (dbType != dbMONSTER) return false;
pDB_4 = GetRecordAddressDB4(*this);
ASSERT(pDB_4->monsterType() < 27,"monsterType");
return d.MonsterDescriptor[pDB_4->monsterType()].levitating();
}
ui8 RN::VerticalSize()
{
DBTYPE dbType;
DB4 *pDB_4;
dbType = this->dbType();
ASSERT(dbType == dbMONSTER,"vsize");
if (dbType != dbMONSTER) return 1;
pDB_4 = GetRecordAddressDB4(*this);
ASSERT(pDB_4->monsterType() < 27,"monsterType");
return d.MonsterDescriptor[pDB_4->monsterType()].verticalSize();
}
const RN DBCOMMON::link()
{
return m_link;
}
RN *DBCOMMON::pLink()
{
return &m_link;
}
void DBCOMMON::link(RN rn)
{
m_link = rn;
}
DBCOMMON *DATABASES::GetCommonAddress(RN object)
{
i32 dbNum, index;
dbNum = object.dbNum();
index = object.idx();
ASSERT(index < m_numEnt[dbNum],"numEnt");
return (DBCOMMON *)
(((char *)m_Address[dbNum]) + index * dbEntrySizes[dbNum]);
}
DBCOMMON *DATABASES::GetCommonAddress(DBTYPE dbType, i32 i)
{
i32 dbNum, index;
dbNum = dbType;
index = i;
ASSERT(index < m_numEnt[dbNum],"numEnt");
return (DBCOMMON *)
(((char *)m_Address[dbNum]) + index * dbEntrySizes[dbType]);
}
void DATABASES::SetAddress(DBTYPE dbType, DBCOMMON *m)
{
i32 dbNum;
dbNum = dbType;
m_Address[dbNum] = m;
}
i32 DATABASES::NumEntry(i32 dbNum)
{
return m_numEnt[dbNum];
}
void DATABASES::NumEntry(i32 dbNum, i32 n)
{
m_numEnt[dbNum] = n;
d.dungeonDatIndex->DBSize(dbNum, (ui16)n);
}
void DATABASES::Allocate(i32 dbNum, i32 numEnt)
{
DeAllocate(dbNum); // In case something is there already.
ASSERT((dbNum & 0xfffffff0) == 0, "dbnum");
dbNum &= 15;
//ASSERT(numEnt <= ((dbNum==15) ? 768 : (dbNum==3)?16000:8000), "database overflow");
ASSERT(numEnt <= Max(dbNum), "database overflow");
m_numEnt[dbNum] = numEnt;
if (numEnt > 0)
{
m_Address[dbNum] = (DBCOMMON *)UI_malloc(dbEntrySizes[dbNum] * numEnt, MALLOC047);
if (m_Address[dbNum]==NULL)
{
UI_MessageBox("Cannot allocate memory",NULL,MESSAGE_OK);
die (0xbad);
};
pnt p = (pnt)m_Address[dbNum];
for (i32 i=0; i<m_numEnt[dbNum]; i++)
{
memset(p,0,dbEntrySizes[dbNum]);
*(RN *)p = RNnul; // Mark entry as empty
p += dbEntrySizes[dbNum];
}
}
else
{
m_Address[dbNum] = NULL;
};
}
void DATABASES::DeAllocate(i32 dbNum)
{
ASSERT((dbNum & 0xfffffff0) == 0, "dbnum");
dbNum &= 15;
if (m_Address[dbNum] != NULL) UI_free(m_Address[dbNum]);
m_Address[dbNum] = NULL;
m_numEnt[dbNum] = 0;
}
i32 DATABASES::Enlarge(i32 dbNum)
{ // Returns index of newly available entry or -1
i32 max, iNew, i;
i32 result;
i32 newSize;
DBCOMMON *pC;
char msg[1000];
ASSERT( (dbNum & 0xfffffff0) == 0,"dbnum");
if (m_noEnlarge & (1 << dbNum)) return RNnul;
// max = (dbNum==15) ? 1000 : ((dbNum==3)?16000:8000);
max = Max(dbNum);
if (m_numEnt[dbNum] >= max)
{
return -1;//Already as big as possible.
};
sprintf(msg,
"Database Number %d (containing %s)\n"
"has become full. There is a rather arbitrary\n"
"limit of %d items in this database. The\n"
"original Atari program handled this by\n"
"discarding items at random. It is possible\n"
"to increase the size to %d but of course this\n"
"will be 'non-standard'.\n"
"\n"
"Do you want me to enlarge the database?"
,dbNum
,dbNames[dbNum]
,m_numEnt[dbNum]
,max);
AutoEnlarge = true; // ***************** PRS 26 Octover 2002
if (AutoEnlarge) result = MESSAGE_IDYES;
else result = UI_MessageBox(msg,NULL,MESSAGE_YESNO);
//if (result == MESSAGE_IDNO)
//{
// m_noEnlarge |= 1 << dbNum;
// return -1;
//};
newSize = m_numEnt[dbNum] + 100;
m_Address[dbNum] =
(DBCOMMON *)UI_realloc(m_Address[dbNum],
newSize*dbEntrySizes[dbNum],
MALLOC064);
if (m_Address[dbNum] == NULL)
{
UI_MessageBox("Cannot allocate memory",NULL,MESSAGE_OK);
die(0);
};
//d.dungeonDatIndex[dbType+6] = sw(max);
d.dungeonDatIndex->DBSize(dbNum, uw(newSize));
// We need to clear all the entries.
iNew = m_numEnt[dbNum]; // First entry to clear
m_numEnt[dbNum] = newSize;
for (i=iNew; i<newSize; i++)
{
//pC = GetCommonAddress(RN(0,dbNum,i));
pC = (DBCOMMON *)((pnt)m_Address[dbNum] + i*dbEntrySizes[dbNum]);
memset((char *)pC,0,dbEntrySizes[dbNum]);
pC->link(RNnul);
};
//pC = GetCommonAddress(RN(0,dbNum,iNew));
pC = (DBCOMMON *)((pnt)m_Address[dbNum] + iNew*dbEntrySizes[dbNum]);
pC->link(RNeof);
return iNew;
}
DBTYPE DATABASES::GetDBType(DBCOMMON *pDB)
{
i32 dbNum;
for (dbNum=0; dbNum<16; dbNum++)
{
if ( (pDB >= m_Address[dbNum])
&&(pDB < m_Address[dbNum] + dbEntrySizes[dbNum]/sizeof(*m_Address[0]) * m_numEnt[dbNum])) return (DBTYPE)dbNum;
};
return (DBTYPE)dbNum;
}
bool DATABASES::IsDBType(DBCOMMON *pDB, DBTYPE type)
{
return GetDBType(pDB) == type;
}
i32 DATABASES::GetDBIndex(DBCOMMON *pDB, DBTYPE dbType)
{
return ((char *)pDB - (char *)m_Address[dbType])/dbEntrySizes[dbType];
}
RN DATABASES::AllocateSegment(i32 /*dbNum*/, DBTYPE /*dbType*/)
{
NotImplemented(0x7ea8);
/*
i32 entrySize;
ASSERT(m_Address[dbNum] == NULL);
ASSERT(m_numEnt[dbNum] == 0);
entrySize = dbEntrySizes[dbType];
m_Address[dbNum] = (DBCOMMON *)UI_malloc(2*entrySize);
m_numEnt[dbNum] = 2;
dataMap[dbNum] = dbType;
return RN(0, dbNum, 1);
*/
return RNnul;
}
// TAG00a0b0
RN DATABASES::FindEmptyDBEntry(DBTYPE dbType, bool important)
{//(RN)
// important means to assign even if database is
// nearly full. Only used to assign fallen hero's bones
// as far as I know.
RN result;
i32 newDML, index;
DBCOMMON *pDBC;
i32 entrySize;
i32 i;
i32 emptyIndexEntry, emptyDBEntry;
ASSERT((dbType&0x7fff) < 16,"dbtype");
entrySize = dbEntrySizes[dbType];
emptyDBEntry = -1;
emptyIndexEntry = -1;
for (i=1; i<dataMapLength; i++)
{
if (dataIndexMap[i] == 0xffff)
{
if (emptyIndexEntry < 0)
{
emptyIndexEntry = i;
if (emptyDBEntry != -1) break;
};
continue;
};
if ( (dataTypeMap[i] & 15) != dbType) continue;
pDBC = (DBCOMMON *)((pnt)m_Address[dbType] + entrySize*dataIndexMap[i]);
if (pDBC->link() == RNnul)
{
emptyDBEntry = dataIndexMap[i];
emptyIndexEntry = i;
break;
};
};
if (emptyDBEntry == -1)
{
pDBC = (DBCOMMON *)((pnt)m_Address[dbType] + entrySize);
for (i=1; i<m_numEnt[dbType]; i++)
{
if (pDBC->link() == RNnul)
{
emptyDBEntry = i;
break;
};
pDBC = (DBCOMMON *)((pnt)pDBC + entrySize);
};
};
if (emptyDBEntry == -1)
{
index = db.Enlarge(dbType);
if (index != -1) return RN(dbType,index);
return RNnul;
};
if (emptyIndexEntry == -1)
{
if (dataMapLength > 0xff00 - 100)
{
if (!important) return RNnul;
};
newDML = 32*(dataMapLength/32) + 33;
dataIndexMap = (ui16 *)UI_realloc(dataIndexMap, 2*newDML, MALLOC065);
dataTypeMap = (ui8 *)UI_realloc(dataTypeMap, newDML, MALLOC066);
emptyIndexEntry = dataMapLength;
dataMapLength++;
dataIndexMap[emptyIndexEntry] = 0xffff;
};
dataIndexMap[emptyIndexEntry] = (ui16)emptyDBEntry;
dataTypeMap[emptyIndexEntry] = (ui8)dbType;
pDBC = (DBCOMMON *)((pnt)m_Address[dbType]+emptyDBEntry*entrySize);
ClearMemory((ui8 *)pDBC, entrySize);
pDBC->link(RNeof);
result.ConstructFromInteger(emptyIndexEntry);
return result;
}
void DeleteDBEntry(DBCOMMON *pDB)
{
DBTYPE dbType;
i32 dbIndex;
i32 i, j;
dbType = db.GetDBType(pDB);
if (dbType == dbUNKNOWN) return;
dbIndex = db.GetDBIndex(pDB, dbType);
for (i=0, j=-1; i<dataMapLength; i++)
{
if ((dataTypeMap[i] & 0xf) != dbType) continue;
if (dataIndexMap[i] != dbIndex) continue;
if (j != -1) return; //Cloned!!!