#include <define.h>

//#define NAME                    SCRIPT_

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



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;

variable test;


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
	script_overrides;
	if (local_var(0) == 0) then begin
		display_msg(message_str(SCRIPT_HTWRBOX, 100));
	end
	else begin
		display_msg(message_str(SCRIPT_HTWRBOX, 101));
	end
end

procedure use_p_proc
begin
	if (local_var(0) == 0) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_HTWRBOX, 102));
	end
	else begin
		if (local_var(1) == 0) then begin
			script_overrides;
			if (local_var(2) == 0) then begin
				test := roll_vs_skill(dude_obj, 11, 0);
				if (is_success(test)) then begin
					reg_anim_func(2, source_obj);
					test := roll_vs_skill(dude_obj, 11, 0);
					if (is_success(test)) then begin
						set_local_var(2, 1);
						set_local_var(1, 1);
						display_msg(message_str(SCRIPT_HTWRBOX, 103));
					end
					else begin
						if (is_critical(test)) then begin
							set_local_var(2, 1);
							set_local_var(1, 1);
							display_msg(message_str(SCRIPT_HTWRBOX, 104));
							call set_off_trap;
						end
						else begin
							set_local_var(2, 1);
							set_local_var(1, 0);
							display_msg(message_str(SCRIPT_HTWRBOX, 105));
						end
					end
				end
				else begin
					set_local_var(1, 1);
					call set_off_trap;
				end
			end
			else begin
				set_local_var(1, 1);
				call set_off_trap;
			end
		end
	end
end

procedure use_obj_on_p_proc
begin
	if (obj_pid(obj_being_used_with) == 77) then begin
		if (local_var(0) == 0) then begin
			script_overrides;
			test := roll_vs_skill(dude_obj, 9, 0);
			if (is_success(test)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_HTWRBOX, 107));
				set_local_var(3, local_var(3) + 1);
				if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
					display_msg(message_str(SCRIPT_HTWRBOX, 108));
					set_local_var(1, 1);
					call set_off_trap;
				end
			end
			else begin
				if (is_critical(test)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_HTWRBOX, 109));
					set_local_var(3, local_var(3) + 1);
					if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
						display_msg(message_str(SCRIPT_HTWRBOX, 108));
						set_local_var(1, 1);
						call set_off_trap;
					end
				end
				else begin
					display_msg(message_str(SCRIPT_HTWRBOX, 110));
					set_local_var(3, local_var(3) + 1);
					if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
						display_msg(message_str(SCRIPT_HTWRBOX, 108));
						set_local_var(1, 1);
						call set_off_trap;
					end
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_HTWRBOX, 106));
		end
	end
end

procedure use_skill_on_p_proc
begin
	if (action_being_used == 9) then begin
		if (local_var(0) == 0) then begin
			script_overrides;
			test := roll_vs_skill(dude_obj, 9, -20);
			if (is_success(test)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_HTWRBOX, 107));
				set_local_var(3, local_var(3) + 1);
				if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
					display_msg(message_str(SCRIPT_HTWRBOX, 108));
					set_local_var(1, 1);
					call set_off_trap;
				end
			end
			else begin
				if (is_critical(test)) then begin
					jam_lock(self_obj);
					display_msg(message_str(SCRIPT_HTWRBOX, 109));
					set_local_var(3, local_var(3) + 1);
					if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
						display_msg(message_str(SCRIPT_HTWRBOX, 108));
						set_local_var(1, 1);
						call set_off_trap;
					end
				end
				else begin
					display_msg(message_str(SCRIPT_HTWRBOX, 110));
					set_local_var(3, local_var(3) + 1);
					if ((local_var(3) >= 3) and (local_var(1) == 0)) then begin
						display_msg(message_str(SCRIPT_HTWRBOX, 108));
						set_local_var(1, 1);
						call set_off_trap;
					end
				end
			end
		end
		else begin
			display_msg(message_str(SCRIPT_HTWRBOX, 106));
		end
	end
	else begin
		if (action_being_used == 11) then begin
			if (local_var(2) == 0) then begin
				test := roll_vs_skill(dude_obj, 11, 0);
				if (is_success(test)) then begin
					if (is_critical(test)) then begin
						script_overrides;
						display_msg(message_str(SCRIPT_HTWRBOX, 111));
						set_local_var(2, 1);
						set_local_var(1, 1);
					end
					else begin
						script_overrides;
						display_msg(message_str(SCRIPT_HTWRBOX, 112));
						set_local_var(2, 1);
					end
				end
				else begin
					if (is_critical(test)) then begin
						script_overrides;
						display_msg(message_str(SCRIPT_HTWRBOX, 113));
						set_local_var(1, 1);
						call set_off_trap;
					end
					else begin
						script_overrides;
						display_msg(message_str(SCRIPT_HTWRBOX, 114));
					end
				end
			end
			else begin
				script_overrides;
				test := roll_vs_skill(dude_obj, 11, 0);
				if (is_success(test)) then begin
					display_msg(message_str(SCRIPT_HTWRBOX, 115));
					set_local_var(1, 1);
				end
				else begin
					if (is_critical(test)) then begin
						display_msg(message_str(SCRIPT_HTWRBOX, 116));
						set_local_var(1, 1);
						call set_off_trap;
					end
					else begin
						display_msg(message_str(SCRIPT_HTWRBOX, 117));
					end
				end
			end
		end
	end
end

procedure set_off_trap
begin
	explosion(tile_num(self_obj), elevation(self_obj), random(6, 36));
	set_local_var(0, 1);
	set_local_var(1, 1);
	obj_unlock(self_obj);
	obj_open(self_obj);
end