#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure look_at_p_proc;
procedure description_p_proc;
procedure use_p_proc;
procedure use_skill_on_p_proc;
procedure use_obj_on_p_proc;
procedure map_enter_p_proc;
procedure map_update_p_proc;
procedure damage_p_proc;
procedure Locked_And_Trapped;
procedure Door_Locked;
procedure Door_Trapped;

variable Set_Off_Trap;


procedure start
begin
	if (script_action == look_at_proc) then begin
		call look_at_p_proc;
	end
	else begin
		if (script_action == description_proc) then begin
			call description_p_proc;
		end
		else begin
			if (script_action == map_update_proc) then begin
				call map_update_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_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
							end
						end
					end
				end
			end
		end
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	if (is_success(do_check(dude_obj, 1, 0))) then begin
		display_msg(message_str(SCRIPT_SETDOOR, 101));
	end
	else begin
		display_msg(message_str(SCRIPT_SETDOOR, 100));
	end
end

procedure description_p_proc
begin
	script_overrides;
	if ((local_var(1) == 0) and (local_var(0) == 0)) then begin
		call Locked_And_Trapped;
	end
	else begin
		if (local_var(0) == 0) then begin
			call Door_Trapped;
		end
		else begin
			if (local_var(1) == 0) then begin
				call Door_Locked;
			end
			else begin
				display_msg(message_str(SCRIPT_SETDOOR, 102));
			end
		end
	end
end

procedure use_p_proc
begin
	variable LVar0 := 0;
	LVar0 := roll_vs_skill(source_obj, 11, 0);
	if ((local_var(1) == 0) and (local_var(0) == 0)) then begin
		script_overrides;
		if (is_success(LVar0)) then begin
			reg_anim_func(2, source_obj);
			if (source_obj == dude_obj) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 106));
			end
			else begin
				display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
			end
		end
		else begin
			display_msg(message_str(SCRIPT_SETDOOR, 104));
			Set_Off_Trap := 1;
			explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
			set_local_var(0, 1);
			set_local_var(2, local_var(2) + 1);
			display_msg(message_str(SCRIPT_SETDOOR, 103));
		end
	end
	else begin
		if (local_var(0) == 0) then begin
			if (is_success(LVar0)) then begin
				script_overrides;
				reg_anim_func(2, source_obj);
				if (source_obj == dude_obj) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 106));
				end
				else begin
					display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
				end
			end
			else begin
				display_msg(message_str(SCRIPT_SETDOOR, 104));
				Set_Off_Trap := 1;
				explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
				set_local_var(0, 1);
				set_local_var(2, local_var(2) + 1);
			end
		end
		else begin
			if (local_var(1) == 0) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_SETDOOR, 103));
			end
		end
	end
end

procedure use_skill_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar1 := action_being_used;
	if (LVar1 == 11) then begin
		script_overrides;
		LVar0 := roll_vs_skill(source_obj, LVar1, 0);
		if (is_success(LVar0)) then begin
			reg_anim_func(2, source_obj);
			if (source_obj == dude_obj) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 106));
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_GENCHAT, 103) + "50" + message_str(SCRIPT_GENCHAT, 104));
				give_exp_points(50);
			end
			else begin
				display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
			end
		end
		else begin
			display_msg(message_str(SCRIPT_SETDOOR, 104));
			Set_Off_Trap := 1;
			explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
			set_local_var(0, 1);
			set_local_var(2, local_var(2) + 1);
		end
	end
	else begin
		if (LVar1 == 9) then begin
			script_overrides;
			LVar0 := roll_vs_skill(source_obj, LVar1, -20);
			if (is_success(LVar0)) then begin
				set_local_var(1, 1);
				display_msg(message_str(SCRIPT_SETDOOR, 111));
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_GENCHAT, 103) + "50" + message_str(SCRIPT_GENCHAT, 104));
				give_exp_points(50);
			end
			else begin
				if (is_critical(LVar0)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_SETDOOR, 112));
				end
				else begin
					display_msg(message_str(SCRIPT_SETDOOR, 110));
				end
			end
		end
	end
end

procedure use_obj_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	variable LVar2 := 0;
	LVar0 := obj_pid(obj_being_used_with);
	LVar1 := roll_vs_skill(source_obj, 11, 0);
	LVar2 := roll_vs_skill(source_obj, 9, 0);
	if (LVar0 == 84) then begin
		if ((local_var(1) == 0) and (local_var(0) == 0)) then begin
			script_overrides;
			if (is_success(LVar1)) then begin
				reg_anim_func(2, source_obj);
				if (source_obj == dude_obj) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 106));
				end
				else begin
					display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
				end
			end
			else begin
				display_msg(message_str(SCRIPT_SETDOOR, 104));
				Set_Off_Trap := 1;
				explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
				set_local_var(0, 1);
				set_local_var(2, local_var(2) + 1);
			end
			display_msg(message_str(SCRIPT_SETDOOR, 108));
		end
		else begin
			if (local_var(0) == 0) then begin
				script_overrides;
				if (is_success(LVar1)) then begin
					reg_anim_func(2, source_obj);
					if (source_obj == dude_obj) then begin
						display_msg(message_str(SCRIPT_SETDOOR, 106));
					end
					else begin
						display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
					end
				end
				else begin
					display_msg(message_str(SCRIPT_SETDOOR, 104));
					Set_Off_Trap := 1;
					explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
					set_local_var(0, 1);
					set_local_var(2, local_var(2) + 1);
				end
				display_msg(message_str(SCRIPT_SETDOOR, 108));
			end
			else begin
				if (local_var(1) == 0) then begin
					script_overrides;
					if (is_success(LVar2)) then begin
						set_local_var(1, 1);
						display_msg(message_str(SCRIPT_SETDOOR, 111));
						display_msg(message_str(SCRIPT_GENCHAT, 103) + "50" + message_str(SCRIPT_GENCHAT, 104));
						give_exp_points(50);
						obj_unlock(self_obj);
					end
					else begin
						if (is_critical(LVar2)) then begin
							rm_obj_from_inven(dude_obj, LVar0);
							destroy_object(LVar0);
							jam_lock(self_obj);
							display_msg(message_str(SCRIPT_SETDOOR, 109));
						end
						else begin
							display_msg(message_str(SCRIPT_SETDOOR, 110));
						end
					end
				end
			end
		end
	end
	else begin
		script_overrides;
		LVar1 := roll_vs_skill(source_obj, 11, 0);
		if ((local_var(1) == 0) and (local_var(0) == 0)) then begin
			script_overrides;
			if (is_success(LVar1)) then begin
				reg_anim_func(2, source_obj);
				if (source_obj == dude_obj) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 106));
				end
				else begin
					display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
				end
			end
			else begin
				display_msg(message_str(SCRIPT_SETDOOR, 104));
				Set_Off_Trap := 1;
				explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
				set_local_var(0, 1);
				set_local_var(2, local_var(2) + 1);
			end
		end
		else begin
			if (local_var(0) == 0) then begin
				if (is_success(LVar1)) then begin
					reg_anim_func(2, source_obj);
					if (source_obj == dude_obj) then begin
						display_msg(message_str(SCRIPT_SETDOOR, 106));
					end
					else begin
						display_msg(proto_data(obj_pid(source_obj), 1) + message_str(SCRIPT_SETDOOR, 105));
					end
				end
				else begin
					display_msg(message_str(SCRIPT_SETDOOR, 104));
					Set_Off_Trap := 1;
					explosion(tile_num(source_obj), elevation(self_obj), random(10, 30));
					set_local_var(0, 1);
					set_local_var(2, local_var(2) + 1);
				end
			end
			else begin
				if (local_var(1) == 0) then begin
					script_overrides;
					display_msg(message_str(SCRIPT_SETDOOR, 107));
				end
			end
		end
	end
end

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

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

procedure damage_p_proc
begin
	set_local_var(2, local_var(2) + 1);
	if (Set_Off_Trap == 0) then begin
		display_msg(message_str(SCRIPT_SETDOOR, 104));
		explosion(tile_num(self_obj), elevation(self_obj), random(20, 40));
		set_local_var(0, 1);
		Set_Off_Trap := 1;
	end
	if (local_var(2) > 1) then begin
		set_obj_visibility(self_obj, 1);
	end
end

procedure Locked_And_Trapped
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := roll_vs_skill(dude_obj, 11, 0);
	LVar1 := roll_vs_skill(dude_obj, 9, 0);
	if (is_success(LVar0)) then begin
		if (is_critical(LVar0)) then begin
			if (is_success(LVar1)) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 113));
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 114));
				end
			end
		end
		else begin
			if (is_success(LVar1)) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 115));
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 116));
				end
			end
		end
	end
	else begin
		if (is_critical(LVar0)) then begin
			if (is_success(LVar1)) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 117));
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 118));
				end
			end
		end
		else begin
			if (is_success(LVar1)) then begin
				display_msg(message_str(SCRIPT_SETDOOR, 117));
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_SETDOOR, 118));
				end
			end
		end
	end
end

procedure Door_Locked
begin
	variable LVar0 := 0;
	LVar0 := roll_vs_skill(dude_obj, 9, 0);
	if (is_success(LVar0)) then begin
		display_msg(message_str(SCRIPT_SETDOOR, 117));
	end
	else begin
		if (is_critical(LVar0)) then begin
			display_msg(message_str(SCRIPT_SETDOOR, 117));
		end
	end
end

procedure Door_Trapped
begin
	variable LVar0 := 0;
	LVar0 := roll_vs_skill(dude_obj, 11, 0);
	if (is_success(LVar0)) then begin
		if (is_critical(LVar0)) then begin
			display_msg(message_str(SCRIPT_SETDOOR, 114));
		end
		else begin
			display_msg(message_str(SCRIPT_SETDOOR, 116));
		end
	end
	else begin
		if (is_critical(LVar0)) then begin
			display_msg(message_str(SCRIPT_SETDOOR, 118));
		end
		else begin
			display_msg(message_str(SCRIPT_SETDOOR, 118));
		end
	end
end