Added the tree scripts, w/ comments
[ProgettoPaperellaDiGomma.git] / movementFollowPassive.lsl
blob14f8b534814ef85f2fd5f86150335ff210433e0d
1 //originally written by Davide Byron
2 //this code is released under the GPLv3
3 //
4 // Reacts to E_PASSIVE_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_PASSIVE_POSITION = 3;
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;
48 //deny moving out of a definite cage
49 //mostly used for debug
50 vector checkCageLimit( vector pos )
52 if( pos.x < CAGE_MIN_X )
53 pos.x = CAGE_MIN_X;
54 if( pos.x > CAGE_MAX_X )
55 pos.x = CAGE_MAX_X;
56 if( pos.y < CAGE_MIN_Y )
57 pos.y = CAGE_MIN_Y;
58 if( pos.y > CAGE_MAX_Y )
59 pos.y = CAGE_MAX_Y;
60 if( pos.z < CAGE_MIN_Z )
61 pos.z = CAGE_MIN_Z;
62 if( llGround( pos - llGetPos() ) > pos.z )
63 pos.z = llGround( pos - llGetPos() ) + 0.1;
64 if( pos.z > CAGE_MAX_Z )
65 pos.z = CAGE_MAX_Z;
67 return pos;
73 //function that will calculate the movement needed to reach
74 //a target in a given axis it's limited by the creature max step value
75 float moveTowardTarget(float creaturePos, float targetPos)
77 //determine if we need to move forward or backward in the axis
78 float direction = targetPos - creaturePos;
79 if( direction >= 0 )
81 //if the creature can reach the object within a single "step"
82 //do it
83 if( targetPos - creaturePos < MaxStepLength )
84 creaturePos = targetPos;
85 //otherwise move as much as possible toward it
86 else
87 creaturePos = creaturePos + MaxStepLength;
89 else
91 //if the creature can reach the object within a single "step"
92 //do it
93 if( creaturePos - targetPos < MaxStepLength )
94 creaturePos = targetPos;
95 //otherwise move as much as possible toward it
96 else
97 creaturePos = creaturePos - MaxStepLength;
100 return creaturePos;
106 default
109 on_rez(integer param)
111 llResetScript();
117 state_entry()
119 //phantom is needed to avoid collision, they cause too much lag
120 llSetStatus(STATUS_PHANTOM, TRUE);
121 //physical is needed to use physical moving functions
122 llSetStatus(STATUS_PHYSICS, TRUE);
129 link_message( integer sender_num, integer num, string str, key id )
131 //a passive object has been sensed
132 if(num == E_PASSIVE_POSITION )
134 vector newPosition = llGetPos();
135 vector targetPosition = (vector)str;
136 //determine the new position
137 newPosition.x = moveTowardTarget( newPosition.x, targetPosition.x );
138 newPosition.y = moveTowardTarget( newPosition.y, targetPosition.y );
139 newPosition.z = targetPosition.z;
140 newPosition = checkCageLimit( newPosition );
142 //point the new target...
143 CurrentTarget = llTarget( newPosition, Proximity );
144 //ask for energy substraction due to movement
145 llMessageLinked(LINK_SET, R_MOD_ENERGY, (string)(-llVecDist(llGetPos(), newPosition)/EnergyDividendum), "");
146 //look at the target
147 llLookAt( newPosition, Force, Dump );
148 //move
149 llMoveToTarget( newPosition, Speed );
156 //this has no use as of today, probably will be removed.
157 at_target(integer tnum, vector targetpos, vector ourpos)
159 //notice the at_target event
160 //llMessageLinked(LINK_SET, E_AT_TARGET, "", "");
166 //this has no use as of today, probably will be removed.
167 not_at_target()
169 //notice the not_at_target event
170 //llMessageLinked(LINK_SET, E_NOT_AT_TARGET, "", "");