#include <define.h>

//#define NAME                    SCRIPT_

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


variable GVar0;

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

import variable Katja_ptr;
import variable messing_with_Morbid_stuff;


procedure start
begin
	if (script_action == look_at_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_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

procedure look_at_p_proc
begin
	set_map_var(4, self_obj);
end

procedure use_p_proc
begin
	messing_with_Morbid_stuff := 1;
	if (source_obj == dude_obj) then begin
		if (obj_is_locked(self_obj)) then begin
			script_overrides;
			display_msg(message_str(SCRIPT_DOCLOCKR, 100));
		end
		else begin
			if (local_var(2) == 0) then begin
				script_overrides;
				if (local_var(1) == 0) then begin
					GVar0 := roll_vs_skill(source_obj, 11, 0);
					if (is_success(GVar0)) then begin
						reg_anim_func(2, source_obj);
						GVar0 := roll_vs_skill(source_obj, 11, 0);
						if (is_success(GVar0)) then begin
							set_local_var(1, 1);
							set_local_var(2, 1);
							display_msg(message_str(SCRIPT_DOCLOCKR, 101));
						end
						else begin
							if (is_critical(GVar0) and (local_var(2) == 0)) then begin
								set_local_var(1, 1);
								set_local_var(2, 1);
								display_msg(message_str(SCRIPT_DOCLOCKR, 103));
								call set_off_trap;
							end
							else begin
								set_local_var(1, 1);
								display_msg(message_str(SCRIPT_DOCLOCKR, 102));
							end
						end
					end
					else begin
						call set_off_trap;
					end
				end
				else begin
					call set_off_trap;
				end
			end
		end
	end
	else begin
		if (source_obj == Katja_ptr) then begin
			set_local_var(1, 1);
			set_local_var(2, 1);
			obj_unlock(self_obj);
		end
	end
end

procedure use_obj_on_p_proc
begin
	messing_with_Morbid_stuff := 1;
	if (obj_pid(obj_being_used_with) == 84) then begin
		if (obj_is_locked(self_obj)) then begin
			script_overrides;
			GVar0 := roll_vs_skill(source_obj, 9, -40);
			if (is_success(GVar0)) then begin
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_DOCLOCKR, 107));
			end
			else begin
				if (is_critical(GVar0)) then begin
					jam_lock(self_obj);
					set_local_var(0, local_var(0) + 1);
					if ((local_var(0) >= 3) and (local_var(2) == 0)) then begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 106));
						call set_off_trap;
					end
					else begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 110));
					end
				end
				else begin
					set_local_var(0, local_var(0) + 1);
					if ((local_var(0) >= 3) and (local_var(2) == 0)) then begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 106));
						call set_off_trap;
					end
					else begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 108));
					end
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_DOCLOCKR, 109));
		end
	end
end

procedure use_skill_on_p_proc
begin
	messing_with_Morbid_stuff := 1;
	if (action_being_used == 9) then begin
		if (obj_is_locked(self_obj)) then begin
			script_overrides;
			GVar0 := roll_vs_skill(dude_obj, 9, -60);
			if (is_success(GVar0)) then begin
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_DOCLOCKR, 107));
			end
			else begin
				if (is_critical(GVar0)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_DOCLOCKR, 110));
				end
				else begin
					display_msg(message_str(SCRIPT_DOCLOCKR, 108));
					set_local_var(0, local_var(0) + 1);
					if ((local_var(0) >= 3) and (local_var(2) == 0)) then begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 106));
						call set_off_trap;
					end
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_DOCLOCKR, 109));
		end
	end
	else begin
		if (action_being_used == 11) then begin
			if (local_var(1) == 0) then begin
				GVar0 := roll_vs_skill(source_obj, 11, 0);
				if (is_success(GVar0)) then begin
					if (is_critical(GVar0)) then begin
						script_overrides;
						display_msg(message_str(SCRIPT_DOCLOCKR, 101));
						set_local_var(1, 1);
						set_local_var(2, 1);
					end
					else begin
						script_overrides;
						display_msg(message_str(SCRIPT_DOCLOCKR, 111));
						set_local_var(1, 1);
					end
				end
				else begin
					if (is_critical(GVar0)) then begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 106));
						call set_off_trap;
					end
				end
			end
			else begin
				if (local_var(2) == 0) then begin
					script_overrides;
					GVar0 := roll_vs_skill(source_obj, 11, 0);
					if (is_success(GVar0)) then begin
						display_msg(message_str(SCRIPT_DOCLOCKR, 104));
						set_local_var(2, 1);
					end
					else begin
						if (is_critical(GVar0)) then begin
							display_msg(message_str(SCRIPT_DOCLOCKR, 106));
							call set_off_trap;
						end
						else begin
							display_msg(message_str(SCRIPT_DOCLOCKR, 105));
						end
					end
				end
			end
		end
	end
end

procedure set_off_trap
begin
	if (local_var(2) == 0) then begin
		explosion(tile_num(self_obj), elevation(self_obj), random(6, 36));
		destroy_object(self_obj);
	end
end