#include <define.h>

//#define NAME                    SCRIPT_

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



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

import variable jake_door_ptr;

variable OnceOnly := 1;


procedure start
begin
	if (OnceOnly) then begin
		OnceOnly := 0;
		jake_door_ptr := self_obj;
		call map_update_p_proc;
	end
	if ((script_action == look_at_proc) or (script_action == description_proc)) then begin
		call look_at_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
				else begin
					if (script_action == damage_proc) then begin
						call damage_p_proc;
					end
				end
			end
		end
	end
end

procedure look_at_p_proc
begin
	if (local_var(0) == 0) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_HUBJAKE, 203));
	end
end

procedure use_obj_on_p_proc
begin
	variable LVar0 := 0;
	variable LVar1 := 0;
	LVar0 := obj_being_used_with;
	LVar1 := roll_vs_skill(dude_obj, 9, -20);
	if (obj_pid(LVar0) == 84) then begin
		script_overrides;
		if (is_success(LVar1)) then begin
			set_local_var(0, 1);
			display_msg(message_str(SCRIPT_HUBJAKE, 204));
		end
		else begin
			if (is_critical(LVar1)) then begin
				rm_obj_from_inven(dude_obj, LVar0);
				destroy_object(LVar0);
				display_msg(message_str(SCRIPT_HUBJAKE, 207));
			end
			else begin
				display_msg(message_str(SCRIPT_HUBJAKE, 205));
			end
		end
	end
end

procedure map_update_p_proc
begin
	if (not((game_time_hour >= 1900) or (game_time_hour < 600))) then begin
		set_local_var(0, 1);
	end
	if (local_var(0) == 0) then begin
		obj_lock(self_obj);
	end
	else begin
		obj_unlock(self_obj);
	end
	set_map_var(7, 0);
	if (map_var(6) == 0) then begin
		if (obj_is_open(self_obj) == 1) then begin
			set_map_var(7, 1);
		end
	end
	if (obj_is_locked(self_obj) == 1) then begin
		set_local_var(0, 1);
	end
	else begin
		set_local_var(0, 0);
	end
end

procedure damage_p_proc
begin
	set_obj_visibility(self_obj, 1);
	set_local_var(1, 1);
	set_map_var(6, 1);
end

procedure use_skill_on_p_proc
begin
	variable LVar0 := 0;
	if (local_var(0) == 0) then begin
		if (action_being_used == 9) then begin
			script_overrides;
			LVar0 := roll_vs_skill(dude_obj, 9, -40);
			if (is_success(LVar0)) then begin
				set_local_var(0, 1);
				display_msg(message_str(SCRIPT_HUBJAKE, 204));
			end
			else begin
				display_msg(message_str(SCRIPT_HUBJAKE, 205));
			end
		end
	end
	else begin
		display_msg(message_str(SCRIPT_HUBJAKE, 206));
	end
end