#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 map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Theresa01;
procedure Theresa02;
procedure Theresa02a;
procedure Theresa03;
procedure Theresa04;
procedure Theresa05;
procedure Theresa06;
procedure Theresa07;
procedure Theresa08;
procedure Theresa09;
procedure Theresa10;
procedure Theresa11;
procedure Theresa12;
procedure Theresa13;
procedure Theresa14;
procedure Theresa15;
procedure Theresa16;
procedure Theresa17;
procedure Theresa18;
procedure Theresa19;
procedure Theresa20;
procedure Theresa21;
procedure Theresa22;
procedure Theresa23;
procedure Theresa24;
procedure Theresa25;
procedure TheresaEnd;
procedure TheresaCombat;
procedure begin_meeting;
procedure say_lines;
procedure rebel_meeting;
procedure sleeping;

variable night_person;
variable wake_time;
variable sleep_time;
variable home_tile;
variable sleep_tile;
variable not_at_meeting := 1;
variable line;
variable hostile1;
variable exp_flag;

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 (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 == map_enter_proc) then begin
						call map_enter_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 (hostile1) then begin
		hostile1 := 0;
		attack_complex(dude_obj, 0, 1, 0, 0, 30000, 0, 0);
	end
	else begin
		if (((game_time / (10 * 60 * 60 * 24)) > map_var(5)) and (global_var(GVAR_CALM_REBELS) != 2)) then begin
			destroy_object(self_obj);
		end
		if ((game_time_hour >= 1700) and (game_time_hour < 1710)) then begin
			if (global_var(GVAR_CALM_REBELS) != 2) then begin
				if (not_at_meeting) then begin
					call rebel_meeting;
				end
			end
		end
		call sleeping;
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_local_var(8, 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
	set_global_var(GVAR_CALM_REBELS, 2);
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_THERESA, 100));
end

procedure map_enter_p_proc
begin
	if (not(local_var(4))) then begin
		set_local_var(4, tile_num(self_obj));
	end
	home_tile := local_var(4);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 87);
	sleep_tile := home_tile;
	sleep_time := 1915;
	wake_time := 715;
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (map_var(5) == 250) then begin
		set_map_var(5, (game_time / (10 * 60 * 60 * 24)) + 3);
	end
	call Get_Reaction;
	if (local_var(5) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_GENVAULT, 166), FLOAT_MSG_NORMAL);
	end
	else begin
		if (global_var(GVAR_WATER_CHIP_4) or local_var(8)) then begin
			float_msg(self_obj, message_str(SCRIPT_THERESA, 174), FLOAT_MSG_RED);
		end
		else begin
			start_gdialog(SCRIPT_THERESA, self_obj, 4, -1, -1);
			gsay_start;
			if (global_var(GVAR_FIND_WATER_CHIP) == 2) then begin
				call Theresa24;
			end
			else begin
				if (global_var(GVAR_CALM_REBELS) == 2) then begin
					call Theresa23;
				end
				else begin
					if ((global_var(GVAR_CALM_REBELS) != 2) and (game_time_hour > 1700) and (game_time_hour < 1710)) then begin
						call Theresa15;
					end
					else begin
						if (local_var(1) >= 2) then begin
							call Theresa01;
						end
						else begin
							call Theresa14;
						end
					end
				end
			end
			gsay_end;
			end_dialogue;
		end
	end
	if (exp_flag) then begin
		exp_flag := 0;
		give_exp_points(750);
		display_msg(message_str(SCRIPT_THERESA, 173));
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		not_at_meeting := 1;
	end
	else begin
		if (fixed_param == 2) then begin
			call begin_meeting;
		end
		else begin
			if (fixed_param == 3) then begin
				call say_lines;
			end
			else begin
				if (fixed_param == 4) then begin
					animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
					add_timer_event(self_obj, game_ticks(60), 2);
				end
			end
		end
	end
end

procedure Theresa01
begin
	gsay_reply(SCRIPT_THERESA, message_str(SCRIPT_THERESA, 101) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_THERESA, 102));
	giq_option(4, SCRIPT_THERESA, 103, Theresa02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_THERESA, 104, Theresa03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_THERESA, 105, Theresa04, NEUTRAL_REACTION);
end

procedure Theresa02
begin
	gsay_reply(SCRIPT_THERESA, 106);
	giq_option(4, SCRIPT_THERESA, 107, Theresa05, BAD_REACTION);
	giq_option(4, SCRIPT_THERESA, 108, Theresa06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_THERESA, 109, Theresa07, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 110, Theresa02a, NEUTRAL_REACTION);
end

procedure Theresa02a
begin
	variable LVar0 := 0;
	if (not(local_var(7))) then begin
		set_local_var(7, 1);
		LVar0 := roll_vs_skill(dude_obj, 14, 0);
		if (is_success(LVar0)) then begin
			call Theresa08;
		end
		else begin
			call Theresa09;
		end
	end
	else begin
		call Theresa25;
	end
end

procedure Theresa03
begin
	gsay_reply(SCRIPT_THERESA, 111);
	giq_option(4, SCRIPT_THERESA, 112, Theresa07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_THERESA, 113, Theresa06, NEUTRAL_REACTION);
end

procedure Theresa04
begin
	gsay_message(SCRIPT_THERESA, 114, NEUTRAL_REACTION);
end

procedure Theresa05
begin
	call Down_React;
	gsay_reply(SCRIPT_THERESA, 115);
	giq_option(4, SCRIPT_THERESA, 116, Theresa09, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 117, Theresa02a, NEUTRAL_REACTION);
end

procedure Theresa06
begin
	gsay_message(SCRIPT_THERESA, 118, NEUTRAL_REACTION);
end

procedure Theresa07
begin
	gsay_reply(SCRIPT_THERESA, 119);
	giq_option(4, SCRIPT_THERESA, 120, Theresa11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_THERESA, 121, Theresa12, NEUTRAL_REACTION);
end

procedure Theresa08
begin
	call Up_React;
	set_global_var(GVAR_CALM_REBELS, 2);
	line := 10;
	exp_flag := 1;
	gsay_reply(SCRIPT_THERESA, 122);
	giq_option(4, SCRIPT_THERESA, 123, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa09
begin
	call Down_React;
	gsay_message(SCRIPT_THERESA, 124, NEUTRAL_REACTION);
end

procedure Theresa10
begin
	gsay_message(SCRIPT_THERESA, 125, NEUTRAL_REACTION);
end

procedure Theresa11
begin
	call Down_React;
	gsay_reply(SCRIPT_THERESA, 126);
	giq_option(4, SCRIPT_THERESA, 127, TheresaEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 128, Theresa02a, NEUTRAL_REACTION);
end

procedure Theresa12
begin
	call Up_React;
	gsay_reply(SCRIPT_THERESA, 129);
	giq_option(4, SCRIPT_THERESA, 130, Theresa13, NEUTRAL_REACTION);
	if (global_var(GVAR_SHADY_SANDS) != 0) then begin
		giq_option(4, SCRIPT_THERESA, 131, Theresa13, NEUTRAL_REACTION);
	end
end

procedure Theresa13
begin
	gsay_message(SCRIPT_THERESA, 132, NEUTRAL_REACTION);
end

procedure Theresa14
begin
	gsay_message(SCRIPT_THERESA, 133, NEUTRAL_REACTION);
end

procedure Theresa15
begin
	gsay_reply(SCRIPT_THERESA, 134);
	giq_option(4, SCRIPT_THERESA, 135, Theresa16, BAD_REACTION);
	giq_option(4, SCRIPT_THERESA, 136, Theresa17, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 137, Theresa18, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_THERESA, 138, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa16
begin
	call Down_React;
	gsay_reply(SCRIPT_THERESA, 139);
	giq_option(4, SCRIPT_THERESA, 140, Theresa20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_THERESA, 141, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa17
begin
	gsay_reply(SCRIPT_THERESA, 142);
	giq_option(4, SCRIPT_THERESA, 143, Theresa19, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 144, Theresa21, NEUTRAL_REACTION);
end

procedure Theresa18
begin
	gsay_reply(SCRIPT_THERESA, 145);
	giq_option(6, SCRIPT_THERESA, 146, Theresa21, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 147, Theresa19, NEUTRAL_REACTION);
end

procedure Theresa19
begin
	gsay_reply(SCRIPT_THERESA, 148);
	giq_option(4, SCRIPT_THERESA, 149, TheresaCombat, BAD_REACTION);
	giq_option(4, SCRIPT_THERESA, 150, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa20
begin
	gsay_reply(SCRIPT_THERESA, 151);
	giq_option(4, SCRIPT_THERESA, 152, Theresa17, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_THERESA, 153, Theresa21, NEUTRAL_REACTION);
end

procedure Theresa21
begin
	if (local_var(7)) then begin
		call Theresa25;
	end
	else begin
		set_local_var(7, 1);
		if (is_success(roll_vs_skill(dude_obj, 14, 0))) then begin
			exp_flag := 1;
			set_global_var(GVAR_CALM_REBELS, 2);
			line := 10;
			gsay_message(SCRIPT_THERESA, 154, NEUTRAL_REACTION);
		end
		else begin
			gsay_reply(SCRIPT_THERESA, 155);
			giq_option(4, SCRIPT_THERESA, 156, TheresaCombat, BAD_REACTION);
			giq_option(4, SCRIPT_THERESA, 157, TheresaEnd, NEUTRAL_REACTION);
		end
	end
end

procedure Theresa22
begin
	set_global_var(GVAR_CALM_REBELS, 2);
	line := 10;
	exp_flag := 1;
	gsay_reply(SCRIPT_THERESA, 158);
	giq_option(4, SCRIPT_THERESA, 159, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa23
begin
	gsay_reply(SCRIPT_THERESA, 170);
	giq_option(4, SCRIPT_THERESA, 171, TheresaEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_THERESA, 138, TheresaEnd, NEUTRAL_REACTION);
end

procedure Theresa24
begin
	if (global_var(GVAR_CALM_REBELS) != 2) then begin
		set_global_var(GVAR_CALM_REBELS, 2);
		exp_flag := 0;
		line := 10;
	end
	gsay_message(SCRIPT_THERESA, 172, NEUTRAL_REACTION);
end

procedure Theresa25
begin
	gsay_message(SCRIPT_THERESA, 175, NEUTRAL_REACTION);
end

procedure TheresaEnd
begin
end

procedure TheresaCombat
begin
	hostile1 := 1;
end

procedure begin_meeting
begin
	animate_move_obj_to_tile(self_obj, home_tile, ANIMATE_WALK);
	line := 1;
	add_timer_event(self_obj, game_ticks(10), 3);
end

procedure say_lines
begin
	variable LVar0 := 0;
	LVar0 := message_str(SCRIPT_THERESA, 159 + line);
	float_msg(self_obj, LVar0, FLOAT_MSG_NORMAL);
	line := line + 1;
	if (line < 11) then begin
		add_timer_event(self_obj, game_ticks(10), 3);
	end
	else begin
		add_timer_event(self_obj, game_ticks(600), 1);
	end
end

procedure rebel_meeting
begin
	not_at_meeting := 0;
	add_timer_event(self_obj, game_ticks(3), 4);
end

procedure sleeping
begin
	if (local_var(5) == 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(5, 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(5, 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(5, 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(5, 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