#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure pickup_p_proc;
procedure look_at_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Gretch00;
procedure Gretch00a;
procedure Gretch00b;
procedure Gretch01;
procedure Gretch01a;
procedure Gretch02;
procedure Gretch02a;
procedure Gretch03;
procedure Gretch03a;
procedure Gretch04;
procedure Gretch05;
procedure Gretch05a;
procedure Gretch06;
procedure Gretch07;
procedure Gretch08;
procedure Gretch09;
procedure Gretch10;
procedure GretchEnd;
procedure GretchCombat;

variable hostile1;
variable initialized1;
variable warned_about_messing;

import variable Gretch_call;
import variable messing_with_Morbid_stuff;

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 damage_p_proc;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 19);
		initialized1 := 1;
	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
			else begin
				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 timed_event_p_proc;
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (messing_with_Morbid_stuff) then begin
			if ((tile_distance_objs(self_obj, dude_obj) < 12) and (elevation(self_obj) == elevation(dude_obj))) then begin
				if (warned_about_messing) then begin
					hostile1 := 1;
				end
				else begin
					warned_about_messing := 1;
					float_msg(self_obj, message_str(SCRIPT_GRETCH, 141), FLOAT_MSG_RED);
				end
			end
			messing_with_Morbid_stuff := 0;
		end
		if (Gretch_call) then begin
			Gretch_call := 0;
			hostile1 := 1;
		end
	end
	if (global_var(GVAR_CURE_JARVIS_7) == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CURE_JARVIS_7, 1);
		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
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(0) == 0) then begin
		display_msg(message_str(SCRIPT_GRETCH, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_GRETCH, 101));
	end
end

procedure talk_p_proc
begin
	start_gdialog(SCRIPT_GRETCH, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(0) == 1) then begin
		call Gretch01;
	end
	else begin
		call Gretch00;
	end
	gsay_end;
	end_dialogue;
end

procedure timed_event_p_proc
begin
end

procedure Gretch00
begin
	gsay_reply(SCRIPT_GRETCH, 102);
	giq_option(-3, SCRIPT_GRETCH, 103, Gretch07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 104, Gretch00a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 105, Gretch00b, BAD_REACTION);
	giq_option(4, SCRIPT_GRETCH, 106, Gretch02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 107, GretchEnd, NEUTRAL_REACTION);
end

procedure Gretch00a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch04;
	end
	else begin
		call Gretch02;
	end
end

procedure Gretch00b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch02;
	end
	else begin
		call Gretch08;
	end
end

procedure Gretch01
begin
	gsay_reply(SCRIPT_GRETCH, 108);
	giq_option(4, SCRIPT_GRETCH, 109, Gretch02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 110, Gretch01a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 111, GretchEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_GRETCH, 103, Gretch07, NEUTRAL_REACTION);
end

procedure Gretch01a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch05;
	end
	else begin
		call Gretch02;
	end
end

procedure Gretch02
begin
	gsay_reply(SCRIPT_GRETCH, 112);
	giq_option(4, SCRIPT_GRETCH, 113, Gretch04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 114, Gretch03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 115, Gretch02a, NEUTRAL_REACTION);
end

procedure Gretch02a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch03;
	end
	else begin
		call Gretch07;
	end
end

procedure Gretch03
begin
	gsay_reply(SCRIPT_GRETCH, 116);
	giq_option(4, SCRIPT_GRETCH, 117, Gretch04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 118, Gretch03a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 119, GretchEnd, NEUTRAL_REACTION);
end

procedure Gretch03a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch05;
	end
	else begin
		call Gretch04;
	end
end

procedure Gretch04
begin
	gsay_reply(SCRIPT_GRETCH, 120);
	giq_option(4, SCRIPT_GRETCH, 121, Gretch06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 122, Gretch05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 123, GretchEnd, NEUTRAL_REACTION);
end

procedure Gretch05
begin
	set_global_var(GVAR_DESTROY_MASTER_1, 1);
	gsay_reply(SCRIPT_GRETCH, 124);
	giq_option(4, SCRIPT_GRETCH, 125, Gretch05a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 126, GretchCombat, BAD_REACTION);
	giq_option(4, SCRIPT_GRETCH, 127, GretchEnd, NEUTRAL_REACTION);
end

procedure Gretch05a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call Gretch09;
	end
	else begin
		call Gretch06;
	end
end

procedure Gretch06
begin
	set_global_var(GVAR_DESTROY_MASTER_1, 1);
	gsay_reply(SCRIPT_GRETCH, 128);
	giq_option(4, SCRIPT_GRETCH, 129, GretchEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 130, Gretch09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 131, Gretch07, NEUTRAL_REACTION);
end

procedure Gretch07
begin
	gsay_reply(SCRIPT_GRETCH, 132);
	giq_option(4, SCRIPT_GRETCH, 133, GretchEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 134, Gretch04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 135, Gretch08, BAD_REACTION);
	giq_option(-3, SCRIPT_GRETCH, 136, Gretch10, NEUTRAL_REACTION);
end

procedure Gretch08
begin
	gsay_message(SCRIPT_GRETCH, 137, BAD_REACTION);
end

procedure Gretch09
begin
	gsay_reply(SCRIPT_GRETCH, 138);
	giq_option(4, SCRIPT_GRETCH, 139, GretchEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GRETCH, 126, GretchCombat, BAD_REACTION);
end

procedure Gretch10
begin
	gsay_message(SCRIPT_GRETCH, 140, NEUTRAL_REACTION);
end

procedure GretchEnd
begin
end

procedure GretchCombat
begin
	hostile1 := 1;
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

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CURE_JARVIS_7, 1);
	end
end