forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxxrandom.cpp
311 lines (276 loc) · 8.09 KB
/
cxxrandom.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* 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 <cstdint>
#include <stdio.h>
#include <stdlib.h>
#include "Error.h"
#include "Core.h"
#include "DataFuncs.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
/*
typedef unsigned short uint16_t;
typedef unsigned long long uint64_t;
typedef long long int64_t;
using uint16_t = unsigned short;
using uint64_t = unsigned long long;
using int64_t = long long;*/
using namespace DFHack;
DFHACK_PLUGIN("cxxrandom");
#define PLUGIN_VERSION 2.0
color_ostream *cout = nullptr;
//command_result cxxrandom (color_ostream &out, std::vector <std::string> & parameters);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
cout = &out;
/*
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;
}
#pragma region "EnginesKeeper Stuff"
class EnginesKeeper
{
private:
EnginesKeeper() {}
std::unordered_map<uint16_t, std::mt19937_64> m_engines;
uint16_t counter = 0;
public:
static EnginesKeeper& Instance()
{
static EnginesKeeper instance;
return instance;
}
uint16_t NewEngine( uint64_t seed )
{
std::mt19937_64 engine( seed != 0 ? seed : std::chrono::system_clock::now().time_since_epoch().count() );
m_engines[++counter] = engine;
return counter;
}
void DestroyEngine( uint16_t id )
{
m_engines.erase( id );
}
void NewSeed( uint16_t id, uint64_t seed )
{
CHECK_INVALID_ARGUMENT( m_engines.find( id ) != m_engines.end() );
m_engines[id].seed( seed != 0 ? seed : std::chrono::system_clock::now().time_since_epoch().count() );
}
std::mt19937_64& RNG( uint16_t id )
{
CHECK_INVALID_ARGUMENT( m_engines.find( id ) != m_engines.end() );
return m_engines[id];
}
};
#pragma endregion
#pragma region "EngineKeeper Wrappers"
uint16_t GenerateEngine( uint64_t seed )
{
return EnginesKeeper::Instance().NewEngine( seed );
}
void DestroyEngine( uint16_t id )
{
EnginesKeeper::Instance().DestroyEngine( id );
}
void NewSeed( uint16_t id, uint64_t seed )
{
EnginesKeeper::Instance().NewSeed( id, seed );
}
#pragma endregion
#pragma region "std Distribution Rollers"
int rollInt(uint16_t id, int min, int max)
{
std::uniform_int_distribution<int> ND(min, max);
return ND(EnginesKeeper::Instance().RNG(id));
}
double rollDouble(uint16_t id, double min, double max)
{
std::uniform_real_distribution<double> ND(min, max);
return ND(EnginesKeeper::Instance().RNG(id));
}
double rollNormal(uint16_t id, double mean, double stddev)
{
std::normal_distribution<double> ND(mean, stddev);
return ND(EnginesKeeper::Instance().RNG(id));
}
bool rollBool(uint16_t id, float p)
{
std::bernoulli_distribution ND(p);
return ND(EnginesKeeper::Instance().RNG(id));
}
#pragma endregion
#pragma region "Number Sequence Stuff"
class NumberSequence
{
private:
unsigned short m_position = 0;
std::vector<int64_t> m_numbers;
public:
NumberSequence(){}
NumberSequence( int64_t start, int64_t end )
{
for( int64_t i = start; i <= end; ++i )
{
m_numbers.push_back( i );
}
}
void Add( int64_t num ) { m_numbers.push_back( num ); }
void Reset() { m_numbers.clear(); }
int64_t Next()
{
if(m_position >= m_numbers.size())
{
m_position = 0;
}
return m_numbers[m_position++];
}
void Shuffle( uint16_t id )
{
std::shuffle( std::begin( m_numbers ), std::end( m_numbers ), EnginesKeeper::Instance().RNG( id ) );
}
void Print()
{
char buffer1[256] = {0};
char buffer2[256] = {0};
for( auto v : m_numbers )
{
sprintf( buffer2, "%s%d", buffer1, v );
sprintf( buffer1, "%s ", buffer2 );
}
cout->print( buffer1 );
}
};
class SequenceKeeper
{
private:
SequenceKeeper() {}
std::unordered_map<uint16_t, NumberSequence> m_sequences;
uint16_t counter = 0;
public:
static SequenceKeeper& Instance()
{
static SequenceKeeper instance;
return instance;
}
uint16_t MakeNumSequence( int64_t start, int64_t end )
{
m_sequences[++counter] = NumberSequence( start, end );
return counter;
}
uint16_t MakeNumSequence()
{
m_sequences[++counter] = NumberSequence();
return counter;
}
void DestroySequence( uint16_t id )
{
m_sequences.erase( id );
}
void AddToSequence( uint16_t id, int64_t num )
{
CHECK_INVALID_ARGUMENT( m_sequences.find( id ) != m_sequences.end() );
m_sequences[id].Add( num );
}
void Shuffle( uint16_t id, uint16_t rng_id )
{
CHECK_INVALID_ARGUMENT( m_sequences.find( id ) != m_sequences.end() );
m_sequences[id].Shuffle( rng_id );
}
int64_t NextInSequence( uint16_t id )
{
CHECK_INVALID_ARGUMENT( m_sequences.find( id ) != m_sequences.end() );
return m_sequences[id].Next();
}
void PrintSequence( uint16_t id )
{
CHECK_INVALID_ARGUMENT( m_sequences.find( id ) != m_sequences.end() );
auto seq = m_sequences[id];
seq.Print();
}
};
#pragma endregion
#pragma region "Sequence Wrappers"
uint16_t MakeNumSequence( int64_t start, int64_t end )
{
if( start == end )
{
return SequenceKeeper::Instance().MakeNumSequence();
}
return SequenceKeeper::Instance().MakeNumSequence( start, end );
}
void DestroyNumSequence( uint16_t id )
{
SequenceKeeper::Instance().DestroySequence( id );
}
void AddToSequence( uint16_t id, int64_t num )
{
SequenceKeeper::Instance().AddToSequence( id, num );
}
void ShuffleSequence( uint16_t rngID, uint16_t id )
{
SequenceKeeper::Instance().Shuffle( id, rngID );
}
int64_t NextInSequence( uint16_t id )
{
return SequenceKeeper::Instance().NextInSequence( id );
}
void DebugSequence( uint16_t id )
{
SequenceKeeper::Instance().PrintSequence( id );
}
#pragma endregion
DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(GenerateEngine),
DFHACK_LUA_FUNCTION(DestroyEngine),
DFHACK_LUA_FUNCTION(NewSeed),
DFHACK_LUA_FUNCTION(rollInt),
DFHACK_LUA_FUNCTION(rollDouble),
DFHACK_LUA_FUNCTION(rollNormal),
DFHACK_LUA_FUNCTION(rollBool),
DFHACK_LUA_FUNCTION(MakeNumSequence),
DFHACK_LUA_FUNCTION(DestroyNumSequence),
DFHACK_LUA_FUNCTION(AddToSequence),
DFHACK_LUA_FUNCTION(ShuffleSequence),
DFHACK_LUA_FUNCTION(NextInSequence),
DFHACK_LUA_FUNCTION(DebugSequence),
DFHACK_LUA_END
};