Added the tree scripts, w/ comments
[ProgettoPaperellaDiGomma.git] / spawnFractalTreeFruit.lsl
blob2402b8e1c3255327ed99d8a82ec30470866c0231
1 //originally written by Davide Byron
2 //this code is released under the GPLv3
3 //
4 // this script takes care of spawning tree fruits at random times, untill the branch dies.
5 // spawning of fruits is limited by the sim lag. If there is too much lag the fruits won't be spawned
6 // It's also possible to spawn a fruit upon user touch on the branch.
7 // in this case lag is not taken into account.
8 //
9 //dedicated to Mealea, thanks for the passion you put into things and for being able to pass it on to me :)
11 string FRUIT_NAME;
16 //how long between a spawning and the other
17 float FruitSpawningCycle = 240.0;
18 //bias
19 float FruitSpawningCycleBias = 45.0;
20 //max lag above which spawning is not permitted
21 float SpawningLagTreshold = 0.95;
22 //two vars to keep track of sim lag
23 float AverageSimLagSum = 0;
24 float AverageSimLagSamples = 0;
29 //generates a random float number ranging from -range to range
30 float myRand( float range )
32 float sign = llFrand(2.1);
33 float random = llFrand( range + 0.1 );
34 if( sign >= 1.0 )
35 return random;
36 else
37 return -random;
43 default
46 on_rez(integer param)
48 llResetScript();
54 state_entry()
56 //delay the spawning of the fruit after the branch rezzing
57 //if the branch is eaten no fruit should be spawned
58 //(this should already work, but keep an eye on it...)
59 FRUIT_NAME = llGetInventoryName(INVENTORY_OBJECT, 0);
60 llSetTimerEvent( FruitSpawningCycle + myRand( FruitSpawningCycleBias) );
61 AverageSimLagSum = llGetRegionTimeDilation();
62 AverageSimLagSamples = 1;
68 timer()
70 AverageSimLagSum += llGetRegionTimeDilation();
71 AverageSimLagSamples += 1;
72 //the only restrain is the lag of the sim
73 if( AverageSimLagSum/AverageSimLagSamples >= SpawningLagTreshold
74 && llGetRegionTimeDilation() >= SpawningLagTreshold )
76 //if the lag is low, let's spawn the fruit
77 llRezObject(FRUIT_NAME,llGetPos(),<0,0,0>,ZERO_ROTATION,1);
83 touch_start(integer touched)
85 //if a user touch the branch, spawn the fruit, its kinda gratifying...
86 llRezObject(FRUIT_NAME,llGetPos(),<0,0,0>,ZERO_ROTATION,1);