00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <Windows.h>
00016 #include <Assert.h>
00017
00018 #include "Host.h"
00019
00020 #include "Server.h"
00021 #include "Client.h"
00022
00023 #include "GMenu.h"
00024
00025 #include "Game\Game.h"
00026
00027 #include "cd.h"
00028
00029 extern int32 CWidth;
00030 extern int32 CHeight;
00031
00032 extern void GenVS_Error(const char *Msg, ...);
00033
00034 Server_Server *GServer;
00035
00036 extern int32 GMode;
00037
00038
00039
00040
00041 Host_Host *Host_Create(geEngine *Engine, Host_Init *InitData, GameMgr *GMgr, VidMode VidMode)
00042 {
00043 Host_Host *NewHost;
00044 geCSNetMgr_NetClient GEClient;
00045 Console_Console *Console;
00046 int32 Width, Height;
00047
00048 assert(Engine != NULL);
00049
00050
00051 NewHost = GE_RAM_ALLOCATE_STRUCT(Host_Host);
00052
00053 assert(NewHost != NULL);
00054
00055 if (!NewHost)
00056 return NULL;
00057
00058
00059 memset(NewHost, 0, sizeof(Host_Host));
00060
00061
00062 Console = GameMgr_GetConsole(GMgr);
00063 assert(Console);
00064
00065
00066 VidMode_GetResolution(GameMgr_GetVidMode(GMgr), &Width, &Height);
00067
00068
00069 NewHost->VidMode = VidMode;
00070 NewHost->Mode = InitData->Mode;
00071 NewHost->GMgr = GMgr;
00072
00073
00074 NewHost->Engine = Engine;
00075
00076
00077
00078
00079 if (InitData->Mode == HOST_MODE_SINGLE_PLAYER)
00080 NewHost->NMgr = NetMgr_Create(GE_TRUE);
00081 else
00082 NewHost->NMgr = NetMgr_Create(GE_FALSE);
00083
00084
00085 if (InitData->Mode != HOST_MODE_SERVER_DEDICATED)
00086 {
00087 Client_Mode ClientMode;
00088
00089 if (InitData->Mode == HOST_MODE_CLIENT || InitData->DemoMode == HOST_DEMO_PLAY)
00090 ClientMode = ClientMode_Proxy;
00091 else
00092 ClientMode = ClientMode_Dumb;
00093
00094
00095
00096 NewHost->Client = Client_Create(Engine, ClientMode, GMgr, NewHost->NMgr, VidMode, InitData->DemoMode, InitData->DemoFile, InitData->Mode != HOST_MODE_SINGLE_PLAYER);
00097
00098 if (!NewHost->Client)
00099 goto ExitWithError;
00100
00101 }
00102
00103
00104 strcpy(GEClient.Name, InitData->ClientName);
00105
00106 if (InitData->Mode == HOST_MODE_SERVER_CLIENT)
00107 {
00108 GMode = 1;
00109
00110
00111 if (!NetMgr_StartSession(NewHost->NMgr, "Genesis Virtual Studio", InitData->ClientName))
00112 goto ExitWithError;
00113
00114
00115 NewHost->Server = Server_Create(GMgr, NewHost->NMgr, NewHost->Client, InitData->LevelHack);
00116 assert(NewHost->Server != NULL);
00117
00118 if (!NewHost->Server)
00119 goto ExitWithError;
00120
00121
00122 GEClient.Id = NetMgr_GetOurID(NewHost->NMgr);
00123 }
00124 else if (InitData->Mode == HOST_MODE_CLIENT)
00125 {
00126 GMode = 2;
00127
00128
00129 if (!GameMgr_ClearBackground(GMgr, (Width/2)-(24*8/2), Height/2-10, "Connecting to server..."))
00130 GenVS_Error("GameMgr_SetWorld: GameMgr_ClearBackground failed.\n");
00131
00132 if (!NetMgr_JoinSession(NewHost->NMgr, InitData->IPAddress, InitData->ClientName))
00133 GenVS_Error("Host_Create: NetMgr_JoinSession failed...");
00134
00135 NewHost->Server = NULL;
00136
00137
00138 GEClient.Id = NetMgr_GetOurID(NewHost->NMgr);
00139
00140 if (!GameMgr_ClearBackground(GMgr, (Width/2)-(13*8/2), Height/2-10, "Connected..."))
00141 GenVS_Error("GameMgr_SetWorld: GameMgr_ClearBackground failed.\n");
00142 }
00143 else if (InitData->Mode == HOST_MODE_SINGLE_PLAYER)
00144 {
00145 GMode = 0;
00146
00147 NewHost->Mode = HOST_MODE_SINGLE_PLAYER;
00148
00149 GEClient.Id = NETMGR_SINGLE_PLAYER_NETID;
00150
00151
00152 if (InitData->DemoMode != HOST_DEMO_PLAY)
00153 {
00154
00155 NewHost->Server = Server_Create(GMgr, NewHost->NMgr, NewHost->Client, InitData->LevelHack);
00156
00157 assert(NewHost->Server != NULL);
00158
00159 if (!NewHost->Server)
00160 goto ExitWithError;
00161
00162
00163
00164 if (!Server_ClientConnect(NewHost->Server, &GEClient))
00165 GenVS_Error("Host_Create: Could not add client in single player mode...\n");
00166 }
00167 }
00168
00169
00170
00171
00172
00173 GServer = NewHost->Server;
00174
00175 NewHost->hWnd = 0;
00176
00177
00178
00179 Console_Printf(GameMgr_GetConsole(NewHost->GMgr), "--- Genesis Host initialized v%i.%i ---\n", NETMGR_VERSION_MAJOR, NETMGR_VERSION_MINOR);
00180
00181 return NewHost;
00182
00183 ExitWithError:
00184 {
00185 if (NewHost)
00186 {
00187 Host_DestroyAllObjects(NewHost);
00188 geRam_Free(NewHost);
00189 }
00190 }
00191 return NULL;
00192 }
00193
00194
00195
00196
00197 void Host_Destroy(Host_Host *Host)
00198 {
00199 assert(Host != NULL);
00200
00201 Host_DestroyAllObjects(Host);
00202
00203 geRam_Free(Host);
00204 }
00205
00206
00207
00208
00209 void Host_ClientRefreshStatusBar(int32 NumPages)
00210 {
00211 Client_RefreshStatusBar(NumPages);
00212 }
00213
00214
00215
00216
00217 void Host_DestroyAllObjects(Host_Host *Host)
00218 {
00219 if (Host->Server)
00220 {
00221 Server_Destroy(Host->Server);
00222 Host->Server = NULL;
00223 }
00224
00225 if (Host->Client)
00226 {
00227 Client_Destroy(Host->Client);
00228 Host->Client = NULL;
00229 }
00230
00231 if (Host->NMgr)
00232 {
00233 NetMgr_Destroy(Host->NMgr);
00234 Host->NMgr = NULL;
00235 }
00236
00237 if (Host->GMgr)
00238 {
00239
00240 GameMgr_FreeWorld(Host->GMgr);
00241 }
00242 }
00243
00244
00245
00246
00247 geBoolean Host_Frame(Host_Host *Host, float Time)
00248 {
00249
00250 if (Time < 0.001f)
00251 Time = 0.001f;
00252
00253 if (Time > 0.1f)
00254 Time = 0.1f;
00255
00256
00257
00258 Host->Engine = GameMgr_GetEngine(Host->GMgr);
00259
00260
00261
00262
00263 if (Host->Server)
00264 {
00265 if (!Server_Frame(Host->Server, Host->GMgr, Time))
00266 return GE_FALSE;
00267 }
00268
00269
00270
00271
00272 NetMgr_ResetClientBuffer(Host->NMgr);
00273
00274 if (Host->Client)
00275 {
00276 if (!Client_Frame(Host->Client, Time))
00277 return GE_FALSE;
00278 }
00279
00280 NetMgr_ResetServerBuffer(Host->NMgr);
00281
00282 return GE_TRUE;
00283 }
00284
00285
00286
00287
00288 geBoolean Host_RenderFrame(Host_Host *Host, float Time)
00289 {
00290 if (Host->Client)
00291 {
00292 if (!Client_RenderFrame(Host->Client, Time))
00293 {
00294 geErrorLog_AddString(-1, "Host_RenderFrame: Client_RenderFrame failed...", NULL);
00295 return GE_FALSE;
00296 }
00297 }
00298
00299 return GE_TRUE;
00300 }