-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles.pas
1005 lines (739 loc) · 31.5 KB
/
Files.pas
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) 2024 Jeffrey Getzin.
Licensed under the GNU General Public License v3.0 with additional terms.
See the LICENSE file in the repository root for details.
*)
[Inherit ('Ranges','Types','SMGRTL','LibRtl','STRRTL')]Module Files;
Const
Items_Filename = 'Items.dat;1';
Log_Filename = 'Stone_Data:Stone_Log.dat;1';
Messages_Filename = 'Messages.dat;1';
Monsters_Filename = 'Monsters.Dat;1';
Treasure_Filename = 'Treasure.Dat;1';
Var
ScoresFile: [External]Score_File;
MazeFile: [External]LevelFile;
TreasFile: [External]Treas_File;
Char_File: [External]Character_File; { Character records }
Item_File: [External]Equip_File; { Item records }
Monster_File: [External]Monst_File; { Monster records }
PicFile: [External]Picture_File_Type; { Pictures }
Message_File: [External]Text; { Game text }
SaveFile: [External]Save_File_Type;
AmountFile: [External]Number_File;
LogFile: [External]Packed file of Line;
[External]Function Roll_Die (Die_Type: Integer): [Volatile]Integer;External;
(**********************************************************************************************************************)
(******************************************************** LOG *********************************************************)
(**********************************************************************************************************************)
[Global]Procedure Clear_Log_File;
Begin
{ Open the file }
Repeat
Open (LogFile, Log_Filename, History:=Unknown, Sharing:=READONLY, Error:=CONTINUE);
Until (Status(LogFile)=PAS$K_SUCCESS);
{ Rewrite it }
Rewrite (LogFile, Error:=Continue);
Write (LogFile, '',Error:=Continue);
Close (LogFile ,Error:=Continue);
End;
(**********************************************************************************************************************)
[Global]Function View_log_File (ScenarioDisplay: Unsigned; Pasteboard: Unsigned): boolean;
{ This procedure allows the user to view the log }
Const
Length=20; { Maximum number of lines allowed }
Var
FirstTime: Boolean;
LineCount: Integer;
L: Line;
Rendition: Unsigned; { Rendition set for line }
[External]Procedure Wait_Key (Time_Out: Integer:=-1);External;
Begin { View Log File }
{ Open the file }
Repeat
Open (LogFile, Log_Filename, History:=UNKNOWN, Sharing:=Readwrite, Error:=CONTINUE)
Until (Status(LogFile)=PAS$K_SUCCESS);
Reset (LogFile);
FirstTime:=True;
LineCount:=0;
SMG$Erase_Display (ScenarioDisplay);
SMG$Begin_Display_Update (ScenarioDisplay);
SMG$Erase_Display (ScenarioDisplay);
SMG$Home_Cursor (ScenarioDisplay);
{ While there are still names on the list }
While Not EOF (LogFile) do
Begin
{ Read a name and print it }
Read (LogFile, L, Error:=Continue);
SMG$Put_Line (ScenarioDisplay, L);
LineCount:=LineCount + 1;
If LineCount=Length then
Begin
LineCount:=0;
Rendition:=1; L:='Press a key for more';
SMG$Set_Cursor_ABS (ScenarioDisplay, 22, 39 - (L.Length div 2));
SMG$Put_Line (ScenarioDisplay, L, 0, Rendition);
SMG$End_Display_Update (ScenarioDisplay);
If FirstTime then
SMG$Paste_Virtual_Display (ScenarioDisplay, Pasteboard, 2, 2);
FirstTime:=False;
Wait_Key;
SMG$Begin_Display_Update (ScenarioDisplay);
SMG$Erase_Display (ScenarioDisplay);
End;
End;
L:='Press a key to continue';
SMG$Put_Chars (ScenarioDisplay, L, 22, 39 - (L.Length div 2), 1, 1);
Close (LogFile, Error:=Continue);
View_Log_File := FirstTime;
End; { View Log File }
(**********************************************************************************************************************)
(**************************************************** MESSAGES ********************************************************)
(**********************************************************************************************************************)
Function Create_New_Messages_File: Message_Group;
Var
Loop: Integer;
returnValue: Message_Group;
Begin { failed to read; create a new one }
returnValue:=Zero;
For Loop:=MIN_MESSAGE_NUMBER to MAX_MESSAGE_NUMBER do
returnValue[Loop]:='';
Open (Message_File, file_name:=Messages_Filename, History:=NEW);
Rewrite (Message_File);
For Loop:=MIN_MESSAGE_NUMBER to MAX_MESSAGE_NUMBER do
Writeln (Message_File, returnValue[Loop]);
Close (Message_File);
Create_New_Messages_File:=returnValue;
End;
(******************************************************************************)
[Global]Function Read_Messages: Message_Group;
{ This procedure reads in the text from the message file }
Var
Loop: Integer;
returnValue: Message_Group;
Begin { Read Messages }
returnValue:=Zero;
Open (Message_File, file_name:=Messages_Filename, History:=READONLY, Error:=CONTINUE, Sharing:=READWRITE);
If (Status(Message_File) = 0) then
Begin { successful read }
Reset (Message_File);
For Loop:=MIN_MESSAGE_NUMBER to MAX_MESSAGE_NUMBER do
Begin
Readln (Message_File, returnValue[Loop],Error:=Continue);
If Not ((Status(Message_File)=PAS$K_SUCCESS) or (Status(Message_File)=PAS$K_EOF)) then
Begin
returnValue[Loop]:='';
End;
End;
Close (Message_File);
Read_Messages:=returnValue;
End { successful read }
Else
Read_Messages:=Create_New_Messages_File;
End; { Read Messages }
{**********************************************************************************************************************}
[Global]Procedure Save_Messages (Messages: Message_Group);
{ This procedure saves the messages to the disk }
Var
Loop: Integer;
Begin { Save Messages }
Open (Message_File, file_name:=Messages_Filename, History:=Unknown, Sharing:=READONLY);
{ TODO: Error handling }
Rewrite (Message_File);
For Loop:=MIN_MESSAGE_NUMBER to MAX_MESSAGE_NUMBER do
Writeln (Message_File, Messages[Loop]);
Close (Message_File);
End; { Save Messages }
(**********************************************************************************************************************)
(**************************************************** MONSTERS ********************************************************)
(**********************************************************************************************************************)
[Global]Procedure Save_Monsters (Monster: List_of_monsters);
{ This procedure will save the updates monster records if the current user is authorized to do so. }
Var
Loop: Integer;
Begin { Save Monsters }
Open (Monster_File, file_name:=Monsters_Filename, History:=OLD, Sharing:=READONLY); { TODO: Error handling }
ReWrite (Monster_File);
For Loop:=MIN_MONSTER_NUMBER to MAX_MONSTER_NUMBER do
Write (Monster_File, Monster[Loop]);
Close (Monster_File);
End; { Save Monsters }
{**********************************************************************************************************************************}
Function Create_New_Monster_file: List_of_monsters;
Var
Loop: Integer;
returnValue: List_of_monsters;
Begin
returnValue:=Zero;
For Loop:=MIN_MONSTER_NUMBER to MAX_MONSTER_NUMBER do
returnValue[Loop]:=Zero;
Open (Monster_File, file_name:=Monsters_Filename, History:=NEW, Sharing:=READONLY);
ReWrite (Monster_File);
For Loop:=MIN_MONSTER_NUMBER to MAX_MONSTER_NUMBER do
Write (Monster_File, returnValue[Loop]);
Close (Monster_File);
Create_New_Monster_file:=returnValue;
End;
(******************************************************************************)
[Global]Function Read_Monsters: List_of_monsters;
{ This procedure will read in the monsters from the file into the array, MONSTERS }
Var
Max_Monsters: Integer;
returnValue: List_of_monsters;
Begin { Read Monsters }
returnValue:=Zero;
Open (Monster_File, file_name:=Monsters_Filename, History:=READONLY, Error:=CONTINUE, Sharing:=READWRITE);
If (Status(Monster_File) = 0) then
Begin { successful read }
Reset (Monster_File);
For Max_Monsters:=MIN_MONSTER_NUMBER to MAX_MONSTER_NUMBER do
Read (Monster_File, returnValue[Max_Monsters]);
Close (Monster_File);
Read_Monsters:=returnValue;
End { successful read }
Else
Read_Monsters:=Create_New_Monster_file;
End; { Read_Monsters }
{**********************************************************************************************************************************}
[Global]Function Get_Monster (Monster_Number: Integer): Monster_Record;
{ This function returns the Monster_Number'th monster from the
Monster_File. }
Begin { Get Monster }
Open (Monster_File, Monsters_Filename, History:=READONLY, Access_Method:=DIRECT, Sharing:=READWRITE, Error:=CONTINUE);
If (Status(Monster_File) = 0) then
Begin
Find (Monster_File, Monster_Number);
Get_Monster:=Monster_File^;
Unlock (Monster_File);
Close (Monster_File);
End
Else
Get_Monster:=Create_New_Monster_file[Monster_Number];
End; { Get Monster }
(**********************************************************************************************************************)
(**************************************************** TREASURE ********************************************************)
(**********************************************************************************************************************)
[Global]Procedure Save_Treasure(Treasure: List_of_Treasures);
{ This procedure will save the updated treasure list if the current user is authorized to do so. }
Var
Loop: Integer;
Begin { Save Treasure }
Open (TreasFile, file_name:=Treasure_Filename, History:=OLD); { TODO: Error handling }
ReWrite (TreasFile);
For Loop:=MIN_TREASURE_NUMBER to MAX_TREASURE_NUMBER do
Write (TreasFile, Treasure[Loop]);
Close (TreasFile);
End; { Save_Treasure }
{**********************************************************************************************************************************}
Function Create_New_Treasure_File: List_of_Treasures;
Var
Loop: Integer;
returnValue: List_of_Treasures;
Begin
returnValue:=Zero;
For Loop:=MIN_TREASURE_NUMBER to MAX_TREASURE_NUMBER do
Begin
returnValue[Loop]:=Zero;
End;
Open (TreasFile, file_name:=Treasure_Filename, History:=NEW);
ReWrite (TreasFile);
For Loop:=MIN_TREASURE_NUMBER to MAX_TREASURE_NUMBER do
Write (TreasFile, returnValue[Loop]);
Close (TreasFile);
Create_New_Treasure_File:=returnValue;
End;
{**********************************************************************************************************************************}
[Global]Function Read_Treasures: List_of_Treasures;
{ This procedure will read in the treasure types }
Var
Loop: Integer;
returnValue: List_of_Treasures;
Begin { Read Treasures }
returnValue:=Zero;
Open (TreasFile, File_Name:=Treasure_Filename, History:=READONLY, Error:=CONTINUE, Sharing:=READONLY);
If (Status(TreasFile) = 0) then
Begin { successful read }
Reset (TreasFile);
For Loop:=MIN_TREASURE_NUMBER to MAX_TREASURE_NUMBER do
Read (TreasFile, returnValue[Loop]);
Close (TreasFile);
Read_Treasures:=returnValue;
End { successful read }
Else
Read_Treasures:=Create_New_Treasure_File;
End; { Read Treasures }
(**********************************************************************************************************************)
(****************************************************** ITEMS *********************************************************)
(**********************************************************************************************************************)
[Global]Procedure Save_Items(Item_List: List_of_Items);
{ This procedure will save the updated item records if the current user is authorized to do so. }
Var
Loop: Integer;
Begin { Save Items }
Open (Item_File, file_name:=Items_Filename, History:=UNKNOWN, Sharing:=READONLY);
ReWrite (Item_File);
For Loop:=MIN_ITEM_NUMBER to MAX_ITEM_NUMBER do
Begin
Item_List[Loop].Item_Number := Loop;
Write (Item_File, Item_List[Loop]);
End;
Close (Item_File);
End; { Save Items }
(******************************************************************************)
Function Create_New_Items_File: List_of_Items;
Var
Loop: Integer;
returnValue: List_of_Items;
Begin
returnValue:=Zero;
For Loop:=MIN_ITEM_NUMBER to MAX_ITEM_NUMBER do
Begin
returnValue[Loop]:=Zero;
returnValue[Loop].Item_Number := Loop;
End;
Open (Item_File, file_name:=Items_Filename, History:=NEW, Organization:=SEQUENTIAL, Access_Method:=SEQUENTIAL);
ReWrite (Item_File);
For Loop:=MIN_ITEM_NUMBER to MAX_ITEM_NUMBER do
Write (Item_File, returnValue[Loop]);
Close (Item_File);
Create_New_Items_File:=returnValue;
End;
(******************************************************************************)
[Global]Function Read_Items: [Volatile]List_of_Items;
{ This procedure will read in the items from the file, and then randomly adjust their prices to simulate increasing and decreasing
values of items in a market place. }
Var
Flux: Real; { Some times you just have to say, "what's the flux?" }
Max_Items, Loop: Integer;
returnValue: List_of_Items;
Begin { Read Items }
returnValue:=Zero;
Open (Item_File, file_name:=Items_Filename, ACCESS_METHOD:=SEQUENTIAL, History:=READONLY, Error:=CONTINUE, Sharing:=READWRITE);
If (Status(Item_File) = 0) then
Begin { successful read }
Reset (Item_File);
For Loop:=MIN_ITEM_NUMBER to MAX_ITEM_NUMBER do
Begin { More data }
Read (Item_File, returnValue[Loop]);
STR$TRIM (returnValue[Loop].Name, returnValue[Loop].Name);
STR$TRIM (returnValue[Loop].True_Name, returnValue[Loop].True_Name);
{ Calculate the price fluctuation, FLUX }
Flux:=returnValue[Loop].Gp_Value;
Flux:=Flux * Roll_Die(10) / 100;
If Roll_Die(2)=2 then
Flux:=Flux*(-1);
{ Add flux to the current price }
returnValue[Loop].Current_Value:=Max(Round(returnValue[Loop].GP_Value + Flux), 1);
End; { More Data }
Close (Item_File);
Read_Items:=returnValue;
End { successful read }
Else
Read_Items:=Create_New_Items_File;
End; { Read Items }
{**********************************************************************************************************************************}
[Global]Function Get_Item (Item_Number: Integer): Item_Record;
{ This function returns the Item_Number'th item from the item_file }
Begin { Get Item }
Open (Item_File, File_Name:=Items_Filename, History:=READONLY, Access_Method:=DIRECT, Sharing:=READWRITE, Error:=Continue);
If (Status(Item_File) = 0) then
Begin
Find (Item_File, Item_Number + 1);
Get_Item:=Item_File^;
Unlock (Item_File);
Close (Item_File);
End
Else
Get_Item:=Create_New_Items_File[Item_Number];
End; { Get Item }
(**********************************************************************************************************************)
(************************************************* STORE QUANTITIES ***************************************************)
(**********************************************************************************************************************)
Procedure Create_New_Quantity_File (Filename: Line);
Var
Loop: Integer;
Begin
Open (AmountFile, file_name:=Filename, History:=NEW, Error:=CONTINUE, Sharing:=READONLY);
If (Status(AmountFile) = 0) then
Begin
ReWrite (AmountFile);
For Loop:=MIN_QUANTITY_NUMBER to MAX_QUANTITY_NUMBER Do
Write (AmountFile, 0);
Close (AmountFile);
End;
End;
(******************************************************************************)
[Global]Procedure Access_Item_Quantity_Record (N: Integer);
{ Finds the Nth item and hold it so that others can't access it until it is UPDATED or UNLOCKED }
Begin
Repeat
Find (AmountFile, N + 1, Error:=CONTINUE)
Until Status(AmountFile)=PAS$K_SUCCESS; { TODO: What if the file does not exist or is corrupted? }
End;
(******************************************************************************)
[Global]Function Item_Count (Item_Number: Integer): [Volatile]Integer;
{ This function assumes that AMOUNTFILE has already been opened for DIRECT access. TODO: Move to Files.pas }
Begin
Access_Item_Quantity_Record (Item_Number);
Item_Count:=AmountFile^;
{ Unlock the record so that others can use it }
Unlock (AmountFile);
End;
(******************************************************************************)
[Global]Function Get_Store_Quantity(slot: Integer): Integer;
Begin
Find(AmountFile, slot + 1);
Get_Store_Quantity:=AmountFile^;
End;
(******************************************************************************)
[Global]Procedure Write_Store_Quantity_Aux(slot: Integer; amount: Integer);
Begin
Find(AmountFile, slot + 1);
AmountFile^:=amount;
Update(AmountFile);
End;
(******************************************************************************)
[Global]Procedure Open_Quantity_File_For_Write;
Const
Filename = 'STORE.DAT;1';
Begin
Open(AmountFile, file_name:=Filename, History:=Unknown, Access_Method:=DIRECT, Sharing:=READWRITE, Error:=CONTINUE);
If (Status(AmountFile) <> 0) then
Begin
Create_New_Quantity_File (Filename);
Open(AmountFile, file_name:=Filename, History:=Unknown, Access_Method:=DIRECT, Sharing:=READWRITE); { This time, crash on failure }
End;
End;
(******************************************************************************)
[Global]Procedure Open_Quantity_File_For_Read;
Const
Filename = 'STORE.DAT;1';
Begin
Open(AmountFile, file_name:=Filename, History:=READONLY, Access_Method:=DIRECT, Sharing:=READWRITE, Error:=CONTINUE);
If (Status(AmountFile) <> 0) then
Begin
Create_New_Quantity_File (Filename);
Open(AmountFile, file_name:=Filename, History:=READONLY, Access_Method:=DIRECT, Sharing:=READWRITE); { This time, crash on failure }
End;
End;
(******************************************************************************)
[Global]Procedure increment_item_quantity(slot: integer);
Begin
Access_Item_Quantity_Record (slot);
If AmountFile^ <> -1 then
Begin
AmountFile^:=AmountFile^ + 1;
Update (AmountFile);
End;
End;
(******************************************************************************)
[Global]Procedure Close_Quantity_File;
Begin
Close (AmountFile);
End;
(******************************************************************************)
[Global]Procedure Write_Store_Quantity(slot: Integer; amount: Integer);
Begin
Open_Quantity_File_For_Write;
Write_Store_Quantity_Aux(slot, amount);
Close(AmountFile);
End;
(******************************************************************************)
[Global]Procedure Decrement_Quantity (slot: Integer);
Begin
Find(AmountFile, slot + 1);
If AmountFile^>0 then
AmountFile^:=AmountFile^ - 1;
Update (AmountFile);
End;
(**********************************************************************************************************************)
(****************************************************** PICTURES ******************************************************)
(**********************************************************************************************************************)
[Global]Procedure Save_Pictures(Pics: Pic_List);
{ This procedure will write an updated set of pictures if the user is authorized to do so. }
Const
Filename = 'Pictures.Dat;1';
Var
Loop: Integer;
Begin { Save Pictures }
Open (PicFile, file_name:=Filename, History:=OLD, Sharing:=READONLY);
ReWrite (PicFile);
For Loop:=MIN_PICTURE_NUMBER to MAX_PICTURE_NUMBER do
Write (PicFile, Pics[Loop]);
Close (PicFile);
End; { Save Pictures }
(******************************************************************************)
Function Create_New_Pictures_File(Filename: Line): Pic_List;
Var
Loop: Integer;
returnValue: Pic_List;
Begin
returnValue:=Zero;
For Loop:=MIN_PICTURE_NUMBER to MAX_PICTURE_NUMBER do
returnValue[Loop]:=Zero;
Open (PicFile, file_name:=Filename, History:=NEW, Error:=CONTINUE, Sharing:=READONLY);
If (Status(PicFile) = 0) then
Begin
ReWrite (PicFile);
For Loop:=MIN_PICTURE_NUMBER to MAX_PICTURE_NUMBER do
Write(PicFile, ReturnValue[Loop]);
Close (PicFile);
Create_New_Pictures_File:=returnValue;
End
Else
Create_New_Pictures_File:=returnValue;
End;
(******************************************************************************)
[Global]Function Read_Pictures: Pic_List;
Const
Filename = 'Pictures.Dat;1';
Var
Loop: Integer;
returnValue: Pic_List;
Begin { Read Pictures }
returnValue:=Zero;
Open (PicFile,
file_name:=Filename,
History:=READONLY,
Error:=CONTINUE,
Sharing:=READWRITE);
If (Status(PicFile) = 0) then
Begin { successful read }
Reset (PicFile, Error:=Continue);
Loop:=MIN_PICTURE_NUMBER;
While (Loop<=MAX_PICTURE_NUMBER) and Not EOF(PicFile) do
Begin
Read(PicFile, returnValue[Loop]);
Loop:=Loop + 1;
End;
Close (PicFile);
Read_Pictures:=returnValue;
End { successful read }
Else
Read_Pictures:=Create_New_Pictures_File(Filename);
End;
(**********************************************************************************************************************)
(******************************************************* ROSTER *******************************************************)
(**********************************************************************************************************************)
[Global]Procedure Write_Roster (Roster: Roster_Type);
{ This procedure is used to write the current roster to the character file }
Const
Filename = 'SYS$LOGIN:Character.Dat;1';
Var
Loop: Integer;
Error: Boolean;
Begin { Write Roster }
Open (Char_File, file_name:=filename, History:=Unknown);
ReWrite (Char_File);
For Loop:=MIN_ROSTER_NUMBER to MAX_ROSTER_NUMBER do
Begin
Write (Char_File, Roster[Loop]);
End;
Close (Char_File);
End; { Write Roster }
{**********************************************************************************************************************************}
Function Create_New_Character_File (filename: line): Roster_Type;
Var
Loop: Integer;
Roster: Roster_Type;
Begin { failed to read; create a new one }
Roster:=Zero;
For Loop:=MIN_ROSTER_NUMBER to MAX_ROSTER_NUMBER do
Roster[Loop].Status:=Deleted;
Open (Char_File, file_name:=Filename, History:=NEW, Error:=CONTINUE, Sharing:=READONLY);
If (Status(Char_File) = 0) then
Begin
ReWrite (Char_File);
For Loop:=MIN_ROSTER_NUMBER to MAX_ROSTER_NUMBER do
Write (Char_File, Roster[Loop]);
Close (Char_File);
Create_New_Character_File:=Roster;
End
Else
Create_New_Character_File:=Roster;
End;
{**********************************************************************************************************************************}
[Global]Function Read_Roster: Roster_Type;
Const
Filename = 'SYS$LOGIN:Character.Dat;1';
Var
Loop: Integer;
Roster: Roster_Type;
Begin { Read Roster }
Roster:=Zero;
Open (Char_File, File_Name:=Filename, History:=READONLY, Error:=CONTINUE, Sharing:=READONLY);
If (Status(Char_File) = 0) then
Begin { successful read }
Reset (Char_File, Error:=Continue);
For Loop:=MIN_ROSTER_NUMBER to MAX_ROSTER_NUMBER do
Read (Char_File, Roster[Loop]);
Close (Char_File);
Read_Roster:=Roster;
End
Else
Read_Roster:=Create_New_Character_File(Filename);
End; { Read Roster }
(**********************************************************************************************************************)
(**************************************************** SAVE FILE *******************************************************)
(**********************************************************************************************************************)
[Global]Procedure Create_Null_SaveFile;
Const
Filename = 'SYS$LOGIN:STONE_SAVE.DAT;1';
Begin
Open (SaveFile, file_name:=filename, History:=Unknown);
ReWrite (SaveFile);
Close (SaveFile);
End;
(******************************************************************************)
[Global]Function Write_Save_File (saveRecord: Save_Record): Boolean;
Var
Error: Boolean;
Begin
Open (SaveFile, 'SYS$LOGIN:STONE_SAVE.DAT;1',HISTORY:=NEW, Error:=Continue);
Error:=(Status(SaveFile)<>PAS$K_SUCCESS);
ReWrite (SaveFile);
Error:=Error or ((Status(SaveFile)<>PAS$K_SUCCESS) and (Status(SaveFile)<>PAS$K_EOF));
Write (SaveFile, saveRecord, Error:=Continue);
Error:=Error or ((Status(SaveFile)<>PAS$K_SUCCESS) and (Status(SaveFile)<>PAS$K_EOF));
Close (SaveFile, Error:=Continue);
Write_Save_File:=Error or ((Status(SaveFile) <> PAS$K_SUCCESS) and (Status(SaveFile) <> PAS$K_EOF));
End;
(**********************************************************************************************************************)
(**************************************************** LEVEL FILE ******************************************************)
(**********************************************************************************************************************)
[Global]Function Get_Maze_File_Name (levelCharacter: Char): Line;
Begin
Get_Maze_File_Name:='MAZE' + levelCharacter + '.DAT;1'
End;
(******************************************************************************)
Function createEmptyLevel (levelNumber: Integer): Level;
Var
returnValue: Level;
x, y: 1..20;
encounterIndex: 1..3;
specialNumber: 0..15;
Begin
returnValue:=Zero;
returnValue.Level_Number:=levelNumber;
for x:=1 to 20 do
for y:=1 to 20 do
begin
returnValue.Room[x][y].North:=Passage;
returnValue.Room[x][y].South:=Passage;
returnValue.Room[x][y].East:=Passage;
returnValue.Room[x][y].West:=Passage;
returnValue.Room[x][y].Contents:=0;
returnValue.Room[x][y].Kind:=Corridor;
end;
for encounterIndex:=1 to 3 do
Begin
returnValue.Monsters[encounterIndex].Base_Monster_Number:=0;
returnValue.Monsters[encounterIndex].Addition.X:=0;
returnValue.Monsters[encounterIndex].Addition.Y:=0;
returnValue.Monsters[encounterIndex].Addition.Z:=0;
returnValue.Monsters[encounterIndex].Probability:=0;
End;
for specialNumber:=0 to 15 do
Begin
returnValue.Special_Table[specialNumber].Pointer1:=0;
returnValue.Special_Table[specialNumber].Pointer2:=0;
returnValue.Special_Table[specialNumber].Pointer3:=0;
returnValue.Special_Table[specialNumber].Special:=Nothing;
End;
createEmptyLevel:=returnValue;
End;
(******************************************************************************)
Function Create_New_Level_File(Var fileVar: LevelFile; filename: Line; levelNumber: Integer): Level;
Var
returnValue: Level;
Begin
Open (fileVar, file_name:=filename, History:=NEW, Error:=CONTINUE, Sharing:=READONLY);
If (Status(fileVar) = 0) then
Begin
returnValue:=createEmptyLevel(levelNumber);
ReWrite(fileVar);
Write (fileVar, returnValue);
Close (fileVar);
Create_New_Level_File:=returnValue;
End { TODO: Handle failure case }
Else
Create_New_Level_File:=createEmptyLevel(levelNumber);
End;
(******************************************************************************)
[Global]Function Read_Level_from_Maze_File(Var fileVar: LevelFile; levelNumber: Integer): Level;
Var
returnValue: Level;
filename: Line;
Begin
filename:=Get_Maze_File_Name(CHR(levelNumber + 64));
Open (fileVar, File_Name:=filename, History:=READONLY, Error:=CONTINUE, Sharing:=READONLY);
If (Status(fileVar) = 0) then
Begin { successful read }
Reset (fileVar);
Read (fileVar, returnValue);
Close (fileVar);
Read_Level_from_Maze_File:=returnValue;
End { successful read }
Else
Read_Level_from_Maze_File:=Create_New_Level_File(fileVar, filename, levelNumber);
End;
(******************************************************************************)
[Global]Procedure Save_Level_to_Maze_File(Var fileVar: LevelFile; filename: Line; Floor: Level);
Begin
Open (MazeFile, file_name:=filename, History:=OLD);
ReWrite (MazeFile);
Write (MazeFile, Floor);
Close (MazeFile);
End;
{**********************************************************************************************************************************}
[Global]Function Get_Level (Level_Number: Integer; Maze: Level; PosZ: Vertical_Type:=0): [Volatile]Level;
{ This function will return a level of the dungeon. If the level of the dungeon is the same as POSZ, i.e., the same level, the
current level will be returned. If POSZ is omitted, this will ALWAYS load a new level even if it's simply loading the same level
as the one in memory }
Begin { Get Level }
If (Level_Number <> PosZ) and (Level_Number > 0) then
Get_Level:=Read_Level_from_Maze_File(MazeFile, Level_Number)
Else
Get_Level:=Maze; { Otherwise, return the current level }
End; { Get Level }
{**********************************************************************************************************************************}
{****************************************************** HIGH SCORE LIST ***********************************************************}
{**********************************************************************************************************************************}
[Global]Function Read_Score_List: High_Score_List;
Var
Score_List: High_Score_List;
Begin
Open(ScoresFile, file_name:='SCORES.DAT', History:=UNKNOWN, Sharing:=READWRITE, Error:=Continue);
If (Status(ScoresFile)=PAS$K_SUCCESS) or (Status(ScoresFile)=PAS$K_EOF) then
Begin
Reset (ScoresFile);
If EOF (ScoresFile) then
Score_List:=Zero
Else
Read (ScoresFile, Score_List);
Close (ScoresFile);
End
Else
Score_List:=Zero;
Read_Score_List := Score_List;
End;
(******************************************************************************)
[Global]Procedure Write_Score_List (Score_List: High_Score_List);
Begin
Open(ScoresFile, file_name:='SCORES.DAT',History:=UNKNOWN, Sharing:=READWRITE, Error:=Continue);
If (Status(ScoresFile)=PAS$K_SUCCESS) or (Status(ScoresFile)=PAS$K_EOF) then
Begin
Rewrite (ScoresFile);
Write (ScoresFile, Score_List);
Close (ScoresFile);
End;
End;
(******************************************************************************)
[Global]Procedure Clear_High_Scores;
Var
Score_List: High_Score_List;