levelLoad.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //-----------------------------------------------------------------------------
  23. // Mission Loading
  24. // The server portion of the client/server mission loading process
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. // Server mission loading
  28. //-----------------------------------------------------------------------------
  29. // On every mission load except the first, there is a pause after
  30. // the initial mission info is downloaded to the client.
  31. $MissionLoadPause = 5000;
  32. //-----------------------------------------------------------------------------
  33. //This is the first call made by the server to kick the loading process off
  34. function loadMission( %missionName, %isFirstMission )
  35. {
  36. endMission();
  37. echo("*** LOADING MISSION: " @ %missionName);
  38. echo("*** Stage 1 load");
  39. // increment the mission sequence (used for ghost sequencing)
  40. $missionSequence++;
  41. $missionRunning = false;
  42. $Server::MissionFile = %missionName;
  43. $Server::LoadFailMsg = "";
  44. // Extract mission info from the mission file,
  45. // including the display name and stuff to send
  46. // to the client.
  47. buildLoadInfo( %missionName );
  48. // Download mission info to the clients
  49. %count = ClientGroup.getCount();
  50. for( %cl = 0; %cl < %count; %cl++ )
  51. {
  52. %client = ClientGroup.getObject( %cl );
  53. if (!%client.isAIControlled())
  54. sendLoadInfoToClient(%client);
  55. }
  56. // Now that we've sent the LevelInfo to the clients
  57. // clear it so that it won't conflict with the actual
  58. // LevelInfo loaded in the level
  59. clearLoadInfo();
  60. // if this isn't the first mission, allow some time for the server
  61. // to transmit information to the clients:
  62. if( %isFirstMission || $Server::ServerType $= "SinglePlayer" )
  63. loadMissionStage2();
  64. else
  65. schedule( $MissionLoadPause, ServerGroup, loadMissionStage2 );
  66. }
  67. //-----------------------------------------------------------------------------
  68. function loadMissionStage2()
  69. {
  70. echo("*** Stage 2 load");
  71. // Create the mission group off the ServerGroup
  72. $instantGroup = ServerGroup;
  73. // Make sure the mission exists
  74. %file = $Server::MissionFile;
  75. if( !isFile( %file ) )
  76. {
  77. $Server::LoadFailMsg = "Could not find mission \"" @ %file @ "\"";
  78. }
  79. else
  80. {
  81. // Calculate the mission CRC. The CRC is used by the clients
  82. // to caching mission lighting.
  83. $missionCRC = getFileCRC( %file );
  84. // Exec the mission. The MissionGroup (loaded components) is added to the ServerGroup
  85. exec(%file);
  86. if( !isObject(MissionGroup) )
  87. {
  88. $Server::LoadFailMsg = "No 'MissionGroup' found in mission \"" @ %file @ "\".";
  89. }
  90. }
  91. if( $Server::LoadFailMsg !$= "" )
  92. {
  93. // Inform clients that are already connected
  94. for (%clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++)
  95. messageClient(ClientGroup.getObject(%clientIndex), 'MsgLoadFailed', $Server::LoadFailMsg);
  96. return;
  97. }
  98. // Set mission name.
  99. if( isObject( theLevelInfo ) )
  100. $Server::MissionName = theLevelInfo.levelName;
  101. // Mission cleanup group. This is where run time components will reside. The MissionCleanup
  102. // group will be added to the ServerGroup.
  103. new SimGroup( MissionCleanup );
  104. // Make the MissionCleanup group the place where all new objects will automatically be added.
  105. $instantGroup = MissionCleanup;
  106. // Construct MOD paths
  107. pathOnMissionLoadDone();
  108. // Mission loading done...
  109. echo("*** Mission loaded");
  110. // Start all the clients in the mission
  111. $missionRunning = true;
  112. for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ )
  113. ClientGroup.getObject(%clientIndex).loadMission();
  114. // Go ahead and launch the game
  115. if(TheLevelInfo.isMethod("onMissionStart"))
  116. TheLevelInfo.onMissionStart();
  117. }
  118. function endMission()
  119. {
  120. if (!isObject( MissionGroup ))
  121. return;
  122. echo("*** ENDING MISSION");
  123. // Inform the game code we're done.
  124. TheLevelInfo.onMissionEnded();
  125. // Inform the clients
  126. for( %clientIndex = 0; %clientIndex < ClientGroup.getCount(); %clientIndex++ ) {
  127. // clear ghosts and paths from all clients
  128. %cl = ClientGroup.getObject( %clientIndex );
  129. %cl.endMission();
  130. %cl.resetGhosting();
  131. %cl.clearPaths();
  132. }
  133. // Delete everything
  134. MissionGroup.delete();
  135. MissionCleanup.delete();
  136. clearServerPaths();
  137. }
  138. function resetMission()
  139. {
  140. echo("*** MISSION RESET");
  141. // Remove any temporary mission objects
  142. MissionCleanup.delete();
  143. $instantGroup = ServerGroup;
  144. new SimGroup( MissionCleanup );
  145. $instantGroup = MissionCleanup;
  146. clearServerPaths();
  147. //
  148. TheLevelInfo.onMissionReset();
  149. }