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 Revolver_Script  

Revolver_Script

Revolver Script.lsl

Category: Weapons
By : John Linden
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Revolver_Script
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Revolver_Script_1.lsl
1
2 // Gun script
3 // John Linden
4
5 // where the gun attaches itself if you click on it
6 // switch this to ATTACH_LHAND if you are left-handed
7 integer gAttachPart = ATTACH_RHAND;
8
9 string INSTR_HELD_1 = "";
10 string INSTR_HELD_2 = "";
11
12 string INSTR_NOT_HELD_1 = "";
13 string INSTR_NOT_HELD_2 = "";
14
15 string INSTR_NOT_OWNER_1 = "";
16 string INSTR_NOT_OWNER_2 = "";
17
18 // this is a permission combination we check for many times
19 // I define it here so we don't have to keep typing it
20 // guns need to take controls, attach themselves, and possibly
21 // trigger animations
22 // NOTE: since I can't initialize to an expression (even if it is
23 // all constants), this moved to default { state_entry() }
24 integer gDesiredPerm;
25 // Indicates whether wearer has yet given permission
26 integer gHavePermissions;
27
28 // Bullet travel speed
29 float gBulletSpeed = 30.0;
30
31 // when we last fired a shot
32 float gLastFireTime = 0;
33 // how long it takes to reload when tapping
34 float gTapReloadTime = 0.05;
35 // how long it takes to reload when holding
36 float gHoldReloadTime = 1.0;
37 // how long it takes when we're out of ammo
38 float gOutOfAmmoReloadTime = 1.4;
39 // how many shots before reload is necessary
40 integer gAmmoClipSize = 6;
41 // how much ammo we have right now
42 integer gAmmo;
43
44 // HACK: how far away the eye is from the avatar's center (approximately)
45 vector gEyeOffset = <0.0, 0.0, 0.84>;
46
47
48 say(string msg)
49 {
50
51 }
52 debug(string msg)
53 {
54 // llSay(0, msg);
55 }
56 func_debug(string msg)
57 {
58 // llSay(0, msg);
59 }
60
61
62 // the gun has only one state
63
64 default
65 {
66 on_rez(integer start_param)
67 {
68
69 // HACK: try to compensate for height of avatar
70 // by changing firing position appropriately
72 gEyeOffset.z = gEyeOffset.z * (size.z / 2.0);
73
74
75
76 // NOTE: can't do this if we want to attach by dragging from
77 // inventory onto the av, because the llResetScript(); clears
78 // all the callbacks, including attach()
79 // llResetScript();
80
81 // NOTE 2: This can be uncommented in 1.1 because you won't have
82 // to ask for permissions, you can just take them. But for now
83 // it pops up two dialog boxes if you drag onto the av, so I'll
84 // leave it out.
85 // Try to attach to the rezzer
86 // if( !gHavePermissions )
87 // {
88 // llRequestPermissions(llGetOwner(), gDesiredPerm);
89 // }
90 }
91
93 {
94 func_debug("default state_entry");
95 gHavePermissions = FALSE;
96 // this should be initialized directly with the variable
97 // but I can't do that due to a bug
98 gDesiredPerm = (PERMISSION_TAKE_CONTROLS
100 // Start with a full clip
101 gAmmo = gAmmoClipSize;
103 }
104
105 // the player has touched us (left-clicked)
106 // give directions
108 {
109 func_debug("touch_start");
110
111 // Guns only work for their owner
112 if( llDetectedKey(0) == llGetOwner() )
113 {
114 if( gHavePermissions )
115 {
116 llWhisper(0, INSTR_HELD_1);
117 llWhisper(0, INSTR_HELD_2);
118 } else
119 {
120 llWhisper(0, INSTR_NOT_HELD_1);
121 llWhisper(0, INSTR_NOT_HELD_2);
122 }
123 } else
124 {
125 // Not the owner
126 llWhisper(0, INSTR_NOT_OWNER_1);
127 llWhisper(0, INSTR_NOT_OWNER_2);
128 }
129 }
130
131 // Player attaches us, either by dragging onto self or from pie menu
132 attach(key av_key)
133 {
134 func_debug("attach");
135
136 if(av_key != NULL_KEY)
137 {
138 // Can't attach if we don't own it
139 if( av_key != llGetOwner() )
140 {
141 llWhisper(0, INSTR_NOT_OWNER_1);
142 llWhisper(0, INSTR_NOT_OWNER_2);
143 return;
144 }
145
146 //
147 // Always request permissions on attach, as we may be re-rezzing on login
148 llRequestPermissions(av_key, gDesiredPerm);
149
150 // run_time_permissions() is executed after this call
151 } else
152 {
153 func_debug(" detach");
154
155 if( gHavePermissions )
156 {
157 // we are being detached
158 llStopAnimation("hold_R_handgun");
160 llSetRot(<0.0, 0.0, 0.0, 1.0>);
161 gHavePermissions = FALSE;
162 }
163 }
164 }
165
166 // this is called whenever llRequestPermissions() returns
167 // i.e. the user has responded or dismissed the dialog
168 // perm is the permissions we now have
170 {
171 func_debug("run_time_permissions");
172
173 // see if we now have the permissions we need
174 if( (perm & gDesiredPerm) == gDesiredPerm )
175 {
176 func_debug(" got perms");
177
178 // we got the permissions we asked for
179 gHavePermissions = TRUE;
181 llStartAnimation("hold_R_handgun");
182 } else
183 {
184 func_debug(" didn't get perms");
185
186 // we didn't get them, kill ourselves
187 llDie();
188 }
189 }
190
191
192 // the player has used the controls, process them
193 control(key owner, integer level, integer edge)
194 {
195 func_debug("control");
196
197 // has our gun reloaded?
198 float time = llGetTime();
199
200 // see if av has fired
201 // (edge & level) == down edges, (edge & !level) == up edges
202 // repeat rate is faster for tapping than holding
203 if( ( ((edge & level) & CONTROL_ML_LBUTTON)
204 && (time > gTapReloadTime) )
205 || ( (time > gHoldReloadTime)
206 && (level & CONTROL_ML_LBUTTON) ) )
207 {
208 // if player is out of ammo, must wait for reload
209 if( (gAmmo >= 1) || (time > gOutOfAmmoReloadTime) )
210 {
211 // bang!
212
213 llResetTime(); // we fired, set timer back to 0
214
215 // HACK: gun shoots from gun pos, mouselook looks
216 // from player's eye, so we try to move the gun
217 // up to the eye. but we really don't
218 // know where it is, so we guess (see gEyeOffset)
219 vector my_pos = llGetPos() + gEyeOffset;
220 rotation my_rot = llGetRot();
221 vector my_fwd = llRot2Fwd(my_rot);
222
223 // Rez a bullet!
224 llRezObject("Shuriken",
225 my_pos,
226 my_fwd * gBulletSpeed,
227 my_rot,
228 1);
229
230 // decrease ammo count
231 --gAmmo;
232 if( gAmmo > 0 )
233 {
234 // still bullets left
235 }
236 else if( gAmmo == 0 )
237 {
238 // we just ran out of shots
239 // play reload sound here after a delay
240
241 }
242 else // gAmmo < 0
243 {
244 // we've just fired first round of new clip
245 gAmmo = gAmmoClipSize - 1;
246 }
247 } else
248 {
249 debug("Can't shoot, reloading...");
250 }
251 }
252
253 } // control
254
255 } // default state
256 // END //

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