#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure destroy_p_proc;
procedure Flash00;
procedure Flash01;
procedure Flash00N;
procedure Flash00Na;
procedure Flash01N;
procedure Flash02N;
procedure Flash03N;
procedure Flash03Na;
procedure Flash04N;
procedure Flash05N;
procedure Flash06N;
procedure Flash07N;
procedure Flash07Na;
procedure Flash08Na;
procedure Flash08Nb;
procedure Flash09N;
procedure Flash10N;
procedure Flash11N;
procedure FlashCombat;
procedure FlashEnd;

variable hostile1;
variable loitering;

import variable fetch_dude;
import variable Morbid_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 damage_p_proc;


procedure start
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
				else begin
					if (script_action == timed_event_proc) then begin
						call timed_event_p_proc;
					end
				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 (fetch_dude) then begin
			fetch_dude := 0;
			hostile1 := 1;
		end
	end
	if (global_var(GVAR_CURE_JARVIS_7) == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
		end
	end
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_JUNKTOWN_INVADED) == 1) then begin
		kill_critter(self_obj, 57);
	end
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 19);
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if ((game_time_hour >= 700) and (game_time_hour < 1800)) then begin
		if (not(local_var(0))) then begin
			call Flash00;
		end
		else begin
			call Flash01;
		end
	end
	else begin
		if (loitering) then begin
			call Flash07N;
		end
		else begin
			start_gdialog(SCRIPT_FLASH, self_obj, 4, -1, -1);
			gsay_start;
			if (not(local_var(0))) then begin
				call Flash00N;
			end
			else begin
				call Flash03N;
			end
			gsay_end;
			end_dialogue;
		end
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			loitering := 1;
			dialogue_system_enter;
		end
	end
	else begin
		if (fixed_param == 2) then begin
			hostile1 := 1;
		end
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CURE_JARVIS_7, 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
	end
end

procedure Flash00
begin
	set_local_var(0, 1);
	float_msg(self_obj, message_str(SCRIPT_FLASH, 100), FLOAT_MSG_NORMAL);
end

procedure Flash01
begin
	float_msg(self_obj, message_str(SCRIPT_FLASH, 106), FLOAT_MSG_NORMAL);
end

procedure Flash00N
begin
	set_local_var(0, 1);
	gsay_reply(SCRIPT_FLASH, 101);
	giq_option(4, SCRIPT_FLASH, 102, Flash01N, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FLASH, 103, Flash10N, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FLASH, 104, Flash00Na, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FLASH, 128, Flash11N, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FLASH, 105, Flash10N, NEUTRAL_REACTION);
end

procedure Flash00Na
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	LVar1 := get_critter_stat(dude_obj, 7);
	LVar2 := get_critter_stat(dude_obj, 35);
	if (LVar2 == LVar1) then begin
		call Flash01N;
	end
	else begin
		LVar0 := -20;
		if (LVar2 < 5) then begin
			LVar0 := 0;
		end
		if (is_success(roll_vs_skill(dude_obj, 14, LVar0))) then begin
			call Flash02N;
		end
		else begin
			call Flash01N;
		end
	end
end

procedure Flash01N
begin
	float_msg(self_obj, message_str(SCRIPT_FLASH, 107), FLOAT_MSG_LIGHT_RED);
	add_timer_event(self_obj, game_ticks(10), 2);
end

procedure Flash02N
begin
	gsay_message(SCRIPT_FLASH, 108, NEUTRAL_REACTION);
end

procedure Flash03N
begin
	set_local_var(0, 1);
	gsay_reply(SCRIPT_FLASH, 109);
	giq_option(4, SCRIPT_FLASH, 110, Flash03Na, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FLASH, 111, Flash03Na, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FLASH, 112, Flash06N, NEUTRAL_REACTION);
end

procedure Flash03Na
begin
	if (elevation(self_obj) == elevation(Morbid_ptr)) then begin
		call Flash05N;
	end
	else begin
		call Flash04N;
	end
end

procedure Flash04N
begin
	gsay_reply(SCRIPT_FLASH, 113);
	giq_option(4, SCRIPT_FLASH, 126, Flash11N, NEUTRAL_REACTION);
	add_timer_event(self_obj, game_ticks(10), 1);
end

procedure Flash05N
begin
	gsay_message(SCRIPT_FLASH, 114, NEUTRAL_REACTION);
end

procedure Flash06N
begin
	gsay_message(SCRIPT_FLASH, 115, NEUTRAL_REACTION);
end

procedure Flash07N
begin
	gsay_reply(SCRIPT_FLASH, 116);
	giq_option(4, SCRIPT_FLASH, 117, FlashCombat, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FLASH, 118, Flash07Na, NEUTRAL_REACTION);
end

procedure Flash07Na
begin
	variable LVar0 := 0;
	if (dude_is_male) then begin
		LVar0 := -15;
	end
	else begin
		LVar0 := -25;
	end
	if (is_success(roll_vs_skill(dude_obj, 14, LVar0))) then begin
		call Flash09N;
	end
	else begin
		if (dude_is_male) then begin
			call Flash08Nb;
		end
		else begin
			call Flash08Na;
		end
	end
end

procedure Flash08Na
begin
	gsay_reply(SCRIPT_FLASH, 119);
	giq_option(4, SCRIPT_FLASH, 120, FlashCombat, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FLASH, 121, FlashEnd, NEUTRAL_REACTION);
end

procedure Flash08Nb
begin
	gsay_reply(SCRIPT_FLASH, 122);
	giq_option(4, SCRIPT_FLASH, 123, FlashCombat, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FLASH, 124, FlashEnd, NEUTRAL_REACTION);
end

procedure Flash09N
begin
	gsay_message(SCRIPT_FLASH, 125, NEUTRAL_REACTION);
end

procedure Flash10N
begin
	add_timer_event(self_obj, game_ticks(10), 1);
end

procedure Flash11N
begin
	gsay_message(SCRIPT_FLASH, 127, NEUTRAL_REACTION);
end

procedure FlashCombat
begin
	hostile1 := 1;
end

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

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_CURE_JARVIS_7, 1);
	end
end