Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / frotz / main.c
1 /* main.c - Frotz V2.40 main function
2  *      Copyright (c) 1995-1997 Stefan Jokisch
3  *
4  * This file is part of Frotz.
5  *
6  * Frotz is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Frotz is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /*
22  * This is an interpreter for Infocom V1 to V6 games. It also supports
23  * the recently defined V7 and V8 games. Please report bugs to
24  *
25  *    s.jokisch@avu.de
26  *
27  */
28
29 #include "frotz.h"
30
31 #ifndef MSDOS_16BIT
32 #define cdecl
33 #endif
34
35 extern void interpret (void);
36 extern void init_memory (void);
37 extern void init_proc (void);
38 extern void init_sound (void);
39 extern void init_text (void);
40 extern void init_undo (void);
41 extern void reset_memory (void);
42
43 /* Story file name, id number and size */
44
45 char *story_name = 0;
46
47 enum story story_id = UNKNOWN;
48 long story_size = 0;
49
50 /* Story file header data */
51
52 zbyte h_version = 0;
53 zbyte h_config = 0;
54 zword h_release = 0;
55 zword h_resident_size = 0;
56 zword h_start_pc = 0;
57 zword h_dictionary = 0;
58 zword h_objects = 0;
59 zword h_globals = 0;
60 zword h_dynamic_size = 0;
61 zword h_flags = 0;
62 zbyte h_serial[6] = { 0, 0, 0, 0, 0, 0 };
63 zword h_abbreviations = 0;
64 zword h_file_size = 0;
65 zword h_checksum = 0;
66 zbyte h_interpreter_number = 0;
67 zbyte h_interpreter_version = 0;
68 zbyte h_screen_rows = 0;
69 zbyte h_screen_cols = 0;
70 zword h_screen_width = 0;
71 zword h_screen_height = 0;
72 zbyte h_font_height = 1;
73 zbyte h_font_width = 1;
74 zword h_functions_offset = 0;
75 zword h_strings_offset = 0;
76 zbyte h_default_background = 0;
77 zbyte h_default_foreground = 0;
78 zword h_terminating_keys = 0;
79 zword h_line_width = 0;
80 zbyte h_standard_high = 1;
81 zbyte h_standard_low = 1;
82 zword h_alphabet = 0;
83 zword h_extension_table = 0;
84 zbyte h_user_name[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
85
86 zword hx_table_size = 0;
87 zword hx_mouse_x = 0;
88 zword hx_mouse_y = 0;
89 zword hx_unicode_table = 0;
90 zword hx_flags = 0;
91 zword hx_fore_colour = 0;
92 zword hx_back_colour = 0;
93
94 /* Stack data */
95
96 zword stack[STACK_SIZE];
97 zword *sp = 0;
98 zword *fp = 0;
99 zword frame_count = 0;
100
101 /* IO streams */
102
103 bool ostream_screen = TRUE;
104 bool ostream_script = FALSE;
105 bool ostream_memory = FALSE;
106 bool ostream_record = FALSE;
107 bool istream_replay = FALSE;
108 bool message = FALSE;
109
110 /* Current window and mouse data */
111
112 int cwin = 0;
113 int mwin = 0;
114
115 int mouse_y = 0;
116 int mouse_x = 0;
117 int menu_selected = 0;
118
119 /* Window attributes */
120
121 bool enable_wrapping = FALSE;
122 bool enable_scripting = TRUE;
123 bool enable_scrolling = FALSE;
124 bool enable_buffering = FALSE;
125
126 /* User options */
127
128 int option_attribute_assignment = 0;
129 int option_attribute_testing = 0;
130 int option_context_lines = 0;
131 int option_object_locating = 0;
132 int option_object_movement = 0;
133 int option_left_margin = 0;
134 int option_right_margin = 0;
135 int option_ignore_errors = 0;
136 int option_piracy = 0;
137 int option_undo_slots = MAX_UNDO_SLOTS;
138 int option_expand_abbreviations = 0;
139 int option_script_cols = 80;
140 int option_save_quetzal = 1;
141 int option_err_report_mode = ERR_DEFAULT_REPORT_MODE;
142
143 int option_sound = 1;
144 char *option_zcode_path;
145
146
147 /* Size of memory to reserve (in bytes) */
148
149 long reserve_mem = 0;
150
151 /*
152  * z_piracy, branch if the story file is a legal copy.
153  *
154  *      no zargs used
155  *
156  */
157
158 void z_piracy (void)
159 {
160
161     branch (!option_piracy);
162
163 }/* z_piracy */
164
165 /*
166  * main
167  *
168  * Prepare and run the game.
169  *
170  */
171
172 #include "glk.h"
173 #include "glkstart.h"
174
175 static int myargc;
176 static char **myargv;
177
178 glkunix_argumentlist_t glkunix_arguments[] =
179 {
180 { "-a", glkunix_arg_NoValue, "-a: watch attribute setting" },
181 { "-A", glkunix_arg_NoValue, "-A: watch attribute testing" },
182 { "-i", glkunix_arg_NoValue, "-i: ignore fatal errors" },
183 { "-o", glkunix_arg_NoValue, "-o: watch object movement" },
184 { "-O", glkunix_arg_NoValue, "-O: watch object locating" },
185 { "-P", glkunix_arg_NoValue, "-P: alter piracy opcode" },
186 { "-Q", glkunix_arg_NoValue, "-Q: use old-style save format" },
187 { "-t", glkunix_arg_NoValue, "-t: set Tandy bit" },
188 { "-x", glkunix_arg_NoValue, "-x: expand abbreviations g/x/z" },
189 { "-s", glkunix_arg_NumberValue, "-s: random number seed value" },
190 { "-S", glkunix_arg_NumberValue, "-S: transcript width" },
191 { "-u", glkunix_arg_NumberValue, "-u: slots for multiple undo" },
192 { "-Z", glkunix_arg_NumberValue, "-Z: error checking mode" },
193 { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
194 { NULL, glkunix_arg_End, NULL }
195 };
196
197 int glkunix_startup_code(glkunix_startup_t *data)
198 {
199     myargc = data->argc;
200     myargv = data->argv;
201
202     os_init_setup ();
203     os_process_arguments (myargc, myargv);
204
205     init_buffer ();
206     init_err ();
207     init_memory ();
208     init_proc ();
209     init_sound ();
210     init_text ();
211
212     os_init_screen ();
213
214     init_undo ();
215     z_restart ();
216     return TRUE;
217 }
218
219 void glk_main (void)
220 {
221     interpret ();
222     reset_memory ();
223 }
224