#include <define.h>

//#define NAME                    SCRIPT_

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



procedure start;
#include <push.h>
procedure use_p_proc;
procedure turn_field_on;
procedure turn_field_off;
procedure toggle_field;

variable initialized1;

import variable field_change;


procedure start
begin
	if (not(initialized1)) then begin
		set_map_var(3, self_obj);
		set_local_var(1, tile_num(self_obj));
		call use_p_proc;
		initialized1 := 1;
	end
	else begin
		if (script_action == use_proc) then begin
			call use_p_proc;
		end
	end
end

procedure use_p_proc
begin
	if (global_var(GVAR_FORCE_FIELDS_OFF)) then begin
		field_change := "off";
	end
	if (source_obj != dude_obj) then begin
		if (field_change == "toggle") then begin
			call toggle_field;
		end
		else begin
			if (field_change == "off") then begin
				call turn_field_off;
			end
			else begin
				if (field_change == "on") then begin
					call turn_field_on;
				end
			end
		end
	end
end

procedure turn_field_on
begin
	set_obj_visibility(self_obj, 0);
	set_local_var(0, 0);
end

procedure turn_field_off
begin
	set_obj_visibility(self_obj, 1);
	set_local_var(0, 1);
end

procedure toggle_field
begin
	if (local_var(0)) then begin
		call turn_field_on;
	end
	else begin
		call turn_field_off;
	end
end