home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / KOOB / control / server / server.cs < prev   
Encoding:
Text File  |  2006-09-25  |  10.4 KB  |  357 lines

  1. //============================================================================
  2. // control/server/server.cs
  3. //  Copyright (c) 2003, 2006 by Kenneth C.  Finney.
  4. //============================================================================
  5. function OnServerCreated()
  6. //----------------------------------------------------------------------------
  7. // Once the engine has fired up the server, this function is called
  8. //----------------------------------------------------------------------------
  9. {
  10.  Exec("./misc/sndprofiles.cs"); 
  11.  
  12.    Exec("./misc/camera.cs");
  13.    Exec("./misc/shapeBase.cs");
  14.    Exec("./misc/item.cs");
  15.    Exec("./misc/marker.cs");
  16.    Exec("./misc/weather.cs");
  17.  
  18.    Exec("./players/player.cs");
  19.    Exec("./players/beast.cs");
  20.    Exec("./players/ai.cs");
  21.    Exec("./weapons/weapon.cs");
  22.    Exec("./weapons/crossbow.cs");
  23. Exec("./weapons/tommygun.cs");///***KCF***24/09/2006 7:15PM
  24. Exec("./vehicles/car.cs");///***KCF***24/09/2006 8:29PM
  25.  Exec("./misc/particles.cs");///***KCF***24/09/2006 9:51PM
  26.  exec("./misc/tracktriggers.cs");///***KCF***24/09/2006 11:53PM
  27. }
  28. function StartGame()
  29. {
  30.   if ($Game::Duration) // Start the game timer
  31.     $Game::Schedule = Schedule($Game::Duration * 1000, 0, "onGameDurationEnd");
  32.   $Game::Running = true;
  33.  // schedule( 2000, 0, "CreateBots");
  34.  PlaceCoins();
  35. }
  36. function PlaceCoins()
  37. {
  38. %W=GetWord(MissionArea.area,2);
  39. %H=GetWord(MissionArea.area,3);
  40. %west = GetWord(MissionArea.area,0);
  41. %south = GetWord(MissionArea.area,1);
  42.   new SimSet (CoinGroup);
  43.   for (%i = 0; %i < 4; %i++)
  44.   {
  45.     %x = GetRandom(%W) + %west;
  46.     %y = GetRandom(%H) + %south;
  47.     %searchMasks = $TypeMasks::PlayerObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::TerrainObjectType | $TypeMasks::ShapeBaseObjectType;
  48.     %scanTarg = ContainerRayCast(%x SPC %y SPC "500", %x SPC %y SPC "-100", %searchMasks);
  49.     if(%scanTarg && !(%scanTarg.getType() & $TypeMasks::InteriorObjectType))
  50.     {
  51.       %newpos = GetWord(%scanTarg,1)  SPC GetWord(%scanTarg,2) SPC GetWord(%scanTarg,3) + 1;
  52.     }
  53.     %coin = new Item("Gold "@%i) {
  54.       position = %newpos;
  55.       rotation = "1 0 0 0";
  56.       scale = "5 5 5";
  57.       dataBlock = "Gold";
  58.       collideable = "0";
  59.       static = "0";
  60.       rotate = "1";
  61.     };
  62.     MissionCleanup.add(%coin);
  63.     CoinGroup.add(%coin);
  64.   }
  65.   // repeat above for silver coin
  66.   for (%i = 0; %i < 8; %i++)
  67.   {
  68.     %x = GetRandom(%W) + %west;
  69.     %y = GetRandom(%H) + %south;
  70.     %searchMasks = $TypeMasks::PlayerObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::TerrainObjectType | $TypeMasks::ShapeBaseObjectType;
  71.     %scanTarg = ContainerRayCast(%x SPC %y SPC "500", %x SPC %y SPC "-100", %searchMasks);
  72.     if(%scanTarg && !(%scanTarg.getType() & $TypeMasks::InteriorObjectType))
  73.     {
  74.       %newpos = GetWord(%scanTarg,1)  SPC GetWord(%scanTarg,2) SPC GetWord(%scanTarg,3) + 1;
  75.     }
  76.     %coin = new Item("Silver "@%i) {
  77.       position = %newpos;
  78.       rotation = "1 0 0 0";
  79.       scale = "5 5 5";
  80.       dataBlock = "Silver";
  81.       collideable = "0";
  82.       static = "0";
  83.       rotate = "1";
  84.     };
  85.     MissionCleanup.add(%coin);
  86.     CoinGroup.add(%coin);
  87.   }
  88.   // repeat above for copper coin
  89.   for (%i = 0; %i < 32; %i++)
  90.   {
  91.     %x = GetRandom(%W) + %west;
  92.     %y = GetRandom(%H) + %south;
  93.     %searchMasks = $TypeMasks::PlayerObjectType | $TypeMasks::InteriorObjectType | $TypeMasks::TerrainObjectType | $TypeMasks::ShapeBaseObjectType;
  94.     %scanTarg = ContainerRayCast(%x SPC %y SPC "500", %x SPC %y SPC "-100", %searchMasks);
  95.     if(%scanTarg && !(%scanTarg.getType() & $TypeMasks::InteriorObjectType))
  96.     {
  97.       %newpos = GetWord(%scanTarg,1)  SPC GetWord(%scanTarg,2) SPC GetWord(%scanTarg,3) + 1;
  98.     }
  99.     %coin = new Item("Copper "@%i) {
  100.       position = %newpos;
  101.       rotation = "1 0 0 0";
  102.       scale = "5 5 5";
  103.       dataBlock = "Copper";
  104.       collideable = "0";
  105.       static = "0";
  106.       rotate = "1";
  107.     };
  108.     MissionCleanup.add(%coin);
  109.     CoinGroup.add(%coin);
  110.   }
  111. }
  112.  
  113. function OnMissionLoaded()
  114. {
  115.    StartGame();
  116. }
  117. function OnMissionEnded()
  118. {
  119.    Cancel($Game::Schedule);
  120.    $Game::Running = false;
  121. }
  122. function GameConnection::OnClientEnterGame(%this)
  123. {
  124.    // Create a new camera object.
  125.    %this.camera = new Camera() {
  126.       dataBlock = Observer;
  127.    };
  128.    MissionCleanup.Add( %this.camera );
  129.    %this.camera.ScopeToClient(%this);
  130.    %this.SpawnPlayer();
  131. }
  132. function GameConnection::SpawnPlayer(%this)
  133. {
  134.  
  135. //   %this.CreatePlayer("0 0 201 1 0 0 0");
  136.    %this.createPlayer(SelectSpawn());///***KCF***24/09/2006 10:42PM
  137. }
  138. function SelectSpawn()
  139. {
  140.    %groupName = "MissionGroup/PlayerSpawns";
  141.    %group = nameToID(%groupName);
  142.  
  143.    if (%group != -1) {
  144.       %count = %group.getCount();
  145.       if (%count != 0) {
  146.          %index = getRandom(%count-1);
  147.          %spawn = %group.getObject(%index);
  148.          return %spawn.getTransform();
  149.       }
  150.       else
  151.          error("No spawn points found in " @ %groupName);
  152.    }
  153.    else
  154.       error("Missing spawn points group " @ %groupName);
  155.  
  156.    return "0 0 201 1 0 0 0"; // if no spawn points then center of world
  157. }
  158.  
  159.  
  160. function GameConnection::CreatePlayer(%this, %spawnPoint)
  161. {
  162.    if (%this.player > 0)//The player should NOT already have an avatar object.
  163.    {                     // If he does, that's a Bad Thing.
  164.       Error( "Attempting to create an angus ghost!" );
  165.    }
  166.    // Create the player object
  167.    %player = new Player() {
  168.       dataBlock = MaleAvatar;   // defined in players/player.cs
  169.       client = %this;           // the avatar will have a pointer to its
  170.    };                           // owner's GameConnection object
  171.    %player.SetTransform(%spawnPoint); // where to put it
  172.    // Update the camera to start with the player
  173.    %this.camera.SetTransform(%player.GetEyeTransform());
  174.    %player.SetEnergyLevel(100);
  175.    // Give the client control of the player
  176.    %this.player = %player;
  177.    %this.setControlObject(%player);
  178.    
  179.    %this.lapsCompleted = 0;
  180.    %this.cpCompleted = 0;
  181.    %this.ResetCPs();
  182.  
  183.    %this.position = 0;
  184.    %this.money = 0;
  185.    %this.deaths = 0;
  186.    %this.kills = 0;
  187.    %this.score = 0;
  188.  
  189.    
  190. }
  191. function GameConnection::OnDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  192. {
  193.    // Switch the client over to the death cam and unhook the player object.
  194.    if (IsObject(%this.camera) && IsObject(%this.player))
  195.    {
  196.       %this.camera.SetMode("Death",%this.player);
  197.       %this.setControlObject(%this.camera);
  198.    }
  199.    %this.player = 0;
  200.    if (%damageType $= "Suicide" || %sourceClient == %this)
  201.    {
  202.    }
  203.    else
  204.    {
  205.      // good hit
  206.    }
  207.   %this.deaths++;
  208.   %this.DoScore();   
  209.   %sourceClient = %sourceObject ? %sourceObject.client : 0;
  210.    if (%obj.getState() $= "Dead")
  211.    {
  212.       if (isObject(%sourceClient))
  213.       {
  214.          %sourceClient.incScore(1);
  215.          if (isObject(%client))
  216.             %client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
  217.       }
  218.    }
  219.  
  220.  
  221. }
  222. //============================================================================
  223. // Server commands
  224. //============================================================================
  225. function ServerCmdToggleCamera(%client)
  226. {
  227.    %co = %client.getControlObject();
  228.    if (%co == %client.player)
  229.    {
  230.       %co = %client.camera;
  231.       %co.mode = toggleCameraFly;
  232.    }
  233.    else
  234.    {
  235.       %co = %client.player;
  236.       %co.mode = observerFly;
  237.    }
  238.    %client.SetControlObject(%co);
  239. }
  240. function ServerCmdDropPlayerAtCamera(%client)
  241. {
  242.    if ($Server::DevMode || IsObject(EditorGui))
  243.    {
  244.       %client.player.SetTransform(%client.camera.GetTransform());
  245.       %client.player.SetVelocity("0 0 0");
  246.       %client.SetControlObject(%client.player);
  247.    }
  248. }
  249. function ServerCmdDropCameraAtPlayer(%client)
  250. {
  251.    %client.camera.SetTransform(%client.player.GetEyeTransform());
  252.    %client.camera.SetVelocity("0 0 0");
  253.    %client.SetControlObject(%client.camera);
  254. }
  255. function ServerCmdUse(%client,%data)
  256. {
  257.    %client.GetControlObject().use(%data);
  258. }
  259. // stubs
  260. function ClearCenterPrintAll()
  261. {
  262. }
  263. function ClearBottomPrintAll()
  264. {
  265. }
  266. function onNeedRelight()
  267. {
  268. }
  269.  
  270.  
  271. function GameConnection::ResetCPs(%client)
  272. {
  273.     for (%i = 0; %i < $Game::NumberOfCheckpoints; %i++)
  274.       %client.cpCompleted[%i]=false;
  275. }
  276. function GameConnection::CheckProgress(%client, %cpnumber)
  277. {
  278.     for (%i = 0; %i < %cpnumber; %i++)
  279.     {
  280.       if (%client.cpCompleted[%i]==false)
  281.         return false;
  282.     }
  283.     %client.cpCompleted = %cpnumber;
  284.     return true;
  285. }
  286. function GameConnection::UpdateLap(%client,%trigger,%obj)
  287. {
  288.   if (%trigger.cp==0)
  289.   {
  290.     if (%client.CheckProgress($Game::NumberOfCheckpoints))
  291.     {
  292.         %client.ResetCPs();
  293.         %client.cpCompleted[0] = true;
  294.         %client.lapsCompleted++;
  295.         %client.DoScore();
  296.         if(%client.lapsCompleted >= $Game::NumberOfLaps)
  297.           EndGame();
  298.     }
  299.     else
  300.     {
  301.       %client.cpCompleted[0] = true;
  302.       %client.DoScore();
  303.     }
  304.   }
  305.   else if (%client.CheckProgress(%trigger.cp))
  306.   {
  307.     %client.cpCompleted[%trigger.cp] = true;
  308.     %client.DoScore();
  309.   }
  310. }
  311. function GameConnection::DoScore(%client)
  312. {   
  313.     %client.score = (%client.lapsCompleted * $Game::Laps_Multiplier) +
  314.                   (%client.money * $Game::Money_Multiplier) +
  315.                   (%client.deaths * $Game::Deaths_Multiplier) +
  316.                   (%client.kills * $Game::Kills_Multiplier) ;
  317.  
  318.   %scoreString  =           %client.score         @
  319.                   " Lap:" @ %client.lapsCompleted @
  320.                   " CP:"  @ %client.cpCompleted+1 @
  321.                   " $:"   @ %client.money         @
  322.                   " D:"   @ %client.deaths        @
  323.                   " K:"   @ %client.kills;
  324.   commandToClient(%client, 'UpdateScore', %scoreString);
  325.     if (%client.score >= $Game::MaxPoints)
  326.     cycleGame();
  327.  
  328. }
  329. function cycleGame()
  330. {
  331.    if (!$Game::Cycling) {
  332.       $Game::Cycling = true;
  333.       $Game::Schedule = schedule(0, 0, "onCycleExec");
  334.    }
  335. }
  336. function onCycleExec()
  337. {
  338.    endGame();
  339.    $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd");
  340. }
  341.  
  342. function onCyclePauseEnd()
  343. {
  344.    $Game::Cycling = false;
  345.    %search = $Server::MissionFileSpec;
  346.    for (%file = findFirstFile(%search); %file !$= "";
  347.          %file = findNextFile(%search)) {
  348.       if (%file $= $Server::MissionFile) {
  349.          %file = findNextFile(%search);
  350.          if (%file $= "")
  351.            %file = findFirstFile(%search);
  352.          break;
  353.       }
  354.    }
  355.    loadMission(%file);
  356. }
  357.