#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure critter_p_proc;
procedure destroy_p_proc;
procedure map_enter_p_proc;
procedure use_obj_on_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure Katja01;
procedure Katja02;
procedure Katja03;
procedure Katja04;
procedure Katja05;
procedure Katja06;
procedure Katja07;
procedure Katja08;
procedure Katja09;
procedure Katja10;
procedure Katja11;
procedure Katja12;
procedure Katja13;
procedure Katja14;
procedure Katja15;
procedure Katja16;
procedure Katja17;
procedure Katja18;
procedure Katja19;
procedure Katja20;
procedure Katja21;
procedure Katja22;
procedure Katja23;
procedure Katja24;
procedure Katja25;
procedure Katja26;
procedure Katja27;
procedure Katja28;
procedure KatjaEnd;
procedure KatjaLeave;
procedure KatjaTactics;
procedure KatjaClose;
procedure KatjaModerate;
procedure KatjaFar;
procedure KatjaExit;
procedure follow_player;
procedure map_commentary;
procedure pick_lock;
procedure set_lock;

variable hostile1;
variable item;
variable unwield_flag;
variable lock;
variable prev_tile := 7000;
variable dest_tile1 := 7000;
variable line173flag;
variable line174flag;
variable line176flag;
variable line184flag;
variable line186flag;
variable line187flag;
variable line188flag;

import variable Ian_ptr;
import variable Katja_ptr;
import variable Tycho_ptr;


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 == map_enter_proc) then begin
				call map_enter_p_proc;
			end
			else begin
				if (script_action == use_obj_on_proc) then begin
					call use_obj_on_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

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
		call map_commentary;
	end
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_KATJA_STATUS, 3);
	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

procedure map_enter_p_proc
begin
	if (global_var(GVAR_KATJA_STATUS) < 2) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 49);
	end
	else begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
		add_timer_event(self_obj, game_ticks(2), 1);
	end
	Katja_ptr := self_obj;
end

procedure use_obj_on_p_proc
begin
	if (global_var(GVAR_KATJA_STATUS) == 2) then begin
		item := obj_pid(obj_being_used_with);
		if ((item != 40) and (item != 47) and (item != 91)) then begin
			script_overrides;
			if (obj_item_subtype(obj_being_used_with) == 3) then begin
				if ((item == 4) or (item == 9) or (item == 22) or (item == 45) or (item == 7) or (item == 159) or (item == 25) or (item == 26) or (item == 27) or (item == 19) or (item == 21) or (item == 234) or (item == 235) or (item == 116) or (item == 236) or (item == 241)) then begin
					rm_obj_from_inven(dude_obj, obj_being_used_with);
					add_obj_to_inven(self_obj, obj_being_used_with);
				end
				else begin
					float_msg(self_obj, message_str(SCRIPT_MODREACT, 109), FLOAT_MSG_PURPLE);
				end
			end
			else begin
				rm_obj_from_inven(dude_obj, obj_being_used_with);
				add_obj_to_inven(self_obj, obj_being_used_with);
			end
		end
	end
end

procedure talk_p_proc
begin
	start_gdialog(SCRIPT_KATJA, self_obj, 4, -1, -1);
	gsay_start;
	if (global_var(GVAR_KATJA_STATUS) == 3) then begin
		call Katja28;
	end
	else begin
		if (global_var(GVAR_KATJA_STATUS) == 2) then begin
			call Katja24;
		end
		else begin
			if (global_var(GVAR_KATJA_STATUS) == 1) then begin
				call Katja25;
			end
			else begin
				call Katja01;
			end
		end
	end
	gsay_end;
	end_dialogue;
	if (unwield_flag == 1) then begin
		unwield_flag := 0;
		inven_unwield;
	end
	if (lock) then begin
		call pick_lock;
	end
	if (global_var(GVAR_KATJA_STATUS) == 2) then begin
		if (global_var(GVAR_DESTROY_MASTER_11) == 0) then begin
			set_global_var(GVAR_DESTROY_MASTER_11, 1);
			give_exp_points(200);
			display_msg(message_str(SCRIPT_KATJA, 180));
		end
	end
end

procedure timed_event_p_proc
begin
	if (global_var(GVAR_KATJA_STATUS) == 2) then begin
		call follow_player;
	end
end

procedure Katja01
begin
	set_global_var(GVAR_KATJA_STATUS, 1);
	gsay_reply(SCRIPT_KATJA, 102);
	giq_option(4, SCRIPT_KATJA, message_str(SCRIPT_KATJA, 103) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_KATJA, 104), Katja02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 105, Katja02, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_KATJA, 106, Katja05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KATJA, 189, KatjaExit, NEUTRAL_REACTION);
end

procedure Katja02
begin
	gsay_reply(SCRIPT_KATJA, 107);
	giq_option(5, SCRIPT_KATJA, 108, Katja03, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_KATJA, 109, Katja04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 110, Katja07, NEUTRAL_REACTION);
	if ((global_var(GVAR_FIND_WATER_CHIP) != 2) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_KATJA, 111, Katja06, NEUTRAL_REACTION);
	end
end

procedure Katja03
begin
	gsay_reply(SCRIPT_KATJA, 112);
	giq_option(5, SCRIPT_KATJA, 109, Katja04, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 110, Katja07, NEUTRAL_REACTION);
	if ((global_var(GVAR_FIND_WATER_CHIP) != 2) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_KATJA, 111, Katja06, NEUTRAL_REACTION);
	end
end

procedure Katja04
begin
	gsay_reply(SCRIPT_KATJA, 113);
	giq_option(5, SCRIPT_KATJA, 114, Katja21, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 116, Katja09, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 115, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja05
begin
	gsay_reply(SCRIPT_KATJA, 117);
	giq_option(4, SCRIPT_KATJA, message_str(SCRIPT_KATJA, 103) + proto_data(obj_pid(dude_obj), 1) + message_str(SCRIPT_KATJA, 104), Katja02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 118, Katja07, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 115, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja06
begin
	gsay_reply(SCRIPT_KATJA, 119);
	giq_option(5, SCRIPT_KATJA, 121, Katja11, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 122, Katja10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 120, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja07
begin
	gsay_reply(SCRIPT_KATJA, 123);
	giq_option(4, SCRIPT_KATJA, 124, Katja06, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 125, Katja08, NEUTRAL_REACTION);
end

procedure Katja08
begin
	gsay_message(SCRIPT_KATJA, 126, BAD_REACTION);
end

procedure Katja09
begin
	gsay_reply(SCRIPT_KATJA, 127);
	if ((global_var(GVAR_FIND_WATER_CHIP) != 2) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_KATJA, 128, Katja06, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_KATJA, 129, Katja10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 130, Katja17, NEUTRAL_REACTION);
end

procedure Katja10
begin
	giq_option(4, SCRIPT_KATJA, 131, Katja13, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 133, Katja15, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 134, Katja16, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 135, Katja12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 130, Katja17, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 136, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja11
begin
	gsay_reply(SCRIPT_KATJA, 137);
	giq_option(4, SCRIPT_KATJA, 135, Katja12, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 138, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja12
begin
	gsay_reply(SCRIPT_KATJA, 139);
	call Katja10;
end

procedure Katja13
begin
	gsay_reply(SCRIPT_KATJA, 140);
	call Katja10;
end

procedure Katja14
begin
	gsay_reply(SCRIPT_KATJA, 141);
	call Katja10;
end

procedure Katja15
begin
	gsay_reply(SCRIPT_KATJA, 142);
	call Katja10;
end

procedure Katja16
begin
	gsay_reply(SCRIPT_KATJA, 143);
	call Katja10;
end

procedure Katja17
begin
	gsay_reply(SCRIPT_KATJA, 144);
	if (not(global_var(GVAR_CHILDREN_OF_CATHEDRAL))) then begin
		set_global_var(GVAR_CHILDREN_OF_CATHEDRAL, 1);
	end
	giq_option(4, SCRIPT_KATJA, 145, Katja18, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 147, Katja10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 146, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja18
begin
	gsay_reply(SCRIPT_KATJA, 148);
	giq_option(4, SCRIPT_KATJA, 149, Katja20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 151, Katja10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 150, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja19
begin
	gsay_reply(SCRIPT_KATJA, 152);
	call Katja10;
end

procedure Katja20
begin
	set_global_var(GVAR_KATJA_STATUS, 2);
	party_add(self_obj);
	add_timer_event(self_obj, game_ticks(1), 1);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
	gsay_message(SCRIPT_KATJA, 153, NEUTRAL_REACTION);
end

procedure Katja21
begin
	gsay_reply(SCRIPT_KATJA, 154);
	giq_option(4, SCRIPT_KATJA, 156, Katja22, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_KATJA, 157, Katja23, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 155, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja22
begin
	gsay_reply(SCRIPT_KATJA, 158);
	call Katja10;
end

procedure Katja23
begin
	gsay_reply(SCRIPT_KATJA, 159);
	giq_option(4, SCRIPT_KATJA, 160, Katja09, NEUTRAL_REACTION);
	if ((global_var(GVAR_FIND_WATER_CHIP) == 0) and (obj_is_carrying_obj_pid(dude_obj, 55) == 0)) then begin
		giq_option(4, SCRIPT_KATJA, 161, Katja06, NEUTRAL_REACTION);
	end
	giq_option(4, SCRIPT_KATJA, 162, KatjaEnd, NEUTRAL_REACTION);
end

procedure Katja24
begin
	gsay_reply(SCRIPT_KATJA, 163);
	if (cur_map_index == MAP_MBENT) then begin
		if (map_var(3)) then begin
			giq_option(4, SCRIPT_KATJA, 164, set_lock, NEUTRAL_REACTION);
		end
	end
	if ((cur_map_index == MAP_JUNKENT) and (elevation(dude_obj) == 1)) then begin
		if (map_var(4)) then begin
			giq_option(4, SCRIPT_KATJA, 164, set_lock, NEUTRAL_REACTION);
		end
	end
	giq_option(4, SCRIPT_TYCHO, 159, KatjaTactics, GOOD_REACTION);
	giq_option(4, SCRIPT_KATJA, 177, Katja26, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 174, Katja27, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 167, KatjaLeave, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 168, KatjaEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KATJA, 168, KatjaEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KATJA, 190, KatjaLeave, NEUTRAL_REACTION);
end

procedure Katja25
begin
	gsay_reply(SCRIPT_KATJA, 169);
	giq_option(4, SCRIPT_KATJA, 171, Katja10, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 170, KatjaEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KATJA, 189, KatjaExit, NEUTRAL_REACTION);
end

procedure Katja26
begin
	gsay_message(SCRIPT_KATJA, 178, NEUTRAL_REACTION);
end

procedure Katja27
begin
	unwield_flag := 1;
	gsay_message(SCRIPT_KATJA, 179, NEUTRAL_REACTION);
	call Katja24;
end

procedure Katja28
begin
	gsay_reply(SCRIPT_KATJA, 181);
	giq_option(4, SCRIPT_KATJA, 182, Katja20, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_KATJA, 183, KatjaEnd, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_KATJA, 189, KatjaExit, NEUTRAL_REACTION);
end

procedure KatjaEnd
begin
end

procedure KatjaLeave
begin
	set_global_var(GVAR_KATJA_STATUS, 3);
	party_remove(self_obj);
	rm_timer_event(self_obj);
end

procedure KatjaTactics
begin
	gsay_reply(SCRIPT_TYCHO, 160);
	giq_option(4, SCRIPT_TYCHO, 161, KatjaClose, GOOD_REACTION);
	giq_option(4, SCRIPT_TYCHO, 162, KatjaModerate, GOOD_REACTION);
	giq_option(4, SCRIPT_TYCHO, 163, KatjaFar, GOOD_REACTION);
end

procedure KatjaClose
begin
	set_global_var(GVAR_DESTROY_VATS_7, 0);
end

procedure KatjaModerate
begin
	set_global_var(GVAR_DESTROY_VATS_7, 1);
end

procedure KatjaFar
begin
	set_global_var(GVAR_DESTROY_VATS_7, 2);
end

procedure KatjaExit
begin
	gsay_message(SCRIPT_KATJA, 191, NEUTRAL_REACTION);
end

procedure follow_player
begin
	variable LVar0 := 0;
	prev_tile := dest_tile1;
	LVar0 := (has_trait(1, dude_obj, 10) + random(2, 4)) % 6;
	dest_tile1 := tile_num_in_direction(tile_num(dude_obj), LVar0, (global_var(GVAR_DESTROY_VATS_7) * 2) + random(1, 2));
	if ((tile_distance_objs(self_obj, dude_obj) > ((global_var(GVAR_DESTROY_VATS_7) + 1) * 2)) or (random(0, 3) == 3)) then begin
		if (tile_distance(prev_tile, tile_num(dude_obj)) > tile_distance(dest_tile1, tile_num(dude_obj))) then begin
			if (tile_distance_objs(self_obj, dude_obj) > 8) then begin
				animate_move_obj_to_tile(self_obj, dest_tile1, ANIMATE_RUN bwor 16);
			end
			else begin
				animate_move_obj_to_tile(self_obj, dest_tile1, ANIMATE_WALK bwor 16);
			end
		end
		else begin
			if (tile_distance_objs(self_obj, dude_obj) > 8) then begin
				animate_move_obj_to_tile(self_obj, dest_tile1, ANIMATE_RUN);
			end
			else begin
				animate_move_obj_to_tile(self_obj, dest_tile1, ANIMATE_WALK);
			end
		end
	end
	add_timer_event(self_obj, game_ticks(1), 1);
end

procedure map_commentary
begin
	if (cur_map_index == MAP_MSTRLR12) then begin
		if ((tile_distance(tile_num(self_obj), 27317) < 4) and (line173flag == 0)) then begin
			float_msg(self_obj, message_str(SCRIPT_KATJA, 173), FLOAT_MSG_PURPLE);
			line173flag := 1;
		end
	end
	else begin
		if (cur_map_index == MAP_VAULT13) then begin
			if (elevation(self_obj) == 1) then begin
				if (tile_distance(tile_num(self_obj), 19111) < 4) then begin
					if ((line174flag == 0) and (global_var(GVAR_CALM_REBELS) != 2) and (Ian_ptr != 0)) then begin
						float_msg(self_obj, message_str(SCRIPT_KATJA, 172), FLOAT_MSG_PURPLE);
						line174flag := 1;
					end
				end
			end
		end
		else begin
			if (cur_map_index == MAP_HUBDWNTN) then begin
				if ((tile_distance(tile_num(self_obj), 28104) < 5) and (line176flag == 0)) then begin
					float_msg(self_obj, message_str(SCRIPT_KATJA, 176), FLOAT_MSG_PURPLE);
					if (global_var(GVAR_HERNANDEZ_STATUS) == 2) then begin
						float_msg(Tycho_ptr, message_str(SCRIPT_TYCHO, 167), FLOAT_MSG_LIGHT_GREY);
					end
					line176flag := 1;
				end
			end
			else begin
				if (cur_map_index == MAP_MBVATS12) then begin
					if (line184flag == 0) then begin
						float_msg(self_obj, message_str(SCRIPT_KATJA, 184), FLOAT_MSG_PURPLE);
						if (global_var(GVAR_IAN_STATUS) == 2) then begin
							float_msg(Ian_ptr, message_str(SCRIPT_IAN, 175), FLOAT_MSG_NORMAL);
						end
						line184flag := 1;
					end
				end
				else begin
					if (cur_map_index == MAP_MSTRLR34) then begin
						if (line186flag == 0) then begin
							float_msg(self_obj, message_str(SCRIPT_KATJA, 186), FLOAT_MSG_PURPLE);
							line186flag := 1;
						end
					end
					else begin
						if (cur_map_index == MAP_HALLDED) then begin
							if ((tile_distance(tile_num(self_obj), 17274) < 4) and (line187flag == 0)) then begin
								float_msg(self_obj, message_str(SCRIPT_KATJA, 187), FLOAT_MSG_PURPLE);
								line187flag := 1;
							end
						end
						else begin
							if (cur_map_index == MAP_LARIPPER) then begin
								if (line188flag == 0) then begin
									float_msg(self_obj, message_str(SCRIPT_KATJA, 188), FLOAT_MSG_PURPLE);
									line188flag := 1;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure pick_lock
begin
	reg_anim_func(2, self_obj);
	reg_anim_func(1, 1);
	reg_anim_obj_move_to_obj(self_obj, lock, -1);
	reg_anim_animate(self_obj, 11, -1);
	reg_anim_func(3, 0);
	use_obj(lock);
	obj_unlock(lock);
	lock := 0;
end

procedure set_lock
begin
	if (cur_map_index == MAP_MBENT) then begin
		lock := map_var(3);
	end
	else begin
		if (cur_map_index == MAP_JUNKENT) then begin
			lock := map_var(4);
		end
	end
end