Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Building Fractal_Generator  

Fractal_Generator

The following is a set of scripts for generating fractals (strictly speaking, fractal trees). I used variants of this script to build all of the fractals in my (2005) burning life build.

Category: Building
By : Seifert Surface
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Fractal_Generator
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Fractal_Generator_1.lsl
Get file # 2. Fractal_Generator_2.lsl
Get file # 3. Fractal_Generator_3.lsl
1 ///////////////////////////////////////////
2 //////////Fractal Generator 1.0////////////
3 ///////////////////////////////////////////
4 ///////////////////by//////////////////////
5 ///////////////////////////////////////////
6 /////////////Seifert Surface///////////////
7 /////////////2G!tGLf 2nLt9cG/////Aug 2005//
8 ///////////////////////////////////////////
9
10 //Instructions for use:
11
12 //Rez a prim, mess around with it however you want. When you're done, make 3 copies of it.
13 //Put this script ("Fractal Generator 1.0") in one, which will be the base prim of the
14 //fractal. Put the script "Fractal Generator 1.0 Child" in another, and "Fractal Generator
15 //1.0 Rescaler" in the third. Take the "rescaler" prim, make sure it's name is "Object",
16 //and put it in the contents of the base prim (the one with this script). The scripts will
17 //build the fractal out of copies of this "rescaler" prim.
18
19 //Now we tell the scripts how we want the fractal to be built. Make some number of copies
20 //of the "child" prims (2 or 3 is a good number), and arrange them near or touching the
21 //"base" prim. Rotating them is fine, scaling them (down usually) is a good idea, but
22 //remember whenever you scale it to scale all three dimensions equally (use the grey boxes
23 //in the "Stretch" tool, not the red, green or blue). Rotating or moving the base prim is
24 //fine, but don't rescale it at this stage (the scripts rely on the "rescaler" prim to be
25 //an exact copy of the base prim).
26
27 //Now decide how many iterations you want the fractal to contain. 3 is a good number to
28 //start with. This script calculates beforehand how many prims the final fractal will
29 //contain, and will not build if it contains more than the "prim_limit" below. You can
30 //change this number (it is set in the first line of code below) to something bigger, but
31 //I take no responsibility if you end up crashing a sim! This is *not* a piece of self
32 //replicating code, but it will rez many prims relatively quickly. You have been warned.
33
34 //The formula for the number of prims in the final fractal, starting from b children and
35 //doing i iterations is:
36 // i+1 if b == 1
37 // (b^(i+1) - 1)/(b - 1) if b >= 1
38
39 //Having decided on the number of iterations you want, simply enter this number into the
40 //"Description" of the object (in the "General" tab of the extended editing controls).
41 //Finally, link all the child prims to the base prim, making sure that the base prim is
42 //the root of the link set. To do this, select the child prims first, add the base prim to
43 //the selection last (by holding down Shift and clicking on it), and then link (Ctrl-L, or
44 //selecting "Link" from the "Tools" menu).
45
46 //Now say "go" (the script will only listen to its owner), and the fractal will begin
47 //building (as long as the final fractal wouldn't contain too many prims). A good trick is
48 //to have the base and child prims (which are now linked) selected in edit mode, so that
49 //once the fractal is built, you can easily move the generator away. The generator will
50 //rez copies of the base and the child prims in their exact locations, so it can be
51 //difficult to select them.
52
53 //If you don't like how it turned out, simply edit the positions and scales of the child
54 //prims (you might want to unlink first and then relink again), and when you're happy, say
55 //"go" again, and it will generate the new fractal.
56
57 //Issues:
58
59 //It is impossible to rez a prim that is smaller than 0.01 metres, or larger than 10.0
60 //metres in any dimension. The script will detect when it tries to do so, and counts how
61 //many prims fail to rez because of this. It is also impossible to rez a prim further than
62 //around 10 metres distance from the rezzing object, which is the base prim. Unfortunately
63 //it doesn't seem to be exactly 10 metres, so I haven't implemented a similar check to see
64 //how many prims would be too far from the base prim to be rezzed. The code is in there,
65 //but commented out. Let me know if you work out something reliable. As is, attempting to
66 //rez a prim too far from the base will silently fail.
67
68 //If you modify the code, please keep my name with it, and make sure you don't crash a sim!
69 //Please do not sell this code, or anything with this code in it. Selling objects created
70 //with this code is ok. It would be nice if people would let me know what they're making
71 //with it :)
72
73 // Seifert Surface
74 // 2G!tGLf 2nLt9cG
75
76 ///////Start of code///////
77
78 integer prim_limit = 255; //wont build anything with more than this number of prims
79 integer iterations; //number of levels to go down
80 integer branching; //number of elts in each list
81
82 list posns;
83 list rotns;
84 list scales;
85
86 vector base_posn;
87
88 integer listen_control;
89 integer count_replies; //for making sure that all child prims in the base have replied
90 integer bad_scales = 0; //counting how many prims were too small or too big to rez
91 //integer bad_positions = 0; //counting how many prims were too far away from the generator to rez
92 //this seems unreliable - according to the wiki rezzing at a distance of more than 10 meters fails silently,
93 //but it seems to be able to go a little further (11 meters?). So the distance checking code is here but commented out,
94 //you can experiment with it if you like. As is, when trying to rez prims too far away, it will just fail silently.
95
96 rez_children(vector pos, rotation rot, float scl, integer iters)
97 {
98 integer branch = 0;
99 for(branch = 0; branch < branching; branch++)
100 {
101 //llSay(0, "iter: "+(string)iters+" branch: "+(string)branch);
102 vector check_scale = llGetScale()*llList2Float(scales, branch)*scl;
103 if(check_scale.x < 0.01 || check_scale.x > 10.0 || check_scale.y < 0.01 || check_scale.y > 10.0 || check_scale.z < 0.01 || check_scale.z > 10.0)
104 {
105 bad_scales++;
106 }
107 else
108 {
109 //start of distance checking code, commented out due to unreliability
110 //vector check_pos = pos + scl*(llList2Vector(posns, branch)*rot);
111 //
112 //if(llVecMag(check_pos) > 10)
113 // {
114 // bad_positions++;
115 // }
116 // else
117 // {
118 //end of dist checking code
119 llRezObject("Object", base_posn + pos + scl*(llList2Vector(posns, branch)*rot), //position to rez
120 <0,0,0>, //given vel... ignore
121 llList2Rot(rotns, branch)*rot, //rotation to rez
122 llRound(1000000.0 * llList2Float(scales, branch)*scl) //scaling, including workaround passing integer
123 );
124 // } //(from dist checking code)
125 }
126
127 //llSleep(1.0); //if you want to slow it down to see what its doing
128 if(iters > 1)
129 {
130 rez_children((pos + scl*llList2Vector(posns, branch)*rot), llList2Rot(rotns, branch)*rot, llList2Float(scales, branch)*scl, iters - 1);
131 }
132 }
133 }
134
135 default
136 {
138 {
139 key owner = llGetOwner(); //will only listen to its owner
140 listen_control = llListen(0, "", owner, "");
141 }
142
143 on_rez(integer number)
144 {
146 }
147
148 listen(integer channel, string name, key id, string message)
149 {
150 if(message == "go") //say "go" to make it start
151 {
152 llSay(0, "Recording data...");
153 state Recording;
154 }
155 }
156
158 {
159 llListenRemove(listen_control);
160 }
161 }
162
163 state Recording
164 {
166 {
167 branching = llGetNumberOfPrims() - 1; //dont count the root prim
168 if(branching <= 0)
169 {
170 llSay(0, "Link child prims first!");
171 state default;
172 }
173 //llSay(0, "branching: "+(string)branching);
174 //check how many prims there will be... (assuming none too small/big/far away)
175 iterations = (integer)llGetObjectDesc(); //put desired number of iterations in the object description
176 if(iterations <= 0)
177 {
178 llSay(0, "Number of iterations (enter it into the object description) must be a positive integer!");
179 state default;
180 }
181 //llSay(0, "iterations: "+(string)iterations);
182 integer num_prims;
183 if(branching == 1)
184 {
185 num_prims = iterations + 1;
186 }
187 else
188 {
189 float fi = (float)iterations;
190 float fb = (float)branching;
191 float temp1 = llPow(fb,(fi + 1)) - 1.0; //some math here...
192 float temp2 = (fb - 1.0);
193 num_prims = llRound(temp1/temp2);
194 }
195 llSay(0, "Final fractal will contain prims: "+(string)num_prims);
196 if(num_prims > prim_limit)
197 {
198 llSay(0, "Too many prims!");
199 state default;
200 }
201
202 count_replies = 0; //going to need all of the child prims to reply
203
204 posns = [];
205 rotns = [];
206 scales = [];
207 integer branch = 0;
208 for(branch = 0; branch < branching; branch++)
209 { //reset lists to correct lengths
210 posns = llListInsertList(posns, [ZERO_VECTOR], branch);
211 rotns = llListInsertList(rotns, [ZERO_ROTATION], branch);
212 scales = llListInsertList(scales, [1.0], branch);
213 }
214
215 llMessageLinked(LINK_ALL_OTHERS, 0, "return", NULL_KEY); //ask all child prims to report their parameters
216 }
217
218 link_message(integer sender_num, integer num, string str, key id)
219 {
220 count_replies++;
221 integer branch = sender_num - 2;
222 llSay(0, "Replied, child prim: "+(string)branch);
223
224 list reply = llParseString2List(str, ["|"], []); //reply is of form <position>|<rotation>|scale
225
226 vector temp_pos = (vector)llList2String(reply, 0);
227 rotation temp_rot = (rotation)llList2String(reply, 1);
228
229 vector temp = llGetScale(); //only measures scaling in x, but doesnt matter which: child prims should
230 float tempx = temp.x; //be scaled in all coords equally for code to work properly
231 float temp_scale = (float)llList2String(reply, 2)/tempx; //stored scale is relative to root prim scale
232
233 //llSay(0, (string)temp_pos+(string)temp_rot+(string)temp_sca le);
234
235 posns = llListReplaceList(posns, [temp_pos], branch, branch);
236 rotns = llListReplaceList(rotns, [temp_rot], branch, branch);
237 scales = llListReplaceList(scales, [temp_scale], branch, branch);
238
239 if(count_replies >= branching)
240 {
241 //llSay(0, "posns: "+llDumpList2String(posns, "|"));
242 //llSay(0, "rotns: "+llDumpList2String(rotns, "|"));
243 //llSay(0, "scales: "+llDumpList2String(scales, "|"));
244 state Building;
245 }
246 }
247 }
248
249 state Building
250 {
252 {
253 //llSay(0, "Starting...depth: "+(string)iterations)+" branching: "+(string)branching);
254 base_posn = llGetPos();
255 bad_scales = 0;
256 //bad_positions = 0;
257
258 llRezObject("Object", base_posn, <0,0,0>, llGetRot(),1); //rez copy of the base
259 rez_children(<0,0,0>, llGetRot(), 1.0, iterations); //yay recursion!!
260 llSay(0, "Done! "+(string)bad_scales+" prims had sizes too large or too small.");
261 //llSay(0, "(string)bad_positions+" had positions too far from base prim.");
262 state default;
263 }
264 }

Fractal_Generator

Child

Category: Building
By : Seifert Surface
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 ///////////////////////////////////////////
2 ///////Fractal Generator 1.0 Child/////////
3 ///////////////////////////////////////////
4 ///////////////////by//////////////////////
5 ///////////////////////////////////////////
6 /////////////Seifert Surface///////////////
7 /////////////2G!tGLf 2nLt9cG/////Aug 2005//
8 ///////////////////////////////////////////
9
10 default
11 {
13 {
14 }
15 link_message(integer sender_num, integer num, string str, key id)
16 {
17 vector pos = llGetLocalPos();
18 rotation rot = llGetLocalRot();
19 vector temp = llGetScale();
20 float scale = temp.x; //everything assumes scaling happens on all 3 axes equally, so just pick one
21 list data = [pos, rot, scale];
22 string reply = llDumpList2String(data, "|");
23 llMessageLinked(1, 0, reply, NULL_KEY);
24 }
25 }

Fractal_Generator

Rescaler

Category: Building
By : Seifert Surface
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 ///////////////////////////////////////////
2 /////Fractal Generator 1.0 Rescaler////////
3 ///////////////////////////////////////////
4 ///////////////////by//////////////////////
5 ///////////////////////////////////////////
6 /////////////Seifert Surface///////////////
7 /////////////2G!tGLf 2nLt9cG/////Aug 2005//
8 ///////////////////////////////////////////
9
10 default
11 {
13 {
14 }
15
16 on_rez(integer number)
17 {
18 if(number != 0) //when rezzed by an avatar, don't do anything
19 {
20 float scale = (float)number/1000000.0; //sneaky way to get a float in here using an integer...
21 llSetScale(llGetScale()*scale);
22 llRemoveInventory(llGetScriptName()); //kill the script when done
23 }
24 }
25 }

Back to the Best Free Tools in Second Life and OpenSim.