- Fixed gui bug where unchecking extremely low undid low
[crack-attack.git] / src / Attack.cxx
blobfe44f1132a80025c618a97af664536e6dde76d06
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 # include <direct.h>
35 # include <glext.h>
36 #endif
38 using namespace std;
40 #include "TextureLoader.h"
41 #include "Game.h"
42 #include "Attack.h"
43 #include "Communicator.h"
44 #include "MetaState.h"
45 #include "Random.h"
47 #ifdef WANT_GTK
48 #include "gtk-gui/gui_main.h"
49 #endif
51 #define GC_HOST_NAME_SIZE (256)
54 * Documentation
55 * html tables don't work right in explorer
56 * man-page bug
58 * Issues and Watches
59 * slow if left sitting a long time before game start
60 * gtk+ frontend
61 * central server for online game setup
62 * remove dying_count_2
63 * find and use correct GL_LIGHT_MODEL_COLOR_CONTROL defines
66 int main ( int argc, char **argv )
68 setupLocalDataDirectory();
69 #ifdef WANT_GTK
70 if (argc <= 1) return gui_main(argc, argv);
71 #endif
72 char player_name[GC_PLAYER_NAME_LENGTH];
73 char host_name[GC_HOST_NAME_SIZE];
74 int port;
75 int mode = 0;
77 player_name[0] = '\0';
78 parseCommandLine(argc, argv, mode, port, host_name, player_name);
79 run_crack_attack(mode, port, host_name, player_name, -1, -1);
81 return 0;
84 inline void usage ( )
86 cerr << "Usage: " GC_BINARY " --server [PORT] [--low] [--extreme] [--wait] "
87 "[--name 'NAME']\n"
88 " <or>\n"
89 " " GC_BINARY " SERVER[:PORT] [[--really] --low] [--name 'NAME']\n"
90 " <or>\n"
91 " " GC_BINARY " --solo [[--really] --low] [-X] [--name 'NAME']\n"
92 " <or in short>\n"
93 " " GC_BINARY " -1 [[-r] -l] [-X] [-n 'NAME']\n"
94 " <or with a computer opponent>\n"
95 " " GC_BINARY " --solo [--hard] [--easy] [--medium] [[--really] --low]"
96 " [--name 'NAME']\n"
97 " <etc...>"
98 << endl;
99 exit(1);
102 void run_crack_attack (
103 int mode,
104 int port,
105 char *host_name,
106 char *player_name,
107 int width,
108 int height) {
109 if (!player_name) {
110 cerr << "Player name not properly allocated" << endl;
111 return;
114 if (player_name[0] == '\0') {
115 #ifndef _WIN32
116 struct passwd *uinfo = getpwuid(getuid());
117 if (uinfo) {
118 strncpy(player_name, uinfo->pw_name, GC_PLAYER_NAME_LENGTH);
119 for (int n = strlen(player_name); n--; )
120 player_name[n] = toupper(player_name[n]);
121 } else
122 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
123 #else
124 strncpy(player_name, GC_DEFAULT_PLAYER_NAME, GC_PLAYER_NAME_LENGTH);
125 #endif
128 cout << GC_MESSAGE << endl;
130 if (!(mode & CM_SOLO))
131 Communicator::initialize(mode, port, host_name, player_name);
132 else
133 Random::seed(Random::generateSeed());
135 MetaState::programStart(mode, player_name, width, height);
137 glutMainLoop();
140 void parseCommandLine ( int argc, char **argv, int &mode, int &port,
141 char *host_name, char *player_name )
143 for (int n = 1; argv[n]; n++) {
145 if (!strcmp(argv[n], "-s") || !strcmp(argv[n], "--server")) {
146 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
148 mode |= CM_SERVER;
149 if (argv[n + 1] && argv[n + 1][0] != '-')
150 port = atoi(argv[++n]);
151 else
152 port = 0;
154 } else if (!strcmp(argv[n], "-1") || !strcmp(argv[n], "--solo")) {
155 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
157 mode |= CM_SOLO;
159 } else if (!strcmp(argv[n], "-n") || !strcmp(argv[n], "--name")) {
160 if (!argv[n + 1]) usage();
162 strncpy(player_name, argv[++n], GC_PLAYER_NAME_LENGTH);
163 player_name[GC_PLAYER_NAME_LENGTH - 1] = '\0';
164 for (char *p = player_name; *p; p++)
165 if (!isprint(*p)) *p = ' ';
167 } else if (!strcmp(argv[n], "-l") || !strcmp(argv[n], "--low"))
169 mode |= CM_LOW_GRAPHICS;
171 else if (!strcmp(argv[n], "-r") || !strcmp(argv[n], "--really")) {
173 mode |= CM_LOW_GRAPHICS;
174 mode |= CM_REALLY_LOW_GRAPHICS;
176 } else if (!strcmp(argv[n], "-X") || !strcmp(argv[n], "--extreme"))
178 mode |= CM_X;
180 else if (!strcmp(argv[n], "-w") || !strcmp(argv[n], "--wait"))
181 mode |= CM_NO_TIME_OUT;
183 else if (!strcmp(argv[n], "--hard")) {
184 mode |= CM_AI;
185 mode |= CM_AI_HARD;
187 } else if (!strcmp(argv[n], "--easy")) {
188 mode |= CM_AI;
189 mode |= CM_AI_EASY;
191 } else if (!strcmp(argv[n], "--medium")) {
192 mode |= CM_AI;
193 mode |= CM_AI_MEDIUM;
195 } else {
196 if (mode & (CM_SERVER | CM_CLIENT | CM_SOLO)) usage();
198 mode |= CM_CLIENT;
199 strncpy(host_name, argv[n], GC_HOST_NAME_SIZE);
200 char *ptr = strchr(host_name, ':');
201 if (ptr) {
202 port = atoi(ptr + 1);
203 *ptr = '\0';
204 } else
205 port = 0;
209 if (!(mode & (CM_SERVER | CM_CLIENT | CM_SOLO)))
210 usage();
212 if ((mode & CM_NO_TIME_OUT) && !(mode & CM_SERVER))
213 usage();
215 if ((mode & CM_REALLY_LOW_GRAPHICS) && !(mode & CM_LOW_GRAPHICS))
216 usage();
219 void setupLocalDataDirectory ( )
221 char local_directory[256];
222 TextureLoader::buildLocalDataDirectoryName(local_directory);
223 if (!TextureLoader::fileExists(local_directory)
224 #ifndef _WIN32
225 && mkdir(local_directory, 0777)
226 #else
227 && _mkdir(local_directory)
228 #endif
230 cerr << "Error creating local data directory '" << local_directory
231 << "'." << endl;
232 exit(1);