forked from dungeons-of-moria/icmoria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.c
1798 lines (1554 loc) · 52.1 KB
/
save.c
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
/* save.c */
/* code for saving and loading characters */
#include "imoria.h"
#include "master.h"
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void data_exception()
{
/* Data Corruption means character is dead, or save file was -RAK-
screwed with. Keep them guessing as to what is actually wrong.*/
clear_from(1);
prt("Data Corruption Error.",1,1);
prt(" ",2,1);
exit_game();
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//void encrypt_line(ntype line)
//{
// /*{ Encrypts a line of text, complete with a data-check sum-RAK- }*/
// /*{ (original by JWT) }*/
//
// integer i1,i2;
// ntype temp;
//
// i2 = 0;
// for (i1 = 0; i1 < strlen(line); i1++) {
// i2 += line[i1] + i1;
// }
// strcpy(temp, line);
// sprintf(line,"%ld %s",i2,temp);
//
// coder(line);
//};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//void coder(ntype line)
//{
// /*{ Uses XOR function to encode data -RAK- }*/
//
// integer i1, x;
// unsigned long i2,i3,i4;
//
// x = strlen(line);
// for (i1 = 0; i1 < x; i1++) {
// i2 = uint(ord(line[i1]));
// i3 = uint(randint(256)-1);
// i4 = uxor(i2,i3);
// line[i1] = i4;
// }
//};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void sc__open_save_file(FILE **f1, vtype fnam, boolean quick, boolean *flag)
{
vtype out_val;
if (py.flags.dead || quick) {
*flag = true;
if (is_from_file) {
*f1 = (FILE *)fopen(finam,"w");
/*open(f1,file_name:=finam,record_length:=1024,history:=old);*/
} else {
*f1 = (FILE *)fopen(finam,"w");
/*open(f1,file_name:=finam,record_length:=1024,history:=new);*/
}
} else {
prt("Enter Filename:",1,1);
*flag = false;
/*{ Open the user's save file -JWT- }*/
if (get_string(fnam,1,17,60)) {
if (strlen(fnam) == 0) {
strcpy(fnam, finam);
}
if ((is_from_file) && (strcmp(fnam, finam) == 0)) {
*f1 = (FILE *)fopen(fnam,"w");
/*open (f1,FILE_NAME:=fnam,record_length:=1024,history:=old);*/
} else {
*f1 = (FILE *)fopen(fnam,"w");
/*open (f1,FILE_NAME:=fnam,record_length:=1024,history:=new);*/
}
if (*f1 == NULL) {
sprintf(out_val, "Error creating> %s", fnam);
msg_print(out_val);
} else {
*flag = true;
}
} else {
erase_line(1,1);
}/* endif else get_string */
} /*endif dead or quick*/
};
//////////////////////////////////////////////////////////////////////
void sc__open_master(FILE *f1, GDBM_FILE *f2, boolean *flag)
{
/*{ Make an attempt to open the MASTER file -JWT- }*/
if (!master_file_open(f2)) {
msg_print("Error saving character (open MASTER), contact MORIA Wizard.");
*flag = true;
}
};
//////////////////////////////////////////////////////////////////////
void sc__write_master(GDBM_FILE f2, boolean *flag)
{
/*{ Write key to MASTER -KRC- }*/
int i2;
master_key mkey;
master_entry mentry;
boolean result;
mkey.creation_time = PM.creation_time;
mentry.save_count = PM.save_count + 1;
mentry.deaths = PM.deaths;
if (PM.save_count == 0) {
result = master_file_write_new(f2, &mkey, &mentry);
PM.creation_time = mkey.creation_time;
PM.save_count++;
} else {
i2 = master_file_verify(f2, &mkey);
if (i2 == MF_CHAR_MISMATCH) {
result = false;
} else {
result = master_file_write(f2, &mkey, &mentry);
PM.save_count++;
}
}
if (!result) {
msg_print("Error in writing to MASTER.");
}
*flag = result;
};
//////////////////////////////////////////////////////////////////////
void sc__display_status(boolean quick, vtype fnam,ntype out_rec)
{
/*{ Message to player on what is happening}*/
if (!py.flags.dead) {
clear_from(1);
if (!quick) {
put_qio();
} else {
strcpy(fnam, finam);
}
sprintf(out_rec, "Saving character in %s...",fnam);
prt(out_rec,1,1);
}
};
//////////////////////////////////////////////////////////////////////
void sc__write_player_record(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Write out the player record. }*/
integer i1, inven_ctr;
//time_type tim;
treas_ptr curse;
time_t current_time, delta_time;
//quad_type current_time,delta_time;
/*with py.misc do*/
sprintf(out_rec, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %d",
PM.xtr_wgt,PM.account,PM.money[0],PM.money[6],PM.money[5],
PM.money[4],PM.money[3],PM.money[2],PM.money[1],(int)PM.diffic);
encrypt_write(f1, cf_state, out_rec);
/*with py.misc.birth do*/
sprintf(out_rec, "%ld %d %d %d %d",
PM.birth.year,(int)PM.birth.month,(int)PM.birth.day,
(int)PM.birth.hour,(int)PM.birth.secs);
encrypt_write(f1, cf_state, out_rec);
/*with py.misc.cur_age do*/
sprintf(out_rec, "%ld %d %d %d %d",
PM.cur_age.year,(int)PM.cur_age.month,(int)PM.cur_age.day,
(int)PM.cur_age.hour,(int)PM.cur_age.secs);
encrypt_write(f1, cf_state, out_rec);
/*{FUBAR modification for quests}*/
sprintf(out_rec, "%d %d %d %ld %d", (int)PF.quested, (int)PM.cur_quest, (int)PM.quests, PM.claim_check, (int)PF.light_on);
encrypt_write(f1, cf_state, out_rec);
// sys_gettim(¤t_time);
// sub_quadtime(current_time,start_time,&delta_time);
// sys_numtim(&tim,&delta_time);
// add_play_time(&tim,py.misc.play_tm);
/*with tim do*/
// sprintf(out_rec, "%ld %ld %ld %ld %ld %ld %ld",
// (int)tim.years,(int)tim.months,(int)tim.days,(int)tim.hours,
// (int)tim.minutes,(int)tim.seconds,(int)tim.hundredths);
// encrypt_write(f1, out_rec);
current_time = time(NULL);
delta_time = current_time - start_time;
sprintf(out_rec,"%ld",convert_time_to_seconds(&py.misc.play_tm)+delta_time);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",py.misc.name);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",py.misc.race);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",py.misc.sex);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",py.misc.tclass);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",py.misc.title);
encrypt_write(f1, cf_state, out_rec);
for (i1 = 0; i1 < 5; i1++) {
strcpy(out_rec,py.misc.history[i1]);
encrypt_write(f1, cf_state, out_rec);
}
sprintf(out_rec,"%d",(int)py.misc.cheated);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec, "%ld %ld %d %d %d %d %d %d %d %ld %ld %ld %d %d %f",
char_row,char_col,(int)PM.pclass,(int)PM.prace,
(int)PM.age,(int)PM.ht,(int)PM.wt,(int)PM.sc,(int)PM.max_exp,
PM.exp,PM.rep,PM.premium,(int)PM.lev,
(int)PM.max_lev,PM.expfact);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec, "%d %d %d %d %d %d %f %d %f %d %d %d %d %d %d %d %d %d %d %d",
(int)PM.srh,(int)PM.fos,(int)PM.stl,(int)PM.bth,
(int)PM.bthb,
(int)PM.mana,PM.cmana,(int)PM.mhp,PM.chp,
(int)PM.ptohit,(int)PM.ptodam,(int)PM.pac,(int)PM.ptoac,
(int)PM.dis_th,(int)PM.dis_td,(int)PM.dis_ac,(int)PM.dis_tac,
(int)PM.disarm,(int)PM.save,(int)PM.hitdie);
encrypt_write(f1, cf_state, out_rec);
/*change by Dean--inven_ctr calculated from scratch to*/
/*(hopefully) solve some of the get-after-EOF save bugs*/
inven_ctr=0;
curse = inventory_list;
while (curse != NULL) {
curse = curse->next;
inven_ctr++;
}
// sprintf(out_rec, "DBG: got some inventory %ld", inven_ctr);
// prt(out_rec,10,10);
sprintf(out_rec, "%ld %ld %ld %ld %ld %ld %ld %lu",
inven_ctr,inven_weight,equip_ctr,dun_level,
missle_ctr,mon_tot_mult,uand(0xF,turn),
randes_seed);
encrypt_write(f1, cf_state, out_rec);
/*end with py.misc*/
/*with py.flags do*/
sprintf(out_rec, "%d %d",(int)PF.insured, (int)PF.dead);
encrypt_write(f1, cf_state, out_rec);
};
//////////////////////////////////////////////////////////////////////
void sc__write_inventory(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Write out the inventory records. }*/
treas_ptr curse;
curse = inventory_list;
while (curse != NULL) {
sprintf(out_rec,"%c %s",curse->data.tchar,curse->data.name);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %d",(int)curse->is_in,(int)curse->insides);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",curse->data.damage);
encrypt_write(f1, cf_state, out_rec);
//with curse->data do;
sprintf(out_rec,"%d %d %d %d %d %d %d %d %d %ld %ld %d %ld",
(int)curse->data.tval,(int)curse->data.subval,
(int)curse->data.weight,(int)curse->data.number,
(int)curse->data.tohit,(int)curse->data.todam,
(int)curse->data.ac,(int)curse->data.toac,
(int)curse->data.p1,
curse->data.flags,curse->data.flags2,
(int)curse->data.level,curse->data.cost);
encrypt_write(f1, cf_state, out_rec);
curse = curse->next;
} /* end while curse */
};
//////////////////////////////////////////////////////////////////////
void sc__write_equipment(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Write out the equipment records. }*/
integer i1;
for (i1 = Equipment_min; i1 < EQUIP_MAX; i1++) {
sprintf(out_rec,"%c %s",equipment[i1].tchar,equipment[i1].name);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",equipment[i1].damage);
encrypt_write(f1, cf_state, out_rec);
//with equipment[i1] do;
sprintf(out_rec,"%d %d %d %d %d %d %d %d %d %ld %ld %d %ld",
(int)equipment[i1].tval,(int)equipment[i1].subval,
(int)equipment[i1].weight,(int)equipment[i1].number,
(int)equipment[i1].tohit,(int)equipment[i1].todam,
(int)equipment[i1].ac,(int)equipment[i1].toac,
(int)equipment[i1].p1,
equipment[i1].flags,equipment[i1].flags2,
(int)equipment[i1].level,equipment[i1].cost);
encrypt_write(f1, cf_state, out_rec);
} /* end for i1 */
};
//////////////////////////////////////////////////////////////////////
void sc__write_stats_and_flags(FILE *f1, encrypt_state *cf_state,ntype out_rec)
{
//with py.stat do
sprintf(out_rec,"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
(int)PS.p[STR],(int)PS.c[STR],(int)PS.m[STR],(int)PS.l[STR],
(int)PS.p[INT],(int)PS.c[INT],(int)PS.m[INT],(int)PS.l[INT],
(int)PS.p[WIS],(int)PS.c[WIS],(int)PS.m[WIS],(int)PS.l[WIS],
(int)PS.p[DEX],(int)PS.c[DEX],(int)PS.m[DEX],(int)PS.l[DEX],
(int)PS.p[CON],(int)PS.c[CON],(int)PS.m[CON],(int)PS.l[CON],
(int)PS.p[CHR],(int)PS.c[CHR],(int)PS.m[CHR],(int)PS.l[CHR]);
encrypt_write(f1, cf_state, out_rec);
//with py.flags do
sprintf(out_rec,"%lu %ld %ld %ld %ld %ld %ld %ld %ld %d",
PF.status,PF.blind,PF.confused,
PF.foodc,PF.food_digested,PF.protection,
PF.speed,PF.afraid,
PF.poisoned,(int)PF.see_inv);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%ld %ld %ld %d %d %d %ld",
PF.fast,PF.slow,PF.protevil,
(int)PF.teleport,(int)PF.free_act,(int)PF.slow_digest,
PF.petrification);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %d %d %d %d %d %d",
(int)PF.aggravate,
(int)PF.sustain[STR],(int)PF.sustain[INT],
(int)PF.sustain[WIS],(int)PF.sustain[CON],
(int)PF.sustain[DEX],(int)PF.sustain[CHR]);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %d %d %d %d %d %d",
(int)PF.fire_resist,(int)PF.cold_resist,
(int)PF.acid_resist,(int)PF.regenerate,
(int)PF.lght_resist,(int)PF.ffall,
(int)PF.confuse_monster);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
PF.image,PF.invuln,PF.hero,
PF.shero,PF.blessed,
PF.resist_heat,PF.resist_cold,PF.detect_inv,
PF.word_recall,PF.see_infra,PF.tim_infra);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %ld %ld %ld %ld %ld %ld %ld %ld %ld",
(int)PF.resist_lght,PF.free_time,PF.ring_fire,
PF.protmon,PF.hoarse,PF.magic_prot,
PF.ring_ice,PF.temp_stealth,PF.resist_petri,
PF.blade_ring);
encrypt_write(f1, cf_state, out_rec);
/* end with py.flags */
};
//////////////////////////////////////////////////////////////////////
void sc__write_magic(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
integer i1;
for (i1 = 0; i1 < MAX_SPELLS; i1++) {
//with magic_spell[py.misc.pclass,i1] do
sprintf(out_rec,"%d %d",(int)PSPELL(i1).learned,PSPELL(i1).sexp);
encrypt_write(f1, cf_state, out_rec);
}
};
//////////////////////////////////////////////////////////////////////
void sc__write_dungeon(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Write the important dungeon info and floor -RAK- }*/
integer i1, i2, tot_treasure, tptr, count = 0;
unsigned long xfloor, prevFloor = 999999;
sprintf(out_rec,"%ld %ld %ld %ld",cur_height,cur_width,
max_panel_rows,max_panel_cols);
encrypt_write(f1, cf_state, out_rec);
/*{ Save the floor }*/
tot_treasure = 0;
for (i1 = 1; i1 <= cur_height; i1++) {
//out_rec = pad(' ',' ',cur_width);
for (i2 = 1; i2 <= cur_width; i2++) {
//with cave[i1][i2]. do;
xfloor = cave[i1][i2].fval;
if (cave[i1][i2].fopen) {
xfloor |= 0x20;
}
if (cave[i1][i2].pl) {
xfloor |= 0x40;
}
if (cave[i1][i2].fm) {
xfloor |= 0x80;
}
if (cave[i1][i2].tptr > 0) {
tot_treasure++;
}
/* run length encoding the floor cuts about 30k from the save file */
if (xfloor == prevFloor) {
count++;
} else {
if (count != 0) {
if (count == 1) {
sprintf(out_rec, "%ld", prevFloor);
} else {
sprintf(out_rec, "%ld %ld", prevFloor, count);
}
encrypt_write(f1, cf_state, out_rec);
}
prevFloor = xfloor;
count = 1;
}
} /* end for i2 */
} /* end for i1 */
if (count == 1) {
sprintf(out_rec, "%ld", prevFloor);
} else {
sprintf(out_rec, "%ld %ld", prevFloor, count);
}
encrypt_write(f1, cf_state, out_rec);
/*{ Save the Treasure List }*/
sprintf(out_rec,"%ld",tot_treasure);
encrypt_write(f1, cf_state, out_rec);
for (i1 = 1; i1 <= cur_height; i1++) {
for (i2 = 1; i2 <= cur_width; i2++) {
if (cave[i1][i2].tptr > 0) {
tptr = cave[i1][i2].tptr;
//with t_list[tptr]. do;
sprintf(out_rec,"%ld %ld",i1,i2);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%c %s",t_list[tptr].tchar,t_list[tptr].name);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",t_list[tptr].damage);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %ld %d %d %d %d %d %d %ld %ld %ld %d %ld",
t_list[tptr].tval,t_list[tptr].subval,t_list[tptr].weight,
t_list[tptr].number,t_list[tptr].tohit,t_list[tptr].todam,
t_list[tptr].ac,t_list[tptr].toac,t_list[tptr].p1,
t_list[tptr].flags,t_list[tptr].flags2,t_list[tptr].level,
t_list[tptr].cost);
encrypt_write(f1, cf_state, out_rec);
}
}
}
};
//////////////////////////////////////////////////////////////////////
void sc__write_identified(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Save identified list }*/
integer i1;
for (i1 = 1; i1 <= MAX_OBJECTS; i1++) {
if (object_ident[i1]) {
out_rec[i1-1] = 'T';
} else {
out_rec[i1-1] = 'F';
}
}
out_rec[MAX_OBJECTS] = 0;
encrypt_write(f1, cf_state, out_rec);
};
//////////////////////////////////////////////////////////////////////
void sc__write_monsters(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Save the Monster List }*/
integer i1, tot_monsters;
for (i1=muptr,tot_monsters = 0; i1 > 0; i1 = m_list[i1].nptr) {
tot_monsters++;
}
sprintf(out_rec,"%ld",tot_monsters);
encrypt_write(f1, cf_state, out_rec);
for (i1 = muptr; i1 > 0; i1 = m_list[i1].nptr) {
// with m_list[i1] do;
sprintf(out_rec,"%d %d %d %d %d %d %d %d %d",
m_list[i1].fy,m_list[i1].fx,m_list[i1].mptr,m_list[i1].hp,
m_list[i1].cspeed,m_list[i1].csleep,m_list[i1].cdis,m_list[i1].ml,
m_list[i1].confused);
encrypt_write(f1, cf_state, out_rec);
}
};
//////////////////////////////////////////////////////////////////////
void sc__write_town(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Save the town level stores }*/
integer i1,i2;
game_time_type st;
sprintf(out_rec,"%ld",town_seed);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%ld %ld %ld %ld %ld %ld %ld",
bank[0],bank[6],bank[5],
bank[4],bank[3],bank[2],bank[1]);
encrypt_write(f1, cf_state, out_rec);
for (i1 = 0; i1 < MAX_STORES; i1++) {
//with stores[i1]. do;
/*{ Save items... }*/
sprintf(out_rec,"%d",stores[i1].store_ctr);
encrypt_write(f1, cf_state, out_rec);
for (i2 = 1; i2 <= stores[i1].store_ctr; i2++) {
//with stores[i1].store_inven[i2].sitem do;
sprintf(out_rec,"%ld",stores[i1].store_inven[i2].scost);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%c %s",
stores[i1].store_inven[i2].sitem.tchar,
stores[i1].store_inven[i2].sitem.name);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%s",stores[i1].store_inven[i2].sitem.damage);
encrypt_write(f1, cf_state, out_rec);
sprintf(out_rec,"%d %ld %d %d %d %d %d %d %ld %ld %ld %d %ld",
stores[i1].store_inven[i2].sitem.tval,
stores[i1].store_inven[i2].sitem.subval,
stores[i1].store_inven[i2].sitem.weight,
stores[i1].store_inven[i2].sitem.number,
stores[i1].store_inven[i2].sitem.tohit,
stores[i1].store_inven[i2].sitem.todam,
stores[i1].store_inven[i2].sitem.ac,
stores[i1].store_inven[i2].sitem.toac,
stores[i1].store_inven[i2].sitem.p1,
stores[i1].store_inven[i2].sitem.flags,
stores[i1].store_inven[i2].sitem.flags2,
stores[i1].store_inven[i2].sitem.level,
stores[i1].store_inven[i2].sitem.cost);
encrypt_write(f1, cf_state, out_rec);
//end with store inven;
} //end for i2;
//with stores[i1].store_inven[i2].store_open. do;
//with py.misc do;
st = stores[i1].store_open;
if ((PM.cur_age.year > st.year) ||
((PM.cur_age.year == st.year) &&
((PM.cur_age.month > st.month) ||
((PM.cur_age.month == st.month) &&
((PM.cur_age.day > st.day) ||
((PM.cur_age.day == st.day) &&
((PM.cur_age.hour > st.hour) ||
((PM.cur_age.hour == st.hour) ||
((PM.cur_age.secs > st.secs)))))))))) {
st.year = 0;
st.month = 0;
st.day = 0;
st.hour = 0;
st.secs = 0;
stores[i1].store_open = st;
}
//with store_open do;
sprintf(out_rec,"%d %d %ld %d %d %d %d",
stores[i1].owner,stores[i1].insult_cur,st.year,
st.month,st.day,st.hour,st.secs);
encrypt_write(f1, cf_state, out_rec);
} //end for i1;
};
//////////////////////////////////////////////////////////////////////
void sc__write_version(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/*{ Version number of Moria }*/
sprintf(out_rec,"%3.2f",CUR_VERSION);
encrypt_write(f1, cf_state, out_rec);
};
//////////////////////////////////////////////////////////////////////
void sc__write_seeds(FILE *f1, encrypt_state *cf_state, ntype out_rec)
{
/* creation_time, save_count and deaths are in the master file, be sure
to fix up char_restore if you move more onto this line */
unsigned long save_seed;
save_seed = get_seed();
save_seed ^= randint(9999999);
sprintf(out_rec,"%lu %ld %ld %ld",save_seed,py.misc.creation_time,
py.misc.save_count,py.misc.deaths);
//sprintf(title2,"%lu %s",save_seed,py.misc.ssn);
set_seed(ENCRYPT_SEED2);
encrypt_write(f1, cf_state, out_rec);
set_seed(save_seed);
};
//////////////////////////////////////////////////////////////////////
boolean save_char(boolean quick)
{
/* { Actual save procedure -RAK- & -JWT- }*/
vtype fnam;
boolean flag;
ntype out_rec;
FILE *f1 = NULL; //: text;
encrypt_state cf_state;
GDBM_FILE f2;
sc__open_save_file(&f1, fnam, quick, &flag);
encrypt_init(&cf_state, saveFileKey, saveFilesAreEncrypted);
//unlink(fnam); /* I don't think this is needed... */
if (flag) {
sc__open_master(f1, &f2, &flag);
}
if (flag) {
sc__write_master(f2, &flag);
master_file_close(&f2);
}
/*{ If ID was written to MASTER, continue saving -RAK- }*/
if (flag) {
sc__write_seeds(f1, &cf_state, out_rec);
sc__display_status(quick, fnam, out_rec);
sc__write_version(f1, &cf_state, out_rec);
sc__write_player_record(f1, &cf_state, out_rec);
// printf("\tgot past player record: %ld\n", flag); fflush(stdout);
sc__write_inventory(f1, &cf_state, out_rec);
sc__write_equipment(f1, &cf_state, out_rec);
sc__write_stats_and_flags(f1, &cf_state, out_rec);
sc__write_magic(f1, &cf_state, out_rec);
sc__write_dungeon(f1, &cf_state, out_rec);
sc__write_identified(f1, &cf_state, out_rec);
sc__write_monsters(f1, &cf_state, out_rec);
sc__write_town(f1, &cf_state, out_rec);
sprintf(out_rec, "%ld", PM.creation_time);
encrypt_write(f1, &cf_state, out_rec);
encrypt_flush(f1, &cf_state);
fclose(f1);
} /* endif flag */
if (flag && !py.flags.dead) {
sprintf(out_rec,"Character saved. [Moria Version %3.2f]\n",CUR_VERSION);
prt(out_rec,2,1);
exit_game();
}
set_seed(get_seed());
return flag;
};
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void gc__add_item(treas_ptr *cur_bag)
{
treas_ptr ptr,curse;
/* {Extensive clarifications and bug fixes here by Dean}*/
ptr = (treas_ptr)safe_malloc(sizeof(treas_rec),"gc__add_item");
if (inventory_list == nil) {
inventory_list = ptr;
} else {
curse = inventory_list;
while (curse->next != nil) {
curse = curse->next;
}
curse->next = ptr;
}
ptr->data = inven_temp->data;
ptr->is_in = inven_temp->is_in;
ptr->insides = inven_temp->insides;
ptr->ok = false;
ptr->next = nil;
if (ptr->data.tval == bag_or_sack) {
*cur_bag = ptr;
}
if (ptr->is_in && (*cur_bag != nil)) {
((*cur_bag)->insides)++;
}
// printf("\n\tgot item: >>>%s<<<\n", ptr->data.name); fflush(stdout);
}
//////////////////////////////////////////////////////////////////////
void gc__open_save_file(FILE **f1, vtype fnam, boolean *paniced)
{
vtype out_str;
*f1 = (FILE *)fopen(fnam,"r");
if (*f1 == NULL) {
sprintf(out_str, "Error Opening> %s", fnam);
prt(out_str,1,1);
prt("",2,1);
*paniced = true;
} else {
rewind(*f1);
}
};
//////////////////////////////////////////////////////////////////////
void gc__open_master(FILE *f1, GDBM_FILE *f2, boolean *paniced)
{
if (!master_file_open(f2)) {
prt("ERROR opening file MASTER.",1,1);
*paniced = true;
}
};
//////////////////////////////////////////////////////////////////////
void gc__read_master(GDBM_FILE f2, boolean *paniced)
{
int result;
master_key mkey;
mkey.creation_time = PM.creation_time;
result = master_file_verify(f2, &mkey);
if (result != MF_CHAR_OK) {
printf("\n\r--------------------------------------------------------\n\r");
printf( "| There was a data corruption error with the character |\n\r");
printf( "| Terminating IMORIA now. |\n\r");
printf( "--------------------------------------------------------\n\r");
// exit_game();
*paniced = true;
}
};
//////////////////////////////////////////////////////////////////////
void gc__read_seeds(FILE *f1, encrypt_state *cf_state, ntype in_rec,
boolean *paniced)
{
/* creation_time, save_count and deaths are in the master file, be sure
to fix up char_restore if you move more onto this line */
unsigned long save_seed;
set_seed(ENCRYPT_SEED2);
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec,"%lu %ld %ld %ld",&save_seed, &py.misc.creation_time,
&py.misc.save_count, &py.misc.deaths) != 4) {
*paniced = true;
}
// strcpy(temp,in_rec+13);
// py.misc.ssn = temp;
// set_seed(ENCRYPT_SEED1);
// coder(temp);
// temp_id = temp;
set_seed(save_seed);
};
//////////////////////////////////////////////////////////////////////
void gc__display_status(vtype fnam, ntype in_rec)
{
prt("Restoring Character...",1,1);
put_qio();
};
//////////////////////////////////////////////////////////////////////
void gc__read_version(FILE *f1, encrypt_state *cf_state,ntype in_rec,
boolean *paniced,real *save_version)
{
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec,"%f",save_version) != 1) {
*paniced = true;
}
if ( (int)(*save_version*100.0) != (int)(CUR_VERSION*100.0)) {
prt("Save file is incompatible with this version.",2,1);
sprintf(in_rec," [Save file version %3.2f]",*save_version);
prt(in_rec,3,1);
sprintf(in_rec," [Moria version %3.2f]",CUR_VERSION);
prt(in_rec,4,1);
exit_game();
prt("Updating character for newer version...",5,1);
pause_game(24);
}
};
//////////////////////////////////////////////////////////////////////
void gc__read_player_record(FILE *f1, encrypt_state *cf_state, ntype in_rec,
boolean *paniced, boolean prop, boolean *was_dead)
{
int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18;
integer i1;
time_t old_time;
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld %ld %ld %ld %ld %ld %ld %ld %ld %d",
&PM.xtr_wgt,&PM.account,&PM.money[0],&PM.money[6],&PM.money[5],
&PM.money[4],&PM.money[3],&PM.money[2],&PM.money[1],&x1) != 10) {
*paniced = true;
}
PM.diffic = x1;
/*with py.misc.birth do*/
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld %d %d %d %d",
&PM.birth.year,&x1,&x2,&x3,&x4) != 5) {
*paniced = true;
}
PM.birth.month = x1;
PM.birth.day = x2;
PM.birth.hour = x3;
PM.birth.secs = x4;
/*with py.misc.cur_age do*/
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld %d %d %d %d",
&PM.cur_age.year,&x1,&x2,&x3,&x4) != 5) {
*paniced = true;
}
PM.cur_age.month = x1;
PM.cur_age.day = x2;
PM.cur_age.hour = x3;
PM.cur_age.secs = x4;
/*{FUBAR modification for quests}*/
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%d %d %d %ld %d", &x1, &x2, &x3, &PM.claim_check, &x4)
!=5 ) {
*paniced = true;
}
PF.quested = x1;
PM.cur_quest = x2;
PM.quests = x3;
PF.light_on = x4;
PF.resting_till_full = false;
// sys_gettim(¤t_time);
// sub_quadtime(current_time,start_time,&delta_time);
// sys_numtim(&tim,&delta_time);
// add_play_time(&tim,py.misc.play_tm);
/*with tim do*/
// read_decrypt(f1, cf_state, in_rec, paniced);
// sscanf(in_rec, "%ld %ld %ld %ld %ld %ld %ld",
// (int)tim.years,(int)tim.months,(int)tim.days,(int)tim.hours,
// (int)tim.minutes,(int)tim.seconds,(int)tim.hundredths);
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld", &old_time) != 1) {
*paniced = true;
}
convert_seconds_to_time(old_time, &py.misc.play_tm);
start_time = time(NULL);
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.name, in_rec, sizeof(vtype));
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.race, in_rec, sizeof(vtype));
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.sex, in_rec, sizeof(vtype));
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.tclass, in_rec, sizeof(vtype));
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.title, in_rec, sizeof(vtype));
for (i1 = 0; i1 < 5; i1++) {
read_decrypt(f1, cf_state, in_rec, paniced);
strncpy(py.misc.history[i1], in_rec, sizeof(vtype));
}
read_decrypt(f1, cf_state, in_rec, paniced);
if(sscanf(in_rec,"%d",&x1) != 1) {
*paniced = true;
}
py.misc.cheated |= x1;
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld %ld %d %d %d %d %d %d %d %ld %ld %ld %d %d %f",
&char_row,&char_col,&x1,&x2,
&x3,&x4,&x5,&x6,&x7,
&PM.exp,&PM.rep,&PM.premium,&x8,
&x9,&PM.expfact) != 15) {
*paniced = true;
}
PM.pclass = x1;
PM.prace = x2;
PM.age = x3;
PM.ht = x4;
PM.wt = x5;
PM.sc = x6;
PM.max_exp = x7;
PM.lev = x8;
PM.max_lev = x9;
if (PM.wt > max_allowable_weight()) {
PM.wt = ( 0.9*max_allowable_weight());
} else if (PM.wt < min_allowable_weight()) {
PM.wt = ( 1.10*min_allowable_weight()) ;
}
if (py.misc.pclass == C_WARRIOR) {
py.misc.mr = -10;
} else if ((py.misc.pclass == C_MAGE) || (py.misc.pclass == C_PRIEST)) {
py.misc.mr = 0;
} else {
py.misc.mr = -5;
}
read_decrypt(f1, cf_state, in_rec, paniced);
// sscanf(in_rec, "%ld %ld %f %ld %f %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld",
// &(PM.srh),&(PM.fos),&(PM.stl),&(PM.bth),
// &x1,
// &x2, &(PM.cmana), &x3, &(PM.chp),
// &x4, &x5, &x7, &x7,
// &x8, &x9, &x10, &x11,
// &x12, &x13, &x14);
if (sscanf(in_rec, "%d %d %d %d %d %d %f %d %f %d %d %d %d %d %d %d %d %d %d %d",
&x1,&x2,&x3,&x4,
&x5,
&x6, &(PM.cmana), &x7, &(PM.chp),
&x8, &x9, &x10, &x11,
&x12, &x13, &x14, &x15,
&x16, &x17, &x18) != 20) {
*paniced = true;
}
PM.srh = x1;
PM.fos = x2;
PM.stl = x3;
PM.bth = x4;
PM.bthb = x5;
PM.mana = x6;
PM.mhp = x7;
PM.ptohit = x8;
PM.ptodam = x9;
PM.pac = x10;
PM.ptoac = x11;
PM.dis_th = x12;
PM.dis_td = x13;
PM.dis_ac = x14;
PM.dis_tac = x15;
PM.disarm = x16;
PM.save = x17;
PM.hitdie = x18;
read_decrypt(f1, cf_state, in_rec, paniced);
if (sscanf(in_rec, "%ld %ld %ld %ld %ld %ld %ld %lu",
&inven_ctr,&inven_weight,&equip_ctr,&dun_level,
&missle_ctr,&mon_tot_mult,&turn,
&randes_seed) != 8) {
*paniced = true;
}
/*end with py.misc*/