#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure damage_p_proc;
procedure description_p_proc;
procedure map_enter_p_proc;
procedure spatial_p_proc;
procedure use_obj_on_p_proc;
procedure use_skill_on_p_proc;
procedure lockpicking;

variable skill_test;
variable triggered;

import variable jail_door_ptr;

procedure use_p_proc;


procedure start
begin
	if (script_action == damage_proc) then begin
		call damage_p_proc;
	end
	else begin
		if (script_action == description_proc) then begin
			call description_p_proc;
		end
		else begin
			if (script_action == map_enter_proc) then begin
				call map_enter_p_proc;
			end
			else begin
				if (script_action == spatial_proc) then begin
					call spatial_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

procedure damage_p_proc
begin
	destroy_object(self_obj);
end

procedure description_p_proc
begin
	script_overrides;
	display_msg(message_str(SCRIPT_JUNKJAIL, 101));
end

procedure map_enter_p_proc
begin
	if (tile_num(self_obj) == 18666) then begin
		obj_close(self_obj);
		obj_lock(self_obj);
		jail_door_ptr := self_obj;
	end
end

procedure spatial_p_proc
begin
	if ((source_obj == dude_obj) and (triggered == 0) and (map_var(1) == 1)) then begin
		display_msg(message_str(SCRIPT_JUNKJAIL, 100));
		set_map_var(1, 0);
		set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 3);
		display_msg(message_str(SCRIPT_JUNKJAIL, 107));
		give_exp_points(250);
		if (global_var(GVAR_ENEMY_JUNKTOWN) == 0) then begin
			set_global_var(GVAR_ENEMY_JUNKTOWN, 1);
			set_global_var(GVAR_PLAYER_REPUTATION, global_var(GVAR_PLAYER_REPUTATION) - 5);
		end
		triggered := 1;
		destroy_object(self_obj);
	end
end

procedure use_obj_on_p_proc
begin
	if (obj_pid(obj_being_used_with) == 84) then begin
		skill_test := roll_vs_skill(dude_obj, 9, -20);
		call lockpicking;
	end
end

procedure use_skill_on_p_proc
begin
	if (action_being_used == 9) then begin
		skill_test := roll_vs_skill(dude_obj, 9, 0);
		call lockpicking;
	end
end

procedure lockpicking
begin
	script_overrides;
	set_map_var(3, map_var(3) + 1);
	if (is_success(skill_test)) then begin
		if (obj_is_locked(self_obj)) then begin
			obj_unlock(self_obj);
			display_msg(message_str(SCRIPT_JUNKJAIL, 103));
		end
		else begin
			obj_lock(self_obj);
			display_msg(message_str(SCRIPT_JUNKJAIL, 104));
		end
	end
	else begin
		if (obj_is_locked(self_obj)) then begin
			display_msg(message_str(SCRIPT_JUNKJAIL, 105));
		end
		else begin
			display_msg(message_str(SCRIPT_JUNKJAIL, 106));
		end
	end
end

procedure use_p_proc
begin
	if (obj_is_locked(self_obj)) then begin
		script_overrides;
		display_msg(message_str(SCRIPT_JUNKJAIL, 102));
	end
end