Fixes for datatype size on amd64.
[crack-attack.git] / src / Attack.cxx
bloba1f66921969e37ad6fd44d80ab6c664f0768bb61
1 /*
2 * Attack.cxx
3 * Daniel Nelson - 8/29/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
27 #include "Attack.h"
29 #include <cstring>
30 #include <cctype>
31 #include <sys/stat.h>
33 #ifndef _WIN32
34 # include <pwd.h>
35 #else
36 # ifndef __MINGW32__
37 # include <direct.h>
38 # include <glext.h>
39 # endif
40 #endif
42 #include "TextureLoader.h"
43 #include "Communicator.h"
44 #include "MetaState.h"
45 #include "Random.h"
47 #ifdef AUDIO_ENABLED
48 #include "Sound.h"
49 #include "Music.h"
50 #endif
52 #ifdef WANT_GTK
53 #include "gtk-gui/gui_main.h"
54 #endif
56 #define GC_HOST_NAME_SIZE (256)
59 * Documentation
60 * html tables don't work right in explorer
61 * man-page bug
63 * Issues and Watches
64 * slow if left sitting a long time before game start
65 * central server for online game setup
66 * remove dying_count_2
67 * find and use correct GL_LIGHT_MODEL_COLOR_CONTROL defines
70 int nosound = 0;
72 int main ( int argc, char **argv )
74 setupLocalDataDirectory();
75 #ifdef WANT_GTK
76 if (argc <= 1) return gui_main(argc, argv);
77 #endif
78 char player_name[GC_PLAYER_NAME_LENGTH];
79 char host_name[GC_HOST_NAME_SIZE];
80 int port;
81 int mode = 0;
82 int height = -1, width = -1;
84 player_name[0] = '\0';
85 glutInit(&argc, argv);
86 parseCommandLine(argc, argv, mode, port, host_name, player_name, height, width);
87 run_crack_attack(mode, port, host_name, player_name, height, width);
89 return 0;
92 inline void usage ( )
94 std::cerr << "Usage: " GC_BINARY " --server [PORT] [--low] [--extreme] "
95 "[--wait] [--name 'NAME']\n"
96 " <or>\n"
97 " " GC_BINARY " SERVER PORT [[--really] --low] [--name 'NAME']\n"
98 " <or>\n"
99 " " GC_BINARY " --solo [[--really] --low] [-X] [--name 'NAME']\n"
100 " <or in short>\n"
101 " " GC_BINARY " -1 [[-r] -l] [-X] [-n 'NAME']\n"
102 " <or with a computer opponent>\n"
103 " " GC_BINARY " --solo [--hard] [--easy] [--medium] [[--really] --low]"
104 " [--name 'NAME']\n"
105 " <etc...>"
106 << std::endl;
107 exit(1);
110 void run_crack_attack (
111 int mode,
112 int port,
113 char *host_name,
114 char *player_name,
115 int width,
116 int height) {
117 if (!player_name) {
118 std::cerr << "Player name not properly allocated" << std::endl;
119 return;
122 if (player_name[0] == '\0') {
123 #ifndef _WIN32
124 struct passwd *uinfo = getpwuid(getuid());
125 if (uinfo) {
126 strncpy(player_name, uinfo->pw_name, GC_PLAYER_NAME_LENGTH);
127 for (int n = strlen(player_name); n--; )
128 player_name[n] = toupper(player_name[n]);
129 } else
130 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
131 #else
132 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
133 #endif
136 std::cout << GC_MESSAGE << std::endl;
138 if (!(mode & CM_SOLO))
139 Communicator::initialize(mode, port, host_name, player_name);
140 else
141 Random::seed(Random::generateSeed());
143 MetaState::programStart(mode, player_name, width, height);
145 #ifdef AUDIO_ENABLED
146 if (!nosound) {
147 Sound::initialize();
148 Music::initialize();
149 Music::play_prelude();
151 #endif
153 glutMainLoop();
155 #ifdef AUDIO_ENABLED
156 Music::stop();
157 Music::cleanup();
158 Sound::cleanup();
159 #endif
162 void parseCommandLine ( int argc, char **argv, int &mode, int &port,
163 char *host_name, char *player_name , int &height, int &width )
165 for (int n = 1; argv[n]; n++) {
167 if (!strcmp(argv[n], "--nosound"))
168 nosound = 1;
169 else
170 if (!strcmp(argv[n], "-s") || !strcmp(argv[n], "--server")) {
171 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
173 mode |= CM_SERVER;
174 if (argv[n + 1] && argv[n + 1][0] != '-')
175 port = atoi(argv[++n]);
176 else
177 port = 0;
179 } else if (!strcmp(argv[n], "-1") || !strcmp(argv[n], "--solo")) {
180 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
182 mode |= CM_SOLO;
184 } else if (!strcmp(argv[n], "-n") || !strcmp(argv[n], "--name")) {
185 if (!argv[n + 1]) usage();
187 strncpy(player_name, argv[++n], GC_PLAYER_NAME_LENGTH);
188 player_name[GC_PLAYER_NAME_LENGTH - 1] = '\0';
189 for (char *p = player_name; *p; p++)
190 if (!isprint(*p)) *p = ' ';
192 } else if (!strcmp(argv[n], "-l") || !strcmp(argv[n], "--low"))
194 mode |= CM_LOW_GRAPHICS;
196 else if (!strcmp(argv[n], "-r") || !strcmp(argv[n], "--really")) {
198 mode |= CM_LOW_GRAPHICS;
199 mode |= CM_REALLY_LOW_GRAPHICS;
201 } else if (!strcmp(argv[n], "-X") || !strcmp(argv[n], "--extreme"))
203 mode |= CM_X;
205 else if (!strcmp(argv[n], "-w") || !strcmp(argv[n], "--wait"))
206 mode |= CM_NO_TIME_OUT;
208 else if (!strcmp(argv[n], "--hard")) {
209 mode |= CM_AI;
210 mode |= CM_AI_HARD;
212 } else if (!strcmp(argv[n], "--easy")) {
213 mode |= CM_AI;
214 mode |= CM_AI_EASY;
216 } else if (!strcmp(argv[n], "--medium")) {
217 mode |= CM_AI;
218 mode |= CM_AI_MEDIUM;
220 } else if (!strcmp(argv[n], "--res")) {
221 if (argv[n + 1] && argv[n + 1][0] != '-')
222 height = width = atoi(argv[++n]);
223 else
224 height = width = -1;
226 } else {
227 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
229 mode |= CM_CLIENT;
230 strncpy(host_name, argv[n], GC_HOST_NAME_SIZE);
231 ++n;
232 if (n < argc) {
233 port = atoi(argv[n]);
234 } else {
235 port = 0;
236 std::cerr << "No port specified.\n";
237 usage();
242 if (!(mode & (CM_SERVER | CM_CLIENT | CM_SOLO)))
243 usage();
245 if ((mode & CM_NO_TIME_OUT) && !(mode & CM_SERVER))
246 usage();
248 if ((mode & CM_REALLY_LOW_GRAPHICS) && !(mode & CM_LOW_GRAPHICS))
249 usage();
251 if ((mode & CM_AI && (mode & CM_X)))
252 usage();
255 #define MKDIR(x,y) (mkdir(x, y))
256 #ifdef _WIN32
257 # include <dir.h>
258 # undef MKDIR
259 # define MKDIR(x,y) (_mkdir(x))
260 #endif
262 void setupLocalDataDirectory ( )
264 char local_directory[256];
265 TextureLoader::buildLocalDataDirectoryName(local_directory);
266 if (!TextureLoader::fileExists(local_directory)
267 && MKDIR(local_directory, 0777)
269 std::cerr << "Error creating local data directory '" << local_directory
270 << "'." << std::endl;
271 exit(1);