intfiction.org

The Interactive Fiction Community Forum
It is currently Sat May 25, 2013 11:18 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Teleportation - TADS3.1
PostPosted: Sat Mar 31, 2012 12:03 pm 
Offline

Joined: Sun Nov 06, 2011 12:17 pm
Posts: 156
Location: Toledo, Ohio, USA
I have enclosed a piece of code that I have a slight problem with. If I enter the command code 'goto' and press enter before I enter a vocabword destination then I get a run-time
error. Is there a way to prevent the run-time error by making sure the destination word is enclosed in the command when I use the enter key. I would also like a text error message to point out my mistake. Would appreciate some help on this.

RonG



Code:

/*  Teleportation code.  ******************************************************/

    DefineTAction(Goto)
       objInScope(obj){if(obj.ofKind(Room))return true;
          return nil;
   }
;
    VerbRule(Goto)
       'goto'singleDobj
       :GotoAction
       verbPhrase='go/going to a room'
;
    modify Room
       dobjFor(Goto){
       action(){
          me.moveIntoForTravel(self);
          me.lookAround(true);
   }
  }
;



Top
 Profile Send private message  
 
PostPosted: Sat Mar 31, 2012 1:13 pm 
Offline

Joined: Sun Mar 01, 2009 8:02 pm
Posts: 902
This is untested, but the first thing I would try would be:

Code:
objInScope(obj) {if (obj && obj.ofKind(Room)) return true;


Top
 Profile Send private message  
 
PostPosted: Sun Apr 01, 2012 1:04 pm 
Offline

Joined: Sun Nov 06, 2011 12:17 pm
Posts: 156
Location: Toledo, Ohio, USA
I changed the code as shown but it didn't help. It seems to me that we should be able to kill the command if no object is present at all in the command. Such as '!objInScope'. But I didn't have any luck trying to do that. Perhaps it needs some type of if-else statement. I'm learning but still haven't quite figured this one out.

RonG

Code:
/*  Teleportation code.  ******************************************************/

   VerbRule(Goto)
      'goto'singleDobj
      :GotoAction
      verbPhrase='go/going to a room'
;   

   DefineTAction(Goto)
      objInScope(obj){if(obj&&obj.ofKind(Room))return true;
      //objInScope(obj){if(obj.ofKind(Room))return true;
         return nil;
   }
;
   
   modify Room
      dobjFor(Goto){
      action(){
         me.moveIntoForTravel(self);
         me.lookAround(true);
   }
  }
;


Top
 Profile Send private message  
 
PostPosted: Sun Apr 01, 2012 8:12 pm 
Offline

Joined: Tue Apr 27, 2010 1:02 pm
Posts: 797
It's not enough for objInScope(obj) to return true; you also need to make sure the room is in the list returned by getScopeList().

Code:
DefineTAction(Goto)
    objInScope(obj) { return getScopeList.indexOf(obj) != nil; }
    getScopeList()  { return allRooms; }
;

allRooms: ClassList
    type_ = Room
;


One of my hobbies is reimplementing the "list of all objects of a particular class" object in every project I touch. The latest addition to that proud line is my ClassList extension, which you'll need to add to your project to use the above code as-is.


Top
 Profile Send private message  
 
PostPosted: Sun Apr 01, 2012 8:25 pm 
Offline

Joined: Tue Apr 27, 2010 1:02 pm
Posts: 797
And you should also backstop the new verb by adding a default response to Thing:

Code:
modify Thing
    dobjFor(Goto) {
        verify() { illogical('You can\'t go to that. '); }
    }
;


This shouldn't matter after the above change, but it prevents a runtime error when the parser tries to visit the verify method for each object in the scope list.


Top
 Profile Send private message  
 
PostPosted: Mon Apr 02, 2012 2:37 pm 
Offline

Joined: Sun Nov 06, 2011 12:17 pm
Posts: 156
Location: Toledo, Ohio, USA
I'll give all of this a shot and see what happens. I also downloaded the project you had marked. Thanks much.

RonG


Top
 Profile Send private message  
 
PostPosted: Tue Apr 03, 2012 1:06 pm 
Offline

Joined: Sun Nov 06, 2011 12:17 pm
Posts: 156
Location: Toledo, Ohio, USA
I've added/modified the source for the 'goto' command. No problem compiling this but I have two problems when testing.

If I enter the goto command then press the enter key without including a target of any type, I get an 'Invalid Index' error along with 'cannot be indexed'. I know this is an entry error but it still makes a mess of things.

If I enable the 'modify thing' section, then I cannot go to any target room. I get a 'not necessary message'. I tried moving that section of code around but it made no difference. Perhaps that section of code is still not sequenced properly?

Code:

#charset "utf-8"
//#charset "us-ascii"

/*******************************************************************************
*  TADS Copyright (c) 1999, 2002 by Michael J. Roberts. Permission is granted  *
*  to anyone to copy and use this file for any purpose.                        *
*******************************************************************************/

/*  Includes and Source file extensions.  *************************************/

#include <adv3.h>
#include <en_us.h>
#include <tads.h>
#include <vector.h>

/*  List wrapper for all objects of a perticular class.  **********************/

property propNotDefined;
export propNotDefined;

class ClassList: object
   init() {
      local lst = new Vector(50);
      forEachInstance(type_, {obj: lst.append(obj)});
      lst_ = lst.toList();
   }

   propNotDefined(prop, [args]) {
      lst_?? init;           
      return lst_.(prop)(args...);
   }

   operator[](x) {
      lst_ ?? init;
   return lst_[x];
   }

   lst_=nil
   type_ = self
;

/*  Sample template for ClassList objects.  */

ClassList template &type_;

/*  Clock time at start of story.  ********************************************/

ClockEvent eventTime=[1,06,00];

/*  Teleportation code.  ******************************************************/

   VerbRule(Goto)
      'goto'singleDobj
      :GotoAction
      verbPhrase='go/going to a room'
;   

   DefineTAction(Goto)
      objInScope(obj){ return getScopeList.indexOf(obj) !=nil;}
      getScopeList() {return allRooms;}
;
   
allRooms: ClassList
   type_=Room
;

//modify Thing
  // dobjFor(Goto){
    //  verify() {illogical('You can\'t go to that.');}
   //}
//;

modify Room
      dobjFor(Goto){
      action(){
         me.moveIntoForTravel(self);
         me.lookAround(true);
   }
  }
;



Top
 Profile Send private message  
 
PostPosted: Tue Apr 03, 2012 1:36 pm 
Offline

Joined: Tue Apr 27, 2010 1:02 pm
Posts: 797
RonG wrote:
If I enter the goto command then press the enter key without including a target of any type, I get an 'Invalid Index' error along with 'cannot be indexed'. I know this is an entry error but it still makes a mess of things.


This happens because of the way you've defined the Goto verb phrase. You wrote this:

Code:
VerbRule(Goto)
    'goto' singleDobj
    :GotoAction
    verbPhrase='go/going to a room'
;


When doing disambiguation, the parser checks the verb phrase for some text in parentheses, which it will parrot back at the player in the event he doesn't supply an object. So you should always have some parenthesized text, ideally written in a way that the parser's prompt makes sense.

Code:
VerbRule(Goto)
    'goto' singleDobj
    : GotoAction
    verbPhrase = 'go/going (where)'
;


Produces this output:
Quote:
> goto
Where do you want to go?


RonG wrote:
If I enable the 'modify thing' section, then I cannot go to any target room. I get a 'not necessary message'. I tried moving that section of code around but it made no difference. Perhaps that section of code is still not sequenced properly?


Room inherits from Thing, so if you don't override the verify method, Room will also block the goto command.

This should fix it - note the empty verify() method has been added to Room.

Code:
modify Room
   dobjFor(Goto) {
      verify() {}
      action() {
         me.moveIntoForTravel(self);
         me.lookAround(true);
      }
   }
;


A general note on this command - the way you've implemented it, the player only needs to know the name of the room in order to travel there. You may wish to restrict travel to rooms that the player has visited, which you can do by testing the room's "seen" property.

Code:
modify Room
   dobjFor(Goto) {
      verify() {}
      check() {
         if (!seen)
            failCheck('You don\'t know where that is. ');
      }
      action() {
         me.moveIntoForTravel(self);
         me.lookAround(true);
      }
   }
;


Top
 Profile Send private message  
 
PostPosted: Wed Apr 04, 2012 2:35 pm 
Offline

Joined: Sun Nov 06, 2011 12:17 pm
Posts: 156
Location: Toledo, Ohio, USA
Here's the latest version of my 'Goto' code. Everything works except for using the comand Goto followed directly by the enter key (no object). In that case, I get the following error:
Invalid index operation - this type of value cannot be indexed.
Go Figure!!!

Code:

/*  Teleportation code.  ******************************************************/

   VerbRule(Goto)
      'goto'singleDobj
      :GotoAction
      verbPhrase='go/going(where)'
;   

   DefineTAction(Goto)
      objInScope(obj){ return getScopeList.indexOf(obj) !=nil;}
      getScopeList() {return allRooms;}
;
   
allRooms: ClassList
   type_=Room
;

modify Thing
   dobjFor(Goto){
      verify(){illogical('You can\'t go to that.');}
  }
;

modify Room
   dobjFor(Goto){
      verify(){}   
      check(){
         if(!seen)
            failCheck('You don\'t know where that is.');
   }

action(){
   me.moveIntoForTravel(self);
   me.lookAround(true);

  }
 }
;



Top
 Profile Send private message  
 
PostPosted: Wed Apr 04, 2012 2:47 pm 
Offline

Joined: Tue Apr 27, 2010 1:02 pm
Posts: 797
Is there a reason you are deleting the spaces in the VerbRule strings? It should be written like this:

Code:
VerbRule(Goto)
    'goto' singleDobj
    : GotoAction
    verbPhrase = 'go/going (where)'
;


If there's no space in 'go/going(where)', the regex fails to find the match and you're back to the index error.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group