#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure Initialize_Fridge;
procedure Looting_Fridge;
procedure Open_Fridge;
procedure pick_lock;
procedure map_update_p_proc;

import variable Garret_ptr;
import variable Fridge_ptr;

variable last_user;


procedure start
begin
	if (script_action == map_enter_proc) then begin
		call Initialize_Fridge;
	end
	else begin
		if (script_action == map_update_proc) then begin
			call map_update_p_proc;
		end
		else begin
			if (script_action == pickup_proc) then begin
				call Looting_Fridge;
			end
			else begin
				if (script_action == use_proc) then begin
					call Open_Fridge;
				end
				else begin
					if (script_action == timed_event_proc) then begin
						float_msg(Garret_ptr, message_str(SCRIPT_NGARFRDG, 105), FLOAT_MSG_NORMAL);
					end
					else begin
						if (script_action == use_obj_on_proc) then begin
							if (obj_pid(obj_being_used_with) == 84) then begin
								call pick_lock;
							end
						end
						else begin
							if (script_action == use_skill_on_proc) then begin
								if (action_being_used == 9) then begin
									call pick_lock;
								end
							end
						end
					end
				end
			end
		end
	end
end

procedure Initialize_Fridge
begin
	Fridge_ptr := self_obj;
end

procedure Looting_Fridge
begin
	if (source_obj != Garret_ptr) then begin
		if (local_var(0) == 0) then begin
			script_overrides;
		end
		else begin
			if (local_var(1) == 0) then begin
				script_overrides;
			end
		end
	end
end

procedure Open_Fridge
begin
	last_user := source_obj;
	if (source_obj == Garret_ptr) then begin
		if (local_var(1) == 0) then begin
			set_local_var(0, 1);
			set_local_var(1, 1);
		end
		else begin
			if (last_user == Garret_ptr) then begin
				set_local_var(1, 0);
				set_local_var(0, 0);
			end
			else begin
				script_overrides;
				float_msg(Garret_ptr, message_str(SCRIPT_NGARFRDG, 104), FLOAT_MSG_NORMAL);
			end
		end
	end
	else begin
		if (local_var(0) == 0) then begin
			script_overrides;
			display_msg(message_str(SCRIPT_NGARFRDG, 100));
		end
		else begin
			if (local_var(1)) then begin
				set_local_var(1, 0);
			end
			else begin
				set_local_var(1, 1);
			end
		end
	end
end

procedure pick_lock
begin
	script_overrides;
	if (local_var(0)) then begin
		display_msg(message_str(SCRIPT_NGARFRDG, 101));
	end
	else begin
		if (is_success(roll_vs_skill(dude_obj, 9, 0))) then begin
			display_msg(message_str(SCRIPT_NGARFRDG, 102));
			set_local_var(0, 1);
			display_msg(message_str(SCRIPT_GENCHAT, 103) + "25" + message_str(SCRIPT_GENCHAT, 104));
			give_exp_points(25);
		end
		else begin
			display_msg(message_str(SCRIPT_NGARFRDG, 103));
		end
	end
end

procedure map_update_p_proc
begin
	Fridge_ptr := self_obj;
end