#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat;
procedure critter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure Maxson01;
procedure Maxson02;
procedure Maxson02a;
procedure Maxson03;
procedure Maxson04;
procedure Maxson04a;
procedure Maxson05;
procedure Maxson06;
procedure Maxson07;
procedure Maxson08;
procedure Maxson08a;
procedure Maxson09;
procedure Maxson09a;
procedure Maxson10;
procedure Maxson11;
procedure Maxson12;
procedure Maxson13;
procedure Maxson14;
procedure Maxson15;
procedure Maxson16;
procedure Maxson17;
procedure Maxson18;
procedure Maxson19;
procedure Maxson20;
procedure Maxson21;
procedure Maxson22;
procedure Maxson23;
procedure Maxson24;
procedure Maxson25;
procedure Maxson26;
procedure Maxson27;
procedure Maxson28;
procedure Maxson29;
procedure Maxson30;
procedure Maxson31;
procedure Maxson32;
procedure Maxson33;
procedure Maxson34;
procedure MaxsonEnd;
procedure Remove_Player;

variable hostile1;
variable Only_Once := 1;
variable Denounce_Player;

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;

variable exit_line;


procedure start
begin
if (Only_Once) then begin
    Only_Once := 0;
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 44);
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BOS_ELDERS);
  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 == 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

procedure combat
begin
  hostile1 := 1;
end

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

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

procedure talk_p_proc
begin
  call Get_Reaction;
  start_gdialog(SCRIPT_MAXSON, self_obj, 4, HEAD_MAXSON, BACKGROUND_BROTHERHOOD);
  gsay_start;
  if ((global_var(GVAR_BROTHERHOOD_INVASION) == 1) or (global_var(GVAR_BROTHERHOOD_INVASION) == 2)) then begin
    call Maxson34;
  end
  else begin
    if (local_var(5) == 1) then begin
      call Maxson22;
    end
    else begin
      if (global_var(GVAR_THE_VATS) == 2) then begin
        call Maxson20;
      end
      else begin
        if (local_var(4) == 0) then begin
          set_local_var(4, 1);
          call Maxson01;
        end
        else begin
          if (local_var(1) == 1) then begin
            call Maxson21;
          end
          else begin
            call Maxson19;
          end
        end
      end
    end
  end
  gsay_end;
  end_dialogue;
  if (Denounce_Player == 1) then begin
    call Remove_Player;
  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) % 5) == 0) then begin
      set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
    end
  end
end

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

procedure Maxson01
begin
  gsay_reply(SCRIPT_MAXSON, 106);
  giq_option(-3, SCRIPT_MAXSON, 333, Maxson32, NEUTRAL_REACTION);
  if (global_var(GVAR_MISSING_CARAVAN) == 2) then begin
    giq_option(4, SCRIPT_MAXSON, 300, Maxson14, NEUTRAL_REACTION);
  end
  if (global_var(GVAR_MISSING_CARAVAN) == 1) then begin
    giq_option(4, SCRIPT_MAXSON, 301, Maxson06, NEUTRAL_REACTION);
  end
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson02
begin
  gsay_reply(SCRIPT_MAXSON, 162);
  giq_option(4, SCRIPT_MAXSON, 302, Maxson04, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 303, Maxson09, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 304, Maxson02a, BAD_REACTION);
  giq_option(4, SCRIPT_MAXSON, 305, Maxson08, NEUTRAL_REACTION);
end

procedure Maxson02a
begin
  if (is_success(roll_vs_skill(dude_obj, 15, 0))) then begin
    call Maxson09;
  end
  else begin
    call Maxson12;
  end
end

procedure Maxson03
begin
end

procedure Maxson04
begin
  gsay_reply(SCRIPT_MAXSON, 133);
  giq_option(4, SCRIPT_MAXSON, 306, Maxson04a, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 307, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 308, MaxsonEnd, NEUTRAL_REACTION);
end

procedure Maxson04a
begin
  set_map_var(20, 1);
  call Maxson05;
end

procedure Maxson05
begin
  gsay_reply(SCRIPT_MAXSON, 276);
  giq_option(4, SCRIPT_MAXSON, 309, MaxsonEnd, NEUTRAL_REACTION);
end

procedure Maxson06
begin
  gsay_reply(SCRIPT_MAXSON, 122);
  giq_option(4, SCRIPT_MAXSON, 310, Maxson07, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 311, Maxson02, NEUTRAL_REACTION);
end

procedure Maxson07
begin
  gsay_reply(SCRIPT_MAXSON, 128);
  giq_option(4, SCRIPT_MAXSON, 312, Maxson14, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 313, Maxson13, NEUTRAL_REACTION);
end

procedure Maxson08
begin
  gsay_reply(SCRIPT_MAXSON, 184);
  giq_option(4, SCRIPT_MAXSON, 314, Maxson18, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 315, Maxson04a, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 316, Maxson08a, NEUTRAL_REACTION);
end

procedure Maxson08a
begin
  if (is_success(roll_vs_skill(dude_obj, 15, -20))) then begin
    call Maxson04;
  end
  else begin
    call Maxson10;
  end
end

procedure Maxson09
begin
  gsay_reply(SCRIPT_MAXSON, 223);
  giq_option(4, SCRIPT_MAXSON, 317, Maxson09a, BAD_REACTION);
  giq_option(4, SCRIPT_MAXSON, 318, Maxson18, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 319, MaxsonEnd, NEUTRAL_REACTION);
end

procedure Maxson09a
begin
  if (is_success(roll_vs_skill(dude_obj, 15, -10))) then begin
    call Maxson10;
  end
  else begin
    call Maxson11;
  end
end

procedure Maxson10
begin
  call Down_React;
  gsay_reply(SCRIPT_MAXSON, 226);
  giq_option(4, SCRIPT_MAXSON, 320, MaxsonEnd, BAD_REACTION);
end

procedure Maxson11
begin
  gsay_message(SCRIPT_MAXSON, 229, BAD_REACTION);
  Denounce_Player := 1;
end

procedure Maxson12
begin
  gsay_message(SCRIPT_MAXSON, 229, BAD_REACTION);
  Denounce_Player := 1;
end

procedure Maxson13
begin
  gsay_message(SCRIPT_MAXSON, 231, BAD_REACTION);
  Denounce_Player := 1;
end

procedure Maxson14
begin
  gsay_reply(SCRIPT_MAXSON, 146);
  giq_option(4, SCRIPT_MAXSON, 321, Maxson02, NEUTRAL_REACTION);
end

procedure Maxson15
begin
  gsay_reply(SCRIPT_MAXSON, 141);
  giq_option(4, SCRIPT_MAXSON, 322, Maxson02, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 323, Maxson16, NEUTRAL_REACTION);
end

procedure Maxson16
begin
  gsay_reply(SCRIPT_MAXSON, 223);
  giq_option(4, SCRIPT_MAXSON, 324, Maxson17, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 325, Maxson18, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 326, Maxson17, NEUTRAL_REACTION);
end

procedure Maxson17
begin
  gsay_reply(SCRIPT_MAXSON, 168);
  giq_option(4, SCRIPT_MAXSON, 327, MaxsonEnd, BAD_REACTION);
end

procedure Maxson18
begin
  gsay_message(SCRIPT_MAXSON, 222, NEUTRAL_REACTION);
end

procedure Maxson19
begin
  gsay_reply(SCRIPT_MAXSON, 201);
  giq_option(-3, SCRIPT_MAXSON, 333, Maxson32, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 329, Maxson14, NEUTRAL_REACTION);
  if (global_var(GVAR_MISSING_CARAVAN) == 1) then begin
    giq_option(4, SCRIPT_MAXSON, 330, Maxson06, NEUTRAL_REACTION);
  end
  giq_option(4, SCRIPT_MAXSON, 332, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 331, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson20
begin
  gsay_reply(SCRIPT_MAXSON, 193);
  giq_option(-3, SCRIPT_MAXSON, 333, Maxson32, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 334, Maxson22, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 335, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 336, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson21
begin
  gsay_reply(SCRIPT_MAXSON, 201);
  giq_option(-3, SCRIPT_MAXSON, 333, Maxson32, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 334, Maxson22, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 335, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 336, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson22
begin
  gsay_reply(SCRIPT_MAXSON, 205);
  if (global_var(GVAR_THE_VATS) != 2) then begin
    giq_option(4, SCRIPT_MAXSON, 337, Maxson18, NEUTRAL_REACTION);
  end
  else begin
    if (global_var(GVAR_MASTER_BLOWN) == 1) then begin
      giq_option(4, SCRIPT_MAXSON, 338, Maxson23, NEUTRAL_REACTION);
    end
    else begin
      giq_option(4, SCRIPT_MAXSON, 339, Maxson06, NEUTRAL_REACTION);
    end
  end
end

procedure Maxson23
begin
  gsay_reply(SCRIPT_MAXSON, 213);
  giq_option(4, SCRIPT_MAXSON, 340, Maxson24, NEUTRAL_REACTION);
end

procedure Maxson24
begin
  set_local_var(5, 1);
  gsay_reply(SCRIPT_MAXSON, 219);
  giq_option(4, SCRIPT_MAXSON, 341, Maxson25, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 342, Maxson33, NEUTRAL_REACTION);
end

procedure Maxson25
begin
  set_map_var(19, 1);
  gsay_message(SCRIPT_MAXSON, 221, NEUTRAL_REACTION);
end

procedure Maxson26
begin
  gsay_reply(SCRIPT_MAXSON, 210);
  giq_option(4, SCRIPT_MAXSON, 343, Maxson23, NEUTRAL_REACTION);
end

procedure Maxson27
begin
  gsay_reply(SCRIPT_MAXSON, random(138, 140));
  if (local_var(6) == 0) then begin
    giq_option(4, SCRIPT_MAXSON, 344, Maxson28, NEUTRAL_REACTION);
  end
  giq_option(4, SCRIPT_MAXSON, 345, Maxson29, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 346, Maxson30, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 347, Maxson31, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 348, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson28
begin
  variable LVar0 := 0;
  set_local_var(6, 1);
  LVar0 := create_object_sid(216, 0, 0, -1);
  add_obj_to_inven(dude_obj, LVar0);
  gsay_reply(SCRIPT_MAXSON, 186);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson29
begin
  gsay_reply(SCRIPT_MAXSON, 172);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson30
begin
  gsay_reply(SCRIPT_MAXSON, 244);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson31
begin
  gsay_reply(SCRIPT_MAXSON, 262);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson32
begin
  gsay_reply(SCRIPT_MAXSON, 275);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson33
begin
  gsay_reply(SCRIPT_MAXSON, 156);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure Maxson34
begin
  gsay_reply(SCRIPT_MAXSON, 193);
  giq_option(4, SCRIPT_MAXSON, 349, Maxson27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MAXSON, 350, Maxson18, NEUTRAL_REACTION);
end

procedure MaxsonEnd
begin
end

procedure Remove_Player
begin
  set_global_var(GVAR_BECOME_AN_INITIATE, 5);
  set_global_var(GVAR_MARK_BROTHER_2, 0);
  set_global_var(GVAR_MARK_BROTHER_3, 0);
  set_global_var(GVAR_MARK_BROTHER_4, 0);
  set_global_var(GVAR_MARK_BROTHER_5, 0);
  world_map;
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
  exit_line := message_str(SCRIPT_MODREACT, random(100, 105));
end
