home *** CD-ROM | disk | FTP | other *** search
/ Igromania 2003 August / Mania_08_CD1.iso / Demos / ThinkTanks / ThinkTanks_Demo_v1.02.exe / main.cs < prev    next >
Text File  |  2003-05-02  |  9KB  |  307 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.          $argUsed[$i]++;
  196.    }
  197. }
  198.  
  199.  
  200. //-----------------------------------------------------------------------------
  201. // The displayHelp, onStart, onExit and parseArgs function are overriden
  202. // by mod packages to get hooked into initialization and cleanup.
  203.  
  204. function onStart()
  205. {
  206.    // Default startup function
  207. }
  208.  
  209. function onExit()
  210. {
  211.    // OnExit is called directly from C++ code, whereas onStart is
  212.    // invoked at the end of this file.
  213.    disconnect();
  214. }
  215.  
  216. function parseArgs()
  217. {
  218.    // Here for mod override, the arguments have already
  219.    // been parsed.
  220. }
  221.  
  222. package Help {
  223.    function onExit() {
  224.       // Override onExit when displaying help
  225.    }
  226. };
  227.  
  228. function displayHelp() {
  229.    activatePackage(Help);
  230.  
  231.       // Notes on logmode: console logging is written to console.log.
  232.       // -log 0 disables console logging. (default)
  233.       // -log 1 appends to existing logfile; it also closes the file
  234.       // (flushing the write buffer) after every write.
  235.       // -log 2 overwrites any existing logfile; it also only closes
  236.       // the logfile when the application shuts down.
  237.  
  238.    error(
  239.       "ThinkTanks command line options:\n"@
  240.       "  -log <logmode>         Logging behavior; see main.cs comments for details\n"@
  241.       "  -console               Open a separate console\n"@
  242.       "  -jSave  <file_name>    Record a journal\n"@
  243.       "  -jPlay  <file_name>    Play back a journal\n"@
  244.       "  -jDebug <file_name>    Play back a journal and issue an int3 at the end\n"@
  245.       "  -test                  Test launch the app\n"@
  246.       "  -help                  Display this help message\n"
  247.    );
  248. }
  249.  
  250.  
  251. //--------------------------------------------------------------------------
  252.  
  253. // Default to a new logfile each session.
  254. if (!$logModeSpecified) {
  255.    setLogMode(0);
  256. }
  257.  
  258. // Set the mod path which dictates which directories will be visible
  259. // to the scripts and the resource engine.
  260. $modPath = pushback($userMods, $baseMods, ";");
  261. setModPaths($modPath);
  262.  
  263. // Get the first mod on the list, which will be the last to be applied... this
  264. // does not modify the list.
  265. nextToken($modPath, currentMod, ";");
  266.  
  267. // Execute startup scripts for each mod, starting at base and working up
  268. echo("--------- Loading MODS ---------");
  269. function loadMods(%modPath)
  270. {
  271.    %modPath = nextToken(%modPath, token, ";");
  272.    if (%modPath !$= "")
  273.       loadMods(%modPath);
  274.  
  275.    exec(%token @ "/main.cs");
  276. }
  277. loadMods($modPath);
  278. echo("");
  279.  
  280. // Parse the command line arguments
  281. echo("--------- Parsing Arguments ---------");
  282. parseArgs();
  283.  
  284. // Either display the help message or startup the app.
  285. if ($displayHelp)
  286. {
  287.    enableWinConsole(true);
  288.    displayHelp();
  289. }
  290. else if ($Game::argc==2 && $Game::argv[1] $= "-compileAll")
  291. {
  292.    doCompileAll();
  293.    quit();
  294. }
  295. else
  296. {
  297.    onStart();
  298.    echo("Engine initialized...");
  299. }
  300.  
  301. // Display an error message for unused arguments
  302. for ($i = 1; $i < $Game::argc; $i++)  {
  303.    if (!$argUsed[$i])
  304.       error("Error: Unkown command line argument: " @ $Game::argv[$i]);
  305. }
  306.  
  307.