hedgewars/uCommands.pas
changeset 4373 fe0e3903bb9e
child 4374 bcefeeabaa33
equal deleted inserted replaced
4372:3836973380b9 4373:fe0e3903bb9e
       
     1 {$INCLUDE "options.inc"}
       
     2 
       
     3 unit uCommands;
       
     4 
       
     5 interface
       
     6 
       
     7 var isDeveloperMode: boolean;
       
     8 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
       
     9      TCommandHandler = procedure (var params: shortstring);
       
    10 
       
    11 procedure initModule;
       
    12 procedure freeModule;
       
    13 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
       
    14 procedure StopMessages(Message: Longword);
       
    15 procedure doPut(putX, putY: LongInt; fromAI: boolean);
       
    16 
       
    17 implementation
       
    18 uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uMobile,
       
    19      uRandom, uAmmos, uStats, uChat, SDLh, uSound, uVisualGears, uScript, uTypes,
       
    20      uVariables, uConsole, uFloat;
       
    21 
       
    22 type  PVariable = ^TVariable;
       
    23       TVariable = record
       
    24                      Next: PVariable;
       
    25                      Name: string[15];
       
    26                     VType: TVariableType;
       
    27                   Handler: pointer;
       
    28                   Trusted: boolean;
       
    29                   end;
       
    30 
       
    31 var
       
    32       Variables: PVariable;
       
    33 
       
    34 function RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean): PVariable;
       
    35 var value: PVariable;
       
    36 begin
       
    37 New(value);
       
    38 TryDo(value <> nil, 'RegisterVariable: value = nil', true);
       
    39 FillChar(value^, sizeof(TVariable), 0);
       
    40 value^.Name:= Name;
       
    41 value^.VType:= VType;
       
    42 value^.Handler:= p;
       
    43 value^.Trusted:= Trusted;
       
    44 
       
    45 if Variables = nil then Variables:= value
       
    46                    else begin
       
    47                         value^.Next:= Variables;
       
    48                         Variables:= value
       
    49                         end;
       
    50 
       
    51 RegisterVariable:= value;
       
    52 end;
       
    53 
       
    54 
       
    55 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
       
    56 var ii: LongInt;
       
    57     s: shortstring;
       
    58     t: PVariable;
       
    59     c: char;
       
    60 begin
       
    61 //WriteLnToConsole(CmdStr);
       
    62 if CmdStr[0]=#0 then exit;
       
    63 {$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF}
       
    64 c:= CmdStr[1];
       
    65 if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
       
    66 s:= '';
       
    67 SplitBySpace(CmdStr, s);
       
    68 t:= Variables;
       
    69 while t <> nil do
       
    70       begin
       
    71       if t^.Name = CmdStr then
       
    72          begin
       
    73          if TrustedSource or t^.Trusted then
       
    74             case t^.VType of
       
    75               vtCommand: if c='/' then
       
    76                          begin
       
    77                          TCommandHandler(t^.Handler)(s);
       
    78                          end;
       
    79               vtLongInt: if c='$' then
       
    80                          if s[0]=#0 then
       
    81                             begin
       
    82                             str(PLongInt(t^.Handler)^, s);
       
    83                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
    84                             end else val(s, PLongInt(t^.Handler)^);
       
    85               vthwFloat: if c='$' then
       
    86                          if s[0]=#0 then
       
    87                             begin
       
    88                             //str(PhwFloat(t^.Handler)^:4:6, s);
       
    89                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
    90                             end else; //val(s, PhwFloat(t^.Handler)^, i);
       
    91              vtBoolean: if c='$' then
       
    92                          if s[0]=#0 then
       
    93                             begin
       
    94                             str(ord(boolean(t^.Handler^)), s);
       
    95                             WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
       
    96                             end else
       
    97                             begin
       
    98                             val(s, ii);
       
    99                             boolean(t^.Handler^):= not (ii = 0)
       
   100                             end;
       
   101               end;
       
   102          exit
       
   103          end else t:= t^.Next
       
   104       end;
       
   105 case c of
       
   106      '$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
       
   107      else WriteLnToConsole(errmsgUnknownCommand  + ': "/' + CmdStr + '"') end
       
   108 end;
       
   109 
       
   110 
       
   111 procedure StopMessages(Message: Longword);
       
   112 begin
       
   113 if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else
       
   114 if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else
       
   115 if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else
       
   116 if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else
       
   117 if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true)
       
   118 end;
       
   119 
       
   120 {$INCLUDE "CCHandlers.inc"}
       
   121 
       
   122 procedure initModule;
       
   123 begin
       
   124     Variables:= nil;
       
   125     isDeveloperMode:= true;
       
   126 
       
   127     // NOTE: please, keep most frequently used commands on bottom
       
   128     RegisterVariable('flag'    , vtCommand, @chFlag         , false);
       
   129     RegisterVariable('script'  , vtCommand, @chScript       , false);
       
   130     RegisterVariable('proto'   , vtCommand, @chCheckProto   , true );
       
   131     RegisterVariable('spectate', vtBoolean, @fastUntilLag   , false);
       
   132     RegisterVariable('capture' , vtCommand, @chCapture      , true );
       
   133     RegisterVariable('rotmask' , vtCommand, @chRotateMask   , true );
       
   134     RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
       
   135     RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
       
   136     RegisterVariable('map'     , vtCommand, @chSetMap       , false);
       
   137     RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
       
   138     RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
       
   139     RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false);
       
   140     RegisterVariable('mapgen'  , vtLongInt, @cMapGen        , false);
       
   141     RegisterVariable('maze_size',vtLongInt, @cMazeSize      , false);
       
   142     RegisterVariable('delay'   , vtLongInt, @cInactDelay    , false);
       
   143     RegisterVariable('ready'   , vtLongInt, @cReadyDelay    , false);
       
   144     RegisterVariable('casefreq', vtLongInt, @cCaseFactor    , false);
       
   145     RegisterVariable('healthprob', vtLongInt, @cHealthCaseProb, false);
       
   146     RegisterVariable('hcaseamount', vtLongInt, @cHealthCaseAmount, false);
       
   147     RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns  , false);
       
   148     RegisterVariable('waterrise', vtLongInt, @cWaterRise    , false);
       
   149     RegisterVariable('healthdec', vtLongInt, @cHealthDecrease, false);
       
   150     RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false);
       
   151     RegisterVariable('minedudpct',vtLongInt,@cMineDudPercent, false);
       
   152     RegisterVariable('minesnum', vtLongInt, @cLandMines     , false);
       
   153     RegisterVariable('explosives',vtLongInt,@cExplosives    , false);
       
   154     RegisterVariable('gmflags' , vtLongInt, @GameFlags      , false);
       
   155     RegisterVariable('trflags' , vtLongInt, @TrainingFlags  , false);
       
   156     RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false);
       
   157     RegisterVariable('minestime',vtLongInt, @cMinesTime     , false);
       
   158     RegisterVariable('fort'    , vtCommand, @chFort         , false);
       
   159     RegisterVariable('voicepack',vtCommand, @chVoicepack    , false);
       
   160     RegisterVariable('grave'   , vtCommand, @chGrave        , false);
       
   161     RegisterVariable('bind'    , vtCommand, @chBind         , true );
       
   162     RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);
       
   163     RegisterVariable('hat'     , vtCommand, @chSetHat       , false);
       
   164     RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
       
   165     RegisterVariable('ammloadt', vtCommand, @chSetAmmoLoadout, false);
       
   166     RegisterVariable('ammdelay', vtCommand, @chSetAmmoDelay, false);
       
   167     RegisterVariable('ammprob',  vtCommand, @chSetAmmoProbability, false);
       
   168     RegisterVariable('ammreinf', vtCommand, @chSetAmmoReinforcement, false);
       
   169     RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
       
   170     RegisterVariable('quit'    , vtCommand, @chQuit         , true );
       
   171     RegisterVariable('confirm' , vtCommand, @chConfirm      , true );
       
   172     RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
       
   173     RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
       
   174     RegisterVariable('zoomin'  , vtCommand, @chZoomIn       , true );
       
   175     RegisterVariable('zoomout' , vtCommand, @chZoomOut      , true );
       
   176     RegisterVariable('zoomreset',vtCommand, @chZoomReset    , true );
       
   177     RegisterVariable('skip'    , vtCommand, @chSkip         , false);
       
   178     RegisterVariable('history' , vtCommand, @chHistory      , true );
       
   179     RegisterVariable('chat'    , vtCommand, @chChat         , true );
       
   180     RegisterVariable('say'     , vtCommand, @chSay          , true );
       
   181     RegisterVariable('hogsay'  , vtCommand, @chHogSay       , true );
       
   182     RegisterVariable('team'    , vtCommand, @chTeamSay      , true );
       
   183     RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , true);
       
   184     RegisterVariable('+precise', vtCommand, @chPrecise_p    , false);
       
   185     RegisterVariable('-precise', vtCommand, @chPrecise_m    , false);
       
   186     RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
       
   187     RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
       
   188     RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
       
   189     RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
       
   190     RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
       
   191     RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
       
   192     RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
       
   193     RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
       
   194     RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
       
   195     RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
       
   196     RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
       
   197     RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
       
   198     RegisterVariable('timer'   , vtCommand, @chTimer        , false);
       
   199     RegisterVariable('taunt'   , vtCommand, @chTaunt        , false);
       
   200     RegisterVariable('setweap' , vtCommand, @chSetWeapon    , false);
       
   201     RegisterVariable('slot'    , vtCommand, @chSlot         , false);
       
   202     RegisterVariable('put'     , vtCommand, @chPut          , false);
       
   203     RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
       
   204     RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
       
   205     RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
       
   206     RegisterVariable('+volup'  , vtCommand, @chVol_p        , true );
       
   207     RegisterVariable('-volup'  , vtCommand, @chVol_m        , true );
       
   208     RegisterVariable('+voldown', vtCommand, @chVol_m        , true );
       
   209     RegisterVariable('-voldown', vtCommand, @chVol_p        , true );
       
   210     RegisterVariable('findhh'  , vtCommand, @chFindhh       , true );
       
   211     RegisterVariable('pause'   , vtCommand, @chPause        , true );
       
   212     RegisterVariable('+cur_u'  , vtCommand, @chCurU_p       , true );
       
   213     RegisterVariable('-cur_u'  , vtCommand, @chCurU_m       , true );
       
   214     RegisterVariable('+cur_d'  , vtCommand, @chCurD_p       , true );
       
   215     RegisterVariable('-cur_d'  , vtCommand, @chCurD_m       , true );
       
   216     RegisterVariable('+cur_l'  , vtCommand, @chCurL_p       , true );
       
   217     RegisterVariable('-cur_l'  , vtCommand, @chCurL_m       , true );
       
   218     RegisterVariable('+cur_r'  , vtCommand, @chCurR_p       , true );
       
   219     RegisterVariable('-cur_r'  , vtCommand, @chCurR_m       , true );
       
   220 end;
       
   221 
       
   222 procedure freeModule;
       
   223 var t, tt: PVariable;
       
   224 begin
       
   225     tt:= Variables;
       
   226     Variables:= nil;
       
   227     while tt <> nil do
       
   228     begin
       
   229         t:= tt;
       
   230         tt:= tt^.Next;
       
   231         Dispose(t)
       
   232     end;
       
   233 end;
       
   234 
       
   235 end.