DEADSOFTWARE

net: started master-server communication rewrite (phase 1: master i/o moved to separa...
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_netmaster;
18 interface
20 uses
21 ENet, SysUtils, e_msg;
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;
48 TNetServerRow = record
49 Indices: Array of Integer;
50 Current: Integer;
51 end;
53 TNetServerList = array of TNetServer;
54 pTNetServerList = ^TNetServerList;
55 TNetServerTable = array of TNetServerRow;
57 type
58 TMasterHost = record
59 public
60 hostName: AnsiString;
61 hostPort: Word;
63 public
64 //host: pENetHost;
65 peer: pENetPeer;
66 event: ENetEvent;
67 enetAddr: ENetAddress;
68 // inside the game, calling `connect()` is disasterous, as it is blocking.
69 // so we'll use this variable to indicate if "connected" event is received.
70 NetHostConnected: Boolean;
71 NetHostConReqTime: Int64; // to timeout `connect`
72 NetUpdatePending: Boolean; // should we send an update after connection completes?
73 updateSent: Boolean;
74 lastUpdateTime: Int64;
75 addressInited: Boolean;
77 private
78 netmsg: TMsg;
80 private
81 function processPendingConnection (timeout: Integer=0): Boolean;
83 public
84 constructor Create (hostandport: AnsiString);
86 procedure clear ();
88 function setAddress (hostandport: AnsiString): Boolean;
90 function isValid (): Boolean;
91 function isAlive (): Boolean; // not disconnected
92 function isConnecting (): Boolean; // is connection in progress?
93 function isConnected (): Boolean;
95 // returns `false` if connection failed
96 function waitForConnection (): Boolean;
98 // call as often as you want, the object will do the rest
99 // but try to call this at least once in 100 msecs
100 // returns `true` if we got a packet (it won't be parsed to TMsg)
101 function service (timeout: Integer=0): Boolean;
103 procedure disconnect (spamConsole: Boolean=false);
104 function connect (): Boolean;
106 procedure update (immediateSend: Boolean=true);
107 procedure remove ();
109 class procedure writeInfo (var msg: TMsg); static;
111 // call only if `service()` returned `true`!
112 procedure clearPacket ();
113 end;
116 var
117 slCurrent: TNetServerList = nil;
118 slTable: TNetServerTable = nil;
119 slWaitStr: string = '';
120 slReturnPressed: Boolean = True;
122 slMOTD: string = '';
123 slUrgent: string = '';
125 procedure g_Net_Slist_Set (IP: string; Port: Word);
126 function g_Net_Slist_Fetch (var SL: TNetServerList): Boolean;
129 procedure g_Net_Slist_Update (immediateSend: Boolean=true);
130 procedure g_Net_Slist_Remove ();
131 function g_Net_Slist_Connect (blocking: Boolean=True): Boolean;
132 procedure g_Net_Slist_Check ();
133 procedure g_Net_Slist_Disconnect (spamConsole: Boolean=true);
134 procedure g_Net_Slist_WriteInfo ();
136 function g_Net_Slist_IsConnectionActive (): Boolean; // returns `false` if totally disconnected
137 function g_Net_Slist_IsConnectionInProgress (): Boolean;
140 // make this server private
141 procedure g_Net_Slist_Private ();
143 // called on network mode init
144 procedure g_Net_Slist_NetworkStarted ();
145 // called on network mode shutdown
146 procedure g_Net_Slist_NetworkStopped ();
148 procedure g_Net_Slist_Pulse ();
150 procedure g_Serverlist_GenerateTable (SL: TNetServerList; var ST: TNetServerTable);
151 procedure g_Serverlist_Draw (var SL: TNetServerList; var ST: TNetServerTable);
152 procedure g_Serverlist_Control (var SL: TNetServerList; var ST: TNetServerTable);
154 function GetTimerMS (): Int64;
157 implementation
159 uses
160 e_input, e_graphics, e_log, g_window, g_net, g_console,
161 g_map, g_game, g_sound, g_gui, g_menu, g_options, g_language, g_basic,
162 wadreader, g_system, utils;
164 // make this server private
165 procedure g_Net_Slist_Private ();
166 begin
167 end;
170 // called on network mode init
171 procedure g_Net_Slist_NetworkStarted ();
172 begin
173 end;
175 // called on network mode shutdown
176 procedure g_Net_Slist_NetworkStopped ();
177 begin
178 end;
181 var
182 NetMHost: pENetHost = nil;
183 mlist: array of TMasterHost = nil;
185 slSelection: Byte = 0;
186 slFetched: Boolean = False;
187 slDirPressed: Boolean = False;
188 slReadUrgent: Boolean = False;
190 NetMHost: pENetHost = nil;
191 NetMPeer: pENetPeer = nil;
192 NetMEvent: ENetEvent;
193 // inside the game, calling `g_Net_Slist_Connect()` is disasterous, as it is blocking.
194 // so we'll use this variable to indicate if "connected" event is received.
195 NetHostConnected: Boolean = false;
196 NetHostConReqTime: Int64 = 0; // to timeout `connect`
197 NetUpdatePending: Boolean = false;
201 //==========================================================================
202 //
203 // GetTimerMS
204 //
205 //==========================================================================
206 function GetTimerMS (): Int64;
207 begin
208 Result := sys_GetTicks() {div 1000};
209 end;
212 //==========================================================================
213 //
214 // TMasterHost.Create
215 //
216 //==========================================================================
217 constructor TMasterHost.Create (hostandport: AnsiString);
218 begin
219 //host := nil;
220 peer := nil;
221 ZeroMemory(@event, sizeof(event));
222 NetHostConnected := false;
223 NetHostConReqTime := 0;
224 NetUpdatePending := false;
225 updateSent := false;
226 hostName := '';
227 hostPort := 25665;
228 netmsg.Alloc(NET_BUFSIZE);
229 setAddress(hostandport);
230 end;
233 //==========================================================================
234 //
235 // TMasterHost.clear
236 //
237 //==========================================================================
238 procedure TMasterHost.clear ();
239 begin
240 updateSent := false; // do not send 'remove'
241 disconnect();
242 hostName := '';
243 hostPort := 25665;
244 netmsg.Free();
245 end;
248 //==========================================================================
249 //
250 // TMasterHost.setAddress
251 //
252 //==========================================================================
253 function TMasterHost.setAddress (hostandport: AnsiString): Boolean;
254 var
255 cp, pp: Integer;
256 begin
257 result := false;
258 updateSent := false; // do not send 'remove'
259 disconnect();
260 addressInited := false;
261 hostName := '';
262 hostPort := 25665;
263 hostandport := Trim(hostandport);
264 if (length(hostandport) > 0) then
265 begin
266 hostName := hostandport;
267 cp := Pos(':', hostandport);
268 if (cp > 0) then
269 begin
270 hostName := Copy(hostandport, 1, cp-1);
271 Delete(hostandport, 1, cp);
272 if (length(hostandport) > 0) then
273 begin
274 try
275 pp := StrToInt(hostandport);
276 except
277 pp := -1;
278 end;
279 if (pp > 0) and (pp < 65536) then hostPort := pp else hostPort := 0;
280 end;
281 end;
282 end;
284 if not isValid() then exit;
285 if (NetInitDone) then
286 begin
287 if (enet_address_set_host(@enetAddr, PChar(Addr(hostName[1]))) <> 0) then
288 begin
289 hostName := '';
290 hostPort := 0;
291 end;
292 enetAddr.Port := hostPort;
293 end;
295 result := isValid();
296 end;
299 //==========================================================================
300 //
301 // TMasterHost.isValid
302 //
303 //==========================================================================
304 function TMasterHost.isValid (): Boolean;
305 begin
306 result := (length(hostName) > 0) and (hostPort > 0);
307 end;
310 //==========================================================================
311 //
312 // TMasterHost.isAlive
313 //
314 // not disconnected
315 //
316 //==========================================================================
317 function TMasterHost.isAlive (): Boolean;
318 begin
319 result := (NetMHost <> nil) and (peer <> nil);
320 end;
323 //==========================================================================
324 //
325 // TMasterHost.isConnecting
326 //
327 // is connection in progress?
328 //
329 //==========================================================================
330 function TMasterHost.isConnecting (): Boolean;
331 begin
332 result := isAlive() and (not NetHostConnected);
333 end;
336 //==========================================================================
337 //
338 // TMasterHost.isConnected
339 //
340 //==========================================================================
341 function TMasterHost.isConnected (): Boolean;
342 begin
343 result := isAlive() and (NetHostConnected);
344 end;
347 //==========================================================================
348 //
349 // TMasterHost.disconnect
350 //
351 //==========================================================================
352 procedure TMasterHost.disconnect (spamConsole: Boolean=false);
353 begin
354 if not isAlive() then exit;
355 if (NetMode = NET_SERVER) and isConnected() and updateSent then remove();
357 enet_peer_disconnect(peer, 0);
358 enet_host_flush(NetMHost);
360 enet_peer_reset(peer);
361 //enet_host_destroy(NetMHost);
363 peer := nil;
364 //NetMHost := nil;
365 NetHostConnected := False;
366 NetHostConReqTime := 0;
367 NetUpdatePending := false;
368 updateSent := false;
370 if (spamConsole) then g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_DISC]);
371 end;
374 //==========================================================================
375 //
376 // TMasterHost.connect
377 //
378 //==========================================================================
379 function TMasterHost.connect (): Boolean;
380 var
381 res: Integer;
382 begin
383 updateSent := false; // do not send 'remove'
384 disconnect();
385 result := false;
386 if not isValid() then exit;
388 if (not NetInitDone) then exit;
390 if (NetMHost = nil) then
391 begin
392 NetMHost := enet_host_create(nil, 1, NET_MCHANS, 0, 0);
393 if (NetMHost = nil) then
394 begin
395 g_Console_Add(_lc[I_NET_MSG_ERROR]+_lc[I_NET_ERR_CLIENT], True);
396 Exit;
397 end;
398 end;
400 if (not addressInited) then
401 begin
402 if (enet_address_set_host(@enetAddr, PChar(Addr(hostName[1]))) <> 0) then
403 begin
404 hostName := '';
405 hostPort := 0;
406 exit;
407 end;
408 enetAddr.Port := hostPort;
409 addressInited := true;
410 end;
412 peer := enet_host_connect(NetMHost, @enetAddr, NET_MCHANS, 0);
413 if (peer = nil) then
414 begin
415 g_Console_Add(_lc[I_NET_MSG_ERROR]+_lc[I_NET_ERR_CLIENT], true);
416 //enet_host_destroy(NetMHost);
417 //NetMHost := nil;
418 exit;
419 end;
421 res := enet_host_service(NetMHost, @event, 0);
422 if (res < 0) then
423 begin
424 enet_peer_reset(peer);
425 peer := nil;
426 exit;
427 end;
429 result := true;
430 if (res > 0) then
431 begin
432 if (event.kind = ENET_EVENT_TYPE_CONNECT) then
433 begin
434 NetHostConnected := true;
435 g_Console_Add(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN]);
436 exit;
437 end;
438 if (event.kind = ENET_EVENT_TYPE_RECEIVE) then enet_packet_destroy(event.packet);
439 end;
441 if not NetHostConnected then NetHostConReqTime := GetTimerMS();
444 if (blocking) then
445 begin
446 g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
448 if NetMPeer <> nil then enet_peer_reset(NetMPeer);
449 if NetMHost <> nil then enet_host_destroy(NetMHost);
450 NetMPeer := nil;
451 NetMHost := nil;
452 NetHostConnected := False;
453 NetHostConReqTime := 0;
454 NetUpdatePending := false;
455 end
456 else
457 begin
458 NetHostConReqTime := GetTimerMS();
459 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_WCONN]);
460 end;
462 end;
465 //==========================================================================
466 //
467 // TMasterHost.processPendingConnection
468 //
469 // should be called only if host/peer is here
470 // returns `false` if not connected or dead
471 //
472 //==========================================================================
473 function TMasterHost.processPendingConnection (timeout: Integer=0): Boolean;
474 var
475 ct: Int64;
476 cres: Integer;
477 begin
478 result := false;
479 if not isAlive() then exit;
480 // are we waiting for connection?
481 if (not NetHostConnected) then
482 begin
483 // check for connection event
484 cres := enet_host_service(NetMHost, @event, timeout);
485 if (cres < 0) then
486 begin
487 //TODO: reconnect here
488 updateSent := false; // do not send 'remove'
489 disconnect();
490 exit;
491 end;
492 if (cres > 0) then
493 begin
494 if (event.kind = ENET_EVENT_TYPE_CONNECT) then
495 begin
496 NetHostConnected := true;
497 if NetUpdatePending then update(false);
498 g_Console_Add(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN]);
499 result := true;
500 exit;
501 end
502 else if (event.kind = ENET_EVENT_TYPE_DISCONNECT) then
503 begin
504 //TODO: reconnect here
505 updateSent := false; // do not send 'remove'
506 disconnect();
507 exit;
508 end
509 else if (event.kind = ENET_EVENT_TYPE_RECEIVE) then
510 begin
511 enet_packet_destroy(event.packet);
512 end;
513 end;
514 // check for connection timeout
515 if (not NetHostConnected) then
516 begin
517 ct := GetTimerMS();
518 if (ct < NetHostConReqTime) or (ct-NetHostConReqTime >= 3000) then
519 begin
520 // do not spam with error messages, it looks like the master is down
521 //g_Console_Add(_lc[I_NET_MSG_ERROR] + _lc[I_NET_SLIST_ERROR], True);
522 disconnect(false);
523 end;
524 exit;
525 end;
526 end;
527 result := true;
528 end;
531 //==========================================================================
532 //
533 // TMasterHost.writeInfo
534 //
535 //==========================================================================
536 class procedure TMasterHost.writeInfo (var msg: TMsg);
537 var
538 wad, map: string;
539 begin
540 wad := g_ExtractWadNameNoPath(gMapInfo.Map);
541 map := g_ExtractFileName(gMapInfo.Map);
543 msg.Write(NetServerName);
545 msg.Write(wad+':/'+map);
546 msg.Write(gGameSettings.GameMode);
548 msg.Write(Byte(NetClientCount));
550 msg.Write(NetMaxClients);
552 msg.Write(Byte(NET_PROTOCOL_VER));
553 msg.Write(Byte(NetPassword <> ''));
554 end;
557 //==========================================================================
558 //
559 // TMasterHost.update
560 //
561 //==========================================================================
562 procedure TMasterHost.update (immediateSend: Boolean=true);
563 var
564 pkt: pENetPacket;
565 begin
566 if not processPendingConnection() then
567 begin
568 NetUpdatePending := isConnecting();
569 exit;
570 end;
572 NetUpdatePending := false;
574 netmsg.Clear();
575 netmsg.Write(Byte(NET_MMSG_UPD));
576 netmsg.Write(NetAddr.port);
578 writeInfo(netmsg);
580 pkt := enet_packet_create(netmsg.Data, netmsg.CurSize, ENET_PACKET_FLAG_RELIABLE);
581 if assigned(pkt) then
582 begin
583 enet_peer_send(peer, NET_MCHAN_UPD, pkt);
584 if (immediateSend) then enet_host_flush(NetMHost);
585 end;
587 netmsg.Clear();
588 end;
591 //==========================================================================
592 //
593 // TMasterHost.remove
594 //
595 //==========================================================================
596 procedure TMasterHost.remove ();
597 var
598 pkt: pENetPacket;
599 begin
600 if not processPendingConnection() then exit;
602 netmsg.Clear();
603 netmsg.Write(Byte(NET_MMSG_DEL));
604 netmsg.Write(NetAddr.port);
606 pkt := enet_packet_create(netmsg.Data, netmsg.CurSize, ENET_PACKET_FLAG_RELIABLE);
607 if assigned(pkt) then
608 begin
609 enet_peer_send(peer, NET_MCHAN_MAIN, pkt);
610 enet_host_flush(NetMHost);
611 end;
613 netmsg.Clear();
614 end;
617 //==========================================================================
618 //
619 // TMasterHost.waitForConnection
620 //
621 // returns `false` if connection failed
622 //
623 //==========================================================================
624 function TMasterHost.waitForConnection (): Boolean;
625 begin
626 result := isAlive();
627 if not result then exit;
628 while isAlive() and isConnecting() do
629 begin
630 if not processPendingConnection(300) then break;
631 end;
632 if not isConnected() then
633 begin
634 updateSent := false; // do not send 'remove'
635 disconnect();
636 end;
637 result := isAlive();
638 end;
641 //==========================================================================
642 //
643 // TMasterHost.service
644 //
645 // call as often as you want, the object will do the rest
646 // but try to call this at least once in 100 msecs
647 //
648 // returns `true` if we got a packet (it won't be parsed to TMsg)
649 //
650 //==========================================================================
651 function TMasterHost.service (timeout: Integer=0): Boolean;
652 var
653 ct: Int64;
654 hres: Integer;
655 begin
656 result := false;
657 if not isAlive() then exit;
658 if not processPendingConnection() then exit;
660 ct := GetTimerMS();
661 if (ct < lastUpdateTime) or (ct-lastUpdateTime >= 1000*60) then
662 begin
663 lastUpdateTime := ct;
664 update(false);
665 end;
667 while true do
668 begin
669 hres := enet_host_service(NetMHost, @event, timeout);
670 if (hres < 0) then
671 begin
672 //TODO: reconnect here
673 updateSent := false; // do not send 'remove'
674 disconnect();
675 exit;
676 end;
677 if (hres = 0) then break;
678 if (event.kind = ENET_EVENT_TYPE_CONNECT) then
679 begin
680 NetHostConnected := true;
681 if NetUpdatePending then update(false);
682 g_Console_Add(_lc[I_NET_MSG]+_lc[I_NET_SLIST_CONN]);
683 end
684 else if (event.kind = ENET_EVENT_TYPE_DISCONNECT) then
685 begin
686 //TODO: reconnect here
687 g_Console_Add(_lc[I_NET_MSG]+_lc[I_NET_SLIST_LOST], True);
688 updateSent := false; // do not send 'remove'
689 disconnect();
690 exit;
691 end
692 else if (event.kind = ENET_EVENT_TYPE_RECEIVE) then
693 begin
694 //enet_packet_destroy(event.packet);
695 //if (timeout <> 0) then break;
696 result := true;
697 exit;
698 end;
699 end;
700 end;
703 //==========================================================================
704 //
705 // TMasterHost.clearPacket
706 //
707 //==========================================================================
708 procedure TMasterHost.clearPacket ();
709 begin
710 if (event.packet <> nil) then
711 begin
712 enet_packet_destroy(event.packet);
713 event.packet := nil;
714 end;
715 end;
718 //**************************************************************************
719 //
720 // other functions
721 //
722 //**************************************************************************
724 procedure g_Net_Slist_Set (IP: string; Port: Word);
725 begin
726 if (length(mlist) = 0) then
727 begin
728 SetLength(mlist, 1);
729 mlist[0].Create(ip+':'+IntToStr(Port));
730 end
731 else
732 begin
733 mlist[0].setAddress(ip+':'+IntToStr(Port));
734 end;
735 e_LogWritefln('Masterserver address set to %s:%u', [IP, Port], TMsgType.Notify);
737 if NetInitDone then
738 begin
739 enet_address_set_host(@NetSlistAddr, PChar(Addr(IP[1])));
740 NetSlistAddr.Port := Port;
741 e_WriteLog('Masterserver address set to ' + IP + ':' + IntToStr(Port), TMsgType.Notify);
742 end;
744 end;
747 //**************************************************************************
748 //
749 // main pulse
750 //
751 //**************************************************************************
752 procedure g_Net_Slist_Pulse ();
753 begin
754 end;
757 //**************************************************************************
758 //
759 // gui and server list
760 //
761 //**************************************************************************
763 //==========================================================================
764 //
765 // PingServer
766 //
767 //==========================================================================
768 procedure PingServer (var S: TNetServer; Sock: ENetSocket);
769 var
770 Buf: ENetBuffer;
771 Ping: array [0..9] of Byte;
772 ClTime: Int64;
773 begin
774 ClTime := GetTimerMS();
776 Buf.data := Addr(Ping[0]);
777 Buf.dataLength := 2+8;
779 Ping[0] := Ord('D');
780 Ping[1] := Ord('F');
781 Int64(Addr(Ping[2])^) := ClTime;
783 enet_socket_send(Sock, Addr(S.PingAddr), @Buf, 1);
784 end;
787 //==========================================================================
788 //
789 // PingBcast
790 //
791 //==========================================================================
792 procedure PingBcast (Sock: ENetSocket);
793 var
794 S: TNetServer;
795 begin
796 S.IP := '255.255.255.255';
797 S.Port := NET_PING_PORT;
798 enet_address_set_host(Addr(S.PingAddr), PChar(Addr(S.IP[1])));
799 S.Ping := -1;
800 S.PingAddr.port := S.Port;
801 PingServer(S, Sock);
802 end;
805 //==========================================================================
806 //
807 // g_Net_Slist_Fetch
808 //
809 //==========================================================================
810 function g_Net_Slist_Fetch (var SL: TNetServerList): Boolean;
811 var
812 Cnt: Byte;
813 P: pENetPacket;
814 MID: Byte;
815 I, RX: Integer;
816 T: Int64;
817 Sock: ENetSocket;
818 Buf: ENetBuffer;
819 InMsg: TMsg;
820 SvAddr: ENetAddress;
821 FromSL: Boolean;
822 MyVer, Str: string;
824 procedure ProcessLocal ();
825 begin
826 I := Length(SL);
827 SetLength(SL, I + 1);
828 with SL[I] do
829 begin
830 IP := DecodeIPV4(SvAddr.host);
831 Port := InMsg.ReadWord();
832 Ping := InMsg.ReadInt64();
833 Ping := GetTimerMS() - Ping;
834 Name := InMsg.ReadString();
835 Map := InMsg.ReadString();
836 GameMode := InMsg.ReadByte();
837 Players := InMsg.ReadByte();
838 MaxPlayers := InMsg.ReadByte();
839 Protocol := InMsg.ReadByte();
840 Password := InMsg.ReadByte() = 1;
841 LocalPl := InMsg.ReadByte();
842 Bots := InMsg.ReadWord();
843 end;
844 end;
846 procedure CheckLocalServers ();
847 begin
848 SetLength(SL, 0);
850 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
851 if Sock = ENET_SOCKET_NULL then Exit;
852 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
853 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
854 PingBcast(Sock);
856 T := GetTimerMS();
858 InMsg.Alloc(NET_BUFSIZE);
859 Buf.data := InMsg.Data;
860 Buf.dataLength := InMsg.MaxSize;
861 while GetTimerMS() - T <= 500 do
862 begin
863 InMsg.Clear();
865 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
866 if RX <= 0 then continue;
867 InMsg.CurSize := RX;
869 InMsg.BeginReading();
871 if InMsg.ReadChar() <> 'D' then continue;
872 if InMsg.ReadChar() <> 'F' then continue;
874 ProcessLocal();
875 end;
877 InMsg.Free();
878 enet_socket_destroy(Sock);
880 if Length(SL) = 0 then SL := nil;
881 end;
883 begin
884 Result := False;
885 SL := nil;
887 if (length(mlist) > 0) and (mlist[0].isAlive()) then
888 begin
889 CheckLocalServers();
890 Exit;
891 end;
893 if (length(mlist) = 0) or (not mlist[0].connect()) then
894 begin
895 CheckLocalServers();
896 Exit;
897 end;
899 if not mlist[0].waitForConnection() then
900 begin
901 CheckLocalServers();
902 Exit;
903 end;
905 e_WriteLog('Fetching serverlist...', TMsgType.Notify);
906 g_Console_Add(_lc[I_NET_MSG] + _lc[I_NET_SLIST_FETCH]);
908 NetOut.Clear();
909 NetOut.Write(Byte(NET_MMSG_GET));
911 // TODO: what should we identify the build with?
912 MyVer := GAME_VERSION;
913 NetOut.Write(MyVer);
915 P := enet_packet_create(NetOut.Data, NetOut.CurSize, Cardinal(ENET_PACKET_FLAG_RELIABLE));
916 enet_peer_send(mlist[0].peer, NET_MCHAN_MAIN, P);
917 enet_host_flush(NetMHost);
919 while mlist[0].isAlive() do
920 begin
921 if not mlist[0].service(5000) then continue;
922 if not InMsg.Init(mlist[0].event.packet^.data, mlist[0].event.packet^.dataLength, True) then
923 begin
924 mlist[0].clearPacket();
925 continue;
926 end;
928 MID := InMsg.ReadByte();
930 if (MID <> NET_MMSG_GET) then
931 begin
932 mlist[0].clearPacket();
933 continue;
934 end;
936 Cnt := InMsg.ReadByte();
937 g_Console_Add(_lc[I_NET_MSG]+Format(_lc[I_NET_SLIST_RETRIEVED], [Cnt]), True);
939 if (Cnt > 0) then
940 begin
941 SetLength(SL, Cnt);
943 for I := 0 to Cnt-1 do
944 begin
945 SL[I].Number := I;
946 SL[I].IP := InMsg.ReadString();
947 SL[I].Port := InMsg.ReadWord();
948 SL[I].Name := InMsg.ReadString();
949 SL[I].Map := InMsg.ReadString();
950 SL[I].GameMode := InMsg.ReadByte();
951 SL[I].Players := InMsg.ReadByte();
952 SL[I].MaxPlayers := InMsg.ReadByte();
953 SL[I].Protocol := InMsg.ReadByte();
954 SL[I].Password := InMsg.ReadByte() = 1;
955 enet_address_set_host(Addr(SL[I].PingAddr), PChar(Addr(SL[I].IP[1])));
956 SL[I].Ping := -1;
957 SL[I].PingAddr.port := NET_PING_PORT;
958 end;
959 end;
961 if InMsg.ReadCount < InMsg.CurSize then
962 begin
963 // new master, supports version reports
964 Str := InMsg.ReadString();
965 if (Str <> MyVer) then
966 begin
967 { TODO }
968 g_Console_Add('!!! UpdVer = `' + Str + '`');
969 end;
970 // even newer master, supports extra info
971 if (InMsg.ReadCount < InMsg.CurSize) then
972 begin
973 slMOTD := b_Text_Format(InMsg.ReadString());
974 Str := b_Text_Format(InMsg.ReadString());
975 // check if the message has updated and the user has to read it again
976 if slUrgent <> Str then slReadUrgent := False;
977 slUrgent := Str;
978 end;
979 end;
981 mlist[0].clearPacket();
982 Result := True;
983 break;
984 end;
986 mlist[0].disconnect(false);
987 NetOut.Clear();
989 if Length(SL) = 0 then
990 begin
991 CheckLocalServers();
992 Exit;
993 end;
995 Sock := enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
996 if Sock = ENET_SOCKET_NULL then Exit;
997 enet_socket_set_option(Sock, ENET_SOCKOPT_NONBLOCK, 1);
999 for I := Low(SL) to High(SL) do PingServer(SL[I], Sock);
1001 enet_socket_set_option(Sock, ENET_SOCKOPT_BROADCAST, 1);
1002 PingBcast(Sock);
1004 T := GetTimerMS();
1006 InMsg.Alloc(NET_BUFSIZE);
1007 Buf.data := InMsg.Data;
1008 Buf.dataLength := InMsg.MaxSize;
1009 Cnt := 0;
1010 while GetTimerMS() - T <= 500 do
1011 begin
1012 InMsg.Clear();
1014 RX := enet_socket_receive(Sock, @SvAddr, @Buf, 1);
1015 if RX <= 0 then continue;
1016 InMsg.CurSize := RX;
1018 InMsg.BeginReading();
1020 if InMsg.ReadChar() <> 'D' then continue;
1021 if InMsg.ReadChar() <> 'F' then continue;
1023 FromSL := False;
1024 for I := Low(SL) to High(SL) do
1025 if (SL[I].PingAddr.host = SvAddr.host) and
1026 (SL[I].PingAddr.port = SvAddr.port) then
1027 begin
1028 with SL[I] do
1029 begin
1030 Port := InMsg.ReadWord();
1031 Ping := InMsg.ReadInt64();
1032 Ping := GetTimerMS() - Ping;
1033 Name := InMsg.ReadString();
1034 Map := InMsg.ReadString();
1035 GameMode := InMsg.ReadByte();
1036 Players := InMsg.ReadByte();
1037 MaxPlayers := InMsg.ReadByte();
1038 Protocol := InMsg.ReadByte();
1039 Password := InMsg.ReadByte() = 1;
1040 LocalPl := InMsg.ReadByte();
1041 Bots := InMsg.ReadWord();
1042 end;
1043 FromSL := True;
1044 Inc(Cnt);
1045 break;
1046 end;
1047 if not FromSL then
1048 ProcessLocal();
1049 end;
1051 InMsg.Free();
1052 enet_socket_destroy(Sock);
1053 end;
1056 //==========================================================================
1057 //
1058 // GetServerFromTable
1059 //
1060 //==========================================================================
1061 function GetServerFromTable (Index: Integer; SL: TNetServerList; ST: TNetServerTable): TNetServer;
1062 begin
1063 Result.Number := 0;
1064 Result.Protocol := 0;
1065 Result.Name := '';
1066 Result.IP := '';
1067 Result.Port := 0;
1068 Result.Map := '';
1069 Result.Players := 0;
1070 Result.MaxPlayers := 0;
1071 Result.LocalPl := 0;
1072 Result.Bots := 0;
1073 Result.Ping := 0;
1074 Result.GameMode := 0;
1075 Result.Password := false;
1076 FillChar(Result.PingAddr, SizeOf(ENetAddress), 0);
1077 if ST = nil then
1078 Exit;
1079 if (Index < 0) or (Index >= Length(ST)) then
1080 Exit;
1081 Result := SL[ST[Index].Indices[ST[Index].Current]];
1082 end;
1085 //==========================================================================
1086 //
1087 // g_Serverlist_Draw
1088 //
1089 //==========================================================================
1090 procedure g_Serverlist_Draw (var SL: TNetServerList; var ST: TNetServerTable);
1091 var
1092 Srv: TNetServer;
1093 sy, i, y, mw, mx, l, motdh: Integer;
1094 cw: Byte = 0;
1095 ch: Byte = 0;
1096 ww: Word = 0;
1097 hh: Word = 0;
1098 ip: string;
1099 begin
1100 ip := '';
1101 sy := 0;
1103 e_CharFont_GetSize(gMenuFont, _lc[I_NET_SLIST], ww, hh);
1104 e_CharFont_Print(gMenuFont, (gScreenWidth div 2) - (ww div 2), 16, _lc[I_NET_SLIST]);
1106 e_TextureFontGetSize(gStdFont, cw, ch);
1108 ip := _lc[I_NET_SLIST_HELP];
1109 mw := (Length(ip) * cw) div 2;
1111 motdh := gScreenHeight - 49 - ch * b_Text_LineCount(slMOTD);
1113 e_DrawFillQuad(16, 64, gScreenWidth-16, motdh, 64, 64, 64, 110);
1114 e_DrawQuad(16, 64, gScreenWidth-16, motdh, 255, 127, 0);
1116 e_TextureFontPrintEx(gScreenWidth div 2 - mw, gScreenHeight-24, ip, gStdFont, 225, 225, 225, 1);
1118 // MOTD
1119 if slMOTD <> '' then
1120 begin
1121 e_DrawFillQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 64, 64, 64, 110);
1122 e_DrawQuad(16, motdh, gScreenWidth-16, gScreenHeight-44, 255, 127, 0);
1123 e_TextureFontPrintFmt(20, motdh + 3, slMOTD, gStdFont, False, True);
1124 end;
1126 // Urgent message
1127 if not slReadUrgent and (slUrgent <> '') then
1128 begin
1129 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
1130 e_DrawFillQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
1131 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 64, 64, 64, 128);
1132 e_DrawQuad(gScreenWidth div 2 - 256, gScreenHeight div 2 - 60,
1133 gScreenWidth div 2 + 256, gScreenHeight div 2 + 60, 255, 127, 0);
1134 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 - 40,
1135 gScreenWidth div 2 + 256, gScreenHeight div 2 - 40, 255, 127, 0);
1136 l := Length(_lc[I_NET_SLIST_URGENT]) div 2;
1137 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - 58,
1138 _lc[I_NET_SLIST_URGENT], gStdFont);
1139 l := Length(slUrgent) div 2;
1140 e_TextureFontPrintFmt(gScreenWidth div 2 - 253, gScreenHeight div 2 - 38,
1141 slUrgent, gStdFont, False, True);
1142 l := Length(_lc[I_NET_SLIST_URGENT_CONT]) div 2;
1143 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 + 41,
1144 _lc[I_NET_SLIST_URGENT_CONT], gStdFont);
1145 e_DrawLine(1, gScreenWidth div 2 - 256, gScreenHeight div 2 + 40,
1146 gScreenWidth div 2 + 256, gScreenHeight div 2 + 40, 255, 127, 0);
1147 Exit;
1148 end;
1150 if SL = nil then
1151 begin
1152 l := Length(slWaitStr) div 2;
1153 e_DrawFillQuad(17, 65, gScreenWidth-17, motdh-1, 64, 64, 64, 128);
1154 e_DrawQuad(gScreenWidth div 2 - 192, gScreenHeight div 2 - 10,
1155 gScreenWidth div 2 + 192, gScreenHeight div 2 + 11, 255, 127, 0);
1156 e_TextureFontPrint(gScreenWidth div 2 - cw * l, gScreenHeight div 2 - ch div 2,
1157 slWaitStr, gStdFont);
1158 Exit;
1159 end;
1161 y := 90;
1162 if (slSelection < Length(ST)) then
1163 begin
1164 I := slSelection;
1165 sy := y + 42 * I - 4;
1166 Srv := GetServerFromTable(I, SL, ST);
1167 ip := _lc[I_NET_ADDRESS] + ' ' + Srv.IP + ':' + IntToStr(Srv.Port);
1168 if Srv.Password then
1169 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_YES]
1170 else
1171 ip := ip + ' ' + _lc[I_NET_SERVER_PASSWORD] + ' ' + _lc[I_MENU_NO];
1172 end else
1173 if Length(ST) > 0 then
1174 slSelection := 0;
1176 mw := (gScreenWidth - 188);
1177 mx := 16 + mw;
1179 e_DrawFillQuad(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 0);
1180 e_DrawLine(1, 16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205);
1181 e_DrawLine(1, 16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255);
1183 e_DrawLine(1, 16, 85, gScreenWidth - 16, 85, 255, 127, 0);
1184 e_DrawLine(1, 16, motdh-20, gScreenWidth-16, motdh-20, 255, 127, 0);
1186 e_DrawLine(1, mx - 70, 64, mx - 70, motdh, 255, 127, 0);
1187 e_DrawLine(1, mx, 64, mx, motdh-20, 255, 127, 0);
1188 e_DrawLine(1, mx + 52, 64, mx + 52, motdh-20, 255, 127, 0);
1189 e_DrawLine(1, mx + 104, 64, mx + 104, motdh-20, 255, 127, 0);
1191 e_TextureFontPrintEx(18, 68, 'NAME/MAP', gStdFont, 255, 127, 0, 1);
1192 e_TextureFontPrintEx(mx - 68, 68, 'PING', gStdFont, 255, 127, 0, 1);
1193 e_TextureFontPrintEx(mx + 2, 68, 'MODE', gStdFont, 255, 127, 0, 1);
1194 e_TextureFontPrintEx(mx + 54, 68, 'PLRS', gStdFont, 255, 127, 0, 1);
1195 e_TextureFontPrintEx(mx + 106, 68, 'VER', gStdFont, 255, 127, 0, 1);
1197 y := 90;
1198 for I := 0 to High(ST) do
1199 begin
1200 Srv := GetServerFromTable(I, SL, ST);
1201 // Name and map
1202 e_TextureFontPrintEx(18, y, Srv.Name, gStdFont, 255, 255, 255, 1);
1203 e_TextureFontPrintEx(18, y + 16, Srv.Map, gStdFont, 210, 210, 210, 1);
1205 // Ping and similar count
1206 if (Srv.Ping < 0) or (Srv.Ping > 999) then
1207 e_TextureFontPrintEx(mx - 68, y, _lc[I_NET_SLIST_NO_ACCESS], gStdFont, 255, 0, 0, 1)
1208 else
1209 if Srv.Ping = 0 then
1210 e_TextureFontPrintEx(mx - 68, y, '<1' + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1)
1211 else
1212 e_TextureFontPrintEx(mx - 68, y, IntToStr(Srv.Ping) + _lc[I_NET_SLIST_PING_MS], gStdFont, 255, 255, 255, 1);
1214 if Length(ST[I].Indices) > 1 then
1215 e_TextureFontPrintEx(mx - 68, y + 16, '< ' + IntToStr(Length(ST[I].Indices)) + ' >', gStdFont, 210, 210, 210, 1);
1217 // Game mode
1218 e_TextureFontPrintEx(mx + 2, y, g_Game_ModeToText(Srv.GameMode), gStdFont, 255, 255, 255, 1);
1220 // Players
1221 e_TextureFontPrintEx(mx + 54, y, IntToStr(Srv.Players) + '/' + IntToStr(Srv.MaxPlayers), gStdFont, 255, 255, 255, 1);
1222 e_TextureFontPrintEx(mx + 54, y + 16, IntToStr(Srv.LocalPl) + '+' + IntToStr(Srv.Bots), gStdFont, 210, 210, 210, 1);
1224 // Version
1225 e_TextureFontPrintEx(mx + 106, y, IntToStr(Srv.Protocol), gStdFont, 255, 255, 255, 1);
1227 y := y + 42;
1228 end;
1230 e_TextureFontPrintEx(20, motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
1231 ip := IntToStr(Length(ST)) + _lc[I_NET_SLIST_SERVERS];
1232 e_TextureFontPrintEx(gScreenWidth - 48 - (Length(ip) + 1)*cw,
1233 motdh-20+3, ip, gStdFont, 205, 205, 205, 1);
1234 end;
1237 //==========================================================================
1238 //
1239 // g_Serverlist_GenerateTable
1240 //
1241 //==========================================================================
1242 procedure g_Serverlist_GenerateTable (SL: TNetServerList; var ST: TNetServerTable);
1243 var
1244 i, j: Integer;
1246 function FindServerInTable(Name: string): Integer;
1247 var
1248 i: Integer;
1249 begin
1250 Result := -1;
1251 if ST = nil then
1252 Exit;
1253 for i := Low(ST) to High(ST) do
1254 begin
1255 if Length(ST[i].Indices) = 0 then
1256 continue;
1257 if SL[ST[i].Indices[0]].Name = Name then
1258 begin
1259 Result := i;
1260 Exit;
1261 end;
1262 end;
1263 end;
1264 function ComparePing(i1, i2: Integer): Boolean;
1265 var
1266 p1, p2: Int64;
1267 begin
1268 p1 := SL[i1].Ping;
1269 p2 := SL[i2].Ping;
1270 if (p1 < 0) then p1 := 999;
1271 if (p2 < 0) then p2 := 999;
1272 Result := p1 > p2;
1273 end;
1274 procedure SortIndices(var ind: Array of Integer);
1275 var
1276 I, J: Integer;
1277 T: Integer;
1278 begin
1279 for I := High(ind) downto Low(ind) do
1280 for J := Low(ind) to High(ind) - 1 do
1281 if ComparePing(ind[j], ind[j+1]) then
1282 begin
1283 T := ind[j];
1284 ind[j] := ind[j+1];
1285 ind[j+1] := T;
1286 end;
1287 end;
1288 procedure SortRows();
1289 var
1290 I, J: Integer;
1291 T: TNetServerRow;
1292 begin
1293 for I := High(ST) downto Low(ST) do
1294 for J := Low(ST) to High(ST) - 1 do
1295 if ComparePing(ST[j].Indices[0], ST[j+1].Indices[0]) then
1296 begin
1297 T := ST[j];
1298 ST[j] := ST[j+1];
1299 ST[j+1] := T;
1300 end;
1301 end;
1302 begin
1303 ST := nil;
1304 if SL = nil then
1305 Exit;
1306 for i := Low(SL) to High(SL) do
1307 begin
1308 j := FindServerInTable(SL[i].Name);
1309 if j = -1 then
1310 begin
1311 j := Length(ST);
1312 SetLength(ST, j + 1);
1313 ST[j].Current := 0;
1314 SetLength(ST[j].Indices, 1);
1315 ST[j].Indices[0] := i;
1316 end
1317 else
1318 begin
1319 SetLength(ST[j].Indices, Length(ST[j].Indices) + 1);
1320 ST[j].Indices[High(ST[j].Indices)] := i;
1321 end;
1322 end;
1324 for i := Low(ST) to High(ST) do
1325 SortIndices(ST[i].Indices);
1327 SortRows();
1328 end;
1331 //==========================================================================
1332 //
1333 // g_Serverlist_Control
1334 //
1335 //==========================================================================
1336 procedure g_Serverlist_Control (var SL: TNetServerList; var ST: TNetServerTable);
1337 var
1338 qm: Boolean;
1339 Srv: TNetServer;
1340 begin
1341 if gConsoleShow or gChatShow then
1342 Exit;
1344 qm := sys_HandleInput(); // this updates kbd
1346 if qm or e_KeyPressed(IK_ESCAPE) or e_KeyPressed(VK_ESCAPE) or
1347 e_KeyPressed(JOY0_JUMP) or e_KeyPressed(JOY1_JUMP) or
1348 e_KeyPressed(JOY2_JUMP) or e_KeyPressed(JOY3_JUMP) then
1349 begin
1350 SL := nil;
1351 ST := nil;
1352 gState := STATE_MENU;
1353 g_GUI_ShowWindow('MainMenu');
1354 g_GUI_ShowWindow('NetGameMenu');
1355 g_GUI_ShowWindow('NetClientMenu');
1356 g_Sound_PlayEx(WINDOW_CLOSESOUND);
1357 Exit;
1358 end;
1360 // if there's a message on the screen,
1361 if not slReadUrgent and (slUrgent <> '') then
1362 begin
1363 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1364 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1365 slReadUrgent := True;
1366 Exit;
1367 end;
1369 if e_KeyPressed(IK_SPACE) or e_KeyPressed(VK_JUMP) or
1370 e_KeyPressed(JOY0_ACTIVATE) or e_KeyPressed(JOY1_ACTIVATE) or e_KeyPressed(JOY2_ACTIVATE) or e_KeyPressed(JOY3_ACTIVATE) then
1371 begin
1372 if not slFetched then
1373 begin
1374 slWaitStr := _lc[I_NET_SLIST_WAIT];
1376 g_Game_Draw;
1377 sys_Repaint;
1379 if g_Net_Slist_Fetch(SL) then
1380 begin
1381 if SL = nil then
1382 slWaitStr := _lc[I_NET_SLIST_NOSERVERS];
1383 end
1384 else
1385 if SL = nil then
1386 slWaitStr := _lc[I_NET_SLIST_ERROR];
1387 slFetched := True;
1388 slSelection := 0;
1389 g_Serverlist_GenerateTable(SL, ST);
1390 end;
1391 end
1392 else
1393 slFetched := False;
1395 if SL = nil then Exit;
1397 if e_KeyPressed(IK_RETURN) or e_KeyPressed(IK_KPRETURN) or e_KeyPressed(VK_FIRE) or e_KeyPressed(VK_OPEN) or
1398 e_KeyPressed(JOY0_ATTACK) or e_KeyPressed(JOY1_ATTACK) or e_KeyPressed(JOY2_ATTACK) or e_KeyPressed(JOY3_ATTACK) then
1399 begin
1400 if not slReturnPressed then
1401 begin
1402 Srv := GetServerFromTable(slSelection, SL, ST);
1403 if Srv.Password then
1404 begin
1405 PromptIP := Srv.IP;
1406 PromptPort := Srv.Port;
1407 gState := STATE_MENU;
1408 g_GUI_ShowWindow('ClientPasswordMenu');
1409 SL := nil;
1410 ST := nil;
1411 slReturnPressed := True;
1412 Exit;
1413 end
1414 else
1415 g_Game_StartClient(Srv.IP, Srv.Port, '');
1416 SL := nil;
1417 ST := nil;
1418 slReturnPressed := True;
1419 Exit;
1420 end;
1421 end
1422 else
1423 slReturnPressed := False;
1425 if e_KeyPressed(IK_DOWN) or e_KeyPressed(IK_KPDOWN) or e_KeyPressed(VK_DOWN) or
1426 e_KeyPressed(JOY0_DOWN) or e_KeyPressed(JOY1_DOWN) or e_KeyPressed(JOY2_DOWN) or e_KeyPressed(JOY3_DOWN) then
1427 begin
1428 if not slDirPressed then
1429 begin
1430 Inc(slSelection);
1431 if slSelection > High(ST) then slSelection := 0;
1432 slDirPressed := True;
1433 end;
1434 end;
1436 if e_KeyPressed(IK_UP) or e_KeyPressed(IK_KPUP) or e_KeyPressed(VK_UP) or
1437 e_KeyPressed(JOY0_UP) or e_KeyPressed(JOY1_UP) or e_KeyPressed(JOY2_UP) or e_KeyPressed(JOY3_UP) then
1438 begin
1439 if not slDirPressed then
1440 begin
1441 if slSelection = 0 then slSelection := Length(ST);
1442 Dec(slSelection);
1444 slDirPressed := True;
1445 end;
1446 end;
1448 if e_KeyPressed(IK_RIGHT) or e_KeyPressed(IK_KPRIGHT) or e_KeyPressed(VK_RIGHT) or
1449 e_KeyPressed(JOY0_RIGHT) or e_KeyPressed(JOY1_RIGHT) or e_KeyPressed(JOY2_RIGHT) or e_KeyPressed(JOY3_RIGHT) then
1450 begin
1451 if not slDirPressed then
1452 begin
1453 Inc(ST[slSelection].Current);
1454 if ST[slSelection].Current > High(ST[slSelection].Indices) then ST[slSelection].Current := 0;
1455 slDirPressed := True;
1456 end;
1457 end;
1459 if e_KeyPressed(IK_LEFT) or e_KeyPressed(IK_KPLEFT) or e_KeyPressed(VK_LEFT) or
1460 e_KeyPressed(JOY0_LEFT) or e_KeyPressed(JOY1_LEFT) or e_KeyPressed(JOY2_LEFT) or e_KeyPressed(JOY3_LEFT) then
1461 begin
1462 if not slDirPressed then
1463 begin
1464 if ST[slSelection].Current = 0 then ST[slSelection].Current := Length(ST[slSelection].Indices);
1465 Dec(ST[slSelection].Current);
1467 slDirPressed := True;
1468 end;
1469 end;
1471 if (not e_KeyPressed(IK_DOWN)) and
1472 (not e_KeyPressed(IK_UP)) and
1473 (not e_KeyPressed(IK_RIGHT)) and
1474 (not e_KeyPressed(IK_LEFT)) and
1475 (not e_KeyPressed(IK_KPDOWN)) and
1476 (not e_KeyPressed(IK_KPUP)) and
1477 (not e_KeyPressed(IK_KPRIGHT)) and
1478 (not e_KeyPressed(IK_KPLEFT)) and
1479 (not e_KeyPressed(VK_DOWN)) and
1480 (not e_KeyPressed(VK_UP)) and
1481 (not e_KeyPressed(VK_RIGHT)) and
1482 (not e_KeyPressed(VK_LEFT)) and
1483 (not e_KeyPressed(JOY0_UP)) and (not e_KeyPressed(JOY1_UP)) and (not e_KeyPressed(JOY2_UP)) and (not e_KeyPressed(JOY3_UP)) and
1484 (not e_KeyPressed(JOY0_DOWN)) and (not e_KeyPressed(JOY1_DOWN)) and (not e_KeyPressed(JOY2_DOWN)) and (not e_KeyPressed(JOY3_DOWN)) and
1485 (not e_KeyPressed(JOY0_LEFT)) and (not e_KeyPressed(JOY1_LEFT)) and (not e_KeyPressed(JOY2_LEFT)) and (not e_KeyPressed(JOY3_LEFT)) and
1486 (not e_KeyPressed(JOY0_RIGHT)) and (not e_KeyPressed(JOY1_RIGHT)) and (not e_KeyPressed(JOY2_RIGHT)) and (not e_KeyPressed(JOY3_RIGHT))
1487 then
1488 slDirPressed := False;
1489 end;
1492 end.