game.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Game duration in secs, no limit if the duration is set to 0
  23. $Game::Duration = 20 * 60;
  24. // When a client score reaches this value, the game is ended.
  25. $Game::EndGameScore = 30;
  26. // Pause while looking over the end game screen (in secs)
  27. $Game::EndGamePause = 10;
  28. //-----------------------------------------------------------------------------
  29. function onServerCreated()
  30. {
  31. // Server::GameType is sent to the master server.
  32. // This variable should uniquely identify your game and/or mod.
  33. $Server::GameType = $appName;
  34. // Server::MissionType sent to the master server. Clients can
  35. // filter servers based on mission type.
  36. $Server::MissionType = "Deathmatch";
  37. // GameStartTime is the sim time the game started. Used to calculated
  38. // game elapsed time.
  39. $Game::StartTime = 0;
  40. // Create the server physics world.
  41. physicsInitWorld( "server" );
  42. // Load up any objects or datablocks saved to the editor managed scripts
  43. %datablockFiles = new ArrayObject();
  44. %datablockFiles.add( "art/ribbons/ribbonExec.cs" );
  45. %datablockFiles.add( "art/particles/managedParticleData.cs" );
  46. %datablockFiles.add( "art/particles/managedParticleEmitterData.cs" );
  47. %datablockFiles.add( "art/decals/managedDecalData.cs" );
  48. %datablockFiles.add( "art/datablocks/managedDatablocks.cs" );
  49. %datablockFiles.add( "art/forest/managedItemData.cs" );
  50. %datablockFiles.add( "art/datablocks/datablockExec.cs" );
  51. loadDatablockFiles( %datablockFiles, true );
  52. // Run the other gameplay scripts in this folder
  53. exec("./scriptExec.cs");
  54. // Keep track of when the game started
  55. $Game::StartTime = $Sim::Time;
  56. }
  57. function onServerDestroyed()
  58. {
  59. // This function is called as part of a server shutdown.
  60. physicsDestroyWorld( "server" );
  61. // Clean up the GameCore package here as it persists over the
  62. // life of the server.
  63. if (isPackage(GameCore))
  64. {
  65. deactivatePackage(GameCore);
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. function onGameDurationEnd()
  70. {
  71. // This "redirect" is here so that we can abort the game cycle if
  72. // the $Game::Duration variable has been cleared, without having
  73. // to have a function to cancel the schedule.
  74. if ($Game::Duration && !(EditorIsActive() && GuiEditorIsActive()))
  75. Game.onGameDurationEnd();
  76. }
  77. //-----------------------------------------------------------------------------
  78. function cycleGame()
  79. {
  80. // This is setup as a schedule so that this function can be called
  81. // directly from object callbacks. Object callbacks have to be
  82. // carefull about invoking server functions that could cause
  83. // their object to be deleted.
  84. if (!$Game::Cycling)
  85. {
  86. $Game::Cycling = true;
  87. $Game::Schedule = schedule(0, 0, "onCycleExec");
  88. }
  89. }
  90. function onCycleExec()
  91. {
  92. // End the current game and start another one, we'll pause for a little
  93. // so the end game victory screen can be examined by the clients.
  94. //endGame();
  95. endMission();
  96. $Game::Schedule = schedule($Game::EndGamePause * 1000, 0, "onCyclePauseEnd");
  97. }
  98. function onCyclePauseEnd()
  99. {
  100. $Game::Cycling = false;
  101. // Just cycle through the missions for now.
  102. %search = $Server::MissionFileSpec;
  103. %oldMissionFile = makeRelativePath( $Server::MissionFile );
  104. for( %file = findFirstFile( %search ); %file !$= ""; %file = findNextFile( %search ) )
  105. {
  106. if( %file $= %oldMissionFile )
  107. {
  108. // Get the next one, back to the first if there is no next.
  109. %file = findNextFile( %search );
  110. if( %file $= "" )
  111. %file = findFirstFile(%search);
  112. break;
  113. }
  114. }
  115. if( %file $= "" )
  116. %file = findFirstFile( %search );
  117. loadMission(%file);
  118. }
  119. //-----------------------------------------------------------------------------
  120. // GameConnection Methods
  121. // These methods are extensions to the GameConnection class. Extending
  122. // GameConnection makes it easier to deal with some of this functionality,
  123. // but these could also be implemented as stand-alone functions.
  124. //-----------------------------------------------------------------------------
  125. //-----------------------------------------------------------------------------
  126. function GameConnection::onLeaveMissionArea(%this)
  127. {
  128. // The control objects invoke this method when they
  129. // move out of the mission area.
  130. messageClient(%this, 'MsgClientJoin', '\c2Now leaving the mission area!');
  131. }
  132. function GameConnection::onEnterMissionArea(%this)
  133. {
  134. // The control objects invoke this method when they
  135. // move back into the mission area.
  136. messageClient(%this, 'MsgClientJoin', '\c2Now entering the mission area.');
  137. }
  138. //-----------------------------------------------------------------------------
  139. function GameConnection::onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc)
  140. {
  141. game.onDeath(%this, %sourceObject, %sourceClient, %damageType, %damLoc);
  142. }
  143. // ----------------------------------------------------------------------------
  144. // weapon HUD
  145. // ----------------------------------------------------------------------------
  146. function GameConnection::setAmmoAmountHud(%client, %amount, %amountInClips )
  147. {
  148. commandToClient(%client, 'SetAmmoAmountHud', %amount, %amountInClips);
  149. }
  150. function GameConnection::RefreshWeaponHud(%client, %amount, %preview, %ret, %zoomRet, %amountInClips)
  151. {
  152. commandToClient(%client, 'RefreshWeaponHud', %amount, %preview, %ret, %zoomRet, %amountInClips);
  153. }