hedgewars/uIO.pas
changeset 6580 6155187bf599
parent 6453 11c578d30bd3
child 6700 e04da46ee43c
equal deleted inserted replaced
6579:fc52f7c22c9b 6580:6155187bf599
    69 FillChar(command^, sizeof(TCmd), 0);
    69 FillChar(command^, sizeof(TCmd), 0);
    70 command^.loTime:= Time;
    70 command^.loTime:= Time;
    71 command^.str:= str;
    71 command^.str:= str;
    72 if command^.cmd <> 'F' then dec(command^.len, 2); // cut timestamp
    72 if command^.cmd <> 'F' then dec(command^.len, 2); // cut timestamp
    73 if headcmd = nil then
    73 if headcmd = nil then
    74    begin
    74     begin
    75    headcmd:= command;
    75     headcmd:= command;
    76    lastcmd:= command
    76     lastcmd:= command
    77    end else
    77     end
    78    begin
    78 else
    79    lastcmd^.Next:= command;
    79     begin
    80    lastcmd:= command
    80     lastcmd^.Next:= command;
    81    end;
    81     lastcmd:= command
       
    82     end;
    82 AddCmd:= command;
    83 AddCmd:= command;
    83 end;
    84 end;
    84 
    85 
    85 procedure RemoveCmd;
    86 procedure RemoveCmd;
    86 var tmp: PCmd;
    87 var tmp: PCmd;
    87 begin
    88 begin
    88 TryDo(headcmd <> nil, 'Engine bug: headcmd = nil', true);
    89 TryDo(headcmd <> nil, 'Engine bug: headcmd = nil', true);
    89 tmp:= headcmd;
    90 tmp:= headcmd;
    90 headcmd:= headcmd^.Next;
    91 headcmd:= headcmd^.Next;
    91 if headcmd = nil then lastcmd:= nil;
    92 if headcmd = nil then
       
    93     lastcmd:= nil;
    92 dispose(tmp)
    94 dispose(tmp)
    93 end;
    95 end;
    94 
    96 
    95 procedure InitIPC;
    97 procedure InitIPC;
    96 var ipaddr: TIPAddress;
    98 var ipaddr: TIPAddress;
   143 const ss: shortstring = '';
   145 const ss: shortstring = '';
   144 var i: LongInt;
   146 var i: LongInt;
   145     s: shortstring;
   147     s: shortstring;
   146 begin
   148 begin
   147 if IPCSock = nil then
   149 if IPCSock = nil then
   148    exit;
   150     exit;
   149 
   151 
   150 fds^.numsockets:= 0;
   152 fds^.numsockets:= 0;
   151 SDLNet_AddSocket(fds, IPCSock);
   153 SDLNet_AddSocket(fds, IPCSock);
   152 
   154 
   153 while SDLNet_CheckSockets(fds, 0) > 0 do
   155 while SDLNet_CheckSockets(fds, 0) > 0 do
   160         while (Length(ss) > 1) and (Length(ss) > byte(ss[1])) do
   162         while (Length(ss) > 1) and (Length(ss) > byte(ss[1])) do
   161             begin
   163             begin
   162             ParseIPCCommand(copy(ss, 2, byte(ss[1])));
   164             ParseIPCCommand(copy(ss, 2, byte(ss[1])));
   163             Delete(ss, 1, Succ(byte(ss[1])))
   165             Delete(ss, 1, Succ(byte(ss[1])))
   164             end
   166             end
   165         end else OutError('IPC connection lost', true)
   167         end
       
   168     else
       
   169         OutError('IPC connection lost', true)
   166     end;
   170     end;
   167 end;
   171 end;
   168 
   172 
   169 procedure LoadRecordFromFile(fileName: shortstring);
   173 procedure LoadRecordFromFile(fileName: shortstring);
   170 var f: file;
   174 var f: file;
   213 procedure SendIPC(s: shortstring);
   217 procedure SendIPC(s: shortstring);
   214 begin
   218 begin
   215 if IPCSock <> nil then
   219 if IPCSock <> nil then
   216     begin
   220     begin
   217     SendEmptyPacketTicks:= 0;
   221     SendEmptyPacketTicks:= 0;
   218     if s[0]>#251 then s[0]:= #251;
   222     if s[0]>#251 then
       
   223         s[0]:= #251;
       
   224         
   219     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   225     SDLNet_Write16(GameTicks, @s[Succ(byte(s[0]))]);
   220     AddFileLog('[IPC out] '+ s[1]);
   226     AddFileLog('[IPC out] '+ s[1]);
   221     inc(s[0], 2);
   227     inc(s[0], 2);
   222     SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   228     SDLNet_TCP_Send(IPCSock, @s, Succ(byte(s[0])))
   223     end
   229     end
   224 end;
   230 end;
   225 
   231 
   226 procedure SendIPCRaw(p: pointer; len: Longword);
   232 procedure SendIPCRaw(p: pointer; len: Longword);
   227 begin
   233 begin
   228 if IPCSock <> nil then
   234 if IPCSock <> nil then
   229    begin
   235     begin
   230    SDLNet_TCP_Send(IPCSock, p, len)
   236     SDLNet_TCP_Send(IPCSock, p, len)
   231    end
   237     end
   232 end;
   238 end;
   233 
   239 
   234 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
   240 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
   235 var s: shortstring;
   241 var s: shortstring;
   236 begin
   242 begin
   243 
   249 
   244 procedure IPCWaitPongEvent;
   250 procedure IPCWaitPongEvent;
   245 begin
   251 begin
   246 isPonged:= false;
   252 isPonged:= false;
   247 repeat
   253 repeat
   248    IPCCheckSock;
   254     IPCCheckSock;
   249    SDL_Delay(1)
   255     SDL_Delay(1)
   250 until isPonged
   256 until isPonged
   251 end;
   257 end;
   252 
   258 
   253 procedure SendIPCAndWaitReply(s: shortstring);
   259 procedure SendIPCAndWaitReply(s: shortstring);
   254 begin
   260 begin
   303         'J': ParseCommand('hjump', true);
   309         'J': ParseCommand('hjump', true);
   304         ',': ParseCommand('skip', true);
   310         ',': ParseCommand('skip', true);
   305         'c': begin
   311         'c': begin
   306             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   312             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   307             ParseCommand('gencmd ' + s, true);
   313             ParseCommand('gencmd ' + s, true);
   308             end;
   314              end;
   309         's': begin
   315         's': begin
   310             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   316             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   311             ParseCommand('chatmsg ' + s, true);
   317             ParseCommand('chatmsg ' + s, true);
   312             WriteLnToConsole(s)
   318             WriteLnToConsole(s)
   313             end;
   319              end;
   314         'b': begin
   320         'b': begin
   315             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   321             s:= copy(headcmd^.str, 2, Pred(headcmd^.len));
   316             ParseCommand('chatmsg ' + #4 + s, true);
   322             ParseCommand('chatmsg ' + #4 + s, true);
   317             WriteLnToConsole(s)
   323             WriteLnToConsole(s)
   318             end;
   324              end;
   319 // TODO: deprecate 'F'
   325 // TODO: deprecate 'F'
   320         'F': ParseCommand('teamgone ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), true);
   326         'F': ParseCommand('teamgone ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), true);
   321         'N': begin
   327         'N': begin
   322             tmpflag:= false;
   328             tmpflag:= false;
   323             AddFileLog('got cmd "N": time '+IntToStr(hiTicks shl 16 + headcmd^.loTime))
   329             AddFileLog('got cmd "N": time '+IntToStr(hiTicks shl 16 + headcmd^.loTime))
   324             end;
   330              end;
   325         'p': begin
   331         'p': begin
   326             x16:= SDLNet_Read16(@(headcmd^.X));
   332             x16:= SDLNet_Read16(@(headcmd^.X));
   327             y16:= SDLNet_Read16(@(headcmd^.Y));
   333             y16:= SDLNet_Read16(@(headcmd^.Y));
   328             doPut(x16, y16, false)
   334             doPut(x16, y16, false)
   329             end;
   335              end;
   330         'P': begin
   336         'P': begin
   331             // these are equations solved for CursorPoint
   337             // these are equations solved for CursorPoint
   332             // SDLNet_Read16(@(headcmd^.X)) == CursorPoint.X - WorldDx;
   338             // SDLNet_Read16(@(headcmd^.X)) == CursorPoint.X - WorldDx;
   333             // SDLNet_Read16(@(headcmd^.Y)) == cScreenHeight - CursorPoint.Y - WorldDy;
   339             // SDLNet_Read16(@(headcmd^.Y)) == cScreenHeight - CursorPoint.Y - WorldDy;
   334             if not (CurrentTeam^.ExtDriven and bShowAmmoMenu) then
   340             if not (CurrentTeam^.ExtDriven and bShowAmmoMenu) then
   335                begin
   341                begin
   336                CursorPoint.X:= SmallInt(SDLNet_Read16(@(headcmd^.X))) + WorldDx;
   342                CursorPoint.X:= SmallInt(SDLNet_Read16(@(headcmd^.X))) + WorldDx;
   337                CursorPoint.Y:= cScreenHeight - SmallInt(SDLNet_Read16(@(headcmd^.Y))) - WorldDy
   343                CursorPoint.Y:= cScreenHeight - SmallInt(SDLNet_Read16(@(headcmd^.Y))) - WorldDy
   338                end
   344                end
   339             end;
   345              end;
   340         'w': ParseCommand('setweap ' + headcmd^.str[2], true);
   346         'w': ParseCommand('setweap ' + headcmd^.str[2], true);
   341         't': ParseCommand('taunt ' + headcmd^.str[2], true);
   347         't': ParseCommand('taunt ' + headcmd^.str[2], true);
   342         'h': ParseCommand('hogsay ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), true);
   348         'h': ParseCommand('hogsay ' + copy(headcmd^.str, 2, Pred(headcmd^.len)), true);
   343         '1'..'5': ParseCommand('timer ' + headcmd^.cmd, true);
   349         '1'..'5': ParseCommand('timer ' + headcmd^.cmd, true);
   344         #128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(headcmd^.cmd) - 79), true)
   350         #128..char(128 + cMaxSlotIndex): ParseCommand('slot ' + char(byte(headcmd^.cmd) - 79), true)
   365     SendIPC('E' + s);
   371     SendIPC('E' + s);
   366 end;
   372 end;
   367 
   373 
   368 procedure doPut(putX, putY: LongInt; fromAI: boolean);
   374 procedure doPut(putX, putY: LongInt; fromAI: boolean);
   369 begin
   375 begin
   370 if CheckNoTeamOrHH or isPaused then exit;
   376 if CheckNoTeamOrHH or isPaused then
       
   377     exit;
   371 bShowFinger:= false;
   378 bShowFinger:= false;
   372 if not CurrentTeam^.ExtDriven and bShowAmmoMenu then
   379 if not CurrentTeam^.ExtDriven and bShowAmmoMenu then
   373     begin
   380     begin
   374     bSelected:= true;
   381     bSelected:= true;
   375     exit
   382     exit
   384             begin
   391             begin
   385             if fromAI then
   392             if fromAI then
   386                 begin
   393                 begin
   387                 TargetPoint.X:= putX;
   394                 TargetPoint.X:= putX;
   388                 TargetPoint.Y:= putY
   395                 TargetPoint.Y:= putY
   389                 end else
   396                 end
       
   397             else
   390                 begin
   398                 begin
   391                 TargetPoint.X:= CursorPoint.X - WorldDx;
   399                 TargetPoint.X:= CursorPoint.X - WorldDx;
   392                 TargetPoint.Y:= cScreenHeight - CursorPoint.Y - WorldDy;
   400                 TargetPoint.Y:= cScreenHeight - CursorPoint.Y - WorldDy;
   393                 end;
   401                 end;
   394             SendIPCXY('p', TargetPoint.X, TargetPoint.Y);
   402             SendIPCXY('p', TargetPoint.X, TargetPoint.Y);