#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;
variable GVar1;
variable GVar2;

procedure start;
#include <push.h>
procedure do_dialogue;
procedure go_inside;
procedure weapon_check;
procedure PeasantD00;
procedure PeasantD01;
procedure PeasantD02;
procedure PeasantD03;
procedure PeasantD04;

import variable dog_is_angry;

export variable smartass;
export variable smartass2;


procedure start
begin
	if (not(GVar2)) then begin
		smartass := 0;
		smartass2 := 0;
		GVar2 := 1;
	end
	else begin
		if (script_action == talk_proc) then begin
			call do_dialogue;
		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
			else begin
				if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
					script_overrides;
					display_msg(message_str(SCRIPT_PEASANTD, 100));
				end
				else begin
					if (script_action == critter_proc) then begin
						if (smartass2) then begin
							call PeasantD00;
						end
						if (not(dog_is_angry)) then begin
							call go_inside;
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	call weapon_check;
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		call PeasantD02;
	end
	else begin
		if (not(dog_is_angry)) then begin
			call PeasantD04;
		end
		else begin
			if (GVar1) then begin
				call PeasantD01;
			end
			else begin
				call PeasantD03;
			end
		end
	end
end

procedure go_inside
begin
	animate_move_obj_to_tile(self_obj, 15484, ANIMATE_WALK);
end

procedure weapon_check
begin
	if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3)) then begin
		GVar1 := 1;
	end
	else begin
		GVar1 := 0;
	end
end

procedure PeasantD00
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := random(1, 3);
	if (LVar0 == 1) then begin
		LVar1 := message_str(SCRIPT_PEASANTD, 101);
	end
	else begin
		if (LVar0 == 2) then begin
			LVar1 := message_str(SCRIPT_PEASANTD, 102);
		end
		else begin
			LVar1 := message_str(SCRIPT_PEASANTD, 103);
		end
	end
	float_msg(self_obj, LVar1, FLOAT_MSG_NORMAL);
	smartass2 := 0;
end

procedure PeasantD01
begin
	float_msg(self_obj, message_str(SCRIPT_PEASANTD, 104), FLOAT_MSG_NORMAL);
end

procedure PeasantD02
begin
	float_msg(self_obj, message_str(SCRIPT_PEASANTD, 105), FLOAT_MSG_NORMAL);
end

procedure PeasantD03
begin
	float_msg(self_obj, message_str(SCRIPT_PEASANTD, 106), FLOAT_MSG_NORMAL);
	smartass := 1;
end

procedure PeasantD04
begin
	float_msg(self_obj, message_str(SCRIPT_PEASANTD, 107), FLOAT_MSG_NORMAL);
end