Transit System

Hi,
I need some help and advice on a transit system, I have in my game. The system consists of four(4) stations, with a train going around in a loop. I've looked at Pixie's example http://docs.textadventures.co.uk/quest/transit_system.html, but that only seemed to offer instant travel, and I wanted to be able to explore the train. At the moment my system is very involved with lots of 'If' statements and could become longer if I decided to add more 'Stations'. I was wondering if there might be a neater way of doing this. The script uses a timer, firing every 10 seconds to move the train, and will only tell you where the train is on it's route when the player is in a station.
Script follows:

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Test - Medway4">
    <gameid>2b362ff4-a0f6-470c-b877-51223a18e273</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <statusandcompassattop />
    <commandpane type="boolean">false</commandpane>
    <customstatuspane type="boolean">false</customstatuspane>
    <start type="script">
    </start>
    <author>Doctor Agon</author>
  </game>
  <object name="Northgate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
      <medway type="int">0</medway>
      <statusattributes type="stringdictionary">
        <item>
          <key>medway</key>
          <value></value>
        </item>
      </statusattributes>
      <station type="int">0</station>
      <s type="int">0</s>
    </object>
    <exit alias="north" to="Kings Cross Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Eastgate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Fenchurch Street Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Foregate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Liverpool Street Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Watergate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Marylebone Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Kings Cross Station">
    <inherit name="editor_room" />
    <station type="int">1</station>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="south" to="Northgate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="medway1" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="Fenchurch Street Station">
    <inherit name="editor_room" />
    <station type="int">2</station>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="south" to="Eastgate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="medway2" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="Liverpool Street Station">
    <inherit name="editor_room" />
    <station type="int">3</station>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="south" to="Foregate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="medway3" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="Marylebone Station">
    <inherit name="editor_room" />
    <station type="int">4</station>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="south" to="Watergate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="medway4" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="room101">
    <inherit name="editor_room" />
    <object name="Medway Express">
      <beforeenter type="script">
      </beforeenter>
      <station type="int">0</station>
      <usedefaultprefix type="boolean">false</usedefaultprefix>
      <prefix>the</prefix>
      <exit name="medway5" alias="out">
        <inherit name="outdirection" />
        <runscript />
        <visible type="boolean">false</visible>
        <script type="script">
          MoveObject (player, Medway Express.parent)
        </script>
      </exit>
    </object>
  </object>
  <timer name="train timer">
    <interval>10</interval>
    <script>
      player.medway = player.medway+1
      if (player.medway=1) {
        MoveObject (Medway Express, Kings Cross Station)
        MakeObjectVisible (Medway Express)
        MakeExitVisible (medway1)
        MakeExitVisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has arrived at Kings Cross Station.")
        }
        if (game.pov.parent = Kings Cross Station) {
          msg ("You can go {exit:medway1}")
        }
        else if (game.pov.parent = Medway Express) {
          msg ("You can go {exit:medway5}")
        }
      }
      if (player.medway=2) {
        MakeObjectInvisible (Medway Express)
        MakeExitInvisible (medway1)
        MakeExitInvisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has left Kings Cross Station.")
        }
      }
      if (player.medway=3) {
        MoveObject (Medway Express, Fenchurch Street Station)
        MakeObjectVisible (Medway Express)
        MakeExitVisible (medway2)
        MakeExitVisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has arrived at Fenchurch Street Station.")
        }
        if (game.pov.parent = Fenchurch Street Station) {
          msg ("You can go {exit:medway2}.")
        }
        else if (game.pov.parent = Medway Express) {
          msg ("You can go {exit:medway5}.")
        }
      }
      if (player.medway=4) {
        MakeObjectInvisible (Medway Express)
        MakeExitInvisible (medway2)
        MakeExitInvisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has left Fenchurch Street Station.")
        }
      }
      if (player.medway=5) {
        MoveObject (Medway Express, Liverpool Street Station)
        MakeObjectVisible (Medway Express)
        MakeExitVisible (medway3)
        MakeExitVisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has arrived at Liverpool Street Station.")
        }
        if (game.pov.parent = Liverpool Street Station) {
          msg ("You can go {exit:medway3}")
        }
        else if (game.pov.parent = Medway Express) {
          msg ("You can go {exit:medway5}")
        }
      }
      if (player.medway=6) {
        MakeObjectInvisible (Medway Express)
        MakeExitInvisible (medway3)
        MakeExitInvisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has left Liverpool Street Station.")
        }
      }
      if (player.medway=7) {
        MoveObject (Medway Express, Marylebone Station)
        MakeObjectVisible (Medway Express)
        MakeExitVisible (medway4)
        MakeExitVisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has arrived at Marylebone Station.")
        }
        if (game.pov.parent = Marylebone Station) {
          msg ("You can go {exit:medway4}")
        }
        else if (game.pov.parent = Medway Express) {
          msg ("You can go {exit:medway5}")
        }
      }
      if (player.medway=8) {
        MakeObjectInvisible (Medway Express)
        MakeExitInvisible (medway4)
        MakeExitInvisible (medway5)
        if (HasAttribute(game.pov.parent, "station")) {
          msg ("The Medway Express has left Marylebone Station.")
        }
      }
      if (player.medway=8) {
        player.medway = 0
      }
    </script>
    <enabled />
  </timer>
</asl>

I did think of having each station have the attribute 'station' with a different number and using that somehow to save on the 'If' statements but as yet, I've not been able to work anything out.
I'm hoping someone can help with a neater code.
Thankyou.


Hello,

That's very nice coding!

I hate to even mess with it, but, you asked, so here's what I can think up in a hurry:


Maybe a switch? (It looks a little neater, and it's easier to add on to.)

I added a JS.addTextAndScroll("") to keep the screen moving by itself, too..

...and I changed the You are in to You are aboard on the Medway Express.

  <object name="Medway Express">
      <station type="int">0</station>
      <usedefaultprefix type="boolean">false</usedefaultprefix>
      <prefix>the</prefix>
      <beforeenter type="script">
      </beforeenter>
      <descprefix>You are aboard</descprefix>
      <exit name="medway5" alias="out">
        <inherit name="outdirection" />
        <runscript />
        <visible type="boolean">false</visible>
        <script type="script">
          MoveObject (player, Medway Express.parent)
        </script>
      </exit>
    </object>
  <timer name="train timer">
    <interval>10</interval>
    <enabled />
    <script><![CDATA[
      player.medway = player.medway+1
      switch (player.medway) {
        case (1) {
          MoveObject (Medway Express, Kings Cross Station)
          MakeObjectVisible (Medway Express)
          MakeExitVisible (medway1)
          MakeExitVisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("<br/>The Medway Express has arrived at Kings Cross Station.")
          }
          if (game.pov.parent = Kings Cross Station) {
            msg ("You can go {exit:medway1}")
          }
          else if (game.pov.parent = Medway Express) {
            msg ("You can go {exit:medway5}")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (2) {
          MakeObjectInvisible (Medway Express)
          MakeExitInvisible (medway1)
          MakeExitInvisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("<br/>The Medway Express has left Kings Cross Station.")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (3) {
          MoveObject (Medway Express, Fenchurch Street Station)
          MakeObjectVisible (Medway Express)
          MakeExitVisible (medway2)
          MakeExitVisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has arrived at Fenchurch Street Station.")
          }
          if (game.pov.parent = Fenchurch Street Station) {
            msg ("You can go {exit:medway2}.")
          }
          else if (game.pov.parent = Medway Express) {
            msg ("You can go {exit:medway5}.")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (4) {
          MakeObjectInvisible (Medway Express)
          MakeExitInvisible (medway2)
          MakeExitInvisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has left Fenchurch Street Station.")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (5) {
          MoveObject (Medway Express, Liverpool Street Station)
          MakeObjectVisible (Medway Express)
          MakeExitVisible (medway3)
          MakeExitVisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has arrived at Liverpool Street Station.")
          }
          if (game.pov.parent = Liverpool Street Station) {
            msg ("You can go {exit:medway3}")
          }
          else if (game.pov.parent = Medway Express) {
            msg ("You can go {exit:medway5}")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (6) {
          MakeObjectInvisible (Medway Express)
          MakeExitInvisible (medway3)
          MakeExitInvisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has left Liverpool Street Station.")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (7) {
          MoveObject (Medway Express, Marylebone Station)
          MakeObjectVisible (Medway Express)
          MakeExitVisible (medway4)
          MakeExitVisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has arrived at Marylebone Station.")
          }
          if (game.pov.parent = Marylebone Station) {
            msg ("You can go {exit:medway4}")
          }
          else if (game.pov.parent = Medway Express) {
            msg ("You can go {exit:medway5}")
          }
          JS.addTextAndScroll ("<br/>")
        }
        case (8) {
          MakeObjectInvisible (Medway Express)
          MakeExitInvisible (medway4)
          MakeExitInvisible (medway5)
          if (HasAttribute(game.pov.parent, "station")) {
            msg ("The Medway Express has left Marylebone Station.")
          }
          player.medway = 0
          JS.addTextAndScroll ("<br/>")
        }
      }
    ]]></script>
  </timer>

Here is one way to do it.

It gets the list of stations from going though all objects and getting those that end "station". Then the timer works its way through that list. You should find you can get on and off the train at different stations. I made it go in a loop, with just 5 seconds at each stop for faster testing.

I put the attribute on the train, rather than the player, because that is what it is associated with. I renamed the exits so you can derive the name from the station. The timer then just does one thing as the train arrives and another as it leaves.

So why did you pick the stations from Monopoly?

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Test - Medway4">
    <gameid>2b362ff4-a0f6-470c-b877-51223a18e273</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <statusandcompassattop />
    <commandpane type="boolean">false</commandpane>
    <customstatuspane type="boolean">false</customstatuspane>
    <author>Doctor Agon</author>
    <start type="script">
      game.stations = NewObjectList()
      foreach (o, AllObjects()) {
        if (EndsWith(o.name, " Station")) {
          list add (game.stations, o)
        }
      }
      msg (game.stations)
    </start>
  </game>
  <object name="Northgate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Kings Cross Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Eastgate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Fenchurch Street Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Foregate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Liverpool Street Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Watergate Street">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <exit alias="north" to="Marylebone Station">
      <inherit name="northdirection" />
    </exit>
  </object>
  <object name="Kings Cross Station">
    <inherit name="editor_room" />
    <station type="int">1</station>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <exit alias="south" to="Northgate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="Kings Cross Station to medway" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="Fenchurch Street Station">
    <inherit name="editor_room" />
    <station type="int">2</station>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <exit alias="south" to="Eastgate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="Fenchurch Street Station to medway" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
      <statusattributes type="stringdictionary">
        <item>
          <key>medway</key>
          <value></value>
        </item>
      </statusattributes>
      <station type="int">0</station>
      <s type="int">0</s>
    </object>
  </object>
  <object name="Liverpool Street Station">
    <inherit name="editor_room" />
    <station type="int">3</station>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <exit alias="south" to="Foregate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="Liverpool Street Station to medway" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="Marylebone Station">
    <inherit name="editor_room" />
    <station type="int">4</station>
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <beforeenter type="script">
    </beforeenter>
    <onexit type="script">
    </onexit>
    <exit alias="south" to="Watergate Street">
      <inherit name="southdirection" />
    </exit>
    <exit name="Marylebone Station to medway" alias="in" to="Medway Express">
      <inherit name="indirection" />
      <visible type="boolean">false</visible>
    </exit>
  </object>
  <object name="room101">
    <inherit name="editor_room" />
    <object name="Medway Express">
      <station type="int">0</station>
      <usedefaultprefix type="boolean">false</usedefaultprefix>
      <prefix>the</prefix>
      <beforeenter type="script">
      </beforeenter>
      <state type="int">-1</state>
      <exit name="exit from medway" alias="out">
        <inherit name="outdirection" />
        <runscript />
        <visible type="boolean">false</visible>
        <script type="script">
          MoveObject (player, Medway Express.parent)
        </script>
      </exit>
    </object>
  </object>
  <timer name="train timer">
    <interval>5</interval>
    <enabled />
    <script><![CDATA[
      Medway Express.state = Medway Express.state+1
      if (Medway Express.state >= ListCount(game.stations) * 2) {
        Medway Express.state = 0
      }
      station = ObjectListItem(game.stations, Medway Express.state / 2)
      stationexit = GetObject(station.name + " to medway")
      if (Medway Express.state % 2 = 0) {
        MoveObject (Medway Express, station)
        MakeExitVisible (stationexit)
        MakeExitVisible (exit from medway)
        exit from medway.to = station
        msg ("The Medway Express has arrived at " + station.name + ".")
        if (game.pov.parent = station) {
          msg ("You can go {exit:" + stationexit.name + "}.")
        }
        else if (game.pov.parent = Medway Express) {
          msg ("You can go {exit:exit from medway}.")
        }
      }
      else {
        MakeExitInvisible (exit from medway)
        MakeExitInvisible (stationexit)
        msg ("The Medway Express has left " + station.name + ".")
      }
    ]]></script>
  </timer>
</asl>

Cheers Pixie and K.V., tried to do something similar to your suggestion Pixie, but got into such a mess went back to what I knew (Medway 4, being the forth version of trying to do this bit of coding).

Have adopted K.V.s use of the JavaScript to scroll the screen, but attached an 'If' statement to it, to check if the player is at a station, so I've only used it the once, instead of at every station.

@Pixie: Would I get the same result if instead of
if (EndsWith(o.name, " Station")) {
I used
if (HasAttribute(o.name, "station")) {

I just love the game of monopoly, played it since I was a kid. Incidentally, the streets connected to the stations are all in Chester.


It would probably be if (HasAttribute(o, "station")) {


Thanks a lot to you! worked!


classic monopoly board game was awesome... but a much more fun game... the game of life board game... it was a lot of fun... as a kid... getting/reading the different stuff... getting married... having/getting issues/penalities/bonuses... was funny as a kid... sighs:

ht.tps://en.wikipedia.org/wiki/The_Game_of_Life

unfortunately... I had got rid of it... wish I hadn't... sighs.


Log in to post a reply.

Support

Forums