#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_update_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure Fire00;
procedure Fire01;
procedure Fire02;
procedure Fire03;
procedure Fire04;
procedure Fire05;
procedure Fire06;
procedure Fire07;
procedure Fire08;
procedure Fire09;
procedure Fire10;
procedure Fire11;
procedure Fire12;
procedure Fire13;
procedure Fire14;
procedure Fire15;
procedure Fire16;
procedure Fire17;
procedure Fire18;
procedure Fire19;
procedure Fire20;
procedure Fire21;
procedure FireCombat;
procedure FireEnd;
procedure follow_player;
procedure show_true_name;
procedure show_false_name;

variable hostile1;
variable initialized1;
variable prev_tile := 7000;
variable dest_tile1 := 7000;
variable name;

procedure timed_event_p_proc;


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_update_proc) then begin
					call map_update_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
					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
end

procedure destroy_p_proc
begin
	set_global_var(GVAR_BLADES_LEFT, global_var(GVAR_BLADES_LEFT) - 1);
	set_global_var(GVAR_BLADES_HELP, 0);
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_GOOD_MONSTER, global_var(GVAR_GOOD_MONSTER) + 1);
		if ((global_var(GVAR_GOOD_MONSTER) % 7) == 0) then begin
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 1);
		end
	end
end

procedure look_at_p_proc
begin
	if ((global_var(GVAR_BECOME_BLADE) == 2) or (get_critter_stat(dude_obj, 6) > 6)) then begin
		call show_true_name;
	end
	else begin
		if (get_critter_stat(dude_obj, 6) < 4) then begin
			call show_false_name;
		end
		else begin
			if (get_critter_stat(dude_obj, 4) < 5) then begin
				call show_false_name;
			end
			else begin
				if (random(0, 1)) then begin
					call show_false_name;
				end
				else begin
					call show_true_name;
				end
			end
		end
	end
end

procedure map_update_p_proc
begin
	if (not(initialized1)) then begin
		critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_BLADES);
		if (cur_map_index == MAP_LABLADES) then begin
			critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 47);
		end
		else begin
			if ((cur_map_index == MAP_CHILDRN1) or (cur_map_index == MAP_CHILDRN2)) then begin
				if (global_var(GVAR_BLADES_HELP) == 1) then begin
					set_obj_visibility(self_obj, 0);
					critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 0);
					add_timer_event(self_obj, game_ticks(1), 1);
				end
				else begin
					set_obj_visibility(self_obj, 1);
				end
			end
		end
		initialized1 := 1;
	end
	if ((elevation(self_obj) != elevation(dude_obj)) and (global_var(GVAR_BLADES_HELP) == 1)) then begin
		move_to(self_obj, tile_num_in_direction(tile_num(dude_obj), random(4, 5), 1), elevation(dude_obj));
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	start_gdialog(SCRIPT_FIRE, self_obj, 4, -1, -1);
	gsay_start;
	set_local_var(4, 1);
	if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
		call Fire18;
	end
	else begin
		if (global_var(GVAR_BECOME_BLADE) == 2) then begin
			call Fire21;
		end
		else begin
			if (global_var(GVAR_BECOME_BLADE) == 1) then begin
				if (global_var(GVAR_BLADES_HELP) == 1) then begin
					call Fire19;
				end
				else begin
					call Fire20;
				end
			end
			else begin
				call Fire00;
			end
		end
	end
	gsay_end;
	end_dialogue;
end

procedure Fire00
begin
	gsay_reply(SCRIPT_FIRE, 111);
	giq_option(-3, SCRIPT_FIRE, 112, Fire01, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FIRE, 113, Fire07, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FIRE, 114, Fire11, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FIRE, 115, FireCombat, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FIRE, 116, FireEnd, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FIRE, 117, Fire17, NEUTRAL_REACTION);
end

procedure Fire01
begin
	gsay_reply(SCRIPT_FIRE, 118);
	giq_option(-3, SCRIPT_FIRE, 119, Fire02, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FIRE, 120, Fire05, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FIRE, 121, Fire06, NEUTRAL_REACTION);
end

procedure Fire02
begin
	gsay_reply(SCRIPT_FIRE, 122);
	giq_option(-3, SCRIPT_FIRE, 123, Fire03, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_FIRE, 124, Fire04, NEUTRAL_REACTION);
end

procedure Fire03
begin
	gsay_message(SCRIPT_FIRE, 125, NEUTRAL_REACTION);
end

procedure Fire04
begin
	gsay_message(SCRIPT_FIRE, 126, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire05
begin
	gsay_message(SCRIPT_FIRE, 127, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire06
begin
	gsay_message(SCRIPT_FIRE, 128, NEUTRAL_REACTION);
end

procedure Fire07
begin
	gsay_reply(SCRIPT_FIRE, 129);
	giq_option(4, SCRIPT_FIRE, 130, FireEnd, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_FIRE, 131, Fire08, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FIRE, 132, Fire09, NEUTRAL_REACTION);
	giq_option(7, SCRIPT_FIRE, 133, Fire10, NEUTRAL_REACTION);
end

procedure Fire08
begin
	gsay_message(SCRIPT_FIRE, 134, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire09
begin
	gsay_reply(SCRIPT_FIRE, 135);
	giq_option(5, SCRIPT_FIRE, 136, FireEnd, NEUTRAL_REACTION);
	giq_option(5, SCRIPT_FIRE, 137, Fire08, NEUTRAL_REACTION);
end

procedure Fire10
begin
	gsay_message(SCRIPT_FIRE, 138, NEUTRAL_REACTION);
end

procedure Fire11
begin
	gsay_reply(SCRIPT_FIRE, 139);
	giq_option(6, SCRIPT_FIRE, 140, Fire12, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FIRE, 141, Fire15, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FIRE, 142, Fire16, NEUTRAL_REACTION);
end

procedure Fire12
begin
	gsay_reply(SCRIPT_FIRE, 143);
	giq_option(6, SCRIPT_FIRE, 144, Fire13, NEUTRAL_REACTION);
	giq_option(6, SCRIPT_FIRE, 145, Fire14, NEUTRAL_REACTION);
end

procedure Fire13
begin
	gsay_message(SCRIPT_FIRE, 146, NEUTRAL_REACTION);
end

procedure Fire14
begin
	gsay_message(SCRIPT_FIRE, 147, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire15
begin
	gsay_message(SCRIPT_FIRE, 148, NEUTRAL_REACTION);
end

procedure Fire16
begin
	gsay_message(SCRIPT_FIRE, 149, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire17
begin
	gsay_message(SCRIPT_FIRE, 150, NEUTRAL_REACTION);
	call FireCombat;
end

procedure Fire18
begin
	gsay_message(SCRIPT_FIRE, 151, NEUTRAL_REACTION);
	add_timer_event(self_obj, game_ticks(10), 2);
end

procedure Fire19
begin
	gsay_message(SCRIPT_FIRE, 152, NEUTRAL_REACTION);
end

procedure Fire20
begin
	gsay_message(SCRIPT_FIRE, 153, NEUTRAL_REACTION);
end

procedure Fire21
begin
	gsay_message(SCRIPT_FIRE, 154, NEUTRAL_REACTION);
end

procedure FireCombat
begin
	hostile1 := 1;
end

procedure FireEnd
begin
end

procedure follow_player
begin
	variable LVar0 := 0;
	prev_tile := dest_tile1;
	LVar0 := (has_trait(1, dude_obj, 10) + random(4, 5)) % 6;
	dest_tile1 := tile_num_in_direction(tile_num(dude_obj), LVar0, random(2, 3));
	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
	if (tile_distance_objs(self_obj, dude_obj) > 3) then begin
		add_timer_event(self_obj, game_ticks(1), 1);
	end
	else begin
		add_timer_event(self_obj, game_ticks(random(2, 5)), 1);
	end
end

procedure show_true_name
begin
	script_overrides;
	if (local_var(4)) then begin
		display_msg(message_str(SCRIPT_FIRE, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_FIRE, 103));
	end
end

procedure show_false_name
begin
	script_overrides;
	display_msg(message_str(SCRIPT_FIRE, random(104, 110)));
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		call follow_player;
	end
	else begin
		if (obj_can_see_obj(self_obj, dude_obj)) then begin
			hostile1 := 1;
		end
	end
end