#include <define.h>

//#define NAME                    SCRIPT_

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



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

variable initialized1;
variable on_tile;

import variable field_change;


procedure start
begin
	if (not(initialized1)) then begin
		if (tile_num(self_obj) == 21068) then begin
			if (obj_pid(self_obj) == 33554923) then begin
				if (field_change == "off") then begin
					set_map_var(14, 1);
				end
				set_map_var(8, self_obj);
				call use_p_proc;
			end
		end
		else begin
			if (tile_num(self_obj) == 25088) then begin
				if (obj_pid(self_obj) == 33554923) then begin
					if (field_change == "off") then begin
						set_map_var(15, 1);
					end
					set_map_var(9, self_obj);
					call use_p_proc;
				end
			end
		end
		initialized1 := 1;
	end
	else begin
		if (script_action == use_proc) then begin
			call use_p_proc;
		end
		else begin
			if (script_action == spatial_proc) then begin
				call spatial_p_proc;
			end
		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 spatial_p_proc
begin
	if ((tile_num(self_obj) == 21068) and (map_var(14) == 0) or ((tile_num(self_obj) == 25088) and (map_var(15) == 0))) then begin
		if (tile_num(source_obj) == tile_num(self_obj)) then begin
			if (not(on_tile)) then begin
				on_tile := 1;
				critter_dmg(source_obj, random(10, 20), 4);
			end
		end
		else begin
			on_tile := 0;
		end
	end
end

procedure turn_field_off
begin
	set_obj_visibility(self_obj, 1);
	if (tile_num(self_obj) == 21068) then begin
		set_map_var(14, 1);
	end
	else begin
		if (tile_num(self_obj) == 25088) then begin
			set_map_var(15, 1);
		end
	end
end

procedure turn_field_on
begin
	set_obj_visibility(self_obj, 0);
	if (tile_num(self_obj) == 21068) then begin
		set_map_var(14, 0);
	end
	else begin
		if (tile_num(self_obj) == 25088) then begin
			set_map_var(15, 0);
		end
	end
end

procedure toggle_field
begin
	if ((tile_num(self_obj) == 21068) and (map_var(14) == 1) or ((tile_num(self_obj) == 25088) and (map_var(15) == 1))) then begin
		call turn_field_on;
	end
	else begin
		call turn_field_off;
	end
end