1 #ifndef SIMRANDOM_H
2 #define SIMRANDOM_H
3 
4 #include <stddef.h>
5 #include "../simtypes.h"
6 
7 
8 uint32 get_random_seed();
9 
10 uint32 setsimrand(uint32 seed, uint32 noise_seed);
11 
12 /* generates a random number on [0,max-1]-interval
13  * without affecting the game state
14  * Use this for UI etc.
15  */
16 uint32 sim_async_rand(const uint32 max);
17 
18 /* generates a random number on [0,max-1]-interval */
19 uint32 simrand(const uint32 max);
20 
21 /* generates a random number on [0,0xFFFFFFFFu]-interval */
22 uint32 simrand_plain();
23 
24 double perlin_noise_2D(const double x, const double y, const double persistence);
25 
26 // for network debugging, i.e. finding hidden simrands in wrong places
27 enum { INTERACTIVE_RANDOM=1, STEP_RANDOM=2, SYNC_STEP_RANDOM=4, LOAD_RANDOM=8, MAP_CREATE_RANDOM=16 };
28 void set_random_mode( uint16 );
29 void clear_random_mode( uint16 );
30 uint16 get_random_mode();
31 
32 // just more speed with those (generate a precalculated map, which needs only smoothing)
33 void init_perlin_map( sint32 w, sint32 h );
34 void exit_perlin_map();
35 
36 /* Randomly select an entry from the given array. */
pick_any(T const (& array)[N])37 template<typename T, size_t N> T const& pick_any(T const (&array)[N])
38 {
39 	return array[simrand(N)];
40 }
41 
42 /* Randomly select an entry from the given container. */
pick_any(U<T> const & container)43 template<typename T, template<typename> class U> T const& pick_any(U<T> const& container)
44 {
45 	return container[simrand(container.get_count())];
46 }
47 
48 /* Randomly select an entry from the given weighted container. */
pick_any_weighted(U<T> const & container)49 template<typename T, template<typename> class U> T const& pick_any_weighted(U<T> const& container)
50 {
51 	return container.at_weight(simrand(container.get_sum_weight()));
52 }
53 
54 /* Randomly select an entry from the given subset of a weighted container. */
pick_any_weighted_subset(U const & container)55 template<typename T, typename U> T const& pick_any_weighted_subset(U const& container)
56 {
57 	return container.at_weight(simrand(container.get_sum_weight()));
58 }
59 
60 /* Randomly select an entry from the given array. */
pick_any_async(T const (& array)[N])61 template<typename T, size_t N> T const& pick_any_async(T const (&array)[N])
62 {
63 	return array[sim_async_rand(N)];
64 }
65 
66 /* Randomly select an entry from the given container. */
pick_any_async(U<T> const & container)67 template<typename T, template<typename> class U> T const& pick_any_async(U<T> const& container)
68 {
69 	return container[sim_async_rand(container.get_count())];
70 }
71 
72 /* Randomly select an entry from the given weighted container. */
pick_any_weighted_async(U<T> const & container)73 template<typename T, template<typename> class U> T const& pick_any_weighted_async(U<T> const& container)
74 {
75 	return container.at_weight(sim_async_rand(container.get_sum_weight()));
76 }
77 
78 /* Randomly select an entry from the given subset of a weighted container. */
pick_any_weighted_subset_async(U const & container)79 template<typename T, typename U> T const& pick_any_weighted_subset_async(U const& container)
80 {
81 	return container.at_weight(sim_async_rand(container.get_sum_weight()));
82 }
83 
84 // compute integer log10
85 uint32 log10( uint32 v );
86 
87 uint32 log2( uint32 i );
88 
89 
90 // compute integer sqrt
91 uint32 sqrt_i32(uint32 num);
92 uint64 sqrt_i64(uint64 num);
93 
94 #endif
95