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