Need a bit of help with making a gun in my work

Hi all! I created an account primarily for this question, but I hope to be able to contribute to this community in the future.

I’ve been working on trying to figure a gun into my work, so I can have some tension with an enemy character (working on that). I’ve got the gun created and workable, roughly. I’m going to present a rough code sequence that is intended to showcase the gun. This has come about thanks to some searching on this very forum and some finagling of my own.

[code]“Shooting Range” by Anubis

Testing Room is a room. “This is the room Anubis has made to test out his awesome gun functionality.”

The target is scenery in the testing room. “This is a target painted on the wall. Don’t worry, the wall can take your shots.”

A gun is a kind of thing. A gun can be loaded, half-loaded, or unloaded.

Definition: A thing is ungunlike if it is not a gun.

Understand “shoot [gun] at [something ungunlike]” as shooting it with. Understand “shoot [something] with [something]” as shooting it with.

Understand “shoot [something] at [something]” as shooting it with (with nouns reversed). Understand “fire [gun] at [something ungunlike]” as shooting it with (with nouns reversed). Understand “fire at [something ungunlike] with [gun]” as shooting it with. Understand “fire at [something] with [something]” as shooting it with.

Shooting it with is an action applying to two things.

Check shooting something with something:
if the player is not carrying a gun, say “Shoot? With what gun?” instead;
if the noun is the player, say “That would be a spectacularly horrible idea.[if pistol is unloaded] Besides, the pistol’s empty.[end if]” instead;
if the second noun is not a gun, say “Try a gun, you dope.” instead.

Carry out shooting the target with the pistol:
if the pistol is loaded:
now the pistol is half-loaded;
say “BANG! Bulls-eye!”;
reject the player’s command;
if the pistol is half-loaded:
now the pistol is unloaded;
say “BANG! Bulls-eye!”;
reject the player’s command;
if the pistol is unloaded:
say “It’s out of ammo!”;
reject the player’s command.

The pistol is in the testing room. The pistol is a gun. The pistol is loaded. The description of the pistol is “It’s a compact two-barrelled pistol, meant to fit in one’s pocket for emergencies. [if the pistol is loaded]Both barrels are loaded[otherwise if the pistol is half-loaded]One barrel is fired, and one is still loaded[otherwise if the pistol is unloaded]Both barrels are fired and empty[end if].”
[/code]

You can play the above code here to see how it works: http://parchment.googlecode.com/svn/trunk/parchment.html?story=https%3A//dl.dropboxusercontent.com/u/38920383/Shooting%2520Range.zblorb

So far, so good? I’d say so. My problem is this: I feel like I’ve built up code on code, and I’m not sure how to streamline it. I’d like to make the individual “loaded/half-loaded/unloaded” checking be something that happens every time someone tries to use the pistol, regardless of what one is shooting at. Any ideas?

I may be qualified to address this, as I have written games with guns in them, but the guns in my games have never contained more than one bullet. Two shots is straining the limits of my expertise somewhat.

REGARDLESS. Here’s what I would do:

  • Instead of the qualities “loaded” and “half-loaded”, I’d give the gun a number (“A gun has a number called bullets.”) corresponding to how many more times it can be shot. Besides being usefully applicable to various different firearms, this gives you access to a neat Inform phrase:

  • Instead of “now the pistol is half-loaded” or “now the pistol is unloaded”, you can say “decrement the bullets of the pistol”. Now you don’t need separate clauses for each state of the pistol! You still need an exception for zero bullets, but as long as the number is greater than zero, you can just subtract one.

  • The phrase “reject the player’s command” will prevent later sections of the rule from affecting gameplay, but it can have some unpleasant side-effects. For example, an “after shooting Maria’s dog…” rule will never trigger, because the player can never successfully shoot Maria’s dog, even if the text before “reject the player’s command” line says that they did. Instead, you can use the phrase “rule succeeds”, or put “otherwise” in front of later “if:” blocks. I ended up making a separate “check shooting…” rule.

In Captain Verdeterre’s Plunder, I wrote a separate “after shooting…” rule for every single noun. I thought that was necessary, because some objects ceased to exist after being shot, some objects couldn’t be shot because the game wouldn’t let you, and so on. I could have generalized it a little though, and to atone for my sins I’m going to generalize for those cases in this example:


"Working with Firearms" by Ryan Veeder

Firing Range is a room. "This is the room Ryan has made to back up some claims about knowing how to implement guns in Inform 7."

A gun is a kind of thing. A gun has a number called bullets.

The pistol is here. The pistol is a gun. The bullets of the pistol is 2.

Definition: A thing is ungunlike if it is not a gun.

Understand "shoot [gun] at [something ungunlike]" as shooting it with (with nouns reversed). Understand "shoot [something] with [something]" as shooting it with.

Understand "shoot [something] at [something]" as shooting it with (with nouns reversed). Understand "fire [gun] at [something ungunlike]" as shooting it with (with nouns reversed). Understand "fire at [something ungunlike] with [gun]" as shooting it with. Understand "fire at [something] with [something]" as shooting it with.

Shooting it with is an action applying to two things.

Check shooting something with something:
	if the player is not carrying a gun, say "Shoot? With what gun?" instead;
	if the noun is the player, say "That would be a spectacularly horrible idea.[if the bullets of the second noun is 0] Besides, [the second noun] is empty.[end if]" instead;
	if the second noun is not a gun, say "Try a gun, you dope." instead.


A thing can be precious.

A thing has some text called preciousreason. The preciousreason of a thing is usually "You can't shoot that, for reasons that should be obvious."

A thing can be fragile.

A thing has some text called smithereens. The smithereens of a thing is usually "It explodes into a zillion pieces!"

Check shooting something with a gun:
	if the bullets of the second noun is 0:
		say "You're empty." instead;
	otherwise if the noun is precious:
		say "[the preciousreason of the noun][paragraph break]" instead;

Carry out shooting something with a gun:
	decrement the bullets of the second noun;
	say "Nice shot! You fire right into the center of [the noun].";
	if the noun is fragile:
		say "[the smithereens of the noun][paragraph break]";
		remove the noun from play;

The crash test dummy is here.

The delicate ice sculpture is here. The delicate ice sculpture is fragile.

The priceless heirloom is here. The priceless heirloom is precious.

I’ve put this up at Playfic here.

If I had used something like this for Verdeterre, I would only have had to write “after shooting…” rules for a minority of especially weird targets. For the rest, I could have just said: The preciousreason of the whatsit is “The whatsit is too cute for you to wish to harm it.”—and: The smithereens of the glass vial is “The vial explodes.”—and that kind of thing. Oh well.

“reject the player’s command” should only be used in a “reading the player’s command” rule. (It’s actually just a synonym for “rule fails”. If you want an action rule to fail, say that directly – less confusing.)

Thanks Afterward for the tip on making ammunition counters. I’m probably going to keep the gun a two-shooter in this project, for purposes of dramatic tension, but it’ll be useful in future works.

I put the “reject command” in to get it to stop carrying out the ifs for loaded, half-loaded, then unloaded, all in one turn. The coding at that time was the same as it is now except for the “reject command” lines being gone. What can I replace them with to avoid the glitches Afterward mentioned?

An “instead” or “stop the action;”

You can also work around it by checking for the possibilities in reverse order:

If the pistol is unloaded:
     say "You're empty.";
If the pistol is half-loaded:
     say "You fire the last bullet.";
     now the pistol is unloaded;
If the pistol is loaded:
     say "You fire the first of two bullets.";
     now the pistol is half-loaded;

But this is more cute than it is a good idea.

An artistic suggestion… the pistol you’re describing is better known as a derringer. Personally, I like to write detailed descriptions of objects for curious players to explore. I think it helps with the immersion. So…

INVENTORY
You are carrying:
A pistol.
A box of cartridges.

EXAMINE PISTOL
It’s an old Remington Model 95 derringer, a very small two-barreled pistol intended as a last-ditch weapon. It takes .41-short rimfire cartridges.

EXAMINE BOX
It’s a small box containing 13 .41-short rimfire cartridges.