hedgewars/CCHandlers.inc
changeset 4398 36d7e4b6ca81
parent 4389 d1c65b60cd68
child 4401 9cb6990af584
equal deleted inserted replaced
4397:ab577db125c4 4398:36d7e4b6ca81
    14 * You should have received a copy of the GNU General Public License
    14 * You should have received a copy of the GNU General Public License
    15 * along with this program; if not, write to the Free Software
    15 * along with this program; if not, write to the Free Software
    16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17 *)
    17 *)
    18 
    18 
    19 function CheckNoTeamOrHH: boolean;
       
    20 var bRes: boolean;
       
    21 begin
       
    22 bRes:= (CurrentTeam = nil) or (CurrentHedgehog^.Gear = nil);
       
    23 {$IFDEF DEBUGFILE}
       
    24 if bRes then
       
    25 if CurrentTeam = nil then AddFileLog('CONSOLE: CurTeam = nil')
       
    26                         else AddFileLog('CONSOLE: CurTeam <> nil, Gear = nil');
       
    27 {$ENDIF}
       
    28 CheckNoTeamOrHH:= bRes;
       
    29 end;
       
    30 ////////////////////////////////////////////////////////////////////////////////
    19 ////////////////////////////////////////////////////////////////////////////////
    31 procedure chQuit(var s: shortstring);
    20 procedure chQuit(var s: shortstring);
    32 const prevGState: TGameState = gsConfirm;
    21 const prevGState: TGameState = gsConfirm;
    33 begin
    22 begin
    34 s:= s; // avoid compiler hint
    23 s:= s; // avoid compiler hint
    71 TryDo(i <= cNetProtoVersion, 'Protocol version mismatch: engine is too old', true);
    60 TryDo(i <= cNetProtoVersion, 'Protocol version mismatch: engine is too old', true);
    72 TryDo(i >= cNetProtoVersion, 'Protocol version mismatch: engine is too new', true)
    61 TryDo(i >= cNetProtoVersion, 'Protocol version mismatch: engine is too new', true)
    73 end
    62 end
    74 end;
    63 end;
    75 
    64 
    76 procedure chAddTeam(var s: shortstring);
       
    77 var Color: Longword;
       
    78     ts, cs: shortstring;
       
    79 begin
       
    80 cs:= '';
       
    81 ts:= '';
       
    82 if isDeveloperMode then
       
    83 begin
       
    84 SplitBySpace(s, cs);
       
    85 SplitBySpace(cs, ts);
       
    86 val(cs, Color);
       
    87 TryDo(Color <> 0, 'Error: black team color', true);
       
    88 
       
    89 // color is always little endian so the mask must be constant also in big endian archs
       
    90 Color:= Color or $FF000000;
       
    91     
       
    92 AddTeam(Color);
       
    93 CurrentTeam^.TeamName:= ts;
       
    94 CurrentTeam^.PlayerHash:= s;
       
    95 if GameType in [gmtDemo, gmtSave] then CurrentTeam^.ExtDriven:= true;
       
    96 
       
    97 CurrentTeam^.voicepack:= AskForVoicepack('Default')
       
    98 end
       
    99 end;
       
   100 
       
   101 procedure chTeamLocal(var s: shortstring);
    65 procedure chTeamLocal(var s: shortstring);
   102 begin
    66 begin
   103 s:= s; // avoid compiler hint
    67 s:= s; // avoid compiler hint
   104 if not isDeveloperMode then exit;
    68 if not isDeveloperMode then exit;
   105 if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/rdriven"', true);
    69 if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/rdriven"', true);
   120 if s[1]='"' then Delete(s, 1, 1);
    84 if s[1]='"' then Delete(s, 1, 1);
   121 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
    85 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
   122 CurrentTeam^.FortName:= s
    86 CurrentTeam^.FortName:= s
   123 end;
    87 end;
   124 
    88 
   125 procedure chVoicepack(var s: shortstring);
       
   126 begin
       
   127 if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/voicepack"', true);
       
   128 if s[1]='"' then Delete(s, 1, 1);
       
   129 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
       
   130 CurrentTeam^.voicepack:= AskForVoicepack(s)
       
   131 end;
       
   132 
       
   133 procedure chFlag(var s: shortstring);
    89 procedure chFlag(var s: shortstring);
   134 begin
    90 begin
   135 if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/flag"', true);
    91 if CurrentTeam = nil then OutError(errmsgIncorrectUse + ' "/flag"', true);
   136 if s[1]='"' then Delete(s, 1, 1);
    92 if s[1]='"' then Delete(s, 1, 1);
   137 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
    93 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
   141 procedure chScript(var s: shortstring);
    97 procedure chScript(var s: shortstring);
   142 begin
    98 begin
   143 if s[1]='"' then Delete(s, 1, 1);
    99 if s[1]='"' then Delete(s, 1, 1);
   144 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
   100 if s[byte(s[0])]='"' then Delete(s, byte(s[0]), 1);
   145 ScriptLoad(s)
   101 ScriptLoad(s)
   146 end;
       
   147 
       
   148 procedure chAddHH(var id: shortstring);
       
   149 var s: shortstring;
       
   150     Gear: PGear;
       
   151 begin
       
   152 s:= '';
       
   153 if (not isDeveloperMode) or (CurrentTeam = nil) then exit;
       
   154 with CurrentTeam^ do
       
   155     begin
       
   156     SplitBySpace(id, s);
       
   157     CurrentHedgehog:= @Hedgehogs[HedgehogsNumber];
       
   158     val(id, CurrentHedgehog^.BotLevel);
       
   159     Gear:= AddGear(0, 0, gtHedgehog, 0, _0, _0, 0);
       
   160     SplitBySpace(s, id);
       
   161     val(s, Gear^.Health);
       
   162     TryDo(Gear^.Health > 0, 'Invalid hedgehog health', true);
       
   163     Gear^.Hedgehog^.Team:= CurrentTeam;
       
   164     if (GameFlags and gfSharedAmmo) <> 0 then CurrentHedgehog^.AmmoStore:= Clan^.ClanIndex
       
   165     else if (GameFlags and gfPerHogAmmo) <> 0 then
       
   166         begin
       
   167         AddAmmoStore;
       
   168         CurrentHedgehog^.AmmoStore:= StoreCnt - 1
       
   169         end
       
   170     else CurrentHedgehog^.AmmoStore:= TeamsCount - 1;
       
   171     CurrentHedgehog^.Gear:= Gear;
       
   172     CurrentHedgehog^.Name:= id;
       
   173     CurrentHedgehog^.InitialHealth:= Gear^.Health;
       
   174     CurrHedgehog:= HedgehogsNumber;
       
   175     inc(HedgehogsNumber)
       
   176     end
       
   177 end;
   102 end;
   178 
   103 
   179 procedure chSetHat(var s: shortstring);
   104 procedure chSetHat(var s: shortstring);
   180 begin
   105 begin
   181 if (not isDeveloperMode) or (CurrentTeam = nil) then exit;
   106 if (not isDeveloperMode) or (CurrentTeam = nil) then exit;
   189     else
   114     else
   190         CurrentHedgehog^.Hat:= s
   115         CurrentHedgehog^.Hat:= s
   191     end;
   116     end;
   192 end;
   117 end;
   193 
   118 
   194 procedure chSetHHCoords(var x: shortstring);
       
   195 var y: shortstring;
       
   196     t: Longint;
       
   197 begin
       
   198 y:= '';
       
   199 if (not isDeveloperMode) or (CurrentHedgehog = nil) or (CurrentHedgehog^.Gear = nil) then exit;
       
   200 SplitBySpace(x, y);
       
   201 val(x, t);
       
   202 CurrentHedgehog^.Gear^.X:= int2hwFloat(t);
       
   203 val(y, t);
       
   204 CurrentHedgehog^.Gear^.Y:= int2hwFloat(t)
       
   205 end;
       
   206 
       
   207 procedure chSetAmmoLoadout(var descr: shortstring);
   119 procedure chSetAmmoLoadout(var descr: shortstring);
   208 begin
   120 begin
   209 SetAmmoLoadout(descr)
   121 SetAmmoLoadout(descr)
   210 end;
   122 end;
   211 
   123 
   544 with CurrentHedgehog^.Gear^ do
   456 with CurrentHedgehog^.Gear^ do
   545     begin
   457     begin
   546     Message:= Message or gmAnimate;
   458     Message:= Message or gmAnimate;
   547     MsgParam:= byte(s[1])
   459     MsgParam:= byte(s[1])
   548     end
   460     end
   549 end;
       
   550 
       
   551 procedure chHogSay(var s: shortstring);
       
   552 var Gear: PVisualGear;
       
   553     text: shortstring;
       
   554 begin
       
   555 text:= copy(s, 2, Length(s)-1);
       
   556 if CheckNoTeamOrHH
       
   557 or ((CurrentHedgehog^.Gear^.State and gstHHDriven) = 0) then
       
   558     begin
       
   559     chSay(text);
       
   560     exit
       
   561     end;
       
   562 
       
   563 if not CurrentTeam^.ExtDriven then SendIPC('h' + s);
       
   564 
       
   565 if byte(s[1]) < 4 then
       
   566     begin
       
   567     Gear:= AddVisualGear(0, 0, vgtSpeechBubble);
       
   568     if Gear <> nil then
       
   569     begin
       
   570     Gear^.Hedgehog:= CurrentHedgehog;
       
   571     Gear^.Text:= text;
       
   572     Gear^.FrameTicks:= byte(s[1])
       
   573     end
       
   574     end
       
   575 else
       
   576     begin
       
   577     SpeechType:= byte(s[1])-3;
       
   578     SpeechText:= text
       
   579     end;
       
   580 
       
   581 end;
   461 end;
   582 
   462 
   583 procedure doPut(putX, putY: LongInt; fromAI: boolean);
   463 procedure doPut(putX, putY: LongInt; fromAI: boolean);
   584 begin
   464 begin
   585 if CheckNoTeamOrHH or isPaused then exit;
   465 if CheckNoTeamOrHH or isPaused then exit;
   634 begin
   514 begin
   635 s:= s; // avoid compiler hint
   515 s:= s; // avoid compiler hint
   636 flagMakeCapture:= true
   516 flagMakeCapture:= true
   637 end;
   517 end;
   638 
   518 
   639 procedure chSkip(var s: shortstring);
       
   640 begin
       
   641 s:= s; // avoid compiler hint
       
   642 if not CurrentTeam^.ExtDriven then SendIPC(',');
       
   643 uStats.Skipped;
       
   644 skipFlag:= true
       
   645 end;
       
   646 
       
   647 procedure chSetMap(var s: shortstring);
   519 procedure chSetMap(var s: shortstring);
   648 begin
   520 begin
   649 if isDeveloperMode then
   521 if isDeveloperMode then
   650 begin
   522 begin
   651 Pathz[ptMapCurrent]:= Pathz[ptMaps] + '/' + s;
   523 Pathz[ptMapCurrent]:= Pathz[ptMaps] + '/' + s;
   691             end;
   563             end;
   692     if ReadyTimeLeft > 1 then ReadyTimeLeft:= 1
   564     if ReadyTimeLeft > 1 then ReadyTimeLeft:= 1
   693     end
   565     end
   694 end;
   566 end;
   695 
   567 
   696 procedure chFullScr(var s: shortstring);
       
   697 var flags: Longword = 0;
       
   698     ico: PSDL_Surface;
       
   699 {$IFDEF DEBUGFILE}
       
   700     buf: array[byte] of char;
       
   701 {$ENDIF}
       
   702 begin
       
   703     s:= s; // avoid compiler hint
       
   704     if Length(s) = 0 then cFullScreen:= not cFullScreen
       
   705     else cFullScreen:= s = '1';
       
   706 
       
   707 {$IFDEF DEBUGFILE}
       
   708     buf[0]:= char(0); // avoid compiler hint
       
   709     AddFileLog('Prepare to change video parameters...');
       
   710 {$ENDIF}
       
   711 
       
   712     flags:= SDL_OPENGL;// or SDL_RESIZABLE;
       
   713 
       
   714     if cFullScreen then
       
   715         flags:= flags or SDL_FULLSCREEN;
       
   716 
       
   717 {$IFDEF SDL_IMAGE_NEWER}
       
   718     WriteToConsole('Init SDL_image... ');
       
   719     SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
       
   720     WriteLnToConsole(msgOK);
       
   721 {$ENDIF}
       
   722     // load engine icon
       
   723 {$IFDEF DARWIN}
       
   724     ico:= LoadImage(Pathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps);
       
   725 {$ELSE}
       
   726     ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps);
       
   727 {$ENDIF}
       
   728     if ico <> nil then
       
   729     begin
       
   730         SDL_WM_SetIcon(ico, 0);
       
   731         SDL_FreeSurface(ico)
       
   732     end;
       
   733     
       
   734     // set window caption
       
   735     SDL_WM_SetCaption('Hedgewars', nil);
       
   736     
       
   737     if SDLPrimSurface <> nil then
       
   738     begin
       
   739 {$IFDEF DEBUGFILE}
       
   740         AddFileLog('Freeing old primary surface...');
       
   741 {$ENDIF}
       
   742         SDL_FreeSurface(SDLPrimSurface);
       
   743         SDLPrimSurface:= nil;
       
   744     end;
       
   745     
       
   746 {$IFDEF SDL13}
       
   747     if SDLwindow = nil then
       
   748     begin
       
   749         SDLwindow:= SDL_CreateWindow('Hedgewars', 0, 0, cScreenWidth, cScreenHeight,
       
   750                         SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN    
       
   751                         {$IFDEF IPHONEOS} or SDL_WINDOW_BORDERLESS{$ENDIF});     
       
   752         SDL_CreateRenderer(SDLwindow, -1, 0);
       
   753     end;
       
   754     
       
   755     SDL_SetRenderDrawColor(0, 0, 0, 255);    
       
   756     SDL_RenderFill(nil);     
       
   757     SDL_RenderPresent();
       
   758 {$ELSE}
       
   759     SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, cBits, flags);
       
   760     SDLTry(SDLPrimSurface <> nil, true);
       
   761     PixelFormat:= SDLPrimSurface^.format;
       
   762 {$ENDIF}
       
   763 
       
   764 {$IFDEF DEBUGFILE}
       
   765     AddFileLog('Setting up OpenGL...');
       
   766     AddFileLog('SDL video driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))));
       
   767 {$ENDIF}
       
   768     SetupOpenGL();
       
   769 end;
       
   770 
       
   771 procedure chVol_p(var s: shortstring);
   568 procedure chVol_p(var s: shortstring);
   772 begin
   569 begin
   773 s:= s; // avoid compiler hint
   570 s:= s; // avoid compiler hint
   774 inc(cVolumeDelta, 3)
   571 inc(cVolumeDelta, 3)
   775 end;
   572 end;
   846 begin
   643 begin
   847     s:= s; // avoid compiler hint
   644     s:= s; // avoid compiler hint
   848     uChat.showAll:= not uChat.showAll
   645     uChat.showAll:= not uChat.showAll
   849 end;
   646 end;
   850 
   647 
   851 procedure chLandCheck(var s: shortstring);
       
   852 begin
       
   853 {$IFDEF DEBUGFILE}
       
   854     AddFileLog('CheckLandDigest: ' + s + ' digest : ' + digest);
       
   855 {$ENDIF}
       
   856     if digest = '' then
       
   857         digest:= s
       
   858     else
       
   859         TryDo(s = digest, 'Different maps generated, sorry', true);
       
   860 end;
       
   861 
       
   862 procedure chSendLandDigest(var s: shortstring);
       
   863 var adler, i: LongInt;
       
   864 begin
       
   865     adler:= 1;
       
   866     for i:= 0 to LAND_HEIGHT-1 do
       
   867         Adler32Update(adler, @Land[i,0], LAND_WIDTH);
       
   868     s:= 'M' + IntToStr(adler);
       
   869 
       
   870     chLandCheck(s);
       
   871     SendIPCRaw(@s[0], Length(s) + 1)
       
   872 end;