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
Weapons Swarm_Script  

Swarm_Script

Swarm Script.lsl

Category: Weapons
By : Apotheus Silverman
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Swarm_Script
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Swarm_Script_1.lsl
Get file # 2. Swarm_Script_Instructions_NOTECARD_1.lsl
1
2 // Swarm script
3 // by Apotheus Silverman
4 // with mods from Riptide Ramos
5 // This script is my implementation of the well-known swarm algorithm
6 // which can be found in numerous open-source programs.
7 // Due to the specifics of the SL environment, I have strayed from some
8 // of the traditional rules slightly. Regardless, the end effect is
9 // indistiguishable from the original algorithm.
10
11 // Configurable parameters
12
13 // Determines whether or not to enable STATUS_SANDBOX.
14 integer sandbox = FALSE;
15
16 // Timer length
17 float timer_length = 0.7;
18
19 // Die after this many seconds
20 integer kill_time = 300;
21
22 // How much force to apply with each impulse
23 float force_modifier = 1.0;
24
25 // How much force to apply when repulsed by another like me
26 float repulse_force_modifier = 0.5;
27
28 // How much friction to use on a scale from 0 to 1.
29 // Note that friction takes effect each timer cycle, so the lower the timer length,
30 // the more the friction you specify here will take effect, thereby increasing actual
31 // friction applied.
32 float friction = 0.6;
33
34 // How much to modify rotation damping. Higher numbers produce slower rotation.
35 float rotation_modifier = 80;
36
37 // Does this object "swim" in air or water?
38 // 2 = air
39 // 1 = water
40 // 0 = both
41 integer flight_mode = 2;
42
43 // *** Don't change anything below unless you *really* know what you're doing ***
44
45
46 // Collision function
47 collide(vector loc) {
48 vector mypos = llGetPos();
49 float mass = llGetMass();
50 // Apply repulse force
51 vector impulse = llVecNorm(mypos - loc);
52 llApplyImpulse(impulse * repulse_force_modifier * mass, FALSE);
53 // Update rotation
54 llLookAt(mypos + llGetVel(), mass * 0.5, mass * rotation_modifier);
55 }
56
57 // This function is called whether the sensor senses anything or not
58 sensor_any() {
59 // Die after reaching kill_time
60 if(kill_time != 0 && llGetTime() >= kill_time) {
61 llDie();
62 }
63
64 // Get my position and mass
65 vector mypos = llGetPos();
66
67 // Check for air/water breach
68 if(flight_mode == 1) {
69 // water
70 if(mypos.z >= llWater(mypos)) {
71 collide(<mypos.x, mypos.y, mypos.z + 0.3> );
72 }
73 } else if(flight_mode == 2) {
74 // air
75 if(mypos.z <= llWater(mypos)) {
76 collide(<mypos.x, mypos.y, mypos.z - 0.3> );
77 }
78 }
79 }
80
81
82 default {
83 state_entry() {
84 llSay(0, "Fishy spawned.");
85
86 // Sandbox
87 llSetStatus(STATUS_SANDBOX, sandbox);
89
90 // Initialize physics behavior
91 llSetBuoyancy(1.0);
93 llSetStatus(STATUS_PHANTOM, FALSE);
94
95 // Initialize sensor
97 }
98
99
100 collision_start(integer total_number) {
101 collide(llDetectedPos(0));
102 }
103 land_collision_start(vector position) {
104 vector mypos = llGetPos();
105 collide(mypos - llGroundNormal(mypos));
106 }
107
108 no_sensor() {
109 sensor_any();
110 }
111
112 sensor(integer total_number) {
113 sensor_any();
114
115 // Populate neighbors with the positions of the two nearest neighbors.
116 vector mypos = llGetPos();
117 float mass = llGetMass();
118 list neighbors = [];
119 integer i;
120
121 vector v1;
122 vector v2;
123 float d1 = 100;
124 float d2 = 100;
125
126 for (i = 0; i < total_number; i++) {
127 vector current_pos = llDetectedPos(i);
128 float cur_dist = llVecDist(mypos, current_pos);
129 if( cur_dist < d1 ) {
130 // Shift list down, take over top slot.
131 d2 = d1;
132 v2 = v1;
133 d1 = cur_dist;
134 v1 = current_pos;
135 } else if( cur_dist < d2 ) {
136 // Replace second slot only
137 d2 = cur_dist;
138 v2 = current_pos;
139 }
140 }
141
142 // Process movement
143
144 // Apply friction
145 llApplyImpulse(-(llGetVel() * friction * mass), FALSE);
146
147 // Apply force
148 if(llGetListLength(neighbors) == 2) {
149 vector neighbor1 = llList2Vector(neighbors, 0);
150 vector neighbor2 = llList2Vector(neighbors, 1);
151 vector target = neighbor2 + ((neighbor1 - neighbor2) * 0.5);
152 vector impulse = llVecNorm(target - mypos);
153 llSetForce(impulse * force_modifier * mass, FALSE);
154 }
155
156 // Update rotation
157 llLookAt(llGetPos() + llGetVel(), mass * 0.5, mass * rotation_modifier);
158 }
159
160 on_rez(integer start_param) {
162 }
163 }
164 // END //

Swarm_Script

Swarm Script Instructions [NOTECARD].lsl

Category: Weapons
By : Apotheus Silverman
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1
2 The swarm algorithm in its purest sense creates any number of things that are attracted to each other. Any objects you tie this script to (after modifying the configurable parameters according to your object size, desired speed, etc) behave like social animals. That is, they swarm/flock/school... whatever you want to call it. None of generated movement is random, though it may appear to be.
3
4 Swarm rules for each swarm object to follow:
5 1. Accelerate toward the halfway point between your two nearest neighbors.
6 2. Upon collision, momentarily be repulsed by whatever object or ground was collided with.
7 3. If no neighbors are detected, just keep moving in a straight line at a constant speed. If I go off-world, then so be it.
8
9 The traditional swarm algorithm actually has a few more rules than this, but those rules are rather specific to a single application handling the entire swarm rather than multiple state engines working together.
10
11
12 The best way of using this script that I have found is to create your swarm object... let's say you want to make a school of fish. Create your fish model. Add the script into your fish.
13
14 Now comes the tricky part: getting the parameters just right. The swarming behavior does not occur until there are at least 3 of your swarm object within 96 meters of each other. When you have the parameters adjusted and are ready to test your swarm, you must rez at least 3 of the current object. An easy way to do this is to be in edit mode and hit ctrl+d twice. Make sure you take a copy of your object BEFORE you start testing, as incorrectly configured objects tend to fly away very fast! You can also set sandbox to TRUE so they don't get too far away.
15
16 Once you have your swarm behaving the way you want it to, make sure you have a timeout set (I generally use 5 minutes) on the swarm object, then create another object that will be the main center point of your swarm. This object will rez new swarm objects every N seconds. This ensures you always have a swarm that is relatively nearby the rezzing object, and in conjunction with the swarm object timeout, ensures you always have a relatively constant number of swarm objects rezzed. The script code for your rezzing object should be something like this (ignore errors... i'm at work right now and don't have access to SL to test this):
17
18
19
20 code:--------------------------------------------------------------------------------
21 default {
22 state_entry() {
24 }
25 timer() {
26 // Rez in a random location within a 10m cube. Don't use 5 in lieu of 5.0 or you could end up with slightly odd results due to the automatic conversion to integer instead of float.
27 vector mypos = llGetPos();
28 mypos.x += llFrand(10) - 5.0;
29 mypos.y += llFrand(10) - 5.0;
30 mypos.z += llFrand(10) - 5.0;
31 llRezObject("swarm object", mypos, <0,0,0,1>,<0,0,0> );
32 }
33 }
34 --------------------------------------------------------------------------------
35
36
37 Once you apply this script to your rezzing object, add your swarm object to its contents and let it go. Congratulations, you now have a completely self-maintaining swarm.
38
39 Oh and please please please please please do not create huge swarms with a ton of swarm objects and just let it stay that way. You'll dump the sim's script and physics performance right in the toilet and end up making all your neighbors angry. Just as an FYI I had a swarm of 25 going in Cordova and did not see any slowdown or performance loss at all, but your mileage may vary.
40
41 Since it is quite obvious that creating a swarm is not something to be tackled by a non-scripter or even someone who doesn't have much patience, I will be selling my swarm creations soon at my new home in Abbots for a small fee. Since the code is open-source, I will also allow full copy/mod/transfer perms to purchasers.// END //

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