/
treasure.c
2642 lines (2385 loc) · 90.8 KB
/
treasure.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
/*
Daimonin, the Massive Multiuser Online Role Playing Game
Server Applicatiom
Copyright (C) 2001 Michael Toennies
A split from Crossfire, a Multiplayer game for X-windows.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
The author can be reached via e-mail to info@daimonin.org
*/
/* TREASURE_DEBUG does some checking on the treasure lists after loading.
* It is useful for finding bugs in the treasures file. Since it only
* slows the startup some (and not actual game play), it is by default
* left on
*/
#define TREASURE_DEBUG
/* TREASURE_VERBOSE enables copious output concerning artifact generation */
/*#define TREASURE_VERBOSE*/
#include <global.h>
#define PUT(_O_, _C_, _F_) \
if (((_F_) & GT_ENVIRONMENT)) \
{ \
(_O_)->x = (_C_)->x; \
(_O_)->y = (_C_)->y; \
(void)insert_ob_in_map((_O_), (_C_)->map, (_O_), INS_NO_WALK_ON); \
} \
else \
{ \
(void)insert_ob_in_ob((_O_), (_C_)); \
}
/* static functions */
static treasure_t *load_treasure(FILE *fp, int *t_style, int *a_chance);
static void change_treasure(struct _change_arch *ca, object_t *op); /* overrule default values */
static treasurelist_t *get_empty_treasurelist(void);
static treasure_t *get_empty_treasure(void);
static inline void set_material_real(object_t *op, struct _change_arch *change_arch);
static void create_money_table(void);
static void postparse_treasurelist(treasure_t *t, treasurelist_t *tl);
/*
* Opens LIBDIR/treasure and reads all treasure-declarations from it.
* Each treasure is parsed with the help of load_treasure().
*/
static void init_treasures(FILE *fp)
{
static treasurelist_t *previous = NULL; /* important, we call this now recursive */
char buf[MEDIUM_BUF], name[MEDIUM_BUF];
treasurelist_t *tl_tmp;
treasure_t *t;
int t_style, a_chance;
char dummy[10];
while (fgets(buf, MEDIUM_BUF, fp) != NULL)
{
if (*buf == '#' || *buf == '\n' || *buf == '\r')
continue;
if (sscanf(buf, "treasureone %s%[\n\r]", name, dummy) || sscanf(buf, "treasure %s%[\n\r]", name, dummy))
{
treasurelist_t *tl = get_empty_treasurelist();
SHSTR_FREE_AND_ADD_STRING(tl->listname, name);
/* check for double used list name */
for (tl_tmp = first_treasurelist; tl_tmp != NULL; tl_tmp = tl_tmp->next)
{
if (tl->listname == tl_tmp->listname)
break;
}
if (tl_tmp)
{
LOG(llevError, "ERROR: Treasure list name <%s> double used.\n", STRING_SAFE(tl->listname));
FREE(tl);
return;
}
if (previous == NULL)
first_treasurelist = tl;
else
previous->next = tl;
previous = tl;
t_style = T_STYLE_UNSET;
a_chance = ART_CHANCE_UNSET;
tl->items = load_treasure(fp, &t_style, &a_chance);
if (tl->t_style == T_STYLE_UNSET)
tl->t_style = t_style;
if (tl->artifact_chance == ART_CHANCE_UNSET)
tl->artifact_chance = a_chance;
/* This is a one of the many items on the list should be generated.
* Add up the chance total, and check to make sure the yes & no
* fields of the treasures are not being used.
*/
if (!strncmp(buf, "treasureone", 11))
{
for (t = tl->items; t != NULL; t = t->next)
{
#ifdef TREASURE_DEBUG
if (t->next_yes || t->next_no)
{
LOG(llevBug, "BUG: Treasure %s is one item, but on treasure %s\n", tl->listname,
t->item ? t->item->name : t->name);
LOG(llevBug, "BUG: the next_yes or next_no field is set");
}
#endif
tl->total_chance += t->chance;
}
#if 0
LOG(llevDebug, "Total chance for list %s is %d\n", tl->name, tl->total_chance);
#endif
}
}
else
LOG(llevError, "ERROR: Treasure-list didn't understand: %s\n", buf);
}
}
/*
* recusively traverse the given directory and search for *.tl files
* and process them like they are part of the original treasure list file
*/
static void traverse_treasures_files(char* start_dir)
{
DIR* dir; /* pointer to the scanned directory. */
struct dirent* entry=NULL; /* pointer to one directory entry. */
char *fptr, cwd[HUGE_BUF+1]; /* current working directory. */
struct stat dir_stat; /* used by stat(). */
/* first, save path of current working directory */
if (!getcwd(cwd, HUGE_BUF+1)) {
perror("getcwd:");
return;
}
/* open the directory for reading */
if(start_dir)
{
if ((dir = opendir(start_dir)))
{
if (chdir(start_dir) == -1)
{
fprintf(stderr, "Cannot chdir into '%s': ", start_dir);
perror("");
dir = NULL;
}
}
}
else
dir = opendir(".");
if (!dir) {
fprintf(stderr, "Cannot read directory '%s': ", cwd);
perror("");
return;
}
/* scan the directory, traversing each sub-directory, and */
/* matching the pattern for each file name. */
while ((entry = readdir(dir)))
{
/* check if the given entry is a directory. */
/* skip all ".*" entries, to avoid loops and forbidden directories. */
if (entry->d_name[0] == '.')
continue;
if (stat(entry->d_name, &dir_stat) == -1)
{
perror("stat:");
continue;
}
/* is this a directory? */
if (S_ISDIR(dir_stat.st_mode))
{
/* Change into the new directory */
if (chdir(entry->d_name) == -1)
{
fprintf(stderr, "Cannot chdir into '%s': ", entry->d_name);
perror("");
continue;
}
/* check this directory */
traverse_treasures_files(NULL);
/* finally, restore the original working directory. */
if (chdir("..") == -1)
{
fprintf(stderr, "Cannot chdir back to '%s': ", cwd);
perror("");
}
}
else
{
/* lets check its a valid, local artifacts file */
if(entry->d_name[0] != '.' && (fptr = strrchr(entry->d_name, '.')) && !strcmp(fptr, ".tl") )
{
FILE *fp;
LOG(llevDebug, " adding local treasures from %s...", entry->d_name);
if ((fp = fopen(entry->d_name, "r")) == NULL)
{
LOG(llevError, "ERROR: Can't open %s.\n", entry->d_name);
exit(SERVER_EXIT_TREASURE);
}
init_treasures(fp);
fclose(fp);
LOG(llevDebug, "done.\n");
}
}
}
closedir(dir);
if(start_dir) /* clean restore */
{
if (chdir(cwd) == -1)
{
fprintf(stderr, "Cannot chdir into '%s': ", cwd);
perror("");
}
}
}
void load_treasures(void)
{
FILE *fp;
char filename[MEDIUM_BUF];
treasurelist_t *previous = NULL;
/* load default treasure list file from /lib */
sprintf(filename, "%s/%s", settings.datadir, settings.treasures);
if ((fp = fopen(filename,"r")) == NULL)
{
LOG(llevError, "ERROR: Can't open treasure file.\n");
return;
}
init_treasures(fp);
fclose(fp);
/* traverse the /maps folder and load every file with .tl extension as local treasure list */
traverse_treasures_files(settings.mapdir);
LOG(llevInfo, " link treasure lists pass 2...\n");
for (previous = first_treasurelist; previous != NULL; previous = previous->next)
postparse_treasurelist(previous->items, previous);
create_money_table();
}
static void postparse_treasurelist(treasure_t *t, treasurelist_t *tl)
{
treasurelist_t *tl_tmp;
if (t->item == NULL && t->name == NULL)
LOG(llevError, "ERROR: Treasurelist %s has element with no name or archetype\n", STRING_SAFE(tl->listname));
if (t->chance >= 100 && t->next_yes && (t->next || t->next_no))
LOG(llevBug,
"BUG: Treasurelist %s has element that has 100%% generation, next_yes field as well as next or next_no\n",
tl->listname);
/* if we have a list name && its not "none" -> link the list in */
if (t->name && t->name != shstr_cons.none)
{
for (tl_tmp = first_treasurelist; tl_tmp != NULL; tl_tmp = tl_tmp->next)
{
if (t->name == tl_tmp->listname)
break;
}
if (!tl_tmp)
LOG(llevError, "ERROR: Treasurelist %s has element with invalid name <%s>\n", STRING_SAFE(tl->listname),
STRING_SAFE(t->name));
t->tlist = tl_tmp;
}
if (t->next)
postparse_treasurelist(t->next, tl);
if (t->next_yes)
postparse_treasurelist(t->next_yes, tl);
if (t->next_no)
postparse_treasurelist(t->next_no, tl);
}
/* to generate from a value a set of coins (like 3 gold, 4 silver and 19 copper)
* we collect the archetype_t for it out of the arch name for faster access.
*/
static void create_money_table(void)
{
coins_arch[0] = find_archetype("mitcoin");
coins_arch[1] = find_archetype("goldcoin");
coins_arch[2] = find_archetype("silvercoin");
coins_arch[3] = find_archetype("coppercoin");
coins_arch[4] = NULL;
if (!coins_arch[0] || !coins_arch[1] || !coins_arch[2] || !coins_arch[3])
{
LOG(llevError, "create_money_table(): Can't find money.\n (mit: %p - gold: %p - silver: %p - copper: %p)",
coins_arch[0],coins_arch[1],coins_arch[2],coins_arch[3]);
return;
}
}
/*
* Reads the lib/treasure file from disk, and parses the contents
* into an internal treasure structure (very linked lists)
*/
static treasure_t * load_treasure(FILE *fp, int *t_style, int *a_chance)
{
char buf[MEDIUM_BUF], *cp=NULL, variable[MEDIUM_BUF];
treasure_t *t = get_empty_treasure();
int value;
int start_marker = 0, t_style2, a_chance2;
nroftreasures++;
while (fgets(buf, MEDIUM_BUF, fp) != NULL)
{
if (*buf == '#')
continue;
if ((cp = strchr(buf, '\n')) != NULL)
*cp = '\0';
cp = buf;
while (!isalpha(*cp)) /* Skip blanks */
cp++;
if (sscanf(cp, "t_style %d", &value))
{
if (start_marker)
t->t_style = value;
else
{
*t_style = value; /* no, its global for the while treasure list entry */
}
}
else if (sscanf(cp, "artifact_chance %d", &value))
{
if (start_marker)
t->artifact_chance = value;
else
{
*a_chance = value; /* no, its global for the while treasure list entry */
}
}
else if (sscanf(cp, "arch %s", variable))
{
if ((t->item = find_archetype(variable)) == NULL)
LOG(llevBug, "BUG: Treasure lacks archetype: %s\n", variable);
start_marker = 1;
}
else if (sscanf(cp, "list %s", variable))
{
start_marker = 1;
SHSTR_FREE_AND_ADD_STRING(t->name, variable);
}
else if (sscanf(cp, "name %s", variable))
{
SHSTR_FREE_AND_ADD_STRING(t->change_arch.name, cp + 5);
}
else if (sscanf(cp, "race %s", variable))
{
SHSTR_FREE_AND_ADD_STRING(t->change_arch.race, cp + 5);
}
else if (sscanf(cp, "title %s", variable))
{
SHSTR_FREE_AND_ADD_STRING(t->change_arch.title, cp + 6);
}
else if (sscanf(cp, "slaying %s", variable))
{
SHSTR_FREE_AND_ADD_STRING(t->change_arch.slaying, cp + 8);
}
else if (sscanf(cp, "face %s", variable))
{
if (strcmp (cp+5, "NONE") == 0)
{
t->change_arch.face_id = 0;
t->change_arch.face = NULL;
}
else
{
int face_id = FindFace(cp+5, 0);
if(!face_id)
LOG(llevBug,"BUG: TLIST can't find face %s.\n", cp+5);
else
{
t->change_arch.face_id = face_id;
t->change_arch.face = &new_faces[face_id];
}
}
}
else if (sscanf(cp, "anim %s", variable))
{
if (strcmp (cp+5, "NONE") == 0)
t->change_arch.anim_id = 0;
else
t->change_arch.anim_id = find_animation (cp+5);
}
else if (sscanf(cp, "item_race %d", &value))
t->change_arch.item_race = value;
else if (sscanf(cp, "quality %d", &value))
t->change_arch.quality = value;
else if (sscanf(cp, "quality_range %d", &value))
t->change_arch.quality_range = value;
else if (sscanf(cp, "material %d", &value))
t->change_arch.material = value;
else if (sscanf(cp, "material_quality %d", &value))
t->change_arch.material_quality = value;
else if (sscanf(cp, "material_range %d", &value))
t->change_arch.material_range = value;
else if (sscanf(cp, "chance_fix %d", &value))
{
t->chance_fix = (sint16) value;
t->chance = 0; /* important or the chance will stay 100% when not set to 0 in treasure list! */
}
else if (sscanf(cp, "chance %d", &value))
t->chance = (uint8) value;
else if (sscanf(cp, "nrof %d", &value))
t->nrof = (uint16) value;
else if (sscanf(cp, "magic %d", &value))
t->magic = value;
else if (sscanf(cp, "magic_chance %d", &value))
t->magic_chance = value;
else if (sscanf(cp, "difficulty %d", &value))
t->difficulty = value;
else if (!strncmp(cp, "yes", 3))
{
t_style2 = T_STYLE_UNSET;
a_chance2 = ART_CHANCE_UNSET;
t->next_yes = load_treasure(fp, &t_style2, &a_chance2);
if (t->next_yes->artifact_chance == ART_CHANCE_UNSET)
t->next_yes->artifact_chance = a_chance2;
if (t->next_yes->t_style == T_STYLE_UNSET)
t->next_yes->t_style = t_style2;
}
else if (!strncmp(cp, "no", 2))
{
t_style2 = T_STYLE_UNSET;
a_chance2 = ART_CHANCE_UNSET;
t->next_no = load_treasure(fp, &t_style2, &a_chance2);
if (t->next_no->artifact_chance == ART_CHANCE_UNSET)
t->next_no->artifact_chance = a_chance2;
if (t->next_no->t_style == T_STYLE_UNSET)
t->next_no->t_style = t_style2;
}
else if (!strncmp(cp, "end", 3))
return t;
else if (!strncmp(cp, "more", 4))
{
t_style2 = T_STYLE_UNSET;
a_chance2 = ART_CHANCE_UNSET;
t->next = load_treasure(fp, &t_style2, &a_chance2);
if (t->next->artifact_chance == ART_CHANCE_UNSET)
t->next->artifact_chance = a_chance2;
if (t->next->t_style == T_STYLE_UNSET)
t->next->t_style = t_style2;
return t;
}
else
LOG(llevBug, "BUG: Unknown treasure-command: '%s', last entry %s\n", STRING_SAFE(cp), STRING_SAFE(t->name));
}
LOG(llevBug, "BUG: treasure %s lacks 'end'.>%s<\n", STRING_SAFE(t->name), STRING_SAFE(cp));
return t;
}
/*
* Allocate and return the pointer to an empty treasurelist structure.
*/
static treasurelist_t * get_empty_treasurelist(void)
{
treasurelist_t *tl = (treasurelist_t *) malloc(sizeof(treasurelist_t));
if (tl == NULL)
LOG(llevError, "ERROR: get_empty_treasurelist(): OOM.\n");
tl->listname = NULL;
tl->next = NULL;
tl->items = NULL;
tl->t_style = T_STYLE_UNSET; /* -2 is the "unset" marker and will virtually handled as 0 which can be overruled */
tl->artifact_chance = ART_CHANCE_UNSET;
tl->chance_fix = CHANCE_FIX;
tl->total_chance = 0;
return tl;
}
static inline void set_change_arch(_change_arch *ca)
{
ca->face = NULL;
ca->face_id = -1;
ca->anim_id = -1;
ca->animate = -1;
ca->item_race = -1;
ca->name = NULL;
ca->race = NULL;
ca->slaying = NULL;
ca->title = NULL;
ca->material = -1;
ca->material_quality = -1;
ca->material_range = -1;
ca->quality = -1;
ca->quality_range = -1;
}
/*
* Allocate and return the pointer to an empty treasure structure.
*/
static treasure_t * get_empty_treasure(void)
{
treasure_t *t = (treasure_t *) malloc(sizeof(treasure_t));
if (t == NULL)
LOG(llevError, "ERROR: get_empty_treasure(): OOM.\n");
set_change_arch(&t->change_arch);
t->chance_fix = CHANCE_FIX;
t->tlist = NULL;
t->t_style = T_STYLE_UNSET; /* -2 is the "unset" marker and will virtually handled as 0 which can be overruled */
t->item = NULL;
t->name = NULL;
t->next = NULL;
t->next_yes = NULL;
t->next_no = NULL;
t->artifact_chance = ART_CHANCE_UNSET;
t->chance = 100;
t->difficulty = 0;
t->magic_chance = T_MAGIC_CHANCE_UNSET;
t->magic = T_MAGIC_UNSET;
t->nrof = 0;
return t;
}
/*
* Searches for the given treasurelist in the globally linked list
* of treasurelists which has been built by load_treasures().
*/
treasurelist_t * find_treasurelist(const char *name)
{
const char *tmp = shstr_find(name);
treasurelist_t *tl;
/* Special cases - randomitems of none is to override default. If
* first_treasurelist is null, it means we are on the first pass of
* of loading archetyps, so for now, just return - second pass will
* init these values.
*/
if (tmp == shstr_cons.none || !first_treasurelist)
return NULL;
if (tmp != NULL)
{
for (tl = first_treasurelist; tl != NULL; tl = tl->next)
{
if (tmp == tl->listname)
return tl;
}
}
LOG(llevBug, "Bug: Couldn't find treasurelist %s\n", name);
return NULL;
}
static inline treasurelist_t * find_treasurelist_intern(const char *name)
{
treasurelist_t *tl;
for (tl = first_treasurelist; tl != NULL; tl = tl->next)
{
if (name == tl->listname)
return tl;
}
return NULL;
}
/* parse treasure list paramter in randomitems cmd */
static inline void parse_tlist_parm(tlist_tweak *tweak, char *parm)
{
char *tmp;
do
{
/* the paramater list is like "parm1,parm2, parm3..." */
if ((tmp = strchr(parm, ',')))
*tmp = 0;
/* every paramter is like <ID><tail> where <ID> is a single char */
switch(*parm)
{
case 'm': /* (m)agic */
tweak->magic = atoi(parm+1);
break;
case 'M': /* (M)agic chance */
tweak->magic_chance = atoi(parm+1);
break;
case 'x': /* difficulty */
tweak->difficulty = atoi(parm+1);
break;
case 'a': /* (a)rtifact chance*/
tweak->artifact_chance = atoi(parm+1);
break;
case 'd': /* (d)rop chance 1/d */
tweak->drop_chance = atoi(parm+1);
break;
case 'D': /* (D)rop chance % */
tweak->drop100 = atoi(parm+1);
break;
case 's': /* treasure (s)tyle */
tweak->style = atoi(parm+1);
break;
case 'r': /* item (r)ace */
tweak->c_arch.item_race = atoi(parm+1);
break;
case 'i': /* (i)tem material */
tweak->c_arch.material = atoi(parm+1);
break;
case 'I': /* (i)dentified */
tweak->identified = 1;
break;
case 'c': /* (c)reated: material quality*/
tweak->c_arch.material_quality = atoi(parm+1);
break;
case 'C': /* (C)reated: material range */
tweak->c_arch.material_range = atoi(parm+1);
break;
case 'q': /* (q)uality */
tweak->c_arch.quality = atoi(parm+1);
break;
case 'Q': /* (q)uality range */
tweak->c_arch.quality_range = atoi(parm+1);
break;
case 'B': /* (B)reak list generation if this tlist generates something */
tweak->break_list = 1;
break;
default:
LOG(llevBug,"\nBUG ::TLIST PARSE: invalid tlist paramter: %s\n", STRING_SAFE(parm));
return;
break;
}
if (tmp)
parm = tmp + 1;
} while(tmp);
}
/* link_treasurelists will generate a linked lists of treasure list
* using a string in format "listname1;listname2;listname3;..." as
* argument.
* Return: objectlink_t * to list of treasurelist
*/
objectlink_t * link_treasurelists(objectlink_t *base, char *liststring, uint32 flags)
{
char *tmp, *parm;
const char *name;
treasurelist_t *tl;
objectlink_t*list = NULL, *list_start = NULL;
if (!first_treasurelist)
return NULL;
/*LOG(llevNoLog,"LINK list: %s - ",liststring);*/
do
{
if ((tmp = strchr(liststring, ';')))
*tmp = 0;
/* find parameter marker. */
if ((parm = strchr(liststring, '&')))
*parm = 0;
if (!(name = shstr_find(liststring)))
{
/* no treasure list name in hash table = no treasure list with that name */
LOG(llevInfo, "BUG: link_treasurelists(): Treasurelist >%s< not found\n", liststring);
}
else
{
/* we have a 'none' string?
* normally, we should break here but perhaps
* we have something like "list1;none;list2".
* Can't think why but lets do it right.
*/
if (name != shstr_cons.none)
{
tl = find_treasurelist_intern(name);
if (!tl)
LOG(llevInfo, "BUG: link_treasurelists(): Treasurelist >%s< not found\n", liststring);
else
{
if (list)
{
list->next = objectlink_get(OBJLNK_FLAG_TL);
list->next->prev = list;
list = list->next;
}
else
{
list_start = list = objectlink_get(OBJLNK_FLAG_TL);
/* important: we only mark the first list node with
* static or refcount flag.
*/
list->flags |= flags;
if (flags & OBJLNK_FLAG_REF)
list->ref_count++;
/*LOG(llevNoLog," --> %x (%d)\n",list->flags,list->ref_count);*/
}
list->objlink.tl = tl;
if(parm) /* we have a parameter list ('&' tail) for this list? parse it */
{
tlist_tweak *tweak = (tlist_tweak *) get_poolchunk(pool_tlist_tweak);
/* save the tname with paramter. don't patch, save the whole name */
*parm = '&';
tweak->name = shstr_add_string(liststring);
tweak->artifact_chance = ART_CHANCE_UNSET;
tweak->style = T_STYLE_UNSET;
tweak->difficulty = 0;
tweak->identified = 0;
tweak->break_list = 0;
tweak->magic = T_MAGIC_UNSET;
tweak->magic_chance = T_MAGIC_CHANCE_UNSET;
tweak->drop_chance = 0;
tweak->drop100 = 0;
set_change_arch(&tweak->c_arch);
parse_tlist_parm(tweak, parm+1);
list->parmlink.tl_tweak = tweak;
}
}
}
}
if (tmp)
liststring = tmp + 1;
}
while (tmp);
if (base)
{
list->next = base;
base->prev = list;
}
return list_start;
}
objectlink_t *branch_treasurelist(objectlink_t *tl)
{
objectlink_t *list = NULL,
*list_start = NULL,
*new;
tlist_tweak *tweak = NULL;
for (list = tl; list; list = list->next)
{
if (!list_start)
{
new = list_start = objectlink_get(OBJLNK_FLAG_TL);
new->flags |= OBJLNK_FLAG_STATIC;
new->ref_count++;
}
else
{
new->next = objectlink_get(OBJLNK_FLAG_TL);
new->next->prev = new;
new = new->next;
}
new->objlink = list->objlink;
if (list->parmlink.tl_tweak)
{
new->parmlink.tl_tweak = (tlist_tweak *)get_poolchunk(pool_tlist_tweak);
memcpy(&(new->parmlink), &(list->parmlink), sizeof(list->parmlink));
new->parmlink.tl_tweak->name = shstr_add_string(list->parmlink.tl_tweak->name);
}
}
return list_start;
}
void append_treasure_string(const object_t *op, char *buf)
{
if (!op->randomitems)
{
strcat(buf, "none\n");
}
else /* we have different lists - generate the "list1;list2;list3" string */
{
objectlink_t *ol = op->randomitems;
if (ol->parmlink.tl_tweak) /* tlist with or without '&' parameter list* */
{
strcat(buf, ol->parmlink.tl_tweak->name); /* save name and '&' tail */
}
else
{
strcat(buf, ol->objlink.tl->listname); /* clean default list name */
}
ol = ol->next;
for (; ol; ol = ol->next)
{
strcat(buf, ";");
if (ol->parmlink.tl_tweak)
{
strcat(buf, ol->parmlink.tl_tweak->name);
}
else
{
strcat(buf, ol->objlink.tl->listname);
}
}
}
}
/* unlink a treasure list.
* if flag is set to 1, ignore (delete) the OBJLNK_FLAG_STATIC flag
*/
void unlink_treasurelists(objectlink_t *list, int flag)
{
/*LOG(llevNoLog,"unlink list: %s (%x - %d)\n",list->objlink.tl->listname, list->flags, list->ref_count );*/
if (list && (list->flags & OBJLNK_FLAG_REF))
list->ref_count--;
/* skip if we have no list or refcount shows
* that still other objects points to this list.
*/
if (!list || list->ref_count || ((list->flags & OBJLNK_FLAG_STATIC) && !flag))
{
/*LOG(llevNoLog,"skiped listpart: %s\n",list->objlink.tl->listname);*/
return;
}
do
{
/*LOG(llevNoLog,"freed listpat: %s\n",list->objlink.tl->listname); */
if(list->parmlink.tl_tweak)
{
SHSTR_FREE(list->parmlink.tl_tweak->name);
return_poolchunk(list->parmlink.tl_tweak, pool_tlist_tweak);
}
return_poolchunk(list, pool_objectlink);
/* hm, this should work... return_poolchunk() should not effect the objectlink itself */
list = list->next;
}
while (list);
}
/* The point of generate_treasure is that is generates exactly *one*
* item. I tweaked it a bit so it will work now with a linked list.
* generate_treasure can give back NULL (nothing generated).
* We will browse the linked list until we get a valid target.
* We break if we have a item or we are at the end of the list.
* MT-2005
*/
object_t * generate_treasure(struct objectlink_t *t, int difficulty)
{
struct _change_arch *captr;
int t_style, a_chance, flag, magic, magic_chance;
object_t*ob = get_object(), *tmp = NULL;
while (t)
{
flag = 0;
captr = NULL;
t_style = t->objlink.tl->t_style;
a_chance = t->objlink.tl->artifact_chance;
magic = T_MAGIC_UNSET;
magic_chance = T_MAGIC_CHANCE_UNSET;
if(t->parmlink.tl_tweak) /* this treasure list had a '&' paramter list */
{
/* random chance tests for 1/x, drop100 for % chance (1-100%) */
if((t->parmlink.tl_tweak->drop_chance && (RANDOM() % t->parmlink.tl_tweak->drop_chance))
|| (t->parmlink.tl_tweak->drop100 && ((RANDOM()%100) >= t->parmlink.tl_tweak->drop100)))
{
t = t->next;
continue;
}
/* setup the '&' parameter values insertation to the treasure list */
captr = &t->parmlink.tl_tweak->c_arch;
magic = t->parmlink.tl_tweak->magic;
magic_chance = t->parmlink.tl_tweak->magic_chance;
if(t->parmlink.tl_tweak->style != T_STYLE_UNSET)
t_style = t->parmlink.tl_tweak->style;
if(t->parmlink.tl_tweak->difficulty)
difficulty = t->parmlink.tl_tweak->difficulty;
if(t->parmlink.tl_tweak->artifact_chance != ART_CHANCE_UNSET)
a_chance = t->parmlink.tl_tweak->artifact_chance;
if(t->parmlink.tl_tweak->identified)
flag |= GT_IDENTIFIED;
}
create_treasure(t->objlink.tl, ob, flag, difficulty, t_style, a_chance, magic, magic_chance, 0, captr);
if (!ob->inv) /* no treasure, try next tlist */
{
t = t->next;
continue;
}
/* Don't want to free the object we are about to return */
tmp = ob->inv;
remove_ob(tmp); /* remove from inv - no move off */
if (ob->inv)
{
LOG(llevBug, "BUG: generate treasure(): created multiple objects for tlist %s.\n",
STRING_SAFE(t->objlink.tl->listname));
/* remove objects so garbage collection can put them back */
for (; ob->inv;)
remove_ob(ob->inv);
}
return tmp;
}
return tmp;
}
/* This calls the appropriate treasure creation function. tries is passed
* to determine how many list transitions or attempts to create treasure
* have been made. It is really in place to prevent infinite loops with
* list transitions, or so that excessively good treasure will not be
* created on weak maps, because it will exceed the number of allowed tries
* to do that.
*/
/* called from various sources, arch_change will be normally NULL the first time a
* treasure list is generated. This is then the "base" list. We will use the real
* first arch_change as base to other recursive calls.
*/
/* help function to call a objectlink linked list of treasure lists */
void create_treasure_list(struct objectlink_t *t, object_t *op, int flag, int difficulty, int art_chance, int tries)
{
struct _change_arch *captr;
int t_style, a_chance, magic, magic_chance;
while (t)
{
if(t->parmlink.tl_tweak) /* this treasure list had a '&' paramter list */
{
/* random chance tests for 1/x, drop100 for % chance (1-100%) */
if((t->parmlink.tl_tweak->drop_chance && (RANDOM() % t->parmlink.tl_tweak->drop_chance))
|| (t->parmlink.tl_tweak->drop100 && ((RANDOM()%100) >= t->parmlink.tl_tweak->drop100)))
{
t = t->next;
continue;
}
a_chance = art_chance;
/* setup the '&' parameter values to the treasure list */
captr = &t->parmlink.tl_tweak->c_arch;
magic = t->parmlink.tl_tweak->magic;
magic_chance = t->parmlink.tl_tweak->magic_chance;
if(t->parmlink.tl_tweak->style != T_STYLE_UNSET)
t_style = t->parmlink.tl_tweak->style;
else
t_style = t->objlink.tl->t_style;
if(t->parmlink.tl_tweak->artifact_chance != ART_CHANCE_UNSET)
a_chance = t->parmlink.tl_tweak->artifact_chance;
else if(t->objlink.tl->artifact_chance != ART_CHANCE_UNSET)
a_chance = t->objlink.tl->artifact_chance;
if(t->parmlink.tl_tweak->difficulty)
difficulty = t->parmlink.tl_tweak->difficulty;
if(t->parmlink.tl_tweak->identified)
flag |= GT_IDENTIFIED;
}
else
{
a_chance = art_chance;
if(t->objlink.tl->artifact_chance != ART_CHANCE_UNSET)
a_chance = t->objlink.tl->artifact_chance;
captr = NULL;
t_style = T_STYLE_UNSET;
magic = T_MAGIC_UNSET;
magic_chance = T_MAGIC_CHANCE_UNSET;
}
/* if we have a breakpoint set here, stop creating treasures for this list */
if( create_treasure(t->objlink.tl, op, flag, difficulty, t_style, a_chance, magic, magic_chance, tries, captr) &&
(t->parmlink.tl_tweak && t->parmlink.tl_tweak->break_list))
return;