#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure do_dialogue;
procedure tolya00;
procedure tolya01;
procedure tolya02;
procedure tolya03;
procedure tolya04;
procedure tolya05;
procedure tolya06;
procedure tolya07;
procedure tolya08;
procedure tolya09;
procedure tolya10;
procedure tolyaend;
procedure talk_p_proc;
procedure combat;
procedure Critter_Action;
procedure damage_p_proc;

variable hostile1;
variable only_once := 1;

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 (only_once) then begin
		only_once := 0;
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 6);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GENERIC_RAIDER);
	end
	if (script_action == look_at_proc) then begin
		display_msg(message_str(SCRIPT_TOLYA, 100));
		script_overrides;
	end
	else begin
		if (script_action == talk_proc) then begin
			call talk_p_proc;
		end
		else begin
			if (script_action == critter_proc) then begin
				call Critter_Action;
				if (hostile1) then begin
					hostile1 := 0;
					attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
				end
			end
			else begin
				if (script_action == pickup_proc) then begin
					hostile1 := 1;
				end
				else begin
					if (script_action == damage_proc) then begin
						call damage_p_proc;
					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
							set_global_var(GVAR_ENEMY_RAIDERS, 1);
							set_global_var(GVAR_GARLS_FRIEND, 0);
							set_global_var(GVAR_TOTAL_RAIDERS, global_var(GVAR_TOTAL_RAIDERS) - 1);
						end
					end
				end
			end
		end
	end
end

procedure do_dialogue
begin
	set_local_var(3, 1);
	call Get_Reaction;
	start_gdialog(SCRIPT_TOLYA, self_obj, 4, -1, -1);
	gsay_start;
	if (local_var(4) == 5) then begin
		call tolya10;
	end
	else begin
		set_local_var(4, local_var(4) + 1);
		call tolya00;
	end
	gsay_end;
	end_dialogue;
end

procedure tolya00
begin
	gsay_reply(SCRIPT_TOLYA, 101);
	giq_option(4, SCRIPT_TOLYA, message_str(SCRIPT_TOLYA, 102) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_TOLYA, 103), tolya01, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_TOLYA, 104, tolya02, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_TOLYA, 105, tolya03, NEUTRAL_REACTION);
end

procedure tolya01
begin
	gsay_reply(SCRIPT_TOLYA, 106);
	giq_option(4, SCRIPT_TOLYA, 107, tolya04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TOLYA, 108, tolyaend, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TOLYA, 109, tolya05, NEUTRAL_REACTION);
end

procedure tolya02
begin
	gsay_reply(SCRIPT_TOLYA, 110);
	giq_option(4, SCRIPT_TOLYA, message_str(SCRIPT_TOLYA, 111) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_TOLYA, 112), tolya07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TOLYA, 113, combat, NEUTRAL_REACTION);
end

procedure tolya03
begin
	gsay_message(SCRIPT_TOLYA, 114, NEUTRAL_REACTION);
end

procedure tolya04
begin
	gsay_reply(SCRIPT_TOLYA, 115);
	giq_option(4, SCRIPT_TOLYA, 116, tolya06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TOLYA, 117, tolyaend, NEUTRAL_REACTION);
end

procedure tolya05
begin
	gsay_reply(SCRIPT_TOLYA, 118);
	giq_option(4, SCRIPT_TOLYA, 119, tolya08, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_TOLYA, 120, tolya09, NEUTRAL_REACTION);
end

procedure tolya06
begin
	gsay_message(SCRIPT_TOLYA, 121, NEUTRAL_REACTION);
	call combat;
end

procedure tolya07
begin
	gsay_message(SCRIPT_TOLYA, 122, NEUTRAL_REACTION);
end

procedure tolya08
begin
	gsay_reply(SCRIPT_TOLYA, 123);
	set_local_var(6, 1);
	if (local_var(5) == 0) then begin
		giq_option(4, SCRIPT_TOLYA, 124, tolya09, NEUTRAL_REACTION);
	end
	else begin
		call tolyaend;
	end
end

procedure tolya09
begin
	gsay_reply(SCRIPT_TOLYA, 125);
	set_local_var(5, 1);
	if (local_var(6) == 0) then begin
		giq_option(4, SCRIPT_TOLYA, 126, tolya08, NEUTRAL_REACTION);
	end
	else begin
		call tolyaend;
	end
end

procedure tolya10
begin
	gsay_message(SCRIPT_TOLYA, 127, NEUTRAL_REACTION);
end

procedure tolyaend
begin
end

procedure talk_p_proc
begin
	if (global_var(GVAR_MISTAKEN_ID) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENRAIDR, random(102, 106)), FLOAT_MSG_YELLOW);
	end
	else begin
		call do_dialogue;
	end
end

procedure combat
begin
	if (global_var(GVAR_MISTAKEN_ID) == 1) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 87);
	end
	hostile1 := 1;
end

procedure Critter_Action
begin
	variable LVar0 := 0;
	if (global_var(GVAR_TANDI_STATUS) == 2) then begin
		set_global_var(GVAR_ENEMY_RAIDERS, 1);
	end
	else begin
		if (global_var(GVAR_MISTAKEN_ID)) then begin
			set_global_var(GVAR_ENEMY_RAIDERS, 0);
			if (tile_distance_objs(self_obj, dude_obj) < 3) then begin
				LVar0 := tile_num_in_direction(tile_num(self_obj), random(0, 5), 3);
				if (tile_distance(tile_num(self_obj), LVar0) > 2) then begin
					if (random(0, 9) == 0) then begin
						float_msg(self_obj, message_str(SCRIPT_GENRAIDR, random(102, 106)), FLOAT_MSG_YELLOW);
					end
					animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_WALK);
				end
			end
		end
		else begin
			if (global_var(GVAR_TANDI_ESCAPE)) then begin
				set_global_var(GVAR_ENEMY_RAIDERS, 1);
			end
			if (obj_can_see_obj(self_obj, dude_obj)) then begin
				if (global_var(GVAR_RAID_LOOTING)) then begin
					set_global_var(GVAR_ENEMY_RAIDERS, 1);
				end
			end
			if (map_var(2) == 1) then begin
				set_global_var(GVAR_ENEMY_RAIDERS, 1);
			end
		end
	end
	if (global_var(GVAR_ENEMY_RAIDERS) and obj_can_see_obj(self_obj, dude_obj)) then begin
		hostile1 := 1;
	end
	if (tile_distance_objs(self_obj, dude_obj) > 12) then begin
		hostile1 := 0;
	end
end

procedure damage_p_proc
begin
	if (global_var(GVAR_MISTAKEN_ID) == 0) then begin
		set_global_var(GVAR_ENEMY_RAIDERS, 1);
	end
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