#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
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 timed_event_p_proc;
procedure flee_dude;
procedure Julianna01;
procedure Julianna02;
procedure Julianna03;
procedure Julianna04;
procedure Julianna05;
procedure Julianna06;
procedure Julianna07;
procedure Julianna08;
procedure Julianna09;
procedure Julianna10;
procedure JuliannaEnd;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable initialized1;
variable round_counter;

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
		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
		sleep_time := 1930;
		wake_time := 830;
		sleep_tile := 23151;
		home_tile := 22749;
		initialized1 := 1;
	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
							else begin
								if (script_action == timed_event_proc) then begin
									call timed_event_p_proc;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (local_var(5) and (local_var(6) == 0) and (tile_distance_objs(self_obj, dude_obj) < 8)) then begin
		call flee_dude;
	end
	else begin
		call sleeping;
	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 look_at_p_proc
begin
	script_overrides;
	if (local_var(4)) then begin
		display_msg(message_str(SCRIPT_JULIANNA, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_JULIANNA, 101));
	end
end

procedure pickup_p_proc
begin
	set_local_var(5, 1);
end

procedure talk_p_proc
begin
	if (local_var(5)) then begin
		float_msg(self_obj, message_str(SCRIPT_ENEMY, 102), FLOAT_MSG_NORMAL);
	end
	else begin
		start_gdialog(SCRIPT_JULIANNA, self_obj, 4, -1, -1);
		gsay_start;
		if (global_var(GVAR_ROMEO_JULIET) < 3) then begin
			call Julianna01;
		end
		else begin
			call Julianna10;
		end
		gsay_end;
		end_dialogue;
	end
	if (global_var(GVAR_ROMEO_JULIET) == 2) then begin
		if (local_var(7) == 0) then begin
			set_local_var(7, 1);
			display_msg(message_str(SCRIPT_JULIANNA, 126));
			give_exp_points(250);
		end
	end
end

procedure timed_event_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_JULIANNA, 124), FLOAT_MSG_NORMAL);
	display_msg(message_str(SCRIPT_JULIANNA, 125));
	critter_dmg(self_obj, 200, 0);
end

procedure flee_dude
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	while(LVar1 < 5) do begin
		if (tile_distance(tile_num(dude_obj), tile_num_in_direction(tile_num(self_obj), LVar1, 3)) > LVar2) then begin
			LVar0 := tile_num_in_direction(tile_num(self_obj), LVar1, 3);
			LVar2 := tile_distance(tile_num(dude_obj), LVar0);
		end
		LVar1 := LVar1 + 1;
	end
	animate_move_obj_to_tile(self_obj, LVar0, ANIMATE_WALK);
end

procedure Julianna01
begin
	gsay_reply(SCRIPT_JULIANNA, 102);
	giq_option(-3, SCRIPT_JULIANNA, 103, JuliannaEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JULIANNA, 104, JuliannaEnd, NEUTRAL_REACTION);
	if (global_var(GVAR_ROMEO_JULIET) == 1) then begin
		giq_option(0, SCRIPT_JULIANNA, 105, Julianna06, NEUTRAL_REACTION);
	end
	if (not(local_var(4))) then begin
		giq_option(4, SCRIPT_JULIANNA, 106, Julianna02, NEUTRAL_REACTION);
	end
end

procedure Julianna02
begin
	set_local_var(4, 1);
	gsay_reply(SCRIPT_JULIANNA, 107);
	giq_option(4, SCRIPT_JULIANNA, 108, Julianna03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_JULIANNA, 109, Julianna04, NEUTRAL_REACTION);
end

procedure Julianna03
begin
	gsay_reply(SCRIPT_JULIANNA, 110);
	giq_option(4, SCRIPT_JULIANNA, 111, Julianna05, NEUTRAL_REACTION);
	call Goodbyes;
	giq_option(4, SCRIPT_MODREACT, exit_line, JuliannaEnd, NEUTRAL_REACTION);
end

procedure Julianna04
begin
	gsay_reply(SCRIPT_JULIANNA, 112);
	call Goodbyes;
	giq_option(4, SCRIPT_MODREACT, exit_line, JuliannaEnd, NEUTRAL_REACTION);
end

procedure Julianna05
begin
	gsay_reply(SCRIPT_JULIANNA, 113);
	call Goodbyes;
	giq_option(4, SCRIPT_MODREACT, exit_line, JuliannaEnd, NEUTRAL_REACTION);
end

procedure Julianna06
begin
	gsay_reply(SCRIPT_JULIANNA, 114);
	giq_option(0, SCRIPT_JULIANNA, 115, Julianna07, NEUTRAL_REACTION);
	giq_option(0, SCRIPT_JULIANNA, 116, Julianna08, NEUTRAL_REACTION);
	if (obj_is_carrying_obj_pid(dude_obj, 99)) then begin
		giq_option(0, SCRIPT_JULIANNA, 117, Julianna09, NEUTRAL_REACTION);
	end
end

procedure Julianna07
begin
	gsay_message(SCRIPT_JULIANNA, 118, NEUTRAL_REACTION);
	add_timer_event(self_obj, 1, 1);
end

procedure Julianna08
begin
	gsay_reply(SCRIPT_JULIANNA, 119);
	giq_option(4, SCRIPT_JULIANNA, 120, JuliannaEnd, NEUTRAL_REACTION);
	if (obj_is_carrying_obj_pid(dude_obj, 99)) then begin
		giq_option(4, SCRIPT_JULIANNA, 121, Julianna09, NEUTRAL_REACTION);
	end
end

procedure Julianna09
begin
	variable LVar0 := 0;
	LVar0 := obj_carrying_pid_obj(dude_obj, 99);
	rm_obj_from_inven(dude_obj, obj_carrying_pid_obj(dude_obj, 99));
	add_obj_to_inven(self_obj, LVar0);
	gsay_message(SCRIPT_JULIANNA, 122, NEUTRAL_REACTION);
	set_global_var(GVAR_ROMEO_JULIET, 2);
end

procedure Julianna10
begin
	gsay_message(SCRIPT_JULIANNA, 123, NEUTRAL_REACTION);
end

procedure JuliannaEnd
begin
end

procedure sleeping
begin
	if (local_var(6) == 1) then begin
		if (not(night_person) and (game_time_hour >= wake_time) and (game_time_hour < sleep_time) or (night_person and ((game_time_hour >= wake_time) or (game_time_hour < sleep_time)))) then begin
			if (((game_time_hour - wake_time) < 10) and ((game_time_hour - wake_time) > 0)) then begin
				if (tile_num(self_obj) != home_tile) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
				end
				else begin
					set_local_var(6, 0);
				end
			end
			else begin
				move_to(self_obj, home_tile, elevation(self_obj));
				if (tile_num(self_obj) == home_tile) then begin
					set_local_var(6, 0);
				end
			end
		end
	end
	else begin
		if (night_person and (game_time_hour >= sleep_time) and (game_time_hour < wake_time) or (not(night_person) and ((game_time_hour >= sleep_time) or (game_time_hour < wake_time)))) then begin
			if (((game_time_hour - sleep_time) < 10) and ((game_time_hour - sleep_time) > 0)) then begin
				if (tile_num(self_obj) != sleep_tile) then begin
					animate_move_obj_to_tile(self_obj, self_obj, ANIMATE_WALK);
				end
				else begin
					set_local_var(6, 1);
				end
			end
			else begin
				if (tile_num(self_obj) != sleep_tile) then begin
					move_to(self_obj, sleep_tile, elevation(self_obj));
				end
				else begin
					set_local_var(6, 1);
				end
			end
		end
	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