#include <define.h>

//#define NAME                    SCRIPT_

#include <command.h>
//#include <ModReact.h>


variable GVar0;
variable GVar1;
variable GVar2;
variable GVar3;
variable GVar4;
variable GVar5;
variable GVar6;
variable GVar7;
variable GVar8;
variable GVar9;
variable GVar10;
variable GVar11 := 1;
variable GVar12;

procedure start;
#include <push.h>
procedure do_dialogue;
procedure vree_end;
procedure vreecbt;
procedure vree00;
procedure vree01;
procedure vree02;
procedure vree03;
procedure vree03a;
procedure vree17;
procedure vree17a;
procedure vree17z;
procedure vree18;
procedure vree19;
procedure vree21;
procedure vree22;
procedure vree23;
procedure vree24;
procedure vree25;
procedure vree26;
procedure vree27;
procedure vree28;
procedure vree29;
procedure vree30;
procedure vree31;
procedure vree32;
procedure vree33;
procedure vree34;
procedure vree35;
procedure vree36;
procedure vree36_1;
procedure vree37;
procedure vree38;
procedure vree39;
procedure vree40;
procedure vree41;
procedure vree42;
procedure vree43;
procedure vree44;
procedure vree45;
procedure vree46;
procedure vree47;
procedure vree48;
procedure vree200;
procedure vree201;
procedure vree202;
procedure vree203;
procedure vree204;
procedure vree205;
procedure vree300;
procedure vree301;
procedure vree302;
procedure vree303;
procedure vree304;
procedure vree305;
procedure vree306;
procedure branch01;
procedure branch02;
procedure branch03;
procedure branch04;
procedure vree00a;
procedure vree01a;
procedure vree01b;
procedure vree45a;
procedure vree46a;
procedure look_at_p_proc;
procedure talk_p_proc;
procedure time_p_proc;
procedure pickup_p_proc;
procedure critter_p_proc;
procedure destroy_p_proc;

import variable term1_ptr;
import variable term2_ptr;
import variable term3_ptr;
import variable Vree_ptr;

procedure Get_Reaction;
procedure React_To_Level;
procedure Level_To_React;
procedure Up_React;
procedure Down_React;
procedure Bottom_React;
procedure Top_React;
procedure Big_Up_React;
procedure Big_Down_React;
procedure Up_React_Level;
procedure Down_React_Level;
procedure Goodbyes;


procedure start
begin
if (GVar11) then begin
    GVar11 := 0;
    Vree_ptr := self_obj;
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BOS_ELDERS);
    add_timer_event(self_obj, game_ticks(40), 1);
  end
  if (script_action == look_at_proc) then begin
    call look_at_p_proc;
  end
  else begin
    if (script_action == pickup_proc) then begin
      call pickup_p_proc;
    end
    else begin
      if (script_action == talk_proc) then begin
        call talk_p_proc;
      end
      else begin
        if (script_action == timed_event_proc) then begin
          call time_p_proc;
        end
        else begin
          if (script_action == critter_proc) then begin
            call critter_p_proc;
          end
          else begin
            if (script_action == destroy_proc) then begin
              call destroy_p_proc;
            end
          end
        end
      end
    end
  end
end

procedure do_dialogue
begin
  start_gdialog(SCRIPT_TANDI, self_obj, 4, HEAD_VREE, BACKGROUND_BROTHERHOOD);
  call Get_Reaction;
  gsay_start;
  GVar4 := dude_is_male;
  if (local_var(4) > 0) then begin
    if (GVar8) then begin
      call vree32;
    end
    if (GVar9) then begin
      call vree34;
    end
    if (GVar10 == 1) then begin
      call vree43;
    end
    if (GVar10 == 2) then begin
      call vree45;
    end
    if (GVar10 < 1) then begin
      if (local_var(1) > 1) then begin
        call vree17;
      end
      else begin
        call vree46;
      end
    end
  end
  else begin
    set_local_var(4, 1);
    if (local_var(1) >= 2) then begin
      call vree02;
    end
    else begin
      call vree00;
    end
  end
  gsay_end;
  end_dialogue;
end

procedure vree_end
begin
end

procedure vreecbt
begin
end

procedure vree00
begin
  gsay_reply(SCRIPT_VREE, 101);
  giq_option(4, SCRIPT_VREE, 102, vree00a, GOOD_REACTION);
  giq_option(4, SCRIPT_VREE, 103, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 104, vree01, NEUTRAL_REACTION);
  giq_option(-3, SCRIPT_VREE, 105, vree31, NEUTRAL_REACTION);
end

procedure vree01
begin
  gsay_reply(SCRIPT_VREE, 106);
  giq_option(4, SCRIPT_VREE, 107, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 108, vree01a, BAD_REACTION);
  giq_option(4, SCRIPT_VREE, 109, vree01b, BAD_REACTION);
end

procedure vree02
begin
  gsay_reply(SCRIPT_VREE, 110);
  giq_option(4, SCRIPT_VREE, 111, vree03, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_VREE, 112, vree17, NEUTRAL_REACTION);
  giq_option(-3, SCRIPT_VREE, 113, vree31, NEUTRAL_REACTION);
end

procedure vree03
begin
  gsay_reply(SCRIPT_VREE, 114);
  giq_option(4, SCRIPT_VREE, 115, vree03a, NEUTRAL_REACTION);
end

procedure vree03a
begin
  gsay_message(SCRIPT_VREE, 116, NEUTRAL_REACTION);
  call vree17;
end

procedure vree17
begin
  gsay_reply(SCRIPT_VREE, 117);
  call vree17z;
end

procedure vree17a
begin
  gsay_reply(SCRIPT_VREE, 118);
  call vree17z;
end

procedure vree17z
begin
  if (global_var(GVAR_NECROP_WATER_CHIP_TAKEN) != 1) then begin
    giq_option(4, SCRIPT_VREE, 119, vree48, NEUTRAL_REACTION);
  end
  giq_option(4, SCRIPT_VREE, 120, branch01, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_VREE, 121, branch02, NEUTRAL_REACTION);
  if (local_var(5) != 1) then begin
    giq_option(6, SCRIPT_VREE, 122, branch03, NEUTRAL_REACTION);
  end
  if (local_var(6) == 0) then begin
    giq_option(7, SCRIPT_VREE, 123, branch04, NEUTRAL_REACTION);
  end
end

procedure vree18
begin
  set_map_var(0, 1);
  gsay_reply(SCRIPT_VREE, 124);
  giq_option(4, SCRIPT_VREE, 125, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 126, vree17a, NEUTRAL_REACTION);
end

procedure vree19
begin
  gsay_reply(SCRIPT_VREE, 127);
  call vree_end;
end

procedure vree21
begin
  gsay_reply(SCRIPT_VREE, 128);
  if (local_var(7) != 1) then begin
    giq_option(5, SCRIPT_VREE, 129, vree22, NEUTRAL_REACTION);
  end
  giq_option(4, SCRIPT_VREE, 130, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 131, vree17a, NEUTRAL_REACTION);
end

procedure vree22
begin
  set_local_var(7, 1);
  gsay_message(SCRIPT_VREE, 132, NEUTRAL_REACTION);
  add_timer_event(self_obj, game_ticks(4), 2);
end

procedure vree23
begin
end

procedure vree24
begin
  gsay_message(SCRIPT_VREE, 134, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree25
begin
  GVar3 := create_object_sid(109, 0, 0, -1);
  add_obj_to_inven(dude_obj, GVar3);
  gsay_reply(SCRIPT_VREE, 135);
  giq_option(4, SCRIPT_VREE, 136, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 137, vree17a, NEUTRAL_REACTION);
end

procedure vree26
begin
  gsay_message(SCRIPT_VREE, 138, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree27
begin
  gsay_reply(SCRIPT_VREE, 139);
  giq_option(6, SCRIPT_VREE, 140, vree28, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_VREE, 141, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 142, vree17a, NEUTRAL_REACTION);
end

procedure vree28
begin
  gsay_reply(SCRIPT_VREE, 143);
  giq_option(6, SCRIPT_VREE, 144, vree_end, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_VREE, 145, vree29, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 146, vree17a, NEUTRAL_REACTION);
end

procedure vree29
begin
  variable LVar0 := 0;
  LVar0 := create_object_sid(194, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  set_local_var(6, 1);
  set_global_var(GVAR_DESTROY_MASTER_6, 1);
  gsay_reply(SCRIPT_VREE, 147);
  giq_option(4, SCRIPT_VREE, 148, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 149, vree17a, NEUTRAL_REACTION);
end

procedure vree30
begin
  gsay_message(SCRIPT_VREE, 150, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree31
begin
  gsay_message(SCRIPT_VREE, 151, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree32
begin
  gsay_reply(SCRIPT_VREE, 152);
  giq_option(4, SCRIPT_VREE, 153, vreecbt, BAD_REACTION);
  giq_option(5, SCRIPT_VREE, 154, vree33, NEUTRAL_REACTION);
end

procedure vree33
begin
  gsay_reply(SCRIPT_VREE, 155);
  giq_option(4, SCRIPT_VREE, 156, vreecbt, BAD_REACTION);
end

procedure vree34
begin
  gsay_reply(SCRIPT_VREE, 157);
  giq_option(4, SCRIPT_VREE, 158, vree36, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_VREE, 159, vree39, NEUTRAL_REACTION);
  giq_option(-3, SCRIPT_VREE, 160, vree35, NEUTRAL_REACTION);
end

procedure vree35
begin
  gsay_message(SCRIPT_VREE, 161, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree36
begin
  gsay_reply(SCRIPT_VREE, 162);
  giq_option(5, SCRIPT_VREE, 163, vree36_1, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 164, vree_end, NEUTRAL_REACTION);
end

procedure vree36_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
    call vree37;
  end
  else begin
    call vree38;
  end
end

procedure vree37
begin
  gsay_message(SCRIPT_VREE, 165, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree38
begin
  gsay_message(SCRIPT_VREE, 166, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree39
begin
  gsay_reply(SCRIPT_VREE, 167);
  giq_option(6, SCRIPT_VREE, 168, vree40, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_VREE, 169, vree42, NEUTRAL_REACTION);
end

procedure vree40
begin
  gsay_reply(SCRIPT_VREE, 170);
  giq_option(6, SCRIPT_VREE, 171, vree41, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_VREE, 172, vree_end, NEUTRAL_REACTION);
end

procedure vree41
begin
  gsay_message(SCRIPT_VREE, 173, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree42
begin
  gsay_message(SCRIPT_VREE, 174, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree43
begin
  gsay_reply(SCRIPT_VREE, 175);
  giq_option(4, SCRIPT_VREE, 176, vree44, NEUTRAL_REACTION);
  giq_option(-3, SCRIPT_VREE, 177, vree35, NEUTRAL_REACTION);
end

procedure vree44
begin
  gsay_message(SCRIPT_VREE, 178, NEUTRAL_REACTION);
  call vree_end;
end

procedure vree45
begin
  gsay_message(SCRIPT_VREE, 179, NEUTRAL_REACTION);
  call vree45a;
end

procedure vree46
begin
  gsay_reply(SCRIPT_VREE, 180);
  giq_option(4, SCRIPT_VREE, 181, vree47, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 182, vree46a, NEUTRAL_REACTION);
end

procedure vree47
begin
  gsay_message(SCRIPT_VREE, 183, NEUTRAL_REACTION);
  if (local_var(1) > 2) then begin
    call vree17;
  end
  else begin
    gsay_message(SCRIPT_VREE, 184, NEUTRAL_REACTION);
    call vree_end;
  end
end

procedure vree48
begin
  gsay_reply(SCRIPT_VREE, 185);
  giq_option(4, SCRIPT_VREE, 186, vree_end, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_VREE, 187, vree17, NEUTRAL_REACTION);
end

procedure vree200
begin
  gsay_message(SCRIPT_VREE, 188, NEUTRAL_REACTION);
end

procedure vree201
begin
  gsay_message(SCRIPT_VREE, 189, NEUTRAL_REACTION);
end

procedure vree202
begin
  gsay_message(SCRIPT_VREE, 190, NEUTRAL_REACTION);
end

procedure vree203
begin
  gsay_message(SCRIPT_VREE, 191, NEUTRAL_REACTION);
end

procedure vree204
begin
  gsay_message(SCRIPT_VREE, 192, NEUTRAL_REACTION);
end

procedure vree205
begin
  gsay_message(SCRIPT_VREE, 193, NEUTRAL_REACTION);
end

procedure vree300
begin
  gsay_message(SCRIPT_VREE, 194, NEUTRAL_REACTION);
end

procedure vree301
begin
  gsay_message(SCRIPT_VREE, 195, NEUTRAL_REACTION);
end

procedure vree302
begin
  gsay_message(SCRIPT_VREE, 196, NEUTRAL_REACTION);
end

procedure vree303
begin
  gsay_message(SCRIPT_VREE, 197, NEUTRAL_REACTION);
end

procedure vree304
begin
  gsay_message(SCRIPT_VREE, 198, NEUTRAL_REACTION);
end

procedure vree305
begin
  gsay_message(SCRIPT_VREE, 199, NEUTRAL_REACTION);
end

procedure vree306
begin
  gsay_message(SCRIPT_VREE, 200, NEUTRAL_REACTION);
end

procedure branch01
begin
  if (local_var(1) >= 2) then begin
    call vree18;
  end
  else begin
    call vree19;
  end
end

procedure branch02
begin
  if (local_var(1) > 1) then begin
    call vree21;
  end
  else begin
    call vree24;
  end
end

procedure branch03
begin
  set_local_var(5, 1);
  if (local_var(1) > 1) then begin
    call vree25;
  end
  else begin
    call vree26;
  end
end

procedure branch04
begin
  if (local_var(1) > 1) then begin
    call vree27;
  end
  else begin
    call vree30;
  end
end

procedure vree00a
begin
  call Up_React;
  call vree_end;
end

procedure vree01a
begin
  call Down_React;
  call vree_end;
end

procedure vree01b
begin
  call Big_Down_React;
  call vree_end;
end

procedure vree45a
begin
  variable LVar0 := 0;
  LVar0 := create_object_sid(48, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  LVar0 := create_object_sid(48, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  LVar0 := create_object_sid(38, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  LVar0 := create_object_sid(39, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  call Big_Up_React;
  call vree_end;
end

procedure vree46a
begin
  call Down_React;
  call vree_end;
end

procedure look_at_p_proc
begin
  script_overrides;
  display_msg(message_str(SCRIPT_VREE, 100));
end

procedure talk_p_proc
begin
  call do_dialogue;
end

procedure time_p_proc
begin
  if (fixed_param == 1) then begin
    GVar0 := random(2, 3);
    if (GVar0 == 2) then begin
      use_obj(term2_ptr);
    end
    else begin
      if (GVar0 == 3) then begin
        use_obj(term3_ptr);
      end
    end
    add_timer_event(self_obj, game_ticks(40), 1);
  end
  else begin
    if (fixed_param == 2) then begin
      use_obj(term1_ptr);
    end
  end
end

procedure pickup_p_proc
begin
  if (source_obj == dude_obj) then begin
    GVar5 := 1;
  end
end

procedure critter_p_proc
begin
  if (global_var(GVAR_ENEMY_BROTHERHOOD)) then begin
    GVar5 := 1;
  end
  if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
    GVar5 := 0;
  end
  if (GVar5) then begin
    set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
    GVar5 := 0;
    attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
  end
end

procedure destroy_p_proc
begin
  set_global_var(GVAR_ENEMY_BROTHERHOOD, 1);
  rm_timer_event(self_obj);
  if (source_obj == dude_obj) then begin
    if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
      set_global_var(GVAR_BERSERKER_REPUTATION, 1);
      set_global_var(GVAR_CHAMPION_REPUTATION, 0);
    end
    if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
      set_global_var(GVAR_CHAMPION_REPUTATION, 1);
      set_global_var(GVAR_BERSERKER_REPUTATION, 0);
    end
    set_global_var(GVAR_GOOD_MONSTER, global_var(GVAR_GOOD_MONSTER) + 1);
    if ((global_var(GVAR_GOOD_MONSTER) % 2) == 0) then begin
      set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
    end
  end
end

procedure Get_Reaction
begin
  if (local_var(2) == 0) then begin
    set_local_var(0, 50);
    set_local_var(1, 2);
    set_local_var(2, 1);
    set_local_var(0, local_var(0) + (5 * get_critter_stat(dude_obj, 3)) - 25);
    set_local_var(0, local_var(0) + (10 * has_trait(0, dude_obj, 10)));
    if (has_trait(0, dude_obj, 39)) then begin
      if (global_var(GVAR_PLAYER_REPUTATION) > 0) then begin
        set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
      end
      else begin
        set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
      end
    end
    else begin
      if (local_var(3) == 1) then begin
        set_local_var(0, local_var(0) - global_var(GVAR_PLAYER_REPUTATION));
      end
      else begin
        set_local_var(0, local_var(0) + global_var(GVAR_PLAYER_REPUTATION));
      end
    end
    if (global_var(GVAR_CHILDKILLER_REPUTATION) > 2) then begin
      set_local_var(0, local_var(0) - 30);
    end
    if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_BAD_MONSTER) > (3 * global_var(GVAR_GOOD_MONSTER))) or (global_var(GVAR_CHAMPION_REPUTATION) == 1))) then begin
      set_local_var(0, local_var(0) + 20);
    end
    if (((global_var(GVAR_BAD_MONSTER) + global_var(GVAR_GOOD_MONSTER)) >= 25) and ((global_var(GVAR_GOOD_MONSTER) > (2 * global_var(GVAR_BAD_MONSTER))) or (global_var(GVAR_BERSERKER_REPUTATION) == 1))) then begin
      set_local_var(0, local_var(0) - 20);
    end
    call React_To_Level;
  end
end

procedure React_To_Level
begin
  if (local_var(0) <= 25) then begin
    set_local_var(1, 1);
  end
  else begin
    if (local_var(0) <= 75) then begin
      set_local_var(1, 2);
    end
    else begin
      set_local_var(1, 3);
    end
  end
end

procedure Level_To_React
begin
  if (local_var(1) == 1) then begin
    set_local_var(0, random(1, 25));
  end
  else begin
    if (local_var(1) == 2) then begin
      set_local_var(0, random(26, 75));
    end
    else begin
      set_local_var(0, random(76, 100));
    end
  end
end

procedure Up_React
begin
  set_local_var(0, local_var(0) + 10);
  call React_To_Level;
end

procedure Down_React
begin
  set_local_var(0, local_var(0) - 10);
  call React_To_Level;
end

procedure Bottom_React
begin
  set_local_var(1, 1);
  set_local_var(0, 1);
end

procedure Top_React
begin
  set_local_var(0, 100);
  set_local_var(1, 3);
end

procedure Big_Up_React
begin
  set_local_var(0, local_var(0) + 25);
  call React_To_Level;
end

procedure Big_Down_React
begin
  set_local_var(0, local_var(0) - 25);
  call React_To_Level;
end

procedure Up_React_Level
begin
  set_local_var(1, local_var(1) + 1);
  if (local_var(1) > 3) then begin
    set_local_var(1, 3);
  end
  call Level_To_React;
end

procedure Down_React_Level
begin
  set_local_var(1, local_var(1) - 1);
  if (local_var(1) < 1) then begin
    set_local_var(1, 1);
  end
  call Level_To_React;
end

procedure Goodbyes
begin
  GVar12 := message_str(SCRIPT_MODREACT, random(100, 105));
end
