From 1205856d00291579b94fa267bdf8418104787ed3 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Thu, 21 Feb 2019 20:57:38 +0100 Subject: [PATCH] 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. --- .vimrc | 4 ++-- CMakeLists.txt | 3 +++ bh_random/CMakeLists.txt | 25 ++++++++++++++++++++++++ bh_random/src/bh_random.cpp | 39 +++++++++++++++++++++++++++++++++++++ bh_random/src/bh_random.h | 25 ++++++++++++++++++++++++ src/random.c | 9 +++++---- src/time.c | 18 +++++++++++++++++ src/time.h | 18 +++++++++++++++++ 8 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 bh_random/CMakeLists.txt create mode 100644 bh_random/src/bh_random.cpp create mode 100644 bh_random/src/bh_random.h diff --git a/.vimrc b/.vimrc index f769d1a..e3e881a 100644 --- a/.vimrc +++ b/.vimrc @@ -5,5 +5,5 @@ nnoremap :ter ++close env LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ ./_build/debu packadd termdebug 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_cpp_include_dirs = [ 'steamworks_c_wrapper/sdk/public/steam' ] +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', 'bh_random/src' ] diff --git a/CMakeLists.txt b/CMakeLists.txt index bbe3895..c40796f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ endif() if (STEAM) add_subdirectory(steamworks_c_wrapper) endif() +add_subdirectory(bh_random) if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set(CLANG 1) @@ -126,6 +127,7 @@ include_directories( ${PHYSFS_INCLUDE_DIR} ${LUA_INCLUDE_DIR} sqlite3 + bh_random/src ) if (CMOCKA_FOUND) @@ -239,6 +241,7 @@ target_link_libraries(breakhack ${SDL2_MIXER_LIBRARY} ${LUA_LIBRARIES} ${PHYSFS_LIBRARY} + bh_random ) if (STEAM) diff --git a/bh_random/CMakeLists.txt b/bh_random/CMakeLists.txt new file mode 100644 index 0000000..2374c82 --- /dev/null +++ b/bh_random/CMakeLists.txt @@ -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 () + diff --git a/bh_random/src/bh_random.cpp b/bh_random/src/bh_random.cpp new file mode 100644 index 0000000..88742ec --- /dev/null +++ b/bh_random/src/bh_random.cpp @@ -0,0 +1,39 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * 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 . + */ + +#include +#include +extern "C" { +#include "bh_random.h" +} + +static std::mt19937 generator; +static std::uniform_int_distribution 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); +} diff --git a/bh_random/src/bh_random.h b/bh_random/src/bh_random.h new file mode 100644 index 0000000..d967631 --- /dev/null +++ b/bh_random/src/bh_random.h @@ -0,0 +1,25 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * 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 . + */ + +#pragma once + +void +bh_srand(unsigned int); + +unsigned int +bh_rand(void); diff --git a/src/random.c b/src/random.c index bf7964e..5da1f91 100644 --- a/src/random.c +++ b/src/random.c @@ -20,6 +20,7 @@ #include #include #include "random.h" +#include "bh_random.h" #include "util.h" static unsigned int seed = 0; @@ -30,15 +31,15 @@ static void generate_random_seeds(void) { // Use seed for generating map seeds - srand(seed); + bh_srand(seed); info("Core random seed: %d", seed); for (int i = 0; i < 20; ++i) { - map_seeds[i] = rand(); + map_seeds[i] = bh_rand(); } // Set a more random seed for runtime random runtime_seed = (unsigned int) time(NULL); - srand(runtime_seed); + bh_srand(runtime_seed); info("Runtime random seed: %d", runtime_seed); } @@ -75,5 +76,5 @@ unsigned int get_random(unsigned int max) { init_seed(); - return rand() % (max + 1); + return bh_rand() % (max + 1); } diff --git a/src/time.c b/src/time.c index a58fbf8..9ceefbd 100644 --- a/src/time.c +++ b/src/time.c @@ -1,3 +1,21 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * 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 . + */ + #include "time.h" #include "util.h" diff --git a/src/time.h b/src/time.h index b8e32dc..a67614f 100644 --- a/src/time.h +++ b/src/time.h @@ -1,3 +1,21 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * 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 . + */ + #pragma once #include