#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 flee_dude;
procedure damage_p_proc;
procedure Flower00;
procedure Flower01;
procedure Flower02;
procedure Flower03;
procedure Flower03a;
procedure Flower04;
procedure Flower04a;
procedure Flower05;
procedure Flower05a;
procedure Flower06;
procedure Flower07;
procedure Flower08;
procedure Flower09;
procedure Flower10;
procedure Flower11;
procedure Flower12;
procedure Flower13;
procedure Flower14;

variable hostile1;
variable Only_Once := 1;

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, 72);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_LITTLE_KIDS);
	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 ((map_var(6) == 1) and (obj_can_see_obj(self_obj, dude_obj) == 1)) then begin
		critter_set_flee_state(self_obj, 1);
	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;
	if ((global_var(GVAR_CATHEDRAL_ENEMY) == 1) or (map_var(0) == 1)) then begin
		call Flower00;
	end
	else begin
		if (global_var(GVAR_CHILDKILLER_REPUTATION) == 1) then begin
			call Flower01;
		end
		else begin
			if (obj_is_carrying_obj_pid(dude_obj, 117) == 1) then begin
				float_msg(self_obj, message_str(SCRIPT_HUBFLRCD, 114), FLOAT_MSG_RED);
			end
			else begin
				start_gdialog(SCRIPT_HUBFLRCD, self_obj, 4, -1, -1);
				gsay_start;
				call Flower02;
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CHILDKILLER_REPUTATION, global_var(GVAR_CHILDKILLER_REPUTATION) + 1);
		set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
	end
	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 look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_HUBFLRCD, 100));
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_WALK);
end

procedure damage_p_proc
begin
	variable LVar0 := 0;
	LVar0 := obj_pid(source_obj);
	if (party_member_obj(LVar0) != 0) then begin
		set_map_var(6, 1);
	end
end

procedure Flower00
begin
	float_msg(self_obj, message_str(SCRIPT_HUBFLRCD, 101), FLOAT_MSG_RED);
	call flee_dude;
end

procedure Flower01
begin
	float_msg(self_obj, message_str(SCRIPT_HUBFLRCD, 102), FLOAT_MSG_RED);
	call flee_dude;
end

procedure Flower02
begin
	if (dude_is_male) then begin
		gsay_reply(SCRIPT_HUBFLRCD, 103);
	end
	else begin
		gsay_reply(SCRIPT_HUBFLRCD, 104);
	end
	giq_option(7, SCRIPT_HUBFLRCD, 106, Flower03, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_HUBFLRCD, 107, Flower04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 108, Flower05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 109, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 110, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 111, Flower07, BAD_REACTION);
	giq_option(-3, SCRIPT_HUBFLRCD, 112, Flower08, GOOD_REACTION);
	giq_option(-3, SCRIPT_HUBFLRCD, 113, Flower09, BAD_REACTION);
end

procedure Flower03
begin
	gsay_reply(SCRIPT_HUBFLRCD, 115);
	giq_option(7, SCRIPT_HUBFLRCD, 116, Flower03a, GOOD_REACTION);
	giq_option(7, SCRIPT_HUBFLRCD, 107, Flower04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 109, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 110, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 119, Flower07, BAD_REACTION);
end

procedure Flower03a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 20)) == 1) then begin
		call Flower10;
	end
	else begin
		call Flower11;
	end
end

procedure Flower04
begin
	gsay_reply(SCRIPT_HUBFLRCD, 121);
	giq_option(7, SCRIPT_HUBFLRCD, 123, Flower12, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_HUBFLRCD, 124, Flower04a, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 125, Flower05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 126, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 110, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 119, Flower07, BAD_REACTION);
end

procedure Flower04a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0)) == 1) then begin
		call Flower13;
	end
	else begin
		call Flower11;
	end
end

procedure Flower05
begin
	gsay_reply(SCRIPT_HUBFLRCD, 129);
	giq_option(7, SCRIPT_HUBFLRCD, 130, Flower05a, GOOD_REACTION);
	giq_option(7, SCRIPT_HUBFLRCD, 131, Flower04a, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 132, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 110, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 119, Flower07, BAD_REACTION);
end

procedure Flower05a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0)) == 1) then begin
		call Flower14;
	end
	else begin
		call Flower11;
	end
end

procedure Flower06
begin
	variable LVar0 := 0;
	LVar0 := create_object_sid(117, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
	gsay_message(SCRIPT_HUBFLRCD, 135, GOOD_REACTION);
end

procedure Flower07
begin
	gsay_message(SCRIPT_HUBFLRCD, 136, BAD_REACTION);
	call flee_dude;
end

procedure Flower08
begin
	variable LVar0 := 0;
	LVar0 := create_object_sid(117, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
	gsay_message(SCRIPT_HUBFLRCD, 137, GOOD_REACTION);
end

procedure Flower09
begin
	gsay_message(SCRIPT_HUBFLRCD, 138, BAD_REACTION);
	call flee_dude;
end

procedure Flower10
begin
	variable LVar0 := 0;
	LVar0 := create_object_sid(117, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
	gsay_message(SCRIPT_HUBFLRCD, 139, GOOD_REACTION);
end

procedure Flower11
begin
	gsay_message(SCRIPT_HUBFLRCD, 140, BAD_REACTION);
	call flee_dude;
end

procedure Flower12
begin
	gsay_reply(SCRIPT_HUBFLRCD, 141);
	giq_option(7, SCRIPT_HUBFLRCD, 142, Flower05a, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_HUBFLRCD, 143, Flower04a, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 144, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 145, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 146, Flower07, BAD_REACTION);
end

procedure Flower13
begin
	gsay_reply(SCRIPT_HUBFLRCD, 147);
	giq_option(7, SCRIPT_HUBFLRCD, 148, Flower11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 149, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 150, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 151, Flower07, BAD_REACTION);
end

procedure Flower14
begin
	set_global_var(GVAR_DESTROY_VATS_9, 1);
	gsay_reply(SCRIPT_HUBFLRCD, 152);
	giq_option(7, SCRIPT_HUBFLRCD, 153, Flower11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 154, Flower06, GOOD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 155, Flower07, BAD_REACTION);
	giq_option(4, SCRIPT_HUBFLRCD, 151, Flower07, BAD_REACTION);
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