// Power Sources for Puzzles v1.0
// sourcepuzzle.txt
// by Kelandon (tomwatts@berkeley.edu)

// This is the black magic that I have to do so that my initially inactive power
// sources turn off when they are not being powered.

// This script flips terrains based on the presence of up to three mirrors. 
// Basically, you input the places where the mirrors have to be in order to turn 
// on this particular power source, and if they are not there, this source shuts 
// off.

// I have little hope that this script will be useful to anyone but me, but
// maybe someone will be interested.

// Memory Cells - 
//	0 - Whether you want the power source to shut off instantly or delay a
//		little. 0 is delay; 1 is instantly. NOTE: If you have a lot of these
//		scripts in a town, setting all of them to 1 will slow down the game.
//	1 - If there is an alternate mirror location. If this box is 1, the script
// 		will check for a mirror in a place specified in mem cells 6 and 7 and if
//		there are mirrors either in both 2,3 and 4,5 OR in 6,7, this power
//		source will stay on.
//	2,3 - Coordinates of first mirror.
//	4,5 - Coordinates of second mirror.
//		NOTE: If both 0, script does not check for second or third mirror.
//	6,7 - Coordinates of third mirror.
//		NOTE: If both 0, script does not check for third mirror.

beginterrainscript; 

variables;
int num_mirrors,i_am_off;

body;

beginstate INIT_STATE;
if (get_memory_cell(0) == 1)
	set_script_mode(3);
	
	num_mirrors = 3;
if ((get_memory_cell(4) == 0) && (get_memory_cell(5) == 0))
	{num_mirrors = 1;
	end(); }
if (((get_memory_cell(6) == 0) && (get_memory_cell(7) == 0)) || (get_memory_cell(1)))
	num_mirrors = 2;
break;

beginstate START_STATE;
	i_am_off = 0;

	if ((is_object_on_space(get_memory_cell(2),get_memory_cell(3),5) == 0) && (is_object_on_space(get_memory_cell(2),get_memory_cell(3),6) == 0))
		i_am_off = 1;
	
	if ((num_mirrors > 1) && ((is_object_on_space(get_memory_cell(4),get_memory_cell(5),5) == 0) && (is_object_on_space(get_memory_cell(4),get_memory_cell(5),6) == 0)))
		i_am_off = 1;
		
	if ((get_memory_cell(1) == 1) && (i_am_off == 1))
		i_am_off = 2;
			
	if (((num_mirrors > 2) || ((get_memory_cell(1) == 1) && (i_am_off == 2))) && ((is_object_on_space(get_memory_cell(6),get_memory_cell(7),5) == 0) && (is_object_on_space(get_memory_cell(6),get_memory_cell(7),6) == 0)))
		i_am_off = 1;
	
	if (i_am_off == 1)
		set_terrain(my_loc_x(),my_loc_y(),388);
		
break;

beginstate SEARCH_STATE;
break;
