Oolite
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
36typedef struct Random_Seed {
37 uint8_t a, /* 6c */
38 b, /* 6d */
39 c, /* 6e */
40 d, /* 6f */
41 e, /* 70 */
42 f; /* 71 */
44
45typedef struct RNG_Seed {
46 int32_t a,
51
52typedef struct RANROTSeed {
53 uint32_t high,
56
57extern const Random_Seed kNilRandomSeed;
58
59// checksum stuff
60void clear_checksum(void);
61int16_t munge_checksum(long long value);
62
63// cunning price rounding routine:
64double cunningFee(double value, double precision); // precision is the fraction below which numbers become insignificant.
65
66// an implementation of RANROT
67// pseudo random number generator
68void ranrot_srand(uint32_t seed);
69unsigned Ranrot(void);
70#define ranrot_rand() ((int)Ranrot()) // Some uses perform arithmetic that does weird things if result is unsigned -- DustEntity.m, for instance.
71float randf(void);
72float bellf(int n);
73
76
77RANROTSeed MakeRanrotSeed(uint32_t seed);
80
81unsigned RanrotWithSeed(RANROTSeed* ioSeed);
82float randfWithSeed(RANROTSeed* ioSeed);
83
84OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
85OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
86
90void setRandomSeed(RNG_Seed a_seed);
91
92// Range: 0..255
93int gen_rnd_number(void);
94
96
98
99void rotate_seed(Random_Seed* seed_ptr);
101
103
104/*
105 The "really really random" PRNG. This is a separate RANROT seed that is
106 seeded once at startup and never reset under any circumstances. It can
107 also be used to seed get_rnd_number and the main RANROT seed. If doing this,
108 save and restore the seeds using the functions above ore OOSaveRandomState()
109 and OORestoreRandomState().
110
111 Since these use a global seed, they may only be used from the main thread.
112*/
113
114uint32_t OOReallyRandom(void);
115void OOInitReallyRandom(uint64_t seed);
116
118void OOSetReallyRandomRndSeed(void);
120
121/*
122 OOSaveRandomState()/OORestoreRandomState(): save and restore both the main
123 RANROT seed and the rnd seed in one shot.
124*/
130
133
134/*** Only inline definitions beyond this point ***/
135
137{
138 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));
139}
140
142{
143 return equal_seeds(a_seed, kNilRandomSeed);
144}
145
147{
148 return ((x << 1) | (x >> 7)) & 255;
149}
150
151// a method used to determine interplanetary distances,
152// if accurate, it has to scale distance down by a factor of 7.15:7.0
153// to allow routes navigable in the original!
154OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2)
155{
156 int dx = x1 - x2;
157 int dy = (y1 - y2) / 2;
158 int dist = sqrt(dx * dx + dy * dy); // N.b. Rounding error due to truncation is desired.
159 return 0.4 * dist;
160}
161
162OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2)
163{
164 double dx = x1 - x2;
165 double dy = (y1 - y2) / 2.0;
166 double dist = hypot(dx, dy);
167 return 0.4 * dist;
168}
169
170OOINLINE double travelTimeBetweenPlanetPositions(int x1, int y1, int x2, int y2)
171{
172 double distance = distanceBetweenPlanetPositions(x1, y1, x2, y2);
173 return distance * distance;
174}
175
176#endif
#define INLINE_CONST_FUNC
#define OOINLINE
float x
OOINLINE OOScalar distance(Vector v1, Vector v2) INLINE_CONST_FUNC
Definition OOVector.h:280
const Random_Seed kNilRandomSeed
RANROTSeed RanrotSeedFromRandomSeed(Random_Seed seed)
float randf(void)
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)
void OORestoreRandomState(OORandomState state)
OORandomState OOSaveRandomState(void)
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