#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure gameover;
procedure giveme;
procedure do_dialogue;
procedure master00;
procedure master01;
procedure master02;
procedure master03;
procedure master04;
procedure master05;
procedure master06;
procedure master06_1;
procedure master07;
procedure master08;
procedure master08_1;
procedure master09;
procedure master10;
procedure master11;
procedure master11_1;
procedure master12;
procedure master13;
procedure master14;
procedure master16;
procedure master17;
procedure master17_1;
procedure master18;
procedure master19;
procedure master20;
procedure master21;
procedure master21_1;
procedure master22;
procedure master23;
procedure master24;
procedure master25;
procedure master26;
procedure master27;
procedure master28;
procedure master29;
procedure master30;
procedure master31;
procedure master32;
procedure master33;
procedure master34;
procedure master35;
procedure master36;
procedure master37;
procedure master38;
procedure master38_1;
procedure master38_2;
procedure master39;
procedure master40;
procedure master41;
procedure master42;
procedure master43;
procedure master44;
procedure master45;
procedure master46;
procedure master47;
procedure master200;
procedure master201;
procedure master202;
procedure master203;
procedure mastercbt;
procedure masterend;
procedure damage_p_proc;
procedure combat_p_proc;
procedure map_enter_p_proc;

variable MALE;
variable HOSTILE;
variable DISARM;
variable Only_ONce := 1;
variable LIEUTENANTS;
variable everyother := 1;
variable wimpyother := 1;
variable so_long;

import variable MASTER_HAS_ARMED;
import variable Master_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;

variable exit_line;


procedure start
begin
if (Only_ONce) then begin
    Only_ONce := 0;
    Master_Ptr := self_obj;
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 55);
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_MASTER);
  end
  if (script_action == map_enter_proc) then begin
    call map_enter_p_proc;
  end
  if (script_action == damage_proc) then begin
    call damage_p_proc;
  end
  if (script_action == combat_proc) then begin
    call combat_p_proc;
  end
  if (script_action == talk_proc) then begin
    if (global_var(GVAR_MASTER_BLOWN) == 0) then begin
      call do_dialogue;
    end
  end
  else begin
    if (script_action == destroy_proc) then begin
      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_BAD_MONSTER, global_var(GVAR_BAD_MONSTER) + 1);
        if ((global_var(GVAR_BAD_MONSTER) % 6) == 0) then begin
          set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) + 1);
        end
      end
      set_global_var(GVAR_MASTER_BLOWN, 1);
      if (global_var(GVAR_VATS_BLOWN) == 0) then begin
        set_global_var(GVAR_VATS_STATUS, 1);
      end
      set_global_var(GVAR_DESTROY_MASTER_5, 2);
      if (global_var(GVAR_COUNTDOWN_TO_DESTRUCTION) == 0) then begin
        set_global_var(GVAR_COUNTDOWN_TO_DESTRUCTION, game_time / 10);
      end
    end
    else begin
      if (script_action == look_at_proc) then begin
        script_overrides;
        display_msg(message_str(SCRIPT_MASTER, 100));
      end
      else begin
        if (script_action == timed_event_proc) then begin
          set_global_var(GVAR_MORPHEUS_DELIVERS_PLAYER, 3);
          dialogue_system_enter;
        end
        else begin
          if (script_action == critter_proc) then begin
            if (local_var(5)) then begin
              if (obj_can_see_obj(self_obj, dude_obj)) then begin
                HOSTILE := 1;
              end
            end
            if (HOSTILE) then begin
              set_local_var(5, 1);
              HOSTILE := 0;
              attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
            end
            if (global_var(GVAR_MORPHEUS_DELIVERS_PLAYER) == 2) then begin
            end
            else begin
              if (tile_distance_objs(self_obj, dude_obj) < 10) then begin
                dialogue_system_enter;
              end
            end
          end
        end
      end
    end
  end
end

procedure gameover
begin
  so_long := 1;
end

procedure giveme
begin
  variable LVar0 := 0;
  if (obj_is_carrying_obj_pid(dude_obj, 194)) then begin
    LVar0 := obj_carrying_pid_obj(dude_obj, 194);
    rm_obj_from_inven(dude_obj, LVar0);
    destroy_object(LVar0);
    call master14;
  end
  else begin
    call master13;
  end
end

procedure do_dialogue
begin
  call Get_Reaction;
  start_gdialog(SCRIPT_MASTER, self_obj, 4, HEAD_MASTER, BACKGROUND_MASTER);
  gsay_start;
  MALE := dude_is_male;
  if (local_var(5)) then begin
    call master203;
  end
  else begin
    if (local_var(4)) then begin
      if (global_var(GVAR_BOMB_DISARMED)) then begin
        call master46;
      end
      else begin
        if (is_success(do_check(dude_obj, 3, -1))) then begin
          call master43;
        end
        else begin
          call master44;
        end
      end
    end
    else begin
      set_local_var(4, 1);
      call master00;
    end
  end
  gsay_end;
  end_dialogue;
  if (so_long) then begin
    play_gmovie(10);
    play_gmovie(7);
    metarule(13, 0);
  end
end

procedure master00
begin
  gsay_reply(SCRIPT_MASTER, 101);
  giq_option(4, SCRIPT_MASTER, 102, master01, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 103, master04, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 104, mastercbt, BAD_REACTION);
  giq_option(5, SCRIPT_MASTER, 105, master38, NEUTRAL_REACTION);
  giq_option(-3, SCRIPT_MASTER, 106, master42, NEUTRAL_REACTION);
end

procedure master01
begin
  gsay_reply(SCRIPT_MASTER, 107);
  giq_option(4, SCRIPT_MASTER, 108, master02, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 109, gameover, NEUTRAL_REACTION);
end

procedure master02
begin
  gsay_reply(SCRIPT_MASTER, 110);
  giq_option(4, SCRIPT_MASTER, 111, master03, BAD_REACTION);
  giq_option(4, SCRIPT_MASTER, 112, gameover, NEUTRAL_REACTION);
end

procedure master03
begin
  gsay_message(SCRIPT_MASTER, 113, BAD_REACTION);
  call mastercbt;
end

procedure master04
begin
  gsay_reply(SCRIPT_MASTER, 114);
  giq_option(6, SCRIPT_MASTER, 115, master36, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_MASTER, 116, master05, NEUTRAL_REACTION);
end

procedure master05
begin
  gsay_reply(SCRIPT_MASTER, 117);
  giq_option(4, SCRIPT_MASTER, 118, master06, NEUTRAL_REACTION);
end

procedure master06
begin
  gsay_reply(SCRIPT_MASTER, 119);
  giq_option(6, SCRIPT_MASTER, 120, master06_1, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 121, master27, BAD_REACTION);
end

procedure master06_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
    call master07;
  end
  else begin
    call master35;
  end
end

procedure master07
begin
  gsay_reply(SCRIPT_MASTER, 122);
  giq_option(7, SCRIPT_MASTER, 123, master08, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 124, master33, BAD_REACTION);
  giq_option(5, SCRIPT_MASTER, 125, master31, NEUTRAL_REACTION);
end

procedure master08
begin
  gsay_reply(SCRIPT_MASTER, 126);
  giq_option(5, SCRIPT_MASTER, 127, master08_1, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 128, master01, GOOD_REACTION);
  giq_option(4, SCRIPT_MASTER, 129, mastercbt, BAD_REACTION);
end

procedure master08_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
    call master09;
  end
  else begin
    call master30;
  end
end

procedure master09
begin
  gsay_reply(SCRIPT_MASTER, 130);
  giq_option(5, SCRIPT_MASTER, 131, mastercbt, BAD_REACTION);
  giq_option(6, SCRIPT_MASTER, 132, master10, NEUTRAL_REACTION);
  if (obj_carrying_pid_obj(dude_obj, 194) or global_var(GVAR_DESTROY_MASTER_6)) then begin
    giq_option(7, SCRIPT_MASTER, 133, master11, NEUTRAL_REACTION);
  end
end

procedure master10
begin
  gsay_message(SCRIPT_MASTER, 134, BAD_REACTION);
  call mastercbt;
end

procedure master11
begin
  gsay_reply(SCRIPT_MASTER, 135);
  giq_option(6, SCRIPT_MASTER, 136, master11_1, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 137, master20, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 138, master28, NEUTRAL_REACTION);
end

procedure master11_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
    call master12;
  end
  else begin
    call master29;
  end
end

procedure master12
begin
  gsay_reply(SCRIPT_MASTER, 139);
  giq_option(6, SCRIPT_MASTER, 140, giveme, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_MASTER, 141, master26, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 142, master20, NEUTRAL_REACTION);
end

procedure master13
begin
  gsay_message(SCRIPT_MASTER, 143, BAD_REACTION);
  call mastercbt;
end

procedure master14
begin
  game_time_advance(game_ticks(180));
  gsay_reply(SCRIPT_MASTER, 145);
  giq_option(5, SCRIPT_MASTER, 146, master16, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_MASTER, 147, master17, NEUTRAL_REACTION);
end

procedure master16
begin
  gsay_message(SCRIPT_MASTER, 148, BAD_REACTION);
  call mastercbt;
end

procedure master17
begin
  gsay_reply(SCRIPT_MASTER, 149);
  giq_option(6, SCRIPT_MASTER, 150, master17_1, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 151, master18, NEUTRAL_REACTION);
end

procedure master17_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
    call master19;
  end
  else begin
    call master16;
  end
end

procedure master18
begin
  gsay_message(SCRIPT_MASTER, 152, BAD_REACTION);
  call mastercbt;
end

procedure master19
begin
  gsay_message(SCRIPT_MASTER, 153, NEUTRAL_REACTION);
  MASTER_HAS_ARMED := 1;
  if (global_var(GVAR_COUNTDOWN_TO_DESTRUCTION) == 0) then begin
    set_global_var(GVAR_COUNTDOWN_TO_DESTRUCTION, game_time / 10);
    set_global_var(GVAR_MASTER_BLOWN, 1);
    if (global_var(GVAR_VATS_BLOWN) == 0) then begin
      set_global_var(GVAR_VATS_STATUS, 1);
    end
    set_global_var(GVAR_DESTROY_MASTER_5, 2);
  end
end

procedure master20
begin
  gsay_message(SCRIPT_MASTER, 154, NEUTRAL_REACTION);
  call master21;
end

procedure master21
begin
  gsay_reply(SCRIPT_MASTER, 155);
  giq_option(6, SCRIPT_MASTER, 156, master25, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 157, master21_1, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 158, master24, NEUTRAL_REACTION);
end

procedure master21_1
begin
  if (global_var(GVAR_LIEUTENANTS_DEAD) == 1) then begin
    call master22;
  end
  else begin
    call master23;
  end
end

procedure master22
begin
  gsay_message(SCRIPT_MASTER, 159, BAD_REACTION);
  call mastercbt;
end

procedure master23
begin
  gsay_message(SCRIPT_MASTER, 160, BAD_REACTION);
  call mastercbt;
end

procedure master24
begin
  gsay_message(SCRIPT_MASTER, 161, BAD_REACTION);
  call mastercbt;
end

procedure master25
begin
  gsay_reply(SCRIPT_MASTER, 162);
  giq_option(6, SCRIPT_MASTER, 163, master17, NEUTRAL_REACTION);
  giq_option(6, SCRIPT_MASTER, 164, master16, NEUTRAL_REACTION);
end

procedure master26
begin
  gsay_reply(SCRIPT_MASTER, 165);
  giq_option(4, SCRIPT_MASTER, 166, mastercbt, BAD_REACTION);
  giq_option(4, SCRIPT_MASTER, 167, master01, GOOD_REACTION);
end

procedure master27
begin
  gsay_reply(SCRIPT_MASTER, 168);
  giq_option(4, SCRIPT_MASTER, 169, mastercbt, BAD_REACTION);
  giq_option(4, SCRIPT_MASTER, 170, master01, NEUTRAL_REACTION);
end

procedure master28
begin
  gsay_message(SCRIPT_MASTER, 171, BAD_REACTION);
  call mastercbt;
end

procedure master29
begin
  gsay_message(SCRIPT_MASTER, 172, BAD_REACTION);
  call mastercbt;
end

procedure master30
begin
  gsay_reply(SCRIPT_MASTER, 173);
  giq_option(4, SCRIPT_MASTER, 174, mastercbt, BAD_REACTION);
  giq_option(4, SCRIPT_MASTER, 175, master01, NEUTRAL_REACTION);
end

procedure master31
begin
  gsay_reply(SCRIPT_MASTER, 176);
  giq_option(5, SCRIPT_MASTER, 177, master32, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_MASTER, 178, mastercbt, BAD_REACTION);
end

procedure master32
begin
  gsay_reply(SCRIPT_MASTER, 179);
  giq_option(5, SCRIPT_MASTER, 180, mastercbt, BAD_REACTION);
  giq_option(5, SCRIPT_MASTER, 181, master01, GOOD_REACTION);
end

procedure master33
begin
  gsay_reply(SCRIPT_MASTER, 182);
  giq_option(4, SCRIPT_MASTER, 183, master34, NEUTRAL_REACTION);
end

procedure master34
begin
  gsay_reply(SCRIPT_MASTER, 184);
  giq_option(4, SCRIPT_MASTER, 185, master01, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 186, mastercbt, BAD_REACTION);
end

procedure master35
begin
  gsay_reply(SCRIPT_MASTER, 187);
  giq_option(4, SCRIPT_MASTER, 188, master01, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 189, mastercbt, BAD_REACTION);
  giq_option(5, SCRIPT_MASTER, 190, master33, NEUTRAL_REACTION);
end

procedure master36
begin
  gsay_reply(SCRIPT_MASTER, 191);
  giq_option(5, SCRIPT_MASTER, 192, master06, NEUTRAL_REACTION);
  giq_option(5, SCRIPT_MASTER, 193, master37, BAD_REACTION);
end

procedure master37
begin
  gsay_message(SCRIPT_MASTER, 194, BAD_REACTION);
  call mastercbt;
end

procedure master38
begin
  gsay_reply(SCRIPT_MASTER, 195);
  giq_option(5, SCRIPT_MASTER, 197, mastercbt, BAD_REACTION);
  if ((global_var(GVAR_BECOME_AN_INITIATE) == 1) or (global_var(GVAR_BECOME_AN_INITIATE) == 2)) then begin
    giq_option(5, SCRIPT_MASTER, 198, master38_2, NEUTRAL_REACTION);
  end
end

procedure master38_1
begin
  if (is_success(roll_vs_skill(dude_obj, 14, -10))) then begin
    call master41;
  end
  else begin
    call master40;
  end
end

procedure master38_2
begin
  if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
    call master39;
  end
  else begin
    call master40;
  end
end

procedure master39
begin
  gsay_message(SCRIPT_MASTER, 199, NEUTRAL_REACTION);
  call masterend;
end

procedure master40
begin
  gsay_message(SCRIPT_MASTER, 200, BAD_REACTION);
  call mastercbt;
end

procedure master41
begin
  gsay_message(SCRIPT_MASTER, 201, NEUTRAL_REACTION);
  call masterend;
end

procedure master42
begin
  gsay_message(SCRIPT_MASTER, 202, BAD_REACTION);
  call mastercbt;
end

procedure master43
begin
  gsay_message(SCRIPT_MASTER, 203, BAD_REACTION);
  call mastercbt;
end

procedure master44
begin
  gsay_reply(SCRIPT_MASTER, 204);
  giq_option(4, SCRIPT_MASTER, 300, masterend, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 205, master01, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 206, master05, BAD_REACTION);
end

procedure master45
begin
  gsay_message(SCRIPT_MASTER, 207, BAD_REACTION);
  call mastercbt;
end

procedure master46
begin
  gsay_reply(SCRIPT_MASTER, 208);
  giq_option(4, SCRIPT_MASTER, 209, master47, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_MASTER, 210, mastercbt, BAD_REACTION);
end

procedure master47
begin
  gsay_message(SCRIPT_MASTER, 211, BAD_REACTION);
  call mastercbt;
end

procedure master200
begin
  gsay_message(SCRIPT_MASTER, 212, NEUTRAL_REACTION);
end

procedure master201
begin
  gsay_message(SCRIPT_MASTER, 213, NEUTRAL_REACTION);
end

procedure master202
begin
  gsay_message(SCRIPT_MASTER, 214, NEUTRAL_REACTION);
end

procedure master203
begin
  gsay_message(SCRIPT_MASTER, 215, BAD_REACTION);
  call mastercbt;
end

procedure mastercbt
begin
  HOSTILE := 1;
end

procedure masterend
begin
end

procedure damage_p_proc
begin
  set_local_var(6, 1);
end

procedure combat_p_proc
begin
  if (fixed_param == 4) then begin
    if (everyother == 1) then begin
      everyother := 0;
      if (combat_difficulty == 0) then begin
        if (wimpyother == 1) then begin
          wimpyother := 0;
          set_map_var(4, 1);
        end
        else begin
          wimpyother := 1;
        end
      end
      else begin
        set_map_var(4, 1);
      end
    end
    else begin
      everyother := 1;
    end
  end
end

procedure map_enter_p_proc
begin
  animate_stand_obj(self_obj);
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
