#include <define.h>

//#define NAME                    SCRIPT_

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



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


procedure start
begin
	if (script_action == use_proc) then begin
		call use_p_proc;
	end
	else begin
		if (script_action == look_at_proc) then begin
			call look_at_p_proc;
		end
		else begin
			if (script_action == pickup_proc) then begin
				call pickup_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
					else begin
						if (script_action == map_update_proc) then begin
							call map_update_p_proc;
						end
					end
				end
			end
		end
	end
end

procedure pickup_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_GENLOCK, 100));
	end
end

procedure use_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_GENLOCK, 100));
	end
end

procedure use_skill_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := action_being_used;
	if (LVar0 == 9) then begin
		script_overrides;
		LVar1 := roll_vs_skill(source_obj, 9, 0);
		if (obj_is_locked(self_obj)) then begin
			if (is_success(LVar1)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_GENLOCK, 101));
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_GENCHAT, 103) + "25" + message_str(SCRIPT_GENCHAT, 104));
				give_exp_points(25);
			end
			else begin
				if (is_critical(LVar1)) then begin
					display_msg(message_str(SCRIPT_GENLOCK, 102));
					jam_lock(self_obj);
				end
				else begin
					display_msg(message_str(SCRIPT_GENLOCK, 103));
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_GENLOCK, 104));
		end
	end
	else begin
		if (LVar0 == 10) then begin
			if (obj_is_locked(self_obj)) then begin
				script_overrides;
				display_msg(message_str(SCRIPT_GENLOCK, 100));
			end
		end
	end
end

procedure use_obj_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar1 := obj_being_used_with;
	if (obj_pid(LVar1) == 84) then begin
		script_overrides;
		LVar0 := roll_vs_skill(source_obj, 9, 20);
		if (obj_is_locked(self_obj)) then begin
			if (is_success(LVar0)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_GENLOCK, 101));
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_GENCHAT, 103) + "25" + message_str(SCRIPT_GENCHAT, 104));
				give_exp_points(25);
			end
			else begin
				if (is_critical(LVar0)) then begin
					display_msg(message_str(SCRIPT_GENLOCK, 102));
					display_msg(message_str(SCRIPT_GENLOCK, 105));
					jam_lock(self_obj);
					rm_obj_from_inven(source_obj, LVar1);
					destroy_object(LVar1);
				end
				else begin
					display_msg(message_str(SCRIPT_GENLOCK, 103));
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_GENLOCK, 104));
		end
	end
end

procedure look_at_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_GENLOCK, 100));
	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