ClientServer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // The general flow of a gane - server's creation, loading and hosting clients, and then destruction is as follows:
  2. // First, a client will always create a server in the event that they want to host a single player
  3. // game. Torque3D treats even single player connections as a soft multiplayer game, with some stuff
  4. // in the networking short-circuited to sidestep around lag and packet transmission times.
  5. // initServer() is called, loading the default server scripts.
  6. // After that, if this is a dedicated server session, initDedicated() is called, otherwise initClient is called
  7. // to prep a playable client session.
  8. // When a local game is started - a listen server - via calling StartGame() a server is created and then the client is
  9. // connected to it via createAndConnectToLocalServer().
  10. function ClientServer::create( %this )
  11. {
  12. echo("\n--------- Initializing Directory: scripts ---------");
  13. exec( "./scripts/client/client.cs" );
  14. exec( "./scripts/server/server.cs" );
  15. $Game::MissionGroup = "MissionGroup";
  16. initServer();
  17. %dbList = new ArrayObject(DatablockFilesList);
  18. // Start up in either client, or dedicated server mode
  19. if ($Server::Dedicated)
  20. {
  21. initDedicated();
  22. }
  23. else
  24. {
  25. initClient();
  26. }
  27. }
  28. function ClientServer::destroy( %this )
  29. {
  30. // Ensure that we are disconnected and/or the server is destroyed.
  31. // This prevents crashes due to the SceneGraph being deleted before
  32. // the objects it contains.
  33. if ($Server::Dedicated)
  34. destroyServer();
  35. else
  36. disconnect();
  37. // Destroy the physics plugin.
  38. //physicsDestroy();
  39. sfxShutdown();
  40. echo("Exporting client prefs");
  41. %prefPath = getPrefpath();
  42. export("$pref::*", %prefPath @ "/clientPrefs.cs", false);
  43. echo("Exporting server prefs");
  44. export("$Pref::Server::*", %prefPath @ "/serverPrefs.cs", false);
  45. BanList::Export(%prefPath @ "/banlist.cs");
  46. }
  47. //-----------------------------------------------------------------------------
  48. function StartGame( %mission, %hostingType )
  49. {
  50. if( %mission $= "" )
  51. {
  52. %id = CL_levelList.getSelectedId();
  53. %mission = getField(CL_levelList.getRowTextById(%id), 1);
  54. //error("Cannot start a level with no level selected!");
  55. }
  56. if (%hostingType !$= "")
  57. {
  58. %serverType = %hostingType;
  59. }
  60. else
  61. {
  62. if ($pref::HostMultiPlayer)
  63. %serverType = "MultiPlayer";
  64. else
  65. %serverType = "SinglePlayer";
  66. }
  67. // Show the loading screen immediately.
  68. if ( isObject( LoadingGui ) )
  69. {
  70. Canvas.setContent("LoadingGui");
  71. LoadingProgress.setValue(1);
  72. LoadingProgressTxt.setValue("LOADING MISSION FILE");
  73. Canvas.repaint();
  74. }
  75. createAndConnectToLocalServer( %serverType, %mission );
  76. }
  77. function JoinGame( %serverIndex )
  78. {
  79. // The server info index is stored in the row along with the
  80. // rest of displayed info.
  81. if( setServerInfo( %serverIndex ) )
  82. {
  83. Canvas.setContent("LoadingGui");
  84. LoadingProgress.setValue(1);
  85. LoadingProgressTxt.setValue("WAITING FOR SERVER");
  86. Canvas.repaint();
  87. %conn = new GameConnection(ServerConnection);
  88. %conn.setConnectArgs($pref::Player::Name);
  89. %conn.setJoinPassword($Client::Password);
  90. %conn.connect($ServerInfo::Address);
  91. }
  92. }