breakhack/src/random.c

82 lines
1.8 KiB
C
Raw Permalink Normal View History

2018-02-16 18:11:26 +01:00
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-12-17 13:43:41 +01:00
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include "random.h"
#include "bh_random.h"
#include "util.h"
2017-12-17 13:43:41 +01:00
2018-12-14 08:21:16 +01:00
static unsigned int seed = 0;
static unsigned int map_seeds[20];
static unsigned int runtime_seed = 0;
static void
generate_random_seeds(void)
{
// Use seed for generating map seeds
bh_srand(seed);
debug("Core random seed: %d", seed);
for (int i = 0; i < 20; ++i) {
map_seeds[i] = bh_rand();
}
// Set a more random seed for runtime random
runtime_seed = (unsigned int) time(NULL);
bh_srand(runtime_seed);
debug("Runtime random seed: %d", runtime_seed);
}
2018-12-14 08:21:16 +01:00
static void
init_seed(void)
2017-12-17 13:43:41 +01:00
{
2018-12-14 08:21:16 +01:00
if (seed == 0) {
seed = (unsigned int) time(NULL);
generate_random_seeds();
}
2018-12-14 08:21:16 +01:00
}
void
set_random_seed(unsigned int s)
{
seed = s;
generate_random_seeds();
}
2018-12-14 08:21:16 +01:00
unsigned int
get_random_seed(void)
2018-12-14 08:21:16 +01:00
{
init_seed();
return seed;
}
unsigned int
get_random_map_seed(unsigned int level)
{
init_seed();
return map_seeds[level-1];
}
2018-12-14 08:21:16 +01:00
unsigned int
get_random(unsigned int max)
{
init_seed();
return bh_rand() % (max + 1);
2018-05-06 06:19:59 +02:00
}