| | 1 | /* |
| | 2 | * Copyright (c) 2000, 2002 Michael J. Roberts. Permission is |
| | 3 | * granted to anyone to copy and use this file for any purpose. |
| | 4 | * |
| | 5 | * This is a starter TADS 3 source file. This is a complete TADS 3 |
| | 6 | * program that you can compile and run. |
| | 7 | * |
| | 8 | * To compile this game in TADS Workbench, open the "Build" menu and |
| | 9 | * select "Compile for Debugging." To run the game, after compiling it, |
| | 10 | * open the "Debug" menu and select "Go." |
| | 11 | * |
| | 12 | * This is the "introductory" starter program; it provides an example of |
| | 13 | * how to set up a T3 program. |
| | 14 | */ |
| | 15 | |
| | 16 | /* include the TADS and T3 system headers */ |
| | 17 | #include <tads.h> |
| | 18 | #include <t3.h> |
| | 19 | |
| | 20 | /* main program entrypoint */ |
| | 21 | _main(args) |
| | 22 | { |
| | 23 | try |
| | 24 | { |
| | 25 | /* set up the default output routine */ |
| | 26 | t3SetSay(&_say_embed); |
| | 27 | |
| | 28 | /* if we haven't run preinitialization yet, run it now */ |
| | 29 | if (!_global._preinited) |
| | 30 | preinit(); |
| | 31 | |
| | 32 | /* if we're not in preinit mode, run the main program */ |
| | 33 | if (!t3GetVMPreinitMode()) |
| | 34 | main(args); |
| | 35 | } |
| | 36 | catch(RuntimeError exc) |
| | 37 | { |
| | 38 | "Unhandled exception: <<exc.exceptionMessage>>\n"; |
| | 39 | } |
| | 40 | } |
| | 41 | |
| | 42 | /* |
| | 43 | * Function to use as the default output routine - we'll just pass |
| | 44 | * through the output to the TADS tadsSay() built-in |
| | 45 | */ |
| | 46 | _say_embed(val) { tadsSay(val); } |
| | 47 | |
| | 48 | /* |
| | 49 | * this object is a place where we can store some global state |
| | 50 | * information |
| | 51 | */ |
| | 52 | _global: object |
| | 53 | _preinited = nil |
| | 54 | ; |
| | 55 | |
| | 56 | /* |
| | 57 | * RuntimeError class - the VM will throw a new instance of this class |
| | 58 | * each time a run-time error occurs. |
| | 59 | */ |
| | 60 | class RuntimeError: object |
| | 61 | construct(errno, ...) { errno_ = errno; } |
| | 62 | exceptionMessage = '' |
| | 63 | errno_ = 0 |
| | 64 | ; |
| | 65 | |
| | 66 | /* |
| | 67 | * Preinitialization function - this routine can perform any |
| | 68 | * time-consuming static initialization that can be done immediately |
| | 69 | * after compilation, so that it doesn't have to be done every time the |
| | 70 | * program starts up. |
| | 71 | */ |
| | 72 | preinit() |
| | 73 | { |
| | 74 | /* put your pre-initialization code here */ |
| | 75 | } |
| | 76 | |
| | 77 | /* |
| | 78 | * Main program body. Note that _main() is the true entrypoint into the |
| | 79 | * program, but _main() calls this routine as soon as it has completed |
| | 80 | * some set-up operations. preinit() will always be called (either |
| | 81 | * during the original compilation, or at program startup, depending on |
| | 82 | * the compilation mode) before this routine is called. |
| | 83 | */ |
| | 84 | main(args) |
| | 85 | { |
| | 86 | /* show the VM banner and a blank line */ |
| | 87 | tadsSay(t3GetVMBanner()); "\b"; |
| | 88 | |
| | 89 | /* show an introductory message */ |
| | 90 | "Hello from TADS 3! Type any command you like; SCORE will increase |
| | 91 | the score, QUIT will end the program, and everything else will |
| | 92 | be ignored. "; |
| | 93 | |
| | 94 | /* read commands */ |
| | 95 | command_loop: |
| | 96 | for (;;) |
| | 97 | { |
| | 98 | local cmd; |
| | 99 | |
| | 100 | /* increase the turn counter */ |
| | 101 | ++global.turncount; |
| | 102 | |
| | 103 | /* update the status line */ |
| | 104 | "<banner id=StatusLine height=previous border> |
| | 105 | <body bgcolor=statusbg text=statustext><b> |
| | 106 | Command Line Room |
| | 107 | </b> |
| | 108 | <tab align=right><i><<global.score>>/<<global.turncount>></i> |
| | 109 | </banner>"; |
| | 110 | |
| | 111 | /* show a prompt and enter input font mode */ |
| | 112 | "\b><font face='TADS-Input'>"; |
| | 113 | |
| | 114 | /* read a command */ |
| | 115 | cmd = inputLine(); |
| | 116 | |
| | 117 | /* end the input font mode */ |
| | 118 | "</font>"; |
| | 119 | |
| | 120 | /* remove any leading spaces */ |
| | 121 | cmd = rexReplace('^ +', cmd, '', ReplaceOnce); |
| | 122 | |
| | 123 | /* remove any trailing spaces */ |
| | 124 | cmd = rexReplace(' +$', cmd, '', ReplaceOnce); |
| | 125 | |
| | 126 | /* see what we have */ |
| | 127 | switch(cmd.toLower()) |
| | 128 | { |
| | 129 | case 'quit': |
| | 130 | break command_loop; |
| | 131 | |
| | 132 | case 'score': |
| | 133 | "Your score has increased by ten points. "; |
| | 134 | global.score += 10; |
| | 135 | break; |
| | 136 | |
| | 137 | default: |
| | 138 | "That doesn't seem to work&emdash;yet!!! "; |
| | 139 | break; |
| | 140 | } |
| | 141 | } |
| | 142 | |
| | 143 | "Thanks for playing!!!\n"; |
| | 144 | } |
| | 145 | |
| | 146 | /* |
| | 147 | * 'global' - an object where we can store some global state information |
| | 148 | */ |
| | 149 | global: object |
| | 150 | score = 0 |
| | 151 | turncount = 0 |
| | 152 | ; |
| | 153 | |