34 procedure NetGetNextCmd; |
34 procedure NetGetNextCmd; |
35 |
35 |
36 implementation |
36 implementation |
37 uses uConsole, uConsts, uWorld, uMisc, uLand; |
37 uses uConsole, uConsts, uWorld, uMisc, uLand; |
38 const isPonged: boolean = false; |
38 const isPonged: boolean = false; |
39 MAXCMDS = 65535; |
39 |
|
40 type PCmd = ^TCmd; |
|
41 TCmd = packed record |
|
42 Next: PCmd; |
|
43 Time: LongWord; |
|
44 case byte of |
|
45 1: (len: byte; |
|
46 cmd: Char; |
|
47 X, Y: SmallInt); |
|
48 2: (str: shortstring); |
|
49 end; |
|
50 |
40 var IPCSock: PTCPSocket = nil; |
51 var IPCSock: PTCPSocket = nil; |
41 fds: PSDLNet_SocketSet; |
52 fds: PSDLNet_SocketSet; |
42 |
53 |
43 extcmd: array[0..MAXCMDS] of packed record |
54 headcmd: PCmd = nil; |
44 Time: LongWord; |
55 lastcmd: PCmd = nil; |
45 case byte of |
56 |
46 1: (len: byte; |
57 function AddCmd(Time: Longword; str: shortstring): PCmd; |
47 cmd: Char; |
58 var Result: PCmd; |
48 X, Y: SmallInt); |
59 begin |
49 2: (str: shortstring); |
60 new(Result); |
50 end; |
61 FillChar(Result^, sizeof(TCmd), 0); |
51 cmdcurpos: LongInt = 0; |
62 Result^.Time:= Time; |
52 cmdendpos: LongInt = -1; |
63 Result^.str:= str; |
|
64 dec(Result^.len, 4); |
|
65 if headcmd = nil then |
|
66 begin |
|
67 headcmd:= Result; |
|
68 lastcmd:= Result |
|
69 end else |
|
70 begin |
|
71 lastcmd^.Next:= Result; |
|
72 lastcmd:= Result |
|
73 end; |
|
74 AddCmd:= Result |
|
75 end; |
|
76 |
|
77 procedure RemoveCmd; |
|
78 var tmp: PCmd; |
|
79 begin |
|
80 TryDo(headcmd <> nil, 'Engine bug: headcmd = nil', true); |
|
81 tmp:= headcmd; |
|
82 headcmd:= headcmd^.Next; |
|
83 if headcmd = nil then lastcmd:= nil; |
|
84 dispose(tmp) |
|
85 end; |
53 |
86 |
54 procedure InitIPC; |
87 procedure InitIPC; |
55 var ipaddr: TIPAddress; |
88 var ipaddr: TIPAddress; |
56 begin |
89 begin |
57 WriteToConsole('Init SDL_Net... '); |
90 WriteToConsole('Init SDL_Net... '); |
87 'D': GameType:= gmtDemo; |
120 'D': GameType:= gmtDemo; |
88 'N': GameType:= gmtNet; |
121 'N': GameType:= gmtNet; |
89 'S': GameType:= gmtSave; |
122 'S': GameType:= gmtSave; |
90 else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
123 else OutError(errmsgIncorrectUse + ' IPC "T" :' + s[2], true) end; |
91 else |
124 else |
92 inc(cmdendpos); |
125 AddCmd(SDLNet_Read32(@s[byte(s[0]) - 3]), s); |
93 TryDo(cmdendpos <= MAXCMDS, 'Too many commands in queue', true); |
126 {$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(lastcmd^.Time));{$ENDIF} |
94 extcmd[cmdendpos].Time := SDLNet_Read32(@s[byte(s[0]) - 3]); |
|
95 extcmd[cmdendpos].str := s; |
|
96 {$IFDEF DEBUGFILE}AddFileLog('IPC in: '+s[1]+' ticks '+inttostr(extcmd[cmdendpos].Time)+' at '+inttostr(cmdendpos));{$ENDIF} |
|
97 dec(extcmd[cmdendpos].len, 4) |
|
98 end |
127 end |
99 end; |
128 end; |
100 |
129 |
101 procedure IPCCheckSock; |
130 procedure IPCCheckSock; |
102 const ss: string = ''; |
131 const ss: string = ''; |
170 end; |
199 end; |
171 |
200 |
172 procedure NetGetNextCmd; |
201 procedure NetGetNextCmd; |
173 var tmpflag: boolean; |
202 var tmpflag: boolean; |
174 begin |
203 begin |
175 while (cmdcurpos <= cmdendpos)and(extcmd[cmdcurpos].cmd = 's') do |
204 while (headcmd <> nil) and (headcmd^.cmd = 's') do |
176 begin |
205 begin |
177 WriteLnToConsole('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len))); |
206 WriteLnToConsole('> ' + copy(headcmd^.str, 2, Pred(headcmd^.len))); |
178 AddCaption('> ' + copy(extcmd[cmdcurpos].str, 2, Pred(extcmd[cmdcurpos].len)), $FFFFFF, capgrpNetSay); |
207 AddCaption('> ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), $FFFFFF, capgrpNetSay); |
179 inc(cmdcurpos) |
208 RemoveCmd |
180 end; |
209 end; |
181 |
210 |
182 if cmdcurpos <= cmdendpos then |
211 if (headcmd <> nil) then |
183 TryDo(GameTicks <= extcmd[cmdcurpos].Time, |
212 TryDo(GameTicks <= headcmd^.Time, |
184 'oops, queue error. in buffer: ' + extcmd[cmdcurpos].cmd + |
213 'oops, queue error. in buffer: ' + headcmd^.cmd + |
185 ' (' + inttostr(GameTicks) + ' > ' + |
214 ' (' + inttostr(GameTicks) + ' > ' + |
186 inttostr(extcmd[cmdcurpos].Time) + ')', |
215 inttostr(headcmd^.Time) + ')', |
187 true); |
216 true); |
188 |
217 |
189 tmpflag:= true; |
218 tmpflag:= true; |
190 while (cmdcurpos <= cmdendpos)and(GameTicks = extcmd[cmdcurpos].Time) do |
219 while (headcmd <> nil) and (GameTicks = headcmd^.Time) do |
191 begin |
220 begin |
192 case extcmd[cmdcurpos].cmd of |
221 case headcmd^.cmd of |
193 'L': ParseCommand('+left', true); |
222 'L': ParseCommand('+left', true); |
194 'l': ParseCommand('-left', true); |
223 'l': ParseCommand('-left', true); |
195 'R': ParseCommand('+right', true); |
224 'R': ParseCommand('+right', true); |
196 'r': ParseCommand('-right', true); |
225 'r': ParseCommand('-right', true); |
197 'U': ParseCommand('+up', true); |
226 'U': ParseCommand('+up', true); |
204 'j': ParseCommand('ljump', true); |
233 'j': ParseCommand('ljump', true); |
205 'J': ParseCommand('hjump', true); |
234 'J': ParseCommand('hjump', true); |
206 ',': ParseCommand('skip', true); |
235 ',': ParseCommand('skip', true); |
207 'N': begin |
236 'N': begin |
208 tmpflag:= false; |
237 tmpflag:= false; |
209 {$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(extcmd[cmdcurpos].Time)){$ENDIF} |
238 {$IFDEF DEBUGFILE}AddFileLog('got cmd "N": time '+inttostr(headcmd^.Time)){$ENDIF} |
210 end; |
239 end; |
211 'p': begin |
240 'p': begin |
212 TargetPoint.X:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].X)); |
241 TargetPoint.X:= SmallInt(SDLNet_Read16(@(headcmd^.X))); |
213 TargetPoint.Y:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].Y)); |
242 TargetPoint.Y:= SmallInt(SDLNet_Read16(@(headcmd^.Y))); |
214 ParseCommand('put', true) |
243 ParseCommand('put', true) |
215 end; |
244 end; |
216 'P': begin |
245 'P': begin |
217 CursorPoint.X:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].X) + WorldDx); |
246 CursorPoint.X:= SmallInt(SDLNet_Read16(@(headcmd^.X)) + WorldDx); |
218 CursorPoint.Y:= SmallInt(SDLNet_Read16(@extcmd[cmdcurpos].Y) + WorldDy); |
247 CursorPoint.Y:= SmallInt(SDLNet_Read16(@(headcmd^.Y)) + WorldDy); |
219 end; |
248 end; |
220 '1'..'5': ParseCommand('timer ' + extcmd[cmdcurpos].cmd, true); |
249 '1'..'5': ParseCommand('timer ' + headcmd^.cmd, true); |
221 #128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(extcmd[cmdcurpos].cmd) - 79), true) |
250 #128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(headcmd^.cmd) - 79), true) |
222 end; |
251 end; |
223 inc(cmdcurpos) |
252 RemoveCmd |
224 end; |
253 end; |
225 isInLag:= (cmdcurpos > cmdendpos) and tmpflag |
254 isInLag:= (headcmd = nil) and tmpflag |
226 end; |
255 end; |
227 |
256 |
228 end. |
257 end. |