levelDownload.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Loading Phases:
  28. // Phase 1: Transmit Datablocks
  29. // Transmit targets
  30. // Phase 2: Transmit Ghost Objects
  31. // Phase 3: Start Game
  32. //
  33. // The server invokes the client MissionStartPhase[1-3] function to request
  34. // permission to start each phase. When a client is ready for a phase,
  35. // it responds with MissionStartPhase[1-3]Ack.
  36. //----------------------------------------------------------------------------
  37. // Phase 1
  38. //----------------------------------------------------------------------------
  39. function GameConnection::loadMission(%this)
  40. {
  41. // Send over the information that will display the server info
  42. // when we learn it got there, we'll send the data blocks
  43. %this.currentPhase = 0;
  44. if (%this.isAIControlled())
  45. {
  46. // Cut to the chase...
  47. theLevelInfo.onEnterGame(%this);
  48. }
  49. else
  50. {
  51. commandToClient(%this, 'MissionStartPhase1', $missionSequence, $Server::MissionFile);
  52. echo("*** Sending mission load to client: " @ $Server::MissionFile);
  53. }
  54. }
  55. function serverCmdMissionStartPhase1Ack(%client, %seq)
  56. {
  57. // Make sure to ignore calls from a previous mission load
  58. if (%seq != $missionSequence || !$MissionRunning || %client.currentPhase != 0)
  59. return;
  60. %client.currentPhase = 1;
  61. // Start with the CRC
  62. %client.setMissionCRC( $missionCRC );
  63. // Send over the datablocks...
  64. // OnDataBlocksDone will get called when have confirmation
  65. // that they've all been received.
  66. %client.transmitDataBlocks($missionSequence);
  67. }
  68. function GameConnection::onDataBlocksDone( %this, %missionSequence )
  69. {
  70. // Make sure to ignore calls from a previous mission load
  71. if (%missionSequence != $missionSequence || %this.currentPhase != 1)
  72. return;
  73. %this.currentPhase = 1.5;
  74. // On to the next phase
  75. commandToClient(%this, 'MissionStartPhase2', $missionSequence, $Server::MissionFile);
  76. }
  77. //----------------------------------------------------------------------------
  78. // Phase 2
  79. //----------------------------------------------------------------------------
  80. function serverCmdMissionStartPhase2Ack(%client, %seq)
  81. {
  82. // Make sure to ignore calls from a previous mission load
  83. if (%seq != $missionSequence || !$MissionRunning || %client.currentPhase != 1.5)
  84. return;
  85. %client.currentPhase = 2;
  86. // Update mod paths, this needs to get there before the objects.
  87. %client.transmitPaths();
  88. // Start ghosting objects to the client
  89. %client.activateGhosting();
  90. }
  91. function GameConnection::clientWantsGhostAlwaysRetry(%client)
  92. {
  93. if($MissionRunning)
  94. %client.activateGhosting();
  95. }
  96. function GameConnection::onGhostAlwaysFailed(%client)
  97. {
  98. }
  99. function GameConnection::onGhostAlwaysObjectsReceived(%client)
  100. {
  101. // Ready for next phase.
  102. commandToClient(%client, 'MissionStartPhase3', $missionSequence, $Server::MissionFile);
  103. }
  104. //----------------------------------------------------------------------------
  105. // Phase 3
  106. //----------------------------------------------------------------------------
  107. function serverCmdMissionStartPhase3Ack(%client, %seq)
  108. {
  109. // Make sure to ignore calls from a previous mission load
  110. if(%seq != $missionSequence || !$MissionRunning || %client.currentPhase != 2)
  111. return;
  112. %client.currentPhase = 3;
  113. // Server is ready to drop into the game
  114. %entityIds = parseMissionGroupForIds("Entity", "");
  115. %entityCount = getWordCount(%entityIds);
  116. for(%i=0; %i < %entityCount; %i++)
  117. {
  118. %entity = getWord(%entityIds, %i);
  119. for(%e=0; %e < %entity.getCount(); %e++)
  120. {
  121. %child = %entity.getObject(%e);
  122. if(%child.getCLassName() $= "Entity")
  123. %entityIds = %entityIds SPC %child.getID();
  124. }
  125. %entity.notify("onClientConnect", %client);
  126. }
  127. //Have any special game-play handling here
  128. if(theLevelInfo.isMethod("onClientEnterGame"))
  129. {
  130. theLevelInfo.onClientEnterGame(%client);
  131. }
  132. else
  133. {
  134. //No Game mode class for the level info, so just spawn a default camera
  135. // Set the control object to the default camera
  136. if (!isObject(%client.camera))
  137. {
  138. if(!isObject(Observer))
  139. {
  140. datablock CameraData(Observer)
  141. {
  142. mode = "Observer";
  143. };
  144. }
  145. //if (isDefined("$Game::DefaultCameraClass"))
  146. %client.camera = spawnObject("Camera", Observer);
  147. }
  148. // If we have a camera then set up some properties
  149. if (isObject(%client.camera))
  150. {
  151. MissionCleanup.add( %this.camera );
  152. %client.camera.scopeToClient(%client);
  153. %client.setControlObject(%client.camera);
  154. %client.camera.setTransform("0 0 1 0 0 0 0");
  155. }
  156. }
  157. %client.startMission();
  158. }