// Terrain Spawner v1.0.0
// terspawner.txt (significant modification on spawner.txt)
// by Kelandon (tomwatts@berkeley.edu)
// 
// This is a terrain script that spawns creatures until the party uses the 
// ability Ritual of Sanctification near it. When that happens, it explodes,  
// calls a town script, and does a few other fun things. Look at the
// SANCTIFICATION_STATE for more details.

// A party can only get Ritual of Sanctification if someone in the scenario
// teaches to them. Knowledge of the Ritual is taken away when the party
// leaves the scenario.

// The creatures spawned by this altar will hunt the party down. This feature
// may malfunction if there are other summoned creatures in the town, however.

// The text in this script is set to an altar that spawns undead. Change it as
// necessary.

// Memory Cells

//	0 - The creature type it spits out.
//	1 - How many of it it spits out. Defaults to 1 if left at 0.
//	2 - How many turns elapse between each spawn. Defaults to 4 if left at 0.
//	3,4 - What direction to spawn in. Cell 3 is number of steps to the south,
//		and cell 4 is number of steps to the east.
//	5 - What town script to run when spawner is deactivated. Usually this will
//		be to give some sort of message in the form of a message_dialog. If left
//		at 0, none.
//	6,7 - The SDF to activate the spawner. If both 0, none. Spawner will 
//	only spawn if this flag is set to 1.

beginterrainscript; 

variables;

short i,target;
short last_abil_time;
short turns_between = 4;
short num_to_make = 1;
short can_spawn = 1;
short j = 0;
short turn = 0;

body;

beginstate INIT_STATE;
	if (get_flag(get_memory_cell(6),get_memory_cell(7)) > 1)
		set_terrain(my_loc_x(),my_loc_y(),137);
	
	set_script_mode(3);

	last_abil_time = get_current_tick();
	
	if (get_memory_cell(1) > 0)
		num_to_make = get_memory_cell(1);
	if (get_memory_cell(2) > 0)
		turns_between = get_memory_cell(2);
	
break;

beginstate START_STATE;
	if ((get_memory_cell(6) > 0) || (get_memory_cell(7) > 0))
		{if (get_flag(get_memory_cell(6),get_memory_cell(7)) != 1)
			end();
		}
		
	// Spawn critters
		if ((tick_difference(last_abil_time,get_current_tick()) >= turns_between) && (can_spawn > 0)) {
			if (can_see_char(1000))
				{print_str("The altar raises undead!");
				play_sound(-97); }
			i = 0;
			while (i < num_to_make) {
				place_monster(my_loc_x() + get_memory_cell(3),my_loc_y() + get_memory_cell(4),get_memory_cell(0),1);
				force_instant_terrain_redraw();
				alert_char(86 + j);
				j = j + 1;
				i = i + 1;
				}
			last_abil_time = get_current_tick();
			end();
			}	
			
	end();
break;

beginstate SEARCH_STATE;
break;

beginstate SANCTIFICATION_STATE;
	if (get_flag(get_memory_cell(6),get_memory_cell(7)) > 1)
		end();

	if (get_memory_cell(5) != 0)
		run_town_script(get_memory_cell(5));

	put_boom_on_space(my_loc_x(),my_loc_y(),1,0);
	run_animation_sound(152);
	pause(5);

	set_terrain(my_loc_x(),my_loc_y(),137);
	put_boom_on_space(my_loc_x(),my_loc_y(),1,0);
	run_animation_sound(152);
	if ((get_memory_cell(6) > 0) || (get_memory_cell(7) > 0))
		set_flag(get_memory_cell(6),get_memory_cell(7),2);
	pause(5);

	message_dialog("The altar crumbles to pieces, and it will not summon any more monsters.","");
	award_party_xp(50,get_level(0) + 10);
break;