1 //originally written by Davide Byron
2 //this code is released under the GPLv3
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
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_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;
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;
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
)
54 if( pos
.x
> CAGE_MAX_X
)
56 if( pos
.y
< CAGE_MIN_Y
)
58 if( pos
.y
> CAGE_MAX_Y
)
60 if( 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
)
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
;
81 //if the creature can reach the object within a single "step"
83 if( targetPos
- creaturePos
< MaxStepLength
)
84 creaturePos
= targetPos
;
85 //otherwise move as much as possible toward it
87 creaturePos
= creaturePos
+ MaxStepLength
;
91 //if the creature can reach the object within a single "step"
93 if( creaturePos
- targetPos
< MaxStepLength
)
94 creaturePos
= targetPos
;
95 //otherwise move as much as possible toward it
97 creaturePos
= creaturePos
- MaxStepLength
;
109 on_rez(integer param
)
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
), "");
147 llLookAt( newPosition
, Force
, Dump
);
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.
169 //notice the not_at_target event
170 //llMessageLinked(LINK_SET, E_NOT_AT_TARGET, "", "");