home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / server / scripts / game.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  11.7 KB  |  405 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Game duration in secs, no limit if the duration is set to 0
  7. $Game::Duration = 20 * 60;
  8.  
  9. // When a client score reaches this value, the game is ended.
  10. $Game::EndGameScore = 30;
  11.  
  12. // Pause while looking over the end game screen (in secs)
  13. $Game::EndGamePause = 10;
  14.  
  15. $BlockSave = 0;
  16. //-----------------------------------------------------------------------------
  17. //  Functions that implement game-play
  18. //-----------------------------------------------------------------------------
  19.  
  20. //-----------------------------------------------------------------------------
  21.  
  22. function onServerCreated()
  23. {
  24.    // Server::GameType is sent to the master server.
  25.    // This variable should uniquely identify your game and/or mod.
  26.    $Server::GameType = "TGE Demo";
  27.  
  28.    // Server::MissionType sent to the master server.  Clients can
  29.    // filter servers based on mission type.
  30.    $Server::MissionType = "Demo Match";
  31.  
  32.    // GameStartTime is the sim time the game started. Used to calculated
  33.    // game elapsed time.
  34.    $Game::StartTime = 0;
  35.  
  36.    // Load up basic datablocks, objects etc.
  37.    exec("./audioProfiles.cs");
  38.    exec("./camera.cs");
  39.    exec("./markers.cs"); 
  40.    exec("./triggers.cs"); 
  41.    exec("./inventory.cs");
  42.    exec("./shapeBase.cs");
  43.    exec("./staticShape.cs");
  44.    exec("./radiusDamage.cs");
  45.    exec("./chimneyfire.cs");
  46.    exec("./item.cs");
  47.    exec("./weapon.cs");
  48.    exec("./flag.cs");
  49.  
  50.    // Room demo...
  51.    exec("./scene.cs");
  52.    exec("./fxlights.cs");
  53.    exec("./environment.cs");
  54.    
  55.    // Fps game...
  56.    exec("./fps.cs");
  57.    exec("./player.cs");
  58.    exec("./aiPlayer.cs");
  59.    exec("./crossbow.cs");
  60.  
  61.    // Racing game...
  62.    exec("./racing.cs");
  63.    exec("./car.cs");
  64.  
  65.    // Keep track of when the game started
  66.    $Game::StartTime = $Sim::Time;
  67. }
  68.  
  69. function onServerDestroyed()
  70. {
  71.    // This function is called as part of a server shutdown.
  72. }
  73.  
  74.  
  75. //-----------------------------------------------------------------------------
  76.  
  77. function onMissionLoaded()
  78. {
  79.    // Called by loadMission() once the mission is finished loading.
  80.    // Nothing special for now, just start up the game play.
  81.    
  82.    // Determin what type of game play we have and activate the
  83.    // associated package.
  84.    echo("Mission Type: " @ MissionInfo.type);
  85.    activatePackage(MissionInfo.type @ "Game");
  86.    
  87.    // Override the default game type
  88.    $Server::GameType = MissionInfo.type;
  89.    $Server::MissionType = "Play";
  90.    $Game::Duration = 0;
  91.  
  92.    if (MissionInfo.type $= "DemoScene") $BlockSave = 1;
  93.    else $BlockSave = 0;
  94.    // Start game play, this doesn't wait players...
  95.    startGame();
  96. }
  97.  
  98. function onMissionEnded()
  99. {
  100.    // Called by endMission(), right before the mission is destroyed
  101.  
  102.    // Normally the game should be ended first before the next
  103.    // mission is loaded, this is here in case loadMission has been
  104.    // called directly.  The mission will be ended if the server
  105.    // is destroyed, so we only need to cleanup here.
  106.    deactivatePackage($Server::GameType @ "Game");
  107.    cancel($Game::Schedule);
  108.    $Game::Running = false;
  109.    $Game::Cycling = false;
  110. }
  111.  
  112.  
  113. //-----------------------------------------------------------------------------
  114.  
  115. function startGame()
  116. {
  117.    // Start up the game.  Normally only called once ater a mission is loaded,
  118.    // but theoretically a game can be stopped and restarted without reloading
  119.    // the mission.
  120.    if ($Game::Running) {
  121.       error("startGame: End the game first!");
  122.       return;
  123.    }
  124.  
  125.    // Inform the client we're starting up
  126.    for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
  127.       %cl = ClientGroup.getObject( %clientIndex );
  128.       commandToClient(%cl, 'GameStart');
  129.  
  130.       // Other client specific setup..
  131.       %cl.score = 0;
  132.    }
  133.  
  134.    // Start the game timer
  135.    if ($Game::Duration)
  136.       $Game::Schedule = schedule($Game::Duration * 1000, 0, "onGameDurationEnd" );
  137.    
  138.    $Game::Running = true;
  139. }
  140.  
  141. function endGame()
  142. {
  143.    // Game specific cleanup...
  144.    if (!$Game::Running)  {
  145.       error("endGame: No game running!");
  146.       return;
  147.    }
  148.  
  149.    // Stop any game timers
  150.    cancel($Game::Schedule);
  151.  
  152.    // Inform the client the game is over
  153.    for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
  154.       %cl = ClientGroup.getObject( %clientIndex );
  155.       commandToClient(%cl, 'GameEnd');
  156.    }
  157.  
  158.    // Delete all the temporary mission objects
  159.    resetMission();
  160.    $Game::Running = false;
  161. }
  162.  
  163. function onGameDurationEnd()
  164. {
  165.    // This "redirect" is here so that we can abort the game cycle if
  166.    // the $Game::Duration variable has been cleared, without having
  167.    // to have a function to cancel the schedule.
  168.    if ($Game::Duration && !isObject(EditorGui))
  169.       cycleGame();
  170. }
  171.  
  172.  
  173. //-----------------------------------------------------------------------------
  174.  
  175. function cycleGame()
  176. {
  177.    // This is setup as a schedule so that this function can be called
  178.    // directly from object callbacks.  Object callbacks have to be
  179.    // carefull about invoking server functions that could cause
  180.    // their object to be deleted.
  181.    if (!$Game::Cycling) {
  182.       $Game::Cycling = true;
  183.       $Game::Schedule = schedule(0, 0, "onCycleExec");
  184.    }
  185. }
  186.  
  187. function onCycleExec()
  188. {
  189.    // End the current game and start another one, we'll pause for a little
  190.    // so the end game victory screen can be examined by the clients.
  191.    endGame();
  192.    $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd");
  193. }
  194.  
  195. function onCyclePauseEnd()
  196. {
  197.    $Game::Cycling = false;
  198.  
  199.    // Just cycle through the missions for now.
  200.    %search = $Server::MissionFileSpec;
  201.    for (%file = findFirstFile(%search); %file !$= ""; %file = findNextFile(%search)) {
  202.       if (%file $= $Server::MissionFile) {
  203.          // Get the next one, back to the first if there
  204.          // is no next.
  205.          %file = findNextFile(%search);
  206.          if (%file $= "")
  207.            %file = findFirstFile(%search);
  208.          break;
  209.       }
  210.    }
  211.    loadMission(%file);
  212. }
  213.  
  214.  
  215. //-----------------------------------------------------------------------------
  216. //-----------------------------------------------------------------------------
  217.  
  218. function Game::onClientEnterGame(%this,%client)
  219. {
  220.    // Player has joined the game, normally called when a
  221.    // new client has connected to the server.
  222. }
  223.  
  224. function Game::onLeaveEnterGame(%this,%client)
  225. {
  226.    // Player has left the game, normally called when a client
  227.    // disconnects from the server.
  228. }
  229.  
  230. function Game::onDeath(%this,%player,%sourceObject, %sourceClient, %damageType, %damLoc)
  231. {
  232.    // Invoked when a client has died.
  233. }
  234.  
  235. function Game::onLeaveMissionArea(%this,%player)
  236. {
  237.    // The control objects invoked this method when they
  238.    // move out of the mission area.
  239. }
  240.  
  241. function Game::onEnterMissionArea(%this,%player)
  242. {
  243.    // The control objects invoked this method when they
  244.    // move back into the mission area.
  245. }
  246.  
  247. function Game::spawnPlayer(%this,%client)
  248. {
  249.    // Create a new player for the client and drop him into the world.
  250. }
  251.  
  252. function Game::createPlayer(%this, %client, %location)
  253. {
  254.    // Create a new player for the client and start him at the
  255.    // given location.
  256. }
  257.  
  258.  
  259. //-----------------------------------------------------------------------------
  260. // GameConnection Methods
  261. // These methods are extensions to the GameConnection class. Extending
  262. // GameConnection make is easier to deal with some of this functionality,
  263. // but these could also be implemented as stand-alone functions.
  264. //-----------------------------------------------------------------------------
  265.  
  266. //-----------------------------------------------------------------------------
  267.  
  268. function GameConnection::onClientEnterGame(%this)
  269. {
  270.    commandToClient(%this, 'SyncClock', $Sim::Time - $Game::StartTime);
  271.    commandToClient(%this, 'SetGameGUI',"PlayGUI");
  272.  
  273.    // Create a new camera object.
  274.    %this.camera = new Camera() {
  275.       dataBlock = Observer;
  276.    };
  277.    MissionCleanup.add( %this.camera );
  278.    %this.camera.scopeToClient(%this);
  279.    
  280.    // Spawn the player
  281.    %this.score = 0;
  282.    %this.spawnPlayer();
  283. }
  284.  
  285. function GameConnection::onClientLeaveGame(%this)
  286. {
  287.    if (isObject(%this.camera))
  288.       %this.camera.delete();
  289.    if (isObject(%this.player))
  290.       %this.player.delete();
  291. }
  292.  
  293.  
  294. //-----------------------------------------------------------------------------
  295.  
  296. function GameConnection::onLeaveMissionArea(%this)
  297. {
  298.    // The control objects invoked this method when they
  299.    // move out of the mission area.
  300. }
  301.  
  302. function GameConnection::onEnterMissionArea(%this)
  303. {
  304.    // The control objects invoked this method when they
  305.    // move back into the mission area.
  306. }
  307.  
  308.  
  309. //-----------------------------------------------------------------------------
  310.  
  311. function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  312. {
  313.    // Clear out the name on the corpse
  314.    %this.player.setShapeName("");
  315.  
  316.    // Switch the client over to the death cam and unhook the player object.
  317.    if (isObject(%this.camera) && isObject(%this.player)) {
  318.       %this.camera.setMode("Corpse",%this.player);
  319.       %this.setControlObject(%this.camera);
  320.    }
  321.    %this.player = 0;
  322.  
  323.    // Doll out points and display an appropriate message
  324.    if (%damageType $= "Suicide" || %sourceClient == %this) {
  325.       %this.incScore(-1);
  326.       messageAll('MsgClientKilled','%1 takes his own life!',%this.name);
  327.    }
  328.    else {
  329.       %sourceClient.incScore(1);
  330.       messageAll('MsgClientKilled','%1 gets nailed by %2!',%this.name,%sourceClient.name);
  331.       if (%sourceClient.score >= $Game::EndGameScore)
  332.          cycleGame();
  333.    }
  334. }
  335.  
  336.  
  337. //-----------------------------------------------------------------------------
  338.  
  339. function GameConnection::spawnPlayer(%this)
  340. {
  341.    // Combination create player and drop him somewhere
  342.    %spawnPoint = pickSpawnPoint();
  343.    %this.createPlayer(%spawnPoint);
  344. }   
  345.  
  346.  
  347. //-----------------------------------------------------------------------------
  348.  
  349. function GameConnection::createPlayer(%this, %spawnPoint)
  350. {
  351.    if (%this.player > 0)  {
  352.       // The client should not have a player currently
  353.       // assigned.  Assigning a new one could result in 
  354.       // a player ghost.
  355.       error( "Attempting to create an angus ghost!" );
  356.    }
  357.  
  358.    // Create the player object
  359.    %player = new Player() {
  360.       dataBlock = PlayerBody;
  361.       client = %this;
  362.    };
  363.    MissionCleanup.add(%player);
  364.  
  365.    // Player setup...
  366.    %player.setTransform(%spawnPoint);
  367.    %player.setEnergyLevel(60);
  368.    %player.setShapeName(%this.name);
  369.    
  370.    // Update the camera to start with the player
  371.    %this.camera.setTransform(%player.getEyeTransform());
  372.  
  373.    // Give the client control of the player
  374.    %this.player = %player;
  375.    %this.setControlObject(%player);
  376. }
  377.  
  378.  
  379. //-----------------------------------------------------------------------------
  380. // Support functions
  381. //-----------------------------------------------------------------------------
  382.  
  383. function pickSpawnPoint() 
  384. {
  385.    %groupName = "MissionGroup/PlayerDropPoints";
  386.    %group = nameToID(%groupName);
  387.  
  388.    if (%group != -1) {
  389.       %count = %group.getCount();
  390.       if (%count != 0) {
  391.          %index = getRandom(%count-1);
  392.          %spawn = %group.getObject(%index);
  393.          return %spawn.getTransform();
  394.       }
  395.       else
  396.          error("No spawn points found in " @ %groupName);
  397.    }
  398.    else
  399.       error("Missing spawn points group " @ %groupName);
  400.  
  401.    // Could be no spawn points, in which case we'll stick the
  402.    // player at the center of the world.
  403.    return "0 0 300 1 0 0 0";
  404. }
  405.