Added the tree scripts, w/ comments
[ProgettoPaperellaDiGomma.git] / movementFollowScripted.lsl
blob497b5d11bc511dcbbd651d3e0555e71b739e345f
1 //originally written by Davide Byron
2 //this code is released under the GPLv3
3 //
4 // Reacts to E_SCRIPTED_POSITION 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_SCRIPTED_POSITION = 2;
17 //integer E_AT_TARGET = 9;
18 //integer E_NOT_AT_TARGET = 10;
19 integer R_MOD_ENERGY = 11;
20 //cage boundaries expressed in region absolute coords
21 integer CAGE_MIN_X = 1;
22 integer CAGE_MAX_X = 249;
23 integer CAGE_MIN_Y = 1;
24 integer CAGE_MAX_Y = 249;
25 integer CAGE_MIN_Z = 1;
26 integer CAGE_MAX_Z = 100;
31 //global vars
32 //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;
49 //deny moving out of a definite cage
50 //mostly used for debug
51 vector checkCageLimit( vector pos )
53 if( pos.x < CAGE_MIN_X )
54 pos.x = CAGE_MIN_X;
55 if( pos.x > CAGE_MAX_X )
56 pos.x = CAGE_MAX_X;
57 if( pos.y < CAGE_MIN_Y )
58 pos.y = CAGE_MIN_Y;
59 if( pos.y > CAGE_MAX_Y )
60 pos.y = CAGE_MAX_Y;
61 if( pos.z < CAGE_MIN_Z )
62 pos.z = CAGE_MIN_Z;
63 if( llGround( pos - llGetPos() ) > pos.z )
64 pos.z = llGround( pos - llGetPos() ) + 0.1;
65 if( pos.z > CAGE_MAX_Z )
66 pos.z = CAGE_MAX_Z;
68 return pos;
74 //function that will calculate the movement needed to reach
75 //a target in a given axis it's limited by the creature max step value
76 float moveTowardTarget(float creaturePos, float targetPos)
78 //determine if we need to move forward or backward in the axis
79 float direction = targetPos - creaturePos;
80 if( direction >= 0 )
82 //if the creature can reach the object within a single "step"
83 //do it
84 if( targetPos - creaturePos < MaxStepLength )
85 creaturePos = targetPos;
86 //otherwise move as much as possible toward it
87 else
88 creaturePos = creaturePos + MaxStepLength;
90 else
92 //if the creature can reach the object within a single "step"
93 //do it
94 if( creaturePos - targetPos < MaxStepLength )
95 creaturePos = targetPos;
96 //otherwise move as much as possible toward it
97 else
98 creaturePos = creaturePos - MaxStepLength;
101 return creaturePos;
107 default
110 on_rez(integer param)
112 llResetScript();
118 state_entry()
120 //phantom is needed to avoid collision, they cause too much lag
121 llSetStatus(STATUS_PHANTOM, TRUE);
122 //physical is needed to use physical moving functions
123 llSetStatus(STATUS_PHYSICS, TRUE);
130 link_message( integer sender_num, integer num, string str, key id )
132 //a scripted object has been sensed
133 if(num == E_SCRIPTED_POSITION )
135 vector newPosition = llGetPos();
136 vector targetPosition = (vector)str;
137 //determine the new position
138 newPosition.x = moveTowardTarget( newPosition.x, targetPosition.x );
139 newPosition.y = moveTowardTarget( newPosition.y, targetPosition.y );
140 newPosition.z = targetPosition.z;
141 newPosition = checkCageLimit( newPosition );
143 //point the new target...
144 CurrentTarget = llTarget( newPosition, Proximity );
145 //ask for energy substraction due to movement
146 llMessageLinked(LINK_SET, R_MOD_ENERGY, (string)(-llVecDist(llGetPos(), newPosition)/EnergyDividendum), "");
147 //look at the target
148 llLookAt( newPosition, Force, Dump );
149 //move
150 llMoveToTarget( newPosition, Speed );
157 //this has no use as of today, probably will be removed.
158 at_target(integer tnum, vector targetpos, vector ourpos)
160 //notice the at_target event
161 //llMessageLinked(LINK_SET, E_AT_TARGET, "", "");
167 //this has no use as of today, probably will be removed.
168 not_at_target()
170 //notice the not_at_target event
171 //llMessageLinked(LINK_SET, E_NOT_AT_TARGET, "", "");