#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;

procedure start;
#include <push.h>
procedure damage_p_proc;
procedure destroy_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;

import variable Neal_closing_door;
import variable Neal_ptr;
import variable SkumDoor_ptr;


procedure start
begin
	if (script_action == damage_proc) then begin
		call damage_p_proc;
	end
	else begin
		if (script_action == destroy_proc) then begin
			call destroy_p_proc;
		end
		else begin
			if (script_action == map_enter_proc) then begin
				call map_enter_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
	SkumDoor_ptr := 0;
	destroy_object(self_obj);
end

procedure destroy_p_proc
begin
	SkumDoor_ptr := 0;
end

procedure map_enter_p_proc
begin
	SkumDoor_ptr := self_obj;
	if ((game_time_hour > 410) and (game_time_hour < 1400)) then begin
		obj_close(self_obj);
		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 > 410) and (game_time_hour < 1400)) then begin
		if (local_var(0) == 0) then begin
			obj_close(self_obj);
			obj_lock(self_obj);
		end
	end
	else begin
		obj_unlock(self_obj);
	end
end

procedure use_p_proc
begin
	if (source_obj == dude_obj) then begin
		if (obj_is_locked(self_obj)) then begin
			script_overrides;
			display_msg(message_str(SCRIPT_SKUMDOOR, 100));
		end
	end
	else begin
		if (Neal_ptr != 0) then begin
			if (source_obj == Neal_ptr) then begin
				set_local_var(0, 0);
				if (Neal_closing_door == 0) then begin
					script_overrides;
					obj_open(self_obj);
					obj_unlock(self_obj);
				end
				else begin
					script_overrides;
					obj_close(self_obj);
					obj_lock(self_obj);
				end
			end
		end
	end
end

procedure use_obj_on_p_proc
begin
	if (obj_pid(obj_being_used_with) == 84) then begin
		if (obj_is_locked(self_obj)) then begin
			GVar0 := roll_vs_skill(source_obj, 9, 0);
			if (is_success(GVar0)) then begin
				display_msg(message_str(SCRIPT_SKUMDOOR, 101));
				obj_unlock(self_obj);
				set_local_var(0, 1);
			end
			else begin
				if (is_critical(GVar0)) then begin
					display_msg(message_str(SCRIPT_SKUMDOOR, 103));
					jam_lock(self_obj);
				end
				else begin
					display_msg(message_str(SCRIPT_SKUMDOOR, 102));
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_SKUMDOOR, 104));
		end
	end
end

procedure use_skill_on_p_proc
begin
	if (action_being_used == 9) then begin
		script_overrides;
		if (obj_is_locked(self_obj)) then begin
			GVar0 := roll_vs_skill(source_obj, action_being_used, -10);
			if (is_success(GVar0)) then begin
				display_msg(message_str(SCRIPT_SKUMDOOR, 101));
				obj_unlock(self_obj);
				set_local_var(0, 1);
			end
			else begin
				if (is_critical(GVar0)) then begin
					display_msg(message_str(SCRIPT_SKUMDOOR, 103));
					jam_lock(self_obj);
				end
				else begin
					display_msg(message_str(SCRIPT_SKUMDOOR, 102));
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_SKUMDOOR, 104));
		end
	end
end