program quest;
uses
    {$IFDEF WINDOWS}
    windows,
    {$ENDIF}
    crt;

const
	PARAM_COUNT = 5;
	OPTION_COUNT = 5;
	USERVAR_COUNT = 16;
	STORY_FILE = 'test.qst';
	YOUR_ANSWER = '> ';

var
	Story: text;
	CurLine: string;
	Param: array [0..PARAM_COUNT] of string;
	OptStr, OptGoTo: array [1..OPTION_COUNT] of string;
	OptCount: word;
	isReadType, isOptType: boolean;
	OptRead: string;
	UserVar: array [0..USERVAR_COUNT] of string;
	i, j: integer;
	code: word;

function StrToColor(Color: string) : word;
begin
	case Color of
	'white':
		StrToColor := White;
	'black':
		StrToColor := Black;
	'red':
		StrToColor := LightRed;
	'green':
		StrToColor := Green;
	'blue':
		StrToColor := Blue;
	'cyan':
		StrToColor := LightCyan;
	'magenta':
		StrToColor := Magenta
	else
		StrToColor := LightGray;
	end;
end;

{procedure DrawSpaces(Count: integer);
var 
	i: integer;
begin
	for i := 1 to Count do
		write(' ');
	writeln;
end;}

BEGIN
	{$IFDEF WINDOWS}
    SetConsoleOutputCP(CP_UTF8);
    {$ENDIF}
	
	OptCount := 0;
	isOptType := false;
	for i := 0 to OPTION_COUNT do
	begin
		OptStr[i] := '';
		OptGoTo[i] := '';
	end;
	for i := 0 to USERVAR_COUNT do
		UserVar[i] := '';
	clrscr;
	
	//SpecialForIGDC();
	
	assign(Story, STORY_FILE);
	{$I+}
	reset(Story);
	
	while not eof(Story) do
	begin
		readln(Story, CurLine);
		if CurLine = '' then
		begin
			if isOptType then
			begin
				if OptCount > 0 then
				begin
					OptRead := '';
					while true do
					begin
						writeln;
						gotoxy(wherex, wherey - 1);
						
						textcolor(White);
						textbackground(Cyan);
						
						writeln(YOUR_ANSWER);
						gotoxy(length(YOUR_ANSWER) + 1, wherey - 1);
						readln(OptRead);
						
						textcolor(LightGray);
						textbackground(Black);
						
						val(OptRead, i, code);
						if (i >= 1) and (i <= OptCount) and (code = 0) then
						begin
							writeln;
							break;
						end;
					end;
					OptCount := 0;
				end
				else
					i := 1;
				
				if OptGoTo[i] = '' then
				begin
					isOptType := false;
					continue;
				end;
				
				while (CurLine[1] <> '>') or (CurLine <> ('>' + OptGoTo[i])) do
				begin
					readln(Story, CurLine);
					if eof(Story) then
						break;
				end;
				
				isOptType := false;
				continue;
			end
			else begin
				readln;
				continue;
			end;
		end;
		
		j := 0;
		for i := 0 to PARAM_COUNT do
			Param[i] := '';
		isReadType := false;
		
		for i := 1 to length(CurLine) do
		begin
			if CurLine[i] = '"' then
			begin
				isReadType := not isReadType;
				continue;
			end;
			
			if (not isReadType) and (CurLine[i] = ' ') then
				j += 1
			else
				Param[j] += CurLine[i];
		end;
		
		if Param[0] = 't' then
		begin
			{1 - str, 2 - name, 3 - name color}
			if Param[2] = '' then
				writeln(Param[1])
			else begin
				textcolor(StrToColor(Param[3]));
				write(Param[2]);
				textcolor(LightGray);
				writeln(': ', Param[1]);
			end;
		end
		else if Param[0] = 'tx' then
		begin
			{1 - str, 2 - back color, 3 - text color}
			
			textcolor(StrToColor(Param[3]));
			textbackground(StrToColor(Param[2]));
			
			writeln(Param[1]);
			
			textcolor(LightGray);
			textbackground(Black);
		end
		else if Param[0] = 'option' then
		begin
			{1 - str, 2 - goto}
			if not isOptType then
				writeln;
			
			isOptType := true;
			OptCount += 1;
			
			OptStr[OptCount] := Param[1];
			OptGoTo[OptCount] := Param[2];
			
			//textcolor(Cyan);
			writeln(OptCount, '. ', OptStr[OptCount]);
			//textcolor(LightGray);
		end
		else if Param[0] = 'goto' then
		begin
			{1 - goto, 2 - var, 3 - str}
			if Param[2] <> '' then
			begin
				val(Param[2], i, code);
				if UserVar[i] <> Param[3] then
				begin
					readln(Story);
					continue;
				end;
			end;
			
			isOptType := true;
			OptGoTo[1] := Param[1];
		end
		else if Param[0] = 'clear' then
		begin
			clrscr;
		end
		else if Param[0] = 'set' then
		begin
			{1 - var, 2 - str}
			val(Param[1], i, code);
			if (i >= 0) and (i <= USERVAR_COUNT) and (code = 0) then
			begin
				UserVar[i] := Param[2];
			end;
		end
		else if Param[0] = 'end' then
		begin
			break;
		end;
	end;
	
	close(Story);
END.