#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure talk_p_proc;
procedure Initialize_p_proc;
procedure critter_p_proc;
procedure map_enter_p_proc;

variable Only_Once := 1;
variable Hex_Number;


procedure start
begin
	if (Only_Once) then begin
		call Initialize_p_proc;
	end
	else begin
		if (script_action == talk_proc) then begin
			call talk_p_proc;
		end
		else begin
			if (script_action == critter_proc) then begin
				call critter_p_proc;
			end
			else begin
				if (script_action == map_enter_proc) then begin
					call map_enter_p_proc;
				end
			end
		end
	end
end

procedure talk_p_proc
begin
	if (local_var(3) == 0) then begin
		float_msg(self_obj, message_str(SCRIPT_FOLINVAD, random(100, 107)), FLOAT_MSG_WHITE);
	end
end

procedure Initialize_p_proc
begin
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BROTHERHOOD_PALADIN);
	Only_Once := 0;
end

procedure critter_p_proc
begin
	variable LVar0 := 0;
	if (global_var(GVAR_FOLLOWER_MACHO_HELP) == 1) then begin
		if (local_var(4) == 0) then begin
			if (Hex_Number == 0) then begin
				LVar0 := random(1, 10);
				if (LVar0 == 1) then begin
					Hex_Number := 19905;
				end
				else begin
					if (LVar0 == 2) then begin
						Hex_Number := 20308;
					end
					else begin
						if (LVar0 == 3) then begin
							Hex_Number := 20506;
						end
						else begin
							if (LVar0 == 4) then begin
								Hex_Number := 20906;
							end
							else begin
								if (LVar0 == 5) then begin
									Hex_Number := 20110;
								end
								else begin
									if (LVar0 == 6) then begin
										Hex_Number := 19503;
									end
									else begin
										if (LVar0 == 7) then begin
											Hex_Number := 19103;
										end
										else begin
											if (LVar0 == 8) then begin
												Hex_Number := 20904;
											end
											else begin
												if (LVar0 == 9) then begin
													Hex_Number := 21113;
												end
												else begin
													Hex_Number := 19910;
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
			if (tile_num(self_obj) != Hex_Number) then begin
				animate_move_obj_to_tile(self_obj, Hex_Number, ANIMATE_WALK);
			end
			else begin
				if (tile_num(self_obj) == Hex_Number) then begin
					set_local_var(4, 1);
				end
			end
		end
		else begin
			if ((tile_distance_objs(dude_obj, self_obj) > 10) and (tile_distance_objs(dude_obj, self_obj) < 15)) then begin
				animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(dude_obj), random(0, 5), random(3, 6)), ANIMATE_WALK);
			end
			else begin
				if (tile_distance_objs(dude_obj, self_obj) >= 15) then begin
					animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(dude_obj), random(0, 5), random(3, 6)), ANIMATE_RUN);
				end
				else begin
					if (random(1, 100) == 1) then begin
						animate_move_obj_to_tile(self_obj, tile_num_in_direction(tile_num(self_obj), random(0, 5), random(3, 6)), ANIMATE_WALK);
					end
				end
			end
		end
		if (local_var(4) == 1) then begin
			if ((cur_map_index == MAP_CHILDRN1) or (cur_map_index == MAP_CHILDRN2)) then begin
				if (local_var(0) == 0) then begin
					set_local_var(0, 1);
					party_add(self_obj);
					float_msg(self_obj, message_str(SCRIPT_FOLINVAD, 112), FLOAT_MSG_WHITE);
				end
			end
			else begin
				if (cur_map_index == MAP_MSTRLR12) then begin
					if (local_var(1) == 0) then begin
						if (elevation(self_obj) == 1) then begin
							set_local_var(1, 1);
							float_msg(self_obj, message_str(SCRIPT_FOLINVAD, 109), FLOAT_MSG_WHITE);
						end
					end
				end
				else begin
					if (cur_map_index == MAP_MSTRLR34) then begin
						if (local_var(2) == 0) then begin
							set_local_var(2, 1);
							float_msg(self_obj, message_str(SCRIPT_FOLINVAD, 110), FLOAT_MSG_WHITE);
						end
					end
					else begin
						if (local_var(3) == 0) then begin
							set_local_var(3, 1);
							float_msg(self_obj, message_str(SCRIPT_FOLINVAD, 111), FLOAT_MSG_WHITE);
							party_remove(self_obj);
							animate_move_obj_to_tile(self_obj, 26000, ANIMATE_WALK);
							set_global_var(GVAR_FOLLOWER_MACHO_HELP, 5);
						end
					end
				end
			end
		end
	end
	else begin
		set_obj_visibility(self_obj, 1);
	end
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_FOLLOWER_MACHO_HELP) != 1) then begin
		set_obj_visibility(self_obj, 1);
	end
end