Updates for a new interface to the networking
[crack-attack.git] / src / Attack.cxx
blobf7a6aa534d7a2f4d627931b1f935ff927b2edc97
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 <cstring>
28 #include <cctype>
29 #include <sys/stat.h>
31 #ifndef _WIN32
32 # include <pwd.h>
33 #else
34 # ifndef __MINGW32__
35 # include <direct.h>
36 # include <glext.h>
37 # endif
38 #endif
40 using namespace std;
42 #include "TextureLoader.h"
43 #include "Attack.h"
44 #include "Communicator.h"
45 #include "MetaState.h"
46 #include "Random.h"
48 #ifdef AUDIO_ENABLED
49 #include "Sound.h"
50 #include "Music.h"
51 #endif
53 #ifdef WANT_GTK
54 #include "gtk-gui/gui_main.h"
55 #endif
57 #define GC_HOST_NAME_SIZE (256)
60 * Documentation
61 * html tables don't work right in explorer
62 * man-page bug
64 * Issues and Watches
65 * slow if left sitting a long time before game start
66 * gtk+ frontend
67 * central server for online game setup
68 * remove dying_count_2
69 * find and use correct GL_LIGHT_MODEL_COLOR_CONTROL defines
72 int nosound = 0;
74 int main ( int argc, char **argv )
76 setupLocalDataDirectory();
77 #ifdef WANT_GTK
78 if (argc <= 1) return gui_main(argc, argv);
79 #endif
80 char player_name[GC_PLAYER_NAME_LENGTH];
81 char host_name[GC_HOST_NAME_SIZE];
82 int port;
83 int mode = 0;
84 int height = -1, width = -1;
86 player_name[0] = '\0';
87 parseCommandLine(argc, argv, mode, port, host_name, player_name, height, width);
88 run_crack_attack(mode, port, host_name, player_name, height, width);
90 return 0;
93 inline void usage ( )
95 cerr << "Usage: " GC_BINARY " --server [PORT] [--low] [--extreme] [--wait] "
96 "[--name 'NAME']\n"
97 " <or>\n"
98 " " GC_BINARY " SERVER PORT [[--really] --low] [--name 'NAME']\n"
99 " <or>\n"
100 " " GC_BINARY " --solo [[--really] --low] [-X] [--name 'NAME']\n"
101 " <or in short>\n"
102 " " GC_BINARY " -1 [[-r] -l] [-X] [-n 'NAME']\n"
103 " <or with a computer opponent>\n"
104 " " GC_BINARY " --solo [--hard] [--easy] [--medium] [[--really] --low]"
105 " [--name 'NAME']\n"
106 " <etc...>"
107 << endl;
108 exit(1);
111 void run_crack_attack (
112 int mode,
113 int port,
114 char *host_name,
115 char *player_name,
116 int width,
117 int height) {
118 if (!player_name) {
119 cerr << "Player name not properly allocated" << endl;
120 return;
123 if (player_name[0] == '\0') {
124 #ifndef _WIN32
125 struct passwd *uinfo = getpwuid(getuid());
126 if (uinfo) {
127 strncpy(player_name, uinfo->pw_name, GC_PLAYER_NAME_LENGTH);
128 for (int n = strlen(player_name); n--; )
129 player_name[n] = toupper(player_name[n]);
130 } else
131 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
132 #else
133 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
134 #endif
137 cout << GC_MESSAGE << endl;
139 if (!(mode & CM_SOLO))
140 Communicator::initialize(mode, port, host_name, player_name);
141 else
142 Random::seed(Random::generateSeed());
144 MetaState::programStart(mode, player_name, width, height);
146 #ifdef AUDIO_ENABLED
147 if (!nosound) {
148 Sound::initialize();
149 Music::initialize();
150 Music::play_prelude();
152 #endif
154 glutMainLoop();
156 #ifdef AUDIO_ENABLED
157 Music::stop();
158 Music::cleanup();
159 Sound::cleanup();
160 #endif
163 void parseCommandLine ( int argc, char **argv, int &mode, int &port,
164 char *host_name, char *player_name , int &height, int &width )
166 for (int n = 1; argv[n]; n++) {
168 if (!strcmp(argv[n], "--nosound"))
169 nosound = 1;
170 else
171 if (!strcmp(argv[n], "-s") || !strcmp(argv[n], "--server")) {
172 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
174 mode |= CM_SERVER;
175 if (argv[n + 1] && argv[n + 1][0] != '-')
176 port = atoi(argv[++n]);
177 else
178 port = 0;
180 } else if (!strcmp(argv[n], "-1") || !strcmp(argv[n], "--solo")) {
181 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
183 mode |= CM_SOLO;
185 } else if (!strcmp(argv[n], "-n") || !strcmp(argv[n], "--name")) {
186 if (!argv[n + 1]) usage();
188 strncpy(player_name, argv[++n], GC_PLAYER_NAME_LENGTH);
189 player_name[GC_PLAYER_NAME_LENGTH - 1] = '\0';
190 for (char *p = player_name; *p; p++)
191 if (!isprint(*p)) *p = ' ';
193 } else if (!strcmp(argv[n], "-l") || !strcmp(argv[n], "--low"))
195 mode |= CM_LOW_GRAPHICS;
197 else if (!strcmp(argv[n], "-r") || !strcmp(argv[n], "--really")) {
199 mode |= CM_LOW_GRAPHICS;
200 mode |= CM_REALLY_LOW_GRAPHICS;
202 } else if (!strcmp(argv[n], "-X") || !strcmp(argv[n], "--extreme"))
204 mode |= CM_X;
206 else if (!strcmp(argv[n], "-w") || !strcmp(argv[n], "--wait"))
207 mode |= CM_NO_TIME_OUT;
209 else if (!strcmp(argv[n], "--hard")) {
210 mode |= CM_AI;
211 mode |= CM_AI_HARD;
213 } else if (!strcmp(argv[n], "--easy")) {
214 mode |= CM_AI;
215 mode |= CM_AI_EASY;
217 } else if (!strcmp(argv[n], "--medium")) {
218 mode |= CM_AI;
219 mode |= CM_AI_MEDIUM;
221 } else if (!strcmp(argv[n], "--res")) {
222 if (argv[n + 1] && argv[n + 1][0] != '-')
223 height = width = atoi(argv[++n]);
224 else
225 height = width = -1;
227 } else {
228 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
230 mode |= CM_CLIENT;
231 strncpy(host_name, argv[n], GC_HOST_NAME_SIZE);
232 ++n;
233 if (n < argc) {
234 port = atoi(argv[n]);
235 } else {
236 port = 0;
237 cerr << "No port specified.\n";
238 usage();
243 if (!(mode & (CM_SERVER | CM_CLIENT | CM_SOLO)))
244 usage();
246 if ((mode & CM_NO_TIME_OUT) && !(mode & CM_SERVER))
247 usage();
249 if ((mode & CM_REALLY_LOW_GRAPHICS) && !(mode & CM_LOW_GRAPHICS))
250 usage();
252 if ((mode & CM_AI && (mode & CM_X)))
253 usage();
256 void setupLocalDataDirectory ( )
258 char local_directory[256];
259 TextureLoader::buildLocalDataDirectoryName(local_directory);
260 if (!TextureLoader::fileExists(local_directory)
261 #ifndef _WIN32
262 && mkdir(local_directory, 0777)
263 #else
264 #ifndef __MINGW32__
265 && _mkdir(local_directory)
266 #endif
267 #endif
269 cerr << "Error creating local data directory '" << local_directory
270 << "'." << endl;
271 exit(1);