#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure look_at_p_proc;
procedure description_p_proc;
procedure talk_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure pickup_p_proc;
procedure Smitty01;
procedure Smitty02;
procedure Smitty03;
procedure Smitty04;
procedure Smitty05;
procedure Smitty06;
procedure Smitty07;
procedure Smitty08;
procedure Smitty09;
procedure Smitty10;
procedure Smitty11;
procedure Smitty12;
procedure Smitty14;
procedure Smitty14a;
procedure Smitty15;
procedure SmittyEnd;

variable Initialize := 1;
variable DisplayMessage := 100;

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 (Initialize) then begin
		if (obj_is_carrying_obj_pid(self_obj, 41) == 0) then begin
			item_caps_adjust(self_obj, random(500, 750));
		end
		if ((global_var(GVAR_BLADES) == 9103) or (global_var(GVAR_BLADES) == 9102)) then begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
		end
		else begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		end
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_PEASANT);
		Initialize := 0;
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_SMITTY, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_SMITTY, 101));
	end
end

procedure description_p_proc
begin
	script_overrides;
	if (local_var(4) == 1) then begin
		display_msg(message_str(SCRIPT_SMITTY, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_SMITTY, 101));
	end
end

procedure talk_p_proc
begin
	if (global_var(GVAR_ENEMY_ADYTUM) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, random(100, 105)), FLOAT_MSG_RED);
	end
	else begin
		if ((local_var(4) == 1) and (get_critter_stat(dude_obj, 4) < 4)) then begin
			float_msg(self_obj, message_str(SCRIPT_SMITTY, 117), FLOAT_MSG_NORMAL);
		end
		else begin
			if (local_var(5) == 2) then begin
				float_msg(self_obj, message_str(SCRIPT_SMITTY, 131), FLOAT_MSG_NORMAL);
			end
			else begin
				call Get_Reaction;
				start_gdialog(SCRIPT_SMITTY, self_obj, 4, -1, -1);
				gsay_start;
				if (global_var(GVAR_FIX_FARM) == 9303) then begin
					call Smitty11;
				end
				else begin
					if (global_var(GVAR_FIX_FARM) == 9304) then begin
						if (local_var(5) == 0) then begin
							call Smitty12;
						end
						else begin
							if (local_var(5) == 9307) then begin
								if (obj_is_carrying_obj_pid(dude_obj, 15)) then begin
									call Smitty14;
								end
								else begin
									call Smitty15;
								end
							end
						end
					end
					else begin
						if (local_var(1) < 2) then begin
							DisplayMessage := 103;
							call Smitty01;
						end
						else begin
							DisplayMessage := 102;
							call Smitty01;
						end
					end
				end
				gsay_end;
				end_dialogue;
			end
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_ADYTUM, 1);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_ADYTUM, 1);
		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
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
	end
end

procedure pickup_p_proc
begin
	set_global_var(GVAR_ENEMY_ADYTUM, 1);
end

procedure Smitty01
begin
	gsay_reply(SCRIPT_SMITTY, DisplayMessage);
	DisplayMessage := 112;
	if (local_var(4) == 0) then begin
		giq_option(4, SCRIPT_SMITTY, 104, Smitty03, NEUTRAL_REACTION);
	end
	if ((global_var(GVAR_FIX_FARM) < 9303) and (global_var(GVAR_FIX_FARM) > 2)) then begin
		giq_option(4, SCRIPT_SMITTY, 105, Smitty10, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_SMITTY, 106, Smitty02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SMITTY, 107, Smitty04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_SMITTY, 108, Smitty06, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_SMITTY, 109, Smitty05, NEUTRAL_REACTION);
end

procedure Smitty02
begin
	DisplayMessage := 110;
	call Smitty01;
end

procedure Smitty03
begin
	set_local_var(4, 1);
	DisplayMessage := 111;
	call Smitty01;
end

procedure Smitty04
begin
	gsay_reply(SCRIPT_SMITTY, DisplayMessage);
	gsay_option(SCRIPT_SMITTY, 113, Smitty07, NEUTRAL_REACTION);
	gsay_option(SCRIPT_SMITTY, 114, Smitty08, NEUTRAL_REACTION);
	gsay_option(SCRIPT_SMITTY, 115, Smitty09, NEUTRAL_REACTION);
	gsay_option(SCRIPT_SMITTY, 116, Smitty06, NEUTRAL_REACTION);
end

procedure Smitty05
begin
	set_local_var(4, 1);
	gsay_message(SCRIPT_SMITTY, 117, NEUTRAL_REACTION);
end

procedure Smitty06
begin
	gsay_message(SCRIPT_SMITTY, 118, NEUTRAL_REACTION);
end

procedure Smitty07
begin
	DisplayMessage := 119;
	call Smitty04;
end

procedure Smitty08
begin
	DisplayMessage := 120;
	call Smitty04;
end

procedure Smitty09
begin
	DisplayMessage := 121;
	call Smitty04;
end

procedure Smitty10
begin
	gsay_message(SCRIPT_SMITTY, 122, NEUTRAL_REACTION);
	gfade_out(600);
	game_time_advance(10 * 60 * 60);
	gfade_in(600);
	set_global_var(GVAR_FIX_FARM, 9303);
	gsay_message(SCRIPT_SMITTY, 123, NEUTRAL_REACTION);
end

procedure Smitty11
begin
	gsay_message(SCRIPT_SMITTY, 124, NEUTRAL_REACTION);
end

procedure Smitty12
begin
	gsay_message(SCRIPT_SMITTY, 125, NEUTRAL_REACTION);
	set_local_var(5, 9307);
	if (obj_is_carrying_obj_pid(dude_obj, 15)) then begin
		call Smitty14;
	end
end

procedure Smitty14
begin
	gsay_reply(SCRIPT_SMITTY, 127);
	gsay_option(SCRIPT_SMITTY, 126, Smitty14a, NEUTRAL_REACTION);
end

procedure Smitty14a
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	gsay_message(SCRIPT_SMITTY, 128, NEUTRAL_REACTION);
	LVar1 := create_object_sid(42, 0, 0, -1);
	move_obj_inven_to_obj(dude_obj, LVar1);
	move_obj_inven_to_obj(LVar1, dude_obj);
	LVar0 := obj_carrying_pid_obj(dude_obj, 15);
	rm_obj_from_inven(dude_obj, LVar0);
	destroy_object(LVar0);
	destroy_object(LVar1);
	gfade_out(600);
	game_time_advance(10 * 60 * 60 * 2);
	gfade_in(600);
	gsay_message(SCRIPT_SMITTY, 129, NEUTRAL_REACTION);
	LVar0 := create_object_sid(233, 0, 0, -1);
	add_obj_to_inven(dude_obj, LVar0);
	set_local_var(5, 2);
end

procedure Smitty15
begin
	gsay_message(SCRIPT_SMITTY, 130, NEUTRAL_REACTION);
end

procedure SmittyEnd
begin
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