Added the tree scripts, w/ comments
[ProgettoPaperellaDiGomma.git] / playCatchMeOnTouch.lsl
blobc08caf142bbecc72fef28fbd85f9d9acde7f9b0a
1 //originally written by Davide Byron
2 //this code is released under the GPLv3
3 //
4 // Reacts to E_AVATAR_POSITION, E_TOUCH_ID and E_COLLISION_TYPE event
5 // when a touch event is catched the creature begins to play and
6 // activates a timer to determine the game duration
7 // on every avatar position event the creature will try to run away
8 // from her/him
9 // and on a collision event it will declare the avatar winner
10 // if the timer reaches it's event the creature win and will declare it
12 // WARNING: creature must be full physical to use this (and this script will turn it so)
14 // this gaming script is a early experiment to see how people could get involved
15 // in creature social life. more scripts like this will come in the future.
17 //dedicated to Mealea, thanks for the passion you put into things and for being able to pass it on to me :)
19 //standard stuffs
20 integer E_AGENT_POSITION = 1;
21 integer E_TOUCH_ID = 7;
22 integer R_MOD_ENERGY = 11;
23 //cage boundaries expressed in region absolute coords
24 integer CAGE_MIN_X = 1;
25 integer CAGE_MAX_X = 249;
26 integer CAGE_MIN_Y = 1;
27 integer CAGE_MAX_Y = 249;
28 integer CAGE_MIN_Z = 1;
29 integer CAGE_MAX_Z = 100;
35 //Global vars
36 //how far the creature can go with a single step
37 float MaxStep = 20.0;
38 //max time to get to the target point
39 float Speed = 3.0;
40 //how long does the game last
41 float GameDuration = 120.0;
42 //crappy things
43 float Force = 1;
44 float Dump = 0.1;
45 //avatar wich is playing with us
46 key AvatarKey;
47 //energy gain for victory and loss
48 float VictoryEnergy = 100.0;
49 float DefeatEnergy = 40.0;
53 //generates a random float number ranging from -range to range
54 float myRand( float range )
56 float sign = llFrand(2.1);
57 float random = llFrand( range + 0.1 );
58 if( sign >= 1.0 )
59 return random;
60 else
61 return -random;
67 //deny moving out of a definite cage
68 //mostly used for debug
69 vector checkCageLimit( vector pos )
71 if( pos.x < CAGE_MIN_X )
72 pos.x = CAGE_MIN_X;
73 if( pos.x > CAGE_MAX_X )
74 pos.x = CAGE_MAX_X;
75 if( pos.y < CAGE_MIN_Y )
76 pos.y = CAGE_MIN_Y;
77 if( pos.y > CAGE_MAX_Y )
78 pos.y = CAGE_MAX_Y;
79 if( pos.z < CAGE_MIN_Z )
80 pos.z = CAGE_MIN_Z;
81 if( llGround( pos - llGetPos() ) > pos.z )
82 pos.z = llGround( pos - llGetPos() ) + 0.1;
83 if( pos.z > CAGE_MAX_Z )
84 pos.z = CAGE_MAX_Z;
86 return pos;
92 //function that will calculate the movement needed to escape from
93 //a target in a given axis it's limited by the creature max step value
94 float runawayFromTarget(float creaturePos, float targetPos)
96 //determine if we need to move forward or backward in the axis
97 float direction = targetPos - creaturePos;
98 if( direction >= 0 )
100 //flee as far as you can!!!
101 creaturePos = creaturePos - MaxStep;
103 else
105 //flee as far as you can!!!
106 creaturePos = creaturePos + MaxStep;
109 return creaturePos;
115 default
118 on_rez(integer param)
120 llResetScript();
126 state_entry()
128 //collisions are required here.
129 llSetStatus(STATUS_PHANTOM, FALSE);
130 llSetStatus(STATUS_PHYSICS, TRUE);
131 //instruct the avatars
132 llSetText("Touch me and try to catch me!", <llFrand(1.1),llFrand(1.1),llFrand(1.1)>, 1.0);
133 AvatarKey = "";
139 link_message( integer sender_num, integer num, string str, key id )
141 //if the avatar is in sight, try to run away
142 if( num == E_AGENT_POSITION && AvatarKey != "")
144 vector targetPosition = (vector)str;
145 vector newPosition = llGetPos();
147 //determine the new position
148 newPosition.x = runawayFromTarget( newPosition.x, targetPosition.x );
149 newPosition.y = runawayFromTarget( newPosition.y, targetPosition.y );
150 newPosition.z = newPosition.z + myRand(MaxStep);
151 newPosition = checkCageLimit( newPosition );
152 //look at the target
153 llLookAt( newPosition, Force, Dump );
154 //move
155 llMoveToTarget( newPosition, Speed );
158 //if the avatar touches us, the game starts if we're not already playing...
159 if( num == E_TOUCH_ID && AvatarKey == "")
161 llShout(0, "Let's see if you can catch me human! You got " + (string)GameDuration + " seconds");
162 AvatarKey = id;
163 //just in case other script have overruled us...
164 llSetStatus(STATUS_PHANTOM, FALSE);
165 llSetStatus(STATUS_PHYSICS, TRUE);
166 llSetTimerEvent(GameDuration);
173 //in case the avatar don't touch the creature within the given time
174 timer()
176 //the creature wins
177 llShout(0, "I won, you're too slow human!");
178 AvatarKey = "";
179 //and get a lot of energy for this
180 llMessageLinked(LINK_SET, R_MOD_ENERGY, (string)VictoryEnergy, "");
181 //kill the timer too
182 llSetTimerEvent(0);
188 //if a collision occurs
189 collision_start(integer numberDetected)
191 //if we're paying
192 if( AvatarKey != "" )
194 integer i = 0;
195 for( ; i < numberDetected; i++)
197 //if one of the colliders is the avatar we're playing with
198 if( llDetectedKey(i) == AvatarKey )
200 //the avatar wins
201 llSay(0, "You won! Very fast human art thou");
202 AvatarKey = "";
203 //but the creature get a bit of energy anyway
204 llMessageLinked(LINK_SET, R_MOD_ENERGY, (string)DefeatEnergy, "");
205 //game over
206 llSetTimerEvent(0);