DEADSOFTWARE

removed e_writelog from g_netmaster
[d2df-sdl.git] / src / game / g_netmaster.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_netmaster;
19 interface
21 uses ENet;
23 const
24 NET_MCHANS = 2;
26 NET_MCHAN_MAIN = 0;
27 NET_MCHAN_UPD = 1;
29 NET_MMSG_UPD = 200;
30 NET_MMSG_DEL = 201;
31 NET_MMSG_GET = 202;
33 type
34 TNetServer = record
35 Number: Byte;
36 Protocol: Byte;
37 Name: string;
38 IP: string;
39 Port: Word;
40 Map: string;
41 Players, MaxPlayers, LocalPl, Bots: Byte;
42 Ping: Int64;
43 GameMode: Byte;
44 Password: Boolean;
45 PingAddr: ENetAddress;
46 end;
47 pTNetServer = ^TNetServer;
49 TNetServerList = array of TNetServer;
50 pTNetServerList = ^TNetServerList;
52 var
53 NetMHost: pENetHost = nil;
54 NetMPeer: pENetPeer = nil;
56 slCurrent: TNetServerList = nil;
57 slWaitStr: string = '';
58 slReturnPressed: Boolean = True;
60 procedure g_Net_Slist_Set(IP: string; Port: Word);
61 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
62 procedure g_Net_Slist_Update();
63 procedure g_Net_Slist_Remove();
64 function g_Net_Slist_Connect(): Boolean;
65 procedure g_Net_Slist_Check();
66 procedure g_Net_Slist_Disconnect();
67 procedure g_Net_Slist_WriteInfo();
69 procedure g_Serverlist_Draw(var SL: TNetServerList);
70 procedure g_Serverlist_Control(var SL: TNetServerList);
72 implementation
74 uses
75 SysUtils, e_msg, e_input, e_graphics, e_log, g_window, g_net, g_console,
76 g_map, g_game, g_sound, g_textures, g_gui, g_menu, g_options, g_language, wadreader;
78 var
79 NetMEvent: ENetEvent;
80 slSelection: Byte = 0;
81 slFetched: Boolean = False;
82 slDirPressed: Boolean = False;
84 function GetTimerMS(): Int64;
85 begin
86 Result := GetTimer() {div 1000};
87 end;
89 procedure PingServer(var S: TNetServer; Sock: ENetSocket);
90 var
91 Buf: ENetBuffer;
92 Ping: array [0..9] of Byte;
93 ClTime: Int64;
94 begin
95 ClTime := GetTimerMS();
97 Buf.data := Addr(Ping[0]);
98 Buf.dataLength := 2+8;
100 Ping[0] := Ord('D');
101 Ping[1] := Ord('F');
102 Int64(Addr(Ping[2])^) := ClTime;
104 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
105 end;
107 function g_Net_Slist_Fetch(var SL: TNetServerList): Boolean;
108 var
109 Cnt: Byte;
110 P: pENetPacket;
111 MID: Byte;
112 I, RX: Integer;
113 T: Int64;
114 Sock: ENetSocket;
115 Buf: ENetBuffer;
116 InMsg: TMsg;
117 SvAddr: ENetAddress;
118 begin
119 Result := False;
120 SL := nil;
122 if (NetMHost <> nil) or (NetMPeer <> nil) then
123 Exit;
125 if not g_Net_Slist_Connect then
126 Exit;
128 e_WriteLog('Fetching serverlist...', MSG_NOTIFY);
129 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
131 NetOut.Clear();
132 NetOut.Write(Byte(NET_MMSG_GET));
134 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
135 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
136 enet_host_flush(NetMHost);
138 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
139 begin
140 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
141 begin
142 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
144 MID := InMsg.ReadByte();
146 if MID <> NET_MMSG_GET then continue;
148 Cnt := InMsg.ReadByte();
149 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
151 if Cnt > 0 then
152 begin
153 SetLength(SL, Cnt);
155 for I := 0 to Cnt - 1 do
156 begin
157 SL[I].Number := I;
158 SL[I].IP := InMsg.ReadString();
159 SL[I].Port := InMsg.ReadWord();
160 SL[I].Name := InMsg.ReadString();
161 SL[I].Map := InMsg.ReadString();
162 SL[I].GameMode := InMsg.ReadByte();
163 SL[I].Players := InMsg.ReadByte();
164 SL[I].MaxPlayers := InMsg.ReadByte();
165 SL[I].Protocol := InMsg.ReadByte();
166 SL[I].Password := InMsg.ReadByte() = 1;
167 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
168 SL[I].Ping := -1;
169 SL[I].PingAddr.port := SL[I].Port + 1;
170 end;
171 end;
173 Result := True;
174 break;
175 end;
176 end;
178 g_Net_Slist_Disconnect;
179 NetOut.Clear();
181 if Length(SL) = 0 then Exit;
183 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
184 if Sock = ENET_SOCKET_NULL then Exit;
185 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
187 for I := Low(SL) to High(SL) do
188 PingServer(SL[I], Sock);
190 T := GetTimerMS();
192 InMsg.Alloc(NET_BUFSIZE);
193 Buf.data := InMsg.Data;
194 Buf.dataLength := InMsg.MaxSize;
195 Cnt := 0;
196 while Cnt < Length(SL) do
197 begin
198 if GetTimerMS() - T > 500 then break;
200 InMsg.Clear();
202 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
203 if RX <= 0 then continue;
204 InMsg.CurSize := RX;
206 InMsg.BeginReading();
208 if InMsg.ReadChar() <> 'D' then continue;
209 if InMsg.ReadChar() <> 'F' then continue;
211 for I := Low(SL) to High(SL) do
212 if (SL[I].PingAddr.host = SvAddr.host) and
213 (SL[I].PingAddr.port = SvAddr.port) then
214 begin
215 with SL[I] do
216 begin
217 Ping := InMsg.ReadInt64();
218 Ping := GetTimerMS() - Ping;
219 Name := InMsg.ReadString();
220 Map := InMsg.ReadString();
221 GameMode := InMsg.ReadByte();
222 Players := InMsg.ReadByte();
223 MaxPlayers := InMsg.ReadByte();
224 Protocol := InMsg.ReadByte();
225 Password := InMsg.ReadByte() = 1;
226 LocalPl := InMsg.ReadByte();
227 Bots := InMsg.ReadWord();
228 end;
229 Inc(Cnt);
230 break;
231 end;
232 end;
234 InMsg.Free();
235 enet_socket_destroy(Sock);
236 end;
238 procedure g_Net_Slist_WriteInfo();
239 var
240 Wad, Map: string;
241 Cli: Byte;
242 begin
243 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
244 Map := g_ExtractFileName(gMapInfo.Map);
246 NetOut.Write(NetServerName);
248 NetOut.Write(Wad + ':\' + Map);
249 NetOut.Write(gGameSettings.GameMode);
251 Cli := NetClientCount;
252 NetOut.Write(Cli);
254 NetOut.Write(NetMaxClients);
256 NetOut.Write(Byte(NET_PROTOCOL_VER));
257 NetOut.Write(Byte(NetPassword <> ''));
258 end;
260 procedure g_Net_Slist_Update;
261 var
263 P: pENetPacket;
265 begin
266 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
268 NetOut.Clear();
269 NetOut.Write(Byte(NET_MMSG_UPD));
270 NetOut.Write(NetAddr.port);
272 g_Net_Slist_WriteInfo();
274 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
275 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
277 enet_host_flush(NetMHost);
278 NetOut.Clear();
279 end;
281 procedure g_Net_Slist_Remove;
282 var
283 P: pENetPacket;
284 begin
285 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
286 NetOut.Clear();
287 NetOut.Write(Byte(NET_MMSG_DEL));
288 NetOut.Write(NetAddr.port);
290 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
291 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
293 enet_host_flush(NetMHost);
294 NetOut.Clear();
295 end;
297 function g_Net_Slist_Connect: Boolean;
298 begin
299 Result := False;
301 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
302 if (NetMHost = nil) then
303 begin
304 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
305 Exit;
306 end;
308 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
309 if (NetMPeer = nil) then
310 begin
311 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
312 enet_host_destroy(NetMHost);
313 NetMHost := nil;
314 Exit;
315 end;
317 if (enet_host_service(NetMHost, @NetMEvent, 3000) > 0) then
318 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
319 begin
320 Result := True;
321 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
322 Exit;
323 end
324 else
325 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
326 enet_packet_destroy(NetMEvent.packet);
328 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
330 NetMHost := nil;
331 NetMPeer := nil;
332 end;
334 procedure g_Net_Slist_Disconnect;
335 begin
336 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
338 if NetMode = NET_SERVER then g_Net_Slist_Remove;
340 enet_peer_disconnect(NetMPeer, 0);
341 enet_host_flush(NetMHost);
343 enet_peer_reset(NetMPeer);
344 enet_host_destroy(NetMHost);
346 NetMPeer := nil;
347 NetMHost := nil;
349 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
350 end;
352 procedure g_Net_Slist_Check;
353 begin
354 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
356 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
357 begin
358 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
359 begin
360 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
361 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
362 if NetMHost <> nil then enet_host_destroy(NetMHost);
363 NetMPeer := nil;
364 NetMHost := nil;
365 Break;
366 end
367 else
368 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
369 enet_packet_destroy(NetMEvent.packet);
370 end;
371 end;
373 procedure g_Net_Slist_Set(IP: string; Port: Word);
374 begin
375 if NetInitDone then
376 begin
377 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
378 NetSlistAddr.Port := Port;
379 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), MSG_NOTIFY);
380 end;
381 end;
383 procedure g_Serverlist_Draw(var SL: TNetServerList);
384 var
385 sy, i, y, mw, mx, l: Integer;
386 cw, ch: Byte;
387 ww, hh: Word;
388 ip: string;
389 begin
390 ip := '';
391 sy := 0;
393 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
394 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
396 e_TextureFontGetSize(gStdFont, cw, ch);
398 ip := _lc[I_NET_SLIST_HELP];
399 mw := (Length(ip) * cw) div 2;
401 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
402 e_DrawQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
404 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
406 if SL = nil then
407 begin
408 l := Length(slWaitStr) div 2;
409 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 128);
410 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
411 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
412 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
413 slWaitStr, gStdFont);
414 Exit;
415 end;
417 y := 90;
418 if (slSelection < Length(SL)) then
419 begin
420 I := slSelection;
421 sy := y + 42 * I - 4;
422 ip := _lc[I_NET_ADDRESS] + ' ' + SL[I].IP + ':' + IntToStr(SL[I].Port);
423 if SL[I].Password then
424 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
425 else
426 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
427 end else
428 if Length(SL) > 0 then
429 slSelection := 0;
431 mw := (gScreenWidth - 188);
432 mx := 16 + mw;
434 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
435 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
436 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
438 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
439 e_DrawLine(1, 16, gScreenHeight-64, gScreenWidth-16, gScreenHeight-64, 255, 127, 0);
441 e_DrawLine(1, mx - 70, 64, mx - 70, gScreenHeight-44, 255, 127, 0);
442 e_DrawLine(1, mx, 64, mx, gScreenHeight-64, 255, 127, 0);
443 e_DrawLine(1, mx + 52, 64, mx + 52, gScreenHeight-64, 255, 127, 0);
444 e_DrawLine(1, mx + 104, 64, mx + 104, gScreenHeight-64, 255, 127, 0);
446 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
448 y := 90;
449 for I := 0 to High(SL) do
450 begin
451 e_TextureFontPrintEx(18, y, SL[I].Name, gStdFont, 255, 255, 255, 1);
452 e_TextureFontPrintEx(18, y + 16, SL[I].Map, gStdFont, 210, 210, 210, 1);
454 y := y + 42;
455 end;
457 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
458 y := 90;
459 for I := 0 to High(SL) do
460 begin
461 if (SL[I].Ping < 0) or (SL[I].Ping > 999) then
462 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
463 else
464 if SL[I].Ping = 0 then
465 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
466 else
467 e_TextureFontPrintEx(mx - 68, y, IntToStr(SL[I].Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
469 y := y + 42;
470 end;
472 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
473 y := 90;
474 for I := 0 to High(SL) do
475 begin
476 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(SL[I].GameMode), gStdFont, 255, 255, 255, 1);
478 y := y + 42;
479 end;
481 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
482 y := 90;
483 for I := 0 to High(SL) do
484 begin
485 e_TextureFontPrintEx(mx + 54, y, IntToStr(SL[I].Players) + '/' + IntToStr(SL[I].MaxPlayers), gStdFont, 255, 255, 255, 1);
486 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(SL[I].LocalPl) + '+' + IntToStr(SL[I].Bots), gStdFont, 210, 210, 210, 1);
487 y := y + 42;
488 end;
490 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
491 y := 90;
492 for I := 0 to High(SL) do
493 begin
494 e_TextureFontPrintEx(mx + 106, y, IntToStr(SL[I].Protocol), gStdFont, 255, 255, 255, 1);
496 y := y + 42;
497 end;
499 e_TextureFontPrintEx(20, gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
500 ip := IntToStr(Length(SL)) + _lc[I_NET_SLIST_SERVERS];
501 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
502 gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
503 end;
505 procedure g_Serverlist_Control(var SL: TNetServerList);
506 begin
507 if gConsoleShow or gChatShow then
508 Exit;
510 e_PollInput();
512 if e_KeyPressed(IK_ESCAPE) then
513 begin
514 SL := nil;
515 gState := STATE_MENU;
516 g_GUI_ShowWindow('MainMenu');
517 g_GUI_ShowWindow('NetGameMenu');
518 g_GUI_ShowWindow('NetClientMenu');
519 g_Sound_PlayEx(WINDOW_CLOSESOUND);
520 Exit;
521 end;
523 if e_KeyPressed(IK_SPACE) then
524 begin
525 if not slFetched then
526 begin
527 slWaitStr := _lc[I_NET_SLIST_WAIT];
529 g_Game_Draw;
530 g_window.ReDrawWindow;
532 if g_Net_Slist_Fetch(SL) then
533 begin
534 if SL = nil then
535 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
536 end
537 else
538 slWaitStr := _lc[I_NET_SLIST_ERROR];
539 slFetched := True;
540 slSelection := 0;
541 end;
542 end
543 else
544 slFetched := False;
546 if SL = nil then Exit;
548 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) then
549 begin
550 if not slReturnPressed then
551 begin
552 if SL[slSelection].Password then
553 begin
554 PromptIP := SL[slSelection].IP;
555 PromptPort := SL[slSelection].Port;
556 gState := STATE_MENU;
557 g_GUI_ShowWindow('ClientPasswordMenu');
558 SL := nil;
559 slReturnPressed := True;
560 Exit;
561 end
562 else
563 g_Game_StartClient(SL[slSelection].IP, SL[slSelection].Port, '');
564 SL := nil;
565 slReturnPressed := True;
566 Exit;
567 end;
568 end
569 else
570 slReturnPressed := False;
572 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) then
573 begin
574 if not slDirPressed then
575 begin
576 Inc(slSelection);
577 if slSelection > High(SL) then slSelection := 0;
578 slDirPressed := True;
579 end;
580 end;
582 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) then
583 begin
584 if not slDirPressed then
585 begin
586 if slSelection = 0 then slSelection := Length(SL);
587 Dec(slSelection);
589 slDirPressed := True;
590 end;
591 end;
593 if (not e_KeyPressed(IK_DOWN)) and (not e_KeyPressed(IK_UP)) and (not e_KeyPressed(IK_KPDOWN)) and (not e_KeyPressed(IK_KPUP)) then
594 slDirPressed := False;
595 end;
597 end.