#include <define.h>

//#define NAME                    SCRIPT_

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



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


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 damage_p_proc
begin
	set_obj_visibility(self_obj, 1);
	set_local_var(1, 1);
	set_map_var(4, 1);
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;
			if (map_var(5) == 0) then begin
				if (using_skill(dude_obj, 8) != 1) then begin
					set_map_var(4, 1);
				end
				else begin
					LVar0 := roll_vs_skill(dude_obj, 8, 0);
					if (is_success(LVar0) != 1) then begin
						set_map_var(4, 1);
					end
					else begin
						LVar0 := roll_vs_skill(dude_obj, 9, 0);
						if (is_success(LVar0)) then begin
							set_local_var(0, 1);
							display_msg(message_str(SCRIPT_DOOR, 100));
						end
						else begin
							display_msg(message_str(SCRIPT_DOOR, 103));
						end
					end
				end
			end
			else begin
				LVar0 := roll_vs_skill(dude_obj, 9, 0);
				if (is_success(LVar0)) then begin
					set_local_var(0, 1);
					display_msg(message_str(SCRIPT_DOOR, 100));
				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 use_obj_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := obj_being_used_with;
	if (obj_pid(LVar0) == 84) then begin
		script_overrides;
		if (map_var(5) == 0) then begin
			if (using_skill(dude_obj, 8) != 1) then begin
				set_map_var(4, 1);
			end
			else begin
				LVar1 := roll_vs_skill(dude_obj, 8, 0);
				if (is_success(LVar1) != 1) then begin
					set_map_var(4, 1);
				end
				else begin
					LVar1 := roll_vs_skill(dude_obj, 9, 20);
					if (is_success(LVar1)) then begin
						set_local_var(0, 1);
						display_msg(message_str(SCRIPT_DOOR, 100));
					end
					else begin
						if (is_critical(LVar1)) then begin
							rm_obj_from_inven(dude_obj, LVar0);
							destroy_object(LVar0);
							display_msg(message_str(SCRIPT_DOOR, 101));
						end
					end
				end
			end
		end
		else begin
			LVar1 := roll_vs_skill(dude_obj, 9, 20);
			if (is_success(LVar1)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_DOOR, 100));
			end
			else begin
				if (is_critical(LVar1)) then begin
					rm_obj_from_inven(dude_obj, LVar0);
					destroy_object(LVar0);
					display_msg(message_str(SCRIPT_DOOR, 101));
				end
			end
		end
	end
	else begin
		display_msg(message_str(SCRIPT_DOOR, 103));
	end
end

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

procedure use_p_proc
begin
	variable LVar0 := 0;
	if (local_var(0) == 0) then begin
		obj_lock(self_obj);
	end
	else begin
		if (map_var(5) == 0) then begin
			if (using_skill(dude_obj, 8) != 1) then begin
				set_map_var(4, 1);
			end
			else begin
				LVar0 := roll_vs_skill(dude_obj, 8, 0);
				if (is_success(LVar0)) then begin
					obj_unlock(self_obj);
				end
				else begin
					set_map_var(4, 1);
				end
			end
		end
		else begin
			obj_unlock(self_obj);
		end
	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 look_at_p_proc
begin
	if (local_var(0) == 0) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_DOOR, 104));
	end
end