forked from danfinlay/voxel-forest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
229 lines (214 loc) · 6.79 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
module.exports = function (game, opts) {
if (!opts) opts = {};
if (opts.bark === undefined) opts.bark = 1;
if (opts.leaves === undefined) opts.leaves = 2;
if (!opts.height) opts.height = Math.random() * 16 + 4;
if (opts.base === undefined) opts.base = opts.height / 3;
if (opts.radius === undefined) opts.radius = opts.base;
if (opts.treetype === undefined) opts.treetype = 1;
if (opts.position === undefined) throw "voxel-forest requires position option";
if (opts.setBlock === undefined) throw "voxel-forest requires setBlock option";
var set = opts.setBlock;
var pos_ = {
x: opts.position.x,
y: opts.position.y,
z: opts.position.z
};
// clone position so it can be mutated
function position () {
return {
x: pos_.x,
y: pos_.y,
z: pos_.z
};
}
function subspacetree() {
var around = [
[ 0, 1 ], [ 0, -1 ],
[ 1, 1 ], [ 1, 0 ], [ 1, -1 ],
[ -1, 1 ], [ -1, 0 ], [ -1, -1 ]
];
for (var y = 0; y < opts.height - 1; y++) {
var pos = {x:opts.position.x, y:opts.position.y, z:opts.position.y};
pos.y += y
if (set(pos, opts.bark)) break;
if (y < opts.base) continue;
around.forEach(function (offset) {
if (Math.random() > 0.5) return;
var x = offset[0]
var z = offset[1]
pos.x += x;
pos.z += z;
set(pos, opts.leaves);
pos.x -= x;
pos.z -= z;
});
}
}
function guybrushtree() {
var sphere = function(x,y,z, r) {
return x*x + y*y + z*z <= r*r;
}
for (var y = 0; y < opts.height - 1; y++) {
var pos = {x:opts.position.x, y:opts.position.y, z:opts.position.y};
pos.y += y;
if (set(pos, opts.bark)) break;
}
var radius = opts.radius;
for (var xstep = -radius; xstep <= radius; xstep++) {
for (var ystep = -radius; ystep <= radius; ystep++) {
for (var zstep = -radius; zstep <= radius; zstep++) {
if (sphere(xstep,ystep,zstep, radius)) {
var leafpos = {
x: pos.x + xstep,
y: pos.y + ystep,
z: pos.z + zstep
}
set(leafpos, opts.leaves);
}
}
}
}
}
function drawAxiom(axiom, angle, unitsize, units) {
var posstack = [];
var penangle = 0;
var pos = {x:opts.position.x, y:opts.position.y, z:opts.position.y};
pos.y += unitsize * 30;
function moveForward() {
var ryaw = penangle * Math.PI/180;
for (var i = 0; i < units; i++) {
pos.y += unitsize * Math.cos(ryaw);
pos.z += unitsize * Math.sin(ryaw);
set(pos,opts.leaves);
}
}
function setPoint() {
set(pos, opts.bark);
}
function setMaterial(value) {
mindex = value;
}
function yaw(angle) {
penangle += angle;
}
function pitch(angle) {
//turtle.pitch += angle;
}
function roll(angle) {
//turtle.roll += angle;
}
function PushState() {
//penstack.push(turtle);
posstack.push(pos);
}
function PopState() {
// turtle = penstack.pop();
pos = posstack.pop();
}
//F - move forward one unit with the pen down
//G - move forward one unit with the pen up
//# - Changes draw medium.
// + - yaw the turtle right by angle parameter
// - - yaw the turtle left by angle parameter
// & - pitch the turtle down by angle parameter
// ^ - pitch the turtle up by angle parameter
// / - roll the turtle to the right by angle parameter
// * - roll the turtle to the left by angle parameter
// [ - save in stack current state info
// ] - recover from stack state info
for (var i = 0; i < axiom.length; i++) {
var c = axiom.charAt(i);
switch(c) {
case 'F':
moveForward();
setPoint();
break;
case '+':
yaw(+angle);
break;
case '-':
yaw(-angle);
break;
case '&':
pitch(+angle);
break;
case '^':
pitch(-angle);
break;
case '/':
roll(+angle);
break;
case '*':
roll(-angle);
break;
case 'G':
moveForward();
break;
case '[':
PushState();
break;
case ']':
PopState();
break;
case '0':
setMaterial(0);
break;
case '1':
setMaterial(1);
break;
case '2':
setMaterial(2);
break;
case '3':
setMaterial(3);
break;
}
}
}
function fractaltree() {
var axiom = "FX";
var rules = [ ["X", "X+YF+"], ["Y", "-FX-Y"]];
axiom = applyRules(axiom,rules);
axiom = applyRules(axiom,rules);
axiom = applyRules(axiom,rules);
axiom = applyRules(axiom,rules);
axiom = applyRules(axiom,rules);
axiom = applyRules(axiom,rules);
drawAxiom(axiom, 90, 1, 5);
}
switch (opts.treetype) {
case 1:
subspacetree();
break;
case 2:
guybrushtree();
break;
case 3:
fractaltree();
break;
default:
subspacetree();
}
};
function regexRules(rules) {
var regexrule = '';
rules.forEach(function (rule) {
if (regexrule != '') {
regexrule = regexrule+ '|' ;
}
regexrule = regexrule+rule[0];
});
return new RegExp(regexrule, "g");
}
function applyRules(axiom, rules) {
function matchRule(match)
{
for (var i=0;i<rules.length;i++)
{
if (rules[i][0] == match) return rules[i][1];
}
return '';
}
return axiom.replace(regexRules(rules), matchRule);
}