more updates to the bookstore tribute

This commit is contained in:
nhmall
2015-03-20 00:58:29 -04:00
parent c72c531c2c
commit 0a8d24d87d
7 changed files with 168 additions and 19 deletions

View File

@@ -4,6 +4,7 @@
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "dlb.h"
#define Your_Own_Role(mndx) \
((mndx) == urole.malenum || \
@@ -2119,4 +2120,134 @@ 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*/

View File

@@ -373,7 +373,9 @@ boolean mkspecl;
struct obj *novel=mksobj_at(SPE_NOVEL, sx, sy, FALSE, FALSE);
if (novel) novel = oname(novel, noveltitle(&novidx));
context.tribute.bookidx = novidx;
context.tribute.book = 1;
context.tribute.passagecnt = 0;
context.tribute.passagenum = 0;
context.tribute.bookstock = TRUE;
return;
}
@@ -620,7 +622,7 @@ register struct mkroom *sroom;
make_engr_at(m, n, buf, 0L, DUST);
}
if (context.tribute.enabled && !context.tribute.book) {
if (context.tribute.enabled && !context.tribute.bookstock) {
/*
* Out of the number of spots where we're actually
* going to put stuff, randomly single out one in particular.

View File

@@ -413,7 +413,6 @@ register struct obj *spellbook;
{
int booktype = spellbook->otyp;
boolean confused = (Confusion != 0);
const char *whatchadoin = "memorize the spell";
boolean too_hard = FALSE;
/* attempting to read dull book may make hero fall asleep */
@@ -454,15 +453,9 @@ register struct obj *spellbook;
/* 3.6 tribute */
if (booktype == SPE_NOVEL) {
/* Obtain current Terry Pratchett book
title for the current game. */
title for the current game. */
const char *tribtitle = noveltitle(&context.tribute.bookidx);
/*
* Place code here to pull desireable
* random passages from somewhere (data.base?)
* using tribtitle as the key?
*/
pline("Reading \"%s.\"", tribtitle); /* debug confirmation */
read_tribbook(tribtitle, spellbook);
return(1);
}