(Inform 7) Hoping to Make this Code More Consolidated and Efficient

First off, there’s a fair bit of code to this one. Forgive the length please, if you will. (The length is actually what I’m looking to cut down :wink: )
So I’ve got two snakes: A rattlesnake and a king snake. Both can be killed with either a gun or a shovel. (Tho I still need to work out how to make the ‘attack’ verb take a second noun to specify which to use … but I know that’s somewhere in the manual / examples)
The code works, but it seems a bit inefficient considering that the “Instead” rules for each one are virtually identical. I’m thinking it could all be consolidated (perhaps) into a single “Instead” rule attached to the “Attack” verb itself.
… Then again, perhaps making each killable thing have it’s own “Instead” rule for such makes for cleaner and more easily readable code. And easier to edit. Thoughts?
Anyhow, the code …

Chapter - The Rattlesnake

The rattlesnake is an animal. Understand "snake", "rattler", "viper", and "serpent" as the rattlesnake. The rattlesnake can be alive or dead. The rattlesnake is alive. The rattlesnake can be passive or aggressive. The rattlesnake is aggressive.
The description is "An Olive tan body with dark brown blotches across its back, unevenly fringed with white. Yep. This is one of them western rattlesnakes folks have warned you about, for sure. Quite venomous, so you best not get bit.[line break]It's tongue flicks in and out as it stares directly at you[if aggressive], rattling its tail violently, demonstrating that it is clearly irritated by your presence[end if].".

Instead of attacking the rattlesnake:
	If the rattlesnake is dead:
		Say "While your fear of snakes is understandable, you can't kill what's already dead.";
		Rule succeeds;
	If the player is carrying the shovel:
		say "Well now. That's certainly one dead rattlesnake. You nearly chopped it in half with your now-bloody spade.";
		Now the rattlesnake is dead;
		Now the description is "The threat of the rattler still lingers over its limp body and the beauty of its diamond-patterned skin.";
		Rule succeeds;
	If the player is carrying the revolver:
		say "With a bang, a puff of smoke, and the smell of black powder, the rattler is no more.";
		Now the rattlesnake is dead;
		Now the description is "Where the head and fangs of the viper once were, there is now only a charred and bloody stump.";
		Rule succeeds;
	Say "With what do you intend to kill it, your face?".

[Add a rumor in the saloon that bears are afraid of rattlesnakes]

Chapter - The King snake

The king snake is an animal.The printed name is "California mountain king snake". Understand "king snake", "California king snake", "California king snake", "mountain snake", "mountain king snake", "mountain king snake", "California mountain king snake", "California mountain king snake", "snake" and "serpent" as the king snake. The king snake can be alive or dead. The king snake is alive. The king snake can be passive or aggressive. It is passive.
 The description is "As the saying goes, 'Red touch yellow kills a fellow. Red touch black, friend of Jack.' The red, yellow, and black bands on this snake indicate clearly that it is a California mountain king snake. Though often confused with the highly venomous coral snake, the king snake is completely harmless.".

Instead of attacking the king snake:
	If the king snake is dead:
		Say "While your fear of snakes is understandable, you can't kill what's already dead.";
		Rule succeeds;
	If the player is carrying the shovel:
		say "Well now. That's certainly one dead king snake. You nearly chopped it in half with your now-bloody spade.";
		Now the king snake is dead;
		Now the description is "Being hewn asunder by your shovel, the pattern of its rings no longer matters.";
		Rule succeeds;
	If the player is carrying the revolver:
		say "With a bang, a puff of smoke, and the smell of black powder, the king snake is no more.";
		Now the king snake is dead;
		Now the description is "There is little left of the king snake to describe.";
		Rule succeeds;
	Say "With what do you intend to kill it, your face?".
1 Like

Lots of potential ways to shorten this, I think – here’s one (NB I also added an extra line break to your rattlesnake description since it was displaying awkwardly, and turned an “it’s” to an “its”. You also might want to de-capitalize Olive – OK, sorry, I’ll stop talking about grammar):

A snake is a kind of animal.  A snake has some text called the shovel-death-description.  A snake has some text called the revolver-death-description.

Instead of attacking a snake:
	If the noun is dead:
		Say "While your fear of snakes is understandable, you can't kill what's already dead.";
		Rule succeeds;
	If the player is carrying the shovel:
		say "Well now. That's certainly one dead [noun]. You nearly chopped it in half with your now-bloody spade.";
		Now the noun is dead;
		Now the description is "[shovel-death-description]";
		Rule succeeds;
	If the player is carrying the revolver:
		say "With a bang, a puff of smoke, and the smell of black powder, the [noun] is no more.";
		Now the noun is dead;
		Now the description is "[revolver-death-description]";
		Rule succeeds;
	Say "With what do you intend to kill it, your face?".

Chapter - The Rattlesnake

The rattlesnake is a snake. Understand "snake", "rattler", "viper", and "serpent" as the rattlesnake. The rattlesnake can be alive or dead. The rattlesnake is alive. The rattlesnake can be passive or aggressive. The rattlesnake is aggressive.
The description is "An Olive tan body with dark brown blotches across its back, unevenly fringed with white. Yep. This is one of them western rattlesnakes folks have warned you about, for sure. Quite venomous, so you best not get bit.[line break][line break]Its tongue flicks in and out as it stares directly at you[if aggressive], rattling its tail violently, demonstrating that it is clearly irritated by your presence[end if].".  The shovel-death-description is "The threat of the rattler still lingers over its limp body and the beauty of its diamond-patterned skin."  The revolver-death-description is "Where the head and fangs of the viper once were, there is now only a charred and bloody stump."


Chapter - The King snake

The king snake is a snake.The printed name is "California mountain king snake". Understand "king snake", "California king snake", "California king snake", "mountain snake", "mountain king snake", "mountain king snake", "California mountain king snake", "California mountain king snake", "snake" and "serpent" as the king snake. The king snake can be alive or dead. The king snake is alive. The king snake can be passive or aggressive. It is passive.
 The description is "As the saying goes, 'Red touch yellow kills a fellow. Red touch black, friend of Jack.' The red, yellow, and black bands on this snake indicate clearly that it is a California mountain king snake. Though often confused with the highly venomous coral snake, the king snake is completely harmless.".  The shovel-death-description is "Being hewn asunder by your shovel, the pattern of its rings no longer matters."  The revolver-death-description is "There is little left of the king snake to describe."

Hopefully that’s clear. You could also make both the shovel and the revolver weapons and condense the code still further, though there might be reasons not to streamline too much (maybe you want to keep track of the player’s bullets and decrement their ammo count if they use the revolver).

Anyway, hope this is helpful!

2 Likes

Of course, a snake ‘kind.’ … Hadn’t thought of that despite it being so obvious … after all, the word ‘snake’ is right there in both of 'em LOL D’OH!
(why is rattlesnake one word while king snake is two? hmm :wink: :stuck_out_tongue: )
And thanks for pointing out ‘Olive.’ … It’s so easy to miss things like that. Corrected it immediately. … Also, where was the errant it’s ? Such an easy typo yet so hard to find without a full-on grammar checker. Spell checkers don’t care about such typos. LOL
I’ll give this a go. Looks good. Clean and easy to edit without jumping all over the code base sections.

IDK about making the shovel a weapon, as I don’t want the player wandering around trying to kill everything with a shovel LOL … Its primary purpose really is for digging. But killing snakes with a shovel is a pretty obvious and not abnormal alternate use for a shovel.
Along with the pistol tho there IS a rifle, so making both of them a weapon ‘kind’ makes sense, particularly since they both have limited expendable ammo (1849 firearms. Not easy to reload)

2 Likes

The it’s is right after the line break in the rattlesnake description. And makes sense re weapons, you can probably save some effort including the revolver and rifle but seems like the shovel is different enough it’s easier to special-case it. Anyway, glad this was useful!

1 Like

This doesn’t really save much space, but you could rewrite just this part as a separate rule:

Instead of attacking a dead snake:
	say "While your fear of snakes is understandable, you can't kill what's already dead."

This is guaranteed to run before the rest of the instead of attacking a snake rule because that’s considered less specific (due to Inform’s rule sorting… rules).

Also (this is mostly a question of style) within Instead rules you’re supposed to use stop the action rather than rule succeeds (though both work) – and if you like, you can omit those entirely by using otherwise on your conditions.

It’s also possible to split those into individual rules as well, though you might need to be a bit more careful about the order they run in, as that’s not always the order you wrote them in. (Putting things in separate rules vs. longer single rules is also mostly a matter of taste – it’s easier to manage a single big rule, but you can sometimes do fancy things with overriding individual conditions when you have them in separate rules, or you may find that more readable.)

2 Likes

This is roughly how I’d do it.

An animal can be alive or dead. An animal is usually alive.
An animal can be passive or aggressive. An animal is usually passive.

A snake is a kind of animal. 
Understand "snake", "viper" and "serpent" as a snake.

A thing can be a weapon.
The shovel is a weapon.
The revolver is a weapon.

[Rule "block attacking rule" applies.]
The block attacking rule does nothing when the noun is a snake.

Definition: yourself is armed rather than unarmed if the player carries something which is a weapon.

A snake can be bullet-wounded.

Check attacking a dead snake: say "While your fear of snakes is understandable, you can't kill what's already dead." instead.
Check attacking a snake when the player is unarmed: say "With what do you intend to kill it, your face?" instead.
Carry out attacking a snake: now the noun is dead.
Carry out attacking a snake when the player carries the revolver: now the noun is bullet-wounded.

After attacking a snake: say "Well now. That's certainly one dead [if the noun is the rattlesnake]rattle[otherwise]king [end if]snake. You nearly chopped it in half with your now-bloody spade."
After attacking a bullet-wounded snake: say "With a bang, a puff of smoke, and the smell of black powder, the [if the noun is a rattlesnake]rattler[otherwise]king snake[end if] is no more."


Chapter - The Rattlesnake

The rattlesnake is a snake. Understand "rattler" as the rattlesnake. The rattlesnake is aggressive. 
Carry out examining a dead rattlesnake: say "The threat of the rattler still lingers over its limp body and the beauty of its diamond-patterned skin." instead.
Carry out examining the bullet-wounded dead rattlesnake: say "Where the head and fangs of the viper once were, there is now only a charred and bloody stump." instead.

The description of the rattlesnake is "An Olive tan body with dark brown blotches across its back, unevenly fringed with white. Yep. This is one of them western rattlesnakes folks have warned you about, for sure. Quite venomous, so you best not get bit.[paragraph break]Its tongue flicks in and out as it stares directly at you[if aggressive], rattling its tail violently, demonstrating that it is clearly irritated by your presence[end if]."


Chapter - The King snake

The king snake is a snake. The printed name is "California mountain king snake". Understand "California", "mountain" as the king snake. 

Carry out examining the dead king snake: say "Being hewn asunder by your shovel, the pattern of its rings no longer matters." instead.
Carry out examining the bullet-wounded dead king snake: say "There is little left of the king snake to describe." instead.

The description of the king snake is "As the saying goes, 'Red touch yellow kills a fellow. Red touch black, friend of Jack.' The red, yellow, and black bands on this snake indicate clearly that it is a California mountain king snake. Though often confused with the highly venomous coral snake, the king snake is completely harmless."


Chapter - Game 

The outback is a room. All snakes are in the outback. The player carries the shovel and the revolver.

In truth, though, implementation probably needs a bit more work. The first problem is that it doesn’t prioritize which weapon would be used if the player carries both of them. The second is that the dead snake will still remain a member of the person kind, and that means they will react to attempts such as pushing, pulling, eating, taking and so on, as if they were still alive.

A comprehensive solution to the second problem exists in the example “Puff of Orange Smoke”, and can be found in the Writing with Inform manual.

3 Likes

You can simplify those [if] blocks by simply using [the noun].

1 Like

True. It wouldn’t quite match the desired output, which varied a bit (“rattler”, “king snake” rather than “rattlesnake” and “California mountain king snake”, for instance), but you’re right in that I’d probably do that, and some variations could be handled by for printing the name of the king snake while looking or something like that.