// spawner.txt
// Script by Lazarus
// Creates wargs unless an SDF is set.
// Memory Cell 0 - X Radius to place monsters within. If 0, then defaults to 4.
// Memory Cell 1 - Y Radius to place monsters within. If 0, then defaults to 4.
// Memory Cell 2 - Chance to spawn. Defaults to 1/100, as time since last spawn gets greater, so does spawn chance.
// Memory Cell 3 - Cooldown. Defaults to 0.

beginterrainscript;
variables;
short chance,cooldown,xradius,yradius,xspot,yspot,i_ran;
short lastspawn = get_current_tick();
body;

beginstate init_state;
	set_script_mode(3);
	if(get_memory_cell(0) > 0)
		xradius = get_memory_cell(0);
	else
		xradius = 4;

	if(get_memory_cell(1) > 0)
		yradius = get_memory_cell(1);
	else
		yradius = 4;

	if(get_memory_cell(2) > 0)
		chance = get_memory_cell(2);
	else
		chance = 100;

	if(get_memory_cell(3) > 0)
		cooldown = get_memory_cell(3);
	else
		cooldown = 0;

break;

beginstate start_state;
	if(get_flag(7,6) != 1)
		end();
	if(i_ran == get_current_tick())
		end();
	if(get_ran(1,0,chance) > (chance - (tick_difference(lastspawn,get_current_tick())))){
		xspot = get_ran(1,my_loc_x() - xradius,my_loc_x() + xradius);
		yspot = get_ran(1,my_loc_y() - yradius,my_loc_y() + yradius);
		place_monster(xspot,yspot,252,0);
		lastspawn = get_current_tick();
		if(get_ran(1,0,1) == 0)
			play_sound(33);
		else
			play_sound(48);
		i_ran = get_current_tick();
	}
break;

beginstate search_state;
break;
