DEADSOFTWARE

removed all mentions of dynaabb tree from the sources; WARNING! EVERYTHING IS BROKEN!
[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_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: Byte = 0;
387 ch: Byte = 0;
388 ww: Word = 0;
389 hh: Word = 0;
390 ip: string;
391 begin
392 ip := '';
393 sy := 0;
395 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
396 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
398 e_TextureFontGetSize(gStdFont, cw, ch);
400 ip := _lc[I_NET_SLIST_HELP];
401 mw := (Length(ip) * cw) div 2;
403 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
404 e_DrawQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
406 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
408 if SL = nil then
409 begin
410 l := Length(slWaitStr) div 2;
411 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 128);
412 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
413 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
414 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
415 slWaitStr, gStdFont);
416 Exit;
417 end;
419 y := 90;
420 if (slSelection < Length(SL)) then
421 begin
422 I := slSelection;
423 sy := y + 42 * I - 4;
424 ip := _lc[I_NET_ADDRESS] + ' ' + SL[I].IP + ':' + IntToStr(SL[I].Port);
425 if SL[I].Password then
426 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
427 else
428 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
429 end else
430 if Length(SL) > 0 then
431 slSelection := 0;
433 mw := (gScreenWidth - 188);
434 mx := 16 + mw;
436 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
437 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
438 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
440 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
441 e_DrawLine(1, 16, gScreenHeight-64, gScreenWidth-16, gScreenHeight-64, 255, 127, 0);
443 e_DrawLine(1, mx - 70, 64, mx - 70, gScreenHeight-44, 255, 127, 0);
444 e_DrawLine(1, mx, 64, mx, gScreenHeight-64, 255, 127, 0);
445 e_DrawLine(1, mx + 52, 64, mx + 52, gScreenHeight-64, 255, 127, 0);
446 e_DrawLine(1, mx + 104, 64, mx + 104, gScreenHeight-64, 255, 127, 0);
448 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
450 y := 90;
451 for I := 0 to High(SL) do
452 begin
453 e_TextureFontPrintEx(18, y, SL[I].Name, gStdFont, 255, 255, 255, 1);
454 e_TextureFontPrintEx(18, y + 16, SL[I].Map, gStdFont, 210, 210, 210, 1);
456 y := y + 42;
457 end;
459 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
460 y := 90;
461 for I := 0 to High(SL) do
462 begin
463 if (SL[I].Ping < 0) or (SL[I].Ping > 999) then
464 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
465 else
466 if SL[I].Ping = 0 then
467 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
468 else
469 e_TextureFontPrintEx(mx - 68, y, IntToStr(SL[I].Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
471 y := y + 42;
472 end;
474 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
475 y := 90;
476 for I := 0 to High(SL) do
477 begin
478 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(SL[I].GameMode), gStdFont, 255, 255, 255, 1);
480 y := y + 42;
481 end;
483 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
484 y := 90;
485 for I := 0 to High(SL) do
486 begin
487 e_TextureFontPrintEx(mx + 54, y, IntToStr(SL[I].Players) + '/' + IntToStr(SL[I].MaxPlayers), gStdFont, 255, 255, 255, 1);
488 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(SL[I].LocalPl) + '+' + IntToStr(SL[I].Bots), gStdFont, 210, 210, 210, 1);
489 y := y + 42;
490 end;
492 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
493 y := 90;
494 for I := 0 to High(SL) do
495 begin
496 e_TextureFontPrintEx(mx + 106, y, IntToStr(SL[I].Protocol), gStdFont, 255, 255, 255, 1);
498 y := y + 42;
499 end;
501 e_TextureFontPrintEx(20, gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
502 ip := IntToStr(Length(SL)) + _lc[I_NET_SLIST_SERVERS];
503 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
504 gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
505 end;
507 procedure g_Serverlist_Control(var SL: TNetServerList);
508 begin
509 if gConsoleShow or gChatShow then
510 Exit;
512 e_PollInput();
514 if e_KeyPressed(IK_ESCAPE) then
515 begin
516 SL := nil;
517 gState := STATE_MENU;
518 g_GUI_ShowWindow('MainMenu');
519 g_GUI_ShowWindow('NetGameMenu');
520 g_GUI_ShowWindow('NetClientMenu');
521 g_Sound_PlayEx(WINDOW_CLOSESOUND);
522 Exit;
523 end;
525 if e_KeyPressed(IK_SPACE) then
526 begin
527 if not slFetched then
528 begin
529 slWaitStr := _lc[I_NET_SLIST_WAIT];
531 g_Game_Draw;
532 g_window.ReDrawWindow;
534 if g_Net_Slist_Fetch(SL) then
535 begin
536 if SL = nil then
537 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
538 end
539 else
540 slWaitStr := _lc[I_NET_SLIST_ERROR];
541 slFetched := True;
542 slSelection := 0;
543 end;
544 end
545 else
546 slFetched := False;
548 if SL = nil then Exit;
550 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) then
551 begin
552 if not slReturnPressed then
553 begin
554 if SL[slSelection].Password then
555 begin
556 PromptIP := SL[slSelection].IP;
557 PromptPort := SL[slSelection].Port;
558 gState := STATE_MENU;
559 g_GUI_ShowWindow('ClientPasswordMenu');
560 SL := nil;
561 slReturnPressed := True;
562 Exit;
563 end
564 else
565 g_Game_StartClient(SL[slSelection].IP, SL[slSelection].Port, '');
566 SL := nil;
567 slReturnPressed := True;
568 Exit;
569 end;
570 end
571 else
572 slReturnPressed := False;
574 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) then
575 begin
576 if not slDirPressed then
577 begin
578 Inc(slSelection);
579 if slSelection > High(SL) then slSelection := 0;
580 slDirPressed := True;
581 end;
582 end;
584 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) then
585 begin
586 if not slDirPressed then
587 begin
588 if slSelection = 0 then slSelection := Length(SL);
589 Dec(slSelection);
591 slDirPressed := True;
592 end;
593 end;
595 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
596 slDirPressed := False;
597 end;
599 end.