Randomizing item locations

Here is a problem I’m stuck on. I did take a look at “Hatless”. I’m trying to figure out why this doesn’t work, and how to swap two items.

Ration is a kind of thing. Gold coin is a kind of thing.

The Arena is a room. The chest is a container in the Arena.

Loot are a kind of thing. There are 10 loots in the chest.

The Loot Dimension is a room. There are 5 gold coins in the Loot Dimension. There are 5 rations in the Loot Dimension.

When play begins:
    repeat with x running from 1 to 10:
        let the original be a random loot;
        if the original is nothing:
            say "The original was nothing.";
        otherwise:
            let target be the location of the original;
            say target;
            let replacement be a random thing in Loot Dimension;
            move replacement to the target;
            now original is nowhere.

There are two problems:

  • First, the Arena still has placeholder loot in it at the end of the process–something is going wrong. If I use repeat with the original running through loots instead, this is fixed, but I don’t understand why the original problem is present. Is this possibly a bug?
  • Second, the location of the coins becomes “The Arena” and not still in the chest as I’d like. I’m not sure how to actually swap two items. The documentation says location is always a room (or nowhere) but showme loot says the location is in the chest in the Arena. Can I somehow refer to the immediately enclosing container instead?
1 Like

First, the Arena still has placeholder loot in it at the end of the process–something is going wrong. If I use repeat with the original running through loots instead, this is fixed, but I don’t understand why the original problem is present. Is this possibly a bug?

This is pretty obvious: since you pick “a random loot” 10 times, it’s very likely that at some point you’ll pick a loot that you’ve already picked before. The solution is to pick “a random loot in the chest.” (Or just repeat through the list of loots, which will pick each loot exactly once, as you experienced.)

Second, the location of the coins becomes “The Arena” and not still in the chest as I’d like. I’m not sure how to actually swap two items. The documentation says location is always a room (or nowhere) but showme loot says the location is in the chest in the Arena . Can I somehow refer to the immediately enclosing container instead?

If you type a random container enclosing the original this will evaluate to the container you want.

For a general-purpose approach, you could rig up a phrase like this:

To decide which object is the room-or-container containing (contained-item - a thing):
    let the possible-container be a random container enclosing the contained-item;
    if the possible-container is a container, decide on the possible-container;
    decide on the location of the contained-item.

Now you can type something like let the target be the room-or-container containing the original, and you’ll get either a room, a container, or nothing (if the location is “nowhere”).

Ahh, that’d be it. I forgot putting it “nowhere” wasn’t the same as deleting.

1 Like

If I recall correctly, the canonical phrase for this is the holder of the original.

1 Like

For the present purpose, you could also consider using “the holder”, as in: “let target be the holder of the original;”. (Edit: as Adrian already said, while I was typing) :smiley:
That also takes care of supporters, and it captures the thing (or room) which directly contains or supports (etc.) the item.
Enclosing” includes indirect containment, so if you use “a random container enclosing the original”, and you happen to have loot which is in a strongbox in a chest, so that it is indirectly contained in the chest, then the random container will sometimes be the strongbox and sometimes be the chest.

1 Like

Instead of

 repeat with x running from 1 to 10:
        let the original be a random loot;

which doesn’t quite work, since you can pick one loot more than once, you can write

repeat with original running through loots:
1 Like

@ArdiMaster @StJohnLimbo I didn’t know there was a canonical phrase for this. Thanks for the tip!

1 Like