// Local Spawner v1.0.0
// locspawner.txt (variant off terspawner.txt)
// by Kelandon (tomwatts@berkeley.edu)
// 
// This is a terrain script that spawns creatures if the party is nearby and an
// SDF is set. The idea is that there is a central control mechanism that the
// party should turn off in order to shut off all the local spawners.

// Most of the same caveats apply to this as apply to my Terrain Spawner. Read
// that script for more details.

// 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 terrain to change to right before spawning. If left at 0, defaults
//		to 377. NOTE: TERRAIN 377 IS NOT DEFINED IN THE DEFAULT SET. I added:
//
//		begindefineterrain 377;
//		import = 376;
//		te_icon_adjust = 130;
//
//		If you'd rather that the spawner not do this at all, set this cell to
//		600.
//	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,last_check;
short turns_between = 4;
short num_to_make = 1;
short can_spawn = 1;
short j = 0;
short turn = 0;
short my_terrain;

body;

beginstate INIT_STATE;
	set_script_mode(3);
	
	last_check = get_current_tick();
	last_abil_time = get_current_tick();
	
	my_terrain = get_terrain(my_loc_x(),my_loc_y());
	
	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);
	if (get_memory_cell(5) == 0)
		set_memory_cell(5,377);
break;

beginstate START_STATE;
	// If inactive, do nothing.
	if ((get_memory_cell(6) > 0) || (get_memory_cell(7) > 0))
		if (get_flag(get_memory_cell(6),get_memory_cell(7)) != 1)
			end();
	
	// Warn the party before spawning by changing terrain.
	if (get_memory_cell(5) != 600)
		if (tick_difference(last_abil_time,get_current_tick()) % turns_between == turns_between - 1)
			set_terrain(my_loc_x(),my_loc_y(),get_memory_cell(5));
	
	// Otherwise, spawn critters. Only do it at the beginning of the turn.
	if (tick_difference(last_check,get_current_tick()) > 0)
		{if (can_see_char(1000))
			if ((tick_difference(last_abil_time,get_current_tick()) % turns_between == 0) && (tick_difference(last_abil_time,get_current_tick()) >= turns_between) && (can_spawn > 0))
				{if (num_to_make > 1)
					print_str("Monsters appear!");
				else
					print_str("A monster appears!");
				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();
				}
		last_check = get_current_tick();
		set_terrain(my_loc_x(),my_loc_y(),my_terrain);
		}	
break;

beginstate SEARCH_STATE;
break;

beginstate SANCTIFICATION_STATE;
	print_str("Nothing happens. This location is not cursed.");
break;