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;
|
49
|
52 |
var IPCSock: PTCPSocket = nil;
|
4
|
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
|
22
|
91 |
'!': begin {$IFDEF DEBUGFILE}AddFileLog('Ping? Pong!');{$ENDIF}isPonged:= true; end;
|
4
|
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
|
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
|
|
169 |
if GameTicks > extcmd[cmdcurpos].Time then
|
|
170 |
outerror('oops, queue error. in buffer: '+extcmd[cmdcurpos].cmd+' ('+inttostr(GameTicks)+' > '+inttostr(extcmd[cmdcurpos].Time)+')', true);
|
|
171 |
|
|
172 |
tmpflag:= true;
|
|
173 |
while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do
|
|
174 |
begin
|
|
175 |
case extcmd[cmdcurpos].cmd of
|
37
|
176 |
'L': ParseCommand('+left');
|
|
177 |
'l': ParseCommand('-left');
|
|
178 |
'R': ParseCommand('+right');
|
|
179 |
'r': ParseCommand('-right');
|
|
180 |
'U': ParseCommand('+up');
|
|
181 |
'u': ParseCommand('-up');
|
|
182 |
'D': ParseCommand('+down');
|
|
183 |
'd': ParseCommand('-down');
|
|
184 |
'A': ParseCommand('+attack');
|
|
185 |
'a': ParseCommand('-attack');
|
|
186 |
'S': ParseCommand('switch');
|
|
187 |
'j': ParseCommand('ljump');
|
|
188 |
'J': ParseCommand('hjump');
|
48
|
189 |
',': ParseCommand('skip');
|
4
|
190 |
'N': begin
|
|
191 |
tmpflag:= false;
|
|
192 |
{$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF}
|
|
193 |
end;
|
|
194 |
'p': begin
|
|
195 |
TargetPoint.X:= extcmd[cmdcurpos].X;
|
|
196 |
TargetPoint.Y:= extcmd[cmdcurpos].Y;
|
37
|
197 |
ParseCommand('put')
|
4
|
198 |
end;
|
|
199 |
'P': begin
|
|
200 |
CursorPoint.X:= extcmd[cmdcurpos].X + WorldDx;
|
|
201 |
CursorPoint.Y:= extcmd[cmdcurpos].Y + WorldDy;
|
|
202 |
end;
|
37
|
203 |
'1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd);
|
|
204 |
#128..#134: ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79))
|
4
|
205 |
end;
|
|
206 |
inc(cmdcurpos)
|
|
207 |
end;
|
|
208 |
isInLag:= (cmdcurpos > cmdendpos) and tmpflag
|
|
209 |
end;
|
|
210 |
|
|
211 |
procedure LoadFortPoints(Fort: shortstring; isRight: boolean; Count: Longword);
|
|
212 |
const cMAXFORTPOINTS = 20;
|
|
213 |
var f: textfile;
|
|
214 |
i, t: integer;
|
|
215 |
cnt: Longword;
|
|
216 |
ar: array[0..Pred(cMAXFORTPOINTS)] of TPoint;
|
|
217 |
p: TPoint;
|
|
218 |
begin
|
|
219 |
if isRight then Fort:= Pathz[ptForts] + Fort + 'R.txt'
|
|
220 |
else Fort:= Pathz[ptForts] + Fort + 'L.txt';
|
|
221 |
WriteToConsole(msgLoading + Fort + ' ');
|
|
222 |
{$I-}
|
|
223 |
AssignFile(f, Fort);
|
|
224 |
Reset(f);
|
|
225 |
cnt:= 0;
|
|
226 |
while not (eof(f) or (cnt = cMAXFORTPOINTS)) do
|
|
227 |
begin
|
|
228 |
Readln(f, ar[cnt].x, ar[cnt].y);
|
|
229 |
if isRight then inc(ar[cnt].x, 1024);
|
|
230 |
inc(cnt);
|
|
231 |
end;
|
|
232 |
Closefile(f);
|
|
233 |
{$I+}
|
|
234 |
TryDo(IOResult = 0, msgFailed, true);
|
|
235 |
WriteLnToConsole(msgOK);
|
17
|
236 |
TryDo(Count < cnt, 'Fort doesn''t contain needed amount of spawn points', true);
|
4
|
237 |
for i:= 0 to Pred(cnt) do
|
|
238 |
begin
|
|
239 |
t:= GetRandom(cnt);
|
|
240 |
if i <> t then
|
|
241 |
begin
|
|
242 |
p:= ar[i];
|
|
243 |
ar[i]:= ar[t];
|
|
244 |
ar[t]:= p
|
|
245 |
end
|
|
246 |
end;
|
|
247 |
for i:= 0 to Pred(Count) do
|
|
248 |
AddHHPoint(ar[i].x, ar[i].y);
|
|
249 |
end;
|
|
250 |
|
|
251 |
end.
|