Automatic return paths

It's a bit tedious having to define every room as leading back in the opposite direction to the room you just came from:

room hallway "Hallway"
  with description "This is a pretty dull hallway.",
  n_to kitchen,
  s_to front_door;

room kitchen "Kitchen"
  with description "You are in a well-appointed and modern kitchen.",
  s_to hallway,
  n_to back_door;

Note the n_to kitchen in the definition of the hallway, and the corresponding s_to hallway in the definition of the kitchen. In the majority of cases, this is just redundant tedium, and it would be nice to only have to specify one of the directions.

Here's how to do it (see page 440 of the Inform6 Designer's Manual):

class room has light,
  with n_to, s_to, e_to, w_to,
    ne_to, nw_to, se_to, sw_to,
    in_to, out_to, u_to, d_to;

[ autodir x dir1 dir2 y;
  y=x.dir1; if (metaclass(y)==object && y ofclass room) y.dir2=x;
  y=x.dir2; if (metaclass(y)==object && y ofclass room) y.dir1=x;
];

[ initialise x;
  objectloop (x ofclass room) {
    autodir(x,n_to,s_to);
    autodir(x,e_to,w_to);
    autodir(x,ne_to,sw_to);
    autodir(x,nw_to,se_to);
    autodir(x,in_to,out_to);
    autodir(x,u_to,d_to);
  };
  location = starting_point;
];

room hallway "Hallway"
  with description "This is a pretty dull hallway.",
  n_to kitchen;

room kitchen "Kitchen"
  with description "You are in a well-appointed and modern kitchen.",
  n_to back_door;

This makes the game engine itself loop through every object of the class "room" before the game starts, check whether it leads to another room in every possible direction, and if so, ensures that the room it leads to leads back again in the opposite direction.

In this case the hallway would lead back to wherever it was reached from (the front door, perhaps, not shown above), and the kitchen automatically leads south back to the hallway.

One-way?

However, the above would prevent you from implementing one-way connections between rooms (where you either can't get back at all, or the direction is not what you might expect, due to a bend in the path between them, for example).

So, here's how to extend the idea to permit directions such as e_to_nr which means "east leads to this room, but it's a non-return route":

class room has light,
  with n_to, s_to, e_to, w_to,
    ne_to, nw_to, se_to, sw_to,
    in_to, out_to, u_to, d_to,
    n_to_nr, s_to_nr, e_to_nr, w_to_nr,
    ne_to_nr, nw_to_nr, se_to_nr, sw_to_nr,
    in_to_nr, out_to_nr, u_to_nr, d_to_nr;

[ autodir x dir1 dir2 y;
  y=x.dir1; if (metaclass(y)==object && y ofclass room) y.dir2=x;
  y=x.dir2; if (metaclass(y)==object && y ofclass room) y.dir1=x;
];

[ oneway x dir1 dir2 y;
  y=x.dir1; if (metaclass(y)==object && x ofclass room) x.dir2=x.dir1;
];

[ initialise x;
  objectloop (x ofclass room) {
    autodir(x,n_to,s_to);
    autodir(x,e_to,w_to);
    autodir(x,ne_to,sw_to);
    autodir(x,nw_to,se_to);
    autodir(x,in_to,out_to);
    autodir(x,u_to,d_to);
  };
  objectloop (x ofclass room) {
    oneway(x,n_to_nr,n_to);
    oneway(x,s_to_nr,s_to);
    oneway(x,e_to_nr,e_to);
    oneway(x,w_to_nr,w_to);
    oneway(x,ne_to_nr,ne_to);
    oneway(x,nw_to_nr,nw_to);
    oneway(x,se_to_nr,se_to);
    oneway(x,sw_to_nr,sw_to);
    oneway(x,in_to_nr,in_to);
    oneway(x,out_to_nr,out_to);
    oneway(x,u_to_nr,u_to);
    oneway(x,d_to_nr,d_to);
  };

  location = starting_point;
];

room hallway "Hallway"
  with description "This is a pretty dull hallway.",
  n_to_nr kitchen;

room kitchen "Kitchen"
  with description "You are in a well-appointed and modern kitchen.",
  n_to back_door;

In this case the hallway still leads north to the kitchen, but no automatic return path is inserted, so you can no longer go south from the kitchen back to the hallway.


Go up
Return to main index.