Wednesday, March 08, 2006

The Pepperment of Nearby Objects

I am most gratified by the number of people who have mentioned to me, during conversation in Caledon and elsewhere, though mostly in Caledon itself, that they have read this particular "blog" and, amazingly to me, continue to read it and even find it not completely worthless. Dear reader, your correspondent is most flattered indeed, and will certainly try to continue producing whatever it is that people actually come here for.

As to my current activities, well, I have been and continue to be somewhat busy, though not producing an awfully large number of actual products as such. One thing I have done is construct a Pepperbox Pistol for Professor Jefferson Gould. This is a six-barrelled firearm, designed to be used at close range, perhaps across a poker table.

A pepperbox pistol, from both sides 'George Bemis . . . wore in his belt an old original "Allen" revolver, such as irreverent people called a "pepper-box." Simply drawing the trigger back, cocked and fired the pistol. As the trigger came back, the hammer would begin to rise and the barrel to turn over, and presently down would drop the hammer, and away would speed the ball. To aim along the turning barrel and hit the thing aimed at was a feat which was probably never done with an "Allen" in the world. But George's was a reliable weapon, nevertheless, because, as one of the stage-drivers afterward said, "If she didn't get what she went after, she would fetch something else." And so she did. She went after a deuce of spades nailed against a tree, once, and fetched a mule standing about thirty yards to the left of it. Bemis did not want the mule; but the owner came out with a double-barreled shotgun and persuaded him to buy it, anyhow. It was a cheerful weapon--the "Allen." Sometimes all its six barrels would go off at once, and then there was no safe place in all the region round about, but behind it.'
- Mark Twain, "Roughing It"

I have decided to make all of my projectile armaments use (at least) two seperate scripts in the same prim for firing and control, as this eliminates the awkward and somewhat unpredictable sleep after each projectile is rezzed. One script is merely something along the lines of the following:

// Firing sub-script
// Ordinal Malaprop
// 2006-03-08

float gBulletSpeed = 60.0;

link_message(integer c, integer n, string msg, key id)
if (n != 0) return;
vector rot = llGetRot();
vector dir = llRot2Fwd(rot);
llGetPos() + dir + <0.0, 0.0, 0.75>,
dir * gBulletSpeed, rot, 1);

and with the other, whenever one wishes to fire a bullet, one simply calls

llLinkMessage(LINK_THIS, 0, "", NULL_KEY);

from one's main firing script. (One would also likely trigger a sound and activate other special effects.) In the case of the Pepperbox Pistol, the accuracy must be reduced, which is done by modifying the third parameter in the above script - the velocity of the bullet - by a random vector, thus making it travel in a different direction. For completeness it would be better to apply a random factor to the rot variable and thus rez and point the bullet precisely in its direction of travel, but really, in this case, it makes no practical difference.

The other element is to add a chance that the pistol will discharge all of its ammunition at once. For this purpose, we use an llFrand check whenever the trigger is pulled and, if this succeeds, instead of issuing one link message we issue a number equal to the current number of bullets left:

do {
llLinkMessage(LINK_THIS, 0, "", NULL_KEY);
} while (--gAmmo > 0);

The beauty of using link messages for this purpose is that they queue up whilst the fire script is asleep. Recall that after every llRezObject call, there is a short pause, depending on the mass and velocity of the rezzed object. If we had these calls issued from the main script the whole thing would sleep for a while, but given that it doesn't, we can do other things whilst the pistol is chain-firing. Any controls or sensors, for instance, would still activate, say one that detected whether the owner had left mouselook. (In this case there are no other things to be doing, but I can certainly imagine cases where one might.)

One does not wish to have one's master control script ever pausing any more than is absolutely necessary, and this holds even more true when one is considering such particularly sleepy functions as llGiveInventory and, God forbid, llEmail, which pauses for twenty seconds at each invocation. The use of link messages and sub-scripts is a vital tool for any complex scripting, and if I ever become organised enough to put together a scripting class for Withnail Academy, as I have occasionally muttered about doing, I shall certainly devote a lesson or more to it.


Post a Comment

<< Home