More book tribute integration

This commit is contained in:
nhmall
2015-03-21 10:33:59 -04:00
parent b342beea2b
commit d7ff451301
10 changed files with 259 additions and 230 deletions

View File

@@ -4,7 +4,6 @@
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "dlb.h"
#define Your_Own_Role(mndx) \
((mndx) == urole.malenum || \
@@ -2120,134 +2119,4 @@ create_particular()
return madeany;
}
/* 3.6 tribute code */
#define SECTIONSCOPE 1
#define BOOKSCOPE 2
#define PASSAGESCOPE 3
void
read_tribbook(tribtitle, bookobj)
char *tribtitle;
struct obj *bookobj;
{
dlb *fp;
char *endp;
char line[BUFSZ];
int scopes[4] = {0, SECTIONSCOPE, BOOKSCOPE, PASSAGESCOPE};
int scope = 0, section = 0, passage = 0, book = 0;
int linect = 0, passagecnt = 0, rndpassage = 0;
char *sectionnm = "", *booknm = "";
boolean matched = FALSE;
winid tribwin = WIN_ERR;
if (!tribtitle || !bookobj) {
pline("It is an incomprehensible foreign translation of \"%s\"",
tribtitle);
return;
}
debugpline2("Reading %s, a %s.", tribtitle, xname(bookobj));
fp = dlb_fopen(TRIBUTEFILE, "r");
if (!fp) {
/* this is actually an error - cannot open tribute file! */
pline("Overcome with fond memories, you snap \"%s\" shut!",
tribtitle);
return;
}
/*
* Syntax (not case-sensitive):
* %section books
* %section death
*
* In the books section:
* %book title(n)
* where title=book title
* (n)= total number of passages present for this book
* %passage n
* where n=sequential passage number
*
* %e ends the passage/book/section
* If in a passage, it marks the end of that passage.
* If in a book, it marks the end of that book.
* If in a section, it marks the end of that section.
*/
while (dlb_fgets(line, sizeof line, fp) != 0) {
linect++;
if ((endp = index(line, '\n')) != 0) *endp = 0;
switch (line[0]) {
case '%':
if (!strncmpi(&line[1], "section ", sizeof("section ")-1)) {
} else if (!strncmpi(&line[1], "book ", sizeof("book ")-1)) {
char *st = &line[6], *p1, *p2;
if ((p1 = index(st, '(')) != 0) {
*p1++ = '\0';
if ((p2 = index(p1, ')')) != 0) {
*p2 = '\0';
passagecnt = atoi(p1);
/* sanity check here caps #passages at 50 */
if ((passagecnt > 0) && (passagecnt < 50)) {
scope = BOOKSCOPE;
if (!strcmpi(st, tribtitle)) {
matched = TRUE;
rndpassage = rnd(passagecnt);
context.tribute.passagecnt = passagecnt;
context.tribute.passagenum = rndpassage;
} else {
matched = FALSE;
rndpassage = 0;
context.tribute.passagecnt = 0;
context.tribute.passagenum = 0;
/* this should not happen, but compensate */
if (tribwin != WIN_ERR)
destroy_nhwindow(tribwin);
}
}
}
}
} else if (!strncmpi(&line[1], "passage ", sizeof("passage ")-1)) {
int passagenum = 0;
char *st = &line[9];
while(*st == ' ' || *st == '\t') st++;
if (*st && digit(*st) && (strlen(st) < 3))
passagenum = atoi(st);
if (passagenum && (passagenum <= passagecnt)) {
scope = PASSAGESCOPE;
if (passagenum == rndpassage)
tribwin = create_nhwindow(NHW_MENU);
}
} else if (!strncmpi(&line[1], "e ", sizeof("e ")-1)) {
if (matched && (scope == PASSAGESCOPE) && tribwin != WIN_ERR)
goto cleanup;
} else {
pline("tribute file error: bad %%e command, line %d.",
linect);
}
break;
case '#':
/* comment only, next! */
break;
default:
if (matched && (scope == PASSAGESCOPE) && tribwin != WIN_ERR) {
putstr(tribwin, 0, line);
}
}
}
cleanup:
if (tribwin != WIN_ERR) {
if (matched && (scope == PASSAGESCOPE))
display_nhwindow(tribwin, FALSE);
destroy_nhwindow(tribwin);
tribwin = WIN_ERR;
}
(void) dlb_fclose(fp);
return;
}
/*read.c*/