Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / nitfol / main.c
1 /*  Nitfol - z-machine interpreter using Glk for output.
2     Copyright (C) 1999  Evin Robertson
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18     The author can be reached at nitfol@deja.com
19 */
20 #include "nitfol.h"
21
22 #include "gi_blorb.h"
23
24
25 static void set_zfile(strid_t file)
26 {
27   glk_stream_set_position(file, 0, seekmode_End);
28   total_size = glk_stream_get_position(file);
29   glk_stream_set_position(file, 0, seekmode_Start);
30
31   current_zfile = file;
32   zfile_offset = 0;
33
34   if(!load_header(file, total_size, FALSE)) {
35     /* FIMXE: add code to check to see if it's a jzexe product, and perhaps
36        even code to grab it out of disk images */
37     current_zfile = NULL;
38     return;
39   }
40   
41   glk_stream_set_position(file, zfile_offset, seekmode_Start);
42 }
43
44
45 static strid_t savefile;
46
47 int game_use_file(strid_t file)
48 {
49   giblorb_map_t *map;
50   giblorb_result_t res;
51   strid_t z;
52   if(wrap_gib_create_map(file, &map) == giblorb_err_None) {
53     if(!current_zfile) {
54       if(wrap_gib_load_resource(map, giblorb_method_FilePos, &res,
55                 giblorb_ID_Exec, 0) == giblorb_err_None) {
56         current_zfile = file;
57         zfile_offset = res.data.startpos;
58         total_size = res.length;
59       }
60     }
61     if(!blorb_file)
62       wrap_gib_count_resources(map, giblorb_ID_Pict, &imagecount, NULL, NULL);
63     wrap_gib_destroy_map(map);
64
65     if(!blorb_file) {
66       if(wrap_gib_set_resource_map(file) == giblorb_err_None) {
67         blorb_file = file;
68         return TRUE;
69       }
70     }
71   }
72
73   if((z = quetzal_findgamefile(file)) != 0) {
74     savefile = file;
75     file = z;
76   }
77
78   if(!current_zfile) {
79     set_zfile(file);
80
81     return TRUE;
82   }
83
84   return FALSE;
85 }
86
87
88 void glk_main(void)
89 {
90   if(!current_zfile)
91   {
92     winid_t tempwin;
93     tempwin = glk_window_open(0, 0, 100, wintype_TextBuffer, 0);
94         glk_set_window(tempwin);
95         glk_set_style(style_Preformatted);
96         glk_put_string(
97 "Usage: nitfol [OPTIONS] gamefile\n"
98 " -i, -ignore      Ignore Z-machine strictness errors\n"
99 " -f, -fullname    For running under Emacs or DDD\n"
100 " -x, -command     Read commands from this file\n"
101 " -P, -pirate      Aye, matey\n"
102 "     -spell       Perform spelling correction\n"
103 "     -expand      Expand one letter abbreviations\n"
104 " -s, -symbols     Specify symbol file for game\n"
105 " -t, -tandy       Censors some Infocom games\n"
106 " -T, -transcript  Write transcript to this file\n"
107 " -d, -debug       Enter debugger immediatly\n"
108 "     -prompt      Specify debugging prompt\n"
109 "     -autoundo    Ensure '@save_undo' is called every turn\n"
110 " -S, -stacklimit  Exit when the stack is this deep\n"
111 " -a, -alias       Specify an alias\n"
112 "     -ralias      Specify an recursive alias\n"
113 "     -unalias     Remove an alias\n"
114 " -r, -random      Set random seed\n"
115 "     -mapsym      Specify mapping glyphs\n"
116 "     -mapsize     Specify map size\n"
117 "     -maploc      Specify map location\n"
118 "     -terpnum     Specify interpreter number\n"
119 "     -terpver     Specify interpreter version\n");
120     glk_exit();
121   }
122   z_init(current_zfile);
123   if(savefile) {
124     if(restorequetzal(savefile)) {
125       if(zversion <= 3)
126         mop_take_branch();
127       else
128         mop_store_result(2);
129     }
130   }
131   init_undo();
132   decode();
133 }