#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure damage_p_proc;
procedure map_enter_p_proc;
procedure map_update_p_proc;
procedure use_p_proc;
procedure use_obj_on_p_proc;
procedure use_skill_on_p_proc;

variable test;

import variable Killian_ptr;


procedure start
begin
	if (script_action == damage_proc) then begin
		call damage_p_proc;
	end
	else begin
		if (script_action == map_enter_proc) then begin
			call map_enter_p_proc;
		end
		else begin
			if (script_action == map_update_proc) then begin
				call map_update_p_proc;
			end
			else begin
				if (script_action == use_proc) then begin
					call use_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 == use_skill_on_proc) then begin
							call use_skill_on_p_proc;
						end
					end
				end
			end
		end
	end
end

procedure damage_p_proc
begin
	destroy_object(self_obj);
end

procedure map_enter_p_proc
begin
	obj_close(self_obj);
	if ((game_time_hour >= 1900) or (game_time_hour < 600)) then begin
		obj_lock(self_obj);
	end
	else begin
		obj_unlock(self_obj);
	end
	set_local_var(0, 0);
end

procedure map_update_p_proc
begin
	if ((game_time_hour >= 1900) or (game_time_hour < 600) and (local_var(0) == 0)) then begin
		obj_close(self_obj);
		obj_lock(self_obj);
	end
	else begin
		obj_unlock(self_obj);
	end
end

procedure use_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_KILDOOR1, 100));
	end
	else begin
		if ((game_time_hour >= 700) and (game_time_hour < 1800) and obj_can_see_obj(dude_obj, Killian_ptr)) then begin
			script_overrides;
		end
	end
	set_map_var(9, 1);
end

procedure use_obj_on_p_proc
begin
	if (obj_pid(obj_being_used_with) == 84) then begin
		script_overrides;
		if (not((game_time_hour >= 700) and (game_time_hour < 1800)) and (obj_can_see_obj(Killian_ptr, dude_obj) == 0)) then begin
			if (not(obj_is_locked(self_obj))) then begin
				display_msg(message_str(SCRIPT_KILDOOR1, 104));
			end
			else begin
				test := roll_vs_skill(source_obj, 9, 0);
				if (is_success(test)) then begin
					display_msg(message_str(SCRIPT_KILDOOR1, 101));
					obj_unlock(self_obj);
					set_local_var(0, 1);
				end
				else begin
					if (is_critical(test)) then begin
						display_msg(message_str(SCRIPT_KILDOOR1, 103));
						jam_lock(self_obj);
					end
					else begin
						display_msg(message_str(SCRIPT_KILDOOR1, 102));
					end
				end
			end
		end
		set_map_var(9, 1);
	end
end

procedure use_skill_on_p_proc
begin
	if (action_being_used == 9) then begin
		script_overrides;
		if (not((game_time_hour >= 700) and (game_time_hour < 1800)) and (obj_can_see_obj(Killian_ptr, dude_obj) == 0)) then begin
			if (not(obj_is_locked(self_obj))) then begin
				display_msg(message_str(SCRIPT_KILDOOR1, 104));
			end
			else begin
				test := roll_vs_skill(source_obj, 9, -20);
				if (is_success(test)) then begin
					display_msg(message_str(SCRIPT_KILDOOR1, 101));
					obj_unlock(self_obj);
					set_local_var(0, 1);
				end
				else begin
					if (is_critical(test)) then begin
						display_msg(message_str(SCRIPT_KILDOOR1, 103));
						jam_lock(self_obj);
					end
					else begin
						display_msg(message_str(SCRIPT_KILDOOR1, 102));
					end
				end
			end
		end
		set_map_var(9, 1);
	end
end