// Pushable Terrain v1.0
// pushterrain.txt
// by Kelandon (tomwatts@berkeley.edu)
//
// For terrains that can be pushed. Put this script on every space across which
// the terrain can be pushed.
//
// NOTE: This terrain will not bounce off of boundaries the way that objects
// (crates, etc.) bounce off walls. You actually have to push it in the
// direction you want it to go. This becomes important with free movement (cell
// 0 left at 0).
//
// Memory Cells -
//	0 - If the terrain can be pushed in any direction (0), just north-south (1),
//		or just east-west (2).
//	1 - If this tile is a boundary (meaning that the terrain can't be pushed any
//		farther in this direction). If 0, it is not. If 1, it is a northern or
//		western boundary (depending on whether cell 0 was set to 1 or 2). If 2,
//		it is a southern or eastern boundary. If 3, then this is a corner (cell
//		0 must be 0 for this), and cell 2 defines which corner it is.
//	2 - If cell 0 is 0 and cell 1 is 3, this defines what kind of corner this is
//		(0 is northwest, 1 northeast, 2 southwest, 3 southeast). 
//	3,4 - Normally this script will determine the first character in the party
//		(the one doing the pushing) upon town entry. If the leading character
//		may change in the town, set an SDF with these coordinates to the number
//		of the leading character. If both cells are zero, this is ignored.
//	5 - Sound to make when moving. If left at 0, defaults to 45 (stone sliding).
beginterrainscript; 

variables;
short i,j,first_char,my_terrain;

body;

beginstate INIT_STATE; // state 0
	first_char = 0;
	while (char_ok(first_char) == 0)
		{first_char = first_char + 1; }
		
	if (get_memory_cell(5) == 0)
		set_memory_cell(5,45);

	if ((get_memory_cell(1) == 3) && (get_memory_cell(0) != 0))
		print_str_color("Error in pushterrain: memory cells set wrong.",1);
break;

beginstate START_STATE; // state 2
break;

beginstate SEARCH_STATE; // state 100
break;

beginstate UNLOCK_SPELL_STATE; // state 101
break;

beginstate SANCTIFICATION_STATE; // state 102
break;

beginstate BLOCK_MOVE_STATE; // state 112
	if ((get_memory_cell(3) != 0) || (get_memory_cell(4) != 0))
		first_char = get_flag(get_memory_cell(3),get_memory_cell(4));

	if (get_memory_cell(0) == 0)
		set_state_continue(10);
	if (get_memory_cell(0) == 1)
		set_state_continue(11);
		
		set_state_continue(12);
break;

beginstate DISPEL_BARRIER_STATE; // state 113
break;

beginstate STEP_INTO_SPOT_STATE; // state 114
break;

beginstate 10;
	if ((char_loc_x(first_char) > my_loc_x()) && ((get_memory_cell(2) == 0) || (get_memory_cell(2) == 2)))
		end();
	if ((char_loc_x(first_char) < my_loc_x()) && ((get_memory_cell(2) == 1) || (get_memory_cell(2) == 3)))
		end();
	if ((char_loc_y(first_char) > my_loc_y()) && ((get_memory_cell(2) == 0) || (get_memory_cell(2) == 1)))
		end();
	if ((char_loc_y(first_char) < my_loc_y()) && ((get_memory_cell(2) == 2) || (get_memory_cell(2) == 4)))
		end();

	my_terrain = get_terrain(my_loc_x(),my_loc_y());
	set_terrain(my_loc_x(),my_loc_y(),0);
	set_terrain(2 * my_loc_x() - char_loc_x(first_char),2 * my_loc_y() - char_loc_y(first_char),my_terrain);
	play_sound(get_memory_cell(5));
break;

beginstate 11;
	if ((char_loc_y(first_char) > my_loc_y()) && (get_memory_cell(1) == 1))
		end();
		
	if ((char_loc_y(first_char) < my_loc_y()) && (get_memory_cell(1) == 2))
		end();
		
	my_terrain = get_terrain(my_loc_x(),my_loc_y());
	set_terrain(my_loc_x(),my_loc_y(),0);
	set_terrain(my_loc_x(),2 * my_loc_y() - char_loc_y(first_char),my_terrain);
	play_sound(get_memory_cell(5));
break;

beginstate 12;
	if ((char_loc_x(first_char) > my_loc_x()) && (get_memory_cell(1) == 1))
		end();
		
	if ((char_loc_x(first_char) < my_loc_x()) && (get_memory_cell(1) == 2))
		end();
		
	my_terrain = get_terrain(my_loc_x(),my_loc_y());
	set_terrain(my_loc_x(),my_loc_y(),0);
	set_terrain(2 * my_loc_x() - char_loc_x(first_char),my_loc_y(),my_terrain);
	play_sound(get_memory_cell(5));
break;