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