1 //originally written by Davide Byron
2 //this code is released under the GPLv3
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
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 :)
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;
32 //target we're pointing at
33 integer CurrentTarget
;
34 //how many seconds to reach the point of interest
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;
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
)
55 if( pos
.x
> CAGE_MAX_X
)
57 if( pos
.y
< CAGE_MIN_Y
)
59 if( pos
.y
> CAGE_MAX_Y
)
61 if( 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
)
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
;
82 //if the creature can reach the object within a single "step"
84 if( targetPos
- creaturePos
< MaxStepLength
)
85 creaturePos
= targetPos
;
86 //otherwise move as much as possible toward it
88 creaturePos
= creaturePos
+ MaxStepLength
;
92 //if the creature can reach the object within a single "step"
94 if( creaturePos
- targetPos
< MaxStepLength
)
95 creaturePos
= targetPos
;
96 //otherwise move as much as possible toward it
98 creaturePos
= creaturePos
- MaxStepLength
;
110 on_rez(integer param
)
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
), "");
148 llLookAt( newPosition
, Force
, Dump
);
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.
170 //notice the not_at_target event
171 //llMessageLinked(LINK_SET, E_NOT_AT_TARGET, "", "");