#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 PlayerQuestions;
procedure Decker01;
procedure Decker02;
procedure Decker03;
procedure Decker04;
procedure Decker05;
procedure Decker06;
procedure Decker07;
procedure Decker08;
procedure Decker09;
procedure Decker10;
procedure Decker11;
procedure Decker12;
procedure Decker13;
procedure Decker14;
procedure Decker15;
procedure Decker16;
procedure Decker17;
procedure Decker18;
procedure Decker19;
procedure Decker25;
procedure Decker26;
procedure Decker27;
procedure Decker28;
procedure Decker29;
procedure Decker30;
procedure Decker31;
procedure Decker32;
procedure Decker33;
procedure Decker34;
procedure Decker35;
procedure Decker36;
procedure DeckerEndAcceptJob1;
procedure DeckerEndDeclineJob1;
procedure DeckerEndAcceptJob2;
procedure DeckerEndDeclineJob2;
procedure DeckerEndInsult;
procedure DeckerEndNormal;
procedure DeckerTransport;

variable hostile1;
variable Only_Once := 1;
variable travel;

import variable Kane_Ptr;
import variable Decker_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;
    Decker_Ptr := self_obj;
    if (map_var(50) == 1) then begin
      set_obj_visibility(self_obj, 1);
    end
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 38);
    critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_DECKER);
  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 (hostile1) then begin
    hostile1 := 0;
    attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
  end
  if (travel == 1) then begin
    travel := 0;
    call DeckerTransport;
  end
  else begin
    if (tile_distance_objs(self_obj, dude_obj) <= 10) then begin
      dialogue_system_enter;
    end
  end
end

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

procedure talk_p_proc
begin
  set_global_var(GVAR_DECKER_KNOWN, 1);
  call Get_Reaction;
  if ((map_var(49) == 1) or (map_var(45) != 1)) then begin
    if (map_var(52) == 0) then begin
      float_msg(self_obj, message_str(SCRIPT_DECKER, 230), FLOAT_MSG_RED);
      call combat;
    end
    else begin
      float_msg(self_obj, message_str(SCRIPT_DECKER, 231), FLOAT_MSG_RED);
      call combat;
    end
  end
  else begin
    set_map_var(45, 0);
    if (local_var(4) == 0) then begin
      set_local_var(4, 1);
      start_gdialog(SCRIPT_DECKER, self_obj, 4, HEAD_DECKER, BACKGROUND_HUB);
      gsay_start;
      call Decker01;
      gsay_end;
      end_dialogue;
    end
    else begin
      if (global_var(GVAR_KILL_JAIN) == 2) then begin
        start_gdialog(SCRIPT_DECKER, self_obj, 4, HEAD_DECKER, BACKGROUND_HUB);
        gsay_start;
        call Decker15;
        gsay_end;
        end_dialogue;
      end
      else begin
        if (global_var(GVAR_KILL_MERCHANT) == 2) then begin
          start_gdialog(SCRIPT_DECKER, self_obj, 4, HEAD_DECKER, BACKGROUND_HUB);
          gsay_start;
          call Decker26;
          gsay_end;
          end_dialogue;
        end
      end
    end
  end
end

procedure destroy_p_proc
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_map_var(50, 1);
  set_global_var(GVAR_DECKER_STATUS, 1);
  if ((map_var(49) == 1) and (map_var(50) == 1)) then begin
    set_global_var(GVAR_UNDERGROUND_STATUS, 1);
    set_map_var(11, 1);
    set_map_var(44, 1);
  end
end

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

procedure PlayerQuestions
begin
  giq_option(4, SCRIPT_DECKER, 144, Decker17, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 145, Decker25, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 146, DeckerEndNormal, NEUTRAL_REACTION);
end

procedure Decker01
begin
  gsay_reply(SCRIPT_DECKER, 103);
  giq_option(4, SCRIPT_DECKER, message_str(SCRIPT_DECKER, 104) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_DECKER, 105), Decker02, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 106, Decker13, BAD_REACTION);
end

procedure Decker02
begin
  gsay_reply(SCRIPT_DECKER, 107);
  giq_option(4, SCRIPT_DECKER, 108, Decker03, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 109, Decker14, NEUTRAL_REACTION);
end

procedure Decker03
begin
  gsay_reply(SCRIPT_DECKER, 110);
  giq_option(4, SCRIPT_DECKER, 111, Decker04, NEUTRAL_REACTION);
end

procedure Decker04
begin
  gsay_reply(SCRIPT_DECKER, 112);
  giq_option(4, SCRIPT_DECKER, 113, Decker05, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 114, Decker11, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 115, Decker10, BAD_REACTION);
end

procedure Decker05
begin
  gsay_reply(SCRIPT_DECKER, 116);
  giq_option(4, SCRIPT_DECKER, 117, Decker06, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 118, Decker10, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 119, Decker06, NEUTRAL_REACTION);
end

procedure Decker06
begin
  gsay_reply(SCRIPT_DECKER, 120);
  giq_option(4, SCRIPT_DECKER, 121, Decker07, NEUTRAL_REACTION);
end

procedure Decker07
begin
  gsay_reply(SCRIPT_DECKER, 122);
  giq_option(4, SCRIPT_DECKER, 123, Decker08, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 124, Decker09, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 125, Decker10, NEUTRAL_REACTION);
end

procedure Decker08
begin
  gsay_message(SCRIPT_DECKER, 126, NEUTRAL_REACTION);
  call DeckerEndAcceptJob1;
end

procedure Decker09
begin
  gsay_reply(SCRIPT_DECKER, 127);
  giq_option(4, SCRIPT_DECKER, 128, Decker08, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 129, Decker10, NEUTRAL_REACTION);
end

procedure Decker10
begin
  gsay_message(SCRIPT_DECKER, 130, NEUTRAL_REACTION);
  call DeckerEndDeclineJob1;
end

procedure Decker11
begin
  gsay_reply(SCRIPT_DECKER, 131);
  giq_option(4, SCRIPT_DECKER, 132, Decker10, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 133, Decker12, NEUTRAL_REACTION);
end

procedure Decker12
begin
  gsay_message(SCRIPT_DECKER, 134, NEUTRAL_REACTION);
  call DeckerEndAcceptJob1;
end

procedure Decker13
begin
  gsay_reply(SCRIPT_DECKER, 135);
  giq_option(4, SCRIPT_DECKER, message_str(SCRIPT_DECKER, 136) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_DECKER, 137), Decker02, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 138, Decker14, NEUTRAL_REACTION);
end

procedure Decker14
begin
  gsay_message(SCRIPT_DECKER, 139, BAD_REACTION);
  call DeckerEndInsult;
end

procedure Decker15
begin
  gsay_reply(SCRIPT_DECKER, 140);
  giq_option(4, SCRIPT_DECKER, 141, Decker16, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 142, DeckerEndNormal, NEUTRAL_REACTION);
end

procedure Decker16
begin
  gsay_reply(SCRIPT_DECKER, 143);
  call PlayerQuestions;
end

procedure Decker17
begin
  gsay_reply(SCRIPT_DECKER, 148);
  giq_option(4, SCRIPT_DECKER, 149, Decker18, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 150, Decker16, NEUTRAL_REACTION);
end

procedure Decker18
begin
  gsay_reply(SCRIPT_DECKER, 151);
  giq_option(4, SCRIPT_DECKER, 152, Decker19, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 153, Decker16, NEUTRAL_REACTION);
end

procedure Decker19
begin
  gsay_reply(SCRIPT_DECKER, 154);
  giq_option(4, SCRIPT_DECKER, 155, Decker16, NEUTRAL_REACTION);
end

procedure Decker25
begin
  gsay_reply(SCRIPT_DECKER, 167);
  call PlayerQuestions;
end

procedure Decker26
begin
  if (dude_is_male) then begin
    gsay_reply(SCRIPT_DECKER, 232);
  end
  else begin
    gsay_reply(SCRIPT_DECKER, 233);
  end
  giq_option(4, SCRIPT_DECKER, 169, Decker27, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 170, Decker35, BAD_REACTION);
end

procedure Decker27
begin
  gsay_reply(SCRIPT_DECKER, 171);
  giq_option(4, SCRIPT_DECKER, 172, Decker28, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 173, Decker35, BAD_REACTION);
end

procedure Decker28
begin
  gsay_reply(SCRIPT_DECKER, 174);
  giq_option(4, SCRIPT_DECKER, 175, Decker29, NEUTRAL_REACTION);
end

procedure Decker29
begin
  gsay_reply(SCRIPT_DECKER, 176);
  giq_option(4, SCRIPT_DECKER, 177, Decker30, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 178, Decker32, BAD_REACTION);
end

procedure Decker30
begin
  call Up_React;
  gsay_reply(SCRIPT_DECKER, 179);
  giq_option(4, SCRIPT_DECKER, 180, Decker31, NEUTRAL_REACTION);
end

procedure Decker31
begin
  gsay_message(SCRIPT_DECKER, 181, NEUTRAL_REACTION);
  call DeckerEndAcceptJob2;
end

procedure Decker32
begin
  gsay_reply(SCRIPT_DECKER, 182);
  giq_option(4, SCRIPT_DECKER, 183, Decker33, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 184, Decker34, NEUTRAL_REACTION);
end

procedure Decker33
begin
  gsay_reply(SCRIPT_DECKER, 185);
  giq_option(4, SCRIPT_DECKER, 186, Decker31, NEUTRAL_REACTION);
end

procedure Decker34
begin
  if (dude_is_male) then begin
    gsay_message(SCRIPT_DECKER, 234, BAD_REACTION);
  end
  else begin
    gsay_message(SCRIPT_DECKER, 235, BAD_REACTION);
  end
  call DeckerEndDeclineJob2;
end

procedure Decker35
begin
  gsay_reply(SCRIPT_DECKER, 188);
  giq_option(4, SCRIPT_DECKER, 189, Decker36, NEUTRAL_REACTION);
  giq_option(4, SCRIPT_DECKER, 190, Decker28, NEUTRAL_REACTION);
end

procedure Decker36
begin
  gsay_message(SCRIPT_DECKER, 191, BAD_REACTION);
  call DeckerEndDeclineJob2;
end

procedure DeckerEndAcceptJob1
begin
  set_map_var(46, 2);
  set_global_var(GVAR_KILL_MERCHANT, 1);
  travel := 1;
end

procedure DeckerEndDeclineJob1
begin
  set_map_var(46, 1);
  set_map_var(11, 1);
  set_global_var(GVAR_KILL_MERCHANT, 0);
  travel := 1;
end

procedure DeckerEndAcceptJob2
begin
  set_map_var(47, 3);
  set_global_var(GVAR_KILL_JAIN, 1);
  travel := 1;
end

procedure DeckerEndDeclineJob2
begin
  set_map_var(47, 2);
  set_map_var(11, 1);
  set_global_var(GVAR_KILL_JAIN, 0);
  travel := 1;
end

procedure DeckerEndInsult
begin
  set_map_var(46, 1);
  set_map_var(11, 1);
  set_map_var(44, 1);
  travel := 1;
end

procedure DeckerEndNormal
begin
  travel := 1;
end

procedure DeckerTransport
begin
  gfade_out(1000);
  move_to(Kane_Ptr, 24336, 0);
  move_to(dude_obj, 23736, 0);
  anim(Kane_Ptr, 1000, rotation_to_tile(tile_num(Kane_Ptr), tile_num(dude_obj)));
  anim(dude_obj, 1000, rotation_to_tile(tile_num(dude_obj), tile_num(Kane_Ptr)));
  gfade_in(1000);
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
