| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name stratagus.cpp - The main file. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2010 by Lutz Sammer, Francois Beerten, and |
| 13 |
// Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/** |
| 33 |
** @mainpage |
| 34 |
** |
| 35 |
** @section Introduction Introduction |
| 36 |
** |
| 37 |
** Welcome to the source code documentation of the Stratagus engine. |
| 38 |
** Extract the source documentation with doxygen (http://www.doxygen.org) |
| 39 |
** or doc++ (http://www.zib.de/Visual/software/doc++/index.html) tools. |
| 40 |
** |
| 41 |
** Any help to improve this documention is welcome. If you didn't |
| 42 |
** understand something or you found an error or a wrong spelling |
| 43 |
** or wrong grammer please write an email (including a patch :). |
| 44 |
** |
| 45 |
** @section Informations Informations |
| 46 |
** |
| 47 |
** Visit the http://www.boswars.org web page for the latest news and |
| 48 |
** ../doc/readme.html for other documentations. |
| 49 |
** |
| 50 |
** @section Modules Modules |
| 51 |
** |
| 52 |
** This are the main modules of the engine. |
| 53 |
** |
| 54 |
** @subsection Map Map |
| 55 |
** |
| 56 |
** Handles the map. A map is made from tiles. |
| 57 |
** |
| 58 |
** @see map.h @see map.cpp |
| 59 |
** |
| 60 |
** @subsection Unit Unit |
| 61 |
** |
| 62 |
** Handles units. Units are ships, flyers, buildings, creatures, |
| 63 |
** machines. |
| 64 |
** |
| 65 |
** @see unit.h @see unit.cpp @see unittype.h @see unittype.cpp |
| 66 |
** |
| 67 |
** @subsection Missile Missile |
| 68 |
** |
| 69 |
** Handles missiles. Missiles are all other sprites on map |
| 70 |
** which are no unit. |
| 71 |
** |
| 72 |
** @see missile.h @see missile.cpp |
| 73 |
** |
| 74 |
** @subsection Player Player |
| 75 |
** |
| 76 |
** Handles players, all units are owned by a player. A player |
| 77 |
** could be controlled by a human or a computer. |
| 78 |
** |
| 79 |
** @see player.h @see player.cpp @see ::CPlayer |
| 80 |
** |
| 81 |
** @subsection Sound Sound |
| 82 |
** |
| 83 |
** Handles the high and low level of the sound. There are the |
| 84 |
** background music support, voices and sound effects. |
| 85 |
** Following low level backends are supported: OSS and SDL. |
| 86 |
** |
| 87 |
** @see sound.h @see sound.cpp |
| 88 |
** @see script_sound.cpp @see sound_id.cpp @see sound_server.cpp |
| 89 |
** @see unitsound.cpp |
| 90 |
** @see sdl_audio.cpp |
| 91 |
** @see ogg.cpp @see wav.cpp |
| 92 |
** |
| 93 |
** @subsection Video Video |
| 94 |
** |
| 95 |
** Handles the high and low level of the graphics. |
| 96 |
** This also contains the sprite and linedrawing routines. |
| 97 |
** |
| 98 |
** See page @ref VideoModule for more information upon supported |
| 99 |
** features and video platforms. |
| 100 |
** |
| 101 |
** @see video.h @see video.cpp |
| 102 |
** |
| 103 |
** @subsection Network Network |
| 104 |
** |
| 105 |
** Handles the high and low level of the network protocol. |
| 106 |
** The network protocol is needed for multiplayer games. |
| 107 |
** |
| 108 |
** See page @ref NetworkModule for more information upon supported |
| 109 |
** features and API. |
| 110 |
** |
| 111 |
** @see network.h @see network.cpp |
| 112 |
** |
| 113 |
** @subsection Pathfinder Pathfinder |
| 114 |
** |
| 115 |
** @see pathfinder.h @see pathfinder.cpp |
| 116 |
** |
| 117 |
** @subsection AI AI |
| 118 |
** |
| 119 |
** There are currently two AI's. The old one is very hardcoded, |
| 120 |
** but does things like placing buildings better than the new. |
| 121 |
** The old AI shouldn't be used. The new is very flexible, but |
| 122 |
** very basic. It includes none optimations. |
| 123 |
** |
| 124 |
** See page @ref AiModule for more information upon supported |
| 125 |
** features and API. |
| 126 |
** |
| 127 |
** @see ai_local.h |
| 128 |
** @see ai.h @see ai.cpp |
| 129 |
** |
| 130 |
** @subsection CCL CCL |
| 131 |
** |
| 132 |
** CCL is Craft Configuration Language, which is used to |
| 133 |
** configure and customize the engine. |
| 134 |
** |
| 135 |
** @see script.h @see script.cpp |
| 136 |
** |
| 137 |
** @subsection Icon Icon |
| 138 |
** |
| 139 |
** @see icons.h @see icons.cpp |
| 140 |
** |
| 141 |
** @subsection Editor Editor |
| 142 |
** |
| 143 |
** This is the integrated editor, it shouldn't be a perfect |
| 144 |
** editor. It is used to test new features of the engine. |
| 145 |
** |
| 146 |
** @see editor.h @see editor.cpp |
| 147 |
*/ |
| 148 |
|
| 149 |
/*---------------------------------------------------------------------------- |
| 150 |
-- Includes |
| 151 |
----------------------------------------------------------------------------*/ |
| 152 |
|
| 153 |
#include <stdio.h> |
| 154 |
#include <stdlib.h> |
| 155 |
#include <stdarg.h> |
| 156 |
#include <string.h> |
| 157 |
#include <time.h> |
| 158 |
#include <ctype.h> |
| 159 |
#include <sstream> |
| 160 |
#include <algorithm> |
| 161 |
|
| 162 |
#ifndef _MSC_VER |
| 163 |
#include <unistd.h> |
| 164 |
#endif |
| 165 |
#ifdef __CYGWIN__ |
| 166 |
#include <getopt.h> |
| 167 |
#endif |
| 168 |
#if defined(_MSC_VER) || defined(__MINGW32__) |
| 169 |
extern char *optarg; |
| 170 |
extern int optind; |
| 171 |
extern int getopt(int argc, char *const *argv, const char *opt); |
| 172 |
#endif |
| 173 |
|
| 174 |
#ifdef MAC_BUNDLE |
| 175 |
#define Button ButtonOSX |
| 176 |
#include <Carbon/Carbon.h> |
| 177 |
#undef Button |
| 178 |
#endif |
| 179 |
|
| 180 |
#include "SDL.h" |
| 181 |
|
| 182 |
#include "stratagus.h" |
| 183 |
#include "unit_manager.h" |
| 184 |
#include "video.h" |
| 185 |
#include "font.h" |
| 186 |
#include "cursor.h" |
| 187 |
#include "ui.h" |
| 188 |
#include "interface.h" |
| 189 |
#include "menus.h" |
| 190 |
#include "sound_server.h" |
| 191 |
#include "sound.h" |
| 192 |
#include "settings.h" |
| 193 |
#include "script.h" |
| 194 |
#include "network.h" |
| 195 |
#include "netconnect.h" |
| 196 |
#include "ai.h" |
| 197 |
#include "missile.h" |
| 198 |
#include "replay.h" |
| 199 |
#include "results.h" |
| 200 |
#include "editor.h" |
| 201 |
#include "movie.h" |
| 202 |
#include "pathfinder.h" |
| 203 |
#include "widgets.h" |
| 204 |
#include "trigger.h" |
| 205 |
#include "iolib.h" |
| 206 |
#include "iocompat.h" |
| 207 |
#include "guichan.h" |
| 208 |
#include "version.h" |
| 209 |
#include "title.h" |
| 210 |
#include "map.h" |
| 211 |
|
| 212 |
|
| 213 |
#if defined(_MSC_VER) && SDL_VERSION_ATLEAST(1, 2, 13) |
| 214 |
#define REDIRECT_OUTPUT |
| 215 |
#endif |
| 216 |
|
| 217 |
extern void CreateUserDirectories(void); |
| 218 |
|
| 219 |
/*---------------------------------------------------------------------------- |
| 220 |
-- Variables |
| 221 |
----------------------------------------------------------------------------*/ |
| 222 |
|
| 223 |
std::string StratagusLibPath; /// Path for data directory |
| 224 |
std::string LocalPlayerName; /// Name of local player |
| 225 |
|
| 226 |
/// Name, Version, Copyright |
| 227 |
static std::string NameLine = |
| 228 |
"Bos Wars V" VERSION ", (c) 1998-2023 by the Bos Wars and Stratagus Project."; |
| 229 |
|
| 230 |
std::string CliMapName; /// Filename of the map given on the command line |
| 231 |
std::string CompileOptions; /// Compile options. |
| 232 |
|
| 233 |
static std::vector<gcn::Container *> Containers; |
| 234 |
|
| 235 |
/*---------------------------------------------------------------------------- |
| 236 |
-- Speedups FIXME: Move to some other more logic place |
| 237 |
----------------------------------------------------------------------------*/ |
| 238 |
|
| 239 |
int SpeedBuild = 1; /// speed factor for building |
| 240 |
int SpeedTrain = 1; /// speed factor for training |
| 241 |
|
| 242 |
/*============================================================================ |
| 243 |
== DISPLAY |
| 244 |
============================================================================*/ |
| 245 |
|
| 246 |
unsigned long GameCycle; /// Game simulation cycle counter |
| 247 |
unsigned long FastForwardCycle; /// Cycle to fastforward to in a replay |
| 248 |
|
| 249 |
/*============================================================================ |
| 250 |
== MAIN |
| 251 |
============================================================================*/ |
| 252 |
|
| 253 |
/** |
| 254 |
** Show load progress. |
| 255 |
** |
| 256 |
** @param fmt printf format string. |
| 257 |
*/ |
| 258 |
void ShowLoadProgress(const char *fmt, ...) |
| 259 |
{ |
| 260 |
va_list va; |
| 261 |
char temp[4096]; |
| 262 |
|
| 263 |
va_start(va, fmt); |
| 264 |
vsnprintf(temp, sizeof(temp) - 1, fmt, va); |
| 265 |
temp[sizeof(temp) - 1] = '\0'; |
| 266 |
va_end(va); |
| 267 |
|
| 268 |
if (Video.Depth && GameFont && GameFont->IsLoaded()) { |
| 269 |
// Remove non printable chars |
| 270 |
for (char *s = temp; *s; ++s) { |
| 271 |
if (*s < 32) { |
| 272 |
*s = ' '; |
| 273 |
} |
| 274 |
} |
| 275 |
Video.FillRectangle(ColorBlack, 5, Video.Height - 18, Video.Width - 10, 18); |
| 276 |
VideoDrawTextCentered(Video.Width / 2, Video.Height - 16, GameFont, temp); |
| 277 |
InvalidateArea(5, Video.Height - 18, Video.Width - 10, 18); |
| 278 |
RealizeVideoMemory(); |
| 279 |
} else { |
| 280 |
DebugPrint("!!!!%s\n" _C_ temp); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
//---------------------------------------------------------------------------- |
| 285 |
|
| 286 |
/** |
| 287 |
** Pre menu setup. |
| 288 |
*/ |
| 289 |
void PreMenuSetup(void) |
| 290 |
{ |
| 291 |
// |
| 292 |
// Initial menus require some gfx. |
| 293 |
// |
| 294 |
SetDefaultTextColors(FontYellow, FontWhite); |
| 295 |
|
| 296 |
LoadFonts(); |
| 297 |
|
| 298 |
InitVideoCursors(); |
| 299 |
|
| 300 |
LoadCursors(); |
| 301 |
InitSettings(); |
| 302 |
|
| 303 |
InitUserInterface(); |
| 304 |
UI.Load(); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
** Run the guichan main menus loop. |
| 309 |
** |
| 310 |
** @return 0 for success, else exit. |
| 311 |
*/ |
| 312 |
static int MenuLoop() |
| 313 |
{ |
| 314 |
char buf[1024]; |
| 315 |
int status; |
| 316 |
|
| 317 |
initGuichan(); |
| 318 |
InterfaceState = IfaceStateMenu; |
| 319 |
// Clear screen |
| 320 |
Video.ClearScreen(); |
| 321 |
Invalidate(); |
| 322 |
|
| 323 |
ButtonUnderCursor = -1; |
| 324 |
CursorState = CursorStatePoint; |
| 325 |
GameCursor = UI.Point.Cursor; |
| 326 |
|
| 327 |
// FIXME delete this when switching to full guichan GUI |
| 328 |
LibraryFileName("scripts/guichan.lua", buf, sizeof(buf)); |
| 329 |
status = LuaLoadFile(buf); |
| 330 |
|
| 331 |
// We clean up later in Exit |
| 332 |
|
| 333 |
return status; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
** Cleanup game. |
| 338 |
** |
| 339 |
** Call each module to clean up. |
| 340 |
** Contrary to CleanModules, maps can be restarted |
| 341 |
** without reloading all lua files. |
| 342 |
*/ |
| 343 |
void CleanGame(void) |
| 344 |
{ |
| 345 |
FastForwardCycle = 0; |
| 346 |
|
| 347 |
EndReplayLog(); |
| 348 |
CleanMessages(); |
| 349 |
|
| 350 |
CleanTriggers(); |
| 351 |
CleanUnits(); |
| 352 |
CleanAi(); |
| 353 |
CleanSelections(); |
| 354 |
CleanGroups(); |
| 355 |
CleanMissiles(); |
| 356 |
Map.Clean(); |
| 357 |
CleanReplayLog(); |
| 358 |
FreeVisionTable(); |
| 359 |
FreePathfinder(); |
| 360 |
CursorBuilding = NULL; |
| 361 |
UnitUnderCursor = NULL; |
| 362 |
} |
| 363 |
|
| 364 |
static void ExpandPath(std::string &newpath, const std::string &path) |
| 365 |
{ |
| 366 |
if (path[0] == '~') { |
| 367 |
newpath = UserDirectory + path.substr(1); |
| 368 |
} else { |
| 369 |
newpath = StratagusLibPath + "/" + path; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
extern gcn::Gui *Gui; |
| 374 |
|
| 375 |
void StartMap(const std::string &filename, bool clean) |
| 376 |
{ |
| 377 |
std::string nc, rc; |
| 378 |
|
| 379 |
gcn::Widget *oldTop = Gui->getTop(); |
| 380 |
gcn::Container *container = new gcn::Container(); |
| 381 |
Containers.push_back(container); |
| 382 |
container->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height)); |
| 383 |
container->setOpaque(false); |
| 384 |
Gui->setTop(container); |
| 385 |
|
| 386 |
NetConnectRunning = 0; |
| 387 |
InterfaceState = IfaceStateNormal; |
| 388 |
|
| 389 |
// Create the game. |
| 390 |
DebugPrint("Creating game with map: %s\n" _C_ filename.c_str()); |
| 391 |
if (clean) { |
| 392 |
CleanPlayers(); |
| 393 |
} |
| 394 |
GetDefaultTextColors(nc, rc); |
| 395 |
|
| 396 |
CreateGame(filename, &Map); |
| 397 |
|
| 398 |
// Play the game. |
| 399 |
GameMainLoop(); |
| 400 |
|
| 401 |
// Clear screen |
| 402 |
Video.ClearScreen(); |
| 403 |
Invalidate(); |
| 404 |
|
| 405 |
CleanGame(); |
| 406 |
InterfaceState = IfaceStateMenu; |
| 407 |
SetDefaultTextColors(nc, rc); |
| 408 |
|
| 409 |
Gui->setTop(oldTop); |
| 410 |
Containers.erase(std::find(Containers.begin(), Containers.end(), container)); |
| 411 |
delete container; |
| 412 |
} |
| 413 |
|
| 414 |
void StartSavedGame(const std::string &filename) |
| 415 |
{ |
| 416 |
std::string path; |
| 417 |
|
| 418 |
SaveGameLoading = true; |
| 419 |
CleanPlayers(); |
| 420 |
ExpandPath(path, filename); |
| 421 |
LoadGame(path); |
| 422 |
|
| 423 |
// the filename argument of StartMap is unused with saved games |
| 424 |
StartMap("", false); |
| 425 |
} |
| 426 |
|
| 427 |
void StartReplay(const std::string &filename, bool reveal) |
| 428 |
{ |
| 429 |
std::string replay; |
| 430 |
|
| 431 |
CleanPlayers(); |
| 432 |
ExpandPath(replay, filename); |
| 433 |
LoadReplay(replay); |
| 434 |
|
| 435 |
ReplayRevealMap = reveal; |
| 436 |
|
| 437 |
StartMap(CurrentMapPath, false); |
| 438 |
} |
| 439 |
|
| 440 |
//---------------------------------------------------------------------------- |
| 441 |
|
| 442 |
/** |
| 443 |
** Print headerline, copyright, ... |
| 444 |
*/ |
| 445 |
static void PrintHeader(void) |
| 446 |
{ |
| 447 |
fprintf(stdout, "%s\n written by Lutz Sammer, Fabrice Rossi, Vladi Shabanski, Patrice Fortier,\n" |
| 448 |
"Jon Gabrielson, Andreas Arens, Nehal Mistry, Jimmy Salmon, Francois Beerten and others.\n" |
| 449 |
"\t(http://www.boswars.org)" |
| 450 |
"\nCompile options %s", NameLine.c_str(), CompileOptions.c_str()); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
** Main1, called from main. |
| 455 |
** |
| 456 |
** @param argc Number of arguments. |
| 457 |
** @param argv Vector of arguments. |
| 458 |
*/ |
| 459 |
static int main1(int argc, char **argv) |
| 460 |
{ |
| 461 |
PrintHeader(); |
| 462 |
printf( |
| 463 |
"\n" |
| 464 |
"\n" |
| 465 |
"Bos Wars may be copied only under the terms of the GNU General Public License\n" |
| 466 |
"which must be distributed with this program.\n" |
| 467 |
"\n" |
| 468 |
"DISCLAIMER:\n" |
| 469 |
"This software is provided as-is. The author(s) can not be held liable for any\n" |
| 470 |
"damage that might arise from the use of this software.\n" |
| 471 |
"Use it at your own risk.\n" |
| 472 |
"\n"); |
| 473 |
|
| 474 |
// Video display is inited by lua |
| 475 |
|
| 476 |
// Setup sound card |
| 477 |
if (!InitSound()) { |
| 478 |
InitMusic(); |
| 479 |
} |
| 480 |
|
| 481 |
#ifndef DEBUG // For debug it's better not to have: |
| 482 |
srand(time(NULL)); // Random counter = random each start |
| 483 |
#endif |
| 484 |
|
| 485 |
// |
| 486 |
// Show title screens. |
| 487 |
// |
| 488 |
SetDefaultTextColors(FontYellow, FontWhite); |
| 489 |
LoadFonts(); |
| 490 |
SetClipping(0, 0, Video.Width - 1, Video.Height - 1); |
| 491 |
Video.ClearScreen(); |
| 492 |
ShowTitleScreens(); |
| 493 |
|
| 494 |
// Init player data |
| 495 |
ThisPlayer = NULL; |
| 496 |
//Don't clear the Players strucure as it would erase the allowed units. |
| 497 |
// memset(Players, 0, sizeof(Players)); |
| 498 |
NumPlayers = 0; |
| 499 |
|
| 500 |
UnitManager.Init(); // Units memory management |
| 501 |
PreMenuSetup(); // Load everything needed for menus |
| 502 |
|
| 503 |
MenuLoop(); |
| 504 |
|
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
** Exit the game. |
| 510 |
** |
| 511 |
** @param err Error code to pass to shell. |
| 512 |
*/ |
| 513 |
void Exit(int err) |
| 514 |
{ |
| 515 |
StopMusic(); |
| 516 |
QuitSound(); |
| 517 |
NetworkQuit(); |
| 518 |
|
| 519 |
ExitNetwork1(); |
| 520 |
|
| 521 |
CleanModules(); |
| 522 |
FreeBurningBuildingFrames(); |
| 523 |
// FreeGraphics must be called before exit to avoid the static |
| 524 |
// deinitialization fiasco. |
| 525 |
FreeGraphics(); |
| 526 |
|
| 527 |
#ifdef DEBUG |
| 528 |
FreeSounds(); |
| 529 |
FreePlayerColors(); |
| 530 |
FreeButtonStyles(); |
| 531 |
for (size_t i = 0; i < Containers.size(); ++i) { |
| 532 |
delete Containers[i]; |
| 533 |
} |
| 534 |
freeGuichan(); |
| 535 |
DebugPrint("Frames %lu, Slow frames %d = %ld%%\n" _C_ |
| 536 |
FrameCounter _C_ SlowFrameCounter _C_ |
| 537 |
(SlowFrameCounter * 100) / (FrameCounter ? FrameCounter : 1)); |
| 538 |
lua_settop(Lua, 0); |
| 539 |
lua_close(Lua); |
| 540 |
#endif |
| 541 |
|
| 542 |
fputs(_("Thanks for playing Bos Wars.\n"), stdout); |
| 543 |
exit(err); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
** Do a fatal exit. |
| 548 |
** Called on out of memory or crash. |
| 549 |
** |
| 550 |
** @param err Error code to pass to shell. |
| 551 |
*/ |
| 552 |
void ExitFatal(int err) |
| 553 |
{ |
| 554 |
exit(err); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
** Display the usage. |
| 559 |
*/ |
| 560 |
static void Usage(void) |
| 561 |
{ |
| 562 |
PrintHeader(); |
| 563 |
printf( |
| 564 |
"\n\n" |
| 565 |
"Usage: boswars [OPTIONS] [MAP-PRESENTATION-FILE]\n" |
| 566 |
" -c file.lua Configuration start file (default scripts/boswars.lua)\n" |
| 567 |
" -d datapath Path to Bos Wars data\n" |
| 568 |
" -E file.lua Load this script file when starting the map editor\n" |
| 569 |
" (default scripts/editor.lua)\n" |
| 570 |
#ifdef DEBUG |
| 571 |
" -e Show all events received from SDL (requires DEBUG)\n" |
| 572 |
#endif |
| 573 |
" -h Help shows this page\n" |
| 574 |
" -l Disable command log\n" |
| 575 |
" -P port Network port to use\n" |
| 576 |
" -L lag Network lag in # frames (default 10 = 333ms)\n" |
| 577 |
" -U update Network update rate in # frames (default 5=6x per s)\n" |
| 578 |
" -v mode Video mode WIDTHxHEIGHT, at least 640x480. Abbreviations:\n" |
| 579 |
" 1=640x480, 2=800x600, 3=1024x768, 4=1280x960, 5=1600x1200.\n" |
| 580 |
" -D bpp Video mode depth = bits per pixel (for Win32/TNT)\n" |
| 581 |
" -F Full screen video mode\n" |
| 582 |
" -S speed Video sync speed (100 = 30 frames/s)\n" |
| 583 |
" -f cycle Fast forward until the cycle number\n" |
| 584 |
" -W Windowed video mode\n" |
| 585 |
"Specifying MAP-PRESENTATION-FILE starts a single-player game in that map.\n" |
| 586 |
"Example: boswars maps/islandwar.map/presentation.smp\n"); |
| 587 |
} |
| 588 |
|
| 589 |
#ifdef REDIRECT_OUTPUT |
| 590 |
|
| 591 |
static std::string stdoutFile; |
| 592 |
static std::string stderrFile; |
| 593 |
|
| 594 |
static void CleanupOutput() |
| 595 |
{ |
| 596 |
fclose(stdout); |
| 597 |
fclose(stderr); |
| 598 |
|
| 599 |
struct stat st; |
| 600 |
if (stat(stdoutFile.c_str(), &st) == 0 && st.st_size == 0) { |
| 601 |
unlink(stdoutFile.c_str()); |
| 602 |
} |
| 603 |
if (stat(stderrFile.c_str(), &st) == 0 && st.st_size == 0) { |
| 604 |
unlink(stderrFile.c_str()); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
static void RedirectOutput() |
| 609 |
{ |
| 610 |
char path[MAX_PATH]; |
| 611 |
int pathlen; |
| 612 |
|
| 613 |
pathlen = GetModuleFileName(NULL, path, sizeof(path)); |
| 614 |
while (pathlen > 0 && path[pathlen] != '\\') { |
| 615 |
--pathlen; |
| 616 |
} |
| 617 |
path[pathlen] = '\0'; |
| 618 |
|
| 619 |
stdoutFile = std::string(path) + "\\stdout.txt"; |
| 620 |
stderrFile = std::string(path) + "\\stderr.txt"; |
| 621 |
|
| 622 |
if (!freopen(stdoutFile.c_str(), "w", stdout)) { |
| 623 |
printf("freopen stdout failed"); |
| 624 |
} |
| 625 |
if (!freopen(stderrFile.c_str(), "w", stderr)) { |
| 626 |
printf("freopen stderr failed"); |
| 627 |
} |
| 628 |
atexit(CleanupOutput); |
| 629 |
} |
| 630 |
#endif |
| 631 |
|
| 632 |
/** |
| 633 |
** Parses the "-v mode" command-line option. |
| 634 |
** |
| 635 |
** @param arg The "mode" argument of the "-v mode" option. |
| 636 |
*/ |
| 637 |
static void VideoModeOption(const char *arg) |
| 638 |
{ |
| 639 |
int abbrev = 0; |
| 640 |
int width = 0; |
| 641 |
int height = 0; |
| 642 |
char garbage; |
| 643 |
|
| 644 |
if (sscanf(arg, "%d %c", &abbrev, &garbage) == 1) { |
| 645 |
switch (abbrev) { |
| 646 |
case 0: |
| 647 |
Video.Width = 0; |
| 648 |
Video.Height = 0; |
| 649 |
return; |
| 650 |
case 1: |
| 651 |
Video.Width = 640; |
| 652 |
Video.Height = 480; |
| 653 |
return; |
| 654 |
case 2: |
| 655 |
Video.Width = 800; |
| 656 |
Video.Height = 600; |
| 657 |
return; |
| 658 |
case 3: |
| 659 |
Video.Width = 1024; |
| 660 |
Video.Height = 768; |
| 661 |
return; |
| 662 |
case 4: |
| 663 |
Video.Width = 1280; |
| 664 |
Video.Height = 960; |
| 665 |
return; |
| 666 |
case 5: |
| 667 |
Video.Width = 1600; |
| 668 |
Video.Height = 1200; |
| 669 |
return; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
if (sscanf(arg, "%d%*[Xx]%d %c", &width, &height, &garbage) == 2) { |
| 674 |
// Too small resolutions can cause segmentation faults |
| 675 |
// when the engine tries to draw outside the screen. |
| 676 |
if (width < 640 || height < 480) { |
| 677 |
fprintf(stderr, "Video mode must be at least 640x480.\n"); |
| 678 |
ExitFatal(-1); |
| 679 |
} |
| 680 |
|
| 681 |
Video.Width = width; |
| 682 |
Video.Height = height; |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
Usage(); |
| 687 |
ExitFatal(-1); |
| 688 |
} |
| 689 |
|
| 690 |
/** |
| 691 |
** The main program: initialise, parse options and arguments. |
| 692 |
** |
| 693 |
** @param argc Number of arguments. |
| 694 |
** @param argv Vector of arguments. |
| 695 |
*/ |
| 696 |
int main(int argc, char **argv) |
| 697 |
{ |
| 698 |
#ifdef REDIRECT_OUTPUT |
| 699 |
RedirectOutput(); |
| 700 |
#endif |
| 701 |
|
| 702 |
CompileOptions = |
| 703 |
#ifdef DEBUG |
| 704 |
"DEBUG " |
| 705 |
#endif |
| 706 |
#ifdef USE_VORBIS |
| 707 |
"VORBIS " |
| 708 |
#endif |
| 709 |
#ifdef USE_THEORA |
| 710 |
"THEORA " |
| 711 |
#endif |
| 712 |
"" |
| 713 |
; |
| 714 |
|
| 715 |
// |
| 716 |
// Setup some defaults. |
| 717 |
// |
| 718 |
#ifndef MAC_BUNDLE |
| 719 |
StratagusLibPath = STRATAGUS_LIB_PATH; |
| 720 |
#else |
| 721 |
freopen("/tmp/stdout.txt", "w", stdout); |
| 722 |
freopen("/tmp/stderr.txt", "w", stderr); |
| 723 |
// Look for the specified data set inside the application bundle |
| 724 |
// This should be a subdir of the Resources directory |
| 725 |
CFURLRef pluginRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), |
| 726 |
CFSTR(MAC_BUNDLE_DATADIR), NULL, NULL); |
| 727 |
CFStringRef macPath = CFURLCopyFileSystemPath(pluginRef, |
| 728 |
kCFURLPOSIXPathStyle); |
| 729 |
const char *pathPtr = CFStringGetCStringPtr(macPath, |
| 730 |
CFStringGetSystemEncoding()); |
| 731 |
Assert(pathPtr); |
| 732 |
StratagusLibPath = pathPtr; |
| 733 |
#endif |
| 734 |
CclStartFile = "scripts/boswars.lua"; |
| 735 |
EditorStartFile = "scripts/editor.lua"; |
| 736 |
|
| 737 |
// Default player name to username on unix systems. |
| 738 |
LocalPlayerName.clear(); |
| 739 |
#ifdef USE_WIN32 |
| 740 |
LocalPlayerName = "Anonymous"; |
| 741 |
#else |
| 742 |
if (getenv("USER")) { |
| 743 |
LocalPlayerName = getenv("USER"); |
| 744 |
} else { |
| 745 |
LocalPlayerName = "Anonymous"; |
| 746 |
} |
| 747 |
#endif |
| 748 |
|
| 749 |
// FIXME: Parse options before or after scripts? |
| 750 |
|
| 751 |
// |
| 752 |
// Parse commandline |
| 753 |
// |
| 754 |
for (;;) { |
| 755 |
switch (getopt(argc, argv, "c:d:ehlP:v:D:E:FL:S:f:U:W?")) { |
| 756 |
case 'c': |
| 757 |
CclStartFile = optarg; |
| 758 |
continue; |
| 759 |
case 'd': |
| 760 |
{ |
| 761 |
StratagusLibPath = optarg; |
| 762 |
replace(StratagusLibPath.begin(), StratagusLibPath.end(), '\\', '/'); |
| 763 |
continue; |
| 764 |
} |
| 765 |
case 'e': |
| 766 |
// DumpAllSdlEvents is intended for engine |
| 767 |
// hackers, rather than scripters or players. |
| 768 |
// Make it available in DEBUG builds only, so |
| 769 |
// that we are more free to make incompatible |
| 770 |
// changes to it, and so that players won't |
| 771 |
// complain that the feature is cumbersome to |
| 772 |
// use in Windows or Mac OS X. |
| 773 |
#ifdef DEBUG |
| 774 |
DumpAllSdlEvents = true; |
| 775 |
#else |
| 776 |
// The following string is not in _("...") |
| 777 |
// because the translation files have not |
| 778 |
// been loaded yet. |
| 779 |
fprintf(stderr, "Warning: Option -e is available in DEBUG builds only.\n"); |
| 780 |
#endif |
| 781 |
continue; |
| 782 |
case 'E': |
| 783 |
EditorStartFile = optarg; |
| 784 |
continue; |
| 785 |
case 'l': |
| 786 |
CommandLogDisabled = true; |
| 787 |
continue; |
| 788 |
case 'P': |
| 789 |
NetworkPort = atoi(optarg); |
| 790 |
continue; |
| 791 |
case 'v': |
| 792 |
VideoModeOption(optarg); |
| 793 |
continue; |
| 794 |
case 'L': |
| 795 |
NetworkLag = atoi(optarg); |
| 796 |
if (!NetworkLag) { |
| 797 |
fprintf(stderr, "FIXME: zero lag not supported\n"); |
| 798 |
Usage(); |
| 799 |
ExitFatal(-1); |
| 800 |
} |
| 801 |
continue; |
| 802 |
case 'U': |
| 803 |
NetworkUpdates = atoi(optarg); |
| 804 |
continue; |
| 805 |
|
| 806 |
case 'F': |
| 807 |
VideoForceFullScreen = 1; |
| 808 |
Video.FullScreen = 1; |
| 809 |
continue; |
| 810 |
case 'W': |
| 811 |
VideoForceFullScreen = 1; |
| 812 |
Video.FullScreen = 0; |
| 813 |
continue; |
| 814 |
case 'D': |
| 815 |
Video.Depth = atoi(optarg); |
| 816 |
continue; |
| 817 |
case 'S': |
| 818 |
VideoSyncSpeed = atoi(optarg); |
| 819 |
continue; |
| 820 |
case 'f': |
| 821 |
FastForwardCycle = atol(optarg); |
| 822 |
continue; |
| 823 |
case -1: |
| 824 |
break; |
| 825 |
case '?': |
| 826 |
case 'h': |
| 827 |
default: |
| 828 |
Usage(); |
| 829 |
ExitFatal(-1); |
| 830 |
} |
| 831 |
break; |
| 832 |
} |
| 833 |
|
| 834 |
if (argc - optind > 1) { |
| 835 |
fprintf(stderr, "too many files\n"); |
| 836 |
Usage(); |
| 837 |
ExitFatal(-1); |
| 838 |
} |
| 839 |
|
| 840 |
if (argc - optind) { |
| 841 |
CliMapName = argv[optind]; |
| 842 |
replace(CliMapName.begin(), CliMapName.end(), '\\', '/'); |
| 843 |
--argc; |
| 844 |
} |
| 845 |
|
| 846 |
// Init the random number generator. |
| 847 |
InitSyncRand(); |
| 848 |
|
| 849 |
CreateUserDirectories(); |
| 850 |
|
| 851 |
// Init CCL and load configurations! |
| 852 |
InitCcl(); |
| 853 |
|
| 854 |
// Initialise AI module |
| 855 |
InitAiModule(); |
| 856 |
|
| 857 |
LoadCcl(); |
| 858 |
|
| 859 |
main1(argc, argv); |
| 860 |
|
| 861 |
Exit(0); |
| 862 |
return 0; |
| 863 |
} |
| 864 |
|
| 865 |
//@} |