hedgewars/uConsole.pas
changeset 4373 fe0e3903bb9e
parent 4369 334fc848e051
child 4374 bcefeeabaa33
equal deleted inserted replaced
4372:3836973380b9 4373:fe0e3903bb9e
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uConsole;
    21 unit uConsole;
    22 interface
    22 interface
    23 uses uFloat;
       
    24 
       
    25 var isDeveloperMode: boolean;
       
    26 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
       
    27      TCommandHandler = procedure (var params: shortstring);
       
    28 
    23 
    29 procedure initModule;
    24 procedure initModule;
    30 procedure freeModule;
    25 procedure freeModule;
    31 procedure WriteToConsole(s: shortstring);
    26 procedure WriteToConsole(s: shortstring);
    32 procedure WriteLnToConsole(s: shortstring);
    27 procedure WriteLnToConsole(s: shortstring);
    33 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
       
    34 procedure StopMessages(Message: Longword);
       
    35 function  GetLastConsoleLine: shortstring;
    28 function  GetLastConsoleLine: shortstring;
    36 
    29 
    37 procedure doPut(putX, putY: LongInt; fromAI: boolean);
       
    38 
       
    39 implementation
    30 implementation
    40 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uMobile,
    31 uses uMisc, Types, uVariables;
    41      uRandom, uAmmos, uStats, uChat, SDLh, uSound, uVisualGears, uScript, uTypes,
       
    42      uVariables;
       
    43 
    32 
    44 const cLineWidth: LongInt = 0;
    33 const cLineWidth: LongInt = 0;
    45       cLinesCount = 256;
    34       cLinesCount = 256;
    46 
    35 
    47 type  PVariable = ^TVariable;
    36 type
    48       TVariable = record
       
    49                      Next: PVariable;
       
    50                      Name: string[15];
       
    51                     VType: TVariableType;
       
    52                   Handler: pointer;
       
    53                   Trusted: boolean;
       
    54                   end;
       
    55       TTextLine = record
    37       TTextLine = record
    56                   s: shortstring;
    38                   s: shortstring;
    57                   end;
    39                   end;
    58 
    40 
    59 var   ConsoleLines: array[byte] of TTextLine;
    41 var   ConsoleLines: array[byte] of TTextLine;
    60       CurrLine: LongInt;
    42       CurrLine: LongInt;
    61       Variables: PVariable;
       
    62 
    43 
    63 procedure SetLine(var tl: TTextLine; str: shortstring);
    44 procedure SetLine(var tl: TTextLine; str: shortstring);
    64 begin
    45 begin
    65 with tl do
    46 with tl do
    66      s:= str;
    47      s:= str;
    67 end;
       
    68 
       
    69 function RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
       
    70 var value: PVariable;
       
    71 begin
       
    72 New(value);
       
    73 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
       
    74 FillChar(value^, sizeof(TVariable), 0);
       
    75 value^.Name:= Name;
       
    76 value^.VType:= VType;
       
    77 value^.Handler:= p;
       
    78 value^.Trusted:= Trusted;
       
    79 
       
    80 if Variables = nil then Variables:= value
       
    81                    else begin
       
    82                         value^.Next:= Variables;
       
    83                         Variables:= value
       
    84                         end;
       
    85 
       
    86 RegisterVariable:= value;
       
    87 end;
    48 end;
    88 
    49 
    89 procedure WriteToConsole(s: shortstring);
    50 procedure WriteToConsole(s: shortstring);
    90 var Len: LongInt;
    51 var Len: LongInt;
    91     done: boolean;
    52     done: boolean;
   121     CurrLine:= 0;
    82     CurrLine:= 0;
   122 PByte(@ConsoleLines[CurrLine].s)^:= 0
    83 PByte(@ConsoleLines[CurrLine].s)^:= 0
   123 {$ENDIF}
    84 {$ENDIF}
   124 end;
    85 end;
   125 
    86 
   126 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
       
   127 var ii: LongInt;
       
   128     s: shortstring;
       
   129     t: PVariable;
       
   130     c: char;
       
   131 begin
       
   132 //WriteLnToConsole(CmdStr);
       
   133 if CmdStr[0]=#0 then exit;
       
   134 {$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF}
       
   135 c:= CmdStr[1];
       
   136 if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
       
   137 s:= '';
       
   138 SplitBySpace(CmdStr, s);
       
   139 t:= Variables;
       
   140 while t <> nil do
       
   141       begin
       
   142       if t^.Name = CmdStr then
       
   143          begin
       
   144          if TrustedSource or t^.Trusted then
       
   145             case t^.VType of
       
   146               vtCommand: if c='/' then
       
   147                          begin
       
   148                          TCommandHandler(t^.Handler)(s);
       
   149                          end;
       
   150               vtLongInt: if c='$' then
       
   151                          if s[0]=#0 then
       
   152                             begin
       
   153                             str(PLongInt(t^.Handler)^, s);
       
   154                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
   155                             end else val(s, PLongInt(t^.Handler)^);
       
   156               vthwFloat: if c='$' then
       
   157                          if s[0]=#0 then
       
   158                             begin
       
   159                             //str(PhwFloat(t^.Handler)^:4:6, s);
       
   160                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
   161                             end else; //val(s, PhwFloat(t^.Handler)^, i);
       
   162              vtBoolean: if c='$' then
       
   163                          if s[0]=#0 then
       
   164                             begin
       
   165                             str(ord(boolean(t^.Handler^)), s);
       
   166                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
   167                             end else
       
   168                             begin
       
   169                             val(s, ii);
       
   170                             boolean(t^.Handler^):= not (ii = 0)
       
   171                             end;
       
   172               end;
       
   173          exit
       
   174          end else t:= t^.Next
       
   175       end;
       
   176 case c of
       
   177      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
       
   178      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
       
   179 end;
       
   180 
    87 
   181 function GetLastConsoleLine: shortstring;
    88 function GetLastConsoleLine: shortstring;
   182 var valueStr: shortstring;
    89 var valueStr: shortstring;
   183     i: LongWord;
    90     i: LongWord;
   184 begin
    91 begin
   191 valueStr:= valueStr + ConsoleLines[i].s;
    98 valueStr:= valueStr + ConsoleLines[i].s;
   192 
    99 
   193 GetLastConsoleLine:= valueStr;
   100 GetLastConsoleLine:= valueStr;
   194 end;
   101 end;
   195 
   102 
   196 procedure StopMessages(Message: Longword);
       
   197 begin
       
   198 if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else
       
   199 if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else
       
   200 if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else
       
   201 if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else
       
   202 if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true)
       
   203 end;
       
   204 
       
   205 {$INCLUDE "CCHandlers.inc"}
       
   206 procedure initModule;
   103 procedure initModule;
   207 var i: LongInt;
   104 var i: LongInt;
   208 begin
   105 begin
   209     CurrLine:= 0;
   106     CurrLine:= 0;
   210     Variables:= nil;
       
   211     isDeveloperMode:= true;
       
   212 
   107 
   213     // initConsole
   108     // initConsole
   214     cLineWidth:= cScreenWidth div 10;
   109     cLineWidth:= cScreenWidth div 10;
   215     if cLineWidth > 255 then
   110     if cLineWidth > 255 then
   216         cLineWidth:= 255;
   111         cLineWidth:= 255;
   217     for i:= 0 to Pred(cLinesCount) do
   112     for i:= 0 to Pred(cLinesCount) do
   218         PByte(@ConsoleLines[i])^:= 0;
   113         PByte(@ConsoleLines[i])^:= 0;
   219 
       
   220     // NOTE: please, keep most frequently used commands on bottom
       
   221     RegisterVariable('flag'    , vtCommand, @chFlag         , false);
       
   222     RegisterVariable('script'  , vtCommand, @chScript       , false);
       
   223     RegisterVariable('proto'   , vtCommand, @chCheckProto   , true );
       
   224     RegisterVariable('spectate', vtBoolean, @fastUntilLag   , false);
       
   225     RegisterVariable('capture' , vtCommand, @chCapture      , true );
       
   226     RegisterVariable('rotmask' , vtCommand, @chRotateMask   , true );
       
   227     RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
       
   228     RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
       
   229     RegisterVariable('map'     , vtCommand, @chSetMap       , false);
       
   230     RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
       
   231     RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
       
   232     RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false);
       
   233     RegisterVariable('mapgen'  , vtLongInt, @cMapGen        , false);
       
   234     RegisterVariable('maze_size',vtLongInt, @cMazeSize      , false);
       
   235     RegisterVariable('delay'   , vtLongInt, @cInactDelay    , false);
       
   236     RegisterVariable('ready'   , vtLongInt, @cReadyDelay    , false);
       
   237     RegisterVariable('casefreq', vtLongInt, @cCaseFactor    , false);
       
   238     RegisterVariable('healthprob', vtLongInt, @cHealthCaseProb, false);
       
   239     RegisterVariable('hcaseamount', vtLongInt, @cHealthCaseAmount, false);
       
   240     RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns  , false);
       
   241     RegisterVariable('waterrise', vtLongInt, @cWaterRise    , false);
       
   242     RegisterVariable('healthdec', vtLongInt, @cHealthDecrease, false);
       
   243     RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false);
       
   244     RegisterVariable('minedudpct',vtLongInt,@cMineDudPercent, false);
       
   245     RegisterVariable('minesnum', vtLongInt, @cLandMines     , false);
       
   246     RegisterVariable('explosives',vtLongInt,@cExplosives    , false);
       
   247     RegisterVariable('gmflags' , vtLongInt, @GameFlags      , false);
       
   248     RegisterVariable('trflags' , vtLongInt, @TrainingFlags  , false);
       
   249     RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false);
       
   250     RegisterVariable('minestime',vtLongInt, @cMinesTime     , false);
       
   251     RegisterVariable('fort'    , vtCommand, @chFort         , false);
       
   252     RegisterVariable('voicepack',vtCommand, @chVoicepack    , false);
       
   253     RegisterVariable('grave'   , vtCommand, @chGrave        , false);
       
   254     RegisterVariable('bind'    , vtCommand, @chBind         , true );
       
   255     RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);
       
   256     RegisterVariable('hat'     , vtCommand, @chSetHat       , false);
       
   257     RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
       
   258     RegisterVariable('ammloadt', vtCommand, @chSetAmmoLoadout, false);
       
   259     RegisterVariable('ammdelay', vtCommand, @chSetAmmoDelay, false);
       
   260     RegisterVariable('ammprob',  vtCommand, @chSetAmmoProbability, false);
       
   261     RegisterVariable('ammreinf', vtCommand, @chSetAmmoReinforcement, false);
       
   262     RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
       
   263     RegisterVariable('quit'    , vtCommand, @chQuit         , true );
       
   264     RegisterVariable('confirm' , vtCommand, @chConfirm      , true );
       
   265     RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
       
   266     RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
       
   267     RegisterVariable('zoomin'  , vtCommand, @chZoomIn       , true );
       
   268     RegisterVariable('zoomout' , vtCommand, @chZoomOut      , true );
       
   269     RegisterVariable('zoomreset',vtCommand, @chZoomReset    , true );
       
   270     RegisterVariable('skip'    , vtCommand, @chSkip         , false);
       
   271     RegisterVariable('history' , vtCommand, @chHistory      , true );
       
   272     RegisterVariable('chat'    , vtCommand, @chChat         , true );
       
   273     RegisterVariable('say'     , vtCommand, @chSay          , true );
       
   274     RegisterVariable('hogsay'  , vtCommand, @chHogSay       , true );
       
   275     RegisterVariable('team'    , vtCommand, @chTeamSay      , true );
       
   276     RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , true);
       
   277     RegisterVariable('+precise', vtCommand, @chPrecise_p    , false);
       
   278     RegisterVariable('-precise', vtCommand, @chPrecise_m    , false);
       
   279     RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
       
   280     RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
       
   281     RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
       
   282     RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
       
   283     RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
       
   284     RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
       
   285     RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
       
   286     RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
       
   287     RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
       
   288     RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
       
   289     RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
       
   290     RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
       
   291     RegisterVariable('timer'   , vtCommand, @chTimer        , false);
       
   292     RegisterVariable('taunt'   , vtCommand, @chTaunt        , false);
       
   293     RegisterVariable('setweap' , vtCommand, @chSetWeapon    , false);
       
   294     RegisterVariable('slot'    , vtCommand, @chSlot         , false);
       
   295     RegisterVariable('put'     , vtCommand, @chPut          , false);
       
   296     RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
       
   297     RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
       
   298     RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
       
   299     RegisterVariable('+volup'  , vtCommand, @chVol_p        , true );
       
   300     RegisterVariable('-volup'  , vtCommand, @chVol_m        , true );
       
   301     RegisterVariable('+voldown', vtCommand, @chVol_m        , true );
       
   302     RegisterVariable('-voldown', vtCommand, @chVol_p        , true );
       
   303     RegisterVariable('findhh'  , vtCommand, @chFindhh       , true );
       
   304     RegisterVariable('pause'   , vtCommand, @chPause        , true );
       
   305     RegisterVariable('+cur_u'  , vtCommand, @chCurU_p       , true );
       
   306     RegisterVariable('-cur_u'  , vtCommand, @chCurU_m       , true );
       
   307     RegisterVariable('+cur_d'  , vtCommand, @chCurD_p       , true );
       
   308     RegisterVariable('-cur_d'  , vtCommand, @chCurD_m       , true );
       
   309     RegisterVariable('+cur_l'  , vtCommand, @chCurL_p       , true );
       
   310     RegisterVariable('-cur_l'  , vtCommand, @chCurL_m       , true );
       
   311     RegisterVariable('+cur_r'  , vtCommand, @chCurR_p       , true );
       
   312     RegisterVariable('-cur_r'  , vtCommand, @chCurR_m       , true );
       
   313 end;
   114 end;
   314 
   115 
   315 procedure freeModule;
   116 procedure freeModule;
   316 var t, tt: PVariable;
       
   317 begin
   117 begin
   318     tt:= Variables;
   118 
   319     Variables:= nil;
       
   320     while tt <> nil do
       
   321     begin
       
   322         t:= tt;
       
   323         tt:= tt^.Next;
       
   324         Dispose(t)
       
   325     end;
       
   326 end;
   119 end;
   327 
   120 
   328 end.
   121 end.