Inform 7: Clothing with two separately wearable parts?

Hello! I have a couple questions.

First problem, the player carries a magical hooded cloak that magically hides his antlers and feet when the hood part is on. I intended to create a two part garment with a cloak and hood. I intended for the cloak to be wearable separately from the hood part:


A hat is a kind of thing. It is wearable. People can wear one kind of hat.
Clothes are a kind of thing. They are wearable. People can wear one kind of clothes.

The tattered cloak is a clothes. It is unidentified. The description is "This hooded cloak might have been red once. It fits you perfectly."
The tattered hood is a hat. It is part of the tattered cloak. It is unidentified.
The description of the tattered hood is "[IF UNIDENTIFIED]It's part of the cloak[ELSE]Wearing this hood apparently conceals your cervine features[END IF]."
The player is wearing a tattered cloak.
Understand "cloak" as the tattered cloak.
Understand "hood" as the tattered hood.

The testcloak is a clothes. The player is carrying the testcloak.
The testhat is a hat. The player is carrying the testhat.

But whether the player is wearing the tattered cloak or not, attempts to “put on hood” return this message without putting on the hood.

Second problem, I want the player to be able to wear only one hat type clothing at a time, but the player can wear the testcloak and tattered cloak at the same time, implying that he could probably also wear the testhat and testcloak simultaneously.

Lastly, I wonder if there is a way to list all the parts that make up an object in inventory. I can manipulate the hood, but it isn’t listed in inventory:

In summary, I would like to learn a proper way to make multi-part clothing and set restrictions against wearing two of the same kind of clothing at once.

Thanks very much!

That’s easy to do with a couple of instead rules: Instead of wearing a hat when the player is wearing a hat, say "You are already wearing a hat." Instead of wearing clothes when the player is wearing clothes, say "You are already dressed."
The multi-part clothing thing, where you should be able to wear parts of the same clothing independently of each other, is probably trickier.

If the only thing the player can do with the hood is wear it or not wear it, the easiest way might be to model the wearing of the hood with an either/or property of the cloak (or of the player) without making the hood an object at all.

Not all that tricky. Not complete, since I’m at work, but:

[code]A garment is a kind of thing. A garment is wearable.
A hat is a kind of garment. A jacket is a kind of garment. Some pants are a kind of garment.
An outfit is a kind of garment.

Garment-combination relates an outfit to various garment. The verb to comprise (it comprises, they comprise, it is comprising) implies the garment-combination relation. The verb to be comprised by implies the reversed garment-combination relation.

To decide whether (suit - an outfit) is/are defacto worn by (actor - a person):
if the actor wears the suit:
decide yes;
repeat with item running through garments comprised by the suit:
if the item is not worn by the actor, decide no;
decide yes.

The snazzy suit is an outfit.
The fedora, the suit jacket and the suit pants are garments.
The snazzy suit comprises the suit jacket, the fedora and the suit pants.

After wearing something comprised by an outfit (called the threads):
if the actor does not wear the threads:
if the threads are defacto worn by the actor:
now the actor wears the threads;
repeat with item running through things comprised by the threads:
now the item is off-stage.

There is a room. The suit jacket, the fedora and the suit pants are here.[/code]

This is pretty darn clumsy code and you should probably obscure the garments somehow to allow piecemeal removal as opposed to placing them offstage, but this should at least be a starting point.

The lines about “people can wear one kind of hat” and “one kind of clothes” won’t do what you expect. I’d recommend using Instead rules as Felix suggested.

To allow wearing the hat separately, you could modify the rule that’s making you take it first. I think it’s the “can’t wear what’s not carried rule”, but you can check by typing “rules” before trying to wear the hood.

Copy the rule from the standard rules, give it a new name, and change “if the noun is not carried by the player” to “if the noun is not enclosed by the player” (which includes being part of something carried by the player). Then put the new rule in place of the old one: “The new can’t wear what’s not carried rule is listed in place of the can’t wear what’s not carried rule in the check wearing rulebook.”

I would try to implement this instead as clothing with states.

A hood is a kind of thing. A hood can be raised or lowered. A hood is usually lowered.

The tattered hood is part of the tattered cloak.

Then have a RAISE HOOD action to change states. After that it’s as simple as an instead rule:

Instead of wearing a hood:
try raising the noun instead.

Having a separate action for putting the hood up makes more sense because the check, carry out and report steps of the action would probably all have to be different.

Changing the can’t wear what is not carried rule like Draconis suggested might have some unfortunate side-effects, like allowing players to wear things that are inside containers, so it would probably be safer to ad-hoc in this particular case if you go that route. So the test would be ‘if the noun is not carried by the player and the noun is not part of a thing carried by the player’ for example.

ON PREVIEWING: Sequitur is in before me with many of the points I’d made. However, if you’re going to redirect wearing to the custom action you need to use a “before wearing” rule rather than an “instead of wearing” rule, I think, because the carrying requirements get checked between the “Before” and “Instead” rules; so an “instead of wearing the hood” rule will try to implicitly take the hood and get blocked.

I am going to start this post with a SPOILER: You should not try to allow the player to wear the hood. Do what Felix said and model wearing the hood with an either/or property of the cloak, or the player, or (what I think is best) the hood. The code that lets you wear the hood, and the reason why you shouldn’t do it, is in the spoiler tag.

[spoiler]

Unfortunately it’s a bit deeper than this. The wearing action is defined as “applying to one carried thing,” which I believe automatically forces the player to try taking something before wearing, and stops the action if the take fails. I think that some of the things around this will change in the next build of Inform, but in the meantime it is what it is.

Getting the carrying requirements not to apply to the action can be pretty annoying. capmikee has an extension that lets you do this. The thread that originally inspired the extension might allow us to come up with a special-case solution:

[code]An action-processing rule (this is the don’t need to take the hood rule):
if the current action is trying wearing the hood, continue the action;
abide by the carrying requirements rule.

The don’t need to take the hood rule is listed instead of the carrying requirements rule in the action-processing rulebook.[/code]

At this point we also need to fix the “can’t wear what’s not held” rule as Draconis suggested. (Most of the time the “can’t wear what’s not held” rule won’t fire, I think, because if you’re not holding the noun and can’t pick it up then the action will get stopped by the implicit take. But it serves as a backstop when a wearing action somehow manages to reach the check stage without the actor holding the noun – which is what happens when we tweak the carrying requirements rule.)

[code]Check an actor wearing (this is the new can’t wear what’s not held rule):
if the actor does not enclose the noun,
stop the action with library message wearing action number 2 for the noun.

The new can’t wear what’s not held rule is listed instead of the can’t wear what’s not held rule in the check wearing rulebook.[/code]

And now if you test our code, “wear hood” works! Hooray! But then if you try “wear hood/drop hood” we see the problem: When you wear the hood, it stops being part of the cloak. I believe the object hierarchy means everything has to have only one parent, if that’s the term: The hood can be part of the cloak, or worn by the player, but not both. So none of this will work.[/spoiler]

So what I’d do is make the hood part of the cloak, give it a property to determine whether it’s on or off, and redirect the actions of wearing the hood or taking the hood off so they go to changing that property rather than making the hood worn or not per se. Here’s an implementation, which surely has some issues:

[code]Lab is a room.
A thing can be identified or unidentified.
A hat is a kind of thing. It is wearable. Clothes are a kind of thing. They are wearable.
The tattered cloak is a clothes. It is unidentified. The description is “This hooded cloak might have been red once. It fits you perfectly.”
The tattered hood is part of the tattered cloak. The tattered hood is unidentified. The tattered hood can be on or off. The tattered hood is off.
The description of the tattered hood is “[IF UNIDENTIFIED]It’s part of the cloak[ELSE]Wearing this hood apparently conceals your cervine features[END IF].”
The player is wearing a tattered cloak.
Understand “cloak” as the tattered cloak.
Understand “hood” as the tattered hood.

The testcloak is a clothes. The player is carrying the testcloak.
The testhat is a hat. The player is carrying the testhat.

Hood-wearing is an action applying to one thing. [It won’t be triggered by a direct command, just redirected from wearing.] Before wearing the hood: try hood-wearing the hood instead.

Check hood-wearing the hood when the player does not wear the cloak: say “You have to be wearing the cloak before you can put the hood up.” instead.

Check hood-wearing the hood when the player wears a hat (called the chapeau): say “You can’t put the hood on over [the chapeau].” instead.

Carry out hood-wearing the hood: now the hood is on.

After hood-wearing the hood:
say “You pull the hood up over your head.[if unidentified]It conceals your cervine features![end if]”;
now the hood is identified.

Every turn when the player does not wear the cloak: now the hood is off.

Hood-removing is an action applying to one thing. Before taking off the hood: try hood-removing the hood instead.

Check hood-removing the hood when the player does not wear the cloak: say “You’re not wearing the cloak.” instead.
Check hood-removing the hood when the hood is off: say “The hood is already down.” instead.
Carry out hood-removing the hood: now the hood is off.
Report hood-removing the hood: say “You pull the hood down, revealing your antlers.”
[/code]

One thing is that you want to make sure that the rule for taking the hood off when the player takes off the cloak comes into effect soon enough; if you have an every turn rule that checks whether the hood is on, it might run before you’ve tested to see whether the player is wearing the cloak. Also the message for wearing the hood the first time prints on two separate lines. You’d also need to write something to make sure the player can type “put up hood” or “put hood up.” And I notice I didn’t write anything to prevent the player from putting the hat on over the hood.

BTW, you don’t need a line to understand “cloak” as the tattered cloak; unless you set an object as privately-named, the game will automatically understand every word in its name as it.

Instead of having an every turn rule to put down the hood if the player takes off the cloak…

Definition: The hood is raised if the cloak is worn by the player and the hood is on.

Carry out wearing the cloak:
  now the hood is off;
  continue the action.

And then just never check whether the hood is on or off, only whether it is ‘raised’; this way you don’t have to care about the hood’s state at any time when it’s not being worn by the player – after all it’s not even meaningful to ask whether the hood is up if the cloak isn’t being worn.

Thanks all for your replies.
Wearing two of the same type of clothing is fixed thanks to “instead” rules.
The cloak and hood work mostly as intended, but I’m still getting the extra “(first taking the cloak)” output.

I took Sequitur’s suggestion, making the hood either raised or unraised, and the player either hooded or unhooded.
I separated the disguise so that the hood would hide the player’s antlers and the cloak would hide the hooves, using a corresponding cloaked and uncloaked state for the player.

I tried to make a set of “instead of wearing”/“instead of taking off” rules, but the “(first taking the hood) rule” always intercepts the command, doing nothing for the player’s (un)hooded/(un)cloaked states according to player description. I’m currently using “before wearing” rules which does update the clothing state, but the game still outputs “(first taking the hood)” in the end:

(If I used “instead” instead of “before,” wearing the hood goes straight to the taking part. I’m sure using “after” makes the most sense, but that has the same problem as “instead.”)

Relevant code:

A person is either hooded or unhooded. Persons are usually unhooded.
A person is either cloaked or uncloaked. Persons are usually uncloaked. 

Clothes are a kind of thing. They are wearable.
Instead of wearing a clothes when the player is wearing a clothes (called the already worn clothes):
say "You're already wearing [the already worn clothes]." 

A hood is a kind of thing. A hood can be raised or lowered. A hood is usually lowered.

The description of the player is "You have two hands [IF UNCLOAKED]and a pair of short, thin legs that end with hooves[ELSE]and feet[END IF]. [IF UNHOODED]A pair of antlers portrude from the top of your head[ELSE]The tattered hood conceals your antlers[END IF]."

The player is cloaked.

The player is wearing a tattered_cloak.

The tattered_cloak is a clothes. It is unidentified. The printed name is "tattered cloak". The description is "This hooded cloak might have been red once. [IF UNIDENTIFIED]It fits you perfectly[ELSE]It apparently disguises your hooved feet[END IF]."
Before taking off the tattered_cloak:
	say "[IF UNIDENTIFIED]Your legs become long and thin as you remove your cloak! Short brown hair sprouts from your ankles upwards. Your toes retract while your feet become hooves, and a fluffy white deer's tail sprouts from your posterior![ELSE]You feel your legs stretch as they resume their cervine appearance.[END IF]";
	Now the tattered_cloak is identified;
	Now the player is uncloaked;
	If the player is hooded:
		say "[paragraph break]Your antlers reemerge as well from beneath the hood.";
		Now the tattered_hood is lowered;
		Now the player is unhooded.
Before wearing the tattered_cloak:
say "No sooner do you fasten your cloak do your legs contract and straighten. Your toes feel oddly stiff as hooves become feet once more.";
Now the player is cloaked.
Understand "cloak" or "tattered cloak" as the tattered_cloak.

The tattered_hood is a hood. It is part of the tattered_cloak. It is unidentified. The printed name is "tattered hood".
The description of the tattered_hood is "[IF UNIDENTIFIED]It's part of the tattered cloak. You see nothing special about it[ELSE]Wearing this hood apparently conceals your antlers[END IF]."
Before wearing the tattered_hood:
	If the player is not wearing the tattered_cloak:
		say "You try to wear the hood without the tattered cloak but your antlers are getting in the way.";
	Otherwise:
		say "[IF UNIDENTIFIED]Your antlers somehow disappear under your hood![ELSE]You antlers disappear as you pull the hood over your ears.";
		Now the tattered_hood is identified;
		Now the tattered_hood is raised;
		Now the player is hooded.
Before taking off the tattered_hood:
	say "Your antlers reemerge from beneath the hood.";
	Now the tattered_hood is lowered;
	Now the player is unhooded.
Understand "hood" or "tattered hood" as the tattered_hood.

I read your suggestions about putting new rules in place of the standard rules, but I’m a bit apprehensive as I’m new to to Inform and unsure of all the relationships between the standard rules. Is there any other way to bypass the part-wearing check, or alternatively cancel the player’s command after changing the wearing states?

(Not entirely related, but I was also having problems with similarly named rooms and objects with names longer than one word interfering with each other, so I used underscore names and covered the possibilities with understand rules.)

To fix your “(first taking the hood)” problem, you should include the phrase “stop the action” at the end of your “Before wearing the tattered_hood” and “Before taking off the tattered_hood” rules.

What’s happening is that the “Before” rules are running as they’re supposed to, but then the rest of the standard wearing rules are also running, which means that the game is still trying to pick up the hood in order to wear it. This is how “Before” rules usually work; “Instead” rules (once you get to them) stop as soon as they’ve run but “Before” rules pass through to the rest of the rules for the action. The phrase “stop the action” basically says “We’re done here; don’t run any more rules for this action.” Which is what you want, since we’ve packed all the effects of the action into the “Before” rule.

So this phrase, “stop the action,” is basically what you were asking for as a way of canceling the player’s command after changing the wearing states.

One thing I’d note is that you currently don’t have any sanity checks in your “Before taking off” rule, so it would be possible for the player to type “take off hood” and get the relevant messages even if he’s not wearing the cloak.

Also you have more variables than you need, and this might make it hard to keep track of (if you forget to change one of them when you need to, you could wind up with a bug where your game doesn’t behave the way you want it to). You don’t need to define separate “cloaked” and “hooded” properties for the player that you set manually. After all, “cloaked” just means that the player is wearing the cloak, and “hooded” just means that the player is wearing the cloak with a raised hood, doesn’t it? So you can write special phrases to define “cloaked” and “hooded,” as in section 6.4 of the documentation:

A person is cloaked rather than uncloaked if it is wearing the tattered_cloak. A person is hooded rather than unhooded if it is wearing the tattered_cloak and the tattered_hood is raised.

Now you don’t have to (and can’t) say things like “Now the player is cloaked” or “Now the player is hooded.” The game will keep track of whether the player is cloaked or hooded by checking whether he’s wearing the tattered_cloak and whether the tattered_hood is raised. This is a lot easier on you – if the game keeps track of these things itself, you don’t have to make sure that you change whether the player is cloaked or hooded every time you’re supposed to.

In this case you don’t have to put new rules instead of the standard rules! As I was saying, that solution doesn’t wind up working.

A pretty common strategy for by-passing or working around the standard action rules for specific cases where they don’t work as you want, is to define a new custom action and redirect the standard action to it in those specific cases. Like so (also going by Matt’s suggestion of I7 Definitions and splitting rules up into Before Rules to redirect the wearing and taking off actions before they are tried, Instead Rules to block the new actions, Carry Out (or possibly Instead) Rules to change the state of the game and After (or possibly Report) Rules that just give output):

Definition: A person is cloaked rather than uncloaked if it is wearing the tattered_cloak.
Definition: A person is hooded rather than unhooded if it is wearing the tattered_cloak and the tattered_hood is raised.

Raising is an action applying to one thing.
Lowering is an action applying to one thing.

Before wearing the tattered_hood: try raising the tattered_hood instead.
Before taking off the tattered_hood: try lowering the tattered_hood instead.

The tattered_hood is a hood. It is part of the tattered_cloak. It is unidentified. The printed name is "tattered hood".

The description of the tattered_hood is "[IF UNIDENTIFIED]It's part of the tattered cloak. You see nothing special about it[ELSE]Wearing this hood apparently conceals your antlers[END IF]."

Instead of raising the tattered_hood when uncloaked:
	say "You try to wear the hood without the tattered cloak but your antlers are getting in the way.".
	
Instead of raising the tattered_hood when hooded:
	say "You're already wearing the hood."

Carry out raising the tattered_hood:
	Now the tattered_hood is identified;
	Now the tattered_hood is raised;

Report raising the tattered_hood:
	say "[IF UNIDENTIFIED]Your antlers somehow disappear under your hood![ELSE]You antlers disappear as you pull the hood over your ears.";
	
Instead of lowering the tattered_hood when uncloaked:
	say "You're not wearing the tattered cloak with the hood.".
	
Instead of lowering the tattered_hood when unhooded:
	say "You've already taken the hood off."
	
Carry out lowering the tattered_hood:
	Now the tattered_hood is lowered;
	
Report lowering the tattered_hood:
	say "Your antlers reemerge from beneath the hood.";