-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathglyphs.c
More file actions
1263 lines (1156 loc) · 40.5 KB
/
glyphs.c
File metadata and controls
1263 lines (1156 loc) · 40.5 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
/* NetHack 5.0 glyphs.c TODO: add NHDT branch/date/revision tags */
/* Copyright (c) Michael Allison, 2021. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
extern const struct symparse loadsyms[];
extern glyph_map glyphmap[MAX_GLYPH];
extern struct enum_dump monsdump[];
extern struct enum_dump objdump[];
#define Fprintf (void) fprintf
enum reserved_activities { res_nothing, res_dump_glyphnames };
enum things_to_find { find_nothing, find_pm, find_oc, find_cmap, find_glyph };
struct find_struct {
enum things_to_find findtype;
int val;
int loadsyms_offset;
int loadsyms_count;
int *extraval;
uint32 color;
const char *unicode_val; /* U+NNNN format */
void (*callback)(int glyph, struct find_struct *);
enum reserved_activities restype;
genericptr_t reserved;
};
static const struct find_struct zero_find = { 0 };
struct glyphname_hash_index_entry_t {
uint32 hash;
int glyphnum;
};
static struct glyphname_hash_index_entry_t *glyphname_hash_indices_ptr;
static size_t glyphname_hash_indices_count;
static struct find_struct to_custom_symbol_find;
static const long nonzero_black = CLR_BLACK | NH_BASIC_COLOR;
staticfn int find_glyph_in_hashtable(const char *id);
staticfn int cmp_glyphname_entry(const void *a, const void *b);
staticfn uint32 glyph_hash(const char *id);
staticfn int compose_glyph_name(int glyph, char *buf, size_t bufsz);
staticfn int get_cmap_offset(void);
staticfn void to_custom_symset_entry_callback(int glyph,
struct find_struct *findwhat);
staticfn int parse_id(const char *id, struct find_struct *findwhat);
staticfn int glyph_find_core(const char *id, struct find_struct *findwhat);
staticfn char *fix_glyphname(char *str);
staticfn void shuffle_customizations(void);
/* staticfn void purge_custom_entries(enum graphics_sets which_set); */
staticfn void
to_custom_symset_entry_callback(
int glyph,
struct find_struct *findwhat)
{
int idx = gs.symset_which_set;
#ifdef ENHANCED_SYMBOLS
uint8 utf8str[6] = { 0, 0, 0, 0, 0, 0 };
int uval = 0;
#endif
if (findwhat->extraval)
*findwhat->extraval = glyph;
assert(idx >= 0 && idx < NUM_GRAPHICS);
#ifdef ENHANCED_SYMBOLS
if (findwhat->unicode_val)
uval = unicode_val(findwhat->unicode_val);
if (uval && unicodeval_to_utf8str(uval, utf8str, sizeof utf8str)) {
/* presently the customizations are affiliated with a particular
* symset but if we don't have any symset context, ignore it for now
* in order to avoid a segfault.
* FIXME:
* One future idea might be to store the U+ entries under "UTF8"
* and apply those customizations to any current symset if it has
* a UTF8 handler. Similar approach for unaffiliated glyph/symbols
* non-UTF color customizations
*/
if (gs.symset[idx].name) {
add_custom_urep_entry(gs.symset[idx].name, glyph, uval,
utf8str, gs.symset_which_set);
} else {
static int glyphnag = 0;
if (!glyphnag++)
config_error_add("Unimplemented customization feature,"
" ignoring for now");
}
}
#endif
if (findwhat->color) {
if (gs.symset[idx].name) {
add_custom_nhcolor_entry(gs.symset[idx].name, glyph,
findwhat->color, gs.symset_which_set);
} else {
static int colornag = 0;
if (!colornag++)
config_error_add("Unimplemented customization feature,"
" ignoring for now");
}
}
}
/*
* Return value:
* 1 = success
* 0 = failure
*/
int
glyphrep_to_custom_map_entries(
const char *op,
int *glyphptr)
{
to_custom_symbol_find = zero_find;
char buf[BUFSZ], *c_glyphname, *c_unicode, *c_colorval, *cp;
int reslt = 0;
long rgb = 0L;
boolean slash = FALSE, colon = FALSE;
if (!glyphname_hash_indices_ptr)
reslt = 1; /* for debugger use only; no cache available */
nhUse(reslt);
Snprintf(buf, sizeof buf, "%s", op);
c_unicode = c_colorval = (char *) 0;
c_glyphname = cp = buf;
while (*cp) {
if (*cp == ':' || *cp == '/') {
if (*cp == ':') {
colon = TRUE;
*cp = '\0';
}
if (*cp == '/') {
slash = TRUE;
*cp = '\0';
}
}
cp++;
if (colon) {
c_unicode = cp;
colon = FALSE;
}
if (slash) {
c_colorval = cp;
slash = FALSE;
}
}
/* some sanity checks */
if (c_glyphname && *c_glyphname == ' ')
c_glyphname++;
if (c_colorval && *c_colorval == ' ')
c_colorval++;
if (c_unicode && *c_unicode == ' ') {
while (*c_unicode == ' ') {
c_unicode++;
}
}
if (c_unicode && !*c_unicode)
c_unicode = 0;
if ((c_colorval && (rgb = rgbstr_to_int32(c_colorval)) != -1L)
|| !c_colorval) {
/* if the color 0 is an actual color, as opposed to just "not set"
we set a marker bit outside the 24-bit range to indicate a
valid color value 0. That allows valid color 0, but allows a
simple checking for 0 to detect "not set". The window port that
implements the color switch, needs to either check that bit
or appropriately mask colors with 0xFFFFFF. */
to_custom_symbol_find.color = (rgb == -1 || !c_colorval) ? 0L
: (rgb == 0L) ? nonzero_black
: rgb;
}
if (c_unicode)
to_custom_symbol_find.unicode_val = c_unicode;
to_custom_symbol_find.extraval = glyphptr;
to_custom_symbol_find.callback = to_custom_symset_entry_callback;
reslt = glyph_find_core(c_glyphname, &to_custom_symbol_find);
return reslt;
}
staticfn char *
fix_glyphname(char *str)
{
char *c;
for (c = str; *c; c++) {
if (*c >= 'A' && *c <= 'Z')
*c += (char) ('a' - 'A');
else if (*c >= '0' && *c <= '9')
;
else if (*c < 'a' || *c > 'z')
*c = '_';
}
return str;
}
/* First SYM_PCHAR index in loadsyms[], cached (0 = not computed yet;
SYM_PCHAR is never at index 0). */
static int cached_cmap_offset = 0;
staticfn int
get_cmap_offset(void)
{
if (!cached_cmap_offset) {
int i;
for (i = 0; loadsyms[i].range; i++) {
if (loadsyms[i].range == SYM_PCHAR) {
cached_cmap_offset = i;
break;
}
}
}
return cached_cmap_offset;
}
/* Build the canonical "G_xxx" identifier for the given glyph into buf.
* Returns 1 if a name was produced, 0 if this glyph has no canonical name
* (a few unused object indices); buf[0] is set to '\0' in that case.
* Callers must pass a BUFSZ-sized buffer.
* Used by find_glyph_in_hashtable to verify hash matches, by the
* --dumpglyphnames path, by populate_glyphname_hash_indices to fill the
* table, and by wizcustom_glyphnames. */
staticfn int
compose_glyph_name(int glyph, char *buf, size_t bufsz)
{
int i, j, mnum, cmap_offset;
boolean skip_base = FALSE;
const char *buf2, *buf3, *buf4;
char tmpbuf[4][QBUFSZ];
if (bufsz < BUFSZ)
return 0;
buf[0] = '\0';
tmpbuf[0][0] = tmpbuf[1][0] = tmpbuf[2][0] = tmpbuf[3][0] = '\0';
cmap_offset = get_cmap_offset();
if (glyph_is_monster(glyph)) {
buf2 = "";
buf3 = monsdump[glyph_to_mon(glyph)].nm;
if (glyph_is_normal_male_monster(glyph)) {
buf2 = "male_";
} else if (glyph_is_normal_female_monster(glyph)) {
buf2 = "female_";
} else if (glyph_is_ridden_male_monster(glyph)) {
buf2 = "ridden_male_";
} else if (glyph_is_ridden_female_monster(glyph)) {
buf2 = "ridden_female_";
} else if (glyph_is_detected_male_monster(glyph)) {
buf2 = "detected_male_";
} else if (glyph_is_detected_female_monster(glyph)) {
buf2 = "detected_female_";
} else if (glyph_is_male_pet(glyph)) {
buf2 = "pet_male_";
} else if (glyph_is_female_pet(glyph)) {
buf2 = "pet_female_";
}
Snprintf(buf, bufsz, "G_%s%s", buf2, buf3);
} else if (glyph_is_body(glyph)) {
buf2 = glyph_is_body_piletop(glyph) ? "piletop_body_" : "body_";
buf3 = monsdump[glyph_to_body_corpsenm(glyph)].nm;
Snprintf(buf, bufsz, "G_%s%s", buf2, buf3);
} else if (glyph_is_statue(glyph)) {
buf2 = glyph_is_fem_statue_piletop(glyph)
? "piletop_statue_of_female_"
: glyph_is_fem_statue(glyph)
? "statue_of_female_"
: glyph_is_male_statue_piletop(glyph)
? "piletop_statue_of_male_"
: glyph_is_male_statue(glyph)
? "statue_of_male_"
: "";
buf3 = monsdump[glyph_to_statue_corpsenm(glyph)].nm;
Snprintf(buf, bufsz, "G_%s%s", buf2, buf3);
} else if (glyph_is_object(glyph)) {
i = glyph_to_obj(glyph);
if (((i > SCR_STINKING_CLOUD) && (i < SCR_MAIL))
|| ((i > WAN_LIGHTNING) && (i < GOLD_PIECE))) {
return 0;
}
if ((i >= WAN_LIGHT) && (i <= WAN_LIGHTNING))
buf2 = "wand of ";
else if ((i >= SPE_DIG) && (i < SPE_BLANK_PAPER))
buf2 = "spellbook of ";
else if ((i >= SCR_ENCHANT_ARMOR) && (i <= SCR_STINKING_CLOUD))
buf2 = "scroll of ";
else if ((i >= POT_GAIN_ABILITY) && (i <= POT_WATER))
buf2 = (i == POT_WATER) ? "flask of n" : "potion of ";
else if ((i >= RIN_ADORNMENT) && (i <= RIN_PROTECTION_FROM_SHAPE_CHAN))
buf2 = "ring of ";
else if (i == LAND_MINE)
buf2 = "unset ";
else
buf2 = "";
buf3 = (i == SCR_BLANK_PAPER) ? "blank scroll"
: (i == SPE_BLANK_PAPER) ? "blank spellbook"
: (i == SLIME_MOLD) ? "slime mold"
: obj_descr[i].oc_name
? obj_descr[i].oc_name
: obj_descr[i].oc_descr;
Snprintf(buf, bufsz, "G_%s%s%s",
(glyph_is_normal_piletop_obj(glyph)
|| glyph_is_piletop_generic_obj(glyph)) ? "piletop_" : "",
buf2, buf3);
} else if (glyph_is_cmap(glyph) || glyph_is_cmap_zap(glyph)
|| glyph_is_swallow(glyph) || glyph_is_explosion(glyph)) {
int cmap = -1;
buf2 = "";
buf3 = "";
buf4 = "";
if (glyph == GLYPH_CMAP_OFF) {
cmap = S_stone;
buf3 = "stone substrate";
skip_base = TRUE;
} else if (glyph_is_cmap_gehennom(glyph)) {
cmap = (glyph - GLYPH_CMAP_GEH_OFF) + S_vwall;
buf4 = "_gehennom";
} else if (glyph_is_cmap_knox(glyph)) {
cmap = (glyph - GLYPH_CMAP_KNOX_OFF) + S_vwall;
buf4 = "_knox";
} else if (glyph_is_cmap_main(glyph)) {
cmap = (glyph - GLYPH_CMAP_MAIN_OFF) + S_vwall;
buf4 = "_main";
} else if (glyph_is_cmap_mines(glyph)) {
cmap = (glyph - GLYPH_CMAP_MINES_OFF) + S_vwall;
buf4 = "_mines";
} else if (glyph_is_cmap_sokoban(glyph)) {
cmap = (glyph - GLYPH_CMAP_SOKO_OFF) + S_vwall;
buf4 = "_sokoban";
} else if (glyph_is_cmap_a(glyph)) {
cmap = (glyph - GLYPH_CMAP_A_OFF) + S_ndoor;
} else if (glyph_is_cmap_altar(glyph)) {
static const char *const altar_text[] = {
"unaligned", "chaotic", "neutral",
"lawful", "other",
};
j = (glyph - GLYPH_ALTAR_OFF);
cmap = S_altar;
if (j != altar_other) {
Snprintf(tmpbuf[2], sizeof tmpbuf[2], "%s_", altar_text[j]);
buf2 = tmpbuf[2];
} else {
buf3 = "altar other";
skip_base = TRUE;
}
} else if (glyph_is_cmap_b(glyph)) {
cmap = (glyph - GLYPH_CMAP_B_OFF) + S_grave;
} else if (glyph_is_cmap_zap(glyph)) {
static const char *const zap_texts[] = {
"missile", "fire", "frost", "sleep",
"death", "lightning", "poison gas", "acid"
};
j = (glyph - GLYPH_ZAP_OFF);
cmap = (j % 4) + S_vbeam;
Snprintf(tmpbuf[2], sizeof tmpbuf[2], "%s",
loadsyms[cmap + cmap_offset].name + 2);
Snprintf(tmpbuf[3], sizeof tmpbuf[3], "%s zap %s",
zap_texts[j / 4], fix_glyphname(tmpbuf[2]));
buf3 = tmpbuf[3];
buf2 = "";
skip_base = TRUE;
} else if (glyph_is_cmap_c(glyph)) {
cmap = (glyph - GLYPH_CMAP_C_OFF) + S_digbeam;
} else if (glyph_is_swallow(glyph)) {
static const char *const swallow_texts[] = {
"top left", "top center", "top right",
"middle left", "middle right", "bottom left",
"bottom center", "bottom right",
};
j = glyph - GLYPH_SWALLOW_OFF;
cmap = glyph_to_swallow(glyph);
mnum = j / ((S_sw_br - S_sw_tl) + 1);
Snprintf(tmpbuf[3], sizeof tmpbuf[3], "swallow %s %s",
monsdump[mnum].nm, swallow_texts[cmap]);
buf3 = tmpbuf[3];
skip_base = TRUE;
} else if (glyph_is_explosion(glyph)) {
static const char *const expl_type_texts[] = {
"dark", "noxious", "muddy", "wet",
"magical", "fiery", "frosty",
};
static const char *const expl_texts[] = {
"tl", "tc", "tr", "ml", "mc",
"mr", "bl", "bc", "br",
};
int expl;
j = glyph - GLYPH_EXPLODE_OFF;
expl = j / ((S_expl_br - S_expl_tl) + 1);
cmap = glyph_to_explosion(glyph) + S_expl_tl;
i = cmap - S_expl_tl;
Snprintf(tmpbuf[2], sizeof tmpbuf[2], "%s ",
expl_type_texts[expl]);
buf2 = tmpbuf[2];
Snprintf(tmpbuf[3], sizeof tmpbuf[3], "%s%s", "expl_",
expl_texts[i]);
buf3 = tmpbuf[3];
skip_base = TRUE;
}
if (!skip_base) {
if (cmap >= 0 && cmap < MAXPCHARS)
buf3 = loadsyms[cmap + cmap_offset].name + 2;
}
Snprintf(buf, bufsz, "G_%s%s%s", buf2, buf3, buf4);
} else if (glyph_is_invisible(glyph)) {
Snprintf(buf, bufsz, "G_invisible");
} else if (glyph_is_nothing(glyph)) {
Snprintf(buf, bufsz, "G_nothing");
} else if (glyph_is_unexplored(glyph)) {
Snprintf(buf, bufsz, "G_unexplored");
} else if (glyph_is_warning(glyph)) {
j = glyph - GLYPH_WARNING_OFF;
Snprintf(buf, bufsz, "G_%s%d", "warning", j);
}
if (buf[0] == '\0')
return 0;
fix_glyphname(buf + 2);
nhUse(mnum);
return 1;
}
int
glyph_to_cmap(int glyph)
{
if (glyph == GLYPH_CMAP_STONE_OFF)
return S_stone;
else if (glyph_is_cmap_main(glyph))
return (glyph - GLYPH_CMAP_MAIN_OFF) + S_vwall;
else if (glyph_is_cmap_mines(glyph))
return (glyph - GLYPH_CMAP_MINES_OFF) + S_vwall;
else if (glyph_is_cmap_gehennom(glyph))
return (glyph - GLYPH_CMAP_GEH_OFF) + S_vwall;
else if (glyph_is_cmap_knox(glyph))
return (glyph - GLYPH_CMAP_KNOX_OFF) + S_vwall;
else if (glyph_is_cmap_sokoban(glyph))
return (glyph - GLYPH_CMAP_SOKO_OFF) + S_vwall;
else if (glyph_is_cmap_a(glyph))
return (glyph - GLYPH_CMAP_A_OFF) + S_ndoor;
else if (glyph_is_cmap_altar(glyph))
return S_altar;
else if (glyph_is_cmap_b(glyph))
return (glyph - GLYPH_CMAP_B_OFF) + S_grave;
else if (glyph_is_cmap_c(glyph))
return (glyph - GLYPH_CMAP_C_OFF) + S_digbeam;
else if (glyph_is_cmap_zap(glyph))
return ((glyph - GLYPH_ZAP_OFF) % 4) + S_vbeam;
else if (glyph_is_swallow(glyph))
return glyph_to_swallow(glyph) + S_sw_tl;
else if (glyph_is_explosion(glyph))
return glyph_to_explosion(glyph) + S_expl_tl;
else
return MAXPCHARS; /* MAXPCHARS is legal array index because
* of trailing fencepost entry */
}
staticfn int
glyph_find_core(
const char *id,
struct find_struct *findwhat)
{
int glyph;
boolean do_callback, end_find = FALSE;
if (parse_id(id, findwhat)) {
if (findwhat->findtype == find_glyph) {
(*findwhat->callback)(findwhat->val, findwhat);
} else {
for (glyph = 0; glyph < MAX_GLYPH; ++glyph) {
do_callback = FALSE;
switch (findwhat->findtype) {
case find_cmap:
if (glyph_to_cmap(glyph) == findwhat->val)
do_callback = TRUE;
break;
case find_pm:
if (glyph_is_monster(glyph)
&& mons[glyph_to_mon(glyph)].mlet
== findwhat->val)
do_callback = TRUE;
break;
case find_oc:
if (glyph_is_object(glyph)
&& glyph_to_obj(glyph) == findwhat->val)
do_callback = TRUE;
break;
case find_glyph:
if (glyph == findwhat->val) {
do_callback = TRUE;
end_find = TRUE;
}
break;
case find_nothing:
default:
end_find = TRUE;
break;
}
if (do_callback)
(findwhat->callback)(glyph, findwhat);
if (end_find)
break;
}
}
return 1;
}
return 0;
}
/*
* glyphname_hash_indices is a sorted (hash, glyph) index of canonical
* "G_xxx" identifiers. populate_glyphname_hash_indices() allocates one
* block of MAX_GLYPH * sizeof(entry) (some entries go unused for the
* scroll/gem appearance gaps), fills it via compose_glyph_name(), and
* sorts ascending by hash. Lookup is bsearch on the hash with
* compose_glyph_name()+strcmpi to disambiguate the rare collision.
*
* The name "hashtable" is historical; this is a sorted array, not an
* open-addressed hash table. The on-disk API in extern.h is kept so
* that callers (wizcmds.c, options.c) don't need to change.
*
* Memory: one alloc, ~75 KB for the 9577 named slots on a typical 5.0
* build (struct is 8 bytes, no per-name strings). The same data in
* upstream's open-addressed table cost ~256 KB for the buckets plus
* ~290 KB of dupstr'd names plus malloc overhead.
*/
staticfn int
cmp_glyphname_entry(const void *a, const void *b)
{
uint32 ha = ((const struct glyphname_hash_index_entry_t *) a)->hash;
uint32 hb = ((const struct glyphname_hash_index_entry_t *) b)->hash;
if (ha < hb)
return -1;
if (ha > hb)
return 1;
return 0;
}
void
populate_glyphname_hash_indices(void)
{
int glyph;
size_t n = 0;
char buf[BUFSZ];
if (glyphname_hash_indices_ptr)
return;
glyphname_hash_indices_ptr = (struct glyphname_hash_index_entry_t *) alloc(
MAX_GLYPH * sizeof (struct glyphname_hash_index_entry_t));
for (glyph = 0; glyph < MAX_GLYPH; ++glyph) {
if (compose_glyph_name(glyph, buf, sizeof buf)) {
glyphname_hash_indices_ptr[n].hash = glyph_hash(buf);
glyphname_hash_indices_ptr[n].glyphnum = glyph;
++n;
}
}
qsort(glyphname_hash_indices_ptr, n,
sizeof glyphname_hash_indices_ptr[0], cmp_glyphname_entry);
glyphname_hash_indices_count = n;
}
void
empty_glyphname_hash_indices(void)
{
if (!glyphname_hash_indices_ptr)
return;
free(glyphname_hash_indices_ptr);
glyphname_hash_indices_ptr = (struct glyphname_hash_index_entry_t *) 0;
glyphname_hash_indices_count = 0;
}
staticfn int
find_glyph_in_hashtable(const char *id)
{
uint32 want = glyph_hash(id);
size_t lo = 0, hi = glyphname_hash_indices_count, mid;
char buf[BUFSZ];
/* Binary-search the sorted array for the first entry whose hash >= want. */
while (lo < hi) {
mid = (lo + hi) >> 1;
if (glyphname_hash_indices_ptr[mid].hash < want)
lo = mid + 1;
else
hi = mid;
}
/* Walk forward across any equal-hash neighbours, verifying each by
reconstructing the canonical name and strcmpi'ing it back. */
while (lo < glyphname_hash_indices_count
&& glyphname_hash_indices_ptr[lo].hash == want) {
int g = glyphname_hash_indices_ptr[lo].glyphnum;
if (compose_glyph_name(g, buf, sizeof buf) && !strcmpi(id, buf))
return g;
++lo;
}
return -1;
}
staticfn uint32
glyph_hash(const char *id)
{
uint32 hash = 0;
size_t i;
for (i = 0; id[i] != '\0'; ++i) {
char ch = id[i];
if ('A' <= ch && ch <= 'Z') {
ch += 'a' - 'A';
}
hash = (hash << 5) | (hash >> 27);
hash ^= ch;
}
return hash;
}
boolean
glyphname_hash_indices_loaded(void)
{
return (glyphname_hash_indices_ptr != 0);
}
int
match_glyph(char *buf)
{
char workbuf[BUFSZ];
/* buf contains a G_ glyph reference, not an S_ symbol.
There could be an R-G-B color attached too.
Let's get a copy to work with. */
Snprintf(workbuf, sizeof workbuf, "%s", buf); /* get a copy */
return glyphrep(workbuf);
}
int
glyphrep(const char *op)
{
int reslt = 0, glyph = NO_GLYPH;
if (!glyphname_hash_indices_ptr)
reslt = 1; /* for debugger use only; no cache available */
nhUse(reslt);
reslt = glyphrep_to_custom_map_entries(op, &glyph);
if (reslt)
return 1;
return 0;
}
int
add_custom_nhcolor_entry(
const char *customization_name,
int glyphidx,
uint32 nhcolor,
enum graphics_sets which_set)
{
struct symset_customization *gdc
= &gs.sym_customizations[which_set][custom_nhcolor];
struct customization_detail *details, *newdetails = 0;
#if 0
static uint32 closecolor = 0;
static int clridx = 0;
#endif
if (!gdc->details) {
gdc->customization_name = dupstr(customization_name);
gdc->custtype = custom_nhcolor;
gdc->details = 0;
gdc->details_end = 0;
}
details = find_matching_customization(customization_name,
custom_nhcolor, which_set);
if (details) {
while (details) {
if (details->content.ccolor.glyphidx == glyphidx) {
details->content.ccolor.nhcolor = nhcolor;
return 1;
}
details = details->next;
}
}
/* create new details entry */
newdetails = (struct customization_detail *) alloc(sizeof *newdetails);
newdetails->content.urep.glyphidx = glyphidx;
newdetails->content.ccolor.nhcolor = nhcolor;
newdetails->next = (struct customization_detail *) 0;
if (gdc->details == NULL) {
gdc->details = newdetails;
} else {
gdc->details_end->next = newdetails;
}
gdc->details_end = newdetails;
gdc->count++;
return 1;
}
void
apply_customizations(
enum graphics_sets which_set,
enum do_customizations docustomize)
{
glyph_map *gmap;
struct customization_detail *details;
struct symset_customization *sc;
boolean at_least_one = FALSE,
do_colors = ((docustomize & do_custom_colors) != 0),
do_symbols = ((docustomize & do_custom_symbols) != 0);
int custs;
for (custs = 0; custs < (int) custom_count; ++custs) {
sc = &gs.sym_customizations[which_set][custs];
if (sc->count && sc->details) {
at_least_one = TRUE;
/* These glyph customizations get applied to the glyphmap array,
not to symset entries */
details = sc->details;
while (details) {
#ifdef ENHANCED_SYMBOLS
if (iflags.customsymbols && do_symbols) {
if (sc->custtype == custom_ureps) {
gmap = &glyphmap[details->content.urep.glyphidx];
if (gs.symset[which_set].handling == H_UTF8)
(void) set_map_u(gmap,
details->content.urep.u.utf32ch,
details->content.urep.u.utf8str);
}
}
#endif
if (iflags.customcolors && do_colors) {
if (sc->custtype == custom_nhcolor) {
gmap = &glyphmap[details->content.ccolor.glyphidx];
(void) set_map_customcolor(gmap,
details->content.ccolor.nhcolor);
}
}
details = details->next;
}
}
}
iflags.pending_customizations = at_least_one;
}
/* Shuffle the customizations to match shuffled object descriptions,
* so a red potion isn't displayed with a blue customization, and so on.
*/
void
maybe_shuffle_customizations(void)
{
if (iflags.pending_customizations) {
shuffle_customizations();
iflags.pending_customizations = 0;
}
}
#if 0
staticfn void
shuffle_customizations(void)
{
static const int offsets[2] = { GLYPH_OBJ_OFF, GLYPH_OBJ_PILETOP_OFF };
int j;
for (j = 0; j < SIZE(offsets); j++) {
glyph_map *obj_glyphs = glyphmap + offsets[j];
int i;
struct unicode_representation *tmp_u[NUM_OBJECTS];
int duplicate[NUM_OBJECTS];
for (i = 0; i < NUM_OBJECTS; i++) {
duplicate[i] = -1;
tmp_u[i] = (struct unicode_representation *) 0;
}
for (i = 0; i < NUM_OBJECTS; i++) {
int idx = objects[i].oc_descr_idx;
/*
* Shuffling gem appearances can cause the same oc_descr_idx to
* appear more than once. Detect this condition and ensure that
* each pointer points to a unique allocation.
*/
if (duplicate[idx] >= 0) {
/* Current structure already appears in tmp_u */
struct unicode_representation *other = tmp_u[duplicate[idx]];
tmp_u[i] = (struct unicode_representation *)
alloc(sizeof *tmp_u[i]);
*tmp_u[i] = *other;
if (other->utf8str != NULL) {
tmp_u[i]->utf8str = (uint8 *)
dupstr((const char *) other->utf8str);
}
} else {
tmp_u[i] = obj_glyphs[idx].u;
if (obj_glyphs[idx].u != NULL) {
duplicate[idx] = i;
obj_glyphs[idx].u = NULL;
}
}
}
for (i = 0; i < NUM_OBJECTS; i++) {
/* Some glyphmaps may not have been transferred */
if (obj_glyphs[i].u != NULL) {
free(obj_glyphs[i].u->utf8str);
free(obj_glyphs[i].u);
}
obj_glyphs[i].u = tmp_u[i];
}
}
}
#else
staticfn void
shuffle_customizations(void)
{
static const int offsets[2] = { GLYPH_OBJ_OFF, GLYPH_OBJ_PILETOP_OFF };
int j;
for (j = 0; j < SIZE(offsets); j++) {
glyph_map *obj_glyphs = glyphmap + offsets[j];
int i;
#ifdef ENHANCED_SYMBOLS
struct unicode_representation *tmp_u[NUM_OBJECTS];
#endif
uint32 tmp_customcolor[NUM_OBJECTS];
uint16 tmp_color256idx[NUM_OBJECTS];
int duplicate[NUM_OBJECTS];
for (i = 0; i < NUM_OBJECTS; i++) {
duplicate[i] = -1;
#ifdef ENHANCED_SYMBOLS
tmp_u[i] = (struct unicode_representation *) 0;
#endif
tmp_customcolor[i] = 0;
tmp_color256idx[i] = 0;
}
for (i = 0; i < NUM_OBJECTS; i++) {
int idx = objects[i].oc_descr_idx;
/*
* Shuffling gem appearances can cause the same oc_descr_idx to
* appear more than once. Detect this condition and ensure that
* each pointer points to a unique allocation.
*/
if (duplicate[idx] >= 0) {
#ifdef ENHANCED_SYMBOLS
/* Current structure already appears in tmp_u */
struct unicode_representation *other = tmp_u[duplicate[idx]];
#endif
uint32 other_customcolor = tmp_customcolor[duplicate[idx]];
uint16 other_color256idx = tmp_color256idx[duplicate[idx]];
tmp_customcolor[i] = other_customcolor;
tmp_color256idx[i] = other_color256idx;
#ifdef ENHANCED_SYMBOLS
if (other) {
tmp_u[i] = (struct unicode_representation *) alloc(
sizeof *tmp_u[i]);
*tmp_u[i] = *other;
if (other->utf8str != NULL) {
tmp_u[i]->utf8str =
(uint8 *) dupstr((const char *) other->utf8str);
}
}
#endif
} else {
tmp_customcolor[i] = obj_glyphs[idx].customcolor;
tmp_color256idx[i] = obj_glyphs[idx].color256idx;
#ifdef ENHANCED_SYMBOLS
tmp_u[i] = obj_glyphs[idx].u;
#endif
if (
#ifdef ENHANCED_SYMBOLS
obj_glyphs[idx].u != NULL ||
#endif
obj_glyphs[idx].customcolor != 0) {
duplicate[idx] = i;
#ifdef ENHANCED_SYMBOLS
obj_glyphs[idx].u = NULL;
#endif
obj_glyphs[idx].customcolor = 0;
obj_glyphs[idx].color256idx = 0;
}
}
}
for (i = 0; i < NUM_OBJECTS; i++) {
/* Some glyphmaps may not have been transferred */
#ifdef ENHANCED_SYMBOLS
if (obj_glyphs[i].u != NULL) {
free(obj_glyphs[i].u->utf8str);
free(obj_glyphs[i].u);
}
obj_glyphs[i].u = tmp_u[i];
#endif
obj_glyphs[i].customcolor = tmp_customcolor[i];
obj_glyphs[i].color256idx = tmp_color256idx[i];
}
}
}
#endif
struct customization_detail *
find_matching_customization(
const char *customization_name,
enum customization_types custtype,
enum graphics_sets which_set)
{
struct symset_customization *gdc
= &gs.sym_customizations[which_set][custtype];
if ((gdc->custtype == custtype) && gdc->customization_name
&& (strcmp(customization_name, gdc->customization_name) == 0))
return gdc->details;
return (struct customization_detail *) 0;
}
void
purge_all_custom_entries(void)
{
int i;
for (i = 0; i < NUM_GRAPHICS + 1; ++i) {
purge_custom_entries(i);
}
}
void
purge_custom_entries(enum graphics_sets which_set)
{
enum customization_types custtype;
struct symset_customization *gdc;
struct customization_detail *details, *next;
for (custtype = custom_none; custtype < custom_count; ++custtype) {
gdc = &gs.sym_customizations[which_set][custtype];
details = gdc->details;
while (details) {
next = details->next;
if (gdc->custtype == custom_ureps) {
if (details->content.urep.u.utf8str)
free(details->content.urep.u.utf8str);
details->content.urep.u.utf8str = (uint8 *) 0;
} else if (gdc->custtype == custom_symbols) {
details->content.sym.symparse = (struct symparse *) 0;
details->content.sym.val = 0;
} else if (gdc->custtype == custom_nhcolor) {
details->content.ccolor.nhcolor = 0;
details->content.ccolor.glyphidx = 0;
}
free(details);
details = next;
}
gdc->details = 0;
gdc->details_end = 0;
if (gdc->customization_name) {
free((genericptr_t) gdc->customization_name);
gdc->customization_name = 0;
}
gdc->count = 0;
}
}
void
dump_all_glyphnames(FILE *fp)
{
struct find_struct dump_glyphname_find = zero_find;
dump_glyphname_find.findtype = find_nothing;
dump_glyphname_find.reserved = (genericptr_t) fp;
dump_glyphname_find.restype = res_dump_glyphnames;
(void) parse_id((char *) 0, &dump_glyphname_find);
}
void
wizcustom_glyphnames(winid win)
{
int glyphnum;
char buf[BUFSZ];
for (glyphnum = 0; glyphnum < MAX_GLYPH; ++glyphnum) {
if (compose_glyph_name(glyphnum, buf, sizeof buf))
wizcustom_callback(win, glyphnum, buf);
}
}
staticfn int
parse_id(
const char *id,
struct find_struct *findwhat)
{
FILE *fp = (FILE *) 0;
int i = 0, glyph,
pm_offset = 0, oc_offset = 0, cmap_offset = 0,
pm_count = 0, oc_count = 0, cmap_count = 0;