Debian Codegraph @

src/params.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "params.h"
20
21 #include "arg_parse.h"
22 #include "file.h"
23 #include "joystick.h"
24 #include "loudness.h"
25 #include "network.h"
26 #include "opentyr.h"
27 #include "varz.h"
28 #include "xmas.h"
29
30 #include <assert.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdint.h>
34 #include <string.h>
35
36 JE_boolean richMode = false, constantPlay = false, constantDie = false;
37
38 /* YKS: Note: LOOT cheat had non letters removed. */
39 const char pars[][9] = {
40 "LOOT", "RECORD", "NOJOY", "CONSTANT", "DEATH", "NOSOUND", "NOXMAS", "YESXMAS"
41 };
42
43 void JE_paramCheck(int argc, char *argv[])
44 {
45 const Options options[] =
46 {
47 { 'h', 'h', "help", false },
48
49 { 's', 's', "no-sound", false },
50 { 'j', 'j', "no-joystick", false },
51 { 'x', 'x', "no-xmas", false },
52
53 { 't', 't', "data", true },
54
55 { 'n', 'n', "net", true },
56 { 256, 0, "net-player-name", true }, // TODO: no short codes because there should
57 { 257, 0, "net-player-number", true }, // be a menu for entering these in the future
58 { 'p', 'p', "net-port", true },
59 { 'd', 'd', "net-delay", true },
60
61 { 'X', 'X', "xmas", false },
62 { 'c', 'c', "constant", false },
63 { 'k', 'k', "death", false },
64 { 'r', 'r', "record", false },
65 { 'l', 'l', "loot", false },
66
67 { 0, 0, NULL, false}
68 };
69
70 Option option;
71
72 for (; ; )
73 {
74 option = parse_args(argc, (const char **)argv, options);
75
76 if (option.value == NOT_OPTION)
77 break;
78
79 switch (option.value)
80 {
81 case INVALID_OPTION:
82 case AMBIGUOUS_OPTION:
83 case OPTION_MISSING_ARG:
84 fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
85 exit(EXIT_FAILURE);
86 break;
87
88 case 'h':
89 printf("Usage: %s [OPTION...]\n\n"
90 "Options:\n"
91 " -h, --help Show help about options\n\n"
92 " -s, --no-sound Disable audio\n"
93 " -j, --no-joystick Disable joystick/gamepad input\n"
94 " -x, --no-xmas Disable Christmas mode\n\n"
95 " -t, --data=DIR Set Tyrian data directory\n\n"
96 " -n, --net=HOST[:PORT] Start a networked game\n"
97 " --net-player-name=NAME Sets local player name in a networked game\n"
98 " --net-player-number=NUMBER Sets local player number in a networked game\n"
99 " (1 or 2)\n"
100 " -p, --net-port=PORT Local port to bind (default is 1333)\n"
101 " -d, --net-delay=FRAMES Set lag-compensation delay (default is 1)\n", argv[0]);
102 exit(0);
103 break;
104
105 case 's':
106 // Disables sound/music usage
107 audio_disabled = true;
108 break;
109
110 case 'j':
111 // Disables joystick detection
112 ignore_joystick = true;
113 break;
114
115 case 'x':
116 xmas = false;
117 break;
118
119 // set custom Tyrian data directory
120 case 't':
121 custom_data_dir = option.arg;
122 break;
123
124 case 'n':
125 isNetworkGame = true;
126
127 intptr_t temp = (intptr_t)strchr(option.arg, ':');
128 if (temp)
129 {
130 temp -= (intptr_t)option.arg;
131
132 int temp_port = atoi(&option.arg[temp + 1]);
133 if (temp_port > 0 && temp_port < 49152)
134 network_opponent_port = temp_port;
135 else
136 {
137 fprintf(stderr, "%s: error: invalid network port number\n", argv[0]);
138 exit(EXIT_FAILURE);
139 }
140
141 network_opponent_host = malloc(temp + 1);
142 SDL_strlcpy(network_opponent_host, option.arg, temp + 1);
143 }
144 else
145 {
146 network_opponent_host = malloc(strlen(option.arg) + 1);
147 strcpy(network_opponent_host, option.arg);
148 }
149 break;
150
151 case 256: // --net-player-name
152 network_player_name = malloc(strlen(option.arg) + 1);
153 strcpy(network_player_name, option.arg);
154 break;
155
156 case 257: // --net-player-number
157 {
158 int temp = atoi(option.arg);
159 if (temp >= 1 && temp <= 2)
160 thisPlayerNum = temp;
161 else
162 {
163 fprintf(stderr, "%s: error: invalid network player number\n", argv[0]);
164 exit(EXIT_FAILURE);
165 }
166 break;
167 }
168 case 'p':
169 {
170 int temp = atoi(option.arg);
171 if (temp > 0 && temp < 49152)
172 network_player_port = temp;
173 else
174 {
175 fprintf(stderr, "%s: error: invalid network port number\n", argv[0]);
176 exit(EXIT_FAILURE);
177 }
178 break;
179 }
180 case 'd':
181 {
182 int temp;
183 if (sscanf(option.arg, "%d", &temp) == 1)
184 network_delay = 1 + temp;
185 else
186 {
187 fprintf(stderr, "%s: error: invalid network delay value\n", argv[0]);
188 exit(EXIT_FAILURE);
189 }
190 break;
191 }
192 case 'X':
193 xmas = true;
194 break;
195
196 case 'c':
197 /* Constant play for testing purposes (C key activates invincibility)
198 This might be useful for publishers to see everything - especially
199 those who can't play it */
200 constantPlay = true;
201 break;
202
203 case 'k':
204 constantDie = true;
205 break;
206
207 case 'r':
208 record_demo = true;
209 break;
210
211 case 'l':
212 // Gives you mucho bucks
213 richMode = true;
214 break;
215
216 default:
217 assert(false);
218 break;
219 }
220 }
221
222 // legacy parameter support
223 for (int i = option.argn; i < argc; ++i)
224 {
225 for (uint j = 0; j < strlen(argv[i]); ++j)
226 argv[i][j] = toupper((unsigned char)argv[i][j]);
227
228 for (uint j = 0; j < COUNTOF(pars); ++j)
229 {
230 if (strcmp(argv[i], pars[j]) == 0)
231 {
232 switch (j)
233 {
234 case 0:
235 richMode = true;
236 break;
237 case 1:
238 record_demo = true;
239 break;
240 case 2:
241 ignore_joystick = true;
242 break;
243 case 3:
244 constantPlay = true;
245 break;
246 case 4:
247 constantDie = true;
248 break;
249 case 5:
250 audio_disabled = true;
251 break;
252 case 6:
253 xmas = false;
254 break;
255 case 7:
256 xmas = true;
257 break;
258
259 default:
260 assert(false);
261 break;
262 }
263 }
264 }
265 }
266 }