#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure description_p_proc;
procedure look_at_p_proc;
procedure map_enter_p_proc;
procedure pickup_p_proc;
procedure use_p_proc;
procedure use_obj_on_p_proc;
procedure use_skill_on_p_proc;
procedure safe_bonus;

variable item;
variable test;

import variable KillSafe_ptr;


procedure start
begin
	if (script_action == description_proc) then begin
		call description_p_proc;
	end
	else begin
		if (script_action == look_at_proc) then begin
			call look_at_p_proc;
		end
		else begin
			if (script_action == map_enter_proc) then begin
				call map_enter_p_proc;
			end
			else begin
				if (script_action == pickup_proc) then begin
					call pickup_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
		end
	end
end

procedure description_p_proc
begin
	script_overrides;
	if (obj_is_locked(self_obj)) then begin
		display_msg(message_str(SCRIPT_KILLSAFE, 105));
	end
	else begin
		display_msg(message_str(SCRIPT_KILLSAFE, 101));
	end
end

procedure look_at_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_KILLSAFE, 100));
end

procedure map_enter_p_proc
begin
	KillSafe_ptr := self_obj;
	obj_close(self_obj);
	obj_lock(self_obj);
	if (local_var(0) == 0) then begin
		if (random(0, 3) == 3) then begin
			item := create_object_sid(40, 0, 0, -1);
			add_obj_to_inven(self_obj, item);
		end
		if (random(0, 3) == 3) then begin
			item := create_object_sid(31, 0, 0, -1);
			add_obj_to_inven(self_obj, item);
		end
		if (random(0, 3) == 3) then begin
			item := create_object_sid(30, 0, 0, -1);
			add_obj_to_inven(self_obj, item);
		end
		if (random(0, 3) == 3) then begin
			item := create_object_sid(34, 0, 0, -1);
			add_obj_to_inven(self_obj, item);
		end
		if (random(0, 3) == 3) then begin
			item := create_object_sid(4, 0, 0, -1);
			add_obj_to_inven(self_obj, item);
		end
	end
end

procedure pickup_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
	end
end

procedure use_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_KILLSAFE, 105));
		set_map_var(9, 1);
	end
end

procedure use_obj_on_p_proc
begin
	if (obj_pid(obj_being_used_with) == 84) then begin
		script_overrides;
		set_map_var(9, 1);
		if (not(obj_is_locked(self_obj))) then begin
			display_msg(message_str(SCRIPT_KILLSAFE, 101));
		end
		else begin
			test := roll_vs_skill(dude_obj, 9, -10);
			if (is_success(test)) then begin
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_KILLSAFE, 103));
				call safe_bonus;
			end
			else begin
				if (is_critical(test)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_KILLSAFE, 107));
				end
				else begin
					display_msg(message_str(SCRIPT_KILLSAFE, 104));
				end
			end
		end
	end
end

procedure use_skill_on_p_proc
begin
	if (action_being_used == 9) then begin
		script_overrides;
		set_map_var(9, 1);
		if (not(obj_is_locked(self_obj))) then begin
			display_msg(message_str(SCRIPT_KILLSAFE, 101));
		end
		else begin
			test := roll_vs_skill(dude_obj, 9, -30);
			if (is_success(test)) then begin
				obj_unlock(self_obj);
				display_msg(message_str(SCRIPT_KILLSAFE, 103));
				call safe_bonus;
			end
			else begin
				if (is_critical(test)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_KILLSAFE, 107));
				end
				else begin
					display_msg(message_str(SCRIPT_KILLSAFE, 104));
				end
			end
		end
	end
end

procedure safe_bonus
begin
	if (local_var(0) == 0) then begin
		display_msg(message_str(SCRIPT_KILLSAFE, 106));
		set_local_var(0, 1);
		give_exp_points(500);
	end
end