Merge branch cxxrandom

Implements helper functions for random number generation.
Implemented using C++11 <random> library.

Exported Lua Functions:
- seedRNG(seed)
- rollInt(min, max)
- rollDouble(min, max)
- rollNormal(mean, std_deviation)
- rollBool(chance_for_true)
- resetIndexRolls(string, array_length)  --String identifies the instance of SimpleNumDistribution to reset
- rollIndex(string, array_length)        --String identifies the instance of SimpleNumDistribution to use
                                         --(Shuffles a vector of indices, Next() increments through then reshuffles when end() is reached)

 On branch cxxrandom-rel
 Changes to be committed:
   modified:   plugins/CMakeLists.txt
   new file:   plugins/cxxrandom.cpp
   new file:   plugins/lua/cxxrandom.lua

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Squashed commit of the following:

commit 3a7ef70d45f3e0c2fe367141dd0349dddaaff60d
Merge: fd9f1982 7aa0608c
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Fri Dec 22 22:17:27 2017 -0800

    Merge remote-tracking branch 'origin/temp' into cxxrandom

commit 7aa0608cb85dcf82686193db7a6e9d1318f5f2a5
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Thu Dec 21 21:43:35 2017 -0800

    Revises cxxrandom plugin

    New functions:
    - seedRNG(ushort seed)
        --lua exported
    - GetDistribContainer()
        --internal singleton
    - RNG()
        --internal singleton

    Summary:
    - Removed class CXXRNG
        --Refactored functions that used CXXRNG

     Changes to be committed:
            modified:   plugins/cxxrandom.cpp

commit b42979818a01c1121eace7b1ac14676f5ad5d8b2
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Wed Dec 20 13:21:49 2017 -0800

    Fixes plugin_init()

    Misread the lines indicated by lethosor to be excluded, had broken the plugin in the process.
     Changes to be committed:
    	modified:   plugins/cxxrandom.cpp

commit 753a00a14d9e6519d299638e014abf30509940af
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Wed Dec 20 12:36:17 2017 -0800

    Cleans up cxxrandom.cpp

    DFHack contributions specifies:
    -spaces instead of tabs, so tabs were converted.
    -C++ headers before dfhack stuff, so it was done

    *Also added author name, creation date, and last updated date.

     Changes to be committed:
    	modified:   plugins/cxxrandom.cpp

commit 498ebe4b8fdccc01ac1f169269f3093c830a8a10
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Tue Dec 19 22:51:58 2017 -0800

    Updates cxxrandom, fixes instance leak

    deleted header
    moved definition to cpp file #lethosor

    fixed singleton instance, no longer a pointer
    commented out dfhack commands, now only init/shutdown and exported lua functions

    	modified:   cxxrandom.cpp
    	deleted:    cxxrandom.h

commit 821044bef2a0201d0d74192e445c7b29766b42a1
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Sun Dec 17 04:01:11 2017 -0800

    Fixes RollIndex and Renames RollNormal

    Renamed the Normal Distribution RNG function to fit the standard.
    Now named RollNormal(m,s)

    Fixed some wonky white space in the lua macro export block.

    Fixed a stupid mistake with the RollIndex output. (it was outputting 0's)

    Updated usage details.

     Changes to be committed:
    	modified:   plugins/cxxrandom.cpp

commit 1536f43d137b6bc55d55759b43bdccf6ff429b33
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Fri Dec 15 08:50:08 2017 -0800

    Fixes/Improves cxxrandom

    Modified return types
    Corrected index distribution code

commit 8629c7e1509522cb0cc4b649914b90d033cb4763
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Thu Dec 14 19:02:29 2017 -0800

    Implements SimpleNumDistribution

    Exported additional functions to lua.
    Functions allow the generation of random 0-N index values.
    Generation promises all unique values [0,N] will be returned once each when generation is run N times.

     On branch cxxrandom
     Changes to be committed:
    	modified:   plugins/cxxrandom.cpp
    	modified:   plugins/cxxrandom.h

commit f035f3d20415790542cf83e5e696261661d911f3
Author: Josh Cooper <cooper.s.josh@gmail.com>
Date:   Wed Dec 13 23:55:39 2017 -0800

    Implements cxxrandom

    cxxrandom was implemented using a singleton.
    This singleton provides an interface for generating uniform numbers, or numbers in a normal distribution, and also booleans(given the probability for the true outcome)
    The singleton interface is wrapped in functions which are exposed for lua usage.

    Integrated into plugins/CMakeLists.txt

     On branch dev
     Changes to be committed:
    	modified:   CMakeLists.txt
    	new file:   cxxrandom.cpp
    	new file:   cxxrandom.h
    	new file:   lua/cxxrandom.lua
develop
Josh Cooper 2017-12-22 22:41:45 -08:00
parent b6311ec6b8
commit 23b2d5eba5
3 changed files with 194 additions and 0 deletions

@ -105,6 +105,7 @@ if (BUILD_SUPPORTED)
DFHACK_PLUGIN(confirm confirm.cpp LINK_LIBRARIES lua)
DFHACK_PLUGIN(createitem createitem.cpp)
DFHACK_PLUGIN(cursecheck cursecheck.cpp)
DFHACK_PLUGIN(cxxrandom cxxrandom.cpp LINK_LIBRARIES lua)
DFHACK_PLUGIN(deramp deramp.cpp)
DFHACK_PLUGIN(dig dig.cpp)
DFHACK_PLUGIN(digFlood digFlood.cpp)

@ -0,0 +1,190 @@
/* Plugin for exporting C++11 random number functionality
*Exports functions for random number generation
*Functions:
- seedRNG(seed)
- rollInt(min, max)
- rollDouble(min, max)
- rollNormal(mean, std_deviation)
- rollBool(chance_for_true)
- resetIndexRolls(string, array_length) --String identifies the instance of SimpleNumDistribution to reset
- rollIndex(string, array_length) --String identifies the instance of SimpleNumDistribution to use
--(Shuffles a vector of indices, Next() increments through then reshuffles when end() is reached)
Author: Josh Cooper
Created: Dec. 13 2017
Updated: Dec. 21 2017
*/
#include <random>
#include <chrono>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include "Core.h"
#include "DataFuncs.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
using namespace DFHack;
DFHACK_PLUGIN("cxxrandom");
#define PLUGIN_VERSION 1.0
//command_result cxxrandom (color_ostream &out, std::vector <std::string> & parameters);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
/*
commands.push_back(PluginCommand(
"cxxrandom", "C++xx Random Numbers", cxxrandom, false,
" This plugin cannot be used on the commandline.\n"
" Import into a lua script to have access to exported RNG functions:\n"
" local rng = require('plugins.cxxrandom')\n\n"
" Exported Functions:\n"
" rng.ResetIndexRolls(string_ref, total_indices)\n"
" rng.RollIndex(string_ref, total_indices)\n"
" rng.RollInt(min, max)\n"
" rng.RollDouble(min, max)\n"
" rng.RollNormal(mean, std_deviation)\n"
" rng.RollBool(chance_of_true)\n"
" rng.BlastDistributions()\n\n"
));*/
return CR_OK;
}
DFhackCExport command_result plugin_shutdown (color_ostream &out)
{
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
return CR_OK;
}
std::default_random_engine& RNG()
{
static std::default_random_engine instance(std::chrono::system_clock::now().time_since_epoch().count());
return instance;
}
void seedRNG(unsigned short seed)
{
RNG() = std::default_random_engine(seed);
}
class SimpleNumDistribution
{
private:
unsigned short m_position = 0;
std::vector<unsigned short> m_distribution;
public:
SimpleNumDistribution(unsigned short N)
{
m_position = 0;
m_distribution.reserve(N);
for(int i = 1; i <= N; ++i)
{
m_distribution.push_back(i);
}
Reset();
}
void Reset()
{
std::shuffle(std::begin(m_distribution), std::end(m_distribution), RNG());
}
unsigned short Length() const { return m_distribution.size(); }
unsigned short Next()
{
if(m_position >= m_distribution.size())
{
m_position = 0;
Reset();
}
return m_distribution[m_position++];
}
};
typedef std::unordered_map<std::string, SimpleNumDistribution> DistributionContainer;
DistributionContainer& GetDistribContainer()
{
static DistributionContainer instance;
return instance;
}
void resetIndexRolls(std::string ref, unsigned short N)
{
DistributionContainer& ND_index = GetDistribContainer();
auto iter = ND_index.find(ref);
if(iter == ND_index.end() || iter->second.Length() != N )
{
if(iter != ND_index.end())
ND_index.erase(iter);
iter = ND_index.emplace(ref, SimpleNumDistribution(N)).first;
}
iter->second.Reset();
}
int rollIndex(std::string ref, unsigned short N)
{
DistributionContainer& ND_index = GetDistribContainer();
auto iter = GetDistribContainer().find(ref);
if(iter == ND_index.end() || iter->second.Length() != N )
{
if(iter != ND_index.end())
ND_index.erase(iter);
iter = ND_index.emplace(ref, SimpleNumDistribution(N)).first;
}
return iter->second.Next();
}
int rollInt(int min, int max)
{
std::uniform_int_distribution<int> ND(min, max);
return ND(RNG());
}
double rollDouble(double min, double max)
{
std::uniform_real_distribution<double> ND(min, max);
return ND(RNG());
}
double rollNormal(double mean, double stddev)
{
std::normal_distribution<double> ND(mean, stddev);
return ND(RNG());
}
bool rollBool(float p)
{
std::bernoulli_distribution ND(p);
return ND(RNG());
}
DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(resetIndexRolls),
DFHACK_LUA_FUNCTION(rollIndex),
DFHACK_LUA_FUNCTION(seedRNG),
DFHACK_LUA_FUNCTION(rollInt),
DFHACK_LUA_FUNCTION(rollDouble),
DFHACK_LUA_FUNCTION(rollNormal),
DFHACK_LUA_FUNCTION(rollBool),
DFHACK_LUA_END
};

@ -0,0 +1,3 @@
local _ENV = mkmodule('plugins.cxxrandom')
return _ENV