#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure critter_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure look_at_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure Tine01;
procedure Tine02;
procedure Tine03;
procedure Tine04;
procedure Tine05;
procedure Tine06;
procedure Tine07;
procedure Tine08;
procedure TineBarter;
procedure Tine_barter1;
procedure Tine_barter2;
procedure TineEnd;

variable hostile1;
variable initialized1;
variable round_counter;

import variable Tine_barter;
import variable AdyStor1_ptr;
import variable AdyStor2_ptr;

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(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_ADYTOWNER);
		initialized1 := 1;
	end
	else begin
		if (script_action == combat_proc) then begin
			call combat_p_proc;
		end
		else begin
			if (script_action == critter_proc) then begin
				call critter_p_proc;
			end
			else begin
				if (script_action == damage_proc) then begin
					call damage_p_proc;
				end
				else begin
					if (script_action == destroy_proc) then begin
						call destroy_p_proc;
					end
					else begin
						if (script_action == look_at_proc) then begin
							call look_at_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
							end
						end
					end
				end
			end
		end
	end
end

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		if (elevation(self_obj) == 0) then begin
			round_counter := round_counter + 1;
			if (round_counter > 3) then begin
				if (global_var(GVAR_ENEMY_ADYTUM) == 0) then begin
					set_global_var(GVAR_ENEMY_ADYTUM, 1);
					set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (Tine_barter != 0) then begin
			dialogue_system_enter;
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(5, 1);
	end
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_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

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

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	variable LVar0 := 0;
	call Get_Reaction;
	LVar0 := random(0, 1);
	if (Tine_barter == 0) then begin
		if (LVar0 == 1) then begin
			move_obj_inven_to_obj(AdyStor1_ptr, self_obj);
		end
		else begin
			move_obj_inven_to_obj(AdyStor2_ptr, self_obj);
		end
	end
	if (Tine_barter == -1) then begin
		float_msg(self_obj, message_str(SCRIPT_JTGENGRD, 136), FLOAT_MSG_RED);
		hostile1 := 1;
	end
	else begin
		if ((Tine_barter != 0) and ((local_var(5) == 1) or (global_var(GVAR_ENEMY_ADYTUM) == 1))) then begin
			float_msg(self_obj, message_str(SCRIPT_ENEMY, 104), FLOAT_MSG_RED);
		end
		else begin
			start_gdialog(SCRIPT_TAYLOR, self_obj, 4, -1, -1);
			gsay_start;
			if (Tine_barter == 1) then begin
				call Tine_barter1;
			end
			else begin
				if (Tine_barter == 2) then begin
					call Tine_barter2;
				end
				else begin
					if (local_var(4)) then begin
						call Tine07;
					end
					else begin
						call Tine01;
					end
				end
			end
			gsay_end;
			end_dialogue;
		end
	end
	if (Tine_barter == 1) then begin
		move_obj_inven_to_obj(self_obj, AdyStor1_ptr);
	end
	else begin
		if (Tine_barter == 2) then begin
			move_obj_inven_to_obj(self_obj, AdyStor2_ptr);
		end
		else begin
			if (LVar0 == 1) then begin
				move_obj_inven_to_obj(self_obj, AdyStor1_ptr);
			end
			else begin
				move_obj_inven_to_obj(self_obj, AdyStor2_ptr);
			end
		end
	end
	Tine_barter := 0;
end

procedure Tine01
begin
	if (local_var(1) < 2) then begin
		gsay_reply(SCRIPT_TAYLOR, 102);
	end
	else begin
		gsay_reply(SCRIPT_TAYLOR, 103);
	end
	call Tine02;
end

procedure Tine02
begin
	if (not(local_var(4))) then begin
		giq_option(4, SCRIPT_TAYLOR, message_str(SCRIPT_TAYLOR, 104) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_TAYLOR, 105), Tine04, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_TAYLOR, 106, Tine03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TAYLOR, 107, Tine05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TAYLOR, 108, Tine08, NEUTRAL_REACTION);
	giq_option(0, SCRIPT_TAYLOR, 109, Tine06, NEUTRAL_REACTION);
end

procedure Tine03
begin
	if (local_var(1) < 2) then begin
		gsay_reply(SCRIPT_TAYLOR, 110);
	end
	else begin
		gsay_reply(SCRIPT_TAYLOR, 111);
	end
	call Tine02;
end

procedure Tine04
begin
	set_local_var(4, 1);
	if (local_var(1) < 2) then begin
		gsay_reply(SCRIPT_TAYLOR, 112);
	end
	else begin
		gsay_reply(SCRIPT_TAYLOR, 113);
	end
	call Tine02;
end

procedure Tine05
begin
	gdialog_mod_barter(0);
	gsay_message(SCRIPT_TAYLOR, 114, NEUTRAL_REACTION);
end

procedure Tine06
begin
	if (local_var(1) < 2) then begin
		gsay_message(SCRIPT_TAYLOR, 115, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_TAYLOR, 116, NEUTRAL_REACTION);
	end
end

procedure Tine07
begin
	if (local_var(1) < 2) then begin
		gsay_reply(SCRIPT_TAYLOR, 117);
	end
	else begin
		gsay_reply(SCRIPT_TAYLOR, 118);
	end
	call Tine02;
end

procedure Tine08
begin
	if (local_var(0) < 2) then begin
		gsay_reply(SCRIPT_TAYLOR, 119);
	end
	else begin
		gsay_reply(SCRIPT_TAYLOR, 120);
	end
	call Tine02;
end

procedure TineBarter
begin
	gdialog_mod_barter(20);
	gsay_message(SCRIPT_MODREACT, 100, NEUTRAL_REACTION);
end

procedure Tine_barter1
begin
	move_obj_inven_to_obj(AdyStor1_ptr, self_obj);
	gsay_reply(SCRIPT_HBETH, 293);
	giq_option(0, SCRIPT_MODREACT, 106, TineBarter, NEUTRAL_REACTION);
end

procedure Tine_barter2
begin
	move_obj_inven_to_obj(AdyStor2_ptr, self_obj);
	gsay_reply(SCRIPT_HBETH, 293);
	giq_option(0, SCRIPT_MODREACT, 106, TineBarter, NEUTRAL_REACTION);
end

procedure TineEnd
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