X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=misc.c;h=f1bd954c7932cdf9c463c072c2f1a970b166e340;hb=cf219e920bb038c9ac2c879adcfd048dc13bf3bc;hp=63052ced47351603e80fccdd31da28c47ca90cca;hpb=dc9d9e467b4efe76c30f15ccd23ceede2326f5e8;p=open-adventure.git diff --git a/misc.c b/misc.c index 63052ce..f1bd954 100644 --- a/misc.c +++ b/misc.c @@ -2,6 +2,7 @@ #include #include #include "main.h" +#include "share.h" /* for SETUP */ #include "misc.h" #include "funcs.h" @@ -722,7 +723,7 @@ L2: ATDWRF=I; -/* Utility routines (SETBIT, TSTBIT, RAN, RNDVOC, BUG) */ +/* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value, randrange, RNDVOC, BUG) */ #undef SETBIT long fSETBIT(long BIT) { @@ -756,31 +757,28 @@ long TSTBIT; #define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT) -#undef RAN -long fRAN(long RANGE) { -static long D, R = 0, RAN, T; - -/* Since the ran function in LIB40 seems to be a real lose, we'll use one of - * our own. It's been run through many of the tests in Knuth vol. 2 and - * seems to be quite reliable. RAN returns a value uniformly selected - * between 0 and range-1. */ - - - D=1; - if(R != 0 && RANGE >= 0) goto L1; - DATIME(D,T); - R=MOD(T+5,1048576L); - D=1000+MOD(D,1000); -L1: for (T=1; T<=D; T++) { - R=MOD(R*1093L+221587L,1048576L); - } /* end loop */ - RAN=(RANGE*R)/1048576; - return(RAN); + +void set_seed(long seedval) +{ + srand(seedval); + lcgstate.x = (unsigned long) rand() % lcgstate.m; } +unsigned long get_next_lcg_value(void) +{ + /* Return the LCG's current value, and then iterate it. */ + unsigned long old_x = lcgstate.x; + lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m; + return(old_x); +} +long randrange(long range) +{ + /* Return a random integer from [0, range). */ + long result = range * get_next_lcg_value() / lcgstate.m; + return(result); +} -#define RAN(RANGE) fRAN(RANGE) #undef RNDVOC long fRNDVOC(long CHAR, long FORCE) { long DIV, I, J, RNDVOC; @@ -794,7 +792,7 @@ long DIV, I, J, RNDVOC; RNDVOC=FORCE; if(RNDVOC != 0) goto L3; for (I=1; I<=5; I++) { - J=11+RAN(26); + J=11+randrange(26); if(I == 2)J=CHAR; RNDVOC=RNDVOC*64+J; } /* end loop */ @@ -856,8 +854,8 @@ void fBUG(long NUM) { #define BUG(NUM) fBUG(NUM) #undef MAPLIN void fMAPLIN(FILE *OPENED) { -long I, VAL; - + signed char *cp; + /* Read a line of input, from the specified input source, * translate the chars to integers in the range 0-126 and store * them in the common array "INLINE". Integer values are as follows: @@ -885,21 +883,20 @@ long I, VAL; if(MAP2[1] == 0)MPINIT(); - if (!oldstyle && isatty(0)) + if (!oldstyle && SETUP) fputs("> ", stdout); - IGNORE(fgets(INLINE+1,sizeof(INLINE)-1,OPENED)); + IGNORE(fgets(raw_input,sizeof(INLINE)-1,OPENED)); if (feof(OPENED)) { if (logfp) fclose(logfp); } else { if (logfp) - IGNORE(fputs(INLINE+1, logfp)); - LNLENG=0; - for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) { - VAL=INLINE[I]+1; - INLINE[I]=MAP1[VAL]; - if(INLINE[I] != 0)LNLENG=I; - } /* end loop */ + IGNORE(fputs(raw_input, logfp)); + else if (!isatty(0)) + IGNORE(fputs(raw_input, stdout)); + for (cp = raw_input; *cp; cp++) + INLINE[cp - raw_input + 1]=MAP1[*cp + 1]; + LNLENG = (cp - raw_input); LNPOSN=1; } }