aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/tads/tads2/play.cpp
blob: 0c78c6dd094be88096dceb31d937df91d24eb67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/tads/tads2/play.h"
#include "glk/tads/tads2/error.h"
#include "glk/tads/tads2/file_io.h"

namespace Glk {
namespace TADS {
namespace TADS2 {

void plygo(runcxdef *run, voccxdef *voc, tiocxdef *tio, objnum preinit, char *restore_fname) {
   int       err;
    errcxdef *ec = run->runcxerr;
    char      filbuf[128];
    int       first_time;
    int noreg inited = FALSE;

    NOREG((&inited));

    first_time = TRUE;

    /* 
     *   Write out the special <?T2> HTML sequence, in case we're on an HTML
     *   system.  This tells the HTML parser to use the parsing rules for
     *   TADS 2 callers. 
     */
    outformat("\\H+<?T2>\\H-");
    
startover:
    if (!inited)
    {
        /* use Me as the format-string actor for preinit and init */
        tiosetactor(voc->voccxtio, voc->voccxme);

        /*
         *   Run preinit, if it hasn't been run yet.  Note that we only
         *   do this the first time through.  If we come back here via the
         *   restart function, preinit will already have been run in the
         *   restart function itself, so we don't need to run it again.
         */
        if (first_time)
        {
            /* make a note that we've been through here once already */
            first_time = FALSE;

            /* remember the preinit function for later use in restarting */
            voc->voccxpreinit = preinit;

            /* run the preinit() function */
            ERRBEGIN(ec)
            {
                /* reset the interpreter */
                runrst(run);

                /* reset the parser */
                voc_stk_ini(voc, (uint)VOC_STACK_SIZE);

                /* run preinit */
                if (preinit != MCMONINV)
                    runfn(run, preinit, 0);
            }
            ERRCATCH(ec, err)
            {
                /* if they restarted, go back and start over */
                if (err == ERR_RUNRESTART)
                    goto startover;

                /* resignal the error */
                errrse(ec);
            }
            ERREND(ec);
        }
        
        /* 
         *   Run the "init" function.  Do NOT run init if we're restoring
         *   a game directly from the command line AND there's an
         *   initRestore function defined. 
         */
        if (restore_fname == 0 || voc->voccxinitrestore == MCMONINV)
        {
            ERRBEGIN(ec)
            {
                /* reset the interpreter */
                runrst(run);

                /* reset the parser */
                voc_stk_ini(voc, (uint)VOC_STACK_SIZE);

                /* run init */
                runfn(run, (objnum)voc->voccxini, 0);
            }
            ERRCATCH(ec, err)
            {
                /* if they restarted, go back and start over */
                if (err == ERR_RUNRESTART)
                    goto startover;
                
                /* resignal the error */
                errrse(ec);
            }
            ERREND(ec);
        }
    }
    
    /* next time through, we'll need to run init again */
    inited = FALSE;

    /* 
     *   check for startup parameter file to restore - if there's a
     *   system-specific parameter file specified, pretend that it was
     *   specified as the restore file 
     */
    if (os_paramfile(filbuf))
        restore_fname = filbuf;

    /* check for a file to restore */
    if (restore_fname != 0)
    {
        /*
         *   Check to see if the game file supports the initRestore
         *   function.  If so, call it to restore the game.  If not,
         *   restore the game directly. 
         */
        if (voc->voccxinitrestore != MCMONINV)
        {
            char restore_buf[OSFNMAX*2];
            char *src;
            char *dst;
            
            /* convert any backslashes to double backslashes */
            for (src = restore_fname, dst = restore_buf ;
                 *src != '\0' && dst + 2 < restore_buf + sizeof(restore_buf) ;
                 ++src)
            {
                switch(*src)
                {
                case '\\':
                    /* it's a backslash - double it */
                    *dst++ = '\\';
                    *dst++ = '\\';
                    break;

                default:
                    /* copy the character as-is */
                    *dst++ = *src;
                }
            }
            
            /* 
             *   all the game's initRestore function with the name of
             *   saved game file to restore as the argument 
             */

            /* reset the interpreter */
            runrst(run);

            /* reset the parser */
            voc_stk_ini(voc, (uint)VOC_STACK_SIZE);

            /* push the game file name and run initRestore */
            runpstr(run, restore_buf, dst - restore_buf, 0);
            runfn(run, (objnum)voc->voccxinitrestore, 1);
        }
        else
        {
            /* restore the game */
            os_printz("\n\n[Restoring saved game]\n\n");
            err = fiorso(voc, restore_fname);
            if (err)
            {
                char buf[60 + OSFNMAX];

                sprintf(buf, "\n\nError: unable to restore file \"%s\"\n\n",
                        restore_fname);
                os_printz(buf);
            }
        }

        /* forget the saved game name, in case we restore */
        restore_fname = 0;
    }

    /* clear out the redo command buffer */
    voc->voccxredobuf[0] = '\0';
    
    /* read and execute commands */
    for (;;)
    {
        char buf[128];
        
        err = 0;
        ERRBEGIN(ec)
            
        /* read a new command if there's nothing to redo */
        if (!voc->voccxredo)
        {
            /* reset hidden output so we're showing output */
            tioshow(tio);
            tioflush(tio);

            /* clear the interpreter stack */
            runrst(run);

            /* read a command */
            vocread(voc, MCMONINV, MCMONINV, buf, (int)sizeof(buf), 0);

            /* special qa checking */
            if (buf[0] == '@')
            {
                int   quiet = FALSE;
                char *p;
                
                p = buf + 1;
                if (*p == '@')
                {
                    /* turn off MORE mode */
                    setmore(0);

                    /* set NONSTOP mode in the OS layer */
                    os_nonstop_mode(TRUE);

                    /* skip the extra '@' */
                    ++p;
                }
                else if (*p == '!')
                {
                    quiet = TRUE;
                    ++p;
                }
                while (*p != '\0' && t_isspace(*p)) ++p;
                if (*p != '\0')
                {
                    /* open the named file */
                    qasopn(p, quiet);
                }
                else
                {
                    char fname[256];

                    /* no file was named - ask the user to select a file */
                    if (tio_askfile("Read script file:", fname, sizeof(fname),
                                    OS_AFP_OPEN, OSFTCMD) == 0)
                        qasopn(fname, quiet);
                }
                goto end_loop;
            }
        }

        /* 
         *   If there's redo in the redo buffer, use it now.  If the
         *   buffer is empty and the redo flag is set, we'll just
         *   re-execute whatever's in our internal buffer.
         */
        if (voc->voccxredo && voc->voccxredobuf[0] != '\0')
        {
            /* copy the redo buffer into our internal buffer */
            strcpy(buf, voc->voccxredobuf);

            /* we've consumed it now, so clear it out */
            voc->voccxredobuf[0] = '\0';
        }

        /* we've now consumed the redo */
        voc->voccxredo = FALSE;

        /* clear any pending break that's queued up */
        (void)os_break();

        /* execute the command */
        (void)voccmd(voc, buf, (uint)sizeof(buf));
        
    end_loop:
        ERRCATCH(ec, err)
        {
            if (err != ERR_RUNQUIT
                && err != ERR_RUNRESTART
                && !(err == ERR_RUNABRT && voc->voccxredo))
                errclog(ec);
        }
        ERREND(ec);

        /* on interrupt, undo last command (which was partially executed) */
        if (err == ERR_USRINT && voc->voccxundo)
        {
            ERRBEGIN(ec)
                objundo(voc->voccxmem, voc->voccxundo);
            ERRCATCH(ec, err)
                if (err != ERR_NOUNDO && err != ERR_ICUNDO)
                    errrse(ec);
            ERREND(ec)
        }
            
        /* if they want to quit, we're done */
        if (err == ERR_RUNQUIT)
            break;
        else if (err == ERR_RUNRESTART)
            goto startover;
    }

    /*
     *   If we're quitting, give the debugger one last chance at taking
     *   control.  If it just returns, we can go ahead and terminate, but
     *   if it wants it can restart the game by calling bifrst() as
     *   normal.  
     */
    ERRBEGIN(ec)
    {
        /* clear anything in the debugger stack trace */
        run->runcxdbg->dbgcxfcn = 0;
        run->runcxdbg->dbgcxdep = 0;

        /* tell the debugger the game has exited */
        dbguquitting(run->runcxdbg);
    }
    ERRCATCH(ec, err)
    {
        switch(err)
        {
        case ERR_RUNRESTART:
            /* they restarted the game - re-enter the play loop */
            goto startover;

        case ERR_RUNQUIT:
            /* quitting - proceed to return as normal */
            break;

        default:
            /* resignal any other error */
            errrse(ec);
        }
    }
    ERREND(ec);
}

} // End of namespace TADS2
} // End of namespace TADS
} // End of namespace Glk