unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, StdCtrls, ExtCtrls, iniFiles, MMSystem;

type

  TdialogClass = class
    message, firstAnswer, secondAnswer, thirdAnswer, fourthAnswer, image, music, video : string;
    current, firstDirection, secondDirection, thirdDirection, fourthDirection: integer;
    prevMusicStop: boolean;
  end;

  { TmainForm }

  TmainForm = class(TForm)
    inputText: TEdit;
    gameScreen: TImage;
    outputText: TMemo;
    Splitter1: TSplitter;
    procedure FormCreate(Sender: TObject);
    procedure inputTextKeyPress(Sender: TObject; var Key: char);
  private

  public

  end;

var
  mainForm: TmainForm;
  gameIni: TiniFile;
  saveIni: TiniFile;
  dialog: array of TdialogClass;
  curDir: integer;
  lastMusic, lastImage: string;

procedure move(var direction: integer);
procedure loadGame();
procedure saveGame();
procedure playMusic(var music: string);

implementation

{$R *.lfm}

{ TmainForm }

procedure TmainForm.FormCreate(Sender: TObject);
var i, iniSectionsCount: integer;
begin
  iniSectionsCount := 0;
  i := 0;
  if not FileExists('game.ini') then
    outputText.Lines.Add('game.ini not found!')
  else
    begin
      gameIni := TiniFile.Create('game.ini');
      while gameIni.SectionExists('dialog'+IntToStr(iniSectionsCount)) = true do
        begin
         iniSectionsCount := iniSectionsCount + 1;
        end;
      setLength(dialog, iniSectionsCount);
      for i := 0 to iniSectionsCount - 1 do
        begin
         dialog[i] := TdialogClass.Create();
         dialog[i].current := i;
         dialog[i].message := gameIni.ReadString('dialog'+IntToStr(i), 'message', '');
         dialog[i].firstAnswer := gameIni.ReadString('dialog'+IntToStr(i), 'firstAnswer', '');
         dialog[i].firstDirection := gameIni.ReadInteger('dialog'+IntToStr(i), 'firstDirection', 0);
         dialog[i].secondAnswer := gameIni.ReadString('dialog'+IntToStr(i), 'secondAnswer', '');
         dialog[i].secondDirection := gameIni.ReadInteger('dialog'+IntToStr(i), 'secondDirection', 0);
         dialog[i].thirdAnswer := gameIni.ReadString('dialog'+IntToStr(i), 'thirdAnswer', '');
         dialog[i].thirdDirection := gameIni.ReadInteger('dialog'+IntToStr(i), 'thirdDirection', 0);
         dialog[i].fourthAnswer := gameIni.ReadString('dialog'+IntToStr(i), 'fourthAnswer', '');
         dialog[i].fourthDirection := gameIni.ReadInteger('dialog'+IntToStr(i), 'fourthDirection', 0);
         dialog[i].image := gameIni.ReadString('dialog'+IntToStr(i), 'image', '');
         dialog[i].music := gameIni.ReadString('dialog'+IntToStr(i), 'music', '');
         dialog[i].video := gameIni.ReadString('dialog'+IntToStr(i), 'video', '');
         dialog[i].prevMusicStop := gameIni.ReadBool('dialog'+IntToStr(i), 'prevMusicStop', false);
        end;
      gameIni.Free;
      move(dialog[0].current);

   end;

end;

procedure move(var direction: integer);
begin
  mainForm.outputText.Lines.Clear;
  if dialog[direction].video <> '' then
    if FileExists('video/'+dialog[direction].video) then
      begin
        mciSendString(PChar('pause musicFile') , nil, 0, 0);
        ExecuteProcess('cmd.exe', '/c "'+ExtractFilePath(application.ExeName)+'video\'+dialog[direction].video+'"', []);
        mciSendString(PChar('resume musicFile') , nil, 0, 0);
      end
    else
      mainForm.outputText.Lines.Add(lineEnding+'File '+dialog[direction].video+' not found!');

  if dialog[direction].prevMusicStop = true then
    begin
      mciSendString('stop musicFile', nil, 0, 0);
      mciSendString('close musicFile', nil, 0, 0);
    end;

  if (dialog[direction].music <> lastMusic) and (dialog[direction].music <> '') then
    if FileExists('music/'+dialog[direction].music) then
      begin
        playMusic(dialog[direction].music);
        lastMusic := dialog[direction].music;
      end
    else
      mainForm.outputText.Lines.Add(lineEnding+'File '+dialog[direction].music+' not found!');

  if (dialog[direction].image <> lastImage) and (dialog[direction].image <> '') then
    if FileExists('pictures/'+dialog[direction].image) then
      begin
        mainForm.gameScreen.Picture.LoadFromFile('pictures/'+dialog[direction].image);
        lastImage := dialog[direction].image;
      end
    else
      mainForm.outputText.Lines.Add(lineEnding+'File '+dialog[direction].image+' not found!');

  mainForm.outputText.Lines.Add(dialog[direction].message);
  mainForm.outputText.Lines.Add(lineEnding+dialog[direction].firstAnswer);
  mainForm.outputText.Lines.Add(dialog[direction].secondAnswer);
  mainForm.outputText.Lines.Add(dialog[direction].thirdAnswer);
  mainForm.outputText.Lines.Add(dialog[direction].fourthAnswer);
  curDir := dialog[direction].current;
end;

procedure playMusic(var music: string);
begin
  mciSendString('stop musicFile', nil, 0, 0);
  mciSendString('close musicFile', nil, 0, 0);
  mciSendString(PChar('open "' + 'music\'+ music + '" alias musicFile'), nil, 0, 0);
  mciSendString(PChar('play musicFile') , nil, 0, 0);
end;

procedure loadGame();
begin
  if not fileExists('save.ini') then
    mainForm.outputText.Lines.Add('save.ini not found!')
  else
    begin
      mainForm.outputText.Lines.Clear;
      saveIni := TiniFile.Create('save.ini');
      lastImage := saveIni.ReadString('save', 'lastImage', '');
      lastMusic := saveIni.ReadString('save', 'lastMusic', '');
      mainForm.gameScreen.Picture.LoadFromFile('pictures/'+lastImage);
      playMusic(lastMusic);
      move(dialog[saveIni.ReadInteger('save', 'currentDialog', 0)].current);
      saveIni.Free;
    end;

end;

procedure saveGame();
begin
  saveIni := TiniFile.Create(ExtractFilePath(Application.ExeName)+'save.ini');
  saveIni.WriteInteger('save', 'currentDialog', curDir);
  saveIni.WriteString('save', 'lastImage', lastImage);
  saveIni.WriteString('save', 'lastMusic', lastMusic);
  saveIni.UpdateFile;
  saveIni.Free;
  mainForm.outputText.Lines.Add('game saved.');
end;

procedure TmainForm.inputTextKeyPress(Sender: TObject; var Key: char);
begin
  if Key = #13 then
  begin
    inputText.Text := LowerCase(inputText.Text);
    case inputText.Text of
      'clear':
        begin
         outputText.Lines.Clear;
         move(dialog[curDir].current);
        end;
      'exit': Application.Terminate;
      'restart':
        begin
         outputText.Lines.Clear;
         move(dialog[0].current);
        end;
      'help': outputText.Lines.Add(lineEnding+'List of available commands:'+lineEnding+'1, 2, 3 or 4 - choose the answer in dialog'+lineEnding+'save - save the game'+lineEnding+'load - load previously saved game'+lineEnding+'restart - start the game from beginning'+lineEnding+'clear - remove all text and reload current dialog'+lineEnding+'help - shows the list of available commands (you are here)'+lineEnding+'exit - close the game');
      'save': saveGame;
      'load': loadGame;
      '1': if dialog[curDir].firstDirection <> 0 then move(dialog[curDir].firstDirection);
      '2': if dialog[curDir].secondDirection <> 0 then move(dialog[curDir].secondDirection);
      '3': if dialog[curDir].thirdDirection <> 0 then move(dialog[curDir].thirdDirection);
      '4': if dialog[curDir].fourthDirection <> 0 then move(dialog[curDir].fourthDirection);
    else
      outputText.Lines.Append(lineEnding+'Incorrect command. Type correct comand or help to find out a list of available commands.');
    end;
    inputText.Text := '';
  end;
end;


end.

