#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;
variable GVar1 := 1;
variable GVar2;

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 combat_p_proc;
procedure gideon00;
procedure gideon01;
procedure gideon01a;
procedure gideon02;
procedure gideon02a;
procedure gideon03;
procedure gideon04;
procedure gideon04a;
procedure gideon04b;
procedure gideon04c;
procedure gideon05;
procedure gideon05a;
procedure gideon05b;
procedure gideon06;
procedure gideon06a;
procedure gideon07;
procedure gideon07a;
procedure gideon07b;
procedure gideon07c;
procedure gideon08;
procedure gideon09;
procedure gideon09a;
procedure gideon09b;
procedure gideon09c;
procedure gideon10;
procedure gideon11;
procedure gideon12;
procedure gideon12a;
procedure gideon12b;
procedure gideon13;
procedure gideon13a;
procedure gideonnull;
procedure dialog_end;

import variable contpan;


procedure start
begin
	if (GVar1) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 50);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BRAINWASHED_CHILDREN);
	end
	if (script_action == look_at_proc) then begin
		call look_at_p_proc;
	end
	else begin
		if (script_action == combat_proc) then begin
			call combat_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
						animate_move_obj_to_tile(self_obj, 15319, ANIMATE_WALK);
					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
	end
end

procedure combat
begin
	use_obj(contpan);
	GVar0 := 1;
end

procedure critter_p_proc
begin
	if (GVar0) then begin
		GVar0 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
end

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

procedure talk_p_proc
begin
	start_gdialog(SCRIPT_GIDEON, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(4) == 0) then begin
		call gideon00;
	end
	else begin
		call gideon01;
	end
	set_local_var(4, 1);
	gsay_end;
	end_dialogue;
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
end

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

procedure combat_p_proc
begin
end

procedure gideon00
begin
	gsay_reply(SCRIPT_GIDEON, 101);
	giq_option(8, SCRIPT_GIDEON, 102, gideon03, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 103, gideon02, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 104, gideon07, GOOD_REACTION + random(0, 2));
	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
		giq_option(4, SCRIPT_GIDEON, 105, gideon07, GOOD_REACTION + random(0, 2));
	end
	giq_option(-3, SCRIPT_GIDEON, 106, gideon08, GOOD_REACTION + random(0, 2));
end

procedure gideon01
begin
	gsay_reply(SCRIPT_GIDEON, 107);
	giq_option(8, SCRIPT_GIDEON, 108, gideon04, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 109, dialog_end, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 112, gideon01a, GOOD_REACTION + random(0, 2));
end

procedure gideon01a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon03;
	end
	else begin
		call gideon08;
	end
end

procedure gideon02
begin
	gsay_reply(SCRIPT_GIDEON, 113);
	giq_option(4, SCRIPT_GIDEON, 114, dialog_end, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 112, gideon02a, GOOD_REACTION + random(0, 2));
	giq_option(-3, SCRIPT_GIDEON, 115, gideon08, GOOD_REACTION + random(0, 2));
end

procedure gideon02a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon09;
	end
	else begin
		call gideon08;
	end
end

procedure gideon03
begin
	gsay_reply(SCRIPT_GIDEON, 116);
	giq_option(8, SCRIPT_GIDEON, 108, gideon04, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 117, gideon04, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 118, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon04
begin
	gsay_reply(SCRIPT_GIDEON, 119);
	giq_option(8, SCRIPT_GIDEON, 120, gideon05, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 121, gideon04a, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 122, gideon04b, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 124, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon04a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon05;
	end
	else begin
		call gideon08;
	end
end

procedure gideon04b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon03;
	end
	else begin
		call gideon11;
	end
end

procedure gideon04c
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon07;
	end
	else begin
		call gideon08;
	end
end

procedure gideon05
begin
	gsay_reply(SCRIPT_GIDEON, 125);
	giq_option(8, SCRIPT_GIDEON, 126, gideon06, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 127, gideon05a, GOOD_REACTION + random(0, 2));
	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
		giq_option(4, SCRIPT_GIDEON, 128, gideon05b, GOOD_REACTION + random(0, 2));
	end
	giq_option(4, SCRIPT_GIDEON, 129, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon05a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon07;
	end
	else begin
		call gideon11;
	end
end

procedure gideon05b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon10;
	end
	else begin
		call gideon06;
	end
end

procedure gideon06
begin
	gsay_reply(SCRIPT_GIDEON, 130);
	giq_option(8, SCRIPT_GIDEON, 131, gideonnull, GOOD_REACTION + random(0, 2));
	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
		giq_option(4, SCRIPT_GIDEON, 132, combat, GOOD_REACTION + random(0, 2));
	end
	giq_option(4, SCRIPT_GIDEON, 133, gideon06a, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 134, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon06a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideonnull;
	end
	else begin
		call gideon08;
	end
end

procedure gideon07
begin
	gsay_reply(SCRIPT_GIDEON, 135);
	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
		giq_option(4, SCRIPT_GIDEON, 136, gideon07b, GOOD_REACTION + random(0, 2));
	end
	giq_option(4, SCRIPT_GIDEON, 137, gideon07c, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 138, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon07a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon10;
	end
	else begin
		call gideon08;
	end
end

procedure gideon07b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon10;
	end
	else begin
		call gideon11;
	end
end

procedure gideon07c
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon06;
	end
	else begin
		call gideon10;
	end
end

procedure gideon08
begin
	gsay_message(SCRIPT_GIDEON, 139, GOOD_REACTION + random(0, 2));
	call dialog_end;
end

procedure gideon09
begin
	gsay_reply(SCRIPT_GIDEON, 107);
	giq_option(4, SCRIPT_GIDEON, 141, gideon09a, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 127, gideon09b, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 124, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon09a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon04;
	end
	else begin
		call gideon08;
	end
end

procedure gideon09b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -20))) then begin
		call gideon07;
	end
	else begin
		call gideon11;
	end
end

procedure gideon09c
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideon10;
	end
	else begin
		call gideon08;
	end
end

procedure gideon10
begin
	gsay_message(SCRIPT_GIDEON, 143, GOOD_REACTION + random(0, 2));
	call combat;
end

procedure gideon11
begin
	gsay_message(SCRIPT_GIDEON, 144, GOOD_REACTION + random(0, 2));
	call dialog_end;
end

procedure gideon12
begin
	gsay_reply(SCRIPT_GIDEON, 145);
	giq_option(8, SCRIPT_GIDEON, 146, gideon06, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 147, gideon12a, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 148, gideon13, GOOD_REACTION + random(0, 2));
	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
		giq_option(4, SCRIPT_GIDEON, 136, gideon12a, GOOD_REACTION + random(0, 2));
	end
	giq_option(4, SCRIPT_GIDEON, 150, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon12a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call gideon13;
	end
	else begin
		call gideon07;
	end
end

procedure gideon12b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call gideon10;
	end
	else begin
		call gideon08;
	end
end

procedure gideon13
begin
	gsay_reply(SCRIPT_GIDEON, 130);
	giq_option(8, SCRIPT_GIDEON, 131, gideonnull, GOOD_REACTION + random(0, 2));
	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
		giq_option(4, SCRIPT_GIDEON, 132, combat, GOOD_REACTION + random(0, 2));
	end
	giq_option(4, SCRIPT_GIDEON, 133, gideon13a, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 151, gideon07, GOOD_REACTION + random(0, 2));
	giq_option(4, SCRIPT_GIDEON, 134, dialog_end, GOOD_REACTION + random(0, 2));
end

procedure gideon13a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, -30))) then begin
		call gideonnull;
	end
	else begin
		call gideon08;
	end
end

procedure gideonnull
begin
	if (local_var(5) == 0) then begin
		set_local_var(5, 1);
		use_obj(contpan);
		use_obj(dude_obj);
		add_obj_to_inven(dude_obj, create_object_sid(123, 0, 0, -1));
		GVar2 := 1000;
		display_msg(message_str(SCRIPT_GIDEON, 501) + GVar2 + message_str(SCRIPT_GIDEON, 502));
		give_exp_points(GVar2);
		add_timer_event(self_obj, game_ticks(10), 1);
	end
end

procedure dialog_end
begin
end