#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure combat_p_proc;
procedure critter_p_proc;
procedure look_at_p_proc;
procedure pickup_p_proc;
procedure talk_p_proc;
procedure timed_event_p_proc;
procedure damage_p_proc;
procedure destroy_p_proc;
procedure Andrew01;
procedure Andrew02;
procedure Andrew03;
procedure Andrew04;
procedure Andrew05;
procedure Andrew06;

variable hostile1;
variable round_counter;
variable moving_flag := 1;

import variable jail_door_ptr;

procedure map_enter_p_proc;


procedure start
begin
	if (script_action == combat_proc) then begin
		call combat_p_proc;
	end
	else begin
		if (script_action == critter_proc) then begin
			call critter_p_proc;
		end
		else begin
			if (script_action == look_at_proc) then begin
				call look_at_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

procedure combat_p_proc
begin
	if (fixed_param == 4) then begin
		round_counter := round_counter + 1;
	end
	if (round_counter > 3) then begin
		if (global_var(GVAR_ENEMY_JUNKTOWN) == 0) then begin
			set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
		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 ((map_var(1) == 1) and ((game_time / (10 * 60 * 60 * 24)) > map_var(5))) then begin
			if (jail_door_ptr != 0) then begin
				obj_unlock(jail_door_ptr);
				use_obj(jail_door_ptr);
				moving_flag := 0;
				rm_timer_event(self_obj);
				add_timer_event(self_obj, game_ticks(5), 1);
			end
			set_map_var(1, 0);
			float_msg(self_obj, message_str(SCRIPT_ANDREW, 101), FLOAT_MSG_GREEN);
		end
		else begin
			if (map_var(3) == 1) then begin
				set_map_var(3, map_var(3) + 1);
				float_msg(self_obj, message_str(SCRIPT_ANDREW, 116), FLOAT_MSG_RED);
			end
			else begin
				if (map_var(3) == 3) then begin
					if (jail_door_ptr != 0) then begin
						obj_unlock(jail_door_ptr);
					end
					hostile1 := 1;
				end
				else begin
					if (moving_flag == 1) then begin
						if (tile_num(self_obj) != local_var(1)) then begin
							animate_move_obj_to_tile(self_obj, local_var(1), ANIMATE_WALK);
						end
					end
					if ((global_var(GVAR_ENEMY_JUNKTOWN) == 1) and obj_can_see_obj(self_obj, dude_obj)) then begin
						dialogue_system_enter;
					end
				end
			end
		end
	end
end

procedure look_at_p_proc
begin
	if (local_var(0) == 1) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_ANDREW, 100));
	end
end

procedure pickup_p_proc
begin
	hostile1 := 1;
end

procedure talk_p_proc
begin
	if (global_var(GVAR_ENEMY_JUNKTOWN) == 1) then begin
		float_msg(self_obj, message_str(SCRIPT_ANDREW, 102), FLOAT_MSG_RED);
		hostile1 := 1;
	end
	else begin
		start_gdialog(SCRIPT_ANDREW, self_obj, 4, -1, -1);
		gsay_start;
		call Andrew01;
		gsay_end;
		end_dialogue;
	end
end

procedure timed_event_p_proc
begin
	if (fixed_param == 1) then begin
		obj_close(jail_door_ptr);
		rm_timer_event(self_obj);
		add_timer_event(self_obj, game_ticks(10), 2);
	end
	else begin
		if (fixed_param == 2) then begin
			moving_flag := 1;
		end
	end
end

procedure damage_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
	end
end

procedure destroy_p_proc
begin
	if (source_obj == dude_obj) then begin
		set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
		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
end

procedure Andrew01
begin
	gsay_reply(SCRIPT_ANDREW, 103);
	giq_option(4, SCRIPT_ANDREW, 104, Andrew02, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ANDREW, 105, Andrew03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ANDREW, 106, Andrew04, NEUTRAL_REACTION);
	giq_option(-3, SCRIPT_ANDREW, 113, Andrew06, NEUTRAL_REACTION);
end

procedure Andrew02
begin
	set_local_var(0, 1);
	gsay_reply(SCRIPT_ANDREW, 107);
	giq_option(4, SCRIPT_ANDREW, 105, Andrew03, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ANDREW, 106, Andrew04, NEUTRAL_REACTION);
end

procedure Andrew03
begin
	gsay_reply(SCRIPT_ANDREW, 108);
	giq_option(6, SCRIPT_ANDREW, 109, Andrew05, NEUTRAL_REACTION);
	giq_option(4, SCRIPT_ANDREW, 110, Andrew04, NEUTRAL_REACTION);
end

procedure Andrew04
begin
	gsay_message(SCRIPT_ANDREW, 111, NEUTRAL_REACTION);
end

procedure Andrew05
begin
	gsay_reply(SCRIPT_ANDREW, 112);
	giq_option(4, SCRIPT_ANDREW, 110, Andrew04, NEUTRAL_REACTION);
end

procedure Andrew06
begin
	if (dude_is_male) then begin
		gsay_message(SCRIPT_ANDREW, 114, NEUTRAL_REACTION);
	end
	else begin
		gsay_message(SCRIPT_ANDREW, 115, NEUTRAL_REACTION);
	end
end

procedure map_enter_p_proc
begin
	if (global_var(GVAR_JUNKTOWN_INVADED) == 1) then begin
		kill_critter(self_obj, 49);
	end
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_TEAM_NUM, 12);
	critter_add_trait(self_obj, TRAIT_OBJECT, OBJECT_AI_PACKET, AI_GUARD_HUMOROUS);
	if (local_var(1) == 0) then begin
		set_local_var(1, tile_num(self_obj));
	end
end