Home |
Add a script
|
||
|---|---|---|---|
| Category: | Contributor: | Description | |
| Door | Zanlew Wu | Door Script with timer.lsl | |
Door Script with timer.lsl
Door Script with timer.lsl
Download this script - Please use this link to get this script. If you see all the code on one long line, please use Wordpad or another editor, such as LSLEdit.exe. The .LSL file you will download is an ordinary text file.
1 // CATEGORY:Door 2 // DESCRIPTION:Door Script with timer.lsl 3 // ARCHIVED BY:Ferd Frederix 4 5 // ========================================================================== 6 // Basic Door Script 7 // Original Script By Unknown 8 // Modifications made by Zanlew Wu 01-Apr-2003 9 // 10 //============================================ 11 // Declare global constants. 12 // 17 // 18 // Note that it is hard to call a given swing outward or inward as that has 19 // a lot to do witht he rotation and/or orientation of the door, which 20 // swing direction is correct/desired, and whether you are referring to the 21 // swing from the "out" side of the door or the "in" side of the door. It 22 // was easier by convention to call the swings normal and reverse. 23 24 //============================================ 25 // Declare global fields. 26 // 27 key gfOwnerKey; // Owner of the elevator object 28 integer gfDoorClosed; // Current state of the door (Open, Closed) 29 integer gfDoorSwing; // Deteremines which way the door swings (In, Out) 30 31 //============================================ 32 // gmInitFields 33 // 34 gmInitFields() 35 { 36 // 37 // Get the owner of the door. 38 // 39 gfOwnerKey = llGetOwner(); 40 41 // 42 // Close doors by default. 43 // 44 gfDoorClosed = TRUE; 45 46 // 47 // Set the door swing. 48 // 49 gfDoorSwing = SW_NORMAL; 50 51 return; 52 } 53 // 54 // End of gmInitVars 55 //============================================ 56 57 //============================================ 58 // gmSwingDoor 59 // 60 gmSwingDoor(integer direction) 61 { 62 //----------------------- 63 // Local variable defines 64 // 65 rotation rot; 66 rotation delta; 67 float piVal; 68 69 // 70 // First thing we need to do is decide whether we are applying a 71 // negative or positive PI value to the door swing algorythm. The 72 // positive or negative makes the difference on which direction the door 73 // swings. Additionally, since we allow the doors to modify their swing 74 // direction (so the same door can be placed for inward or outward 75 // swing, we have to take that into account as well. Best to determine 76 // that value first. The rest of the formula does not change regardless 77 // of door swing direction. 78 // 79 // So we have two variables to pay attention to: open/close and swing 80 // in/out. First we start with open/close. We will presume the 81 // following: 82 // SW_OPEN: +PI 83 // SW_CLOSE: -PI 84 // This also presumes that the door has a standard swing value of 85 // SW_NORMAL. 86 // 87 // A door that has had it's swing changed would have those values 88 // reversed: 89 // SW_OPEN: -PI 90 // SW_CLOSE: +PI 91 // 92 // The variable passed into this method determines if the intent were 93 // to open the door or close it. 94 // 95 // The global field gfDoorSwing will be used to modify the PI based on 96 // whether the door normally swings in or out. 97 // 98 if (direction == SW_OPEN) 99 {100 //101 // Ok, we know the door is opening. Assign a +PI value to piVal.102 //103 piVal = PI/4;104 //105 // Now check to see if the door has it's swing reversed.106 //107 if (gfDoorSwing == SW_REVERSE)108 {109 //110 // Yep, it's reversed and we are opening the door, so replace 111 // piVal with a -PI value.112 //113 piVal = -PI/4;114 }115 } else116 {117 //118 // So we know we are closing the door this time. Assign a -PI value119 // to piVal.120 //121 piVal = -PI/4;122 //123 // Now check to see if the door has it's swing reversed.124 //125 if (gfDoorSwing == SW_REVERSE)126 {127 //128 // Yep, it's reversed and we are closing the door, so we need to129 // assing a +PI value to piVal.130 //131 piVal = PI/4;132 }133 }134 135 //136 // This formula was part of the original script and is what makes137 // the door swing open and closed. This formula use a Pi/-Pi to 138 // move the door one quarter-circle in total distance.139 //140 // The only change I've made to this function is to replace the hard-142 // programmatically to suit the operation at hand. 143 //144 rot = llGetRot();145 delta = llEuler2Rot(<0,-piVal,0> );146 rot = delta * rot;147 llSetRot(rot);148 llSleep(0.25); 149 rot = delta * rot;150 llSetRot(rot);151 152 return;153 }154 //155 // End of gmSwingDoor156 //============================================157 158 //============================================159 // gmCloseDoor160 //161 // The close command is used to close doors. If the doors are 162 // locked, the doors cannot be closed. (Note: presumably, this 163 // script does not allow doors to be opened AND locked at the same 164 // time). If the doors are already closed, they cannot be 165 // re-closed. These checks will be made before performing a door 166 // close operation. Once the door is successfully closed, the 167 // door's state will be updated.168 //169 gmCloseDoor()170 {171 //172 // First let's check to see if the door is already closed. If it 173 // is, let the user know.174 //175 if (gfDoorClosed == TRUE) 176 {177 //178 // Yep, it was already closed.179 //180 llSay (0, "This door is already closed.");181 return;182 }183 184 //185 // Now we generate the proper sound for the door closing.186 //187 llTriggerSound("open_creaky_door", 0.2);188 189 //190 // Now we call the method gmSwingDoor with the SW_CLOSE argument (since191 // we are closing the door.192 // 193 gmSwingDoor(SW_CLOSE); 194 195 //196 // Now that the door is closed, set the door's state.197 //198 gfDoorClosed = TRUE;199 200 return;201 }202 //203 // End of gmCloseDoor204 //============================================205 206 //============================================207 // gmOpenDoor208 //209 // The open command is used to open the doors. If the doors are 210 // locked, the doors cannot be opened. If the doors are already 211 // opened, they cannot be re-opened. These checks will be made 212 // before performing a door open operation. Once the door is 213 // successfully opened, the door's state will be updated.214 //215 gmOpenDoor()216 {217 //218 // First let's check to see if the door is open already. If it is,219 // let the user know.220 //221 if (gfDoorClosed == FALSE) 222 {223 //224 // Yep, it was already open.225 //226 llSay (0, "This door is already open.");227 return;228 }229 230 //231 // Now we generate the proper sound for the door closing.232 //233 llTriggerSound("open_creaky_door", 0.2);234 235 //236 // Now we call the method gmSwingDoor with the SW_OPEN argument (since237 // we are opening the door.238 // 239 gmSwingDoor(SW_OPEN);240 241 //242 // Now that the door is opened, set the door's state.243 //244 gfDoorClosed = FALSE;245 return;246 }247 //248 // End of gmOpenDoor249 //============================================250 251 252 //============================================253 // Default State254 //255 // This is the state that is automatically bootstrapped when the object256 // is first created/rez'd, or the world or environment resets.257 // 258 default259 {260 //261 // state_entry() is the first method executed when the state it resides262 // in is run. So State A, B, and C all can have state_entry methods,263 // and if they do, they are run when their respective states are called264 // and or executed.265 //266 state_entry()267 {268 //269 // Perform global field initialization270 //271 gmInitFields();272 273 //274 // We are listening for two different commands. This script is set 275 // up to accept spoken commands only from the object owner.276 //277 llListen(999, "", "", "");278 279 }280 283 {284 285 //llOwnerSay("door heard :" + msg);286 287 //-----------------------288 // Local variable defines289 //290 string operName;291 string ownerName;292 293 //294 // Ideally, we want the door only to work on spoken commands 295 // from the owner. To accomplish this task, we need to check the 296 // id of the owner and the person issuing the command to see if 297 // they match.298 //299 // Alternately, commands can be issued from the control panel, 300 // which can be used by anyone. Later on, it will be presumed that301 // access to the control panel will be controlled.302 //303 304 //305 // First get the string names of the owner and the operator so they306 // can be compared.307 //308 operName = llKey2Name(id);309 ownerName = llKey2Name(gfOwnerKey);310 311 312 313 //----------------------------------------314 // OPEN DOOR315 //316 if(msg == "open") 317 { 318 gmOpenDoor();319 }320 321 //----------------------------------------322 // CLOSE DOOR323 //324 if (msg == "close")325 {326 gmCloseDoor();327 }328 }329 330 }331 // END //