Using existing actions on values

Hey All–

In my Library/books question, I confidently said I could figure out the coding, but it will surprise everyone to learn I was wrong.

I want the player to pick a book by shelf letter, book number, and page number, so something like READ (or X or TAKE) Y 24 280.

That way I can do something like:

A booknumber is a kind of value. A 0 0 specifies a booknumber. [it's not clear to me if those numbers can go past one digit or not in the examples in the docs]

Report examining a booknumber:
	if the booknumber understood matches "Y 24 280":
		say "You learn the secret of life!";
	otherwise:
		say "Reading random passages will not be useful.".

but that, of course, does not work.

This isn’t all that different from creating a phone number, which Alias in the docs has as a value. But I’ve futzed with that example forever and can’t get a handle on it. I want existing actions to apply to a value, but it seems like I can’t do that since values aren’t things.

So either there’s a way to do this, or I’m going about this all wrong and need to try something else.

I think the issue is just that actions can apply to kinds of values, but examining doesn’t by default. So short of editing the standard rules, you’d probably need to do something like:

understand “examine [booknumber]” as booklooking. understand “x [booknumber]” as booklooking. Booklooking is an action applying to one booknumber.

And then have all your rules reference booklooking rather than examining.

2 Likes

Thanks! That seemed to take care of examining. But Inform does not like something about my value:
Code:

A booknumber is a kind of value. A 0 0 specifies a booknumber.[still not sure if this allows more than one digit since I can't get anything to work]

understand “examine [booknumber]” as booklooking. understand “x [booknumber]” as booklooking. Booklooking is an action applying to one booknumber.
	
Report booklooking a booknumber:
	if  the booknumber understood is [also tried matches] "A 1 1":
		say "You learn the secret of life!";
	otherwise:
		say "Reading random passages will not be useful.".

Error:


.
.

Trying the whole thing with dashes (as in the phone number example in Alias) yields a slightly different error:

“A-1-1” is text; A-1-1 is a book number. So just use it without the quotes and you should be good!

2 Likes

Scooped! I also think you need to say “is A 1 1”, not “matches A 1 1” since “matches” just works on topics?

1 Like

Ack, no.

Problem. In the sentence ‘if the booknumber understood is A 1 1’ , you use the notation ‘A 1 1’ to write a constant value. But the notation was specified as ‘A 0 0’, which means that the second numerical part should range between 0 and 0.

Same with dashes. Alias didn’t get as far as having a specific reply to a specific value, and I couldn’t find anything else like it in the docs.

What if you say “A 9 9 specifies a booknumber”? Like maybe zeroes are a special case.

1 Like

Ah, yeah. When you define a kind of value including numbers like this, the number you use in the definition is how high it can go—for every number except the first, which has effectively no limit. Which is generally what you want for addition to work properly: if “$1.99 specifies a cost”, then $5.99 + $0.01 is $6.00 instead of $5.100.

So just put the maximum shelf and book number into your definition and that should cover it.

4 Likes

HAHA! It works.

Now I see they used all 9s in the Alias example, but they didn’t say why so I didn’t think about it.

Thanks!

2 Likes

The first digit (or, technically, the first sequence of contiguous digits of any length) always means an unbounded number. Each subsequent occurrence of a digit is placing a max on what digits may occur there.

Are these booknumbers going to be randomly generated? If so, will it be dynamic during the course of the game, or all set up at the beginning? About how many will there be?

There’s a problem with what you’ve got: all shelves must be “A”.

1 Like

Though that’ll change in the upcoming version of Inform where you can specify that a letter is one of the elements of the value!

3 Likes

Actually, more importantly, is it really relevant for you to be able to refer to book numbers in that format in your code? Or were you just setting it up that way for the convenience of using the booknumber understood in your action?

1 Like

You might want to pursue a strategy like:

a book-id has a text called the shelf.
a book-id has a number called the book-num.
a book-id has a number called the page-num.
a book-id can be actual or in-potentia.
a book-id has a text called the understood.
Understand the understood property as describing a book-id.
There are 50 book-ids.

to say (b - book-id): say "[shelf of b] [book-num of b] [page-num of b]".

to decide what book-id is a new book id:
  let b be a random in-potentia book-id;
  now b is actual;
  decide on b;

to decide what book-id is new book id with bookshelf (sh - text) and/-- book num (b - number) and/-- page (pg - number):
let new-book be a new book id;
now the shelf of new-book is sh;
now the book-num of new-book is b;
now the page-num of new-book is pg;
now the understood of new-book is the substituted form of "[shelf of new-book in lower case]-[book-num of new-book]-[page-num of new-book]";
say new-book;
say line break;

to recycle book id (b - book-id):
  now the shelf of b is "";
  now the book-num of b is 0;
  now the page-num of b is 0;
  now the understood of b is "";
  now b is in-potentia.

the book-id-regexp is always "\s+(<a-z>)\s+0*(\d+)\s+0*(\d+)(\s|$)".

after reading a command:
let pc be the substituted form of "[the player's command]";
  if pc matches the regular expression book-id-regexp begin;
    let the replacement be the substituted form of " [text matching subexpression 1]-[text matching subexpression 2]-[text matching subexpression 3] ";
    replace the regular expression book-id-regexp in pc with replacement;
    change the text of the player's command to pc;
  end if;

Sorry to code-dump at you, but there were enough tricky bits that it was a lot easier to code than it would be to describe…

1 Like

Figured that out all by myself! I have 6 letters, so it wasn’t an issue to just add more booknumber specifications.

I’m just super-pleased that I had the right idea at all and that minor tweaks made it work.

Does that actually work? If you write this:

A 9 9 specifies a booknumber,
B 9 9 specifies a booknumber.

I’m pretty sure that means that A 9 9 and B 9 9 are the same value – the letter doesn’t affect the value at all. Try it:

When play begins:
	if A 9 9 is B 9 9:
		say "A equals B".
1 Like

Yes! It totally works and you get the responses you want for each individual letter number number you make an answer for.

Code:

Lab is a room. 

A booknumber is a kind of value. A 0 1000 specifies a booknumber. B 0 1000 specifies a booknumber. M 0 1000 specifies a booknumber. L 0 1000 specifies a booknumber. V 0 1000 specifies a booknumber. Q 0 1000 specifies a booknumber.

Understand “examine [booknumber]” as booklooking. Understand “x [booknumber]” as booklooking. Understand “read [booknumber]” as booklooking. Understand “choose [booknumber]” as booklooking. Understand “pick [booknumber]” as booklooking. Booklooking is an action applying to one booknumber.

Report booklooking a booknumber:
	if  the booknumber understood is A 1 1:
		say "Youlearn the secret of life!";
	otherwise if the booknumber understood is B 42 963:
		say "You learn the secret of death!";
	otherwise:
		say "Reading random passages will not be useful.".
		
test me with "read A 1 1/read b 42 963/ read A 29 4"

I’m still thinking about how to report letters and numbers outside of the accepted range.

Edit- Wait! Crap. You’re right. Substituting A for B yields the same thing. Argh. Back to the drawing board.

Dude. I don’t even understand how I would use this, even after reading it through a couple of times.

I think the easier solution here would be to make it a kind of value with three parts (“0 99 999 specifies a book number”) and use a regex to change \b([abcde])(\s+\d+\s+\d+)\b into that format, no?

1 Like

That’s a reason I was asking about how desirable it was to call 'em by booknumber in the code.

lab is a room.
booknumber is a kind of value. #9-999-999 specifies a booknumber with parts shelf-num and book-num and page-num.

book-description relates one text to one booknumber.
The verb to book-describe means the book-description relation.

to decide what K is (v - value) cast as a/an/-- (name of kind of value K): (- {v} -).

#1-1-1 is book-described by "You learn the secret of life!".
#2-42-963 is book-described by "You learn the secret of death!".

the book-id-regexp is always "\s+(<a-z>)\s+0*(\d+)\s+0*(\d+)(\s|$)".

after reading a command:
let pc be the substituted form of "[the player's command]";
  if pc matches the regular expression book-id-regexp begin;
    let shelfnum be 999;
    let shelf-understood be the substituted form of the text matching subexpression 1;
    repeat with x running from 1 to 26 begin;
      let letter be "[(x + 96) cast as a unicode character]"; 
      if letter is shelf-understood begin;
        now shelfnum is x;
        break;
      end if;
   end repeat;
   let the replacement be the substituted form of " #[shelfnum]-[text matching subexpression 2]-[text matching subexpression 3] ";
    replace the regular expression book-id-regexp in pc with replacement;
    change the text of the player's command to pc;
  end if;

book-peeping is an action applying to one booknumber.

understand "examine [booknumber]" as book-peeping.
understand "read [booknumber]" as book-peeping.

carry out book-peeping a booknumber (called book):
  if a text relates to the book by the book-description relation, say "[text that relates to the book by the book-description relation][line break]";
  else say "Reading random passages will not be useful.";

to say (b - booknumber): let x be 64 + shelf-num part of b; say "[x cast as a unicode character] [book-num part of b] [page-num part of b]".

test me with "read A 1 1/read b 42 963/ read A 29 4"