#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure trap_stuff;
procedure find_trap;
procedure find_iq;
procedure disarm_trap;
procedure disarm_mech;
procedure failure;
procedure explode;
procedure use_door;

variable damage;
variable test;
variable temp;
variable use_skill;


procedure start
begin
	if (script_action == use_proc) then begin
		if (source_obj == dude_obj) then begin
			if (local_var(2)) then begin
				call use_door;
			end
			if (local_var(3) == 0) then begin
				set_local_var(3, 1);
				temp := 100;
				give_exp_points(temp);
				display_msg(message_str(SCRIPT_SETSCTDR, 200) + temp + message_str(SCRIPT_SETSCTDR, 201));
			end
			else begin
				if (local_var(1)) then begin
					call use_door;
				end
				else begin
					if (local_var(0)) then begin
						call failure;
					end
					else begin
						call find_iq;
					end
				end
			end
		end
		else begin
			call use_door;
		end
	end
	else begin
		if (script_action == use_skill_on_proc) then begin
			call trap_stuff;
		end
	end
end

procedure trap_stuff
begin
	use_skill := action_being_used;
	if (local_var(1) == 0) then begin
		if (local_var(0)) then begin
			if (use_skill == 11) then begin
				call disarm_trap;
			end
			else begin
				if (use_skill == 13) then begin
					call disarm_mech;
				end
			end
		end
	end
	else begin
		call find_trap;
	end
end

procedure find_trap
begin
	script_overrides;
	test := roll_vs_skill(dude_obj, 11, -20);
	if (is_success(test)) then begin
		display_msg(message_str(SCRIPT_SETSCTDR, 100));
		set_local_var(0, 1);
	end
	else begin
		display_msg(message_str(SCRIPT_SETSCTDR, 104));
		call explode;
	end
end

procedure find_iq
begin
	script_overrides;
	test := do_check(dude_obj, 4, 2);
	if (is_success(test)) then begin
		display_msg(message_str(SCRIPT_SETSCTDR, 100));
		set_local_var(0, 1);
	end
	else begin
		display_msg(message_str(SCRIPT_SETSCTDR, 104));
		call explode;
	end
end

procedure disarm_trap
begin
	script_overrides;
	test := roll_vs_skill(dude_obj, 11, 0);
	if (is_success(test)) then begin
		display_msg(message_str(SCRIPT_SETSCTDR, 102));
		set_local_var(1, 1);
	end
	else begin
		if (is_critical(test)) then begin
			display_msg(message_str(SCRIPT_SETSCTDR, 104));
			call explode;
		end
		else begin
			display_msg(message_str(SCRIPT_SETSCTDR, 103));
		end
	end
end

procedure disarm_mech
begin
	script_overrides;
	test := roll_vs_skill(dude_obj, 13, 20);
	if (is_success(test)) then begin
		display_msg(message_str(SCRIPT_SETSCTDR, 102));
		set_local_var(1, 1);
	end
	else begin
		if (is_critical(test)) then begin
			display_msg(message_str(SCRIPT_SETSCTDR, 104));
			call explode;
		end
		else begin
			display_msg(message_str(SCRIPT_SETSCTDR, 103));
		end
	end
end

procedure failure
begin
	script_overrides;
	display_msg(message_str(SCRIPT_SETSCTDR, 105));
	call explode;
end

procedure explode
begin
	set_local_var(1, 1);
	explosion(tile_num(self_obj), elevation(self_obj), 0);
	damage := random(1, 6) + random(1, 6) + random(1, 6) + random(1, 6) + random(1, 6) + random(1, 6);
	critter_dmg(dude_obj, damage, 0);
end

procedure use_door
begin
	if (local_var(2)) then begin
		set_local_var(2, 0);
	end
	else begin
		set_local_var(2, 1);
	end
end