Source:NetHack 3.6.2/src/botl.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to botl.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/botl.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	botl.c	$NHDT-Date: 1557094795 2019/05/05 22:19:55 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.145 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Michael Allison, 2006. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #ifndef LONG_MAX
8. #include <limits.h>
9. #endif
10. 
11. extern const char *hu_stat[]; /* defined in eat.c */
12. 
13. const char *const enc_stat[] = { "",         "Burdened",  "Stressed",
14.                                  "Strained", "Overtaxed", "Overloaded" };
15. 
16. STATIC_OVL NEARDATA int mrank_sz = 0; /* loaded by max_rank_sz (from u_init) */
17. STATIC_DCL const char *NDECL(rank);
18. STATIC_DCL void NDECL(bot_via_windowport);
19. STATIC_DCL void NDECL(stat_update_time);
20. 
21. static char *
22. get_strength_str()
23. {
24.     static char buf[32];
25.     int st = ACURR(A_STR);
26. 
27.     if (st > 18) {
28.         if (st > STR18(100))
29.             Sprintf(buf, "%2d", st - 100);
30.         else if (st < STR18(100))
31.             Sprintf(buf, "18/%02d", st - 18);
32.         else
33.             Sprintf(buf, "18/**");
34.     } else
35.         Sprintf(buf, "%-1d", st);
36. 
37.     return buf;
38. }
39. 
40. void
41. check_gold_symbol()
42. {
43.     nhsym goldch = showsyms[COIN_CLASS + SYM_OFF_O];
44. 
45.     iflags.invis_goldsym = (goldch <= (nhsym) ' ');
46. }
47. 
48. char *
49. do_statusline1()
50. {
51.     static char newbot1[BUFSZ];
52.     register char *nb;
53.     register int i, j;
54. 
55.     Strcpy(newbot1, plname);
56.     if ('a' <= newbot1[0] && newbot1[0] <= 'z')
57.         newbot1[0] += 'A' - 'a';
58.     newbot1[10] = 0;
59.     Sprintf(nb = eos(newbot1), " the ");
60. 
61.     if (Upolyd) {
62.         char mbot[BUFSZ];
63.         int k = 0;
64. 
65.         Strcpy(mbot, mons[u.umonnum].mname);
66.         while (mbot[k] != 0) {
67.             if ((k == 0 || (k > 0 && mbot[k - 1] == ' ')) && 'a' <= mbot[k]
68.                 && mbot[k] <= 'z')
69.                 mbot[k] += 'A' - 'a';
70.             k++;
71.         }
72.         Strcpy(nb = eos(nb), mbot);
73.     } else
74.         Strcpy(nb = eos(nb), rank());
75. 
76.     Sprintf(nb = eos(nb), "  ");
77.     i = mrank_sz + 15;
78.     j = (int) ((nb + 2) - newbot1); /* strlen(newbot1) but less computation */
79.     if ((i - j) > 0)
80.         Sprintf(nb = eos(nb), "%*s", i - j, " "); /* pad with spaces */
81. 
82.     Sprintf(nb = eos(nb), "St:%s Dx:%-1d Co:%-1d In:%-1d Wi:%-1d Ch:%-1d",
83.             get_strength_str(),
84.             ACURR(A_DEX), ACURR(A_CON), ACURR(A_INT), ACURR(A_WIS),
85.             ACURR(A_CHA));
86.     Sprintf(nb = eos(nb),
87.             (u.ualign.type == A_CHAOTIC)
88.                 ? "  Chaotic"
89.                 : (u.ualign.type == A_NEUTRAL) ? "  Neutral" : "  Lawful");
90. #ifdef SCORE_ON_BOTL
91.     if (flags.showscore)
92.         Sprintf(nb = eos(nb), " S:%ld", botl_score());
93. #endif
94.     return newbot1;
95. }
96. 
97. char *
98. do_statusline2()
99. {
100.     static char newbot2[BUFSZ], /* MAXCO: botl.h */
101.          /* dungeon location (and gold), hero health (HP, PW, AC),
102.             experience (HD if poly'd, else Exp level and maybe Exp points),
103.             time (in moves), varying number of status conditions */
104.          dloc[QBUFSZ], hlth[QBUFSZ], expr[QBUFSZ], tmmv[QBUFSZ], cond[QBUFSZ];
105.     register char *nb;
106.     unsigned dln, dx, hln, xln, tln, cln;
107.     int hp, hpmax, cap;
108.     long money;
109. 
110.     /*
111.      * Various min(x,9999)'s are to avoid having excessive values
112.      * violate the field width assumptions in botl.h and should not
113.      * impact normal play.  Particularly 64-bit long for gold which
114.      * could require many more digits if someone figures out a way
115.      * to get and carry a really large (or negative) amount of it.
116.      * Turn counter is also long, but we'll risk that.
117.      */
118. 
119.     /* dungeon location plus gold */
120.     (void) describe_level(dloc); /* includes at least one trailing space */
121.     if ((money = money_cnt(invent)) < 0L)
122.         money = 0L; /* ought to issue impossible() and then discard gold */
123.     Sprintf(eos(dloc), "%s:%-2ld", /* strongest hero can lift ~300000 gold */
124.             (iflags.in_dumplog || iflags.invis_goldsym) ? "$"
125.               : encglyph(objnum_to_glyph(GOLD_PIECE)),
126.             min(money, 999999L));
127.     dln = strlen(dloc);
128.     /* '$' encoded as \GXXXXNNNN is 9 chars longer than display will need */
129.     dx = strstri(dloc, "\\G") ? 9 : 0;
130. 
131.     /* health and armor class (has trailing space for AC 0..9) */
132.     hp = Upolyd ? u.mh : u.uhp;
133.     hpmax = Upolyd ? u.mhmax : u.uhpmax;
134.     if (hp < 0)
135.         hp = 0;
136.     Sprintf(hlth, "HP:%d(%d) Pw:%d(%d) AC:%-2d",
137.             min(hp, 9999), min(hpmax, 9999),
138.             min(u.uen, 9999), min(u.uenmax, 9999), u.uac);
139.     hln = strlen(hlth);
140. 
141.     /* experience */
142.     if (Upolyd)
143.         Sprintf(expr, "HD:%d", mons[u.umonnum].mlevel);
144.     else if (flags.showexp)
145.         Sprintf(expr, "Xp:%u/%-1ld", u.ulevel, u.uexp);
146.     else
147.         Sprintf(expr, "Exp:%u", u.ulevel);
148.     xln = strlen(expr);
149. 
150.     /* time/move counter */
151.     if (flags.time)
152.         Sprintf(tmmv, "T:%ld", moves);
153.     else
154.         tmmv[0] = '\0';
155.     tln = strlen(tmmv);
156. 
157.     /* status conditions; worst ones first */
158.     cond[0] = '\0'; /* once non-empty, cond will have a leading space */
159.     nb = cond;
160.     /*
161.      * Stoned, Slimed, Strangled, and both types of Sick are all fatal
162.      * unless remedied before timeout expires.  Should we order them by
163.      * shortest time left?  [Probably not worth the effort, since it's
164.      * unusual for more than one of them to apply at a time.]
165.      */
166.     if (Stoned)
167.         Strcpy(nb = eos(nb), " Stone");
168.     if (Slimed)
169.         Strcpy(nb = eos(nb), " Slime");
170.     if (Strangled)
171.         Strcpy(nb = eos(nb), " Strngl");
172.     if (Sick) {
173.         if (u.usick_type & SICK_VOMITABLE)
174.             Strcpy(nb = eos(nb), " FoodPois");
175.         if (u.usick_type & SICK_NONVOMITABLE)
176.             Strcpy(nb = eos(nb), " TermIll");
177.     }
178.     if (u.uhs != NOT_HUNGRY)
179.         Sprintf(nb = eos(nb), " %s", hu_stat[u.uhs]);
180.     if ((cap = near_capacity()) > UNENCUMBERED)
181.         Sprintf(nb = eos(nb), " %s", enc_stat[cap]);
182.     if (Blind)
183.         Strcpy(nb = eos(nb), " Blind");
184.     if (Deaf)
185.         Strcpy(nb = eos(nb), " Deaf");
186.     if (Stunned)
187.         Strcpy(nb = eos(nb), " Stun");
188.     if (Confusion)
189.         Strcpy(nb = eos(nb), " Conf");
190.     if (Hallucination)
191.         Strcpy(nb = eos(nb), " Hallu");
192.     /* levitation and flying are mutually exclusive; riding is not */
193.     if (Levitation)
194.         Strcpy(nb = eos(nb), " Lev");
195.     if (Flying)
196.         Strcpy(nb = eos(nb), " Fly");
197.     if (u.usteed)
198.         Strcpy(nb = eos(nb), " Ride");
199.     cln = strlen(cond);
200. 
201.     /*
202.      * Put the pieces together.  If they all fit, keep the traditional
203.      * sequence.  Otherwise, move least important parts to the end in
204.      * case the interface side of things has to truncate.  Note that
205.      * dloc[] contains '$' encoded in ten character sequence \GXXXXNNNN
206.      * so we want to test its display length rather than buffer length.
207.      *
208.      * We don't have an actual display limit here, so have to go by the
209.      * width of the map.  Since we're reordering rather than truncating,
210.      * wider displays can still show wider status than the map if the
211.      * interface supports that.
212.      */
213.     if ((dln - dx) + 1 + hln + 1 + xln + 1 + tln + 1 + cln <= COLNO) {
214.         Sprintf(newbot2, "%s %s %s %s %s", dloc, hlth, expr, tmmv, cond);
215.     } else {
216.         if (dln + 1 + hln + 1 + xln + 1 + tln + 1 + cln + 1 > MAXCO) {
217.             panic("bot2: second status line exceeds MAXCO (%u > %d)",
218.                   (dln + 1 + hln + 1 + xln + 1 + tln + 1 + cln + 1), MAXCO);
219.         } else if ((dln - dx) + 1 + hln + 1 + xln + 1 + cln <= COLNO) {
220.             Sprintf(newbot2, "%s %s %s %s %s", dloc, hlth, expr, cond, tmmv);
221.         } else if ((dln - dx) + 1 + hln + 1 + cln <= COLNO) {
222.             Sprintf(newbot2, "%s %s %s %s %s", dloc, hlth, cond, expr, tmmv);
223.         } else {
224.             Sprintf(newbot2, "%s %s %s %s %s", hlth, cond, dloc, expr, tmmv);
225.         }
226.         /* only two or three consecutive spaces available to squeeze out */
227.         mungspaces(newbot2);
228.     }
229.     return newbot2;
230. }
231. 
232. void
233. bot()
234. {
235.     /* dosave() flags completion by setting u.uhp to -1 */
236.     if ((u.uhp != -1) && youmonst.data && iflags.status_updates) {
237.         if (VIA_WINDOWPORT()) {
238.             bot_via_windowport();
239.         } else {
240.             curs(WIN_STATUS, 1, 0);
241.             putstr(WIN_STATUS, 0, do_statusline1());
242.             curs(WIN_STATUS, 1, 1);
243.             putmixed(WIN_STATUS, 0, do_statusline2());
244.         }
245.     }
246.     context.botl = context.botlx = iflags.time_botl = FALSE;
247. }
248. 
249. void
250. timebot()
251. {
252.     if (flags.time && iflags.status_updates) {
253.         if (VIA_WINDOWPORT()) {
254.             stat_update_time();
255.         } else {
256.             /* old status display updates everything */
257.             bot();
258.         }
259.     }
260.     iflags.time_botl = FALSE;
261. }
262. 
263. /* convert experience level (1..30) to rank index (0..8) */
264. int
265. xlev_to_rank(xlev)
266. int xlev;
267. {
268.     return (xlev <= 2) ? 0 : (xlev <= 30) ? ((xlev + 2) / 4) : 8;
269. }
270. 
271. #if 0 /* not currently needed */
272. /* convert rank index (0..8) to experience level (1..30) */
273. int
274. rank_to_xlev(rank)
275. int rank;
276. {
277.     return (rank <= 0) ? 1 : (rank <= 8) ? ((rank * 4) - 2) : 30;
278. }
279. #endif
280. 
281. const char *
282. rank_of(lev, monnum, female)
283. int lev;
284. short monnum;
285. boolean female;
286. {
287.     register const struct Role *role;
288.     register int i;
289. 
290.     /* Find the role */
291.     for (role = roles; role->name.m; role++)
292.         if (monnum == role->malenum || monnum == role->femalenum)
293.             break;
294.     if (!role->name.m)
295.         role = &urole;
296. 
297.     /* Find the rank */
298.     for (i = xlev_to_rank((int) lev); i >= 0; i--) {
299.         if (female && role->rank[i].f)
300.             return role->rank[i].f;
301.         if (role->rank[i].m)
302.             return role->rank[i].m;
303.     }
304. 
305.     /* Try the role name, instead */
306.     if (female && role->name.f)
307.         return role->name.f;
308.     else if (role->name.m)
309.         return role->name.m;
310.     return "Player";
311. }
312. 
313. STATIC_OVL const char *
314. rank()
315. {
316.     return rank_of(u.ulevel, Role_switch, flags.female);
317. }
318. 
319. int
320. title_to_mon(str, rank_indx, title_length)
321. const char *str;
322. int *rank_indx, *title_length;
323. {
324.     register int i, j;
325. 
326.     /* Loop through each of the roles */
327.     for (i = 0; roles[i].name.m; i++)
328.         for (j = 0; j < 9; j++) {
329.             if (roles[i].rank[j].m
330.                 && !strncmpi(str, roles[i].rank[j].m,
331.                              strlen(roles[i].rank[j].m))) {
332.                 if (rank_indx)
333.                     *rank_indx = j;
334.                 if (title_length)
335.                     *title_length = strlen(roles[i].rank[j].m);
336.                 return roles[i].malenum;
337.             }
338.             if (roles[i].rank[j].f
339.                 && !strncmpi(str, roles[i].rank[j].f,
340.                              strlen(roles[i].rank[j].f))) {
341.                 if (rank_indx)
342.                     *rank_indx = j;
343.                 if (title_length)
344.                     *title_length = strlen(roles[i].rank[j].f);
345.                 return (roles[i].femalenum != NON_PM) ? roles[i].femalenum
346.                                                       : roles[i].malenum;
347.             }
348.         }
349.     return NON_PM;
350. }
351. 
352. void
353. max_rank_sz()
354. {
355.     register int i, r, maxr = 0;
356.     for (i = 0; i < 9; i++) {
357.         if (urole.rank[i].m && (r = strlen(urole.rank[i].m)) > maxr)
358.             maxr = r;
359.         if (urole.rank[i].f && (r = strlen(urole.rank[i].f)) > maxr)
360.             maxr = r;
361.     }
362.     mrank_sz = maxr;
363.     return;
364. }
365. 
366. #ifdef SCORE_ON_BOTL
367. long
368. botl_score()
369. {
370.     long deepest = deepest_lev_reached(FALSE);
371.     long utotal;
372. 
373.     utotal = money_cnt(invent) + hidden_gold();
374.     if ((utotal -= u.umoney0) < 0L)
375.         utotal = 0L;
376.     utotal += u.urexp + (50 * (deepest - 1))
377.           + (deepest > 30 ? 10000 : deepest > 20 ? 1000 * (deepest - 20) : 0);
378.     if (utotal < u.urexp)
379.         utotal = LONG_MAX; /* wrap around */
380.     return utotal;
381. }
382. #endif /* SCORE_ON_BOTL */
383. 
384. /* provide the name of the current level for display by various ports */
385. int
386. describe_level(buf)
387. char *buf;
388. {
389.     int ret = 1;
390. 
391.     /* TODO:    Add in dungeon name */
392.     if (Is_knox(&u.uz)) {
393.         Sprintf(buf, "%s ", dungeons[u.uz.dnum].dname);
394.     } else if (In_quest(&u.uz)) {
395.         Sprintf(buf, "Home %d ", dunlev(&u.uz));
396.     } else if (In_endgame(&u.uz)) {
397.         /* [3.6.2: this used to be "Astral Plane" or generic "End Game"] */
398.         (void) endgamelevelname(buf, depth(&u.uz));
399.         (void) strsubst(buf, "Plane of ", ""); /* just keep <element> */
400.         Strcat(buf, " ");
401.     } else {
402.         /* ports with more room may expand this one */
403.         Sprintf(buf, "Dlvl:%-2d ", depth(&u.uz));
404.         ret = 0;
405.     }
406.     return ret;
407. }
408. 
409. /* =======================================================================*/
410. /*  statusnew routines                                                    */
411. /* =======================================================================*/
412. 
413. /* structure that tracks the status details in the core */
414. 
415. #define MAXVALWIDTH 80 /* actually less, but was using 80 to allocate title
416.                         * and leveldesc then using QBUFSZ everywhere else   */
417. #ifdef STATUS_HILITES
418. struct hilite_s {
419.     enum statusfields fld;
420.     boolean set;
421.     unsigned anytype;
422.     anything value;
423.     int behavior;
424.     char textmatch[MAXVALWIDTH];
425.     enum relationships rel;
426.     int coloridx;
427.     struct hilite_s *next;
428. };
429. #endif /* STATUS_HILITES */
430. 
431. struct istat_s {
432.     const char *fldname;
433.     const char *fldfmt;
434.     long time;  /* moves when this field hilite times out */
435.     boolean chg; /* need to recalc time? */
436.     unsigned anytype;
437.     anything a;
438.     char *val;
439.     int valwidth;
440.     enum statusfields idxmax;
441.     enum statusfields fld;
442. #ifdef STATUS_HILITES
443.     struct hilite_s *hilite_rule; /* the entry, if any, in 'thresholds'
444.                                    * list that currently applies        */
445.     struct hilite_s *thresholds;
446. #endif
447. };
448. 
449. STATIC_DCL boolean FDECL(eval_notify_windowport_field, (int, boolean *, int));
450. STATIC_DCL void FDECL(evaluate_and_notify_windowport, (boolean *, int));
451. STATIC_DCL void NDECL(init_blstats);
452. STATIC_DCL int FDECL(compare_blstats, (struct istat_s *, struct istat_s *));
453. STATIC_DCL char *FDECL(anything_to_s, (char *, anything *, int));
454. STATIC_DCL int FDECL(percentage, (struct istat_s *, struct istat_s *));
455. 
456. #ifdef STATUS_HILITES
457. STATIC_DCL void FDECL(s_to_anything, (anything *, char *, int));
458. STATIC_DCL enum statusfields FDECL(fldname_to_bl_indx, (const char *));
459. STATIC_DCL boolean FDECL(hilite_reset_needed, (struct istat_s *, long));
460. STATIC_DCL boolean FDECL(noneoftheabove, (const char *));
461. STATIC_DCL struct hilite_s *FDECL(get_hilite, (int, int, genericptr_t,
462.                                                int, int, int *));
463. STATIC_DCL void FDECL(split_clridx, (int, int *, int *));
464. STATIC_DCL boolean FDECL(is_ltgt_percentnumber, (const char *));
465. STATIC_DCL boolean FDECL(has_ltgt_percentnumber, (const char *));
466. STATIC_DCL int FDECL(splitsubfields, (char *, char ***, int));
467. STATIC_DCL boolean FDECL(is_fld_arrayvalues, (const char *,
468.                                               const char *const *,
469.                                               int, int, int *));
470. STATIC_DCL int FDECL(query_arrayvalue, (const char *, const char *const *,
471.                                         int, int));
472. STATIC_DCL void FDECL(status_hilite_add_threshold, (int, struct hilite_s *));
473. STATIC_DCL boolean FDECL(parse_status_hl2, (char (*)[QBUFSZ], BOOLEAN_P));
474. STATIC_DCL char *FDECL(conditionbitmask2str, (unsigned long));
475. STATIC_DCL unsigned long FDECL(match_str2conditionbitmask, (const char *));
476. STATIC_DCL unsigned long FDECL(str2conditionbitmask, (char *));
477. STATIC_DCL boolean FDECL(parse_condition, (char (*)[QBUFSZ], int));
478. STATIC_DCL char *FDECL(hlattr2attrname, (int, char *, int));
479. STATIC_DCL void FDECL(status_hilite_linestr_add, (int, struct hilite_s *,
480.                                                 unsigned long, const char *));
481. STATIC_DCL void NDECL(status_hilite_linestr_done);
482. STATIC_DCL int FDECL(status_hilite_linestr_countfield, (int));
483. STATIC_DCL void NDECL(status_hilite_linestr_gather_conditions);
484. STATIC_DCL void NDECL(status_hilite_linestr_gather);
485. STATIC_DCL char *FDECL(status_hilite2str, (struct hilite_s *));
486. STATIC_DCL int NDECL(status_hilite_menu_choose_field);
487. STATIC_DCL int FDECL(status_hilite_menu_choose_behavior, (int));
488. STATIC_DCL int FDECL(status_hilite_menu_choose_updownboth, (int, const char *,
489.                                                        BOOLEAN_P, BOOLEAN_P));
490. STATIC_DCL boolean FDECL(status_hilite_menu_add, (int));
491. #define has_hilite(i) (blstats[0][(i)].thresholds)
492. /* TH_UPDOWN encompasses specific 'up' and 'down' also general 'changed' */
493. #define Is_Temp_Hilite(rule) ((rule) && (rule)->behavior == BL_TH_UPDOWN)
494. 
495. /* pointers to current hilite rule and list of this field's defined rules */
496. #define INIT_THRESH  , (struct hilite_s *) 0, (struct hilite_s *) 0
497. #else /* !STATUS_HILITES */
498. #define INIT_THRESH /*empty*/
499. #endif
500. 
501. #define INIT_BLSTAT(name, fmtstr, anytyp, wid, fld)                     \
502.     { name, fmtstr, 0L, FALSE, anytyp,  { (genericptr_t) 0 }, (char *) 0, \
503.       wid,  -1, fld INIT_THRESH }
504. #define INIT_BLSTATP(name, fmtstr, anytyp, wid, maxfld, fld)            \
505.     { name, fmtstr, 0L, FALSE, anytyp,  { (genericptr_t) 0 }, (char *) 0, \
506.       wid,  maxfld, fld INIT_THRESH }
507. 
508. /* If entries are added to this, botl.h will require updating too */
509. STATIC_VAR struct istat_s initblstats[MAXBLSTATS] = {
510.     INIT_BLSTAT("title", "%s", ANY_STR, MAXVALWIDTH, BL_TITLE),
511.     INIT_BLSTAT("strength", " St:%s", ANY_INT, 10, BL_STR),
512.     INIT_BLSTAT("dexterity", " Dx:%s", ANY_INT,  10, BL_DX),
513.     INIT_BLSTAT("constitution", " Co:%s", ANY_INT, 10, BL_CO),
514.     INIT_BLSTAT("intelligence", " In:%s", ANY_INT, 10, BL_IN),
515.     INIT_BLSTAT("wisdom", " Wi:%s", ANY_INT, 10, BL_WI),
516.     INIT_BLSTAT("charisma", " Ch:%s", ANY_INT, 10, BL_CH),
517.     INIT_BLSTAT("alignment", " %s", ANY_STR, 40, BL_ALIGN),
518.     INIT_BLSTAT("score", " S:%s", ANY_LONG, 20, BL_SCORE),
519.     INIT_BLSTAT("carrying-capacity", " %s", ANY_INT, 20, BL_CAP),
520.     INIT_BLSTAT("gold", " %s", ANY_LONG, 30, BL_GOLD),
521.     INIT_BLSTATP("power", " Pw:%s", ANY_INT, 10, BL_ENEMAX, BL_ENE),
522.     INIT_BLSTAT("power-max", "(%s)", ANY_INT, 10, BL_ENEMAX),
523.     INIT_BLSTAT("experience-level", " Xp:%s", ANY_INT, 10, BL_XP),
524.     INIT_BLSTAT("armor-class", " AC:%s", ANY_INT, 10, BL_AC),
525.     INIT_BLSTAT("HD", " HD:%s", ANY_INT, 10, BL_HD),
526.     INIT_BLSTAT("time", " T:%s", ANY_LONG, 20, BL_TIME),
527.     /* hunger used to be 'ANY_UINT'; see note below in bot_via_windowport() */
528.     INIT_BLSTAT("hunger", " %s", ANY_INT, 40, BL_HUNGER),
529.     INIT_BLSTATP("hitpoints", " HP:%s", ANY_INT, 10, BL_HPMAX, BL_HP),
530.     INIT_BLSTAT("hitpoints-max", "(%s)", ANY_INT, 10, BL_HPMAX),
531.     INIT_BLSTAT("dungeon-level", "%s", ANY_STR, MAXVALWIDTH, BL_LEVELDESC),
532.     INIT_BLSTAT("experience", "/%s", ANY_LONG, 20, BL_EXP),
533.     INIT_BLSTAT("condition", "%s", ANY_MASK32, 0, BL_CONDITION)
534. };
535. 
536. #undef INIT_BLSTATP
537. #undef INIT_BLSTAT
538. #undef INIT_THRESH
539. 
540. struct istat_s blstats[2][MAXBLSTATS];
541. static boolean blinit = FALSE, update_all = FALSE;
542. static boolean valset[MAXBLSTATS];
543. #ifdef STATUS_HILITES
544. static long bl_hilite_moves = 0L;
545. #endif
546. 
547. /* we don't put this next declaration in #ifdef STATUS_HILITES.
548.  * In the absence of STATUS_HILITES, each array
549.  * element will be 0 however, and quite meaningless,
550.  * but we need to pass the first array element as
551.  * the final argument of status_update, with or
552.  * without STATUS_HILITES.
553.  */
554. static unsigned long cond_hilites[BL_ATTCLR_MAX];
555. static int now_or_before_idx = 0; /* 0..1 for array[2][] first index */
556. 
557. void
558. bot_via_windowport()
559. {
560.     char buf[BUFSZ];
561.     const char *titl;
562.     register char *nb;
563.     int i, idx, cap;
564.     long money;
565. 
566.     if (!blinit)
567.         panic("bot before init.");
568. 
569.     /* toggle from previous iteration */
570.     idx = 1 - now_or_before_idx; /* 0 -> 1, 1 -> 0 */
571.     now_or_before_idx = idx;
572. 
573.     /* clear the "value set" indicators */
574.     (void) memset((genericptr_t) valset, 0, MAXBLSTATS * sizeof (boolean));
575. 
576.     /*
577.      * Note: min(x,9999) - we enforce the same maximum on hp, maxhp,
578.      * pw, maxpw, and gold as basic status formatting so that the two
579.      * modes of status display don't produce different information.
580.      */
581. 
582.     /*
583.      *  Player name and title.
584.      */
585.     Strcpy(nb = buf, plname);
586.     nb[0] = highc(nb[0]);
587.     titl = !Upolyd ? rank() : mons[u.umonnum].mname;
588.     i = (int) (strlen(buf) + sizeof " the " + strlen(titl) - sizeof "");
589.     /* if "Name the Rank/monster" is too long, we truncate the name
590.        but always keep at least 10 characters of it; when hitpintbar is
591.        enabled, anything beyond 30 (long monster name) will be truncated */
592.     if (i > 30) {
593.         i = 30 - (int) (sizeof " the " + strlen(titl) - sizeof "");
594.         nb[max(i, 10)] = '\0';
595.     }
596.     Strcpy(nb = eos(nb), " the ");
597.     Strcpy(nb = eos(nb), titl);
598.     if (Upolyd) { /* when poly'd, capitalize monster name */
599.         for (i = 0; nb[i]; i++)
600.             if (i == 0 || nb[i - 1] == ' ')
601.                 nb[i] = highc(nb[i]);
602.     }
603.     Sprintf(blstats[idx][BL_TITLE].val, "%-30s", buf);
604.     valset[BL_TITLE] = TRUE; /* indicate val already set */
605. 
606.     /* Strength */
607.     blstats[idx][BL_STR].a.a_int = ACURR(A_STR);
608.     Strcpy(blstats[idx][BL_STR].val, get_strength_str());
609.     valset[BL_STR] = TRUE; /* indicate val already set */
610. 
611.     /*  Dexterity, constitution, intelligence, wisdom, charisma. */
612.     blstats[idx][BL_DX].a.a_int = ACURR(A_DEX);
613.     blstats[idx][BL_CO].a.a_int = ACURR(A_CON);
614.     blstats[idx][BL_IN].a.a_int = ACURR(A_INT);
615.     blstats[idx][BL_WI].a.a_int = ACURR(A_WIS);
616.     blstats[idx][BL_CH].a.a_int = ACURR(A_CHA);
617. 
618.     /* Alignment */
619.     Strcpy(blstats[idx][BL_ALIGN].val, (u.ualign.type == A_CHAOTIC)
620.                                           ? "Chaotic"
621.                                           : (u.ualign.type == A_NEUTRAL)
622.                                                ? "Neutral"
623.                                                : "Lawful");
624. 
625.     /* Score */
626.     blstats[idx][BL_SCORE].a.a_long =
627. #ifdef SCORE_ON_BOTL
628.         flags.showscore ? botl_score() :
629. #endif
630.         0L;
631. 
632.     /*  Hit points  */
633.     i = Upolyd ? u.mh : u.uhp;
634.     if (i < 0)
635.         i = 0;
636.     blstats[idx][BL_HP].a.a_int = min(i, 9999);
637.     i = Upolyd ? u.mhmax : u.uhpmax;
638.     blstats[idx][BL_HPMAX].a.a_int = min(i, 9999);
639. 
640.     /*  Dungeon level. */
641.     (void) describe_level(blstats[idx][BL_LEVELDESC].val);
642.     valset[BL_LEVELDESC] = TRUE; /* indicate val already set */
643. 
644.     /* Gold */
645.     if ((money = money_cnt(invent)) < 0L)
646.         money = 0L; /* ought to issue impossible() and then discard gold */
647.     blstats[idx][BL_GOLD].a.a_long = min(money, 999999L);
648.     /*
649.      * The tty port needs to display the current symbol for gold
650.      * as a field header, so to accommodate that we pass gold with
651.      * that already included. If a window port needs to use the text
652.      * gold amount without the leading "$:" the port will have to
653.      * skip past ':' to the value pointer it was passed in status_update()
654.      * for the BL_GOLD case.
655.      *
656.      * Another quirk of BL_GOLD is that the field display may have
657.      * changed if a new symbol set was loaded, or we entered or left
658.      * the rogue level.
659.      *
660.      * The currency prefix is encoded as ten character \GXXXXNNNN
661.      * sequence.
662.      */
663.     Sprintf(blstats[idx][BL_GOLD].val, "%s:%ld",
664.             (iflags.in_dumplog || iflags.invis_goldsym) ? "$"
665.               : encglyph(objnum_to_glyph(GOLD_PIECE)),
666.             blstats[idx][BL_GOLD].a.a_long);
667.     valset[BL_GOLD] = TRUE; /* indicate val already set */
668. 
669.     /* Power (magical energy) */
670.     blstats[idx][BL_ENE].a.a_int = min(u.uen, 9999);
671.     blstats[idx][BL_ENEMAX].a.a_int = min(u.uenmax, 9999);
672. 
673.     /* Armor class */
674.     blstats[idx][BL_AC].a.a_int = u.uac;
675. 
676.     /* Monster level (if Upolyd) */
677.     blstats[idx][BL_HD].a.a_int = Upolyd ? (int) mons[u.umonnum].mlevel : 0;
678. 
679.     /* Experience */
680.     blstats[idx][BL_XP].a.a_int = u.ulevel;
681.     blstats[idx][BL_EXP].a.a_long = u.uexp;
682. 
683.     /* Time (moves) */
684.     blstats[idx][BL_TIME].a.a_long = moves;
685. 
686.     /* Hunger */
687.     /* note: u.uhs is unsigned, and 3.6.1's STATUS_HILITE defined
688.        BL_HUNGER to be ANY_UINT, but that was the only non-int/non-long
689.        numeric field so it's far simpler to treat it as plain int and
690.        not need ANY_UINT handling at all */
691.     blstats[idx][BL_HUNGER].a.a_int = (int) u.uhs;
692.     Strcpy(blstats[idx][BL_HUNGER].val,
693.            (u.uhs != NOT_HUNGRY) ? hu_stat[u.uhs] : "");
694.     valset[BL_HUNGER] = TRUE;
695. 
696.     /* Carrying capacity */
697.     cap = near_capacity();
698.     blstats[idx][BL_CAP].a.a_int = cap;
699.     Strcpy(blstats[idx][BL_CAP].val,
700.            (cap > UNENCUMBERED) ? enc_stat[cap] : "");
701.     valset[BL_CAP] = TRUE;
702. 
703.     /* Conditions */
704.     blstats[idx][BL_CONDITION].a.a_ulong = 0L;
705.     if (Stoned)
706.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_STONE;
707.     if (Slimed)
708.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_SLIME;
709.     if (Strangled)
710.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_STRNGL;
711.     if (Sick && (u.usick_type & SICK_VOMITABLE) != 0)
712.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_FOODPOIS;
713.     if (Sick && (u.usick_type & SICK_NONVOMITABLE) != 0)
714.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_TERMILL;
715.     /*
716.      * basic formatting puts hunger status and encumbrance here
717.      */
718.     if (Blind)
719.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_BLIND;
720.     if (Deaf)
721.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_DEAF;
722.     if (Stunned)
723.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_STUN;
724.     if (Confusion)
725.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_CONF;
726.     if (Hallucination)
727.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_HALLU;
728.     /* levitation and flying are mututally exclusive */
729.     if (Levitation)
730.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_LEV;
731.     if (Flying)
732.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_FLY;
733.     if (u.usteed)
734.         blstats[idx][BL_CONDITION].a.a_ulong |= BL_MASK_RIDE;
735.     evaluate_and_notify_windowport(valset, idx);
736. }
737. 
738. /* update just the status lines' 'time' field */
739. STATIC_OVL void
740. stat_update_time()
741. {
742.     int idx = now_or_before_idx; /* no 0/1 toggle */
743.     int fld = BL_TIME;
744. 
745.     /* Time (moves) */
746.     blstats[idx][fld].a.a_long = moves;
747.     valset[fld] = FALSE;
748. 
749.     eval_notify_windowport_field(fld, valset, idx);
750.     if ((windowprocs.wincap2 & WC2_FLUSH_STATUS) != 0L)
751.         status_update(BL_FLUSH, (genericptr_t) 0, 0, 0,
752.                       NO_COLOR, (unsigned long *) 0);
753.     return;
754. }
755. 
756. STATIC_OVL boolean
757. eval_notify_windowport_field(fld, valsetlist, idx)
758. int fld, idx;
759. boolean *valsetlist;
760. {
761.     static int oldrndencode = 0;
762.     static nhsym oldgoldsym = 0;
763.     int pc, chg, color = NO_COLOR;
764.     unsigned anytype;
765.     boolean updated = FALSE, reset;
766.     struct istat_s *curr = NULL, *prev = NULL;
767.     enum statusfields idxmax;
768. 
769.     /*
770.      *  Now pass the changed values to window port.
771.      */
772.     anytype = blstats[idx][fld].anytype;
773.     curr = &blstats[idx][fld];
774.     prev = &blstats[1 - idx][fld];
775.     color = NO_COLOR;
776. 
777.     chg = update_all ? 0 : compare_blstats(prev, curr);
778. 
779.     /* Temporary? hack: moveloop()'s prolog for a new game sets
780.      * context.rndencode after the status window has been init'd,
781.      * so $:0 has already been encoded and cached by the window
782.      * port.  Without this hack, gold's \G sequence won't be
783.      * recognized and ends up being displayed as-is for 'update_all'.
784.      *
785.      * Also, even if context.rndencode hasn't changed and the
786.      * gold amount itself hasn't changed, the glyph portion of the
787.      * encoding may have changed if a new symset was put into effect.
788.      *
789.      *  \GXXXXNNNN:25
790.      *  XXXX = the context.rndencode portion
791.      *  NNNN = the glyph portion
792.      *  25   = the gold amount
793.      *
794.      * Setting 'chg = 2' is enough to render the field properly, but
795.      * not to honor an initial highlight, so force 'update_all = TRUE'.
796.      */
797.     if (fld == BL_GOLD
798.         && (context.rndencode != oldrndencode
799.             || showsyms[COIN_CLASS + SYM_OFF_O] != oldgoldsym)) {
800.         update_all = TRUE; /* chg = 2; */
801.         oldrndencode = context.rndencode;
802.         oldgoldsym = showsyms[COIN_CLASS + SYM_OFF_O];
803.     }
804. 
805.     reset = FALSE;
806. #ifdef STATUS_HILITES
807.     if (!update_all && !chg && curr->time) {
808.         reset = hilite_reset_needed(prev, bl_hilite_moves);
809.         if (reset)
810.             curr->time = prev->time = 0L;
811.     }
812. #endif
813. 
814.         /*
815.          * TODO?
816.          *  It's possible for HPmax (or ENEmax) to change while current
817.          *  HP (or energy) stays the same.  [Perhaps current and maximum
818.          *  both go up, then before the next status update takes place
819.          *  current goes down again.]  If that happens with HPmax, we
820.          *  ought to force the windowport to treat current HP as changed
821.          *  if hitpointbar is On, in order for that to be re-rendered.
822.          */
823.     if (update_all || chg || reset) {
824.         idxmax = curr->idxmax;
825.         pc = (idxmax >= 0) ? percentage(curr, &blstats[idx][idxmax]) : 0;
826. 
827.         if (!valsetlist[fld])
828.             (void) anything_to_s(curr->val, &curr->a, anytype);
829. 
830.         if (anytype != ANY_MASK32) {
831. #ifdef STATUS_HILITES
832.             if (chg || *curr->val) {
833.                 curr->hilite_rule = get_hilite(idx, fld,
834.                                                (genericptr_t) &curr->a,
835.                                                chg, pc, &color);
836.                 prev->hilite_rule = curr->hilite_rule;
837.                 if (chg == 2) {
838.                     color = NO_COLOR;
839.                     chg = 0;
840.                 }
841.             }
842. #endif /* STATUS_HILITES */
843.             status_update(fld, (genericptr_t) curr->val,
844.                           chg, pc, color, (unsigned long *) 0);
845.         } else {
846.             /* Color for conditions is done through cond_hilites[] */
847.             status_update(fld, (genericptr_t) &curr->a.a_ulong,
848.                           chg, pc, color, cond_hilites);
849.         }
850.         curr->chg = prev->chg = TRUE;
851.         updated = TRUE;
852.     }
853.     return updated;
854. }
855. 
856. STATIC_OVL void
857. evaluate_and_notify_windowport(valsetlist, idx)
858. int idx;
859. boolean *valsetlist;
860. {
861.     int i, updated = 0, notpresent = 0;
862. 
863.     /*
864.      *  Now pass the changed values to window port.
865.      */
866.     for (i = 0; i < MAXBLSTATS; i++) {
867.         if (((i == BL_SCORE) && !flags.showscore)
868.             || ((i == BL_EXP) && !flags.showexp)
869.             || ((i == BL_TIME) && !flags.time)
870.             || ((i == BL_HD) && !Upolyd)
871.             || ((i == BL_XP || i == BL_EXP) && Upolyd)) {
872.             notpresent++;
873.             continue;
874.         }
875.         if (eval_notify_windowport_field(i, valsetlist, idx))
876.             updated++;
877.     }
878.     /*
879.      * Notes:
880.      *  1. It is possible to get here, with nothing having been pushed
881.      *     to the window port, when none of the info has changed.
882.      *
883.      *  2. Some window ports are also known to optimize by only drawing
884.      *     fields that have changed since the previous update.
885.      *
886.      * In both of those situations, we need to force updates to
887.      * all of the fields when context.botlx is set. The tty port in
888.      * particular has a problem if that isn't done, since the core sets
889.      * context.botlx when a menu or text display obliterates the status
890.      * line.
891.      *
892.      * For those situations, to trigger the full update of every field
893.      * whether changed or not, call status_update() with BL_RESET.
894.      *
895.      * For regular processing and to notify the window port that a
896.      * bot() round has finished and it's time to trigger a flush of
897.      * all buffered changes received thus far but not reflected in
898.      * the display, call status_update() with BL_FLUSH.
899.      *
900.      */
901.     if (context.botlx && (windowprocs.wincap2 & WC2_RESET_STATUS) != 0L)
902.         status_update(BL_RESET, (genericptr_t) 0, 0, 0,
903.                       NO_COLOR, (unsigned long *) 0);
904.     else if ((updated || context.botlx)
905.              && (windowprocs.wincap2 & WC2_FLUSH_STATUS) != 0L)
906.         status_update(BL_FLUSH, (genericptr_t) 0, 0, 0,
907.                       NO_COLOR, (unsigned long *) 0);
908. 
909.     context.botl = context.botlx = iflags.time_botl = FALSE;
910.     update_all = FALSE;
911. }
912. 
913. void
914. status_initialize(reassessment)
915. boolean reassessment; /* TRUE: just recheck fields w/o other initialization */
916. {
917.     enum statusfields fld;
918.     boolean fldenabl;
919.     int i;
920.     const char *fieldfmt, *fieldname;
921. 
922.     if (!reassessment) {
923.         if (blinit)
924.             impossible("2nd status_initialize with full init.");
925.         init_blstats();
926.         (*windowprocs.win_status_init)();
927.         blinit = TRUE;
928.     } else if (!blinit) {
929.         panic("status 'reassess' before init");
930.     }
931.     for (i = 0; i < MAXBLSTATS; ++i) {
932.         fld = initblstats[i].fld;
933.         fldenabl = (fld == BL_SCORE) ? flags.showscore
934.                    : (fld == BL_TIME) ? flags.time
935.                      : (fld == BL_EXP) ? (boolean) (flags.showexp && !Upolyd)
936.                        : (fld == BL_XP) ? (boolean) !Upolyd
937.                          : (fld == BL_HD) ? (boolean) Upolyd
938.                            : TRUE;
939. 
940.         fieldname = initblstats[i].fldname;
941.         fieldfmt = (fld == BL_TITLE && iflags.wc2_hitpointbar) ? "%-30.30s"
942.                    : initblstats[i].fldfmt;
943.         status_enablefield(fld, fieldname, fieldfmt, fldenabl);
944.     }
945.     update_all = TRUE;
946. }
947. 
948. void
949. status_finish()
950. {
951.     int i;
952. 
953.     /* call the window port cleanup routine first */
954.     if (windowprocs.win_status_finish)
955.         (*windowprocs.win_status_finish)();
956. 
957.     /* free memory that we alloc'd now */
958.     for (i = 0; i < MAXBLSTATS; ++i) {
959.         if (blstats[0][i].val)
960.             free((genericptr_t) blstats[0][i].val), blstats[0][i].val = 0;
961.         if (blstats[1][i].val)
962.             free((genericptr_t) blstats[1][i].val), blstats[1][i].val = 0;
963. #ifdef STATUS_HILITES
964.         /* pointer to an entry in thresholds list; Null it out since
965.            that list is about to go away */
966.         blstats[0][i].hilite_rule = blstats[1][i].hilite_rule = 0;
967.         if (blstats[0][i].thresholds) {
968.             struct hilite_s *temp, *next;
969. 
970.             for (temp = blstats[0][i].thresholds; temp; temp = next) {
971.                 next = temp->next;
972.                 free((genericptr_t) temp);
973.             }
974.             blstats[0][i].thresholds = blstats[1][i].thresholds = 0;
975.         }
976. #endif /* STATUS_HILITES */
977.     }
978. }
979. 
980. STATIC_OVL void
981. init_blstats()
982. {
983.     static boolean initalready = FALSE;
984.     int i, j;
985. 
986.     if (initalready) {
987.         impossible("init_blstats called more than once.");
988.         return;
989.     }
990.     for (i = 0; i <= 1; ++i) {
991.         for (j = 0; j < MAXBLSTATS; ++j) {
992. #ifdef STATUS_HILITES
993.             struct hilite_s *keep_hilite_chain = blstats[i][j].thresholds;
994. #endif
995. 
996.             blstats[i][j] = initblstats[j];
997.             blstats[i][j].a = zeroany;
998.             if (blstats[i][j].valwidth) {
999.                 blstats[i][j].val = (char *) alloc(blstats[i][j].valwidth);
1000.                 blstats[i][j].val[0] = '\0';
1001.             } else
1002.                 blstats[i][j].val = (char *) 0;
1003. #ifdef STATUS_HILITES
1004.             blstats[i][j].thresholds = keep_hilite_chain;
1005. #endif
1006.         }
1007.     }
1008.     initalready = TRUE;
1009. }
1010. 
1011. /*
1012.  * This compares the previous stat with the current stat,
1013.  * and returns one of the following results based on that:
1014.  *
1015.  *   if prev_value < new_value (stat went up, increased)
1016.  *      return 1
1017.  *
1018.  *   if prev_value > new_value (stat went down, decreased)
1019.  *      return  -1
1020.  *
1021.  *   if prev_value == new_value (stat stayed the same)
1022.  *      return 0
1023.  *
1024.  *   Special cases:
1025.  *     - for bitmasks, 0 = stayed the same, 1 = changed
1026.  *     - for strings,  0 = stayed the same, 1 = changed
1027.  *
1028.  */
1029. STATIC_OVL int
1030. compare_blstats(bl1, bl2)
1031. struct istat_s *bl1, *bl2;
1032. {
1033.     int anytype, result = 0;
1034. 
1035.     if (!bl1 || !bl2) {
1036.         panic("compare_blstat: bad istat pointer %s, %s",
1037.               fmt_ptr((genericptr_t) bl1), fmt_ptr((genericptr_t) bl2));
1038.     }
1039. 
1040.     anytype = bl1->anytype;
1041.     if ((!bl1->a.a_void || !bl2->a.a_void)
1042.         && (anytype == ANY_IPTR || anytype == ANY_UPTR || anytype == ANY_LPTR
1043.             || anytype == ANY_ULPTR)) {
1044.         panic("compare_blstat: invalid pointer %s, %s",
1045.               fmt_ptr((genericptr_t) bl1->a.a_void),
1046.               fmt_ptr((genericptr_t) bl2->a.a_void));
1047.     }
1048. 
1049.     switch (anytype) {
1050.     case ANY_INT:
1051.         result = (bl1->a.a_int < bl2->a.a_int)
1052.                      ? 1
1053.                      : (bl1->a.a_int > bl2->a.a_int) ? -1 : 0;
1054.         break;
1055.     case ANY_IPTR:
1056.         result = (*bl1->a.a_iptr < *bl2->a.a_iptr)
1057.                      ? 1
1058.                      : (*bl1->a.a_iptr > *bl2->a.a_iptr) ? -1 : 0;
1059.         break;
1060.     case ANY_LONG:
1061.         result = (bl1->a.a_long < bl2->a.a_long)
1062.                      ? 1
1063.                      : (bl1->a.a_long > bl2->a.a_long) ? -1 : 0;
1064.         break;
1065.     case ANY_LPTR:
1066.         result = (*bl1->a.a_lptr < *bl2->a.a_lptr)
1067.                      ? 1
1068.                      : (*bl1->a.a_lptr > *bl2->a.a_lptr) ? -1 : 0;
1069.         break;
1070.     case ANY_UINT:
1071.         result = (bl1->a.a_uint < bl2->a.a_uint)
1072.                      ? 1
1073.                      : (bl1->a.a_uint > bl2->a.a_uint) ? -1 : 0;
1074.         break;
1075.     case ANY_UPTR:
1076.         result = (*bl1->a.a_uptr < *bl2->a.a_uptr)
1077.                      ? 1
1078.                      : (*bl1->a.a_uptr > *bl2->a.a_uptr) ? -1 : 0;
1079.         break;
1080.     case ANY_ULONG:
1081.         result = (bl1->a.a_ulong < bl2->a.a_ulong)
1082.                      ? 1
1083.                      : (bl1->a.a_ulong > bl2->a.a_ulong) ? -1 : 0;
1084.         break;
1085.     case ANY_ULPTR:
1086.         result = (*bl1->a.a_ulptr < *bl2->a.a_ulptr)
1087.                      ? 1
1088.                      : (*bl1->a.a_ulptr > *bl2->a.a_ulptr) ? -1 : 0;
1089.         break;
1090.     case ANY_STR:
1091.         result = sgn(strcmp(bl1->val, bl2->val));
1092.         break;
1093.     case ANY_MASK32:
1094.         result = (bl1->a.a_ulong != bl2->a.a_ulong);
1095.         break;
1096.     default:
1097.         result = 1;
1098.     }
1099.     return result;
1100. }
1101. 
1102. STATIC_OVL char *
1103. anything_to_s(buf, a, anytype)
1104. char *buf;
1105. anything *a;
1106. int anytype;
1107. {
1108.     if (!buf)
1109.         return (char *) 0;
1110. 
1111.     switch (anytype) {
1112.     case ANY_ULONG:
1113.         Sprintf(buf, "%lu", a->a_ulong);
1114.         break;
1115.     case ANY_MASK32:
1116.         Sprintf(buf, "%lx", a->a_ulong);
1117.         break;
1118.     case ANY_LONG:
1119.         Sprintf(buf, "%ld", a->a_long);
1120.         break;
1121.     case ANY_INT:
1122.         Sprintf(buf, "%d", a->a_int);
1123.         break;
1124.     case ANY_UINT:
1125.         Sprintf(buf, "%u", a->a_uint);
1126.         break;
1127.     case ANY_IPTR:
1128.         Sprintf(buf, "%d", *a->a_iptr);
1129.         break;
1130.     case ANY_LPTR:
1131.         Sprintf(buf, "%ld", *a->a_lptr);
1132.         break;
1133.     case ANY_ULPTR:
1134.         Sprintf(buf, "%lu", *a->a_ulptr);
1135.         break;
1136.     case ANY_UPTR:
1137.         Sprintf(buf, "%u", *a->a_uptr);
1138.         break;
1139.     case ANY_STR: /* do nothing */
1140.         ;
1141.         break;
1142.     default:
1143.         buf[0] = '\0';
1144.     }
1145.     return buf;
1146. }
1147. 
1148. #ifdef STATUS_HILITES
1149. STATIC_OVL void
1150. s_to_anything(a, buf, anytype)
1151. anything *a;
1152. char *buf;
1153. int anytype;
1154. {
1155.     if (!buf || !a)
1156.         return;
1157. 
1158.     switch (anytype) {
1159.     case ANY_LONG:
1160.         a->a_long = atol(buf);
1161.         break;
1162.     case ANY_INT:
1163.         a->a_int = atoi(buf);
1164.         break;
1165.     case ANY_UINT:
1166.         a->a_uint = (unsigned) atoi(buf);
1167.         break;
1168.     case ANY_ULONG:
1169.         a->a_ulong = (unsigned long) atol(buf);
1170.         break;
1171.     case ANY_IPTR:
1172.         if (a->a_iptr)
1173.             *a->a_iptr = atoi(buf);
1174.         break;
1175.     case ANY_UPTR:
1176.         if (a->a_uptr)
1177.             *a->a_uptr = (unsigned) atoi(buf);
1178.         break;
1179.     case ANY_LPTR:
1180.         if (a->a_lptr)
1181.             *a->a_lptr = atol(buf);
1182.         break;
1183.     case ANY_ULPTR:
1184.         if (a->a_ulptr)
1185.             *a->a_ulptr = (unsigned long) atol(buf);
1186.         break;
1187.     case ANY_MASK32:
1188.         a->a_ulong = (unsigned long) atol(buf);
1189.         break;
1190.     default:
1191.         a->a_void = 0;
1192.         break;
1193.     }
1194.     return;
1195. }
1196. #endif /* STATUS_HILITES */
1197. 
1198. STATIC_OVL int
1199. percentage(bl, maxbl)
1200. struct istat_s *bl, *maxbl;
1201. {
1202.     int result = 0;
1203.     int anytype;
1204.     int ival;
1205.     long lval;
1206.     unsigned uval;
1207.     unsigned long ulval;
1208. 
1209.     if (!bl || !maxbl) {
1210.         impossible("percentage: bad istat pointer %s, %s",
1211.                    fmt_ptr((genericptr_t) bl), fmt_ptr((genericptr_t) maxbl));
1212.         return 0;
1213.     }
1214. 
1215.     ival = 0, lval = 0L, uval = 0U, ulval = 0UL;
1216.     anytype = bl->anytype;
1217.     if (maxbl->a.a_void) {
1218.         switch (anytype) {
1219.         case ANY_INT:
1220.             ival = bl->a.a_int;
1221.             result = ((100 * ival) / maxbl->a.a_int);
1222.             break;
1223.         case ANY_LONG:
1224.             lval  = bl->a.a_long;
1225.             result = (int) ((100L * lval) / maxbl->a.a_long);
1226.             break;
1227.         case ANY_UINT:
1228.             uval = bl->a.a_uint;
1229.             result = (int) ((100U * uval) / maxbl->a.a_uint);
1230.             break;
1231.         case ANY_ULONG:
1232.             ulval = bl->a.a_ulong;
1233.             result = (int) ((100UL * ulval) / maxbl->a.a_ulong);
1234.             break;
1235.         case ANY_IPTR:
1236.             ival = *bl->a.a_iptr;
1237.             result = ((100 * ival) / (*maxbl->a.a_iptr));
1238.             break;
1239.         case ANY_LPTR:
1240.             lval = *bl->a.a_lptr;
1241.             result = (int) ((100L * lval) / (*maxbl->a.a_lptr));
1242.             break;
1243.         case ANY_UPTR:
1244.             uval = *bl->a.a_uptr;
1245.             result = (int) ((100U * uval) / (*maxbl->a.a_uptr));
1246.             break;
1247.         case ANY_ULPTR:
1248.             ulval = *bl->a.a_ulptr;
1249.             result = (int) ((100UL * ulval) / (*maxbl->a.a_ulptr));
1250.             break;
1251.         }
1252.     }
1253.     /* don't let truncation from integer division produce a zero result
1254.        from a non-zero input; note: if we ever change to something like
1255.        ((((1000 * val) / max) + 5) / 10) for a rounded result, we'll
1256.        also need to check for and convert false 100 to 99 */
1257.     if (result == 0 && (ival != 0 || lval != 0L || uval != 0U || ulval != 0UL))
1258.         result = 1;
1259. 
1260.     return result;
1261. }
1262. 
1263. /* callback so that interface can get capacity index rather than trying
1264.    to reconstruct that from the encumbrance string or asking the general
1265.    core what the value is */
1266. int
1267. stat_cap_indx()
1268. {
1269.     int cap;
1270. 
1271. #ifdef STATUS_HILITES
1272.     cap = blstats[now_or_before_idx][BL_CAP].a.a_int;
1273. #else
1274.     cap = near_capacity();
1275. #endif
1276.     return cap;
1277. }
1278. 
1279. /* callback so that interface can get hunger index rather than trying to
1280.    reconstruct that from the hunger string or dipping into core internals */
1281. int
1282. stat_hunger_indx()
1283. {
1284.     int uhs;
1285. 
1286. #ifdef STATUS_HILITES
1287.     uhs = blstats[now_or_before_idx][BL_HUNGER].a.a_int;
1288. #else
1289.     uhs = (int) u.uhs;
1290. #endif
1291.     return uhs;
1292. }
1293. 
1294. /* used by X11 for "tty status" even when STATUS_HILITES is disabled */
1295. const char *
1296. bl_idx_to_fldname(idx)
1297. int idx;
1298. {
1299.     if (idx >= 0 && idx < MAXBLSTATS)
1300.         return initblstats[idx].fldname;
1301.     return (const char *) 0;
1302. }
1303. 
1304. #ifdef STATUS_HILITES
1305. 
1306. /****************************************************************************/
1307. /* Core status hiliting support */
1308. /****************************************************************************/
1309. 
1310. struct hilite_s status_hilites[MAXBLSTATS];
1311. 
1312. static struct fieldid_t {
1313.     const char *fieldname;
1314.     enum statusfields fldid;
1315. } fieldids_alias[] = {
1316.     { "characteristics",   BL_CHARACTERISTICS },
1317.     { "encumbrance",       BL_CAP },
1318.     { "experience-points", BL_EXP },
1319.     { "dx",       BL_DX },
1320.     { "co",       BL_CO },
1321.     { "con",      BL_CO },
1322.     { "points",   BL_SCORE },
1323.     { "cap",      BL_CAP },
1324.     { "pw",       BL_ENE },
1325.     { "pw-max",   BL_ENEMAX },
1326.     { "xl",       BL_XP },
1327.     { "xplvl",    BL_XP },
1328.     { "ac",       BL_AC },
1329.     { "hit-dice", BL_HD },
1330.     { "turns",    BL_TIME },
1331.     { "hp",       BL_HP },
1332.     { "hp-max",   BL_HPMAX },
1333.     { "dgn",      BL_LEVELDESC },
1334.     { "xp",       BL_EXP },
1335.     { "exp",      BL_EXP },
1336.     { "flags",    BL_CONDITION },
1337.     {0,           BL_FLUSH }
1338. };
1339. 
1340. /* format arguments */
1341. static const char threshold_value[] = "hilite_status threshold ",
1342.                   is_out_of_range[] = " is out of range";
1343. 
1344. 
1345. /* field name to bottom line index */
1346. STATIC_OVL enum statusfields
1347. fldname_to_bl_indx(name)
1348. const char *name;
1349. {
1350.     int i, nmatches = 0, fld = 0;
1351. 
1352.     if (name && *name) {
1353.         /* check matches to canonical names */
1354.         for (i = 0; i < SIZE(initblstats); i++)
1355.             if (fuzzymatch(initblstats[i].fldname, name, " -_", TRUE)) {
1356.                 fld = initblstats[i].fld;
1357.                 nmatches++;
1358.             }
1359. 
1360.         if (!nmatches) {
1361.             /* check aliases */
1362.             for (i = 0; fieldids_alias[i].fieldname; i++)
1363.                 if (fuzzymatch(fieldids_alias[i].fieldname, name,
1364.                                " -_", TRUE)) {
1365.                     fld = fieldids_alias[i].fldid;
1366.                     nmatches++;
1367.                 }
1368.         }
1369. 
1370.         if (!nmatches) {
1371.             /* check partial matches to canonical names */
1372.             int len = (int) strlen(name);
1373. 
1374.             for (i = 0; i < SIZE(initblstats); i++)
1375.                 if (!strncmpi(name, initblstats[i].fldname, len)) {
1376.                     fld = initblstats[i].fld;
1377.                     nmatches++;
1378.                 }
1379.         }
1380. 
1381.     }
1382.     return (nmatches == 1) ? fld : BL_FLUSH;
1383. }
1384. 
1385. STATIC_OVL boolean
1386. hilite_reset_needed(bl_p, augmented_time)
1387. struct istat_s *bl_p;
1388. long augmented_time; /* no longer augmented; it once encoded fractional
1389.                       * amounts for multiple moves within same turn     */
1390. {
1391.     /*
1392.      * This 'multi' handling may need some tuning...
1393.      */
1394.     if (multi)
1395.         return FALSE;
1396. 
1397.     if (!Is_Temp_Hilite(bl_p->hilite_rule))
1398.         return FALSE;
1399. 
1400.     if (bl_p->time == 0 || bl_p->time >= augmented_time)
1401.         return FALSE;
1402. 
1403.     return TRUE;
1404. }
1405. 
1406. /* called from moveloop(); sets context.botl if temp hilites have timed out */
1407. void
1408. status_eval_next_unhilite()
1409. {
1410.     int i;
1411.     struct istat_s *curr;
1412.     long next_unhilite, this_unhilite;
1413. 
1414.     bl_hilite_moves = moves; /* simpllfied; used to try to encode fractional
1415.                               * amounts for multiple moves within same turn */
1416.     /* figure out whether an unhilight needs to be performed now */
1417.     next_unhilite = 0L;
1418.     for (i = 0; i < MAXBLSTATS; ++i) {
1419.         curr = &blstats[0][i]; /* blstats[0][*].time == blstats[1][*].time */
1420. 
1421.         if (curr->chg) {
1422.             struct istat_s *prev = &blstats[1][i];
1423. 
1424.             if (Is_Temp_Hilite(curr->hilite_rule))
1425.                 curr->time = prev->time = (bl_hilite_moves
1426.                                            + iflags.hilite_delta);
1427.             else
1428.                 curr->time = prev->time = 0L;
1429. 
1430.             curr->chg = prev->chg = FALSE;
1431.             context.botl = TRUE;
1432.         }
1433.         if (context.botl)
1434.             continue; /* just process other blstats[][].time and .chg */
1435. 
1436.         this_unhilite = curr->time;
1437.         if (this_unhilite > 0L
1438.             && (next_unhilite == 0L || this_unhilite < next_unhilite)
1439.             && hilite_reset_needed(curr, this_unhilite + 1L)) {
1440.             next_unhilite = this_unhilite;
1441.             if (next_unhilite < bl_hilite_moves)
1442.                 context.botl = TRUE;
1443.         }
1444.     }
1445. }
1446. 
1447. /* called by options handling when 'statushilites' value is changed */
1448. void
1449. reset_status_hilites()
1450. {
1451.     if (iflags.hilite_delta) {
1452.         int i;
1453. 
1454.         for (i = 0; i < MAXBLSTATS; ++i)
1455.             blstats[0][i].time = blstats[1][i].time = 0L;
1456.         update_all = TRUE;
1457.     }
1458.     context.botlx = TRUE;
1459. }
1460. 
1461. /* test whether the text from a title rule matches the string for
1462.    title-while-polymorphed in the 'textmatch' menu */
1463. STATIC_OVL boolean
1464. noneoftheabove(hl_text)
1465. const char *hl_text;
1466. {
1467.     if (fuzzymatch(hl_text, "none of the above", "\" -_", TRUE)
1468.         || fuzzymatch(hl_text, "(polymorphed)", "\"()", TRUE)
1469.         || fuzzymatch(hl_text, "none of the above (polymorphed)",
1470.                       "\" -_()", TRUE))
1471.         return TRUE;
1472.     return FALSE;
1473. }
1474. 
1475. /*
1476.  * get_hilite
1477.  *
1478.  * Returns, based on the value and the direction it is moving,
1479.  * the highlight rule that applies to the specified field.
1480.  *
1481.  * Provide get_hilite() with the following to work with:
1482.  *     actual value vp
1483.  *          useful for BL_TH_VAL_ABSOLUTE
1484.  *     indicator of down, up, or the same (-1, 1, 0) chg
1485.  *          useful for BL_TH_UPDOWN or change detection
1486.  *     percentage (current value percentage of max value) pc
1487.  *          useful for BL_TH_VAL_PERCENTAGE
1488.  *
1489.  * Get back:
1490.  *     pointer to rule that applies; Null if no rule does.
1491.  */
1492. STATIC_OVL struct hilite_s *
1493. get_hilite(idx, fldidx, vp, chg, pc, colorptr)
1494. int idx, fldidx, chg, pc;
1495. genericptr_t vp;
1496. int *colorptr;
1497. {
1498.     struct hilite_s *hl, *rule = 0;
1499.     anything *value = (anything *) vp;
1500.     char *txtstr;
1501. 
1502.     if (fldidx < 0 || fldidx >= MAXBLSTATS)
1503.         return (struct hilite_s *) 0;
1504. 
1505.     if (has_hilite(fldidx)) {
1506.         int dt;
1507.         /* there are hilites set here */
1508.         int max_pc = -1, min_pc = 101;
1509.         /* LARGEST_INT isn't INT_MAX; it fits within 16 bits, but that
1510.            value is big enough to handle all 'int' status fields */
1511.         int max_ival = -LARGEST_INT, min_ival = LARGEST_INT;
1512.         /* LONG_MAX comes from <limits.h> which might not be available for
1513.            ancient configurations; we don't need LONG_MIN */
1514.         long max_lval = -LONG_MAX, min_lval = LONG_MAX;
1515.         boolean exactmatch = FALSE, updown = FALSE, changed = FALSE,
1516.                 perc_or_abs = FALSE;
1517. 
1518.         /* min_/max_ are used to track best fit */
1519.         for (hl = blstats[0][fldidx].thresholds; hl; hl = hl->next) {
1520.             dt = initblstats[fldidx].anytype; /* only needed for 'absolute' */
1521.             /* if we've already matched a temporary highlight, it takes
1522.                precedence over all persistent ones; we still process
1523.                updown rules to get the last one which qualifies */
1524.             if ((updown || changed) && hl->behavior != BL_TH_UPDOWN)
1525.                 continue;
1526.             /* among persistent highlights, if a 'percentage' or 'absolute'
1527.                rule has been matched, it takes precedence over 'always' */
1528.             if (perc_or_abs && hl->behavior == BL_TH_ALWAYS_HILITE)
1529.                 continue;
1530. 
1531.             switch (hl->behavior) {
1532.             case BL_TH_VAL_PERCENTAGE: /* percent values are always ANY_INT */
1533.                 if (hl->rel == EQ_VALUE && pc == hl->value.a_int) {
1534.                     rule = hl;
1535.                     min_pc = max_pc = hl->value.a_int;
1536.                     exactmatch = perc_or_abs = TRUE;
1537.                 } else if (exactmatch) {
1538.                     ; /* already found best fit, skip lt,ge,&c */
1539.                 } else if (hl->rel == LT_VALUE
1540.                            && (pc < hl->value.a_int)
1541.                            && (hl->value.a_int <= min_pc)) {
1542.                     rule = hl;
1543.                     min_pc = hl->value.a_int;
1544.                     perc_or_abs = TRUE;
1545.                 } else if (hl->rel == LE_VALUE
1546.                            && (pc <= hl->value.a_int)
1547.                            && (hl->value.a_int <= min_pc)) {
1548.                     rule = hl;
1549.                     min_pc = hl->value.a_int;
1550.                     perc_or_abs = TRUE;
1551.                 } else if (hl->rel == GT_VALUE
1552.                            && (pc > hl->value.a_int)
1553.                            && (hl->value.a_int >= max_pc)) {
1554.                     rule = hl;
1555.                     max_pc = hl->value.a_int;
1556.                     perc_or_abs = TRUE;
1557.                 } else if (hl->rel == GE_VALUE
1558.                            && (pc >= hl->value.a_int)
1559.                            && (hl->value.a_int >= max_pc)) {
1560.                     rule = hl;
1561.                     max_pc = hl->value.a_int;
1562.                     perc_or_abs = TRUE;
1563.                 }
1564.                 break;
1565.             case BL_TH_UPDOWN: /* uses 'chg' (set by caller), not 'dt' */
1566.                 /* specific 'up' or 'down' takes precedence over general
1567.                    'changed' regardless of their order in the rule set */
1568.                 if (chg < 0 && hl->rel == LT_VALUE) {
1569.                     rule = hl;
1570.                     updown = TRUE;
1571.                 } else if (chg > 0 && hl->rel == GT_VALUE) {
1572.                     rule = hl;
1573.                     updown = TRUE;
1574.                 } else if (chg != 0 && hl->rel == EQ_VALUE && !updown) {
1575.                     rule = hl;
1576.                     changed = TRUE;
1577.                 }
1578.                 break;
1579.             case BL_TH_VAL_ABSOLUTE: /* either ANY_INT or ANY_LONG */
1580.                 /*
1581.                  * The int and long variations here are identical aside from
1582.                  * union field and min_/max_ variable names.  If you change
1583.                  * one, be sure to make a corresponding change in the other.
1584.                  */
1585.                 if (dt == ANY_INT) {
1586.                     if (hl->rel == EQ_VALUE
1587.                         && hl->value.a_int == value->a_int) {
1588.                         rule = hl;
1589.                         min_ival = max_ival = hl->value.a_int;
1590.                         exactmatch = perc_or_abs = TRUE;
1591.                     } else if (exactmatch) {
1592.                         ; /* already found best fit, skip lt,ge,&c */
1593.                     } else if (hl->rel == LT_VALUE
1594.                                && (value->a_int < hl->value.a_int)
1595.                                && (hl->value.a_int <= min_ival)) {
1596.                         rule = hl;
1597.                         min_ival = hl->value.a_int;
1598.                         perc_or_abs = TRUE;
1599.                     } else if (hl->rel == LE_VALUE
1600.                                && (value->a_int <= hl->value.a_int)
1601.                                && (hl->value.a_int <= min_ival)) {
1602.                         rule = hl;
1603.                         min_ival = hl->value.a_int;
1604.                         perc_or_abs = TRUE;
1605.                     } else if (hl->rel == GT_VALUE
1606.                                && (value->a_int > hl->value.a_int)
1607.                                && (hl->value.a_int >= max_ival)) {
1608.                         rule = hl;
1609.                         max_ival = hl->value.a_int;
1610.                         perc_or_abs = TRUE;
1611.                     } else if (hl->rel == GE_VALUE
1612.                                && (value->a_int >= hl->value.a_int)
1613.                                && (hl->value.a_int >= max_ival)) {
1614.                         rule = hl;
1615.                         max_ival = hl->value.a_int;
1616.                         perc_or_abs = TRUE;
1617.                     }
1618.                 } else { /* ANY_LONG */
1619.                     if (hl->rel == EQ_VALUE
1620.                         && hl->value.a_long == value->a_long) {
1621.                         rule = hl;
1622.                         min_lval = max_lval = hl->value.a_long;
1623.                         exactmatch = perc_or_abs = TRUE;
1624.                     } else if (exactmatch) {
1625.                         ; /* already found best fit, skip lt,ge,&c */
1626.                     } else if (hl->rel == LT_VALUE
1627.                                && (value->a_long < hl->value.a_long)
1628.                                && (hl->value.a_long <= min_lval)) {
1629.                         rule = hl;
1630.                         min_lval = hl->value.a_long;
1631.                         perc_or_abs = TRUE;
1632.                     } else if (hl->rel == LE_VALUE
1633.                                && (value->a_long <= hl->value.a_long)
1634.                                && (hl->value.a_long <= min_lval)) {
1635.                         rule = hl;
1636.                         min_lval = hl->value.a_long;
1637.                         perc_or_abs = TRUE;
1638.                     } else if (hl->rel == GT_VALUE
1639.                                && (value->a_long > hl->value.a_long)
1640.                                && (hl->value.a_long >= max_lval)) {
1641.                         rule = hl;
1642.                         max_lval = hl->value.a_long;
1643.                         perc_or_abs = TRUE;
1644.                     } else if (hl->rel == GE_VALUE
1645.                                && (value->a_long >= hl->value.a_long)
1646.                                && (hl->value.a_long >= max_lval)) {
1647.                         rule = hl;
1648.                         max_lval = hl->value.a_long;
1649.                         perc_or_abs = TRUE;
1650.                     }
1651.                 }
1652.                 break;
1653.             case BL_TH_TEXTMATCH: /* ANY_STR */
1654.                 txtstr = blstats[idx][fldidx].val;
1655.                 if (fldidx == BL_TITLE)
1656.                     /* "<name> the <rank-title>", skip past "<name> the " */
1657.                     txtstr += (strlen(plname) + sizeof " the " - sizeof "");
1658.                 if (hl->rel == TXT_VALUE && hl->textmatch[0]) {
1659.                     if (fuzzymatch(hl->textmatch, txtstr, "\" -_", TRUE)) {
1660.                         rule = hl;
1661.                         exactmatch = TRUE;
1662.                     } else if (exactmatch) {
1663.                         ; /* already found best fit, skip "noneoftheabove" */
1664.                     } else if (fldidx == BL_TITLE
1665.                                && Upolyd && noneoftheabove(hl->textmatch)) {
1666.                         rule = hl;
1667.                     }
1668.                 }
1669.                 break;
1670.             case BL_TH_ALWAYS_HILITE:
1671.                 rule = hl;
1672.                 break;
1673.             case BL_TH_NONE:
1674.                 break;
1675.             default:
1676.                 break;
1677.             }
1678.         }
1679.     }
1680.     *colorptr = rule ? rule->coloridx : NO_COLOR;
1681.     return rule;
1682. }
1683. 
1684. STATIC_OVL void
1685. split_clridx(idx, coloridx, attrib)
1686. int idx;
1687. int *coloridx, *attrib;
1688. {
1689.     if (coloridx)
1690.         *coloridx = idx & 0x00FF;
1691.     if (attrib)
1692.         *attrib = (idx >> 8) & 0x00FF;
1693. }
1694. 
1695. /*
1696.  * This is the parser for the hilite options.
1697.  *
1698.  * parse_status_hl1() separates each hilite entry into
1699.  * a set of field threshold/action component strings,
1700.  * then calls parse_status_hl2() to parse further
1701.  * and configure the hilite.
1702.  */
1703. boolean
1704. parse_status_hl1(op, from_configfile)
1705. char *op;
1706. boolean from_configfile;
1707. {
1708. #define MAX_THRESH 21
1709.     char hsbuf[MAX_THRESH][QBUFSZ];
1710.     boolean rslt, badopt = FALSE;
1711.     int i, fldnum, ccount = 0;
1712.     char c;
1713. 
1714.     fldnum = 0;
1715.     for (i = 0; i < MAX_THRESH; ++i) {
1716.         hsbuf[i][0] = '\0';
1717.     }
1718.     while (*op && fldnum < MAX_THRESH && ccount < (QBUFSZ - 2)) {
1719.         c = lowc(*op);
1720.         if (c == ' ') {
1721.             if (fldnum >= 1) {
1722.                 if (fldnum == 1 && strcmpi(hsbuf[0], "title") == 0) {
1723.                     /* spaces are allowed in title */
1724.                     hsbuf[fldnum][ccount++] = c;
1725.                     hsbuf[fldnum][ccount] = '\0';
1726.                     op++;
1727.                     continue;
1728.                 }
1729.                 rslt = parse_status_hl2(hsbuf, from_configfile);
1730.                 if (!rslt) {
1731.                     badopt = TRUE;
1732.                     break;
1733.                 }
1734.             }
1735.             for (i = 0; i < MAX_THRESH; ++i) {
1736.                 hsbuf[i][0] = '\0';
1737.             }
1738.             fldnum = 0;
1739.             ccount = 0;
1740.         } else if (c == '/') {
1741.             fldnum++;
1742.             ccount = 0;
1743.         } else {
1744.             hsbuf[fldnum][ccount++] = c;
1745.             hsbuf[fldnum][ccount] = '\0';
1746.         }
1747.         op++;
1748.     }
1749.     if (fldnum >= 1 && !badopt) {
1750.         rslt = parse_status_hl2(hsbuf, from_configfile);
1751.         if (!rslt)
1752.             badopt = TRUE;
1753.     }
1754.     if (badopt)
1755.         return FALSE;
1756.     return TRUE;
1757. }
1758. 
1759. /* is str in the format of "[<>]?=?[-+]?[0-9]+%?" regex */
1760. STATIC_OVL boolean
1761. is_ltgt_percentnumber(str)
1762. const char *str;
1763. {
1764.     const char *s = str;
1765. 
1766.     if (*s == '<' || *s == '>')
1767.         s++;
1768.     if (*s == '=')
1769.         s++;
1770.     if (*s == '-' || *s == '+')
1771.         s++;
1772.     if (!digit(*s))
1773.         return FALSE;
1774.     while (digit(*s))
1775.         s++;
1776.     if (*s == '%')
1777.         s++;
1778.     return (*s == '\0');
1779. }
1780. 
1781. /* does str only contain "<>=-+0-9%" chars */
1782. STATIC_OVL boolean
1783. has_ltgt_percentnumber(str)
1784. const char *str;
1785. {
1786.     const char *s = str;
1787. 
1788.     while (*s) {
1789.         if (!index("<>=-+0123456789%", *s))
1790.             return FALSE;
1791.         s++;
1792.     }
1793.     return TRUE;
1794. }
1795. 
1796. /* splitsubfields(): splits str in place into '+' or '&' separated strings.
1797.  * returns number of strings, or -1 if more than maxsf or MAX_SUBFIELDS
1798.  */
1799. #define MAX_SUBFIELDS 16
1800. STATIC_OVL int
1801. splitsubfields(str, sfarr, maxsf)
1802. char *str;
1803. char ***sfarr;
1804. int maxsf;
1805. {
1806.     static char *subfields[MAX_SUBFIELDS];
1807.     char *st = (char *) 0;
1808.     int sf = 0;
1809. 
1810.     if (!str)
1811.         return 0;
1812.     for (sf = 0; sf < MAX_SUBFIELDS; ++sf)
1813.         subfields[sf] = (char *) 0;
1814. 
1815.     maxsf = (maxsf == 0) ? MAX_SUBFIELDS : min(maxsf, MAX_SUBFIELDS);
1816. 
1817.     if (index(str, '+') || index(str, '&')) {
1818.         char *c = str;
1819. 
1820.         sf = 0;
1821.         st = c;
1822.         while (*c && sf < maxsf) {
1823.             if (*c == '&' || *c == '+') {
1824.                 *c = '\0';
1825.                 subfields[sf] = st;
1826.                 st = c+1;
1827.                 sf++;
1828.             }
1829.             c++;
1830.         }
1831.         if (sf >= maxsf - 1)
1832.             return -1;
1833.         if (!*c && c != st)
1834.             subfields[sf++] = st;
1835.     } else {
1836.         sf = 1;
1837.         subfields[0] = str;
1838.     }
1839.     *sfarr = subfields;
1840.     return sf;
1841. }
1842. #undef MAX_SUBFIELDS
1843. 
1844. STATIC_OVL boolean
1845. is_fld_arrayvalues(str, arr, arrmin, arrmax, retidx)
1846. const char *str;
1847. const char *const *arr;
1848. int arrmin, arrmax;
1849. int *retidx;
1850. {
1851.     int i;
1852. 
1853.     for (i = arrmin; i < arrmax; i++)
1854.         if (!strcmpi(str, arr[i])) {
1855.             *retidx = i;
1856.             return TRUE;
1857.         }
1858.     return FALSE;
1859. }
1860. 
1861. STATIC_OVL int
1862. query_arrayvalue(querystr, arr, arrmin, arrmax)
1863. const char *querystr;
1864. const char *const *arr;
1865. int arrmin, arrmax;
1866. {
1867.     int i, res, ret = arrmin - 1;
1868.     winid tmpwin;
1869.     anything any;
1870.     menu_item *picks = (menu_item *) 0;
1871.     int adj = (arrmin > 0) ? 1 : arrmax;
1872. 
1873.     tmpwin = create_nhwindow(NHW_MENU);
1874.     start_menu(tmpwin);
1875. 
1876.     for (i = arrmin; i < arrmax; i++) {
1877.         any = zeroany;
1878.         any.a_int = i + adj;
1879.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
1880.                  arr[i], MENU_UNSELECTED);
1881.     }
1882. 
1883.     end_menu(tmpwin, querystr);
1884. 
1885.     res = select_menu(tmpwin, PICK_ONE, &picks);
1886.     destroy_nhwindow(tmpwin);
1887.     if (res > 0) {
1888.         ret = picks->item.a_int - adj;
1889.         free((genericptr_t) picks);
1890.     }
1891. 
1892.     return ret;
1893. }
1894. 
1895. STATIC_OVL void
1896. status_hilite_add_threshold(fld, hilite)
1897. int fld;
1898. struct hilite_s *hilite;
1899. {
1900.     struct hilite_s *new_hilite;
1901. 
1902.     if (!hilite)
1903.         return;
1904. 
1905.     /* alloc and initialize a new hilite_s struct */
1906.     new_hilite = (struct hilite_s *) alloc(sizeof (struct hilite_s));
1907.     *new_hilite = *hilite;   /* copy struct */
1908. 
1909.     new_hilite->set = TRUE;
1910.     new_hilite->fld = fld;
1911.     new_hilite->next = blstats[0][fld].thresholds;
1912.     blstats[0][fld].thresholds = new_hilite;
1913.     /* sort_hilites(fld) */
1914. 
1915.     /* current and prev must both point at the same hilites */
1916.     blstats[1][fld].thresholds = blstats[0][fld].thresholds;
1917. }
1918. 
1919. 
1920. STATIC_OVL boolean
1921. parse_status_hl2(s, from_configfile)
1922. char (*s)[QBUFSZ];
1923. boolean from_configfile;
1924. {
1925.     char *tmp, *how;
1926.     int sidx = 0, i = -1, dt = -1;
1927.     int coloridx = -1, successes = 0;
1928.     int disp_attrib = 0;
1929.     boolean percent, changed, numeric, down, up,
1930.             gt, lt, ge, le, eq, txtval, always;
1931.     const char *txt;
1932.     enum statusfields fld = BL_FLUSH;
1933.     struct hilite_s hilite;
1934.     char tmpbuf[BUFSZ];
1935.     static const char *aligntxt[] = { "chaotic", "neutral", "lawful" };
1936.     /* hu_stat[] from eat.c has trailing spaces which foul up comparisons */
1937.     static const char *hutxt[] = { "Satiated", "", "Hungry", "Weak",
1938.                                    "Fainting", "Fainted", "Starved" };
1939. 
1940.     /* Examples:
1941.         3.6.1:
1942.       OPTION=hilite_status: hitpoints/<10%/red
1943.       OPTION=hilite_status: hitpoints/<10%/red/<5%/purple/1/red+blink+inverse
1944.       OPTION=hilite_status: experience/down/red/up/green
1945.       OPTION=hilite_status: cap/strained/yellow/overtaxed/orange
1946.       OPTION=hilite_status: title/always/blue
1947.       OPTION=hilite_status: title/blue
1948.     */
1949. 
1950.     /* field name to statusfield */
1951.     fld = fldname_to_bl_indx(s[sidx]);
1952. 
1953.     if (fld == BL_CHARACTERISTICS) {
1954.         boolean res = FALSE;
1955. 
1956.         /* recursively set each of strength, dexterity, constitution, &c */
1957.         for (fld = BL_STR; fld <= BL_CH; fld++) {
1958.             Strcpy(s[sidx], initblstats[fld].fldname);
1959.             res = parse_status_hl2(s, from_configfile);
1960.             if (!res)
1961.                 return FALSE;
1962.         }
1963.         return TRUE;
1964.     }
1965.     if (fld == BL_FLUSH) {
1966.         config_error_add("Unknown status field '%s'", s[sidx]);
1967.         return FALSE;
1968.     }
1969.     if (fld == BL_CONDITION)
1970.         return parse_condition(s, sidx);
1971. 
1972.     ++sidx;
1973.     while (s[sidx]) {
1974.         char buf[BUFSZ], **subfields;
1975.         int sf = 0;     /* subfield count */
1976.         int kidx;
1977. 
1978.         txt = (const char *)0;
1979.         percent = numeric = always = FALSE;
1980.         down = up = changed = FALSE;
1981.         gt = ge = eq = le = lt = txtval = FALSE;
1982. 
1983.         /* threshold value */
1984.         if (!s[sidx][0])
1985.             return TRUE;
1986. 
1987.         memset((genericptr_t) &hilite, 0, sizeof (struct hilite_s));
1988.         hilite.set = FALSE; /* mark it "unset" */
1989.         hilite.fld = fld;
1990. 
1991.         if (*s[sidx + 1] == '\0' || !strcmpi(s[sidx], "always")) {
1992.             /* "field/always/color" OR "field/color" */
1993.             always = TRUE;
1994.             if (*s[sidx + 1] == '\0')
1995.                 sidx--;
1996.         } else if (!strcmpi(s[sidx], "up") || !strcmpi(s[sidx], "down")) {
1997.             if (initblstats[fld].anytype == ANY_STR)
1998.                 /* ordered string comparison is supported but LT/GT for
1999.                    the string fields (title, dungeon-level, alignment)
2000.                    is pointless; treat 'up' or 'down' for string fields
2001.                    as 'changed' rather than rejecting them outright */
2002.                 ;
2003.             else if (!strcmpi(s[sidx], "down"))
2004.                 down = TRUE;
2005.             else
2006.                 up = TRUE;
2007.             changed = TRUE;
2008.         } else if (fld == BL_CAP
2009.                    && is_fld_arrayvalues(s[sidx], enc_stat,
2010.                                          SLT_ENCUMBER, OVERLOADED + 1,
2011.                                          &kidx)) {
2012.             txt = enc_stat[kidx];
2013.             txtval = TRUE;
2014.         } else if (fld == BL_ALIGN
2015.                    && is_fld_arrayvalues(s[sidx], aligntxt, 0, 3, &kidx)) {
2016.             txt = aligntxt[kidx];
2017.             txtval = TRUE;
2018.         } else if (fld == BL_HUNGER
2019.                    && is_fld_arrayvalues(s[sidx], hutxt,
2020.                                          SATIATED, STARVED + 1, &kidx)) {
2021.             txt = hu_stat[kidx];   /* store hu_stat[] val, not hutxt[] */
2022.             txtval = TRUE;
2023.         } else if (!strcmpi(s[sidx], "changed")) {
2024.             changed = TRUE;
2025.         } else if (is_ltgt_percentnumber(s[sidx])) {
2026.             const char *op;
2027. 
2028.             tmp = s[sidx]; /* is_ltgt_() guarantees [<>]?=?[-+]?[0-9]+%? */
2029.             if (strchr(tmp, '%'))
2030.                percent = TRUE;
2031.             if (*tmp == '<') {
2032.                 if (tmp[1] == '=')
2033.                     le = TRUE;
2034.                 else
2035.                     lt = TRUE;
2036.             } else if (*tmp == '>') {
2037.                 if (tmp[1] == '=')
2038.                     ge = TRUE;
2039.                 else
2040.                     gt = TRUE;
2041.             }
2042.             /* '%', '<', '>' have served their purpose, '=' is either
2043.                part of '<' or '>' or optional for '=N', unary '+' is
2044.                just decorative, so get rid of them, leaving -?[0-9]+ */
2045.             tmp = stripchars(tmpbuf, "%<>=+", tmp);
2046.             numeric = TRUE;
2047.             dt = percent ? ANY_INT : initblstats[fld].anytype;
2048.             (void) s_to_anything(&hilite.value, tmp, dt);
2049. 
2050.             op = gt ? ">" : ge ? ">=" : lt ? "<" : le ? "<=" : "=";
2051.             if (dt == ANY_INT
2052.                 /* AC is the only field where negative values make sense but
2053.                    accept >-1 for other fields; reject <0 for non-AC */
2054.                 && (hilite.value.a_int
2055.                     < ((fld == BL_AC) ? -128 : gt ? -1 : lt ? 1 : 0)
2056.                 /* percentages have another more comprehensive check below */
2057.                     || hilite.value.a_int > (percent ? (lt ? 101 : 100)
2058.                                                      : LARGEST_INT))) {
2059.                 config_error_add("%s'%s%d%s'%s", threshold_value,
2060.                                  op, hilite.value.a_int, percent ? "%" : "",
2061.                                  is_out_of_range);
2062.                 return FALSE;
2063.             } else if (dt == ANY_LONG
2064.                        && (hilite.value.a_long < (gt ? -1L : lt ? 1L : 0L))) {
2065.                 config_error_add("%s'%s%ld'%s", threshold_value,
2066.                                  op, hilite.value.a_long, is_out_of_range);
2067.                 return FALSE;
2068.             }
2069.         } else if (initblstats[fld].anytype == ANY_STR) {
2070.             txt = s[sidx];
2071.             txtval = TRUE;
2072.         } else {
2073.             config_error_add(has_ltgt_percentnumber(s[sidx])
2074.                  ? "Wrong format '%s', expected a threshold number or percent"
2075.                  : "Unknown behavior '%s'",
2076.                              s[sidx]);
2077.             return FALSE;
2078.         }
2079. 
2080.         /* relationships {LT_VALUE, LE_VALUE, EQ_VALUE, GE_VALUE, GT_VALUE} */
2081.         if (gt || up)
2082.             hilite.rel = GT_VALUE;
2083.         else if (lt || down)
2084.             hilite.rel = LT_VALUE;
2085.         else if (ge)
2086.             hilite.rel = GE_VALUE;
2087.         else if (le)
2088.             hilite.rel = LE_VALUE;
2089.         else if (eq  || percent || numeric || changed)
2090.             hilite.rel = EQ_VALUE;
2091.         else if (txtval)
2092.             hilite.rel = TXT_VALUE;
2093.         else
2094.             hilite.rel = LT_VALUE;
2095. 
2096.         if (initblstats[fld].anytype == ANY_STR && (percent || numeric)) {
2097.             config_error_add("Field '%s' does not support numeric values",
2098.                              initblstats[fld].fldname);
2099.             return FALSE;
2100.         }
2101. 
2102.         if (percent) {
2103.             if (initblstats[fld].idxmax < 0) {
2104.                 config_error_add("Cannot use percent with '%s'",
2105.                                  initblstats[fld].fldname);
2106.                 return FALSE;
2107.             } else if ((hilite.value.a_int < -1)
2108.                        || (hilite.value.a_int == -1
2109.                            && hilite.value.a_int != GT_VALUE)
2110.                        || (hilite.value.a_int == 0
2111.                            && hilite.rel == LT_VALUE)
2112.                        || (hilite.value.a_int == 100
2113.                            && hilite.rel == GT_VALUE)
2114.                        || (hilite.value.a_int == 101
2115.                            && hilite.value.a_int != LT_VALUE)
2116.                        || (hilite.value.a_int > 101)) {
2117.                 config_error_add(
2118.                            "hilite_status: invalid percentage value '%s%d%%'",
2119.                                  (hilite.rel == LT_VALUE) ? "<"
2120.                                    : (hilite.rel == LE_VALUE) ? "<="
2121.                                      : (hilite.rel == GT_VALUE) ? ">"
2122.                                        : (hilite.rel == GE_VALUE) ? ">="
2123.                                          : "=",
2124.                                  hilite.value.a_int);
2125.                 return FALSE;
2126.             }
2127.         }
2128. 
2129.         /* actions */
2130.         sidx++;
2131.         how = s[sidx];
2132.         if (!how) {
2133.             if (!successes)
2134.                 return FALSE;
2135.         }
2136.         coloridx = -1;
2137.         Strcpy(buf, how);
2138.         sf = splitsubfields(buf, &subfields, 0);
2139. 
2140.         if (sf < 1)
2141.             return FALSE;
2142. 
2143.         disp_attrib = HL_UNDEF;
2144. 
2145.         for (i = 0; i < sf; ++i) {
2146.             int a = match_str2attr(subfields[i], FALSE);
2147. 
2148.             if (a == ATR_DIM)
2149.                 disp_attrib |= HL_DIM;
2150.             else if (a == ATR_BLINK)
2151.                 disp_attrib |= HL_BLINK;
2152.             else if (a == ATR_ULINE)
2153.                 disp_attrib |= HL_ULINE;
2154.             else if (a == ATR_INVERSE)
2155.                 disp_attrib |= HL_INVERSE;
2156.             else if (a == ATR_BOLD)
2157.                 disp_attrib |= HL_BOLD;
2158.             else if (a == ATR_NONE)
2159.                 disp_attrib = HL_NONE;
2160.             else {
2161.                 int c = match_str2clr(subfields[i]);
2162. 
2163.                 if (c >= CLR_MAX || coloridx != -1)
2164.                     return FALSE;
2165.                 coloridx = c;
2166.             }
2167.         }
2168.         if (coloridx == -1)
2169.             coloridx = NO_COLOR;
2170. 
2171.         /* Assign the values */
2172.         hilite.coloridx = coloridx | (disp_attrib << 8);
2173. 
2174.         if (always)
2175.             hilite.behavior = BL_TH_ALWAYS_HILITE;
2176.         else if (percent)
2177.             hilite.behavior = BL_TH_VAL_PERCENTAGE;
2178.         else if (changed)
2179.             hilite.behavior = BL_TH_UPDOWN;
2180.         else if (numeric)
2181.             hilite.behavior = BL_TH_VAL_ABSOLUTE;
2182.         else if (txtval)
2183.             hilite.behavior = BL_TH_TEXTMATCH;
2184.         else if (hilite.value.a_void)
2185.             hilite.behavior = BL_TH_VAL_ABSOLUTE;
2186.        else
2187.             hilite.behavior = BL_TH_NONE;
2188. 
2189.         hilite.anytype = dt;
2190. 
2191.         if (hilite.behavior == BL_TH_TEXTMATCH && txt) {
2192.             (void) strncpy(hilite.textmatch, txt, sizeof hilite.textmatch);
2193.             hilite.textmatch[sizeof hilite.textmatch - 1] = '\0';
2194.             (void) trimspaces(hilite.textmatch);
2195.         }
2196. 
2197.         status_hilite_add_threshold(fld, &hilite);
2198. 
2199.         successes++;
2200.         sidx++;
2201.     }
2202. 
2203.     return TRUE;
2204. }
2205. #endif /* STATUS_HILITES */
2206. 
2207. const struct condmap valid_conditions[] = {
2208.     { "stone",    BL_MASK_STONE },
2209.     { "slime",    BL_MASK_SLIME },
2210.     { "strngl",   BL_MASK_STRNGL },
2211.     { "foodPois", BL_MASK_FOODPOIS },
2212.     { "termIll",  BL_MASK_TERMILL },
2213.     { "blind",    BL_MASK_BLIND },
2214.     { "deaf",     BL_MASK_DEAF },
2215.     { "stun",     BL_MASK_STUN },
2216.     { "conf",     BL_MASK_CONF },
2217.     { "hallu",    BL_MASK_HALLU },
2218.     { "lev",      BL_MASK_LEV },
2219.     { "fly",      BL_MASK_FLY },
2220.     { "ride",     BL_MASK_RIDE },
2221. };
2222. 
2223. #ifdef STATUS_HILITES
2224. 
2225. const struct condmap condition_aliases[] = {
2226.     { "strangled",      BL_MASK_STRNGL },
2227.     { "all",            BL_MASK_STONE | BL_MASK_SLIME | BL_MASK_STRNGL
2228.                         | BL_MASK_FOODPOIS | BL_MASK_TERMILL
2229.                         | BL_MASK_BLIND | BL_MASK_DEAF | BL_MASK_STUN
2230.                         | BL_MASK_CONF | BL_MASK_HALLU
2231.                         | BL_MASK_LEV | BL_MASK_FLY | BL_MASK_RIDE },
2232.     { "major_troubles", BL_MASK_STONE | BL_MASK_SLIME | BL_MASK_STRNGL
2233.                         | BL_MASK_FOODPOIS | BL_MASK_TERMILL },
2234.     { "minor_troubles", BL_MASK_BLIND | BL_MASK_DEAF | BL_MASK_STUN
2235.                         | BL_MASK_CONF | BL_MASK_HALLU },
2236.     { "movement",       BL_MASK_LEV | BL_MASK_FLY | BL_MASK_RIDE }
2237. };
2238. 
2239. unsigned long
2240. query_conditions()
2241. {
2242.     int i,res;
2243.     unsigned long ret = 0UL;
2244.     winid tmpwin;
2245.     anything any;
2246.     menu_item *picks = (menu_item *) 0;
2247. 
2248.     tmpwin = create_nhwindow(NHW_MENU);
2249.     start_menu(tmpwin);
2250. 
2251.     for (i = 0; i < SIZE(valid_conditions); i++) {
2252.         any = zeroany;
2253.         any.a_ulong = valid_conditions[i].bitmask;
2254.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2255.                  valid_conditions[i].id, MENU_UNSELECTED);
2256.     }
2257. 
2258.     end_menu(tmpwin, "Choose status conditions");
2259. 
2260.     res = select_menu(tmpwin, PICK_ANY, &picks);
2261.     destroy_nhwindow(tmpwin);
2262.     if (res > 0) {
2263.         for (i = 0; i < res; i++)
2264.             ret |= picks[i].item.a_ulong;
2265.         free((genericptr_t) picks);
2266.     }
2267.     return ret;
2268. }
2269. 
2270. STATIC_OVL char *
2271. conditionbitmask2str(ul)
2272. unsigned long ul;
2273. {
2274.     static char buf[BUFSZ];
2275.     int i;
2276.     boolean first = TRUE;
2277.     const char *alias = (char *) 0;
2278. 
2279. 
2280.     buf[0] = '\0';
2281.     if (!ul)
2282.         return buf;
2283. 
2284.     for (i = 1; i < SIZE(condition_aliases); i++)
2285.         if (condition_aliases[i].bitmask == ul)
2286.             alias = condition_aliases[i].id;
2287. 
2288.     for (i = 0; i < SIZE(valid_conditions); i++)
2289.         if ((valid_conditions[i].bitmask & ul) != 0UL) {
2290.             Sprintf(eos(buf), "%s%s", (first) ? "" : "+",
2291.                     valid_conditions[i].id);
2292.             first = FALSE;
2293.         }
2294. 
2295.     if (!first && alias)
2296.         Sprintf(buf, "%s", alias);
2297. 
2298.     return buf;
2299. }
2300. 
2301. STATIC_OVL unsigned long
2302. match_str2conditionbitmask(str)
2303. const char *str;
2304. {
2305.     int i, nmatches = 0;
2306.     unsigned long mask = 0UL;
2307. 
2308.     if (str && *str) {
2309.         /* check matches to canonical names */
2310.         for (i = 0; i < SIZE(valid_conditions); i++)
2311.             if (fuzzymatch(valid_conditions[i].id, str, " -_", TRUE)) {
2312.                 mask |= valid_conditions[i].bitmask;
2313.                 nmatches++;
2314.             }
2315. 
2316.         if (!nmatches) {
2317.             /* check aliases */
2318.             for (i = 0; i < SIZE(condition_aliases); i++)
2319.                 if (fuzzymatch(condition_aliases[i].id, str, " -_", TRUE)) {
2320.                     mask |= condition_aliases[i].bitmask;
2321.                     nmatches++;
2322.                 }
2323.         }
2324. 
2325.         if (!nmatches) {
2326.             /* check partial matches to aliases */
2327.             int len = (int) strlen(str);
2328. 
2329.             for (i = 0; i < SIZE(condition_aliases); i++)
2330.                 if (!strncmpi(str, condition_aliases[i].id, len)) {
2331.                     mask |= condition_aliases[i].bitmask;
2332.                     nmatches++;
2333.                 }
2334.         }
2335.     }
2336. 
2337.     return mask;
2338. }
2339. 
2340. STATIC_OVL unsigned long
2341. str2conditionbitmask(str)
2342. char *str;
2343. {
2344.     unsigned long conditions_bitmask = 0UL;
2345.     char **subfields;
2346.     int i, sf;
2347. 
2348.     sf = splitsubfields(str, &subfields, SIZE(valid_conditions));
2349. 
2350.     if (sf < 1)
2351.         return 0UL;
2352. 
2353.     for (i = 0; i < sf; ++i) {
2354.         unsigned long bm = match_str2conditionbitmask(subfields[i]);
2355. 
2356.         if (!bm) {
2357.             config_error_add("Unknown condition '%s'", subfields[i]);
2358.             return 0UL;
2359.         }
2360.         conditions_bitmask |= bm;
2361.     }
2362.     return conditions_bitmask;
2363. }
2364. 
2365. STATIC_OVL boolean
2366. parse_condition(s, sidx)
2367. char (*s)[QBUFSZ];
2368. int sidx;
2369. {
2370.     int i;
2371.     int coloridx = NO_COLOR;
2372.     char *tmp, *how;
2373.     unsigned long conditions_bitmask = 0UL;
2374.     boolean success = FALSE;
2375. 
2376.     if (!s)
2377.         return FALSE;
2378. 
2379.     /*3.6.1:
2380.       OPTION=hilite_status: condition/stone+slime+foodPois/red&inverse */
2381. 
2382.     /*
2383.      * TODO?
2384.      *  It would be simpler to treat each condition (also hunger state
2385.      *  and encumbrance level) as if it were a separate field.  That
2386.      *  way they could have either or both 'changed' temporary rule and
2387.      *  'always' persistent rule and wouldn't need convoluted access to
2388.      *  the intended color and attributes.
2389.      */
2390. 
2391.     sidx++;
2392.     while(s[sidx]) {
2393.         int sf = 0;     /* subfield count */
2394.         char buf[BUFSZ], **subfields;
2395. 
2396.         tmp = s[sidx];
2397.         if (!*tmp) {
2398.             if (!success)
2399.                 config_error_add("Missing condition(s)");
2400.             return success;
2401.         }
2402. 
2403.         Strcpy(buf, tmp);
2404.         conditions_bitmask = str2conditionbitmask(buf);
2405. 
2406.         if (!conditions_bitmask)
2407.             return FALSE;
2408. 
2409.         /*
2410.          * We have the conditions_bitmask with bits set for
2411.          * each ailment we want in a particular color and/or
2412.          * attribute, but we need to assign it to an array of
2413.          * bitmasks indexed by the color chosen
2414.          *        (0 to (CLR_MAX - 1))
2415.          * and/or attributes chosen
2416.          *        (HL_ATTCLR_DIM to (BL_ATTCLR_MAX - 1))
2417.          * We still have to parse the colors and attributes out.
2418.          */
2419. 
2420.         /* actions */
2421.         sidx++;
2422.         how = s[sidx];
2423.         if (!how || !*how) {
2424.             config_error_add("Missing color+attribute");
2425.             return FALSE;
2426.         }
2427. 
2428.         Strcpy(buf, how);
2429.         sf = splitsubfields(buf, &subfields, 0);
2430. 
2431.         /*
2432.          * conditions_bitmask now has bits set representing
2433.          * the conditions that player wants represented, but
2434.          * now we parse out *how* they will be represented.
2435.          *
2436.          * Only 1 colour is allowed, but potentially multiple
2437.          * attributes are allowed.
2438.          *
2439.          * We have the following additional array offsets to
2440.          * use for storing the attributes beyond the end of
2441.          * the color indexes, all of which are less than CLR_MAX.
2442.          * HL_ATTCLR_DIM        = CLR_MAX
2443.          * HL_ATTCLR_BLINK      = CLR_MAX + 1
2444.          * HL_ATTCLR_ULINE      = CLR_MAX + 2
2445.          * HL_ATTCLR_INVERSE    = CLR_MAX + 3
2446.          * HL_ATTCLR_BOLD       = CLR_MAX + 4
2447.          * HL_ATTCLR_MAX        = CLR_MAX + 5 (this is past array boundary)
2448.          *
2449.          */
2450. 
2451.         for (i = 0; i < sf; ++i) {
2452.             int a = match_str2attr(subfields[i], FALSE);
2453. 
2454.             if (a == ATR_DIM)
2455.                 cond_hilites[HL_ATTCLR_DIM] |= conditions_bitmask;
2456.             else if (a == ATR_BLINK)
2457.                 cond_hilites[HL_ATTCLR_BLINK] |= conditions_bitmask;
2458.             else if (a == ATR_ULINE)
2459.                 cond_hilites[HL_ATTCLR_ULINE] |= conditions_bitmask;
2460.             else if (a == ATR_INVERSE)
2461.                 cond_hilites[HL_ATTCLR_INVERSE] |= conditions_bitmask;
2462.             else if (a == ATR_BOLD)
2463.                 cond_hilites[HL_ATTCLR_BOLD] |= conditions_bitmask;
2464.             else if (a == ATR_NONE) {
2465.                 cond_hilites[HL_ATTCLR_DIM] &= ~conditions_bitmask;
2466.                 cond_hilites[HL_ATTCLR_BLINK] &= ~conditions_bitmask;
2467.                 cond_hilites[HL_ATTCLR_ULINE] &= ~conditions_bitmask;
2468.                 cond_hilites[HL_ATTCLR_INVERSE] &= ~conditions_bitmask;
2469.                 cond_hilites[HL_ATTCLR_BOLD] &= ~conditions_bitmask;
2470.             } else {
2471.                 int k = match_str2clr(subfields[i]);
2472. 
2473.                 if (k >= CLR_MAX)
2474.                     return FALSE;
2475.                 coloridx = k;
2476.             }
2477.         }
2478.         /* set the bits in the appropriate member of the
2479.            condition array according to color chosen as index */
2480. 
2481.         cond_hilites[coloridx] |= conditions_bitmask;
2482.         success = TRUE;
2483.         sidx++;
2484.     }
2485.     return TRUE;
2486. }
2487. 
2488. void
2489. clear_status_hilites()
2490. {
2491.     int i;
2492. 
2493.     for (i = 0; i < MAXBLSTATS; ++i) {
2494.         struct hilite_s *temp, *next;
2495. 
2496.         for (temp = blstats[0][i].thresholds; temp; temp = next) {
2497.             next = temp->next;
2498.             free(temp);
2499.         }
2500.         blstats[0][i].thresholds = blstats[1][i].thresholds = 0;
2501.         /* pointer into thresholds list, now stale */
2502.         blstats[0][i].hilite_rule = blstats[1][i].hilite_rule = 0;
2503.     }
2504. }
2505. 
2506. STATIC_OVL char *
2507. hlattr2attrname(attrib, buf, bufsz)
2508. int attrib, bufsz;
2509. char *buf;
2510. {
2511.     if (attrib && buf) {
2512.         char attbuf[BUFSZ];
2513.         int k, first = 0;
2514. 
2515.         attbuf[0] = '\0';
2516.         if (attrib == HL_NONE) {
2517.             Strcpy(buf, "normal");
2518.             return buf;
2519.         }
2520. 
2521.         if (attrib & HL_BOLD)
2522.             Strcat(attbuf, first++ ? "+bold" : "bold");
2523.         if (attrib & HL_INVERSE)
2524.             Strcat(attbuf, first++ ? "+inverse" : "inverse");
2525.         if (attrib & HL_ULINE)
2526.             Strcat(attbuf, first++ ? "+underline" : "underline");
2527.         if (attrib & HL_BLINK)
2528.             Strcat(attbuf, first++ ? "+blink" : "blink");
2529.         if (attrib & HL_DIM)
2530.             Strcat(attbuf, first++ ? "+dim" : "dim");
2531. 
2532.         k = strlen(attbuf);
2533.         if (k < (bufsz - 1))
2534.             Strcpy(buf, attbuf);
2535.         return buf;
2536.     }
2537.     return (char *) 0;
2538. }
2539. 
2540. 
2541. struct _status_hilite_line_str {
2542.     int id;
2543.     int fld;
2544.     struct hilite_s *hl;
2545.     unsigned long mask;
2546.     char str[BUFSZ];
2547.     struct _status_hilite_line_str *next;
2548. };
2549. 
2550. static struct _status_hilite_line_str *status_hilite_str = 0;
2551. static int status_hilite_str_id = 0;
2552. 
2553. STATIC_OVL void
2554. status_hilite_linestr_add(fld, hl, mask, str)
2555. int fld;
2556. struct hilite_s *hl;
2557. unsigned long mask;
2558. const char *str;
2559. {
2560.     struct _status_hilite_line_str *tmp, *nxt;
2561. 
2562.     tmp = (struct _status_hilite_line_str *) alloc(sizeof *tmp);
2563.     (void) memset(tmp, 0, sizeof *tmp);
2564.     tmp->next = (struct _status_hilite_line_str *) 0;
2565. 
2566.     tmp->id = ++status_hilite_str_id;
2567.     tmp->fld = fld;
2568.     tmp->hl = hl;
2569.     tmp->mask = mask;
2570.     if (fld == BL_TITLE)
2571.         Strcpy(tmp->str, str);
2572.     else
2573.         (void) stripchars(tmp->str, " ", str);
2574. 
2575.     if ((nxt = status_hilite_str) != 0) {
2576.         while (nxt->next)
2577.             nxt = nxt->next;
2578.         nxt->next = tmp;
2579.     } else {
2580.         status_hilite_str = tmp;
2581.     }
2582. }
2583. 
2584. STATIC_OVL void
2585. status_hilite_linestr_done()
2586. {
2587.     struct _status_hilite_line_str *nxt, *tmp = status_hilite_str;
2588. 
2589.     while (tmp) {
2590.         nxt = tmp->next;
2591.         free(tmp);
2592.         tmp = nxt;
2593.     }
2594.     status_hilite_str = (struct _status_hilite_line_str *) 0;
2595.     status_hilite_str_id = 0;
2596. }
2597. 
2598. STATIC_OVL int
2599. status_hilite_linestr_countfield(fld)
2600. int fld;
2601. {
2602.     struct _status_hilite_line_str *tmp;
2603.     boolean countall = (fld == BL_FLUSH);
2604.     int count = 0;
2605. 
2606.     for (tmp = status_hilite_str; tmp; tmp = tmp->next) {
2607.         if (countall || tmp->fld == fld)
2608.             count++;
2609.     }
2610.     return count;
2611. }
2612. 
2613. /* used by options handling, doset(options.c) */
2614. int
2615. count_status_hilites(VOID_ARGS)
2616. {
2617.     int count;
2618. 
2619.     status_hilite_linestr_gather();
2620.     count = status_hilite_linestr_countfield(BL_FLUSH);
2621.     status_hilite_linestr_done();
2622.     return count;
2623. }
2624. 
2625. STATIC_OVL void
2626. status_hilite_linestr_gather_conditions()
2627. {
2628.     int i;
2629.     struct _cond_map {
2630.         unsigned long bm;
2631.         unsigned long clratr;
2632.     } cond_maps[SIZE(valid_conditions)];
2633. 
2634.     (void) memset(cond_maps, 0,
2635.                   SIZE(valid_conditions) * sizeof (struct _cond_map));
2636. 
2637.     for (i = 0; i < SIZE(valid_conditions); i++) {
2638.         int clr = NO_COLOR;
2639.         int atr = HL_NONE;
2640.         int j;
2641. 
2642.         for (j = 0; j < CLR_MAX; j++)
2643.             if (cond_hilites[j] & valid_conditions[i].bitmask) {
2644.                 clr = j;
2645.                 break;
2646.             }
2647.         if (cond_hilites[HL_ATTCLR_DIM] & valid_conditions[i].bitmask)
2648.             atr |= HL_DIM;
2649.         if (cond_hilites[HL_ATTCLR_BOLD] & valid_conditions[i].bitmask)
2650.             atr |= HL_BOLD;
2651.         if (cond_hilites[HL_ATTCLR_BLINK] & valid_conditions[i].bitmask)
2652.             atr |= HL_BLINK;
2653.         if (cond_hilites[HL_ATTCLR_ULINE] & valid_conditions[i].bitmask)
2654.             atr |= HL_ULINE;
2655.         if (cond_hilites[HL_ATTCLR_INVERSE] & valid_conditions[i].bitmask)
2656.             atr |= HL_INVERSE;
2657.         if (atr != HL_NONE)
2658.             atr &= ~HL_NONE;
2659. 
2660.         if (clr != NO_COLOR || atr != HL_NONE) {
2661.             unsigned long ca = clr | (atr << 8);
2662.             boolean added_condmap = FALSE;
2663. 
2664.             for (j = 0; j < SIZE(valid_conditions); j++)
2665.                 if (cond_maps[j].clratr == ca) {
2666.                     cond_maps[j].bm |= valid_conditions[i].bitmask;
2667.                     added_condmap = TRUE;
2668.                     break;
2669.                 }
2670.             if (!added_condmap) {
2671.                 for (j = 0; j < SIZE(valid_conditions); j++)
2672.                     if (!cond_maps[j].bm) {
2673.                         cond_maps[j].bm = valid_conditions[i].bitmask;
2674.                         cond_maps[j].clratr = ca;
2675.                         break;
2676.                     }
2677.             }
2678.         }
2679.     }
2680. 
2681.     for (i = 0; i < SIZE(valid_conditions); i++)
2682.         if (cond_maps[i].bm) {
2683.             int clr = NO_COLOR, atr = HL_NONE;
2684. 
2685.             split_clridx(cond_maps[i].clratr, &clr, &atr);
2686.             if (clr != NO_COLOR || atr != HL_NONE) {
2687.                 char clrbuf[BUFSZ];
2688.                 char attrbuf[BUFSZ];
2689.                 char condbuf[BUFSZ];
2690.                 char *tmpattr;
2691. 
2692.                 (void) strNsubst(strcpy(clrbuf, clr2colorname(clr)),
2693.                                  " ", "-", 0);
2694.                 tmpattr = hlattr2attrname(atr, attrbuf, BUFSZ);
2695.                 if (tmpattr)
2696.                     Sprintf(eos(clrbuf), "&%s", tmpattr);
2697.                 Sprintf(condbuf, "condition/%s/%s",
2698.                         conditionbitmask2str(cond_maps[i].bm), clrbuf);
2699.                 status_hilite_linestr_add(BL_CONDITION, 0,
2700.                                           cond_maps[i].bm, condbuf);
2701.             }
2702.         }
2703. }
2704. 
2705. STATIC_OVL void
2706. status_hilite_linestr_gather()
2707. {
2708.     int i;
2709.     struct hilite_s *hl;
2710. 
2711.     status_hilite_linestr_done();
2712. 
2713.     for (i = 0; i < MAXBLSTATS; i++) {
2714.         hl = blstats[0][i].thresholds;
2715.         while (hl) {
2716.             status_hilite_linestr_add(i, hl, 0UL, status_hilite2str(hl));
2717.             hl = hl->next;
2718.         }
2719.     }
2720. 
2721.     status_hilite_linestr_gather_conditions();
2722. }
2723. 
2724. 
2725. char *
2726. status_hilite2str(hl)
2727. struct hilite_s *hl;
2728. {
2729.     static char buf[BUFSZ];
2730.     int clr = 0, attr = 0;
2731.     char behavebuf[BUFSZ];
2732.     char clrbuf[BUFSZ];
2733.     char attrbuf[BUFSZ];
2734.     char *tmpattr;
2735.     const char *op;
2736. 
2737.     if (!hl)
2738.         return (char *) 0;
2739. 
2740.     behavebuf[0] = '\0';
2741.     clrbuf[0] = '\0';
2742.     op = (hl->rel == LT_VALUE) ? "<"
2743.            : (hl->rel == LE_VALUE) ? "<="
2744.              : (hl->rel == GT_VALUE) ? ">"
2745.                : (hl->rel == GE_VALUE) ? ">="
2746.                  : (hl->rel == EQ_VALUE) ? "="
2747.                    : 0;
2748. 
2749.     switch (hl->behavior) {
2750.     case BL_TH_VAL_PERCENTAGE:
2751.         if (op)
2752.             Sprintf(behavebuf, "%s%d%%", op, hl->value.a_int);
2753.         else
2754.             impossible("hl->behavior=percentage, rel error");
2755.         break;
2756.     case BL_TH_UPDOWN:
2757.         if (hl->rel == LT_VALUE)
2758.             Sprintf(behavebuf, "down");
2759.         else if (hl->rel == GT_VALUE)
2760.             Sprintf(behavebuf, "up");
2761.         else if (hl->rel == EQ_VALUE)
2762.             Sprintf(behavebuf, "changed");
2763.         else
2764.             impossible("hl->behavior=updown, rel error");
2765.         break;
2766.     case BL_TH_VAL_ABSOLUTE:
2767.         if (op)
2768.             Sprintf(behavebuf, "%s%d", op, hl->value.a_int);
2769.         else
2770.             impossible("hl->behavior=absolute, rel error");
2771.         break;
2772.     case BL_TH_TEXTMATCH:
2773.         if (hl->rel == TXT_VALUE && hl->textmatch[0])
2774.             Sprintf(behavebuf, "%s", hl->textmatch);
2775.         else
2776.             impossible("hl->behavior=textmatch, rel or textmatch error");
2777.         break;
2778.     case BL_TH_CONDITION:
2779.         if (hl->rel == EQ_VALUE)
2780.             Sprintf(behavebuf, "%s", conditionbitmask2str(hl->value.a_ulong));
2781.         else
2782.             impossible("hl->behavior=condition, rel error");
2783.         break;
2784.     case BL_TH_ALWAYS_HILITE:
2785.         Sprintf(behavebuf, "always");
2786.         break;
2787.     case BL_TH_NONE:
2788.         break;
2789.     default:
2790.         break;
2791.     }
2792. 
2793.     split_clridx(hl->coloridx, &clr, &attr);
2794.     (void) strNsubst(strcpy(clrbuf, clr2colorname(clr)), " ", "-", 0);
2795.     if (attr != HL_UNDEF) {
2796.         if ((tmpattr = hlattr2attrname(attr, attrbuf, BUFSZ)) != 0)
2797.             Sprintf(eos(clrbuf), "&%s", tmpattr);
2798.     }
2799.     Sprintf(buf, "%s/%s/%s", initblstats[hl->fld].fldname, behavebuf, clrbuf);
2800. 
2801.     return buf;
2802. }
2803. 
2804. STATIC_OVL int
2805. status_hilite_menu_choose_field()
2806. {
2807.     winid tmpwin;
2808.     int i, res, fld = BL_FLUSH;
2809.     anything any;
2810.     menu_item *picks = (menu_item *) 0;
2811. 
2812.     tmpwin = create_nhwindow(NHW_MENU);
2813.     start_menu(tmpwin);
2814. 
2815.     for (i = 0; i < MAXBLSTATS; i++) {
2816. #ifndef SCORE_ON_BOTL
2817.         if (initblstats[i].fld == BL_SCORE
2818.             && !blstats[0][BL_SCORE].thresholds)
2819.             continue;
2820. #endif
2821.         any = zeroany;
2822.         any.a_int = (i + 1);
2823.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2824.                  initblstats[i].fldname, MENU_UNSELECTED);
2825.     }
2826. 
2827.     end_menu(tmpwin, "Select a hilite field:");
2828. 
2829.     res = select_menu(tmpwin, PICK_ONE, &picks);
2830.     destroy_nhwindow(tmpwin);
2831.     if (res > 0) {
2832.         fld = picks->item.a_int - 1;
2833.         free((genericptr_t) picks);
2834.     }
2835.     return fld;
2836. }
2837. 
2838. STATIC_OVL int
2839. status_hilite_menu_choose_behavior(fld)
2840. int fld;
2841. {
2842.     winid tmpwin;
2843.     int res = 0, beh = BL_TH_NONE-1;
2844.     anything any;
2845.     menu_item *picks = (menu_item *) 0;
2846.     char buf[BUFSZ];
2847.     int at;
2848.     int onlybeh = BL_TH_NONE, nopts = 0;
2849. 
2850.     if (fld < 0 || fld >= MAXBLSTATS)
2851.         return BL_TH_NONE;
2852. 
2853.     at = initblstats[fld].anytype;
2854. 
2855.     tmpwin = create_nhwindow(NHW_MENU);
2856.     start_menu(tmpwin);
2857. 
2858.     if (fld != BL_CONDITION) {
2859.         any = zeroany;
2860.         any.a_int = onlybeh = BL_TH_ALWAYS_HILITE;
2861.         Sprintf(buf, "Always highlight %s", initblstats[fld].fldname);
2862.         add_menu(tmpwin, NO_GLYPH, &any, 'a', 0, ATR_NONE,
2863.                  buf, MENU_UNSELECTED);
2864.         nopts++;
2865.     }
2866. 
2867.     if (fld == BL_CONDITION) {
2868.         any = zeroany;
2869.         any.a_int = onlybeh = BL_TH_CONDITION;
2870.         add_menu(tmpwin, NO_GLYPH, &any, 'b', 0, ATR_NONE,
2871.                  "Bitmask of conditions", MENU_UNSELECTED);
2872.         nopts++;
2873.     }
2874. 
2875.     if (fld != BL_CONDITION) {
2876.         any = zeroany;
2877.         any.a_int = onlybeh = BL_TH_UPDOWN;
2878.         Sprintf(buf, "%s value changes", initblstats[fld].fldname);
2879.         add_menu(tmpwin, NO_GLYPH, &any, 'c', 0, ATR_NONE,
2880.                  buf, MENU_UNSELECTED);
2881.         nopts++;
2882.     }
2883. 
2884.     if (fld != BL_CAP && fld != BL_HUNGER
2885.         && (at == ANY_INT || at == ANY_LONG)) {
2886.         any = zeroany;
2887.         any.a_int = onlybeh = BL_TH_VAL_ABSOLUTE;
2888.         add_menu(tmpwin, NO_GLYPH, &any, 'n', 0, ATR_NONE,
2889.                  "Number threshold", MENU_UNSELECTED);
2890.         nopts++;
2891.     }
2892. 
2893.     if (initblstats[fld].idxmax >= 0) {
2894.         any = zeroany;
2895.         any.a_int = onlybeh = BL_TH_VAL_PERCENTAGE;
2896.         add_menu(tmpwin, NO_GLYPH, &any, 'p', 0, ATR_NONE,
2897.                  "Percentage threshold", MENU_UNSELECTED);
2898.         nopts++;
2899.     }
2900. 
2901.     if (initblstats[fld].anytype == ANY_STR
2902.         || fld == BL_CAP || fld == BL_HUNGER) {
2903.         any = zeroany;
2904.         any.a_int = onlybeh = BL_TH_TEXTMATCH;
2905.         Sprintf(buf, "%s text match", initblstats[fld].fldname);
2906.         add_menu(tmpwin, NO_GLYPH, &any, 't', 0, ATR_NONE,
2907.                  buf, MENU_UNSELECTED);
2908.         nopts++;
2909.     }
2910. 
2911.     Sprintf(buf, "Select %s field hilite behavior:", initblstats[fld].fldname);
2912.     end_menu(tmpwin, buf);
2913. 
2914.     if (nopts > 1) {
2915.         res = select_menu(tmpwin, PICK_ONE, &picks);
2916.         if (res == 0) /* none chosen*/
2917.             beh = BL_TH_NONE;
2918.         else if (res == -1) /* menu cancelled */
2919.             beh = (BL_TH_NONE - 1);
2920.     } else if (onlybeh != BL_TH_NONE)
2921.         beh = onlybeh;
2922.     destroy_nhwindow(tmpwin);
2923.     if (res > 0) {
2924.         beh = picks->item.a_int;
2925.         free((genericptr_t) picks);
2926.     }
2927.     return beh;
2928. }
2929. 
2930. STATIC_OVL int
2931. status_hilite_menu_choose_updownboth(fld, str, ltok, gtok)
2932. int fld;
2933. const char *str;
2934. boolean ltok, gtok;
2935. {
2936.     int res, ret = NO_LTEQGT;
2937.     winid tmpwin;
2938.     char buf[BUFSZ];
2939.     anything any;
2940.     menu_item *picks = (menu_item *) 0;
2941. 
2942.     tmpwin = create_nhwindow(NHW_MENU);
2943.     start_menu(tmpwin);
2944. 
2945.     if (ltok) {
2946.         if (str)
2947.             Sprintf(buf, "%s than %s",
2948.                     (fld == BL_AC) ? "Better (lower)" : "Less", str);
2949.         else
2950.             Sprintf(buf, "Value goes down");
2951.         any = zeroany;
2952.         any.a_int = 10 + LT_VALUE;
2953.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2954.                  buf, MENU_UNSELECTED);
2955. 
2956.         if (str) {
2957.             Sprintf(buf, "%s or %s",
2958.                     str, (fld == BL_AC) ? "better (lower)" : "less");
2959.             any = zeroany;
2960.             any.a_int = 10 + LE_VALUE;
2961.             add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2962.                      buf, MENU_UNSELECTED);
2963.         }
2964.     }
2965. 
2966.     if (str)
2967.         Sprintf(buf, "Exactly %s", str);
2968.     else
2969.         Sprintf(buf, "Value changes");
2970.     any = zeroany;
2971.     any.a_int = 10 + EQ_VALUE;
2972.     add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2973.              buf, MENU_UNSELECTED);
2974. 
2975.     if (gtok) {
2976.         if (str) {
2977.             Sprintf(buf, "%s or %s",
2978.                     str, (fld == BL_AC) ? "worse (higher)" : "more");
2979.             any = zeroany;
2980.             any.a_int = 10 + GE_VALUE;
2981.             add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2982.                      buf, MENU_UNSELECTED);
2983.         }
2984. 
2985.         if (str)
2986.             Sprintf(buf, "%s than %s",
2987.                     (fld == BL_AC) ? "Worse (higher)" : "More", str);
2988.         else
2989.             Sprintf(buf, "Value goes up");
2990.         any = zeroany;
2991.         any.a_int = 10 + GT_VALUE;
2992.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2993.              buf, MENU_UNSELECTED);
2994.     }
2995.     Sprintf(buf, "Select field %s value:", initblstats[fld].fldname);
2996.     end_menu(tmpwin, buf);
2997. 
2998.     res = select_menu(tmpwin, PICK_ONE, &picks);
2999.     destroy_nhwindow(tmpwin);
3000.     if (res > 0) {
3001.         ret = picks->item.a_int - 10;
3002.         free((genericptr_t) picks);
3003.     }
3004. 
3005.     return ret;
3006. }
3007. 
3008. STATIC_OVL boolean
3009. status_hilite_menu_add(origfld)
3010. int origfld;
3011. {
3012.     int fld;
3013.     int behavior;
3014.     int lt_gt_eq;
3015.     int clr = NO_COLOR, atr = HL_UNDEF;
3016.     struct hilite_s hilite;
3017.     unsigned long cond = 0UL;
3018.     char colorqry[BUFSZ];
3019.     char attrqry[BUFSZ];
3020. 
3021. choose_field:
3022.     fld = origfld;
3023.     if (fld == BL_FLUSH) {
3024.         fld = status_hilite_menu_choose_field();
3025.         /* isn't this redundant given what follows? */
3026.         if (fld == BL_FLUSH)
3027.             return FALSE;
3028.     }
3029. 
3030.     if (fld == BL_FLUSH)
3031.         return FALSE;
3032. 
3033.     colorqry[0] = '\0';
3034.     attrqry[0] = '\0';
3035. 
3036.     memset((genericptr_t) &hilite, 0, sizeof (struct hilite_s));
3037.     hilite.next = (struct hilite_s *) 0;
3038.     hilite.set = FALSE; /* mark it "unset" */
3039.     hilite.fld = fld;
3040. 
3041. choose_behavior:
3042.     behavior = status_hilite_menu_choose_behavior(fld);
3043. 
3044.     if (behavior == (BL_TH_NONE - 1)) {
3045.         return FALSE;
3046.     } else if (behavior == BL_TH_NONE) {
3047.         if (origfld == BL_FLUSH)
3048.             goto choose_field;
3049.         return FALSE;
3050.     }
3051. 
3052.     hilite.behavior = behavior;
3053. 
3054. choose_value:
3055.     if (behavior == BL_TH_VAL_PERCENTAGE
3056.         || behavior == BL_TH_VAL_ABSOLUTE) {
3057.         char inbuf[BUFSZ], buf[BUFSZ];
3058.         anything aval;
3059.         int val, dt;
3060.         boolean gotnum = FALSE, percent = (behavior == BL_TH_VAL_PERCENTAGE);
3061.         char *inp, *numstart;
3062.         const char *op;
3063. 
3064.         lt_gt_eq = NO_LTEQGT; /* not set up yet */
3065.         inbuf[0] = '\0';
3066.         Sprintf(buf, "Enter %svalue for %s threshold:",
3067.                 percent ? "percentage " : "",
3068.                 initblstats[fld].fldname);
3069.         getlin(buf, inbuf);
3070.         if (inbuf[0] == '\0' || inbuf[0] == '\033')
3071.             goto choose_behavior;
3072. 
3073.         inp = numstart = trimspaces(inbuf);
3074.         if (!*inp)
3075.             goto choose_behavior;
3076. 
3077.         /* allow user to enter "<50%" or ">50" or just "50"
3078.            or <=50% or >=50 or =50 */
3079.         if (*inp == '>' || *inp == '<' || *inp == '=') {
3080.             lt_gt_eq = (*inp == '>') ? ((inp[1] == '=') ? GE_VALUE : GT_VALUE)
3081.                      : (*inp == '<') ? ((inp[1] == '=') ? LE_VALUE : LT_VALUE)
3082.                        : EQ_VALUE;
3083.             *inp++ = ' ';
3084.             numstart++;
3085.             if (lt_gt_eq == GE_VALUE || lt_gt_eq == LE_VALUE) {
3086.                 *inp++ = ' ';
3087.                 numstart++;
3088.             }
3089.         }
3090.         if (*inp == '-') {
3091.             inp++;
3092.         } else if (*inp == '+') {
3093.             *inp++ = ' ';
3094.             numstart++;
3095.         }
3096.         while (digit(*inp)) {
3097.             inp++;
3098.             gotnum = TRUE;
3099.         }
3100.         if (*inp == '%') {
3101.             if (!percent) {
3102.                 pline("Not expecting a percentage.");
3103.                 goto choose_behavior;
3104.             }
3105.             *inp = '\0'; /* strip '%' [this accepts trailing junk!] */
3106.         } else if (*inp) {
3107.             /* some random characters */
3108.             pline("\"%s\" is not a recognized number.", inp);
3109.             goto choose_value;
3110.         }
3111.         if (!gotnum) {
3112.             pline("Is that an invisible number?");
3113.             goto choose_value;
3114.         }
3115.         op = (lt_gt_eq == LT_VALUE) ? "<"
3116.                : (lt_gt_eq == LE_VALUE) ? "<="
3117.                  : (lt_gt_eq == GT_VALUE) ? ">"
3118.                    : (lt_gt_eq == GE_VALUE) ? ">="
3119.                      : (lt_gt_eq == EQ_VALUE) ? "="
3120.                        : ""; /* didn't specify lt_gt_eq with number */
3121. 
3122.         aval = zeroany;
3123.         dt = percent ? ANY_INT : initblstats[fld].anytype;
3124.         (void) s_to_anything(&aval, numstart, dt);
3125. 
3126.         if (percent) {
3127.             val = aval.a_int;
3128.             if (initblstats[fld].idxmax == -1) {
3129.                 pline("Field '%s' does not support percentage values.",
3130.                       initblstats[fld].fldname);
3131.                 behavior = BL_TH_VAL_ABSOLUTE;
3132.                 goto choose_value;
3133.             }
3134.             /* if player only specified a number then lt_gt_eq isn't set
3135.                up yet and the >-1 and <101 exceptions can't be honored;
3136.                deliberate use of those should be uncommon enough for
3137.                that to be palatable; for 0 and 100, choose_updown_both()
3138.                will prevent useless operations */
3139.             if ((val < 0 && (val != -1 || lt_gt_eq != GT_VALUE))
3140.                 || (val == 0 && lt_gt_eq == LT_VALUE)
3141.                 || (val == 100 && lt_gt_eq == GT_VALUE)
3142.                 || (val > 100 && (val != 101 || lt_gt_eq != LT_VALUE))) {
3143.                 pline("'%s%d%%' is not a valid percent value.", op, val);
3144.                 goto choose_value;
3145.             }
3146.             /* restore suffix for use in color and attribute prompts */
3147.             if (!index(numstart, '%'))
3148.                 Strcat(numstart, "%");
3149. 
3150.         /* reject negative values except for AC and >-1; reject 0 for < */
3151.         } else if (dt == ANY_INT
3152.                    && (aval.a_int < ((fld == BL_AC) ? -128
3153.                                      : (lt_gt_eq == GT_VALUE) ? -1
3154.                                        : (lt_gt_eq == LT_VALUE) ? 1 : 0))) {
3155.             pline("%s'%s%d'%s", threshold_value,
3156.                   op, aval.a_int, is_out_of_range);
3157.             goto choose_value;
3158.         } else if (dt == ANY_LONG
3159.                    && (aval.a_long < ((lt_gt_eq == GT_VALUE) ? -1L
3160.                                       : (lt_gt_eq == LT_VALUE) ? 1L : 0L))) {
3161.             pline("%s'%s%ld'%s", threshold_value,
3162.                   op, aval.a_long, is_out_of_range);
3163.             goto choose_value;
3164.         }
3165. 
3166.         if (lt_gt_eq == NO_LTEQGT) {
3167.             boolean ltok = ((dt == ANY_INT)
3168.                             ? (aval.a_int > 0 || fld == BL_AC)
3169.                             : (aval.a_long > 0L)),
3170.                     gtok = (!percent || aval.a_long < 100);
3171. 
3172.             lt_gt_eq = status_hilite_menu_choose_updownboth(fld, inbuf,
3173.                                                             ltok, gtok);
3174.             if (lt_gt_eq == NO_LTEQGT)
3175.                 goto choose_value;
3176.         }
3177. 
3178.         Sprintf(colorqry, "Choose a color for when %s is %s%s%s:",
3179.                 initblstats[fld].fldname,
3180.                 (lt_gt_eq == LT_VALUE) ? "less than "
3181.                   : (lt_gt_eq == GT_VALUE) ? "more than "
3182.                     : "",
3183.                 numstart,
3184.                 (lt_gt_eq == LE_VALUE) ? " or less"
3185.                   : (lt_gt_eq == GE_VALUE) ? " or more"
3186.                     : "");
3187.         Sprintf(attrqry, "Choose attribute for when %s is %s%s%s:",
3188.                 initblstats[fld].fldname,
3189.                 (lt_gt_eq == LT_VALUE) ? "less than "
3190.                   : (lt_gt_eq == GT_VALUE) ? "more than "
3191.                     : "",
3192.                 numstart,
3193.                 (lt_gt_eq == LE_VALUE) ? " or less"
3194.                   : (lt_gt_eq == GE_VALUE) ? " or more"
3195.                     : "");
3196. 
3197.         hilite.rel = lt_gt_eq;
3198.         hilite.value = aval;
3199.     } else if (behavior == BL_TH_UPDOWN) {
3200.         if (initblstats[fld].anytype != ANY_STR) {
3201.             boolean ltok = (fld != BL_TIME), gtok = TRUE;
3202. 
3203.             lt_gt_eq = status_hilite_menu_choose_updownboth(fld, (char *)0,
3204.                                                             ltok, gtok);
3205.             if (lt_gt_eq == NO_LTEQGT)
3206.                 goto choose_behavior;
3207.         } else { /* ANY_STR */
3208.             /* player picked '<field> value changes' in outer menu;
3209.                ordered string comparison is supported but LT/GT for the
3210.                string status fields (title, dungeon level, alignment)
3211.                is pointless; rather than calling ..._choose_updownboth()
3212.                with ltok==False plus gtok=False and having a menu with a
3213.                single choice, skip it altogether and just use 'changed' */
3214.             lt_gt_eq = EQ_VALUE;
3215.         }
3216.         Sprintf(colorqry, "Choose a color for when %s %s:",
3217.                 initblstats[fld].fldname,
3218.                 (lt_gt_eq == EQ_VALUE) ? "changes"
3219.                   : (lt_gt_eq == LT_VALUE) ? "decreases"
3220.                     : "increases");
3221.         Sprintf(attrqry, "Choose attribute for when %s %s:",
3222.                 initblstats[fld].fldname,
3223.                 (lt_gt_eq == EQ_VALUE) ? "changes"
3224.                   : (lt_gt_eq == LT_VALUE) ? "decreases"
3225.                     : "increases");
3226.         hilite.rel = lt_gt_eq;
3227.     } else if (behavior == BL_TH_CONDITION) {
3228.         cond = query_conditions();
3229.         if (!cond) {
3230.             if (origfld == BL_FLUSH)
3231.                 goto choose_field;
3232.             return FALSE;
3233.         }
3234.         Sprintf(colorqry, "Choose a color for conditions %s:",
3235.                 conditionbitmask2str(cond));
3236.         Sprintf(attrqry, "Choose attribute for conditions %s:",
3237.                 conditionbitmask2str(cond));
3238.     } else if (behavior == BL_TH_TEXTMATCH) {
3239.         char qry_buf[BUFSZ];
3240. 
3241.         Sprintf(qry_buf, "%s %s text value to match:",
3242.                 (fld == BL_CAP
3243.                  || fld == BL_ALIGN
3244.                  || fld == BL_HUNGER
3245.                  || fld == BL_TITLE) ? "Choose" : "Enter",
3246.                 initblstats[fld].fldname);
3247.         if (fld == BL_CAP) {
3248.             int rv = query_arrayvalue(qry_buf,
3249.                                       enc_stat,
3250.                                       SLT_ENCUMBER, OVERLOADED + 1);
3251. 
3252.             if (rv < SLT_ENCUMBER)
3253.                 goto choose_behavior;
3254. 
3255.             hilite.rel = TXT_VALUE;
3256.             Strcpy(hilite.textmatch, enc_stat[rv]);
3257.         } else if (fld == BL_ALIGN) {
3258.             static const char *aligntxt[] = { "chaotic", "neutral", "lawful" };
3259.             int rv = query_arrayvalue(qry_buf,
3260.                                       aligntxt, 0, 2 + 1);
3261. 
3262.             if (rv < 0)
3263.                 goto choose_behavior;
3264. 
3265.             hilite.rel = TXT_VALUE;
3266.             Strcpy(hilite.textmatch, aligntxt[rv]);
3267.         } else if (fld == BL_HUNGER) {
3268.             static const char *hutxt[] = { "Satiated", (char *) 0, "Hungry",
3269.                                            "Weak", "Fainting", "Fainted",
3270.                                            "Starved" };
3271.             int rv = query_arrayvalue(qry_buf,
3272.                                       hutxt,
3273.                                       SATIATED, STARVED + 1);
3274. 
3275.             if (rv < SATIATED)
3276.                 goto choose_behavior;
3277. 
3278.             hilite.rel = TXT_VALUE;
3279.             Strcpy(hilite.textmatch, hutxt[rv]);
3280.         } else if (fld == BL_TITLE) {
3281.             const char *rolelist[3 * 9 + 1];
3282.             char mbuf[MAXVALWIDTH], fbuf[MAXVALWIDTH], obuf[MAXVALWIDTH];
3283.             int i, j, rv;
3284. 
3285.             for (i = j = 0; i < 9; i++) {
3286.                 Sprintf(mbuf, "\"%s\"", urole.rank[i].m);
3287.                 if (urole.rank[i].f) {
3288.                     Sprintf(fbuf, "\"%s\"", urole.rank[i].f);
3289.                     Sprintf(obuf, "%s or %s",
3290.                             flags.female ? fbuf : mbuf,
3291.                             flags.female ? mbuf : fbuf);
3292.                 } else {
3293.                     fbuf[0] = obuf[0] = '\0';
3294.                 }
3295.                 if (flags.female) {
3296.                     if (*fbuf)
3297.                         rolelist[j++] = dupstr(fbuf);
3298.                     rolelist[j++] = dupstr(mbuf);
3299.                     if (*obuf)
3300.                         rolelist[j++] = dupstr(obuf);
3301.                 } else {
3302.                     rolelist[j++] = dupstr(mbuf);
3303.                     if (*fbuf)
3304.                         rolelist[j++] = dupstr(fbuf);
3305.                     if (*obuf)
3306.                         rolelist[j++] = dupstr(obuf);
3307.                 }
3308.             }
3309.             rolelist[j++] = dupstr("\"none of the above (polymorphed)\"");
3310. 
3311.             rv = query_arrayvalue(qry_buf, rolelist, 0, j);
3312.             if (rv >= 0) {
3313.                 hilite.rel = TXT_VALUE;
3314.                 Strcpy(hilite.textmatch, rolelist[rv]);
3315.             }
3316.             for (i = 0; i < j; i++)
3317.                 free((genericptr_t) rolelist[i]), rolelist[i] = 0;
3318.             if (rv < 0)
3319.                 goto choose_behavior;
3320.         } else {
3321.             char inbuf[BUFSZ];
3322. 
3323.             inbuf[0] = '\0';
3324.             getlin(qry_buf, inbuf);
3325.             if (inbuf[0] == '\0' || inbuf[0] == '\033')
3326.                 goto choose_behavior;
3327. 
3328.             hilite.rel = TXT_VALUE;
3329.             if (strlen(inbuf) < sizeof hilite.textmatch)
3330.                 Strcpy(hilite.textmatch, inbuf);
3331.             else
3332.                 return FALSE;
3333.         }
3334.         Sprintf(colorqry, "Choose a color for when %s is '%s':",
3335.                 initblstats[fld].fldname, hilite.textmatch);
3336.         Sprintf(attrqry, "Choose attribute for when %s is '%s':",
3337.                 initblstats[fld].fldname, hilite.textmatch);
3338.     } else if (behavior == BL_TH_ALWAYS_HILITE) {
3339.         Sprintf(colorqry, "Choose a color to always hilite %s:",
3340.                 initblstats[fld].fldname);
3341.         Sprintf(attrqry, "Choose attribute to always hilite %s:",
3342.                 initblstats[fld].fldname);
3343.     }
3344. 
3345. choose_color:
3346.     clr = query_color(colorqry);
3347.     if (clr == -1) {
3348.         if (behavior != BL_TH_ALWAYS_HILITE)
3349.             goto choose_value;
3350.         else
3351.             goto choose_behavior;
3352.     }
3353.     atr = query_attr(attrqry);
3354.     if (atr == -1)
3355.         goto choose_color;
3356. 
3357.     if (behavior == BL_TH_CONDITION) {
3358.         char clrbuf[BUFSZ];
3359.         char attrbuf[BUFSZ];
3360.         char *tmpattr;
3361. 
3362.         if (atr & HL_DIM)
3363.             cond_hilites[HL_ATTCLR_DIM] |= cond;
3364.         if (atr & HL_BLINK)
3365.             cond_hilites[HL_ATTCLR_BLINK] |= cond;
3366.         if (atr & HL_ULINE)
3367.             cond_hilites[HL_ATTCLR_ULINE] |= cond;
3368.         if (atr & HL_INVERSE)
3369.             cond_hilites[HL_ATTCLR_INVERSE] |= cond;
3370.         if (atr & HL_BOLD)
3371.             cond_hilites[HL_ATTCLR_BOLD] |= cond;
3372.         if (atr == HL_NONE) {
3373.             cond_hilites[HL_ATTCLR_DIM] &= ~cond;
3374.             cond_hilites[HL_ATTCLR_BLINK] &= ~cond;
3375.             cond_hilites[HL_ATTCLR_ULINE] &= ~cond;
3376.             cond_hilites[HL_ATTCLR_INVERSE] &= ~cond;
3377.             cond_hilites[HL_ATTCLR_BOLD] &= ~cond;
3378.         }
3379.         cond_hilites[clr] |= cond;
3380.         (void) strNsubst(strcpy(clrbuf, clr2colorname(clr)), " ", "-", 0);
3381.         tmpattr = hlattr2attrname(atr, attrbuf, BUFSZ);
3382.         if (tmpattr)
3383.             Sprintf(eos(clrbuf), "&%s", tmpattr);
3384.         pline("Added hilite condition/%s/%s",
3385.               conditionbitmask2str(cond), clrbuf);
3386.     } else {
3387.         char *p, *q;
3388. 
3389.         hilite.coloridx = clr | (atr << 8);
3390.         hilite.anytype = initblstats[fld].anytype;
3391. 
3392.         if (fld == BL_TITLE && (p = strstri(hilite.textmatch, " or ")) != 0) {
3393.             /* split menu choice "male-rank or female-rank" into two distinct
3394.                but otherwise identical rules, "male-rank" and "female-rank" */
3395.             *p = '\0'; /* chop off " or female-rank" */
3396.             /* new rule for male-rank */
3397.             status_hilite_add_threshold(fld, &hilite);
3398.             pline("Added hilite %s", status_hilite2str(&hilite));
3399.             /* transfer female-rank to start of hilite.textmatch buffer */
3400.             p += sizeof " or " - sizeof "";
3401.             q = hilite.textmatch;
3402.             while ((*q++ = *p++) != '\0')
3403.                 continue;
3404.             /* proceed with normal addition of new rule */
3405.         }
3406.         status_hilite_add_threshold(fld, &hilite);
3407.         pline("Added hilite %s", status_hilite2str(&hilite));
3408.     }
3409.     reset_status_hilites();
3410.     return TRUE;
3411. }
3412. 
3413. boolean
3414. status_hilite_remove(id)
3415. int id;
3416. {
3417.     struct _status_hilite_line_str *hlstr = status_hilite_str;
3418. 
3419.     while (hlstr && hlstr->id != id) {
3420.         hlstr = hlstr->next;
3421.     }
3422. 
3423.     if (!hlstr)
3424.         return FALSE;
3425. 
3426.     if (hlstr->fld == BL_CONDITION) {
3427.         int i;
3428. 
3429.         for (i = 0; i < CLR_MAX; i++)
3430.             cond_hilites[i] &= ~hlstr->mask;
3431.         cond_hilites[HL_ATTCLR_DIM] &= ~hlstr->mask;
3432.         cond_hilites[HL_ATTCLR_BOLD] &= ~hlstr->mask;
3433.         cond_hilites[HL_ATTCLR_BLINK] &= ~hlstr->mask;
3434.         cond_hilites[HL_ATTCLR_ULINE] &= ~hlstr->mask;
3435.         cond_hilites[HL_ATTCLR_INVERSE] &= ~hlstr->mask;
3436.         return TRUE;
3437.     } else {
3438.         int fld = hlstr->fld;
3439.         struct hilite_s *hl, *hlprev = (struct hilite_s *) 0;
3440. 
3441.         for (hl = blstats[0][fld].thresholds; hl; hl = hl->next) {
3442.             if (hlstr->hl == hl) {
3443.                 if (hlprev) {
3444.                     hlprev->next = hl->next;
3445.                 } else {
3446.                     blstats[0][fld].thresholds = hl->next;
3447.                     blstats[1][fld].thresholds = blstats[0][fld].thresholds;
3448.                 }
3449.                 if (blstats[0][fld].hilite_rule == hl) {
3450.                     blstats[0][fld].hilite_rule
3451.                         = blstats[1][fld].hilite_rule = (struct hilite_s *) 0;
3452.                     blstats[0][fld].time = blstats[1][fld].time = 0L;
3453.                 }
3454.                 free((genericptr_t) hl);
3455.                 return TRUE;
3456.             }
3457.             hlprev = hl;
3458.         }
3459.     }
3460.     return FALSE;
3461. }
3462. 
3463. boolean
3464. status_hilite_menu_fld(fld)
3465. int fld;
3466. {
3467.     winid tmpwin;
3468.     int i, res;
3469.     menu_item *picks = (menu_item *) 0;
3470.     anything any;
3471.     int count = status_hilite_linestr_countfield(fld);
3472.     struct _status_hilite_line_str *hlstr;
3473.     char buf[BUFSZ];
3474.     boolean acted = FALSE;
3475. 
3476.     if (!count) {
3477.         if (status_hilite_menu_add(fld)) {
3478.             status_hilite_linestr_done();
3479.             status_hilite_linestr_gather();
3480.             count = status_hilite_linestr_countfield(fld);
3481.         } else
3482.             return FALSE;
3483.     }
3484. 
3485.     tmpwin = create_nhwindow(NHW_MENU);
3486.     start_menu(tmpwin);
3487. 
3488.     if (count) {
3489.         hlstr = status_hilite_str;
3490.         while (hlstr) {
3491.             if (hlstr->fld == fld) {
3492.                 any = zeroany;
3493.                 any.a_int = hlstr->id;
3494.                 add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
3495.                          hlstr->str, MENU_UNSELECTED);
3496.             }
3497.             hlstr = hlstr->next;
3498.         }
3499.     } else {
3500.         any = zeroany;
3501.         Sprintf(buf, "No current hilites for %s", initblstats[fld].fldname);
3502.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, buf, MENU_UNSELECTED);
3503.     }
3504. 
3505.     /* separator line */
3506.     any = zeroany;
3507.     add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
3508. 
3509.     if (count) {
3510.         any = zeroany;
3511.         any.a_int = -1;
3512.         add_menu(tmpwin, NO_GLYPH, &any, 'X', 0, ATR_NONE,
3513.                  "Remove selected hilites", MENU_UNSELECTED);
3514.     }
3515. 
3516. #ifndef SCORE_ON_BOTL
3517.     if (fld == BL_SCORE) {
3518.         /* suppress 'Z - Add a new hilite' for 'score' when SCORE_ON_BOTL
3519.            is disabled; we wouldn't be called for 'score' unless it has
3520.            hilite rules from the config file, so count must be positive
3521.            (hence there's no risk that we're putting up an empty menu) */
3522.         ;
3523.     } else
3524. #endif
3525.     {
3526.         any = zeroany;
3527.         any.a_int = -2;
3528.         add_menu(tmpwin, NO_GLYPH, &any, 'Z', 0, ATR_NONE,
3529.                  "Add a new hilite", MENU_UNSELECTED);
3530.     }
3531. 
3532.     Sprintf(buf, "Current %s hilites:", initblstats[fld].fldname);
3533.     end_menu(tmpwin, buf);
3534. 
3535.     if ((res = select_menu(tmpwin, PICK_ANY, &picks)) > 0) {
3536.         int mode = 0;
3537. 
3538.         for (i = 0; i < res; i++) {
3539.             int idx = picks[i].item.a_int;
3540. 
3541.             if (idx == -1) {
3542.                 /* delete selected hilites */
3543.                 if (mode)
3544.                     goto shlmenu_free;
3545.                 mode = -1;
3546.                 break;
3547.             } else if (idx == -2) {
3548.                 /* create a new hilite */
3549.                 if (mode)
3550.                     goto shlmenu_free;
3551.                 mode = -2;
3552.                 break;
3553.             }
3554.         }
3555. 
3556.         if (mode == -1) {
3557.             /* delete selected hilites */
3558.             for (i = 0; i < res; i++) {
3559.                 int idx = picks[i].item.a_int;
3560. 
3561.                 if (idx > 0)
3562.                     (void) status_hilite_remove(idx);
3563.             }
3564.             reset_status_hilites();
3565.             acted = TRUE;
3566.         } else if (mode == -2) {
3567.             /* create a new hilite */
3568.             if (status_hilite_menu_add(fld))
3569.                 acted = TRUE;
3570.         }
3571. 
3572.         free((genericptr_t) picks);
3573.     }
3574. 
3575. shlmenu_free:
3576. 
3577.     picks = (menu_item *) 0;
3578.     destroy_nhwindow(tmpwin);
3579.     return acted;
3580. }
3581. 
3582. void
3583. status_hilites_viewall()
3584. {
3585.     winid datawin;
3586.     struct _status_hilite_line_str *hlstr = status_hilite_str;
3587.     char buf[BUFSZ];
3588. 
3589.     datawin = create_nhwindow(NHW_TEXT);
3590. 
3591.     while (hlstr) {
3592.         Sprintf(buf, "OPTIONS=hilite_status: %.*s",
3593.                 (int) (BUFSZ - sizeof "OPTIONS=hilite_status: " - 1),
3594.                 hlstr->str);
3595.         putstr(datawin, 0, buf);
3596.         hlstr = hlstr->next;
3597.     }
3598. 
3599.     display_nhwindow(datawin, FALSE);
3600.     destroy_nhwindow(datawin);
3601. }
3602. 
3603. boolean
3604. status_hilite_menu()
3605. {
3606.     winid tmpwin;
3607.     int i, res;
3608.     menu_item *picks = (menu_item *) 0;
3609.     anything any;
3610.     boolean redo;
3611.     int countall;
3612. 
3613. shlmenu_redo:
3614.     redo = FALSE;
3615. 
3616.     tmpwin = create_nhwindow(NHW_MENU);
3617.     start_menu(tmpwin);
3618. 
3619.     status_hilite_linestr_gather();
3620.     countall = status_hilite_linestr_countfield(BL_FLUSH);
3621.     if (countall) {
3622.         any = zeroany;
3623.         any.a_int = -1;
3624.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
3625.                  "View all hilites in config format", MENU_UNSELECTED);
3626. 
3627.         any = zeroany;
3628.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
3629.     }
3630. 
3631.     for (i = 0; i < MAXBLSTATS; i++) {
3632.         int count = status_hilite_linestr_countfield(i);
3633.         char buf[BUFSZ];
3634. 
3635. #ifndef SCORE_ON_BOTL
3636.         /* config file might contain rules for highlighting 'score'
3637.            even when SCORE_ON_BOTL is disabled; if so, 'O' command
3638.            menus will show them and allow deletions but not additions,
3639.            otherwise, it won't show 'score' at all */
3640.         if (initblstats[i].fld == BL_SCORE && !count)
3641.             continue;
3642. #endif
3643.         any = zeroany;
3644.         any.a_int = i + 1;
3645.         Sprintf(buf, "%-18s", initblstats[i].fldname);
3646.         if (count)
3647.             Sprintf(eos(buf), " (%d defined)", count);
3648.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
3649.                  buf, MENU_UNSELECTED);
3650.     }
3651. 
3652.     end_menu(tmpwin, "Status hilites:");
3653.     if ((res = select_menu(tmpwin, PICK_ONE, &picks)) > 0) {
3654.         i = picks->item.a_int - 1;
3655.         if (i < 0)
3656.             status_hilites_viewall();
3657.         else
3658.             (void) status_hilite_menu_fld(i);
3659.         free((genericptr_t) picks), picks = (menu_item *) 0;
3660.         redo = TRUE;
3661.     }
3662. 
3663.     destroy_nhwindow(tmpwin);
3664.     countall = status_hilite_linestr_countfield(BL_FLUSH);
3665.     status_hilite_linestr_done();
3666. 
3667.     if (redo)
3668.         goto shlmenu_redo;
3669. 
3670.     /* hilite_delta=='statushilites' does double duty:  it is the
3671.        number of turns for temporary highlights to remain visible
3672.        and also when non-zero it is the flag to enable highlighting */
3673.     if (countall > 0 && !iflags.hilite_delta)
3674.         pline(
3675.  "To have highlights become active, set 'statushilites' option to non-zero.");
3676. 
3677.     return TRUE;
3678. }
3679. 
3680. #endif /* STATUS_HILITES */
3681. 
3682. /*botl.c*/