-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.cpp
214 lines (181 loc) · 5.63 KB
/
random.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
// Copyright 2010-2021 Chris Spiegel.
//
// This file is part of Bocfel.
//
// Bocfel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version
// 2 or 3, as published by the Free Software Foundation.
//
// Bocfel 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 Bocfel. If not, see <http://www.gnu.org/licenses/>.
#include <cerrno>
#include <climits>
#include <cstring>
#include <ctime>
#include <fstream>
#include "random.h"
#include "iff.h"
#include "io.h"
#include "process.h"
#include "stash.h"
#include "types.h"
#include "util.h"
#include "zterp.h"
// The random number generator can be in either a random or predictable
// state. The predictable state always uses a PRNG with a seed provided
// by the game. The random state uses either a PRNG or a random device
// file. If no device file is used, a seed comes either from the user or
// from the current time.
//
// In predictable mode, autosaves include the current state of the PRNG
// so autorestores can pick up right where they left off. In random
// mode, the state is not stored.
//
// In other saves (normal and meta), the state of the PRNG is never
// stored. Doing so portably would be impossible since different
// interpreters use different PRNGs, and intepreters may even be using
// non-deterministic RNGs, e.g. /dev/urandom on Linux. Even if a private
// chunk were used (such as that used for autosaves), Infocom never
// intended for seeds to be stored anyway: per the EZIP documentation:
// “The seed for RANDOM should not be saved or restored”.
//
// This implementation is actually at odds with Infocom’s EZIP
// documentation, which is at odds with the standard’s recommendation
// (or rather the other way around: the standard is at odds with
// Infocom). Infocom mandates that predictable mode generate a sequence
// of numbers from 1 to S, where S is the provided seed value. The
// standard recommends this but only for values of S less than 1000.
// Here a PRNG is always used, which is what ZVM also does on the
// recommendation of Andrew Plotkin:
// https://intfiction.org/t/z-machine-rng/7298
static enum class Mode {
Random,
Predictable,
} mode = Mode::Random;
// The PRNG used here is Xorshift32.
static uint32_t xstate;
static void zterp_srand(uint32_t s)
{
if (s == 0) {
s = 1;
}
xstate = s;
}
static std::ifstream random_file;
static uint32_t zterp_rand()
{
if (mode == Mode::Random && random_file.is_open()) {
uint32_t value;
if (random_file.read(reinterpret_cast<char *>(&value), sizeof value)) {
return value;
} else {
warning("error reading from %s: switching to PRNG", options.random_device->c_str());
random_file.close();
}
}
xstate ^= xstate << 13;
xstate ^= xstate >> 17;
xstate ^= xstate << 5;
return xstate;
}
// Called with 0, set the PRNG to random mode. Then seed it with either
// a) a user-provided seed (via -z) if available, or
// b) a seed derived from a hash of the constituent bytes of the value
// returned by time(nullptr)
//
// Otherwise, set the PRNG to predictable mode and seed with the
// provided value.
static void seed_random(uint32_t seed)
{
if (seed == 0) {
mode = Mode::Random;
if (options.random_seed == nullptr) {
std::time_t t = std::time(nullptr);
unsigned char *p = reinterpret_cast<unsigned char *>(&t);
uint32_t s = 0;
// time_t hashing based on code by Lawrence Kirby.
for (size_t i = 0; i < sizeof t; i++) {
s = s * (UCHAR_MAX + 2U) + p[i];
}
zterp_srand(s);
} else {
zterp_srand(*options.random_seed);
}
} else {
mode = Mode::Predictable;
zterp_srand(seed);
}
}
enum class RNGType {
XORShift = 0,
};
IFF::TypeID random_write_rand(IO &io)
{
if (mode == Mode::Random) {
return IFF::TypeID();
}
io.write16(static_cast<uint16_t>(RNGType::XORShift));
io.write32(xstate);
return IFF::TypeID(&"Rand");
}
void random_read_rand(IO &io)
{
uint16_t rng_type;
uint32_t state;
try {
rng_type = io.read16();
state = io.read32();
} catch (const IO::IOError &) {
return;
}
if (rng_type == static_cast<uint16_t>(RNGType::XORShift) && state != 0) {
xstate = state;
mode = Mode::Predictable;
}
}
static struct {
Mode mode;
uint32_t xstate;
} stash;
static void random_stash_backup()
{
stash.mode = mode;
stash.xstate = xstate;
}
static bool random_stash_restore()
{
mode = stash.mode;
xstate = stash.xstate;
return true;
}
static void random_stash_free()
{
}
void init_random(bool first_run)
{
seed_random(0);
if (first_run) {
if (options.random_device != nullptr && !random_file.is_open()) {
random_file.open(options.random_device->c_str(), std::ifstream::binary);
if (!random_file.is_open()) {
warning("unable to open random device %s: %s\n", options.random_device->c_str(), std::strerror(errno));
}
}
stash_register(random_stash_backup, random_stash_restore, random_stash_free);
}
}
void zrandom()
{
int16_t v = as_signed(zargs[0]);
if (v <= 0) {
seed_random(-v);
store(0);
} else {
store(zterp_rand() % zargs[0] + 1);
}
}