#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 destroy_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure GenSupr00;
procedure GenSupr03;
procedure GenSupr03a;
procedure GenSupr03b;
procedure GenSupr04;
procedure GenSupr05;
procedure GenSupr06;
procedure GenSupr07;
procedure GenSupr08;
procedure GenSuprAlert;
procedure GenSuprxx;

variable initialized1;
variable hostile1;
variable round_count;
variable home_tile := 7000;
variable waypoint;

import variable ignoring_dude;
import variable removal_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, 34);
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_SUPER_MUTANT_AGGRESSIVE);
		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 == destroy_proc) then begin
					call destroy_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

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		round_count := round_count + 1;
	end
	if (round_count > 2) then begin
		call GenSuprAlert;
	end
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if (hostile1) then begin
			hostile1 := 0;
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
		else begin
			if (global_var(GVAR_VATS_ALERT)) then begin
				hostile1 := 1;
			end
			else begin
				if (not(ignoring_dude)) then begin
					if (tile_distance_objs(self_obj, dude_obj) < 12) then begin
						dialogue_system_enter;
					end
				end
			end
		end
	end
	if (cur_map_index == MAP_MBENT) then begin
		if (map_var(0)) then begin
			if (not(waypoint)) then begin
				if (tile_num(self_obj) != 22312) then begin
					animate_move_obj_to_tile(self_obj, 22312, ANIMATE_WALK);
				end
				else begin
					waypoint := 1;
				end
			end
			else begin
				if (waypoint == 1) then begin
					if (tile_num(self_obj) != 31944) then begin
						animate_move_obj_to_tile(self_obj, 31944, ANIMATE_WALK);
					end
					else begin
						waypoint := 2;
					end
				end
				else begin
					if (waypoint == 2) then begin
						removal_ptr := self_obj;
					end
				end
			end
		end
	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_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

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (global_var(GVAR_LIEUTENANTS_DEAD)) then begin
		call GenSupr08;
	end
	else begin
		if ((obj_item_subtype(critter_inven_obj(dude_obj, 1)) == 3) or (obj_item_subtype(critter_inven_obj(dude_obj, 2)) == 3) and not(hostile1)) then begin
			if (random(0, 5) == 5) then begin
				call GenSupr00;
			end
			else begin
				hostile1 := 1;
			end
		end
		else begin
			start_gdialog(SCRIPT_GENSUPR, self_obj, 4, -1, -1);
			gsay_start;
			call GenSupr03;
			gsay_end;
			end_dialogue;
		end
	end
end

procedure GenSupr00
begin
	float_msg(self_obj, message_str(SCRIPT_GENSUPR, random(101, 103)), FLOAT_MSG_RED);
	hostile1 := 1;
end

procedure GenSupr03
begin
	gsay_reply(SCRIPT_GENSUPR, random(104, 106));
	giq_option(-3, SCRIPT_GENSUPR, 107, GenSupr04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_GENSUPR, 108, GenSupr04, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_GENSUPR, 109, GenSupr05, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_GENSUPR, 110, GenSupr03a, NEUTRAL_REACTION);
	if (obj_pid(critter_inven_obj(dude_obj, 0)) == 113) then begin
		giq_option(6, SCRIPT_GENSUPR, 111, GenSupr03b, NEUTRAL_REACTION);
	end
end

procedure GenSupr03a
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
		call GenSupr07;
	end
	else begin
		call GenSupr06;
	end
end

procedure GenSupr03b
begin
	if (is_success(roll_vs_skill(dude_obj, 14, 25))) then begin
		call GenSupr07;
	end
	else begin
		call GenSupr06;
	end
end

procedure GenSupr04
begin
	hostile1 := 1;
	gsay_message(SCRIPT_GENSUPR, random(112, 114), BAD_REACTION);
end

procedure GenSupr05
begin
	gsay_reply(SCRIPT_GENSUPR, 115);
	giq_option(5, SCRIPT_GENSUPR, 116, GenSuprxx, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_GENSUPR, 117, GenSuprAlert, BAD_REACTION);
end

procedure GenSupr06
begin
	hostile1 := 1;
	gsay_message(SCRIPT_GENSUPR, random(118, 120), BAD_REACTION);
end

procedure GenSupr07
begin
	ignoring_dude := 1;
	gsay_message(SCRIPT_GENSUPR, random(121, 123), NEUTRAL_REACTION);
end

procedure GenSupr08
begin
	float_msg(self_obj, message_str(SCRIPT_GENSUPR, random(124, 127)), FLOAT_MSG_RED);
	hostile1 := 1;
end

procedure GenSuprAlert
begin
	set_global_var(GVAR_VATS_ALERT, 1);
	hostile1 := 1;
end

procedure GenSuprxx
begin
	set_global_var(GVAR_ENTERING_VATS_HOW, 2);
	load_map(32, 5);
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