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 |
procedure LoadFortPoints(Fort: shortstring; isRight: boolean; Count: Longword);
|
|
48 |
|
|
49 |
implementation
|
|
50 |
uses uConsole, uConsts, uWorld, uMisc, uRandom, uLand;
|
|
51 |
const isPonged: boolean = false;
|
|
52 |
var IPCSock: PTCPSocket;
|
|
53 |
fds: PSDLNet_SocketSet;
|
|
54 |
|
|
55 |
extcmd: array[word] of packed record
|
|
56 |
Time: LongWord;
|
|
57 |
case byte of
|
|
58 |
1: (len: byte;
|
|
59 |
cmd: Char;
|
|
60 |
X, Y: integer;);
|
|
61 |
2: (str: shortstring);
|
|
62 |
end;
|
|
63 |
cmdcurpos: integer = 0;
|
|
64 |
cmdendpos: integer = -1;
|
|
65 |
|
|
66 |
procedure InitIPC;
|
|
67 |
var ipaddr: TIPAddress;
|
|
68 |
begin
|
|
69 |
WriteToConsole('Init SDL_Net... ');
|
|
70 |
SDLTry(SDLNet_Init = 0, true);
|
|
71 |
fds:= SDLNet_AllocSocketSet(1);
|
|
72 |
SDLTry(fds <> nil, true);
|
|
73 |
WriteLnToConsole(msgOK);
|
|
74 |
WriteToConsole('Establishing IPC connection... ');
|
|
75 |
SDLTry(SDLNet_ResolveHost(ipaddr, '127.0.0.1', ipcPort) = 0, true);
|
|
76 |
IPCSock:= SDLNet_TCP_Open(ipaddr);
|
|
77 |
SDLTry(IPCSock <> nil, true);
|
|
78 |
WriteLnToConsole(msgOK)
|
|
79 |
end;
|
|
80 |
|
|
81 |
procedure CloseIPC;
|
|
82 |
begin
|
|
83 |
SDLNet_FreeSocketSet(fds);
|
|
84 |
SDLNet_TCP_Close(IPCSock);
|
|
85 |
SDLNet_Quit
|
|
86 |
end;
|
|
87 |
|
|
88 |
procedure ParseIPCCommand(s: shortstring);
|
|
89 |
begin
|
|
90 |
case s[1] of
|
|
91 |
'!': isPonged:= true;
|
|
92 |
'?': SendIPC('!');
|
|
93 |
'e': ParseCommand(copy(s, 2, Length(s) - 1));
|
|
94 |
'E': OutError(copy(s, 2, Length(s) - 1), true);
|
|
95 |
'W': OutError(copy(s, 2, Length(s) - 1), false);
|
|
96 |
'T': case s[2] of
|
|
97 |
'L': GameType:= gmtLocal;
|
|
98 |
'D': GameType:= gmtDemo;
|
|
99 |
'N': GameType:= gmtNet;
|
|
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
|
|
137 |
//WriteLnToConsole(s);
|
|
138 |
if s[0]>#251 then s[0]:= #251;
|
|
139 |
PLongWord(@s[Succ(byte(s[0]))])^:= GameTicks;
|
|
140 |
{$IFDEF DEBUGFILE}AddFileLog('IPC send: '+s);{$ENDIF}
|
|
141 |
inc(s[0],4);
|
|
142 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
|
|
143 |
end;
|
|
144 |
|
|
145 |
procedure SendIPCAndWaitReply(s: shortstring);
|
|
146 |
begin
|
|
147 |
SendIPC(s);
|
|
148 |
s:= '?';
|
|
149 |
SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])));
|
|
150 |
isPonged:= false;
|
|
151 |
repeat
|
|
152 |
IPCCheckSock;
|
|
153 |
SDL_Delay(1)
|
|
154 |
until isPonged
|
|
155 |
end;
|
|
156 |
|
|
157 |
procedure NetGetNextCmd;
|
|
158 |
var tmpflag: boolean;
|
|
159 |
begin
|
|
160 |
while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do
|
|
161 |
begin
|
|
162 |
WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)));
|
|
163 |
AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay);
|
|
164 |
inc(cmdcurpos)
|
|
165 |
end;
|
|
166 |
|
|
167 |
if cmdcurpos <= cmdendpos then
|
|
168 |
if GameTicks > extcmd[cmdcurpos].Time then
|
|
169 |
outerror('oops, queue error. in buffer: '+extcmd[cmdcurpos].cmd+' ('+inttostr(GameTicks)+' > '+inttostr(extcmd[cmdcurpos].Time)+')', true);
|
|
170 |
|
|
171 |
tmpflag:= true;
|
|
172 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do
|
|
173 |
begin
|
|
174 |
case extcmd[cmdcurpos].cmd of
|
|
175 |
'L': ParseCommand('/+left');
|
|
176 |
'l': ParseCommand('/-left');
|
|
177 |
'R': ParseCommand('/+right');
|
|
178 |
'r': ParseCommand('/-right');
|
|
179 |
'U': ParseCommand('/+up');
|
|
180 |
'u': ParseCommand('/-up');
|
|
181 |
'D': ParseCommand('/+down');
|
|
182 |
'd': ParseCommand('/-down');
|
|
183 |
'A': ParseCommand('/+attack');
|
|
184 |
'a': ParseCommand('/-attack');
|
|
185 |
'S': ParseCommand('/switch');
|
|
186 |
'j': ParseCommand('/ljump');
|
|
187 |
'J': ParseCommand('/hjump');
|
|
188 |
'N': begin
|
|
189 |
tmpflag:= false;
|
|
190 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF}
|
|
191 |
end;
|
|
192 |
'p': begin
|
|
193 |
TargetPoint.X:= extcmd[cmdcurpos].X;
|
|
194 |
TargetPoint.Y:= extcmd[cmdcurpos].Y;
|
|
195 |
ParseCommand('/put')
|
|
196 |
end;
|
|
197 |
'P': begin
|
|
198 |
CursorPoint.X:= extcmd[cmdcurpos].X + WorldDx;
|
|
199 |
CursorPoint.Y:= extcmd[cmdcurpos].Y + WorldDy;
|
|
200 |
end;
|
|
201 |
'1'..'5': ParseCommand('/timer ' + extcmd[cmdcurpos].cmd);
|
|
202 |
#128..#131: ParseCommand('/slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79))
|
|
203 |
end;
|
|
204 |
inc(cmdcurpos)
|
|
205 |
end;
|
|
206 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag
|
|
207 |
end;
|
|
208 |
|
|
209 |
procedure LoadFortPoints(Fort: shortstring; isRight: boolean; Count: Longword);
|
|
210 |
const cMAXFORTPOINTS = 20;
|
|
211 |
var f: textfile;
|
|
212 |
i, t: integer;
|
|
213 |
cnt: Longword;
|
|
214 |
ar: array[0..Pred(cMAXFORTPOINTS)] of TPoint;
|
|
215 |
p: TPoint;
|
|
216 |
begin
|
|
217 |
if isRight then Fort:= Pathz[ptForts] + Fort + 'R.txt'
|
|
218 |
else Fort:= Pathz[ptForts] + Fort + 'L.txt';
|
|
219 |
WriteToConsole(msgLoading + Fort + ' ');
|
|
220 |
{$I-}
|
|
221 |
AssignFile(f, Fort);
|
|
222 |
Reset(f);
|
|
223 |
cnt:= 0;
|
|
224 |
while not (eof(f) or (cnt = cMAXFORTPOINTS)) do
|
|
225 |
begin
|
|
226 |
Readln(f, ar[cnt].x, ar[cnt].y);
|
|
227 |
if isRight then inc(ar[cnt].x, 1024);
|
|
228 |
inc(cnt);
|
|
229 |
end;
|
|
230 |
Closefile(f);
|
|
231 |
{$I+}
|
|
232 |
TryDo(IOResult = 0, msgFailed, true);
|
|
233 |
WriteLnToConsole(msgOK);
|
|
234 |
TryDo(Count < cnt, 'Fort doesn''t contain needed spawn points', true);
|
|
235 |
for i:= 0 to Pred(cnt) do
|
|
236 |
begin
|
|
237 |
t:= GetRandom(cnt);
|
|
238 |
if i <> t then
|
|
239 |
begin
|
|
240 |
p:= ar[i];
|
|
241 |
ar[i]:= ar[t];
|
|
242 |
ar[t]:= p
|
|
243 |
end
|
|
244 |
end;
|
|
245 |
for i:= 0 to Pred(Count) do
|
|
246 |
AddHHPoint(ar[i].x, ar[i].y);
|
|
247 |
end;
|
|
248 |
|
|
249 |
end.
|