4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*)
|
|
33 |
|
|
34 |
unit uIO;
|
|
35 |
interface
|
|
36 |
uses SDLh;
|
|
37 |
{$INCLUDE options.inc}
|
|
38 |
|
|
39 |
const ipcPort: Word = 0;
|
|
40 |
|
|
41 |
procedure SendIPC(s: shortstring);
|
|
42 |
procedure SendIPCAndWaitReply(s: shortstring);
|
|
43 |
procedure IPCCheckSock;
|
|
44 |
procedure InitIPC;
|
|
45 |
procedure CloseIPC;
|
|
46 |
procedure NetGetNextCmd;
|
|
47 |
|
|
48 |
implementation
|
|
49 |
uses uConsole, uConsts, uWorld, uMisc, uRandom, uLand;
|
|
50 |
const isPonged: boolean = false;
|
49
|
51 |
var IPCSock: PTCPSocket = nil;
|
4
|
52 |
fds: PSDLNet_SocketSet;
|
|
53 |
|
|
54 |
extcmd: array[word] of packed record
|
|
55 |
Time: LongWord;
|
|
56 |
case byte of
|
|
57 |
1: (len: byte;
|
|
58 |
cmd: Char;
|
|
59 |
X, Y: integer;);
|
|
60 |
2: (str: shortstring);
|
|
61 |
end;
|
|
62 |
cmdcurpos: integer = 0;
|
|
63 |
cmdendpos: integer = -1;
|
|
64 |
|
|
65 |
procedure InitIPC;
|
|
66 |
var ipaddr: TIPAddress;
|
|
67 |
begin
|
|
68 |
WriteToConsole('Init SDL_Net... ');
|
|
69 |
SDLTry(SDLNet_Init = 0, true);
|
|
70 |
fds:= SDLNet_AllocSocketSet(1);
|
|
71 |
SDLTry(fds <> nil, true);
|
|
72 |
WriteLnToConsole(msgOK);
|
|
73 |
WriteToConsole('Establishing IPC connection... ');
|
|
74 |
SDLTry(SDLNet_ResolveHost(ipaddr, '127.0.0.1', ipcPort) = 0, true);
|
|
75 |
IPCSock:= SDLNet_TCP_Open(ipaddr);
|
|
76 |
SDLTry(IPCSock <> nil, true);
|
|
77 |
WriteLnToConsole(msgOK)
|
|
78 |
end;
|
|
79 |
|
|
80 |
procedure CloseIPC;
|
|
81 |
begin
|
|
82 |
SDLNet_FreeSocketSet(fds);
|
|
83 |
SDLNet_TCP_Close(IPCSock);
|
|
84 |
SDLNet_Quit
|
|
85 |
end;
|
|
86 |
|
|
87 |
procedure ParseIPCCommand(s: shortstring);
|
|
88 |
begin
|
|
89 |
case s[1] of
|
22
|
90 |
'!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end;
|
4
|
91 |
'?': SendIPC('!');
|
|
92 |
'e': ParseCommand(copy(s, 2, Length(s) - 1));
|
|
93 |
'E': OutError(copy(s, 2, Length(s) - 1), true);
|
|
94 |
'W': OutError(copy(s, 2, Length(s) - 1), false);
|
|
95 |
'T': case s[2] of
|
|
96 |
'L': GameType:= gmtLocal;
|
|
97 |
'D': GameType:= gmtDemo;
|
|
98 |
'N': GameType:= gmtNet;
|
72
|
99 |
'S': GameType:= gmtSave;
|
4
|
100 |
else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end;
|
|
101 |
else
|
|
102 |
inc(cmdendpos);
|
|
103 |
extcmd[cmdendpos].Time := PLongWord(@s[byte(s[0]) - 3])^;
|
|
104 |
extcmd[cmdendpos].str := s;
|
|
105 |
{$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(extcmd[cmdendpos].Time)+' at '+inttostr(cmdendpos));{$ENDIF}
|
|
106 |
dec(extcmd[cmdendpos].len, 4)
|
|
107 |
end
|
|
108 |
end;
|
|
109 |
|
|
110 |
procedure IPCCheckSock;
|
|
111 |
const ss: string = '';
|
|
112 |
var i: integer;
|
|
113 |
buf: array[0..255] of byte;
|
|
114 |
s: shortstring absolute buf;
|
|
115 |
begin
|
|
116 |
fds.numsockets:= 0;
|
|
117 |
SDLNet_AddSocket(fds, IPCSock);
|
|
118 |
|
|
119 |
while SDLNet_CheckSockets(fds, 0) > 0 do
|
|
120 |
begin
|
|
121 |
i:= SDLNet_TCP_Recv(IPCSock, @buf[1], 255);
|
|
122 |
if i > 0 then
|
|
123 |
begin
|
|
124 |
buf[0]:= i;
|
|
125 |
ss:= ss + s;
|
|
126 |
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do
|
|
127 |
begin
|
|
128 |
ParseIPCCommand(copy(ss, 2, byte(ss[1])));
|
|
129 |
Delete(ss, 1, Succ(byte(ss[1])))
|
|
130 |
end
|
|
131 |
end else OutError('IPC connection lost', true)
|
|
132 |
end;
|
|
133 |
end;
|
|
134 |
|
|
135 |
procedure SendIPC(s: shortstring);
|
|
136 |
begin
|
49
|
137 |
if IPCSock <> nil then
|
|
138 |
begin
|
|
139 |
if s[0]>#251 then s[0]:= #251;
|
|
140 |
PLongWord(@s[Succ(byte(s[0]))])^:= GameTicks;
|
|
141 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF}
|
|
142 |
inc(s[0],4);
|
|
143 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
|
|
144 |
end
|
4
|
145 |
end;
|
|
146 |
|
|
147 |
procedure SendIPCAndWaitReply(s: shortstring);
|
|
148 |
begin
|
|
149 |
SendIPC(s);
|
22
|
150 |
SendIPC('?');
|
4
|
151 |
isPonged:= false;
|
|
152 |
repeat
|
|
153 |
IPCCheckSock;
|
|
154 |
SDL_Delay(1)
|
|
155 |
until isPonged
|
|
156 |
end;
|
|
157 |
|
|
158 |
procedure NetGetNextCmd;
|
|
159 |
var tmpflag: boolean;
|
|
160 |
begin
|
|
161 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do
|
|
162 |
begin
|
|
163 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)));
|
|
164 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay);
|
|
165 |
inc(cmdcurpos)
|
|
166 |
end;
|
|
167 |
|
|
168 |
if cmdcurpos <= cmdendpos then
|
70
|
169 |
TryDo(GameTicks <= extcmd[cmdcurpos].Time,
|
|
170 |
'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd +
|
|
171 |
' (' + inttostr(GameTicks) + ' > ' +
|
|
172 |
inttostr(extcmd[cmdcurpos].Time) + ')',
|
|
173 |
true);
|
4
|
174 |
|
|
175 |
tmpflag:= true;
|
|
176 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do
|
|
177 |
begin
|
|
178 |
case extcmd[cmdcurpos].cmd of
|
37
|
179 |
'L': ParseCommand('+left');
|
|
180 |
'l': ParseCommand('-left');
|
|
181 |
'R': ParseCommand('+right');
|
|
182 |
'r': ParseCommand('-right');
|
|
183 |
'U': ParseCommand('+up');
|
|
184 |
'u': ParseCommand('-up');
|
|
185 |
'D': ParseCommand('+down');
|
|
186 |
'd': ParseCommand('-down');
|
|
187 |
'A': ParseCommand('+attack');
|
|
188 |
'a': ParseCommand('-attack');
|
|
189 |
'S': ParseCommand('switch');
|
|
190 |
'j': ParseCommand('ljump');
|
|
191 |
'J': ParseCommand('hjump');
|
48
|
192 |
',': ParseCommand('skip');
|
4
|
193 |
'N': begin
|
|
194 |
tmpflag:= false;
|
|
195 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF}
|
|
196 |
end;
|
|
197 |
'p': begin
|
|
198 |
TargetPoint.X:= extcmd[cmdcurpos].X;
|
|
199 |
TargetPoint.Y:= extcmd[cmdcurpos].Y;
|
37
|
200 |
ParseCommand('put')
|
4
|
201 |
end;
|
|
202 |
'P': begin
|
|
203 |
CursorPoint.X:= extcmd[cmdcurpos].X + WorldDx;
|
|
204 |
CursorPoint.Y:= extcmd[cmdcurpos].Y + WorldDy;
|
|
205 |
end;
|
37
|
206 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd);
|
|
207 |
#128..#134: ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79))
|
4
|
208 |
end;
|
|
209 |
inc(cmdcurpos)
|
|
210 |
end;
|
|
211 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag
|
|
212 |
end;
|
|
213 |
|
|
214 |
end.
|