#include <define.h>

//#define NAME                    SCRIPT_

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



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

import variable Red_Door_Ptr;
import variable Laura_Ptr;


procedure start
begin
	if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
		call look_at_p_proc;
	end
	else begin
		if (script_action == use_proc) then begin
			call use_p_proc;
		end
		else begin
			if (script_action == use_skill_on_proc) then begin
				call use_skill_on_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 == damage_proc) then begin
						call damage_p_proc;
					end
					else begin
						if (script_action == map_update_proc) then begin
							call map_update_p_proc;
						end
					end
				end
			end
		end
	end
end

procedure use_p_proc
begin
	if (source_obj == Laura_Ptr) then begin
		set_local_var(0, 1);
	end
	if (local_var(0) == 0) then begin
		obj_lock(self_obj);
	end
	else begin
		obj_unlock(self_obj);
	end
	if ((local_var(0) == 0) and (source_obj == dude_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_DOOR, 104));
	end
end

procedure use_skill_on_p_proc
begin
	variable LVar0 := 0;
	if (local_var(0) == 0) then begin
		if (action_being_used == 9) then begin
			script_overrides;
			LVar0 := roll_vs_skill(dude_obj, 9, -60);
			if (is_success(LVar0)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_DOOR, 100));
				display_msg(message_str(SCRIPT_GENCHAT, 103) + "85" + message_str(SCRIPT_GENCHAT, 104));
				give_exp_points(85);
			end
			else begin
				if (is_critical(LVar0)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_DOOR, 110));
				end
				else begin
					display_msg(message_str(SCRIPT_DOOR, 103));
				end
			end
		end
	end
	else begin
		display_msg(message_str(SCRIPT_DOOR, 109));
	end
end

procedure look_at_p_proc
begin
	if (local_var(0) == 0) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_DOOR, 104));
	end
end

procedure use_obj_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := obj_being_used_with;
	LVar1 := roll_vs_skill(dude_obj, 9, -40);
	if (obj_pid(LVar0) == 84) then begin
		script_overrides;
		if (is_success(LVar1)) then begin
			set_local_var(0, 1);
			display_msg(message_str(SCRIPT_DOOR, 100));
			display_msg(message_str(SCRIPT_GENCHAT, 103) + "85" + message_str(SCRIPT_GENCHAT, 104));
			give_exp_points(85);
		end
		else begin
			if (is_critical(LVar1)) then begin
				rm_obj_from_inven(dude_obj, LVar0);
				destroy_object(LVar0);
				jam_lock(self_obj);
				display_msg(message_str(SCRIPT_DOOR, 110));
				display_msg(message_str(SCRIPT_DOOR, 101));
			end
			else begin
				display_msg(message_str(SCRIPT_DOOR, 103));
			end
		end
	end
	else begin
		if ((obj_pid(LVar0) == 141) or (obj_pid(LVar0) == 142)) then begin
			script_overrides;
			set_local_var(0, 1);
			display_msg(message_str(SCRIPT_DOOR, 100));
		end
	end
end

procedure map_update_p_proc
begin
	Red_Door_Ptr := self_obj;
	if (local_var(0) == 0) then begin
		obj_lock(self_obj);
	end
	else begin
		obj_unlock(self_obj);
	end
end

procedure damage_p_proc
begin
	set_obj_visibility(self_obj, 1);
	set_local_var(1, 1);
end