Added the tree scripts, w/ comments
[ProgettoPaperellaDiGomma.git] / movementRandom.lsl
blob0710154a244ec42c51a4786fe8844890c78ab21a
1 //originally written by Davide Byron
2 //this code is released under the GPLv3
3 //
4 // Reacts to E_NO_SENSOR event
5 // Produces R_SUB_ENERGY events
6 // makes the creature move in the direction of a detected active object at the cost of some energy
7 //
8 // the creature tryies to reach the object and will use an energy amount proportional to the
9 // distance covered with the movement.
10 // if there is not enough energy the creature will succed in moving but die soon after if the reached
11 // object does not provide food.
13 //dedicated to Mealea, thanks for the passion you put into things and for being able to pass it on to me :)
15 //standards
16 //integer E_AT_TARGET = 9;
17 //integer E_NOT_AT_TARGET = 10;
18 integer E_NO_SENSOR = 5;
19 integer R_MOD_ENERGY = 11;
20 //cage boundaries expressed in region absolute coords
21 //you might want to customize them
22 integer CAGE_MIN_X = 1;
23 integer CAGE_MAX_X = 249;
24 integer CAGE_MIN_Y = 1;
25 integer CAGE_MAX_Y = 249;
26 integer CAGE_MIN_Z = 1;
27 integer CAGE_MAX_Z = 100;
31 //global vars for the creature, can be subjected to evolution one day...
32 i//target we're pointing at
33 integer CurrentTarget;
34 //how many seconds to reach the point of interest
35 float Speed = 3.0;
36 //treshold for the at target event
37 float Proximity = 1.0;
38 //how far can we go in a single movement
39 float MaxStepLength = 40.0;
40 //used to calculate energy consumption, the higher the value, the less the energy required
41 float EnergyDividendum = 100;
42 //crappy stuffs
43 float Force = 1;
44 float Dump = 0.1;
48 //generates a random float number ranging from -range to range
49 float myRand( float range )
51 float sign = llFrand(2.1);
52 float random = llFrand( range + 0.1 );
53 if( sign >= 1.0 )
54 return random;
55 else
56 return -random;
62 //deny moving out of a definite "cage"
63 //mostly used for debug
64 vector checkCageLimit( vector pos )
66 if( pos.x < CAGE_MIN_X )
67 pos.x = CAGE_MIN_X;
68 if( pos.x > CAGE_MAX_X )
69 pos.x = CAGE_MAX_X;
70 if( pos.y < CAGE_MIN_Y )
71 pos.y = CAGE_MIN_Y;
72 if( pos.y > CAGE_MAX_Y )
73 pos.y = CAGE_MAX_Y;
74 if( pos.z < CAGE_MIN_Z )
75 pos.z = CAGE_MIN_Z;
76 if( llGround( pos - llGetPos() ) > pos.z )
77 pos.z = llGround( pos - llGetPos() ) + 0.1;
78 if( pos.z > CAGE_MAX_Z )
79 pos.z = CAGE_MAX_Z;
81 return pos;
87 default
90 on_rez(integer param)
92 llResetScript();
98 state_entry()
100 //phantom is needed to avoid collision, they cause too much lag
101 llSetStatus(STATUS_PHANTOM, TRUE);
102 //physical is needed to use physical moving functions
103 llSetStatus(STATUS_PHYSICS, TRUE);
110 link_message( integer sender_num, integer num, string str, key id )
112 //the creature sees nothing, so move at random...
113 if( num == E_NO_SENSOR )
115 //calculates the new position
116 vector newPosition = llGetPos();
117 newPosition.x = newPosition.x + myRand(MaxStepLength);
118 newPosition.y = newPosition.y + myRand(MaxStepLength);
119 newPosition.z = newPosition.z + myRand(MaxStepLength);
121 newPosition = checkCageLimit( newPosition );
123 //point the target
124 CurrentTarget = llTarget( newPosition, Proximity );
125 //ask for energy substraction due to movement
126 llMessageLinked(LINK_SET, R_MOD_ENERGY, (string)(-llVecDist(llGetPos(), newPosition)/10), "");
127 //look at the target
128 llLookAt( newPosition, Force, Dump );
129 //move
130 llMoveToTarget( newPosition, Speed );
137 //this has no use as of today, probably will be removed.
138 at_target(integer tnum, vector targetpos, vector ourpos)
140 //notice the at_target event
141 //llMessageLinked(LINK_SET, E_AT_TARGET, "", "");
147 //this has no use as of today, probably will be removed.
148 not_at_target()
150 //notice the not_at_target event
151 //llMessageLinked(LINK_SET, E_NOT_AT_TARGET, "", "");