#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure timeforwhat;
procedure do_dialogue;
procedure ghoulcbt;
procedure ghoul00;
procedure ghoul01;
procedure ghoul02;
procedure ghoul03;
procedure ghoul04;
procedure ghoul05;
procedure ghoul05a;
procedure ghoul06;
procedure ghoul07;
procedure ghoul08;
procedure ghoul09;
procedure ghoul10;
procedure ghoul11;

variable Hostile;
variable initialized1;
variable noevent;
variable loopcount;


procedure start
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 32);
		initialized1 := 1;
	end
	if (script_action == timed_event_proc) then begin
		call timeforwhat;
	end
	else begin
		if (script_action == talk_proc) then begin
			call do_dialogue;
		end
		else begin
			if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_MANAGER, 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
							loopcount := loopcount + 1;
							if (loopcount > 40) then begin
								loopcount := 0;
								if (tile_distance_objs(self_obj, dude_obj) < 7) then begin
									if (obj_can_see_obj(self_obj, dude_obj)) then begin
										if (noevent == 0) then begin
											noevent := 1;
											add_timer_event(self_obj, game_ticks(5), 0);
										end
									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_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
					end
				end
			end
		end
	end
end

procedure timeforwhat
begin
	if (fixed_param == 1) then begin
		Hostile := 1;
	end
	else begin
		if (local_var(0) == 0) then begin
			dialogue_system_enter;
		end
	end
end

procedure do_dialogue
begin
	start_gdialog(SCRIPT_MANAGER, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(0)) then begin
		call ghoul08;
	end
	else begin
		set_local_var(0, 1);
		call ghoul00;
	end
	gsay_end;
	end_dialogue;
end

procedure ghoulcbt
begin
	add_timer_event(self_obj, game_ticks(2), 1);
end

procedure ghoul00
begin
	gsay_reply(SCRIPT_MANAGER, 101);
	giq_option(6, SCRIPT_MANAGER, 102, ghoul01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MANAGER, 103, ghoul04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MANAGER, 118, ghoul09, NEUTRAL_REACTION);
	giq_option(3, SCRIPT_MANAGER, 104, ghoul05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_MANAGER, 105, ghoul01, NEUTRAL_REACTION);
end

procedure ghoul01
begin
	gsay_reply(SCRIPT_MANAGER, 106);
	giq_option(6, SCRIPT_MANAGER, 107, ghoul02, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_MANAGER, 108, ghoul03, NEUTRAL_REACTION);
end

procedure ghoul02
begin
	gsay_message(SCRIPT_MANAGER, 109, NEUTRAL_REACTION);
end

procedure ghoul03
begin
	gsay_message(SCRIPT_MANAGER, 110, NEUTRAL_REACTION);
end

procedure ghoul04
begin
	gsay_message(SCRIPT_MANAGER, 111, NEUTRAL_REACTION);
end

procedure ghoul05
begin
	gsay_reply(SCRIPT_MANAGER, 112);
	giq_option(6, SCRIPT_MANAGER, 113, ghoul07, NEUTRAL_REACTION);
	giq_option(8, SCRIPT_MANAGER, 114, ghoul05a, NEUTRAL_REACTION);
end

procedure ghoul05a
begin
	if (is_success(do_check(dude_obj, 14, 0))) then begin
		call ghoul06;
	end
	else begin
		call ghoul07;
	end
end

procedure ghoul06
begin
	gsay_message(SCRIPT_MANAGER, 115, NEUTRAL_REACTION);
end

procedure ghoul07
begin
	gsay_message(SCRIPT_MANAGER, 116, NEUTRAL_REACTION);
	call ghoulcbt;
end

procedure ghoul08
begin
	gsay_message(SCRIPT_MANAGER, 117, NEUTRAL_REACTION);
end

procedure ghoul09
begin
	gsay_reply(SCRIPT_MANAGER, 119);
	giq_option(4, SCRIPT_MANAGER, 120, ghoul10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_MANAGER, 121, ghoul11, NEUTRAL_REACTION);
end

procedure ghoul10
begin
	gsay_message(SCRIPT_MANAGER, 122, NEUTRAL_REACTION);
end

procedure ghoul11
begin
	gsay_message(SCRIPT_MANAGER, 123, NEUTRAL_REACTION);
end