unit upgrade_window;

interface

uses variables, OpenGL, font, buildings, constants, flat_button, records,
     files_names, sysutils;

type
  RUpgrade = record
    num : integer;
    button : integer;
    text_upgrade : string;
  end;

  RUpgradeWindow = record
    x1, y1, x2, y2, width, height : integer;
    uy : array[1..3] of integer;
    aviable_upgrades : array[1..3] of RUpgrade;
  end;

procedure RedrawUpgradeWindow;
procedure SetUpgradeWindow;
procedure BuyUpgrade(n : integer);
procedure SetUpgradesForSelected;
procedure SetBonuses;

var
  all_upgrades : array[0..3] of array[1..max_upgrades] of RTowerUpgrade;
  total_upgrades : array[0..3] of integer;
  upgradewindow : RUpgradeWindow;

implementation

procedure SetBonuses;
var
i : integer;
begin
for i := 1 to 3 do
  with upgradewindow.aviable_upgrades[i] do
    begin
      num := i;
      buttons[14 + i].texture := all_upgrades[0][num].button;
      with all_upgrades[0][num] do
        buttons[14 + i].text := name + '       [ ' + inttostr(price_credits)
            + ' кр. ' + inttostr(price_score) + ' оч. ]';
      text_upgrade := '';
    end;
end;

procedure SetUpgradesForSelected;
var
i, m, w : integer;
s : string;

function GetNumber(n : integer) : integer;
var
n0, s : integer;
begin
n0 := 1;
s := 0;
result := 0;
repeat
s := s + 1;
if not gTurrels[selected_tower].maded_upgrades[s] then
  begin
    if n0 = n then begin result := s; exit; end;
    n0 := n0 + 1;
  end;
until (s >= 10);
end;

procedure AddFeature(text : string; feature : integer);
begin
if feature > 0 then s := s + text + ' +' + inttostr(feature) + '  ';
if feature < 0 then s := s + text + inttostr(feature) + '  ';
end;

begin
if selected_tower = 0 then exit;
m := gTurrels[selected_tower].mode;
for i := 1 to 3 do
  with upgradewindow.aviable_upgrades[i] do
    begin
      num := GetNumber(i);
      if (num < 1) or (num > total_upgrades[m]) then
        buttons[14 + i].texture := gui_cancel_id
      else
        buttons[14 + i].texture := all_upgrades[m][num].button;
      with all_upgrades[m][num] do
        if (num < 1) or (num > total_upgrades[m]) then buttons[14 + i].text := 'Отмена'
      else
        buttons[14 + i].text := name + '    ' + '       [ ' + inttostr(price_credits)
            + ' кр. ' + inttostr(price_score) + ' оч. ]';
      s := '';

      with all_upgrades[m][num].add_params do
        begin
          AddFeature('Гарнизон: ', weapons_aviable);
          AddFeature('Радиус света: ', light_radius);
          with weapons[1] do
            begin
              AddFeature('Дальность выстрела: ', radius);
              AddFeature('Скорострельность: ',   round(speed*10));
              AddFeature('Урон здоровью: ',      round(damage_h*10));
              AddFeature('Урон силе: ',          round(damage_d*10));
              AddFeature('Урон скорости: ',      round(damage_s*10));
            end;
         text_upgrade := s;
       end;
    end;
end;

procedure BuyUpgrade(n : integer);
var
w, upgrade_number : integer;

function CanBuy : boolean;
begin
result := true;
with gTurrels[selected_tower] do
  with all_upgrades[mode][upgrade_number] do
    if (not maded_upgrades[upgrade_number]) and
       (credits >= price_credits) and (score >= price_score) then exit;

result := false;
play_error := play_error + 1;
end;

begin
builded_towers[0] := builded_towers[0] + 1;
upgrade_number := upgradewindow.aviable_upgrades[n].num;

if bonuses_showed then
  with all_upgrades[0][upgrade_number] do
    begin
      if (credits < price_credits) or (score < price_score) then
        begin play_error := play_error + 1; exit; end;
      credits := credits - price_credits;
      score := score - price_score;
      with gBase do
        begin
          default_health := default_health + add_params.max_weapons_aviable;
          base_health := base_health + add_params.weapons_aviable;
          if base_health > default_health then base_health := default_health;             
          time_control := time_control + add_params.light_radius;
        end;
      bonuses_showed := false;
      exit;
    end;    

with gTurrels[selected_tower] do
  with all_upgrades[mode][upgrade_number] do
    begin
      if not CanBuy then exit;
      maded_upgrades[upgrade_number] := true;
      credits := credits - price_credits;
      score := score - price_score;
      upgrades_showed := false;

      with params do
        begin
          weapons_aviable := weapons_aviable + add_params.weapons_aviable;
          if weapons_aviable > max_weapons then weapons_aviable := max_weapons;
          ResetLight(light_radius, light_radius + add_params.light_radius);
          light_radius := light_radius + add_params.light_radius;
          for w := 1 to max_weapons do
            with weapons[w] do
              begin
                radius   := radius   + add_params.weapons[1].radius;
                speed    := speed    + add_params.weapons[1].speed;
                damage_h := damage_h + add_params.weapons[1].damage_h;
                damage_d := damage_d + add_params.weapons[1].damage_d;
                damage_s := damage_s + add_params.weapons[1].damage_s;
              end;
        end;
  end;
end;

procedure SetUpgradeWindow;
var
i : integer;
begin
with upgradewindow do
  begin
    width := round(screen_width / 2);
    height := round(screen_height / 4);
    x1 := (screen_width - width) div 2;
    y1 := (screen_height - height) div 2;
    x2 := (screen_width + width) div 2;
    y2 := (screen_height + height) div 2 + 40;
    uy[1] := 20 + y1 + 0 * height div 3;
    uy[2] := 20 + y1 + 1 * height div 3;
    uy[3] := 20 + y1 + 2 * height div 3;

    buttons[15] := TFlatButton.Create(15, x1 + 20, uy[1], 32, 32, gui_cancel_id);
    buttons[16] := TFlatButton.Create(16, x1 + 20, uy[2], 32, 32, gui_cancel_id);
    buttons[17] := TFlatButton.Create(17, x1 + 20, uy[3], 32, 32, gui_cancel_id);
    buttons[15].draw_by_gui := false; buttons[15].click_by_gui := false;
    buttons[16].draw_by_gui := false; buttons[16].click_by_gui := false;
    buttons[17].draw_by_gui := false; buttons[17].click_by_gui := false;
    buttons[18] := TFlatButton.Create(18, x2 - 52, y2 - 52, 32, 32, gui_cancel_id);
    buttons[18].draw_by_gui := false; buttons[18].click_by_gui := false;
    buttons[18].hint := 'Отмена';
  end;
end;

procedure RedrawUpgradeWindow;
begin
if (not upgrades_showed) and (not bonuses_showed) then exit;

buttons[15].Redraw(1);
buttons[16].Redraw(1);
buttons[17].Redraw(1);
buttons[18].Redraw(1);

glDisable(GL_TEXTURE_2D);
with upgradewindow do
  begin
    glColor4f(1, 0.5, 0.25, 0.5);
    glBegin(GL_QUADS);
    glVertex2f(x1, y1);
    glVertex2f(x1, y2);
    glVertex2f(x2, y2);
    glVertex2f(x2, y1);
    glEnd;

    glLineWidth(2);
    glColor4f(0, 0, 0, 1);
    glBegin(GL_LINES);
    glVertex2f(x1, y1);
    glVertex2f(x1, y2);
    glVertex2f(x1, y2);
    glVertex2f(x2, y2);
    glVertex2f(x2, y2);
    glVertex2f(x2, y1);
    glVertex2f(x2, y1);
    glVertex2f(x1, y1);
    glEnd;

    glColor4f(1, 1, 1, interface_opacity);
    WriteText(x1 + 30, uy[1] + 50, aviable_upgrades[1].text_upgrade);
    WriteText(x1 + 30, uy[2] + 50, aviable_upgrades[2].text_upgrade);
    WriteText(x1 + 30, uy[3] + 50, aviable_upgrades[3].text_upgrade);
  end;

buttons[15].Redraw(2);
buttons[16].Redraw(2);
buttons[17].Redraw(2);
buttons[18].Redraw(2);
end;

end.
