unit expa;

interface

uses sysutils, loadz;

procedure LevelUp;
procedure CheckForNextLevel;

implementation

uses
  MyCRT;

const
  MAXSKILLS = 9;

var
  arr: array[1..MAXSKILLS] of byte;

procedure CheckForNextLevel;
begin
  if data.expa > data.expa_2_next then
    GenerateNextLevel;
end;

procedure DrawSkill(var num: byte; skill, numskill: byte; s: string);
begin
  arr[num] := numskill;
  gotoxy(3, num);
  write(s + ' (' + IntToStr(skill) + ')');
  inc(num);
end;

function GetStrByIndex(i: integer): string;
begin
  case i of
    1: Result := STR_SKILL_STRENGTH;
    2: Result := STR_SKILL_INTELLECT;
    3: Result := STR_SKILL_LUCK;
    4: Result := STR_SKILL_STAMINA;
    5: Result := STR_SKILL_ATTENTION;
    6: Result := STR_SKILL_AGILITY;
    7: Result := STR_SKILL_ACCURACY;
    8: Result := STR_SKILL_PROTECT_BASE;
    9: Result := STR_SKILL_WEAPON;
    10: Result := STR_SKILL_RESISTANCE;
  end;
end;

function GetSkillByIndex(i: integer): integer;
begin
  Result := 0;
  case i of
    1: Result := data.Strength;
    2: Result := data.Intellect;
    3: Result := data.Luck;
    4: Result := data.Stamina;
    5: Result := data.Attention;
    6: Result := data.Agility;
    7: Result := data.Accuracy;
    8: Result := data.Protect_base;
    9: Result := data.Weapon;
    10: Result := data.Resistance;
  end;
end;

procedure DrawSkillPoints;
var
  x, y: integer;
begin
  x := wherex;
  y := wherey;
  GotoXY(3, RESOLUTION_Y - 1);
  if data.expoints = 0 then
    TextColor(LightRed);
  write(STR_EXPA_1 + IntToStr(data.expoints));
  if data.expoints > 0 then
    write(STR_EXPA_2)
  else
    write(STR_EXPA_2_WITHOUT);
  while (WhereX < RESOLUTION_X) do
    write(' ');
  TextColor(White);
  GotoXY(x, y);
end;

procedure DrawLevelupScreen;
var
  num, i: byte;
begin
  textcolor(white);
  num := 2;
  clrscr;
  Ramka(white, HUDSIZE - 1);
  for i := 1 to SKILL_COUNT do
    DrawSkill(num, GetSkillByIndex(i), i, GetStrByIndex(i));
  DrawSkillPoints;
end;

procedure DrawActive(num: integer);
begin
  GoToXY(2, num + 1);
  TextColor(LightRed);
  write('>' + GetStrByIndex(num) + ' (' + IntToStr(GetSkillByIndex(num)) + ')');
  TextColor(White);
end;

procedure DrawInActive(num: integer);
begin
  GoToXY(2, num + 1);
  write(' ' + GetStrByIndex(num) + ' (' + IntToStr(GetSkillByIndex(num)) + ')');
end;

procedure inclevel(var skill: word);
begin
  if skill < MAX_SKILL then
  begin
    inc(skill);
    dec(data.expoints);
  end;
end;


procedure levelup;
var
  pos: byte;
  c: char;
begin
  for pos := 1 to MAXSKILLS do
    arr[pos] := 0;
  pos := 1;
  DrawLevelupScreen;
  DrawActive(pos);
  repeat
    c := readkey;
    if (c = KEY_DOWN) and (arr[pos + 2] > 0) then
    begin
      DrawInActive(pos);
      inc(pos);
      DrawActive(pos);
    end
    else if (c = KEY_UP) and (pos > 1) then
    begin
      DrawInActive(pos);
      dec(pos);
      DrawActive(pos);
    end
    else if (c = KEY_ESCAPE) then
      exit
    else if (c = KEY_RETURN) and (data.expoints > 0) then
    begin
      case arr[pos + 1] of
        1:
        begin
          inclevel(data.strength);
          data.maxweight := data.maxweight + INC_MAXWEIGHT;
        end;
        2: inclevel(data.intellect);
        3: inclevel(data.luck);
        4:
          if data.Stamina < MAX_SKILL then
          begin
            inclevel(data.Stamina);
            if data.health = data.max_health then
            begin
              inc(data.max_health, INC_MAXHEALTH);
              data.health := data.max_health;
            end
            else
              inc(data.max_health, INC_MAXHEALTH);
          end;
        5: inclevel(data.Attention);
        6: inclevel(data.Agility);
        7: inclevel(data.Accuracy);
        8: inclevel(data.Protect_base);
        9: inclevel(data.Weapon);
        10: inclevel(data.Resistance);
      end;
      DrawActive(pos);
      DrawSkillPoints;
    end;
  until (c = KEY_ESCAPE);
end;


end.