home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 February / GSSH0204.iso / Action / ThinkTanks / ThinkTanks-Demo_v1.1.exe / main.cs < prev    next >
Text File  |  2003-06-18  |  9KB  |  308 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine
  3. //
  4. // Copyright (c) 2001 GarageGames.Com
  5. // Portions Copyright (c) 2001 by Sierra Online, Inc.
  6. //-----------------------------------------------------------------------------
  7.  
  8. $baseMods   = "common";
  9. $userMods   = "game";
  10. $displayHelp = false;
  11.  
  12. //-----------------------------------------------------------------------------
  13. // Support functions used to manage the mod string
  14.  
  15. function pushFront(%list, %token, %delim)
  16. {
  17.    if (%list !$= "")
  18.       return %token @ %delim @ %list;
  19.    return %token;
  20. }
  21.  
  22. function pushBack(%list, %token, %delim)
  23. {
  24.    if (%list !$= "")
  25.       return %list @ %delim @ %token;
  26.    return %token;
  27. }
  28.  
  29. function popFront(%list, %delim)
  30. {
  31.    return nextToken(%list, unused, %delim);
  32. }
  33.  
  34. function doCompileAll()
  35. {
  36.    for(%file = findFirstFile("*.cs"); %file !$= ""; %file = findNextFile("*.cs"))
  37.    {
  38.       if (strstr(%file,"/CVS/") != -1)
  39.          continue;
  40.       compile(%file);
  41.    }
  42.    for(%file = findFirstFile("*.gui"); %file !$= ""; %file = findNextFile("*.gui"))
  43.    {
  44.       if (strstr(%file,"/CVS/") != -1)
  45.          continue;
  46.       compile(%file);
  47.    }
  48.    %misfile = new FileObject();
  49.    if (%misfile.openForWrite("game/data/missions/missions.txt"))
  50.    {
  51.       exec("common/server/missionInfo.cs");
  52.       %misfile.writeLine("[name]");
  53.       for (%file = findFirstFile("*.mis"); %file !$= ""; %file = findNextFile("*.mis"))
  54.       {
  55.          if (strstr(%file,"/TT") == -1 && strstr(%file,"Brain") == -1)
  56.             continue;
  57.          if (strstr(%file,"/CVS/") != -1)
  58.             continue;
  59.          buildLoadInfo(%file);
  60.          while (strstr(%file,"/") != -1)
  61.             %file = getSubStr(%file,strstr(%file,"/")+1,strlen(%file)-strstr(%file,"/")-1);
  62.          %file = getSubStr(%file,0,strlen(%file)-4);
  63.          %misfile.writeLine(%file @ "=" @ MissionInfo.name);
  64.       }
  65.       %misfile.close();
  66.       clearLoadInfo();
  67.    }
  68.  
  69.    %savePref = $pref::Terrain::useSmall;
  70.    $pref::Terrain::useSmall = false;
  71.    for(%file = findFirstFile("*.ter"); %file !$= ""; %file = findNextFile("*.ter"))
  72.    {
  73.       if (strstr(%file,"/TT") == -1)
  74.          continue;
  75.       if (strstr(%file,"/CVS/") != -1)
  76.          continue;
  77.       %terr = new TerrainBlock() {
  78.          terrainFile = %file;
  79.          squareSize = "8";
  80.          tile = "0";
  81.          blockShift = "7";
  82.       };
  83.       if (%terr.blockShift == 7)
  84.       {
  85.          // possible that terrain was already 6, even though
  86.          // block shift was set to 7 in constructor above...
  87.          // ...that's why this is conditional.
  88.          echo("Reducing terrain :" @ %file);
  89.          %terr.blockShift = "6";
  90.          %terr.resave("","_LO");
  91.       }
  92.       %terr.delete();
  93.    }
  94.    $pref::Terrain::useSmall = %savePref;
  95. }
  96.  
  97. //------------------------------------------------------------------------------
  98. // Process command line arguments
  99.  
  100. for ($i = 1; $i < $Game::argc ; $i++)
  101. {
  102.    $arg = $Game::argv[$i];
  103.    $nextArg = $Game::argv[$i+1];
  104.    $hasNextArg = $Game::argc - $i > 1;
  105.    $logModeSpecified = false;
  106.  
  107.    switch$ ($arg)
  108.    {
  109.       //--------------------
  110.       case "-log":
  111.          $argUsed[$i]++;
  112.          if ($hasNextArg)
  113.          {
  114.             // Turn on console logging
  115.             if ($nextArg != 0)
  116.             {
  117.                // Dump existing console to logfile first.
  118.                $nextArg += 4;
  119.             }
  120.             setLogMode($nextArg);
  121.             $logModeSpecified = true;
  122.             $argUsed[$i+1]++;
  123.             $i++;
  124.          }
  125.          else
  126.             error("Error: Missing Command Line argument. Usage: -log <Mode: 0,1,2>");
  127.  
  128.       //--------------------
  129.       case "-demo":
  130.          $argUsed[$i]++;
  131.          $Game::DemoMode = true;
  132.  
  133.       //--------------------
  134.       case "-show":
  135.          // A useful shortcut for -mod show
  136.          $userMods = strreplace($userMods, "show", "");
  137.          $userMods = pushFront($userMods, "show", ";");
  138.          $argUsed[$i]++;
  139.  
  140.       //--------------------
  141.       case "-console":
  142.          enableWinConsole(true);
  143.          $argUsed[$i]++;
  144.  
  145.       //--------------------
  146.       case "-compileAll":
  147.          $argUsed[$i]++;
  148.          enableWinConsole(true);
  149.          // call doCompileAll() below...
  150.  
  151.       //--------------------
  152.       case "-jSave":
  153.          $argUsed[$i]++;
  154.          if ($hasNextArg)
  155.          {
  156.             echo("Saving event log to journal: " @ $nextArg);
  157.             saveJournal($nextArg);
  158.             $argUsed[$i+1]++;
  159.             $i++;
  160.          }
  161.          else
  162.             error("Error: Missing Command Line argument. Usage: -jSave <journal_name>");
  163.  
  164.       //--------------------
  165.       case "-jPlay":
  166.          $argUsed[$i]++;
  167.          if ($hasNextArg)
  168.          {
  169.             playJournal($nextArg,false);
  170.             $argUsed[$i+1]++;
  171.             $i++;
  172.          }
  173.          else
  174.             error("Error: Missing Command Line argument. Usage: -jPlay <journal_name>");
  175.  
  176.       //--------------------
  177.       case "-jDebug":
  178.          $argUsed[$i]++;
  179.          if ($hasNextArg)
  180.          {
  181.             playJournal($nextArg,true);
  182.             $argUsed[$i+1]++;
  183.             $i++;
  184.          }
  185.          else
  186.             error("Error: Missing Command Line argument. Usage: -jDebug <journal_name>");
  187.  
  188.       //-------------------
  189.       case "-help":
  190.          $displayHelp = true;
  191.          $argUsed[$i]++;
  192.  
  193.       //-------------------
  194.       case "-test":
  195.          $Game::testMode=true;
  196.          $argUsed[$i]++;
  197.    }
  198. }
  199.  
  200.  
  201. //-----------------------------------------------------------------------------
  202. // The displayHelp, onStart, onExit and parseArgs function are overriden
  203. // by mod packages to get hooked into initialization and cleanup.
  204.  
  205. function onStart()
  206. {
  207.    // Default startup function
  208. }
  209.  
  210. function onExit()
  211. {
  212.    // OnExit is called directly from C++ code, whereas onStart is
  213.    // invoked at the end of this file.
  214.    disconnect();
  215. }
  216.  
  217. function parseArgs()
  218. {
  219.    // Here for mod override, the arguments have already
  220.    // been parsed.
  221. }
  222.  
  223. package Help {
  224.    function onExit() {
  225.       // Override onExit when displaying help
  226.    }
  227. };
  228.  
  229. function displayHelp() {
  230.    activatePackage(Help);
  231.  
  232.       // Notes on logmode: console logging is written to console.log.
  233.       // -log 0 disables console logging. (default)
  234.       // -log 1 appends to existing logfile; it also closes the file
  235.       // (flushing the write buffer) after every write.
  236.       // -log 2 overwrites any existing logfile; it also only closes
  237.       // the logfile when the application shuts down.
  238.  
  239.    error(
  240.       "ThinkTanks command line options:\n"@
  241.       "  -log <logmode>         Logging behavior; see main.cs comments for details\n"@
  242.       "  -console               Open a separate console\n"@
  243.       "  -jSave  <file_name>    Record a journal\n"@
  244.       "  -jPlay  <file_name>    Play back a journal\n"@
  245.       "  -jDebug <file_name>    Play back a journal and issue an int3 at the end\n"@
  246.       "  -test                  Test launch the app\n"@
  247.       "  -help                  Display this help message\n"
  248.    );
  249. }
  250.  
  251.  
  252. //--------------------------------------------------------------------------
  253.  
  254. // Default to a new logfile each session.
  255. if (!$logModeSpecified) {
  256.    setLogMode(0);
  257. }
  258.  
  259. // Set the mod path which dictates which directories will be visible
  260. // to the scripts and the resource engine.
  261. $modPath = pushback($userMods, $baseMods, ";");
  262. setModPaths($modPath);
  263.  
  264. // Get the first mod on the list, which will be the last to be applied... this
  265. // does not modify the list.
  266. nextToken($modPath, currentMod, ";");
  267.  
  268. // Execute startup scripts for each mod, starting at base and working up
  269. echo("--------- Loading MODS ---------");
  270. function loadMods(%modPath)
  271. {
  272.    %modPath = nextToken(%modPath, token, ";");
  273.    if (%modPath !$= "")
  274.       loadMods(%modPath);
  275.  
  276.    exec(%token @ "/main.cs");
  277. }
  278. loadMods($modPath);
  279. echo("");
  280.  
  281. // Parse the command line arguments
  282. echo("--------- Parsing Arguments ---------");
  283. parseArgs();
  284.  
  285. // Either display the help message or startup the app.
  286. if ($displayHelp)
  287. {
  288.    enableWinConsole(true);
  289.    displayHelp();
  290. }
  291. else if ($Game::argc==2 && $Game::argv[1] $= "-compileAll")
  292. {
  293.    doCompileAll();
  294.    quit();
  295. }
  296. else
  297. {
  298.    onStart();
  299.    echo("Engine initialized...");
  300. }
  301.  
  302. // Display an error message for unused arguments
  303. for ($i = 1; $i < $Game::argc; $i++)  {
  304.    if (!$argUsed[$i])
  305.       error("Error: Unkown command line argument: " @ $Game::argv[$i]);
  306. }
  307.  
  308.