1 /**
2     Holds seed and seeding helpers
3 */
4 module dosimplex.seed;
5 
6 /**
7     A seed is the seed (tadaa) for all procedual generations inside the engine. 
8     It can be initialized with all types of input, always resulting in a long as representation.
9     Also there is a fixed conversion into float/double and other types
10 */
11 struct Seed
12 {
13     /// Constructs a Seed with a native uint as input
14     @nogc this(long s) 
15     {
16         _seed = s;
17     }
18 
19     /// Construct from a input string. Now here things become interesting
20     @nogc this(string s)
21     {
22         this.seed = s;
23     }
24 
25 
26     /// gets the seed
27     @nogc @safe @property long seed() const
28     {
29         return _seed;
30     }
31     /// sets the seed
32     @nogc @safe @property long seed(long s) 
33     {
34         return _seed=s;
35     }
36     /// sets the seed from string
37     /// uses djb2 - http://www.cse.yorku.ca/~oz/hash.html
38     @nogc @safe @property long seed(string s)
39     {
40         _seed = 5381;
41         foreach(c; s) {
42             _seed = ((_seed << 5) + _seed) + c; // hash * 33 + c 
43         }
44 
45         return _seed;
46     }
47     
48     /// returns the floating point represenatation of this seed
49     @nogc @safe @property float fSeed() const
50     {
51         return cast(float)_seed;
52     }
53 
54     /// Ditto
55     @nogc @safe @property double dSeed() const
56     {
57         return cast(double)_seed;
58     }
59     
60 
61 private:
62     /// The seed generated/stored
63     long _seed = 1787569; // we default to a somehow shitty seed we just say is random - 1337^2
64 }