Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
legacy_random.h
Go to the documentation of this file.
1/*
2
3legacy_random.h
4
5Pseudo-random number generator designed to produce identical results to that
6used in BBC Elite (for dynamic world generation), and related functions.
7
8
9Oolite
10Copyright (C) 2004-2013 Giles C Williams and contributors
11
12This program is free software; you can redistribute it and/or
13modify it under the terms of the GNU General Public License
14as published by the Free Software Foundation; either version 2
15of the License, or (at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program; if not, write to the Free Software
24Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25MA 02110-1301, USA.
26
27*/
28
29#ifndef LEGACY_RANDOM_H
30#define LEGACY_RANDOM_H
31
33#include <math.h>
34#include <stdint.h>
35
36
37typedef struct Random_Seed
38{
39 uint8_t a, /* 6c */
40 b, /* 6d */
41 c, /* 6e */
42 d, /* 6f */
43 e, /* 70 */
44 f; /* 71 */
46
47
48typedef struct RNG_Seed
49{
50 int32_t a,
55
56
57typedef struct RANROTSeed
58{
59 uint32_t high,
62
63
64extern const Random_Seed kNilRandomSeed;
65
66
67// checksum stuff
68void clear_checksum(void);
69int16_t munge_checksum(long long value);
70
71// cunning price rounding routine:
72double cunningFee(double value, double precision); // precision is the fraction below which numbers become insignificant.
73
74// an implementation of RANROT
75// pseudo random number generator
76void ranrot_srand(uint32_t seed);
77unsigned Ranrot(void);
78#define ranrot_rand() ((int)Ranrot()) // Some uses perform arithmetic that does weird things if result is unsigned -- DustEntity.m, for instance.
79float randf(void);
80float bellf(int n);
81
84
85RANROTSeed MakeRanrotSeed(uint32_t seed);
88
89unsigned RanrotWithSeed(RANROTSeed *ioSeed);
90float randfWithSeed(RANROTSeed *ioSeed);
91
92
93OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
94OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
95
99void setRandomSeed(RNG_Seed a_seed);
100
101// Range: 0..255
102int gen_rnd_number (void);
103
104void make_pseudo_random_seed (Random_Seed *seed_ptr);
105
107
108void rotate_seed (Random_Seed *seed_ptr);
110
112
113
114/*
115 The "really really random" PRNG. This is a separate RANROT seed that is
116 seeded once at startup and never reset under any circumstances. It can
117 also be used to seed get_rnd_number and the main RANROT seed. If doing this,
118 save and restore the seeds using the functions above ore OOSaveRandomState()
119 and OORestoreRandomState().
120
121 Since these use a global seed, they may only be used from the main thread.
122*/
123
124uint32_t OOReallyRandom(void);
125void OOInitReallyRandom(uint64_t seed);
126
128void OOSetReallyRandomRndSeed(void);
130
131/*
132 OOSaveRandomState()/OORestoreRandomState(): save and restore both the main
133 RANROT seed and the rnd seed in one shot.
134*/
140
143
144
145
146/*** Only inline definitions beyond this point ***/
147
149{
150 return ((seed1.a == seed2.a)&&(seed1.b == seed2.b)&&(seed1.c == seed2.c)&&(seed1.d == seed2.d)&&(seed1.e == seed2.e)&&(seed1.f == seed2.f));
151}
152
153
155{
156 return equal_seeds(a_seed, kNilRandomSeed);
157}
158
159
161{
162 return ((x << 1) | (x >> 7)) & 255;
163}
164
165
166// a method used to determine interplanetary distances,
167// if accurate, it has to scale distance down by a factor of 7.15:7.0
168// to allow routes navigable in the original!
169OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2)
170{
171 int dx = x1 - x2;
172 int dy = (y1 - y2)/2;
173 int dist = sqrt(dx*dx + dy*dy); // N.b. Rounding error due to truncation is desired.
174 return 0.4 * dist;
175}
176
177
178OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2)
179{
180 double dx = x1 - x2;
181 double dy = (y1 - y2) / 2.0;
182 double dist = hypot(dx, dy);
183 return 0.4 * dist;
184}
185
186
187OOINLINE double travelTimeBetweenPlanetPositions(int x1, int y1, int x2, int y2)
188{
189 double distance = distanceBetweenPlanetPositions(x1, y1, x2, y2);
190 return distance * distance;
191}
192
193#endif
#define INLINE_CONST_FUNC
#define OOINLINE
float x
RANROTSeed RanrotSeedFromRandomSeed(Random_Seed seed)
const Random_Seed kNilRandomSeed
float randf(void)
struct RANROTSeed RANROTSeed
OOINLINE int is_nil_seed(Random_Seed a_seed) INLINE_CONST_FUNC
void make_pseudo_random_seed(Random_Seed *seed_ptr)
RANROTSeed RANROTGetFullSeed(void)
OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2) INLINE_CONST_FUNC
void seed_RNG_only_for_planet_description(Random_Seed s_seed)
void ranrot_srand(uint32_t seed)
OOINLINE int rotate_byte_left(int x) INLINE_CONST_FUNC
void OOSetReallyRandomRANROTSeed(void)
void setRandomSeed(RNG_Seed a_seed)
uint32_t OOReallyRandom(void)
float randfWithSeed(RANROTSeed *ioSeed)
float bellf(int n)
int16_t munge_checksum(long long value)
RNG_Seed currentRandomSeed(void)
void OOInitReallyRandom(uint64_t seed)
struct Random_Seed Random_Seed
void OORestoreRandomState(OORandomState state)
OORandomState OOSaveRandomState(void)
struct RNG_Seed RNG_Seed
void RANROTSetFullSeed(RANROTSeed seed)
RANROTSeed RanrotSeedFromRNGSeed(RNG_Seed seed)
OOINLINE double travelTimeBetweenPlanetPositions(int x1, int y1, int x2, int y2)
void OOSetReallyRandomRndSeed(void)
unsigned RanrotWithSeed(RANROTSeed *ioSeed)
int gen_rnd_number(void)
double cunningFee(double value, double precision)
void clear_checksum(void)
OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC
void seed_for_planet_description(Random_Seed s_seed)
RANROTSeed MakeRanrotSeed(uint32_t seed)
void OOSetReallyRandomRANROTAndRndSeeds(void)
unsigned Ranrot(void)
void rotate_seed(Random_Seed *seed_ptr)
OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC
RANROTSeed ranrot
uint32_t low
uint32_t high
int32_t c
int32_t d
int32_t a
int32_t b