-
Notifications
You must be signed in to change notification settings - Fork 4
/
seed.clj
62 lines (48 loc) · 1.77 KB
/
seed.clj
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
(ns hard.seed
(:import [UnityEngine] [Hard PerlinNoise]))
(defn seed! [v] (set! UnityEngine.Random/seed (hash v)))
(defn srand
([] UnityEngine.Random/value)
([n] (* n (srand))))
(defn srand-int [n] (int (* (srand) n)))
(defn srand-nth [col] (get col (srand-int (count col))))
(defn sshuffle [col] (sort-by (fn [_](srand)) col))
;https://gist.github.com/nasser/de0ddaead927dfa5261b
(defmacro schance [& body]
(let [parts (partition 2 body)
total (apply + (map first parts))
rsym (gensym "random_")
clauses (->> parts
(sort-by first (comparator >))
(reductions
(fn [[odds-1 _]
[odds-2 expr-2]]
[(+ odds-1 (/ odds-2 total)) expr-2])
[0 nil])
rest
(mapcat
(fn [[odds expr]]
[(or (= 1 odds) `(< ~rsym ~odds))
expr])))]
`(let [~rsym (srand)]
(cond ~@clauses))))
(defn srand-vec [& more]
(mapv (fn [col]
(cond (number? col) (srand col)
(sequential? col)
(case (count col)
0 (srand)
1 (srand (first col))
2 (+ (srand (apply - (reverse col))) (first col))
(srand)
:else (srand)))) more))
(defn noise*
([] (noise* 0 {}))
([seed] (noise* seed {}))
([seed opts]
(let [i (PerlinNoise. (hash seed))
{:keys [in out]} opts
nf (fn [v] (.Noise i (double (.x v)) (double (.y v) ) (double (.z v))))]
(apply comp (filter fn? [out nf in])))))
(defn harmonic [f mags f2] (fn [v] (reduce f2 (map #(f (UnityEngine.Vector3/op_Multiply v %)) mags))))
(defn list-reduce [a b] (if (seq? a) (cons b a) (list b a)))