-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathdb.c
More file actions
3027 lines (2479 loc) · 80.9 KB
/
db.c
File metadata and controls
3027 lines (2479 loc) · 80.9 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
/* ************************************************************************
* File: db.c Part of CircleMUD *
* Usage: Loading/saving chars, booting/resetting world, internal funcs *
* *
* All rights reserved. See license.doc for complete information. *
* *
* Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991. *
************************************************************************ */
#define __DB_C__
#include "conf.h"
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "db.h"
#include "comm.h"
#include "handler.h"
#include "spells.h"
#include "mail.h"
#include "interpreter.h"
#include "house.h"
#include "constants.h"
/**************************************************************************
* declarations of most of the 'global' variables *
**************************************************************************/
struct room_data *world = NULL; /* array of rooms */
room_rnum top_of_world = 0; /* ref to top element of world */
struct char_data *character_list = NULL; /* global linked list of
* chars */
struct index_data *mob_index; /* index table for mobile file */
struct char_data *mob_proto; /* prototypes for mobs */
mob_rnum top_of_mobt = 0; /* top of mobile index table */
struct obj_data *object_list = NULL; /* global linked list of objs */
struct index_data *obj_index; /* index table for object file */
struct obj_data *obj_proto; /* prototypes for objs */
obj_rnum top_of_objt = 0; /* top of object index table */
struct zone_data *zone_table; /* zone table */
zone_rnum top_of_zone_table = 0;/* top element of zone tab */
struct message_list fight_messages[MAX_MESSAGES]; /* fighting messages */
struct player_index_element *player_table = NULL; /* index to plr file */
FILE *player_fl = NULL; /* file desc of player file */
int top_of_p_table = 0; /* ref to top of table */
long top_idnum = 0; /* highest idnum in use */
int no_mail = 0; /* mail disabled? */
int mini_mud = 0; /* mini-mud mode? */
int no_rent_check = 0; /* skip rent check on boot? */
time_t boot_time = 0; /* time of mud boot */
int circle_restrict = 0; /* level of game restriction */
room_rnum r_mortal_start_room; /* rnum of mortal start room */
room_rnum r_immort_start_room; /* rnum of immort start room */
room_rnum r_frozen_start_room; /* rnum of frozen start room */
char *credits = NULL; /* game credits */
char *news = NULL; /* mud news */
char *motd = NULL; /* message of the day - mortals */
char *imotd = NULL; /* message of the day - immorts */
char *GREETINGS = NULL; /* opening credits screen */
char *help = NULL; /* help screen */
char *info = NULL; /* info page */
char *wizlist = NULL; /* list of higher gods */
char *immlist = NULL; /* list of peon gods */
char *background = NULL; /* background story */
char *handbook = NULL; /* handbook for new immortals */
char *policies = NULL; /* policies page */
struct help_index_element *help_table = 0; /* the help table */
int top_of_helpt = 0; /* top of help index table */
struct time_info_data time_info;/* the infomation about the time */
struct weather_data weather_info; /* the infomation about the weather */
struct player_special_data dummy_mob; /* dummy spec area for mobs */
struct reset_q_type reset_q; /* queue of zones to be reset */
/* local functions */
int check_bitvector_names(bitvector_t bits, size_t namecount, const char *whatami, const char *whatbits);
int check_object_spell_number(struct obj_data *obj, int val);
int check_object_level(struct obj_data *obj, int val);
void setup_dir(FILE *fl, int room, int dir);
void index_boot(int mode);
void discrete_load(FILE *fl, int mode, char *filename);
int check_object(struct obj_data *);
void parse_room(FILE *fl, int virtual_nr);
void parse_mobile(FILE *mob_f, int nr);
char *parse_object(FILE *obj_f, int nr);
void load_zones(FILE *fl, char *zonename);
void load_help(FILE *fl);
void assign_mobiles(void);
void assign_objects(void);
void assign_rooms(void);
void assign_the_shopkeepers(void);
void build_player_index(void);
int is_empty(zone_rnum zone_nr);
void reset_zone(zone_rnum zone);
int file_to_string(const char *name, char *buf);
int file_to_string_alloc(const char *name, char **buf);
void reboot_wizlists(void);
ACMD(do_reboot);
void boot_world(void);
int count_alias_records(FILE *fl);
int count_hash_records(FILE *fl);
bitvector_t asciiflag_conv(char *flag);
void parse_simple_mob(FILE *mob_f, int i, int nr);
void interpret_espec(const char *keyword, const char *value, int i, int nr);
void parse_espec(char *buf, int i, int nr);
void parse_enhanced_mob(FILE *mob_f, int i, int nr);
void get_one_line(FILE *fl, char *buf);
void save_etext(struct char_data *ch);
void check_start_rooms(void);
void renum_world(void);
void renum_zone_table(void);
void log_zone_error(zone_rnum zone, int cmd_no, const char *message);
void reset_time(void);
long get_ptable_by_name(const char *name);
/* external functions */
void paginate_string(char *str, struct descriptor_data *d);
struct time_info_data *mud_time_passed(time_t t2, time_t t1);
void free_alias(struct alias_data *a);
void load_messages(void);
void weather_and_time(int mode);
void mag_assign_spells(void);
void boot_social_messages(void);
void update_obj_file(void); /* In objsave.c */
void sort_commands(void);
void sort_spells(void);
void load_banned(void);
void Read_Invalid_List(void);
void boot_the_shops(FILE *shop_f, char *filename, int rec_count);
int hsort(const void *a, const void *b);
void prune_crlf(char *txt);
void destroy_shops(void);
/* external vars */
extern int no_specials;
extern int scheck;
extern room_vnum mortal_start_room;
extern room_vnum immort_start_room;
extern room_vnum frozen_start_room;
extern struct descriptor_data *descriptor_list;
extern const char *unused_spellname; /* spell_parser.c */
/*************************************************************************
* routines for booting the system *
*************************************************************************/
/* this is necessary for the autowiz system */
void reboot_wizlists(void)
{
file_to_string_alloc(WIZLIST_FILE, &wizlist);
file_to_string_alloc(IMMLIST_FILE, &immlist);
}
/* Wipe out all the loaded text files, for shutting down. */
void free_text_files(void)
{
char **textfiles[] = {
&wizlist, &immlist, &news, &credits, &motd, &imotd, &help, &info,
&policies, &handbook, &background, &GREETINGS, NULL
};
int rf;
for (rf = 0; textfiles[rf]; rf++)
if (*textfiles[rf]) {
free(*textfiles[rf]);
*textfiles[rf] = NULL;
}
}
/*
* Too bad it doesn't check the return values to let the user
* know about -1 values. This will result in an 'Okay.' to a
* 'reload' command even when the string was not replaced.
* To fix later, if desired. -gg 6/24/99
*/
ACMD(do_reboot)
{
char arg[MAX_INPUT_LENGTH];
one_argument(argument, arg);
if (!str_cmp(arg, "all") || *arg == '*') {
if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0)
prune_crlf(GREETINGS);
file_to_string_alloc(WIZLIST_FILE, &wizlist);
file_to_string_alloc(IMMLIST_FILE, &immlist);
file_to_string_alloc(NEWS_FILE, &news);
file_to_string_alloc(CREDITS_FILE, &credits);
file_to_string_alloc(MOTD_FILE, &motd);
file_to_string_alloc(IMOTD_FILE, &imotd);
file_to_string_alloc(HELP_PAGE_FILE, &help);
file_to_string_alloc(INFO_FILE, &info);
file_to_string_alloc(POLICIES_FILE, &policies);
file_to_string_alloc(HANDBOOK_FILE, &handbook);
file_to_string_alloc(BACKGROUND_FILE, &background);
} else if (!str_cmp(arg, "wizlist"))
file_to_string_alloc(WIZLIST_FILE, &wizlist);
else if (!str_cmp(arg, "immlist"))
file_to_string_alloc(IMMLIST_FILE, &immlist);
else if (!str_cmp(arg, "news"))
file_to_string_alloc(NEWS_FILE, &news);
else if (!str_cmp(arg, "credits"))
file_to_string_alloc(CREDITS_FILE, &credits);
else if (!str_cmp(arg, "motd"))
file_to_string_alloc(MOTD_FILE, &motd);
else if (!str_cmp(arg, "imotd"))
file_to_string_alloc(IMOTD_FILE, &imotd);
else if (!str_cmp(arg, "help"))
file_to_string_alloc(HELP_PAGE_FILE, &help);
else if (!str_cmp(arg, "info"))
file_to_string_alloc(INFO_FILE, &info);
else if (!str_cmp(arg, "policy"))
file_to_string_alloc(POLICIES_FILE, &policies);
else if (!str_cmp(arg, "handbook"))
file_to_string_alloc(HANDBOOK_FILE, &handbook);
else if (!str_cmp(arg, "background"))
file_to_string_alloc(BACKGROUND_FILE, &background);
else if (!str_cmp(arg, "greetings")) {
if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0)
prune_crlf(GREETINGS);
} else if (!str_cmp(arg, "xhelp")) {
if (help_table)
free_help();
index_boot(DB_BOOT_HLP);
} else {
send_to_char(ch, "Unknown reload option.\r\n");
return;
}
send_to_char(ch, "%s", OK);
}
void boot_world(void)
{
log("Loading zone table.");
index_boot(DB_BOOT_ZON);
log("Loading rooms.");
index_boot(DB_BOOT_WLD);
log("Renumbering rooms.");
renum_world();
log("Checking start rooms.");
check_start_rooms();
log("Loading mobs and generating index.");
index_boot(DB_BOOT_MOB);
log("Loading objs and generating index.");
index_boot(DB_BOOT_OBJ);
log("Renumbering zone table.");
renum_zone_table();
if (!no_specials) {
log("Loading shops.");
index_boot(DB_BOOT_SHP);
}
}
void free_extra_descriptions(struct extra_descr_data *edesc)
{
struct extra_descr_data *enext;
for (; edesc; edesc = enext) {
enext = edesc->next;
free(edesc->keyword);
free(edesc->description);
free(edesc);
}
}
/* Free the world, in a memory allocation sense. */
void destroy_db(void)
{
ssize_t cnt, itr;
struct char_data *chtmp;
struct obj_data *objtmp;
/* Active Mobiles & Players */
while (character_list) {
chtmp = character_list;
character_list = character_list->next;
free_char(chtmp);
}
/* Active Objects */
while (object_list) {
objtmp = object_list;
object_list = object_list->next;
free_obj(objtmp);
}
/* Rooms */
for (cnt = 0; cnt <= top_of_world; cnt++) {
if (world[cnt].name)
free(world[cnt].name);
if (world[cnt].description)
free(world[cnt].description);
free_extra_descriptions(world[cnt].ex_description);
for (itr = 0; itr < NUM_OF_DIRS; itr++) {
if (!world[cnt].dir_option[itr])
continue;
if (world[cnt].dir_option[itr]->general_description)
free(world[cnt].dir_option[itr]->general_description);
if (world[cnt].dir_option[itr]->keyword)
free(world[cnt].dir_option[itr]->keyword);
free(world[cnt].dir_option[itr]);
}
}
free(world);
/* Objects */
for (cnt = 0; cnt <= top_of_objt; cnt++) {
if (obj_proto[cnt].name)
free(obj_proto[cnt].name);
if (obj_proto[cnt].description)
free(obj_proto[cnt].description);
if (obj_proto[cnt].short_description)
free(obj_proto[cnt].short_description);
if (obj_proto[cnt].action_description)
free(obj_proto[cnt].action_description);
free_extra_descriptions(obj_proto[cnt].ex_description);
}
free(obj_proto);
free(obj_index);
/* Mobiles */
for (cnt = 0; cnt <= top_of_mobt; cnt++) {
if (mob_proto[cnt].player.name)
free(mob_proto[cnt].player.name);
if (mob_proto[cnt].player.title)
free(mob_proto[cnt].player.title);
if (mob_proto[cnt].player.short_descr)
free(mob_proto[cnt].player.short_descr);
if (mob_proto[cnt].player.long_descr)
free(mob_proto[cnt].player.long_descr);
if (mob_proto[cnt].player.description)
free(mob_proto[cnt].player.description);
while (mob_proto[cnt].affected)
affect_remove(&mob_proto[cnt], mob_proto[cnt].affected);
}
free(mob_proto);
free(mob_index);
/* Shops */
destroy_shops();
/* Zones */
for (cnt = 0; cnt <= top_of_zone_table; cnt++) {
if (zone_table[cnt].name)
free(zone_table[cnt].name);
if (zone_table[cnt].cmd)
free(zone_table[cnt].cmd);
}
free(zone_table);
}
/* body of the booting system */
void boot_db(void)
{
zone_rnum i;
log("Boot db -- BEGIN.");
log("Resetting the game time:");
reset_time();
log("Reading news, credits, help, bground, info & motds.");
file_to_string_alloc(NEWS_FILE, &news);
file_to_string_alloc(CREDITS_FILE, &credits);
file_to_string_alloc(MOTD_FILE, &motd);
file_to_string_alloc(IMOTD_FILE, &imotd);
file_to_string_alloc(HELP_PAGE_FILE, &help);
file_to_string_alloc(INFO_FILE, &info);
file_to_string_alloc(WIZLIST_FILE, &wizlist);
file_to_string_alloc(IMMLIST_FILE, &immlist);
file_to_string_alloc(POLICIES_FILE, &policies);
file_to_string_alloc(HANDBOOK_FILE, &handbook);
file_to_string_alloc(BACKGROUND_FILE, &background);
if (file_to_string_alloc(GREETINGS_FILE, &GREETINGS) == 0)
prune_crlf(GREETINGS);
log("Loading spell definitions.");
mag_assign_spells();
boot_world();
log("Loading help entries.");
index_boot(DB_BOOT_HLP);
log("Generating player index.");
build_player_index();
log("Loading fight messages.");
load_messages();
log("Loading social messages.");
boot_social_messages();
log("Assigning function pointers:");
if (!no_specials) {
log(" Mobiles.");
assign_mobiles();
log(" Shopkeepers.");
assign_the_shopkeepers();
log(" Objects.");
assign_objects();
log(" Rooms.");
assign_rooms();
}
log("Assigning spell and skill levels.");
init_spell_levels();
log("Sorting command list and spells.");
sort_commands();
sort_spells();
log("Booting mail system.");
if (!scan_file()) {
log(" Mail boot failed -- Mail system disabled");
no_mail = 1;
}
log("Reading banned site and invalid-name list.");
load_banned();
Read_Invalid_List();
if (!no_rent_check) {
log("Deleting timed-out crash and rent files:");
update_obj_file();
log(" Done.");
}
/* Moved here so the object limit code works. -gg 6/24/98 */
if (!mini_mud) {
log("Booting houses.");
House_boot();
}
for (i = 0; i <= top_of_zone_table; i++) {
log("Resetting #%d: %s (rooms %d-%d).", zone_table[i].number,
zone_table[i].name, zone_table[i].bot, zone_table[i].top);
reset_zone(i);
}
reset_q.head = reset_q.tail = NULL;
boot_time = time(0);
log("Boot db -- DONE.");
}
/* reset the time in the game from file */
void reset_time(void)
{
time_t beginning_of_time = 0;
FILE *bgtime;
if ((bgtime = fopen(TIME_FILE, "r")) == NULL)
log("SYSERR: Can't read from '%s' time file.", TIME_FILE);
else {
fscanf(bgtime, "%ld\n", &beginning_of_time);
fclose(bgtime);
}
if (beginning_of_time == 0)
beginning_of_time = 650336715;
time_info = *mud_time_passed(time(0), beginning_of_time);
if (time_info.hours <= 4)
weather_info.sunlight = SUN_DARK;
else if (time_info.hours == 5)
weather_info.sunlight = SUN_RISE;
else if (time_info.hours <= 20)
weather_info.sunlight = SUN_LIGHT;
else if (time_info.hours == 21)
weather_info.sunlight = SUN_SET;
else
weather_info.sunlight = SUN_DARK;
log(" Current Gametime: %dH %dD %dM %dY.", time_info.hours,
time_info.day, time_info.month, time_info.year);
weather_info.pressure = 960;
if ((time_info.month >= 7) && (time_info.month <= 12))
weather_info.pressure += dice(1, 50);
else
weather_info.pressure += dice(1, 80);
weather_info.change = 0;
if (weather_info.pressure <= 980)
weather_info.sky = SKY_LIGHTNING;
else if (weather_info.pressure <= 1000)
weather_info.sky = SKY_RAINING;
else if (weather_info.pressure <= 1020)
weather_info.sky = SKY_CLOUDY;
else
weather_info.sky = SKY_CLOUDLESS;
}
/* Write the time in 'when' to the MUD-time file. */
void save_mud_time(struct time_info_data *when)
{
FILE *bgtime;
if ((bgtime = fopen(TIME_FILE, "w")) == NULL)
log("SYSERR: Can't write to '%s' time file.", TIME_FILE);
else {
fprintf(bgtime, "%ld\n", mud_time_to_secs(when));
fclose(bgtime);
}
}
void free_player_index(void)
{
int tp;
if (!player_table)
return;
for (tp = 0; tp <= top_of_p_table; tp++)
if (player_table[tp].name)
free(player_table[tp].name);
free(player_table);
player_table = NULL;
top_of_p_table = 0;
}
/* generate index table for the player file */
void build_player_index(void)
{
int nr = -1, i;
long size, recs;
struct char_file_u dummy;
if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) {
if (errno != ENOENT) {
perror("SYSERR: fatal error opening playerfile");
exit(1);
} else {
log("No playerfile. Creating a new one.");
touch(PLAYER_FILE);
if (!(player_fl = fopen(PLAYER_FILE, "r+b"))) {
perror("SYSERR: fatal error opening playerfile");
exit(1);
}
}
}
fseek(player_fl, 0L, SEEK_END);
size = ftell(player_fl);
rewind(player_fl);
if (size % sizeof(struct char_file_u))
log("\aWARNING: PLAYERFILE IS PROBABLY CORRUPT!");
recs = size / sizeof(struct char_file_u);
if (recs) {
log(" %ld players in database.", recs);
CREATE(player_table, struct player_index_element, recs);
} else {
player_table = NULL;
top_of_p_table = -1;
return;
}
for (;;) {
fread(&dummy, sizeof(struct char_file_u), 1, player_fl);
if (feof(player_fl))
break;
/* new record */
nr++;
CREATE(player_table[nr].name, char, strlen(dummy.name) + 1);
for (i = 0; (*(player_table[nr].name + i) = LOWER(*(dummy.name + i))); i++)
;
player_table[nr].id = dummy.char_specials_saved.idnum;
top_idnum = MAX(top_idnum, dummy.char_specials_saved.idnum);
}
top_of_p_table = nr;
}
/*
* Thanks to Andrey ([email protected]) for this bit of code, although I
* did add the 'goto' and changed some "while()" into "do { } while()".
* -gg 6/24/98 (technically 6/25/98, but I care not.)
*/
int count_alias_records(FILE *fl)
{
char key[READ_SIZE], next_key[READ_SIZE];
char line[READ_SIZE], *scan;
int total_keywords = 0;
/* get the first keyword line */
get_one_line(fl, key);
while (*key != '$') {
/* skip the text */
do {
get_one_line(fl, line);
if (feof(fl))
goto ackeof;
} while (*line != '#');
/* now count keywords */
scan = key;
do {
scan = one_word(scan, next_key);
if (*next_key)
++total_keywords;
} while (*next_key);
/* get next keyword line (or $) */
get_one_line(fl, key);
if (feof(fl))
goto ackeof;
}
return (total_keywords);
/* No, they are not evil. -gg 6/24/98 */
ackeof:
log("SYSERR: Unexpected end of help file.");
exit(1); /* Some day we hope to handle these things better... */
}
/* function to count how many hash-mark delimited records exist in a file */
int count_hash_records(FILE *fl)
{
char buf[128];
int count = 0;
while (fgets(buf, 128, fl))
if (*buf == '#')
count++;
return (count);
}
void index_boot(int mode)
{
const char *index_filename, *prefix = NULL; /* NULL or egcs 1.1 complains */
FILE *db_index, *db_file;
int rec_count = 0, size[2];
char buf2[PATH_MAX], buf1[MAX_STRING_LENGTH];
switch (mode) {
case DB_BOOT_WLD:
prefix = WLD_PREFIX;
break;
case DB_BOOT_MOB:
prefix = MOB_PREFIX;
break;
case DB_BOOT_OBJ:
prefix = OBJ_PREFIX;
break;
case DB_BOOT_ZON:
prefix = ZON_PREFIX;
break;
case DB_BOOT_SHP:
prefix = SHP_PREFIX;
break;
case DB_BOOT_HLP:
prefix = HLP_PREFIX;
break;
default:
log("SYSERR: Unknown subcommand %d to index_boot!", mode);
exit(1);
}
if (mini_mud)
index_filename = MINDEX_FILE;
else
index_filename = INDEX_FILE;
snprintf(buf2, sizeof(buf2), "%s%s", prefix, index_filename);
if (!(db_index = fopen(buf2, "r"))) {
log("SYSERR: opening index file '%s': %s", buf2, strerror(errno));
exit(1);
}
/* first, count the number of records in the file so we can malloc */
fscanf(db_index, "%s\n", buf1);
while (*buf1 != '$') {
snprintf(buf2, sizeof(buf2), "%s%s", prefix, buf1);
if (!(db_file = fopen(buf2, "r"))) {
log("SYSERR: File '%s' listed in '%s/%s': %s", buf2, prefix,
index_filename, strerror(errno));
fscanf(db_index, "%s\n", buf1);
continue;
} else {
if (mode == DB_BOOT_ZON)
rec_count++;
else if (mode == DB_BOOT_HLP)
rec_count += count_alias_records(db_file);
else
rec_count += count_hash_records(db_file);
}
fclose(db_file);
fscanf(db_index, "%s\n", buf1);
}
/* Exit if 0 records, unless this is shops */
if (!rec_count) {
if (mode == DB_BOOT_SHP)
return;
log("SYSERR: boot error - 0 records counted in %s/%s.", prefix,
index_filename);
exit(1);
}
/*
* NOTE: "bytes" does _not_ include strings or other later malloc'd things.
*/
switch (mode) {
case DB_BOOT_WLD:
CREATE(world, struct room_data, rec_count);
size[0] = sizeof(struct room_data) * rec_count;
log(" %d rooms, %d bytes.", rec_count, size[0]);
break;
case DB_BOOT_MOB:
CREATE(mob_proto, struct char_data, rec_count);
CREATE(mob_index, struct index_data, rec_count);
size[0] = sizeof(struct index_data) * rec_count;
size[1] = sizeof(struct char_data) * rec_count;
log(" %d mobs, %d bytes in index, %d bytes in prototypes.", rec_count, size[0], size[1]);
break;
case DB_BOOT_OBJ:
CREATE(obj_proto, struct obj_data, rec_count);
CREATE(obj_index, struct index_data, rec_count);
size[0] = sizeof(struct index_data) * rec_count;
size[1] = sizeof(struct obj_data) * rec_count;
log(" %d objs, %d bytes in index, %d bytes in prototypes.", rec_count, size[0], size[1]);
break;
case DB_BOOT_ZON:
CREATE(zone_table, struct zone_data, rec_count);
size[0] = sizeof(struct zone_data) * rec_count;
log(" %d zones, %d bytes.", rec_count, size[0]);
break;
case DB_BOOT_HLP:
CREATE(help_table, struct help_index_element, rec_count);
size[0] = sizeof(struct help_index_element) * rec_count;
log(" %d entries, %d bytes.", rec_count, size[0]);
break;
}
rewind(db_index);
fscanf(db_index, "%s\n", buf1);
while (*buf1 != '$') {
snprintf(buf2, sizeof(buf2), "%s%s", prefix, buf1);
if (!(db_file = fopen(buf2, "r"))) {
log("SYSERR: %s: %s", buf2, strerror(errno));
exit(1);
}
switch (mode) {
case DB_BOOT_WLD:
case DB_BOOT_OBJ:
case DB_BOOT_MOB:
discrete_load(db_file, mode, buf2);
break;
case DB_BOOT_ZON:
load_zones(db_file, buf2);
break;
case DB_BOOT_HLP:
/*
* If you think about it, we have a race here. Although, this is the
* "point-the-gun-at-your-own-foot" type of race.
*/
load_help(db_file);
break;
case DB_BOOT_SHP:
boot_the_shops(db_file, buf2, rec_count);
break;
}
fclose(db_file);
fscanf(db_index, "%s\n", buf1);
}
fclose(db_index);
/* sort the help index */
if (mode == DB_BOOT_HLP) {
qsort(help_table, top_of_helpt, sizeof(struct help_index_element), hsort);
top_of_helpt--;
}
}
void discrete_load(FILE *fl, int mode, char *filename)
{
int nr = -1, last;
char line[READ_SIZE];
const char *modes[] = {"world", "mob", "obj"};
for (;;) {
/*
* we have to do special processing with the obj files because they have
* no end-of-record marker :(
*/
if (mode != DB_BOOT_OBJ || nr < 0)
if (!get_line(fl, line)) {
if (nr == -1) {
log("SYSERR: %s file %s is empty!", modes[mode], filename);
} else {
log("SYSERR: Format error in %s after %s #%d\n"
"...expecting a new %s, but file ended!\n"
"(maybe the file is not terminated with '$'?)", filename,
modes[mode], nr, modes[mode]);
}
exit(1);
}
if (*line == '$')
return;
if (*line == '#') {
last = nr;
if (sscanf(line, "#%d", &nr) != 1) {
log("SYSERR: Format error after %s #%d", modes[mode], last);
exit(1);
}
if (nr >= 99999)
return;
else
switch (mode) {
case DB_BOOT_WLD:
parse_room(fl, nr);
break;
case DB_BOOT_MOB:
parse_mobile(fl, nr);
break;
case DB_BOOT_OBJ:
strlcpy(line, parse_object(fl, nr), sizeof(line));
break;
}
} else {
log("SYSERR: Format error in %s file %s near %s #%d", modes[mode],
filename, modes[mode], nr);
log("SYSERR: ... offending line: '%s'", line);
exit(1);
}
}
}
bitvector_t asciiflag_conv(char *flag)
{
bitvector_t flags = 0;
int is_num = TRUE;
char *p;
for (p = flag; *p; p++) {
if (islower(*p))
flags |= 1 << (*p - 'a');
else if (isupper(*p))
flags |= 1 << (26 + (*p - 'A'));
if (!isdigit(*p))
is_num = FALSE;
}
if (is_num)
flags = atol(flag);
return (flags);
}
/* load the rooms */
void parse_room(FILE *fl, int virtual_nr)
{
static int room_nr = 0, zone = 0;
int t[10], i;
char line[READ_SIZE], flags[128], buf2[MAX_STRING_LENGTH], buf[128];
struct extra_descr_data *new_descr;
/* This really had better fit or there are other problems. */
snprintf(buf2, sizeof(buf2), "room #%d", virtual_nr);
if (virtual_nr < zone_table[zone].bot) {
log("SYSERR: Room #%d is below zone %d.", virtual_nr, zone);
exit(1);
}
while (virtual_nr > zone_table[zone].top)
if (++zone > top_of_zone_table) {
log("SYSERR: Room %d is outside of any zone.", virtual_nr);
exit(1);
}
world[room_nr].zone = zone;
world[room_nr].number = virtual_nr;
world[room_nr].name = fread_string(fl, buf2);
world[room_nr].description = fread_string(fl, buf2);
if (!get_line(fl, line)) {
log("SYSERR: Expecting roomflags/sector type of room #%d but file ended!",
virtual_nr);
exit(1);
}
if (sscanf(line, " %d %s %d ", t, flags, t + 2) != 3) {
log("SYSERR: Format error in roomflags/sector type of room #%d",
virtual_nr);
exit(1);
}
/* t[0] is the zone number; ignored with the zone-file system */
world[room_nr].room_flags = asciiflag_conv(flags);
sprintf(flags, "object #%d", virtual_nr); /* sprintf: OK (until 399-bit integers) */
check_bitvector_names(world[room_nr].room_flags, room_bits_count, flags, "room");
world[room_nr].sector_type = t[2];
world[room_nr].func = NULL;
world[room_nr].contents = NULL;
world[room_nr].people = NULL;
world[room_nr].light = 0; /* Zero light sources */
for (i = 0; i < NUM_OF_DIRS; i++)
world[room_nr].dir_option[i] = NULL;
world[room_nr].ex_description = NULL;
snprintf(buf, sizeof(buf), "SYSERR: Format error in room #%d (expecting D/E/S)", virtual_nr);
for (;;) {
if (!get_line(fl, line)) {
log("%s", buf);
exit(1);
}
switch (*line) {
case 'D':
setup_dir(fl, room_nr, atoi(line + 1));
break;
case 'E':
CREATE(new_descr, struct extra_descr_data, 1);
new_descr->keyword = fread_string(fl, buf2);
new_descr->description = fread_string(fl, buf2);
new_descr->next = world[room_nr].ex_description;
world[room_nr].ex_description = new_descr;
break;
case 'S': /* end of room */
top_of_world = room_nr++;
return;
default:
log("%s", buf);
exit(1);
}
}
}
/* read direction data */
void setup_dir(FILE *fl, int room, int dir)
{
int t[5];
char line[READ_SIZE], buf2[128];
snprintf(buf2, sizeof(buf2), "room #%d, direction D%d", GET_ROOM_VNUM(room), dir);
CREATE(world[room].dir_option[dir], struct room_direction_data, 1);
world[room].dir_option[dir]->general_description = fread_string(fl, buf2);
world[room].dir_option[dir]->keyword = fread_string(fl, buf2);
if (!get_line(fl, line)) {
log("SYSERR: Format error, %s", buf2);
exit(1);
}
if (sscanf(line, " %d %d %d ", t, t + 1, t + 2) != 3) {