hedgewars/uConsole.pas
changeset 2699 249adefa9c1c
parent 2695 ed789a7ef68d
child 2747 7889a3a9724f
equal deleted inserted replaced
2698:90585aba87ad 2699:249adefa9c1c
    20 
    20 
    21 unit uConsole;
    21 unit uConsole;
    22 interface
    22 interface
    23 uses uFloat;
    23 uses uFloat;
    24 
    24 
    25 const isDeveloperMode: boolean = true;
    25 var isDeveloperMode: boolean;
    26 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
    26 type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean);
    27      TCommandHandler = procedure (var params: shortstring);
    27      TCommandHandler = procedure (var params: shortstring);
    28 
    28 
       
    29 procedure init_uConsole;
       
    30 procedure free_uConsole;
    29 procedure WriteToConsole(s: shortstring);
    31 procedure WriteToConsole(s: shortstring);
    30 procedure WriteLnToConsole(s: shortstring);
    32 procedure WriteLnToConsole(s: shortstring);
    31 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    33 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
    32 procedure StopMessages(Message: Longword);
    34 procedure StopMessages(Message: Longword);
    33 function  GetLastConsoleLine: shortstring;
    35 function  GetLastConsoleLine: shortstring;
    53       TTextLine = record
    55       TTextLine = record
    54                   s: shortstring;
    56                   s: shortstring;
    55                   end;
    57                   end;
    56 
    58 
    57 var   ConsoleLines: array[byte] of TTextLine;
    59 var   ConsoleLines: array[byte] of TTextLine;
    58       CurrLine: LongInt = 0;
    60       CurrLine: LongInt;
    59       Variables: PVariable = nil;
    61       Variables: PVariable;
    60 
    62 
    61 procedure SetLine(var tl: TTextLine; str: shortstring);
    63 procedure SetLine(var tl: TTextLine; str: shortstring);
    62 begin
    64 begin
    63 with tl do
    65 with tl do
    64      s:= str;
    66      s:= str;
   110 	end else b:= '';
   112 	end else b:= '';
   111 end;
   113 end;
   112 
   114 
   113 procedure WriteToConsole(s: shortstring);
   115 procedure WriteToConsole(s: shortstring);
   114 var Len: LongInt;
   116 var Len: LongInt;
   115 begin
   117     done: boolean;
   116 {$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF}
   118 begin
   117 Write(s);
   119 	{$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF}
   118 repeat
   120 	Write(s);
   119 Len:= cLineWidth - Length(ConsoleLines[CurrLine].s);
   121 	done:= false;
   120 SetLine(ConsoleLines[CurrLine], ConsoleLines[CurrLine].s + copy(s, 1, Len));
   122 	
   121 Delete(s, 1, Len);
   123 	while not done do
   122 if byte(ConsoleLines[CurrLine].s[0]) = cLineWidth then
   124 	begin
   123    begin
   125 		Len:= cLineWidth - Length(ConsoleLines[CurrLine].s);
   124    inc(CurrLine);
   126 		SetLine(ConsoleLines[CurrLine], ConsoleLines[CurrLine].s + copy(s, 1, Len));
   125    if CurrLine = cLinesCount then CurrLine:= 0;
   127 		Delete(s, 1, Len);
   126    PByte(@ConsoleLines[CurrLine].s)^:= 0
   128 		if byte(ConsoleLines[CurrLine].s[0]) = cLineWidth then
   127    end;
   129 		begin
   128 until Length(s) = 0
   130 			inc(CurrLine);
       
   131 			if CurrLine = cLinesCount then CurrLine:= 0;
       
   132 			PByte(@ConsoleLines[CurrLine].s)^:= 0
       
   133 		end;
       
   134 		done:= (Length(s) = 0);
       
   135 	end;
   129 end;
   136 end;
   130 
   137 
   131 procedure WriteLnToConsole(s: shortstring);
   138 procedure WriteLnToConsole(s: shortstring);
   132 begin
   139 begin
   133 WriteToConsole(s);
   140 	WriteToConsole(s);
   134 WriteLn;
   141 	WriteLn;
   135 inc(CurrLine);
   142 	inc(CurrLine);
   136 if CurrLine = cLinesCount then CurrLine:= 0;
   143 	if CurrLine = cLinesCount then
   137 PByte(@ConsoleLines[CurrLine].s)^:= 0
   144 		CurrLine:= 0;
   138 end;
   145 	PByte(@ConsoleLines[CurrLine].s)^:= 0
   139 
       
   140 procedure InitConsole;
       
   141 var i: LongInt;
       
   142 begin
       
   143 cLineWidth:= cScreenWidth div 10;
       
   144 if cLineWidth > 255 then cLineWidth:= 255;
       
   145 for i:= 0 to Pred(cLinesCount) do PByte(@ConsoleLines[i])^:= 0
       
   146 end;
   146 end;
   147 
   147 
   148 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
   148 procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean);
   149 type PhwFloat = ^hwFloat;
   149 type PhwFloat = ^hwFloat;
   150 var ii: LongInt;
   150 var ii: LongInt;
   223 if (Message and gm_Down) <> 0 then ParseCommand('/-down', true) else
   223 if (Message and gm_Down) <> 0 then ParseCommand('/-down', true) else
   224 if (Message and gm_Attack) <> 0 then ParseCommand('/-attack', true)
   224 if (Message and gm_Attack) <> 0 then ParseCommand('/-attack', true)
   225 end;
   225 end;
   226 
   226 
   227 {$INCLUDE "CCHandlers.inc"}
   227 {$INCLUDE "CCHandlers.inc"}
   228 
   228 procedure init_uConsole;
   229 initialization
   229 var i: LongInt;
   230 InitConsole;
   230 begin
   231 RegisterVariable('proto'   , vtCommand, @chCheckProto   , true );
   231 	CurrLine:= 0;
   232 RegisterVariable('spectate', vtBoolean, @fastUntilLag   , false);
   232 	Variables:= nil;
   233 RegisterVariable('capture' , vtCommand, @chCapture      , true );
   233 	isDeveloperMode:= true;
   234 RegisterVariable('rotmask' , vtCommand, @chRotateMask   , true );
   234 	
   235 RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
   235 	// initConsole
   236 RegisterVariable('addtrig' , vtCommand, @chAddTrigger   , false);
   236 	cLineWidth:= cScreenWidth div 10;
   237 RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
   237 	if cLineWidth > 255 then
   238 RegisterVariable('map'     , vtCommand, @chSetMap       , false);
   238 		cLineWidth:= 255;
   239 RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
   239 	for i:= 0 to Pred(cLinesCount) do 
   240 RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
   240 		PByte(@ConsoleLines[i])^:= 0;
   241 RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false);
   241 	
   242 RegisterVariable('delay'   , vtLongInt, @cInactDelay    , false);
   242 	RegisterVariable('proto'   , vtCommand, @chCheckProto   , true );
   243 RegisterVariable('casefreq', vtLongInt, @cCaseFactor    , false);
   243 	RegisterVariable('spectate', vtBoolean, @fastUntilLag   , false);
   244 RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns  , false);
   244 	RegisterVariable('capture' , vtCommand, @chCapture      , true );
   245 RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false);
   245 	RegisterVariable('rotmask' , vtCommand, @chRotateMask   , true );
   246 RegisterVariable('landadds', vtLongInt, @cLandAdditions , false);
   246 	RegisterVariable('addteam' , vtCommand, @chAddTeam      , false);
   247 RegisterVariable('gmflags' , vtLongInt, @GameFlags      , false);
   247 	RegisterVariable('addtrig' , vtCommand, @chAddTrigger   , false);
   248 RegisterVariable('trflags' , vtLongInt, @TrainingFlags  , false);
   248 	RegisterVariable('rdriven' , vtCommand, @chTeamLocal    , false);
   249 RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false);
   249 	RegisterVariable('map'     , vtCommand, @chSetMap       , false);
   250 RegisterVariable('minestime',vtLongInt, @cMinesTime     , false);
   250 	RegisterVariable('theme'   , vtCommand, @chSetTheme     , false);
   251 RegisterVariable('fort'    , vtCommand, @chFort         , false);
   251 	RegisterVariable('seed'    , vtCommand, @chSetSeed      , false);
   252 RegisterVariable('voicepack',vtCommand, @chVoicepack    , false);
   252 	RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false);
   253 RegisterVariable('grave'   , vtCommand, @chGrave        , false);
   253 	RegisterVariable('delay'   , vtLongInt, @cInactDelay    , false);
   254 RegisterVariable('bind'    , vtCommand, @chBind         , true );
   254 	RegisterVariable('casefreq', vtLongInt, @cCaseFactor    , false);
   255 RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);
   255 	RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns  , false);
   256 RegisterVariable('hat'     , vtCommand, @chSetHat       , false);
   256 	RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false);
   257 RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
   257 	RegisterVariable('landadds', vtLongInt, @cLandAdditions , false);
   258 RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
   258 	RegisterVariable('gmflags' , vtLongInt, @GameFlags      , false);
   259 RegisterVariable('quit'    , vtCommand, @chQuit         , true );
   259 	RegisterVariable('trflags' , vtLongInt, @TrainingFlags  , false);
   260 RegisterVariable('confirm' , vtCommand, @chConfirm      , true );
   260 	RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false);
   261 RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
   261 	RegisterVariable('minestime',vtLongInt, @cMinesTime     , false);
   262 RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
   262 	RegisterVariable('fort'    , vtCommand, @chFort         , false);
   263 RegisterVariable('zoomin'  , vtCommand, @chZoomIn       , true );
   263 	RegisterVariable('voicepack',vtCommand, @chVoicepack    , false);
   264 RegisterVariable('zoomout' , vtCommand, @chZoomOut      , true );
   264 	RegisterVariable('grave'   , vtCommand, @chGrave        , false);
   265 RegisterVariable('zoomreset',vtCommand, @chZoomReset    , true );
   265 	RegisterVariable('bind'    , vtCommand, @chBind         , true );
   266 RegisterVariable('skip'    , vtCommand, @chSkip         , false);
   266 	RegisterVariable('addhh'   , vtCommand, @chAddHH        , false);
   267 RegisterVariable('history' , vtCommand, @chHistory      , true );
   267 	RegisterVariable('hat'     , vtCommand, @chSetHat       , false);
   268 RegisterVariable('chat'    , vtCommand, @chChat         , true );
   268 	RegisterVariable('hhcoords', vtCommand, @chSetHHCoords  , false);
   269 RegisterVariable('newgrave', vtCommand, @chNewGrave     , false);
   269 	RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false);
   270 RegisterVariable('say'     , vtCommand, @chSay          , true );
   270 	RegisterVariable('quit'    , vtCommand, @chQuit         , true );
   271 RegisterVariable('hogsay'  , vtCommand, @chHogSay       , true );
   271 	RegisterVariable('confirm' , vtCommand, @chConfirm      , true );
   272 RegisterVariable('team'    , vtCommand, @chTeamSay      , true );
   272 	RegisterVariable('+speedup', vtCommand, @chSpeedup_p    , true );
   273 RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
   273 	RegisterVariable('-speedup', vtCommand, @chSpeedup_m    , true );
   274 RegisterVariable('+precise', vtCommand, @chPrecise_p    , false);
   274 	RegisterVariable('zoomin'  , vtCommand, @chZoomIn       , true );
   275 RegisterVariable('-precise', vtCommand, @chPrecise_m    , false);
   275 	RegisterVariable('zoomout' , vtCommand, @chZoomOut      , true );
   276 RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
   276 	RegisterVariable('zoomreset',vtCommand, @chZoomReset    , true );
   277 RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
   277 	RegisterVariable('skip'    , vtCommand, @chSkip         , false);
   278 RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
   278 	RegisterVariable('history' , vtCommand, @chHistory      , true );
   279 RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
   279 	RegisterVariable('chat'    , vtCommand, @chChat         , true );
   280 RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
   280 	RegisterVariable('newgrave', vtCommand, @chNewGrave     , false);
   281 RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
   281 	RegisterVariable('say'     , vtCommand, @chSay          , true );
   282 RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
   282 	RegisterVariable('hogsay'  , vtCommand, @chHogSay       , true );
   283 RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
   283 	RegisterVariable('team'    , vtCommand, @chTeamSay      , true );
   284 RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
   284 	RegisterVariable('ammomenu', vtCommand, @chAmmoMenu     , false);
   285 RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
   285 	RegisterVariable('+precise', vtCommand, @chPrecise_p    , false);
   286 RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
   286 	RegisterVariable('-precise', vtCommand, @chPrecise_m    , false);
   287 RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
   287 	RegisterVariable('+left'   , vtCommand, @chLeft_p       , false);
   288 RegisterVariable('timer'   , vtCommand, @chTimer        , false);
   288 	RegisterVariable('-left'   , vtCommand, @chLeft_m       , false);
   289 RegisterVariable('taunt'   , vtCommand, @chTaunt        , false);
   289 	RegisterVariable('+right'  , vtCommand, @chRight_p      , false);
   290 RegisterVariable('setweap' , vtCommand, @chSetWeapon    , false);
   290 	RegisterVariable('-right'  , vtCommand, @chRight_m      , false);
   291 RegisterVariable('slot'    , vtCommand, @chSlot         , false);
   291 	RegisterVariable('+up'     , vtCommand, @chUp_p         , false);
   292 RegisterVariable('put'     , vtCommand, @chPut          , false);
   292 	RegisterVariable('-up'     , vtCommand, @chUp_m         , false);
   293 RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
   293 	RegisterVariable('+down'   , vtCommand, @chDown_p       , false);
   294 RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
   294 	RegisterVariable('-down'   , vtCommand, @chDown_m       , false);
   295 RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
   295 	RegisterVariable('+attack' , vtCommand, @chAttack_p     , false);
   296 RegisterVariable('+volup'  , vtCommand, @chVol_p        , true );
   296 	RegisterVariable('-attack' , vtCommand, @chAttack_m     , false);
   297 RegisterVariable('-volup'  , vtCommand, @chVol_m        , true );
   297 	RegisterVariable('switch'  , vtCommand, @chSwitch       , false);
   298 RegisterVariable('+voldown', vtCommand, @chVol_m        , true );
   298 	RegisterVariable('nextturn', vtCommand, @chNextTurn     , false);
   299 RegisterVariable('-voldown', vtCommand, @chVol_p        , true );
   299 	RegisterVariable('timer'   , vtCommand, @chTimer        , false);
   300 RegisterVariable('findhh'  , vtCommand, @chFindhh       , true );
   300 	RegisterVariable('taunt'   , vtCommand, @chTaunt        , false);
   301 RegisterVariable('pause'   , vtCommand, @chPause        , true );
   301 	RegisterVariable('setweap' , vtCommand, @chSetWeapon    , false);
   302 RegisterVariable('+cur_u'  , vtCommand, @chCurU_p       , true );
   302 	RegisterVariable('slot'    , vtCommand, @chSlot         , false);
   303 RegisterVariable('-cur_u'  , vtCommand, @chCurU_m       , true );
   303 	RegisterVariable('put'     , vtCommand, @chPut          , false);
   304 RegisterVariable('+cur_d'  , vtCommand, @chCurD_p       , true );
   304 	RegisterVariable('ljump'   , vtCommand, @chLJump        , false);
   305 RegisterVariable('-cur_d'  , vtCommand, @chCurD_m       , true );
   305 	RegisterVariable('hjump'   , vtCommand, @chHJump        , false);
   306 RegisterVariable('+cur_l'  , vtCommand, @chCurL_p       , true );
   306 	RegisterVariable('fullscr' , vtCommand, @chFullScr      , true );
   307 RegisterVariable('-cur_l'  , vtCommand, @chCurL_m       , true );
   307 	RegisterVariable('+volup'  , vtCommand, @chVol_p        , true );
   308 RegisterVariable('+cur_r'  , vtCommand, @chCurR_p       , true );
   308 	RegisterVariable('-volup'  , vtCommand, @chVol_m        , true );
   309 RegisterVariable('-cur_r'  , vtCommand, @chCurR_m       , true );
   309 	RegisterVariable('+voldown', vtCommand, @chVol_m        , true );
   310 
   310 	RegisterVariable('-voldown', vtCommand, @chVol_p        , true );
   311 finalization
   311 	RegisterVariable('findhh'  , vtCommand, @chFindhh       , true );
   312 FreeVariablesList
   312 	RegisterVariable('pause'   , vtCommand, @chPause        , true );
       
   313 	RegisterVariable('+cur_u'  , vtCommand, @chCurU_p       , true );
       
   314 	RegisterVariable('-cur_u'  , vtCommand, @chCurU_m       , true );
       
   315 	RegisterVariable('+cur_d'  , vtCommand, @chCurD_p       , true );
       
   316 	RegisterVariable('-cur_d'  , vtCommand, @chCurD_m       , true );
       
   317 	RegisterVariable('+cur_l'  , vtCommand, @chCurL_p       , true );
       
   318 	RegisterVariable('-cur_l'  , vtCommand, @chCurL_m       , true );
       
   319 	RegisterVariable('+cur_r'  , vtCommand, @chCurR_p       , true );
       
   320 	RegisterVariable('-cur_r'  , vtCommand, @chCurR_m       , true );
       
   321 end;
       
   322 
       
   323 procedure free_uConsole;
       
   324 begin
       
   325 	FreeVariablesList();
       
   326 end;
   313 
   327 
   314 end.
   328 end.