Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
OpenSim NPC NPC Attacker  

NPC Attacker

This is a NPC attacker for Opensim. I use it in a RAM to butt people.
License: http://creativecommons.org/licenses/by-nc

Category: OpenSim NPC
By : Ferd Frederix
Created: 2015-08-28 Edited: 2015-08-28
Worlds: OpenSim

the Zip file

Download all files for NPC Attacker
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Script.lsl

This script by Ferd Frederix may be used in any manner, modified, and republished.  Unless specified otherwise, my scripts are always free and open source.  Objects made with these scripts may be sold with no restrictions.  All I ask is that you point others to this location should they ask you about it and to not sell this script, unless it is for $0 L. Please help improve my work by reporting bugs and improvements.

1
2 // 8-1-2014 for Rambo, the attack Ram
3 // 8-11-2014 make a round prim, set it to phantom, add this script. Yotouch it to record your appearance as a RAM
4 // Set the prim to 4 X 4 meters and the Ram will stay ontop of it and attack anyone that steps on it.
5
6 integer debug = FALSE;
7 key avatarKey;
8 vector vRandSpot ;
9
10 DEBUG(string msg)
11 {
12 if(debug)
13 llOwnerSay(msg);
14 }
15
16 // tunables
17 float max_distance; //distance for sensoring a real avatar
18 string fName = "Cobra";
19 string lName = "";
20 float SenseTime = 10;
21 integer JamTimer = 10;
22
23 // OS_NPC_CREATOR_OWNED will create an 'owned' NPC that will only respond to osNpc* commands issued from scripts that have the same owner as the one that created the NPC.
24 // OS_NPC_NOT_OWNED will create an 'unowned' NPC that will respond to any script that has OSSL permissions to call osNpc* commands.
25 integer NPCOptions = OS_NPC_CREATOR_OWNED; // only the owner of this box can control this NPC.
26 integer iWaitCounter ; // 10 second attack counter
27 integer iListenChannel; // dialog channel
28 integer iListenHandle; // holds the listener handle;
29 key kNPCKey; // npc key
30 integer off = FALSE;
31 string lastAnim; // thw last animation played
32 vector vSensedPos ; // the last place we sensed an avatar
33 vector vDestination; // where we are headed
34
35 // animations list
36 string Stand = "SnakeStand";
37 string Idle = "SnakeStand";
38 string Run = "SnakeStand";
39 string Walk = "SnakeWalk";
40
41 // Sounds
42 string AttackSound = "Hiss";
43 string YouLose = "sad-trombone";
44
45 Restart() {
46 // we must have fallen down and can't get up
47
48 osNpcRemove((key) KeyValueGet("key"));
49
50 // reboot
51
52 Start();
53 llSleep(2);
54 iWaitCounter = JamTimer;
55
56
57 }
58 // this function is Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
59
60 string KeyValueGet(string var) {
61 list dVars = llParseString2List(llGetObjectDesc(), ["&"], []);
62 do {
63 list data = llParseString2List(llList2String(dVars, 0), ["="], []);
64 string k = llList2String(data, 0);
65 if(k != var) jump continue;
66 //DEBUG("got " + var + " = " + llList2String(data, 1));
67 return llList2String(data, 1);
68 @continue;
69 dVars = llDeleteSubList(dVars, 0, 0);
70 } while(llGetListLength(dVars));
71 return "";
72 }
73 // this function is Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
74 KeyValueSet(string var, string val) {
75
76 //DEBUG("set " + var + " = " + val);
77 list dVars = llParseString2List(llGetObjectDesc(), ["&"], []);
78 if(llGetListLength(dVars) == 0)
79 {
80 llSetObjectDesc(var + "=" + val);
81 return;
82 }
83 list result = [];
84 do {
85 list data = llParseString2List(llList2String(dVars, 0), ["="], []);
86 string k = llList2String(data, 0);
87 if(k == "") jump continue;
88 if(k == var && val == "") jump continue;
89 if(k == var) {
90 result += k + "=" + val;
91 val = "";
92 jump continue;
93 }
94 string v = llList2String(data, 1);
95 if(v == "") jump continue;
96 result += k + "=" + v;
97 @continue;
98 dVars = llDeleteSubList(dVars, 0, 0);
99 } while(llGetListLength(dVars));
100 if(val != "") result += var + "=" + val;
102 }
103
104
105 Play(string anima)
106 {
107 if(anima == lastAnim)
108 return;
109
110 if(anima == Stand && llFrand(3) < 1)
111 {
112 anima = Idle;
113 }
114
115 DEBUG(anima);
116 osNpcPlayAnimation(kNPCKey,anima);
117 osNpcStopAnimation(kNPCKey,lastAnim);
118 lastAnim = anima;
119 }
120
121 Sound()
122 {
123 if(llFrand(2) > 0.5)
124 return;
125
127 integer n2 = llCeil(llFrand(n));
128
130 llTriggerSound(name,1.0);
131 DEBUG(name);
132 }
133
134
135 GoTo(vector myPos)
136 {
137 DEBUG("Walk to " + (string) myPos);
138 myPos.z += .5;
139
140 osNpcMoveToTarget(kNPCKey, myPos, OS_NPC_NO_FLY );
141 }
142
143 float randBetween(float min, float max)
144 {
145 return llFrand(max - min) + min ;
146 }
147
148
149 dialog(key avi)
150 {
151 iListenChannel = llCeil(llFrand(5000) + 5000);
152 iListenHandle = llListen(iListenChannel,"","","");
153 llDialog(avi, "Choose:",["Start","Remove","Save Appearance"],iListenChannel);
154 }
155
156 Start()
157 {
158 vector size = llGetScale();
159 max_distance = size.x/2;
160 iWaitCounter = JamTimer;
161 vDestination = llGetPos();
162
163
164 osNpcRemove((key) KeyValueGet("key"));
165 llSleep(1);
166 vector npcPos = llGetPos() + <0.0,0.0,1.0>;
167 kNPCKey = osNpcCreate(fName,lName, npcPos, "Appearance",NPCOptions);
168 KeyValueSet("key",(string) kNPCKey);
169 off = FALSE;
170 llSensorRepeat("", NULL_KEY, AGENT, max_distance, PI,SenseTime);
171 }
172
173
174 vector CirclePoint(float radius) {
175 float x = llFrand(radius *2) - radius; // +/- radius, randomized
176 float y = llFrand(radius *2) - radius; // +/- radius, randomized
177 return <x, y, 0>; // so this should always happen
178 }
179
180
181 default
182 {
183
185 {
186 Start();
187 }
188
189 changed (integer what)
190 {
191 if(what & CHANGED_REGION_RESTART)
192 {
193 Start();
194 }
195 }
196
198 {
200 }
201
203 {
204 if(llDetectedKey(0) == llGetOwner()) {
205 dialog(llDetectedKey(0));
206 }
207 }
208
209 listen(integer channel, string name, key id, string msg) {
210 llListenRemove(iListenHandle);
211 if(msg == "Start")
212 {
213 if( off ) {
214 Start();
215 } else {
216 llOwnerSay("Already running");
217 }
218 } else if(msg == "Remove") {
219 off = TRUE;
220 osNpcRemove((key) KeyValueGet("key"));
222 } else if(msg == "Save Appearance") {
223 osAgentSaveAppearance(llGetOwner(), "Appearance");
224 llOwnerSay("Your appearance has been saved");
225 }
226
227 }
228
229 sensor(integer num)
230 {
231 DEBUG("sensed");
232
233 vector Dest = llDetectedPos(0);
234 avatarKey = llDetectedKey(0);
235 iWaitCounter = JamTimer; // 10 second attacks
236
237 vSensedPos = Dest; // make a backup, no trashing the 'real' Dest we will walk to
238 vSensedPos.z = 0;
239
240 GoTo(Dest);
241 llPlaySound(AttackSound,1.0);
242
243 llSetTimerEvent(2); // check Positions every couple of seconds
244
245 }
246
247 no_sensor()
248 {
249 DEBUG("noone");
250
252
253 list npcPosition = llGetObjectDetails(kNPCKey,[OBJECT_POS]);
254 vector vNpcPos = llList2Vector(npcPosition, 0);
255
256 float Distance = llVecDist(vNpcPos, vDestination ) ;
257 DEBUG("Distance: " + (string) Distance);
258
259 if(Distance > 1.0) {
260
261 Play(Walk);
262 DEBUG("idlectr: " + iWaitCounter);
263 if(! --iWaitCounter) {
264 Restart();
265 }
266
267 } else {
268 // new destination
269 Play(Idle); // munchies
270 vRandSpot = CirclePoint(max_distance);
271 vDestination = llGetPos() + vRandSpot;
272 iWaitCounter = JamTimer;
273 }
274 GoTo(vDestination);
275 }
276
277
278 timer()
279 {
280
281 llPreloadSound(YouLose);
282 Play(Run);
283
284 // we only have so many seconds to get there
285 if(! --iWaitCounter) {
286 Restart();
287 }
288
289 list npcPosition = llGetObjectDetails(kNPCKey,[OBJECT_POS]);
290 vector vNpcPos = llList2Vector(npcPosition, 0);
291 vNpcPos.z = 0; // ignore height by using z = 0
292
293 list aviPosition = llGetObjectDetails(avatarKey,[OBJECT_POS]);
294 vector vAviPos = llList2Vector(aviPosition, 0);
295 vAviPos.z = 0; // ignore height by using z = 0
296
297
298 if(llVecDist(vNpcPos, vAviPos ) < 1.5 ) {
299
300 llTriggerSound(YouLose,1.0);
301 DEBUG("Dead");
302 llSleep(5.0);
303 llMessageLinked(LINK_SET,0,"DEAD",avatarKey);
304 Play(Stand); // cancel the hot ramming action
305 }
306 }
307 }

Back to the Best Free Tools in Second Life and OpenSim.