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>
|
2017-12-19 21:00:02 +01:00
|
|
|
#include "random.h"
|
2019-02-21 20:57:38 +01:00
|
|
|
#include "bh_random.h"
|
2018-12-17 13:10:11 +01:00
|
|
|
#include "util.h"
|
2017-12-17 13:43:41 +01:00
|
|
|
|
2018-12-14 08:21:16 +01:00
|
|
|
static unsigned int seed = 0;
|
2019-01-10 19:26:19 +01:00
|
|
|
static unsigned int map_seeds[20];
|
|
|
|
static unsigned int runtime_seed = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
generate_random_seeds(void)
|
|
|
|
{
|
|
|
|
// Use seed for generating map seeds
|
2019-02-21 20:57:38 +01:00
|
|
|
bh_srand(seed);
|
2019-02-24 19:42:35 +01:00
|
|
|
debug("Core random seed: %d", seed);
|
2019-01-10 19:26:19 +01:00
|
|
|
for (int i = 0; i < 20; ++i) {
|
2019-02-21 20:57:38 +01:00
|
|
|
map_seeds[i] = bh_rand();
|
2019-01-10 19:26:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a more random seed for runtime random
|
|
|
|
runtime_seed = (unsigned int) time(NULL);
|
2019-02-21 20:57:38 +01:00
|
|
|
bh_srand(runtime_seed);
|
2019-02-24 19:42:35 +01:00
|
|
|
debug("Runtime random seed: %d", runtime_seed);
|
2019-01-10 19:26:19 +01:00
|
|
|
}
|
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);
|
2019-01-10 19:26:19 +01:00
|
|
|
generate_random_seeds();
|
2017-12-18 09:11:00 +01:00
|
|
|
}
|
2018-12-14 08:21:16 +01:00
|
|
|
}
|
2017-12-18 09:11:00 +01:00
|
|
|
|
2018-12-17 13:10:11 +01:00
|
|
|
void
|
|
|
|
set_random_seed(unsigned int s)
|
|
|
|
{
|
|
|
|
seed = s;
|
2019-01-10 19:26:19 +01:00
|
|
|
generate_random_seeds();
|
2018-12-17 13:10:11 +01:00
|
|
|
}
|
|
|
|
|
2018-12-14 08:21:16 +01:00
|
|
|
unsigned int
|
2018-12-17 13:10:11 +01:00
|
|
|
get_random_seed(void)
|
2018-12-14 08:21:16 +01:00
|
|
|
{
|
|
|
|
init_seed();
|
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:26:19 +01:00
|
|
|
unsigned int
|
|
|
|
get_random_map_seed(unsigned int level)
|
|
|
|
{
|
|
|
|
return map_seeds[level-1];
|
|
|
|
}
|
|
|
|
|
2018-12-14 08:21:16 +01:00
|
|
|
unsigned int
|
|
|
|
get_random(unsigned int max)
|
|
|
|
{
|
|
|
|
init_seed();
|
2019-02-21 20:57:38 +01:00
|
|
|
return bh_rand() % (max + 1);
|
2018-05-06 06:19:59 +02:00
|
|
|
}
|