#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure leadercbt;
procedure leaderend;
procedure leader00;
procedure leader02;
procedure leader03;
procedure leader03a;
procedure leader04;
procedure leader04a;
procedure leader04b;
procedure leader05;
procedure leader05a;
procedure leader06;
procedure leader06a;
procedure leader06b;
procedure leader07;
procedure leader08;
procedure leader09;
procedure leader10;
procedure leader10a;
procedure leader11;
procedure leader12;
procedure leader13;
procedure leader14;
procedure leader15;
procedure leader15a;
procedure leader15b;
procedure leader16;
procedure leader17;
procedure leader18;
procedure leader19;
procedure leader19a;
procedure leader19b;
procedure leader20;
procedure leader21;
procedure leader22;
procedure leader23;
procedure leader24;
procedure leader25;
procedure leader26;
procedure leader27;
procedure leader28;
procedure leader29;
procedure leader30;
procedure leader31;

variable Hostile;
variable init_teams;
variable has_parts;
variable repair_skill;
variable 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 start
begin
	if (not(init_teams)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 31);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GHOUL_NORMAL);
		init_teams := 1;
	end
	if (script_action == talk_proc) then begin
		call do_dialogue;
	end
	else begin
		if (script_action == look_at_proc) then begin
			script_overrides;
			display_msg(message_str(SCRIPT_LEADER, 100));
		end
		else begin
			if (script_action == pickup_proc) then begin
				Hostile := 1;
			end
			else begin
				if (script_action == critter_proc) then begin
					if (Hostile) then begin
						Hostile := 0;
						attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
					end
					else begin
						if (obj_can_see_obj(self_obj, dude_obj)) then begin
							if (local_var(7) == 0) then begin
								set_local_var(7, 1);
								if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
									float_msg(self_obj, message_str(SCRIPT_LEADER, 193), FLOAT_MSG_YELLOW);
								end
								else begin
									float_msg(self_obj, message_str(SCRIPT_LEADER, 194), FLOAT_MSG_YELLOW);
								end
							end
						end
					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_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
			end
		end
	end
end

procedure do_dialogue
begin
	call Get_Reaction;
	has_parts := obj_carrying_pid_obj(dude_obj, 98);
	start_gdialog(SCRIPT_LEADER, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(4)) then begin
		if (local_var(1) < 2) then begin
			call leader30;
		end
		else begin
			if (global_var(GVAR_NECROP_WATER_PUMP_FIXED) == 2) then begin
				call leader26;
			end
			else begin
				if (local_var(6) == 1) then begin
					call leader20;
				end
				else begin
					if (global_var(GVAR_NECROP_WATER_CHIP_TAKEN) == 1) then begin
						call leader29;
					end
					else begin
						if (local_var(5) == 1) then begin
							if (has_parts) then begin
								call leader18;
							end
							else begin
								call leader16;
							end
						end
						else begin
							call leader00;
						end
					end
				end
			end
		end
	end
	else begin
		set_local_var(4, 1);
		call leader00;
	end
	gsay_end;
	end_dialogue;
end

procedure leadercbt
begin
	call Big_Down_React;
	Hostile := 1;
end

procedure leaderend
begin
end

procedure leader00
begin
	gsay_reply(SCRIPT_LEADER, 101);
	giq_option(4, SCRIPT_LEADER, 102, leader04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 103, leader03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_LEADER, 104, leader02, NEUTRAL_REACTION);
end

procedure leader02
begin
	gsay_reply(SCRIPT_LEADER, 105);
	giq_option(-3, SCRIPT_LEADER, 106, leaderend, NEUTRAL_REACTION);
end

procedure leader03
begin
	gsay_reply(SCRIPT_LEADER, 107);
	giq_option(4, SCRIPT_LEADER, 108, leader04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 109, leader03a, BAD_REACTION);
	giq_option(4, SCRIPT_LEADER, 110, leaderend, NEUTRAL_REACTION);
end

procedure leader03a
begin
	call Down_React;
	call leader30;
end

procedure leader04
begin
	gsay_reply(SCRIPT_LEADER, 111);
	if (global_var(GVAR_SET_DEAD) != 1) then begin
		giq_option(4, SCRIPT_LEADER, 112, leader05, NEUTRAL_REACTION);
	end
	else begin
		giq_option(4, SCRIPT_LEADER, 113, leader23, NEUTRAL_REACTION);
	end
	giq_option(5, SCRIPT_LEADER, 114, leader04a, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_LEADER, 115, leader04b, BAD_REACTION);
end

procedure leader04a
begin
	if (global_var(GVAR_SET_DEAD) != 1) then begin
		call leader06;
	end
	else begin
		call leader07;
	end
end

procedure leader04b
begin
	call Down_React;
	call leader05;
end

procedure leader05
begin
	gsay_reply(SCRIPT_LEADER, 116);
	giq_option(4, SCRIPT_LEADER, 117, leader05a, BAD_REACTION);
	giq_option(4, SCRIPT_LEADER, 118, leader06, NEUTRAL_REACTION);
end

procedure leader05a
begin
	call Down_React;
	call leader06;
end

procedure leader06
begin
	gsay_reply(SCRIPT_LEADER, 119);
	giq_option(4, SCRIPT_LEADER, 120, leader07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 121, leader06a, BAD_REACTION);
	giq_option(4, SCRIPT_LEADER, 122, leader08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 123, leader06b, BAD_REACTION);
end

procedure leader06a
begin
	call Down_React;
	call leader08;
end

procedure leader06b
begin
	call Down_React;
	call leader07;
end

procedure leader07
begin
	gsay_reply(SCRIPT_LEADER, 124);
	giq_option(6, SCRIPT_LEADER, 125, leader25, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 126, leader09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 127, leader24, NEUTRAL_REACTION);
end

procedure leader08
begin
	gsay_reply(SCRIPT_LEADER, 128);
	giq_option(4, SCRIPT_LEADER, 129, leaderend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 130, leader09, NEUTRAL_REACTION);
end

procedure leader09
begin
	gsay_reply(SCRIPT_LEADER, 131);
	giq_option(4, SCRIPT_LEADER, 129, leader10, NEUTRAL_REACTION);
end

procedure leader10
begin
	gsay_reply(SCRIPT_LEADER, 132);
	giq_option(4, SCRIPT_LEADER, 133, leader10a, BAD_REACTION);
	giq_option(4, SCRIPT_LEADER, 134, leader11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 135, leader11, NEUTRAL_REACTION);
end

procedure leader10a
begin
	call Down_React;
	call leader13;
end

procedure leader11
begin
	gsay_reply(SCRIPT_LEADER, 136);
	giq_option(4, SCRIPT_LEADER, 137, leader12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 138, leader14, NEUTRAL_REACTION);
end

procedure leader12
begin
	gsay_message(SCRIPT_LEADER, 139, NEUTRAL_REACTION);
	call leadercbt;
end

procedure leader13
begin
	gsay_reply(SCRIPT_LEADER, 140);
	giq_option(4, SCRIPT_LEADER, 141, leader11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 142, leadercbt, BAD_REACTION);
end

procedure leader14
begin
	gsay_reply(SCRIPT_LEADER, 143);
	giq_option(4, SCRIPT_LEADER, 144, leader15, NEUTRAL_REACTION);
	if (has_parts) then begin
		giq_option(4, SCRIPT_LEADER, 145, leader21, NEUTRAL_REACTION);
	end
end

procedure leader15
begin
	gsay_reply(SCRIPT_LEADER, 146);
	giq_option(4, SCRIPT_LEADER, 147, leader12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 148, leader15b, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 149, leader15a, BAD_REACTION);
end

procedure leader15a
begin
	call Down_React;
	call leader15b;
end

procedure leader15b
begin
	if (global_var(GVAR_NECROP_WATER_PUMP_FIXED) != 2) then begin
		set_global_var(GVAR_NECROP_WATER_PUMP_FIXED, 1);
	end
	set_local_var(5, 1);
	gsay_reply(SCRIPT_LEADER, 150);
	giq_option(4, SCRIPT_LEADER, 151, leaderend, NEUTRAL_REACTION);
end

procedure leader16
begin
	gsay_reply(SCRIPT_LEADER, 152);
	giq_option(4, SCRIPT_LEADER, 153, leader17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 154, leader12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 155, leader22, NEUTRAL_REACTION);
end

procedure leader17
begin
	gsay_message(SCRIPT_LEADER, 156, NEUTRAL_REACTION);
end

procedure leader18
begin
	gsay_reply(SCRIPT_LEADER, 157);
	giq_option(4, SCRIPT_LEADER, 158, leader19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 159, leader12, NEUTRAL_REACTION);
end

procedure leader19
begin
	set_local_var(6, 1);
	repair_skill := has_skill(dude_obj, 13);
	if (repair_skill < 60) then begin
		call leader19a;
	end
	else begin
		call leader19b;
	end
end

procedure leader19a
begin
	stuff := create_object_sid(76, 0, 0, -1);
	add_obj_to_inven(dude_obj, stuff);
	stuff := create_object_sid(76, 0, 0, -1);
	add_obj_to_inven(dude_obj, stuff);
	stuff := create_object_sid(76, 0, 0, -1);
	add_obj_to_inven(dude_obj, stuff);
	gsay_message(SCRIPT_LEADER, 160, NEUTRAL_REACTION);
end

procedure leader19b
begin
	gsay_message(SCRIPT_LEADER, 161, NEUTRAL_REACTION);
end

procedure leader20
begin
	gsay_reply(SCRIPT_LEADER, 162);
	giq_option(4, SCRIPT_LEADER, 163, leader17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 164, leader12, NEUTRAL_REACTION);
end

procedure leader21
begin
	gsay_reply(SCRIPT_LEADER, 165);
	giq_option(4, SCRIPT_LEADER, 166, leader19, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 167, leader12, NEUTRAL_REACTION);
end

procedure leader22
begin
	gsay_reply(SCRIPT_LEADER, 168);
	giq_option(4, SCRIPT_LEADER, 169, leaderend, NEUTRAL_REACTION);
end

procedure leader23
begin
	gsay_reply(SCRIPT_LEADER, 170);
	giq_option(4, SCRIPT_LEADER, 171, leader07, NEUTRAL_REACTION);
end

procedure leader24
begin
	gsay_reply(SCRIPT_LEADER, 172);
	giq_option(4, SCRIPT_LEADER, 173, leader09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 129, leader10, NEUTRAL_REACTION);
end

procedure leader25
begin
	gsay_reply(SCRIPT_LEADER, 174);
	giq_option(4, SCRIPT_LEADER, 175, leader09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 176, leader24, NEUTRAL_REACTION);
end

procedure leader26
begin
	gsay_reply(SCRIPT_LEADER, 177);
	giq_option(4, SCRIPT_LEADER, 178, leaderend, NEUTRAL_REACTION);
end

procedure leader27
begin
	gsay_reply(SCRIPT_LEADER, 179);
	giq_option(4, SCRIPT_LEADER, 180, leader28, NEUTRAL_REACTION);
end

procedure leader28
begin
	gsay_reply(SCRIPT_LEADER, 181);
	giq_option(4, SCRIPT_LEADER, 129, leaderend, NEUTRAL_REACTION);
end

procedure leader29
begin
	gsay_reply(SCRIPT_LEADER, 182);
	if (local_var(5) == 1) then begin
		giq_option(4, SCRIPT_LEADER, 183, leader22, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_LEADER, 184, leader12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_LEADER, 185, leader31, NEUTRAL_REACTION);
end

procedure leader30
begin
	gsay_message(SCRIPT_LEADER, 186, NEUTRAL_REACTION);
end

procedure leader31
begin
	gsay_reply(SCRIPT_LEADER, 187);
	giq_option(4, SCRIPT_LEADER, 189, leader15, NEUTRAL_REACTION);
	if (has_parts) then begin
		giq_option(4, SCRIPT_LEADER, 145, leader21, NEUTRAL_REACTION);
	end
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