*/
#include "map_object_actions.h" /* for player_wait(), move_player() */
#include "map.h" /* for struct Map, init_map() */
-#include "misc.h" /* for rrand(), update_log(), toggle_window(), exit_game(),
+#include "misc.h" /* for update_log(), toggle_window(), exit_game(),
* find_passable_pos(), meta_keys(), save_game()
*/
#include "yx_uint16.h" /* for dir enum */
+#include "rrand.h" /* for rrand(), rrand_seed() */
int main(int argc, char *argv[])
{
/* Generate map from seed and, if newly generated world, start positions of
* actors.
*/
- rrand(1, world.seed);
+ rrand_seed(world.seed);
struct Map map = init_map();
world.map = ↦
if (1 == world.turn)
{
player.pos = find_passable_pos(&map);
void * foo = build_map_objects(&world, &world.monster,
- 0, 1 + rrand(0,0) % 27,
+ 0, 1 + rrand() % 27,
sizeof(struct Monster),
build_map_objects_monsterdata);
- foo = build_map_objects(&world, foo, 1, 1 + rrand(0,0) % 9,
+ foo = build_map_objects(&world, foo, 1, 1 + rrand() % 9,
sizeof(struct Monster),
build_map_objects_monsterdata);
- build_map_objects(&world, foo, 2, 1 + rrand(0,0) % 3,
+ build_map_objects(&world, foo, 2, 1 + rrand() % 3,
sizeof(struct Monster),
build_map_objects_monsterdata);
- foo = build_map_objects(&world, &world.item, 3, 1 + rrand(0,0) % 3,
+ foo = build_map_objects(&world, &world.item, 3, 1 + rrand() % 3,
sizeof(struct Item),
build_map_objects_itemdata);
- build_map_objects(&world, foo, 4, 1 + rrand(0,0) % 3,
+ build_map_objects(&world, foo, 4, 1 + rrand() % 3,
sizeof(struct Item), build_map_objects_itemdata);
}
#include "map.h"
#include <stdlib.h> /* for malloc() */
#include <stdint.h> /* for uint16_t, uint32_t */
-#include "misc.h" /* for rrand() and center_offset() */
+#include "misc.h" /* for center_offset() */
#include "map_objects.h" /* for Player struct */
#include "yx_uint16.h" /* for yx_uint16 and dir enums */
+#include "rrand.h" /* for rrand() */
uint32_t curpos;
while (1)
{
- y = rrand(0, 0) % map.size.y;
- x = rrand(0, 0) % map.size.x;
+ y = rrand() % map.size.y;
+ x = rrand() % map.size.x;
curpos = y * map.size.x + x;
if ('~' == map.cells[curpos]
&& ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
#include "map_object_actions.h"
#include <stdlib.h> /* for malloc(), calloc(), free() */
#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
-#include "misc.h" /* for rrand(), update_log(), turn_over()*/
+#include "misc.h" /* for update_log(), turn_over()*/
#include "map.h" /* for Map struct */
#include "main.h" /* for World struct */
#include "map_objects.h" /* for map object (definition) structs */
+#include "rrand.h" /* for rrand() */
extern void move_monster(struct World * world, struct Monster * monster)
{
- char d = rrand(0, 0) % 5;
+ char d = rrand() % 5;
struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
char * msg = malloc(100);
struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
#include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */
#include "main.h" /* for World struct */
#include "yx_uint16.h" /* for yx_uint16 */
+#include "rrand.h" /* for rrand(), rrand_seed() */
-extern uint16_t rrand(char use_seed, uint32_t new_seed)
-{
- static uint32_t seed;
- if (0 != use_seed)
- {
- seed = new_seed;
- }
-
- /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
- seed = ((seed * 1103515245) + 12345) % 2147483648;
-
- return (seed >> 16); /* Ignore less random least significant 16 bits. */
-}
-
-
extern void update_log(struct World * world, char * text)
{
static char * last_msg;
fclose(file);
}
world->turn++;
- rrand(1, world->seed * world->turn);
+ rrand_seed(world->seed * world->turn);
struct Monster * monster;
for (monster = world->monster;
monster != 0;
struct yx_uint16 pos;
for (pos.y = pos.x = 0; 0 == is_passable(map, pos);)
{
- pos.y = rrand(0, 0) % map->size.y;
- pos.x = rrand(0, 0) % map->size.x;
+ pos.y = rrand() % map->size.y;
+ pos.x = rrand() % map->size.x;
}
return pos;
}
-/* Pseudo-random number generator using a Linear Congruential Generator
- * algorithm with some proven constants. Used instead of rand() to ensure
- * portable pseudo-randomness predictability. Set "use_seed" to !0 to seed it
- * with "new_seed".
- *
- * TODO: Write a wrapper for all non-seeding uses that demands no input.
- */
-extern uint16_t rrand(char use_seed, uint32_t new_seed);
-
-
-
/* Update game log by appending "text", or by appending a "." if "text" is the
* same as the last one passed.
*/
--- /dev/null
+#include "rrand.h"
+#include <stdint.h> /* for uint16_t, uint32_t */
+
+
+
+static uint32_t seed = 0;
+
+
+
+extern uint16_t rrand()
+{
+ /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */
+ seed = ((seed * 1103515245) + 12345) % 2147483648;
+
+ return (seed >> 16); /* Ignore less random least significant 16 bits. */
+}
+
+
+
+extern void rrand_seed(uint32_t new_seed)
+{
+ seed = new_seed;
+}
--- /dev/null
+/* rrand.h
+ *
+ * Provide pseudo-random numbers via a Linear Congruential Generator algorithm
+ * with some proven constants. Use these functions instead of rand() and
+ * srand() to ensure portable pseudo-randomness portability.
+ */
+
+
+
+#ifndef RRAND_H
+#define RRAND_H
+
+#include <stdint.h> /* for uint32_t */
+
+
+
+/* Return 16-bit number pseudo-randomly generated. */
+extern uint16_t rrand();
+
+
+
+/* Set seed that rrand() starts from. */
+extern void rrand_seed(uint32_t new_seed);
+
+
+
+#endif