// Rock Pusher v1.0
// rockpusher.txt 
// by Kelandon (tomwatts@berkeley.edu) 
// 
// Pushes a terrain around, up hills, in various directions, with various
// limitations.
// 
// Memory Cells
// 1 - North.
// 2 - West.
// 3 - South.
// 4 - East.
//
// If the cell is 0, the direction is flat and unblocked.
// If 1, the direction is a small uphill. The terrain moves two spaces.
// If 2, same as 1 but downhill.
// If 3, the direction is too steep. The terrain cannot move this way.
// If 4, the ground is too uneven.
// If 5, there is a rock in the way.
// If 6, just can't push it this way.
// If 7, pushing it this way solves the puzzle.
//
// Cell 5 - If this is not 0, terrain resets to a blue rune rather than
//		emptyness once the rock is moved away.
// Cell 6 - If this is not 0, this terrain is a dead spot. If the rock is here,
//		you have to restart.

beginterrainscript; 

variables;
short i,j,k;

body; 

beginstate INIT_STATE; // state 0 
break; 

beginstate START_STATE; // state 2 
break; 

beginstate 10; // moving north
if (get_memory_cell(1) == 3)
	set_state_continue(23);
if (get_memory_cell(1) == 4)
	set_state_continue(24);
if (get_memory_cell(1) == 5)
	set_state_continue(25);
if (get_memory_cell(1) == 6)
	set_state_continue(26);
if (get_memory_cell(1) == 7)
	set_state_continue(27);
	
if (get_memory_cell(1) == 0) {
	print_str_color("You push the rock.",2);
	set_terrain(my_loc_x(),my_loc_y() - 1,422);
	}
else {
	if (get_memory_cell(1) == 1)
		print_str_color("You push the rock uphill.",2);
	else
		print_str_color("You push the rock downhill.",2);
	set_terrain(my_loc_x(),my_loc_y() - 2,422);
	}
	
	set_state_continue(20);
break;

beginstate 11; // moving west
if (get_memory_cell(2) == 3)
	set_state_continue(23);
if (get_memory_cell(2) == 4)
	set_state_continue(24);
if (get_memory_cell(2) == 5)
	set_state_continue(25);
if (get_memory_cell(2) == 6)
	set_state_continue(26);
if (get_memory_cell(2) == 7)
	set_state_continue(27);
	
if (get_memory_cell(2) == 0) {
	print_str_color("You push the rock.",2);
	set_terrain(my_loc_x() - 1,my_loc_y(),422);
	}
else {
	if (get_memory_cell(2) == 1)
		print_str_color("You push the rock uphill.",2);
	else
		print_str_color("You push the rock downhill.",2);
	set_terrain(my_loc_x() - 2,my_loc_y(),422);
	}
	
	set_state_continue(20);
break;

beginstate 12; // moving south
if (get_memory_cell(3) == 3)
	set_state_continue(23);
if (get_memory_cell(3) == 4)
	set_state_continue(24);
if (get_memory_cell(3) == 5)
	set_state_continue(25);
if (get_memory_cell(3) == 6)
	set_state_continue(26);
if (get_memory_cell(3) == 7)
	set_state_continue(27);
	
if (get_memory_cell(3) == 0) {
	print_str_color("You push the rock.",2);
	set_terrain(my_loc_x(),my_loc_y() + 1,422);
	}
else {
	if (get_memory_cell(3) == 1)
		print_str_color("You push the rock uphill.",2);
	else
		print_str_color("You push the rock downhill.",2);
	set_terrain(my_loc_x(),my_loc_y() + 2,422);
	}
	
	set_state_continue(20);
break;

beginstate 13; // moving east
if (get_memory_cell(4) == 3)
	set_state_continue(23);
if (get_memory_cell(4) == 4)
	set_state_continue(24);
if (get_memory_cell(4) == 5)
	set_state_continue(25);
if (get_memory_cell(4) == 6)
	set_state_continue(26);
if (get_memory_cell(4) == 7)
	set_state_continue(27);
	
if (get_memory_cell(4) == 0) {
	print_str_color("You push the rock.",2);
	set_terrain(my_loc_x() + 1,my_loc_y(),422);
	}
else {
	if (get_memory_cell(4) == 1)
		print_str_color("You push the rock uphill.",2);
	else
		print_str_color("You push the rock downhill.",2);
	set_terrain(my_loc_x() + 2,my_loc_y(),422);
	}
	
	set_state_continue(20);
break;

beginstate 20;
	if (get_memory_cell(5) == 0)
		set_terrain(my_loc_x(),my_loc_y(),0);
	else
		set_terrain(my_loc_x(),my_loc_y(),271);
break;

beginstate 23; // Too steep uphill
	print_str_color("The ground is too steep to push the rock this way.",2);
break;

beginstate 24; // Too uneven
	print_str_color("The ground is too uneven to push the rock this way.",2);
break;

beginstate 25; // Rock in the way
	print_str_color("A rock prevents you from pushing the rock this way.",2);
break;

beginstate 26; // Can't do it
	print_str_color("You can't push the rock this way.",2);
break;

beginstate 27; // Victory!
	put_boom_on_space(my_loc_x(),my_loc_y(),3,0);
	set_terrain(my_loc_x(),my_loc_y(),271);
	run_animation_sound(60);
	message_dialog("You push the boulder onto the platform, and it vanishes! You can hear Siisufoss sobbing with gratitude down below.","You have taken pity on a cursed soul. This was a great display of compassion.");
	set_flag(48,3,1);
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_terrain(my_loc_x(),my_loc_y()) != 422)
	end();
	
if (get_memory_cell(6) != 0) {
	reset_dialog();
	add_dialog_str(0,"Siisufoss shouts from below, _I think you're trapped! You've cornered yourself into a place where you can't get good leverage without losing control of the rock! You'll have to give it a good shove and send it rolling._",0);
	add_dialog_str(1,"He leaves unsaid that you would likely roll the rock in an unpredictable direction, but you can see this immediately. If all goes well, it would simply roll down the hill in the most downward direction, back to the start.",0);
	add_dialog_str(2,"What do you do?",0);
	add_dialog_choice(0,"Proceed normally.");
	add_dialog_choice(1,"Shove the rock hard to get it away from the place where it's stuck.");
	if (run_dialog(1) == 2) {
		message_dialog("You roll the boulder out of the spot it's stuck in, and it rolls out of control all the way down the hill. Somehow, amazingly, it manages to land in the same spot where it started.","(You must start over.)");
		set_terrain(my_loc_x(),my_loc_y(),0);
		set_terrain(32,21,422);
		}
	}
	
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	if ((char_loc_x(i) == my_loc_x()) && (char_loc_y(i) == my_loc_y() + 1))
		set_state_continue(10); // moving north
	if ((char_loc_x(i) == my_loc_x() + 1) && (char_loc_y(i) == my_loc_y()))
		set_state_continue(11); // moving west
	if ((char_loc_x(i) == my_loc_x()) && (char_loc_y(i) == my_loc_y() - 1))
		set_state_continue(12); // moving south
	if ((char_loc_x(i) == my_loc_x() - 1) && (char_loc_y(i) == my_loc_y()))
		set_state_continue(13); // moving east
break; 

beginstate DISPEL_BARRIER_STATE; // state 113 
break; 

beginstate STEP_INTO_SPOT_STATE; // state 114 
break;