DEADSOFTWARE

replaced e_fixedbuffer with e_msg
[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));
133 e_WriteLog('Wr ' + IntToStr(PByte(NetOut.Data)^) + ' !.', MSG_NOTIFY);
134 e_WriteLog('Wr ' + IntToStr(NetOut.CurSize) + ' !.', MSG_NOTIFY);
136 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
137 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
138 enet_host_flush(NetMHost);
140 while enet_host_service(NetMHost, @NetMEvent, 5000) > 0 do
141 begin
142 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
143 begin
144 if not InMsg.Init(NetMEvent.packet^.data, NetMEvent.packet^.dataLength, True) then continue;
146 MID := InMsg.ReadByte();
147 e_WriteLog('Retrieved ' + IntToStr(MID) + ' !.', MSG_NOTIFY);
149 if MID <> NET_MMSG_GET then continue;
151 Cnt := InMsg.ReadByte();
152 e_WriteLog('Retrieved ' + IntToStr(Cnt) + ' server(s).', MSG_NOTIFY);
153 g_Console_Add(_lc[I_NET_MSG] + Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
154 //writeln('BOO!');
156 if Cnt > 0 then
157 begin
158 SetLength(SL, Cnt);
160 for I := 0 to Cnt - 1 do
161 begin
162 SL[I].Number := I;
163 SL[I].IP := InMsg.ReadString();
164 SL[I].Port := InMsg.ReadWord();
165 SL[I].Name := InMsg.ReadString();
166 SL[I].Map := InMsg.ReadString();
167 SL[I].GameMode := InMsg.ReadByte();
168 SL[I].Players := InMsg.ReadByte();
169 SL[I].MaxPlayers := InMsg.ReadByte();
170 SL[I].Protocol := InMsg.ReadByte();
171 SL[I].Password := InMsg.ReadByte() = 1;
172 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
173 SL[I].Ping := -1;
174 SL[I].PingAddr.port := SL[I].Port + 1;
175 end;
176 end;
178 Result := True;
179 break;
180 end;
181 end;
183 g_Net_Slist_Disconnect;
184 NetOut.Clear();
186 if Length(SL) = 0 then Exit;
188 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
189 if Sock = ENET_SOCKET_NULL then Exit;
190 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
192 for I := Low(SL) to High(SL) do
193 PingServer(SL[I], Sock);
195 T := GetTimerMS();
197 InMsg.Alloc(NET_BUFSIZE);
198 Buf.data := InMsg.Data;
199 Buf.dataLength := InMsg.MaxSize;
200 Cnt := 0;
201 while Cnt < Length(SL) do
202 begin
203 if GetTimerMS() - T > 500 then break;
205 InMsg.Clear();
207 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
208 if RX <= 0 then continue;
209 InMsg.CurSize := RX;
211 InMsg.BeginReading();
213 if InMsg.ReadChar() <> 'D' then continue;
214 if InMsg.ReadChar() <> 'F' then continue;
216 for I := Low(SL) to High(SL) do
217 if (SL[I].PingAddr.host = SvAddr.host) and
218 (SL[I].PingAddr.port = SvAddr.port) then
219 begin
220 with SL[I] do
221 begin
222 Ping := InMsg.ReadInt64();
223 Ping := GetTimerMS() - Ping;
224 Name := InMsg.ReadString();
225 Map := InMsg.ReadString();
226 GameMode := InMsg.ReadByte();
227 Players := InMsg.ReadByte();
228 MaxPlayers := InMsg.ReadByte();
229 Protocol := InMsg.ReadByte();
230 Password := InMsg.ReadByte() = 1;
231 LocalPl := InMsg.ReadByte();
232 Bots := InMsg.ReadWord();
233 end;
234 Inc(Cnt);
235 break;
236 end;
237 end;
239 InMsg.Free();
240 enet_socket_destroy(Sock);
241 end;
243 procedure g_Net_Slist_WriteInfo();
244 var
245 Wad, Map: string;
246 Cli: Byte;
247 begin
248 Wad := g_ExtractWadNameNoPath(gMapInfo.Map);
249 Map := g_ExtractFileName(gMapInfo.Map);
251 NetOut.Write(NetServerName);
253 NetOut.Write(Wad + ':\' + Map);
254 NetOut.Write(gGameSettings.GameMode);
256 Cli := NetClientCount;
257 NetOut.Write(Cli);
259 NetOut.Write(NetMaxClients);
261 NetOut.Write(Byte(NET_PROTOCOL_VER));
262 NetOut.Write(Byte(NetPassword <> ''));
263 end;
265 procedure g_Net_Slist_Update;
266 var
268 P: pENetPacket;
270 begin
271 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
273 NetOut.Clear();
274 NetOut.Write(Byte(NET_MMSG_UPD));
275 NetOut.Write(NetAddr.port);
277 g_Net_Slist_WriteInfo();
279 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
280 enet_peer_send(NetMPeer, NET_MCHAN_UPD, P);
282 enet_host_flush(NetMHost);
283 NetOut.Clear();
284 end;
286 procedure g_Net_Slist_Remove;
287 var
288 P: pENetPacket;
289 begin
290 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
291 NetOut.Clear();
292 NetOut.Write(Byte(NET_MMSG_DEL));
293 NetOut.Write(NetAddr.port);
295 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
296 enet_peer_send(NetMPeer, NET_MCHAN_MAIN, P);
298 enet_host_flush(NetMHost);
299 NetOut.Clear();
300 end;
302 function g_Net_Slist_Connect: Boolean;
303 begin
304 Result := False;
306 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
307 if (NetMHost = nil) then
308 begin
309 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
310 Exit;
311 end;
313 NetMPeer := enet_host_connect(NetMHost, @NetSlistAddr, NET_MCHANS, 0);
314 if (NetMPeer = nil) then
315 begin
316 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_ERR_CLIENT], True);
317 enet_host_destroy(NetMHost);
318 NetMHost := nil;
319 Exit;
320 end;
322 if (enet_host_service(NetMHost, @NetMEvent, 3000) > 0) then
323 if NetMEvent.kind = ENET_EVENT_TYPE_CONNECT then
324 begin
325 Result := True;
326 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_CONN]);
327 Exit;
328 end
329 else
330 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
331 enet_packet_destroy(NetMEvent.packet);
333 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
335 NetMHost := nil;
336 NetMPeer := nil;
337 end;
339 procedure g_Net_Slist_Disconnect;
340 begin
341 if (NetMHost = nil) and (NetMPeer = nil) then Exit;
343 if NetMode = NET_SERVER then g_Net_Slist_Remove;
345 enet_peer_disconnect(NetMPeer, 0);
346 enet_host_flush(NetMHost);
348 enet_peer_reset(NetMPeer);
349 enet_host_destroy(NetMHost);
351 NetMPeer := nil;
352 NetMHost := nil;
354 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
355 end;
357 procedure g_Net_Slist_Check;
358 begin
359 if (NetMHost = nil) or (NetMPeer = nil) then Exit;
361 while (enet_host_service(NetMHost, @NetMEvent, 0) > 0) do
362 begin
363 if NetMEvent.kind = ENET_EVENT_TYPE_DISCONNECT then
364 begin
365 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_LOST], True);
366 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
367 if NetMHost <> nil then enet_host_destroy(NetMHost);
368 NetMPeer := nil;
369 NetMHost := nil;
370 Break;
371 end
372 else
373 if NetMEvent.kind = ENET_EVENT_TYPE_RECEIVE then
374 enet_packet_destroy(NetMEvent.packet);
375 end;
376 end;
378 procedure g_Net_Slist_Set(IP: string; Port: Word);
379 begin
380 if NetInitDone then
381 begin
382 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
383 NetSlistAddr.Port := Port;
384 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), MSG_NOTIFY);
385 end;
386 end;
388 procedure g_Serverlist_Draw(var SL: TNetServerList);
389 var
390 sy, i, y, mw, mx, l: Integer;
391 cw, ch: Byte;
392 ww, hh: Word;
393 ip: string;
394 begin
395 ip := '';
396 sy := 0;
398 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
399 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
401 e_TextureFontGetSize(gStdFont, cw, ch);
403 ip := _lc[I_NET_SLIST_HELP];
404 mw := (Length(ip) * cw) div 2;
406 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
407 e_DrawQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
409 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
411 if SL = nil then
412 begin
413 l := Length(slWaitStr) div 2;
414 e_DrawFillQuad(16, 64, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 128);
415 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
416 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
417 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
418 slWaitStr, gStdFont);
419 Exit;
420 end;
422 y := 90;
423 if (slSelection < Length(SL)) then
424 begin
425 I := slSelection;
426 sy := y + 42 * I - 4;
427 ip := _lc[I_NET_ADDRESS] + ' ' + SL[I].IP + ':' + IntToStr(SL[I].Port);
428 if SL[I].Password then
429 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
430 else
431 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
432 end else
433 if Length(SL) > 0 then
434 slSelection := 0;
436 mw := (gScreenWidth - 188);
437 mx := 16 + mw;
439 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
440 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
441 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
443 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
444 e_DrawLine(1, 16, gScreenHeight-64, gScreenWidth-16, gScreenHeight-64, 255, 127, 0);
446 e_DrawLine(1, mx - 70, 64, mx - 70, gScreenHeight-44, 255, 127, 0);
447 e_DrawLine(1, mx, 64, mx, gScreenHeight-64, 255, 127, 0);
448 e_DrawLine(1, mx + 52, 64, mx + 52, gScreenHeight-64, 255, 127, 0);
449 e_DrawLine(1, mx + 104, 64, mx + 104, gScreenHeight-64, 255, 127, 0);
451 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
453 y := 90;
454 for I := 0 to High(SL) do
455 begin
456 e_TextureFontPrintEx(18, y, SL[I].Name, gStdFont, 255, 255, 255, 1);
457 e_TextureFontPrintEx(18, y + 16, SL[I].Map, gStdFont, 210, 210, 210, 1);
459 y := y + 42;
460 end;
462 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
463 y := 90;
464 for I := 0 to High(SL) do
465 begin
466 if (SL[I].Ping < 0) or (SL[I].Ping > 999) then
467 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
468 else
469 if SL[I].Ping = 0 then
470 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
471 else
472 e_TextureFontPrintEx(mx - 68, y, IntToStr(SL[I].Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
474 y := y + 42;
475 end;
477 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
478 y := 90;
479 for I := 0 to High(SL) do
480 begin
481 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(SL[I].GameMode), gStdFont, 255, 255, 255, 1);
483 y := y + 42;
484 end;
486 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
487 y := 90;
488 for I := 0 to High(SL) do
489 begin
490 e_TextureFontPrintEx(mx + 54, y, IntToStr(SL[I].Players) + '/' + IntToStr(SL[I].MaxPlayers), gStdFont, 255, 255, 255, 1);
491 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(SL[I].LocalPl) + '+' + IntToStr(SL[I].Bots), gStdFont, 210, 210, 210, 1);
492 y := y + 42;
493 end;
495 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
496 y := 90;
497 for I := 0 to High(SL) do
498 begin
499 e_TextureFontPrintEx(mx + 106, y, IntToStr(SL[I].Protocol), gStdFont, 255, 255, 255, 1);
501 y := y + 42;
502 end;
504 e_TextureFontPrintEx(20, gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
505 ip := IntToStr(Length(SL)) + _lc[I_NET_SLIST_SERVERS];
506 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
507 gScreenHeight-61, ip, gStdFont, 205, 205, 205, 1);
508 end;
510 procedure g_Serverlist_Control(var SL: TNetServerList);
511 begin
512 if gConsoleShow or gChatShow then
513 Exit;
515 e_PollInput();
517 if e_KeyPressed(IK_ESCAPE) then
518 begin
519 SL := nil;
520 gState := STATE_MENU;
521 g_GUI_ShowWindow('MainMenu');
522 g_GUI_ShowWindow('NetGameMenu');
523 g_GUI_ShowWindow('NetClientMenu');
524 g_Sound_PlayEx(WINDOW_CLOSESOUND);
525 Exit;
526 end;
528 if e_KeyPressed(IK_SPACE) then
529 begin
530 if not slFetched then
531 begin
532 slWaitStr := _lc[I_NET_SLIST_WAIT];
534 g_Game_Draw;
535 g_window.ReDrawWindow;
537 if g_Net_Slist_Fetch(SL) then
538 begin
539 if SL = nil then
540 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
541 end
542 else
543 slWaitStr := _lc[I_NET_SLIST_ERROR];
544 slFetched := True;
545 slSelection := 0;
546 end;
547 end
548 else
549 slFetched := False;
551 if SL = nil then Exit;
553 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) then
554 begin
555 if not slReturnPressed then
556 begin
557 if SL[slSelection].Password then
558 begin
559 PromptIP := SL[slSelection].IP;
560 PromptPort := SL[slSelection].Port;
561 gState := STATE_MENU;
562 g_GUI_ShowWindow('ClientPasswordMenu');
563 SL := nil;
564 slReturnPressed := True;
565 Exit;
566 end
567 else
568 g_Game_StartClient(SL[slSelection].IP, SL[slSelection].Port, '');
569 SL := nil;
570 slReturnPressed := True;
571 Exit;
572 end;
573 end
574 else
575 slReturnPressed := False;
577 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) then
578 begin
579 if not slDirPressed then
580 begin
581 Inc(slSelection);
582 if slSelection > High(SL) then slSelection := 0;
583 slDirPressed := True;
584 end;
585 end;
587 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) then
588 begin
589 if not slDirPressed then
590 begin
591 if slSelection = 0 then slSelection := Length(SL);
592 Dec(slSelection);
594 slDirPressed := True;
595 end;
596 end;
598 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
599 slDirPressed := False;
600 end;
602 end.