get_rnd_text() for rumors and other stuff
Solve the uneven distribution situation that has been present for picking random rumors for a long time and for random engravings, epitaphs, and hallucinatory monster names since 3.6.0. This relies on the previous partial solution where short lines have been padded to a longer length. When that length is N and random seek lands in a long line of length L, retry if the position is in the first L-N characters. Put differently, it if takes more than N characters to reach the next newline, reject that random seek and try again. This effectively makes long lines behave as if they had the same length of N as the short lines have been padded to and when all lines are the same length, all entries have the same chance to be chosen.
This commit is contained in:
@@ -2263,7 +2263,7 @@ extern const char *Goodbye(void);
|
||||
/* ### rumors.c ### */
|
||||
|
||||
extern char *getrumor(int, char *, boolean);
|
||||
extern char *get_rnd_text(const char *, char *, int(*)(int));
|
||||
extern char *get_rnd_text(const char *, char *, int(*)(int), unsigned);
|
||||
extern void outrumor(int, int);
|
||||
extern void outoracle(boolean, boolean);
|
||||
extern void save_oracles(NHFILE *);
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* Files expected to exist in the playground directory.
|
||||
* Files expected to exist in the playground directory (possibly inside
|
||||
* a dlb container file).
|
||||
*/
|
||||
|
||||
#define RECORD "record" /* file containing list of topscorers */
|
||||
@@ -32,6 +33,14 @@
|
||||
#define TRIBUTEFILE "tribute" /* 3.6 tribute to Terry Pratchett */
|
||||
#define LEV_EXT ".lua" /* extension for special level files */
|
||||
|
||||
/* padding amounts for files that have lines chosen by fseek to random spot,
|
||||
advancing to the next line, and using that line; makedefs forces shorter
|
||||
lines to be padded to these lengths; value of 0 will inhibit any padding,
|
||||
avoiding an increase in files' sizes, but resulting in biased selection;
|
||||
used by makedefs while building and by core's callers of get_rnd_text() */
|
||||
#define MD_PAD_RUMORS 60u /* for RUMORFILE, EPITAPHFILE, and ENGRAVEFILE */
|
||||
#define MD_PAD_BOGONS 20u /* for BOGUSMONFILE */
|
||||
|
||||
/* Assorted definitions that may depend on selections in config.h. */
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user