From c97425f1f4845d3c27ca7e5a6026c2dbd0ab2dec Mon Sep 17 00:00:00 2001 From: DTB Date: Sat, 25 Nov 2023 10:44:23 -0700 Subject: [PATCH] remove random.c --- rogue/hit.c | 1 + rogue/init.c | 5 +- rogue/inventory.c | 1 + rogue/level.c | 1 + rogue/machdep.c | 27 --------- rogue/machdep.h | 1 - rogue/monster.c | 1 + rogue/move.c | 1 + rogue/object.c | 1 + rogue/random.c | 137 ---------------------------------------------- rogue/random.h | 3 + rogue/ring.c | 1 + rogue/rogue.h | 4 -- rogue/room.c | 1 + rogue/spec_hit.c | 1 + rogue/throw.c | 1 + rogue/trap.c | 1 + rogue/use.c | 1 + rogue/zap.c | 1 + 19 files changed, 17 insertions(+), 173 deletions(-) delete mode 100644 rogue/random.c create mode 100644 rogue/random.h diff --git a/rogue/hit.c b/rogue/hit.c index 08f09ff..6b32d2d 100644 --- a/rogue/hit.c +++ b/rogue/hit.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" static int damage_for_strength(void); diff --git a/rogue/init.c b/rogue/init.c index 01653e2..648bca3 100644 --- a/rogue/init.c +++ b/rogue/init.c @@ -50,6 +50,7 @@ #include /* */ #include /* getpwuid(3) */ +#include "random.h" #include "rogue.h" static void do_args(int, char **); @@ -77,7 +78,6 @@ gid_t gid, egid; int init(int argc, char *argv[]) { - int seed; int fd; gid = getgid(); @@ -89,7 +89,6 @@ init(int argc, char *argv[]) exit(1); close(fd); - seed = 0; { char *ps; struct passwd *p; @@ -130,8 +129,6 @@ init(int argc, char *argv[]) if (score_only) { put_scores(NULL, 0); } - seed = md_gseed(); - (void)srrandom(seed); if (rest_file) { restore(rest_file); return(1); diff --git a/rogue/inventory.c b/rogue/inventory.c index d4c2e8c..9d1dcfb 100644 --- a/rogue/inventory.c +++ b/rogue/inventory.c @@ -45,6 +45,7 @@ */ #include +#include "random.h" #include "rogue.h" boolean is_wood[WANDS]; diff --git a/rogue/level.c b/rogue/level.c index 674af56..63af842 100644 --- a/rogue/level.c +++ b/rogue/level.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" #define SWAP(x,y) (t = (x), (x) = (y), (y) = t) diff --git a/rogue/machdep.c b/rogue/machdep.c index 1790095..936a984 100644 --- a/rogue/machdep.c +++ b/rogue/machdep.c @@ -309,33 +309,6 @@ md_malloc(size_t n) return(t); } -/* md_gseed() (Get Seed) - * - * This function returns a seed for the random number generator (RNG). This - * seed causes the RNG to begin generating numbers at some point in its - * sequence. Without a random seed, the RNG will generate the same set - * of numbers, and every game will start out exactly the same way. A good - * number to use is the process id, given by getpid() on most UNIX systems. - * - * You need to find some single random integer, such as: - * process id. - * current time (minutes + seconds) returned from md_gct(), if implemented. - * - * It will not help to return "get_rand()" or "rand()" or the return value of - * any pseudo-RNG. If you don't have a random number, you can just return 1, - * but this means your games will ALWAYS start the same way, and will play - * exactly the same way given the same input. - */ - -int -md_gseed(void) -{ - time_t seconds; - - time(&seconds); - return((int)seconds); -} - /* md_exit(): * * This function causes the program to discontinue execution and exit. diff --git a/rogue/machdep.h b/rogue/machdep.h index e65aa8f..a15e3c7 100644 --- a/rogue/machdep.h +++ b/rogue/machdep.h @@ -12,7 +12,6 @@ boolean md_df(const char *fname); void md_sleep(int nsecs); char *md_getenv(const char *name); void *md_malloc(size_t n); -int md_gseed(void); void md_exit(int status); void md_lock(boolean l); void md_shell(const char *shell); diff --git a/rogue/monster.c b/rogue/monster.c index a911629..8499ae7 100644 --- a/rogue/monster.c +++ b/rogue/monster.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" object level_monsters; diff --git a/rogue/move.c b/rogue/move.c index b1a6976..c04bee8 100644 --- a/rogue/move.c +++ b/rogue/move.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" short m_moves = 0; diff --git a/rogue/object.c b/rogue/object.c index 27397df..85fedb6 100644 --- a/rogue/object.c +++ b/rogue/object.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" object level_objects; diff --git a/rogue/random.c b/rogue/random.c deleted file mode 100644 index f3bdc8c..0000000 --- a/rogue/random.c +++ /dev/null @@ -1,137 +0,0 @@ -/* $NetBSD: random.c,v 1.8 2009/08/12 08:44:45 dholland Exp $ */ - -/* - * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Timothy C. Stoehr. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include "rogue.h" - -/* - * random.c - * - * This source herein may be modified and/or distributed by anybody who - * so desires, with the following restrictions: - * 1.) No portion of this notice shall be removed. - * 2.) Credit shall not be taken for the creation of this source. - * 3.) This code is not to be traded, sold, or used for personal - * gain or profit. - * - */ - -static long rntb[32] = { - 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, - 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b, - 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, - 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, - 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, - 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, - 0x8999220b, 0x27fb47b9 -}; - -static long *fptr = &rntb[4]; -static long *rptr = &rntb[1]; -static long *state = &rntb[1]; -static int rand_type = 3; -static int rand_deg = 31; -static int rand_sep = 3; -static long *end_ptr = &rntb[32]; - -static long rrandom(void); - -void -srrandom(int x) -{ - int i; - - state[0] = x; - if (rand_type != 0) { - for (i = 1; i < rand_deg; i++) { - state[i] = 1103515245 * state[i - 1] + 12345; - } - fptr = &state[rand_sep]; - rptr = &state[0]; - for (i = 0; i < 10 * rand_deg; i++) { - (void)rrandom(); - } - } -} - -static long -rrandom(void) -{ - long i; - - if (rand_type == 0) { - i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff; - } else { - *fptr += *rptr; - i = (*fptr >> 1) & 0x7fffffff; - if (++fptr >= end_ptr) { - fptr = state; - ++rptr; - } else { - if (++rptr >= end_ptr) { - rptr = state; - } - } - } - return(i); -} - -int -get_rand(int x, int y) -{ - int r, t; - long lr; - - if (x > y) { - t = y; - y = x; - x = t; - } - lr = rrandom(); - lr &= 0x00003fffL; - r = (int)lr; - r = (r % ((y - x) + 1)) + x; - return(r); -} - -int -rand_percent(int percentage) -{ - return(get_rand(1, 100) <= percentage); -} - -int -coin_toss(void) -{ - return(((rrandom() & 01) ? 1 : 0)); -} diff --git a/rogue/random.h b/rogue/random.h new file mode 100644 index 0000000..6fe22b7 --- /dev/null +++ b/rogue/random.h @@ -0,0 +1,3 @@ +#define get_rand(x, y) (((rand() % ((y) - (x))) + 1) + (x)) /* 0 index */ +#define rand_percent(odds) ((rand() % 99 + 1) <= (odds)) +#define coin_toss (rand() & 1) diff --git a/rogue/ring.c b/rogue/ring.c index ae78900..4a3f4ac 100644 --- a/rogue/ring.c +++ b/rogue/ring.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" static const char left_or_right[] = "left or right hand?"; diff --git a/rogue/rogue.h b/rogue/rogue.h index 2b52383..facb3cb 100644 --- a/rogue/rogue.h +++ b/rogue/rogue.h @@ -503,7 +503,6 @@ int get_hit_chance(const object *); int get_input_line(const char *, const char *, char *, size_t, const char *, boolean, boolean); char get_mask_char(unsigned short); int get_number(const char *); -int get_rand(int, int); short get_room_number(int, int); void get_wand_and_ring_materials(void); int get_weapon_damage(const object *); @@ -543,7 +542,6 @@ void md_exit(int) __dead; void md_gct(struct rogue_time *); int md_get_file_id(const char *); void md_gfmt(const char *, struct rogue_time *); -int md_gseed(void); void md_heed_signals(void); void md_ignore_signals(void); int md_link_count(const char *); @@ -587,7 +585,6 @@ void quaff(void); void quit(boolean); int r_index(const char *, int, boolean); void rand_around(short, short *, short *); -int rand_percent(int); void read_scroll(void); boolean reg_move(void); void relight(void); @@ -613,7 +610,6 @@ void show_traps(void); void single_inv(short); void sound_bell(void); void special_hit(object *); -void srrandom(int); void start_window(void); void stop_window(void); void take_a_nap(void); diff --git a/rogue/room.c b/rogue/room.c index d5adbe3..a076f04 100644 --- a/rogue/room.c +++ b/rogue/room.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" room rooms[MAXROOMS]; diff --git a/rogue/spec_hit.c b/rogue/spec_hit.c index e656875..bb99af9 100644 --- a/rogue/spec_hit.c +++ b/rogue/spec_hit.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" static void disappear(object *); diff --git a/rogue/throw.c b/rogue/throw.c index 81d3a96..ee43766 100644 --- a/rogue/throw.c +++ b/rogue/throw.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" static void flop_weapon(object *, short, short); diff --git a/rogue/trap.c b/rogue/trap.c index 5b31198..77faf33 100644 --- a/rogue/trap.c +++ b/rogue/trap.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" trap traps[MAX_TRAPS]; diff --git a/rogue/use.c b/rogue/use.c index 50ecc33..52966fa 100644 --- a/rogue/use.c +++ b/rogue/use.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" short halluc = 0; diff --git a/rogue/zap.c b/rogue/zap.c index cdd7911..4b7b62d 100644 --- a/rogue/zap.c +++ b/rogue/zap.c @@ -44,6 +44,7 @@ * */ +#include "random.h" #include "rogue.h" static object *get_zapped_monster(short, short *, short *);