Attempt att syncing random generation

The default rand() and srand() differ between msvc and gcc.
Attempting to fix this by implementing a custom C++ library.
This commit is contained in:
Linus Probert 2019-02-21 20:57:38 +01:00
parent d1efa8450d
commit 1205856d00
8 changed files with 135 additions and 6 deletions

4
.vimrc
View File

@ -5,5 +5,5 @@ nnoremap <F4> :ter ++close env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./_build/debu
packadd termdebug packadd termdebug
let g:termdebug_wide = 1 let g:termdebug_wide = 1
let g:syntastic_c_include_dirs = [ '_build/debug', '/usr/include/SDL2', 'steamworks_c_wrapper/src', 'physfs-3.0/src' ] let g:syntastic_c_include_dirs = [ '_build/debug', '/usr/include/SDL2', 'steamworks_c_wrapper/src', 'physfs-3.0/src', 'bh_random/src' ]
let g:syntastic_cpp_include_dirs = [ 'steamworks_c_wrapper/sdk/public/steam' ] let g:syntastic_cpp_include_dirs = [ 'steamworks_c_wrapper/sdk/public/steam', 'bh_random/src' ]

View File

@ -39,6 +39,7 @@ endif()
if (STEAM) if (STEAM)
add_subdirectory(steamworks_c_wrapper) add_subdirectory(steamworks_c_wrapper)
endif() endif()
add_subdirectory(bh_random)
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
set(CLANG 1) set(CLANG 1)
@ -126,6 +127,7 @@ include_directories(
${PHYSFS_INCLUDE_DIR} ${PHYSFS_INCLUDE_DIR}
${LUA_INCLUDE_DIR} ${LUA_INCLUDE_DIR}
sqlite3 sqlite3
bh_random/src
) )
if (CMOCKA_FOUND) if (CMOCKA_FOUND)
@ -239,6 +241,7 @@ target_link_libraries(breakhack
${SDL2_MIXER_LIBRARY} ${SDL2_MIXER_LIBRARY}
${LUA_LIBRARIES} ${LUA_LIBRARIES}
${PHYSFS_LIBRARY} ${PHYSFS_LIBRARY}
bh_random
) )
if (STEAM) if (STEAM)

25
bh_random/CMakeLists.txt Normal file
View File

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.1)
project(bh_random)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()
add_library(bh_random STATIC
src/bh_random
)
IF ( MSVC )
MESSAGE ( STATUS "Setting MSVC MT switches")
SET (
CMAKE_CXX_FLAGS_DEBUG
"${CMAKE_CXX_FLAGS_DEBUG} /MTd"
CACHE STRING "MSVC MT flags " FORCE
)
SET (
CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} /MT"
CACHE STRING "MSVC MT flags " FORCE
)
endif ()

View File

@ -0,0 +1,39 @@
/*
* 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/>.
*/
#include <random>
#include <climits>
extern "C" {
#include "bh_random.h"
}
static std::mt19937 generator;
static std::uniform_int_distribution<int> distribution(0, INT_MAX);
extern "C" void
bh_srand(unsigned int seed)
{
generator.seed(seed);
}
extern "C" unsigned int
bh_rand(void)
{
return distribution(generator);
}

25
bh_random/src/bh_random.h Normal file
View File

@ -0,0 +1,25 @@
/*
* 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/>.
*/
#pragma once
void
bh_srand(unsigned int);
unsigned int
bh_rand(void);

View File

@ -20,6 +20,7 @@
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "random.h" #include "random.h"
#include "bh_random.h"
#include "util.h" #include "util.h"
static unsigned int seed = 0; static unsigned int seed = 0;
@ -30,15 +31,15 @@ static void
generate_random_seeds(void) generate_random_seeds(void)
{ {
// Use seed for generating map seeds // Use seed for generating map seeds
srand(seed); bh_srand(seed);
info("Core random seed: %d", seed); info("Core random seed: %d", seed);
for (int i = 0; i < 20; ++i) { for (int i = 0; i < 20; ++i) {
map_seeds[i] = rand(); map_seeds[i] = bh_rand();
} }
// Set a more random seed for runtime random // Set a more random seed for runtime random
runtime_seed = (unsigned int) time(NULL); runtime_seed = (unsigned int) time(NULL);
srand(runtime_seed); bh_srand(runtime_seed);
info("Runtime random seed: %d", runtime_seed); info("Runtime random seed: %d", runtime_seed);
} }
@ -75,5 +76,5 @@ unsigned int
get_random(unsigned int max) get_random(unsigned int max)
{ {
init_seed(); init_seed();
return rand() % (max + 1); return bh_rand() % (max + 1);
} }

View File

@ -1,3 +1,21 @@
/*
* 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/>.
*/
#include "time.h" #include "time.h"
#include "util.h" #include "util.h"

View File

@ -1,3 +1,21 @@
/*
* 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/>.
*/
#pragma once #pragma once
#include <time.h> #include <time.h>