#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 look_at_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure use_obj_on_p_proc;
procedure dog_joins_dude;

variable hostile1;
variable waiting_to_follow;

import variable Katja_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 == 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
							else begin
								if (script_action == use_obj_on_proc) then begin
									call use_obj_on_p_proc;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure critter_p_proc
begin
	if (obj_can_see_obj(self_obj, dude_obj)) then begin
		if (obj_pid(critter_inven_obj(dude_obj, 0)) == 74) then begin
			if (not(global_var(GVAR_DOG_EMPATHY))) then begin
				display_msg(message_str(SCRIPT_JUNKDOG, 106));
				call dog_joins_dude;
			end
		end
	end
	if (global_var(GVAR_DOG_EMPATHY) == 1) then begin
		if (tile_distance_objs(self_obj, dude_obj) > 4) then begin
			animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(dude_obj), random(0, 5), 1), ANIMATE_RUN);
		end
		else begin
			animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(dude_obj), random(0, 5), 3), ANIMATE_WALK);
		end
	end
	else begin
		if (map_var(8)) then begin
			if (map_var(5)) then begin
				add_timer_event(self_obj, game_ticks(6), 1);
			end
			set_map_var(8, 0);
		end
	end
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_DOG_EMPATHY, 0);
	set_global_var(GVAR_DOG_1, 2);
	set_global_var(GVAR_DOG_2, 2);
	party_remove(self_obj);
	set_map_var(5, 0);
end

procedure look_at_p_proc
begin
	script_overrides;
	if (global_var(GVAR_DOG_EMPATHY)) then begin
		display_msg(message_str(SCRIPT_JUNKDOG, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_JUNKDOG, 101));
	end
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_DOG_EMPATHY) == 1) 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, 6);
	end
	if ((cur_map_index == MAP_JUNKCSNO) and (global_var(GVAR_DOG_EMPATHY) == 0)) then begin
		set_map_var(5, 1);
	end
end

procedure pickup_p_proc
begin
	script_overrides;
end

procedure talk_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_JUNKDOG, 102), FLOAT_MSG_NORMAL);
	if (Katja_ptr != 0) then begin
		float_msg(Katja_ptr, message_str(SCRIPT_KATJA, 175), FLOAT_MSG_PURPLE);
	end
end

procedure timed_event_p_proc
begin
	float_msg(self_obj, message_str(SCRIPT_JUNKDOG, 103), FLOAT_MSG_NORMAL);
	set_map_var(9, 1);
	set_map_var(7, 1);
end

procedure use_obj_on_p_proc
begin
	if ((obj_pid(obj_being_used_with) == 103) or (obj_pid(obj_being_used_with) == 81)) then begin
		if (not(global_var(GVAR_DOG_EMPATHY))) then begin
			display_msg(message_str(SCRIPT_JUNKDOG, 105));
			call dog_joins_dude;
		end
	end
end

procedure dog_joins_dude
begin
	display_msg(message_str(SCRIPT_JUNKDOG, 104));
	give_exp_points(100);
	set_global_var(GVAR_DOG_EMPATHY, 1);
	set_global_var(GVAR_DOG_2, 2);
	set_global_var(GVAR_DOG_1, 2);
	party_add(self_obj);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
	set_map_var(5, 0);
end