/*
creates the lists of entities for each wave. 
these are used when spawning in new groups.
a new list is created each time a new wave begins.
*/
function make_spawn_list() {
	var list = [];
	var type = ["hunter"];
	var total = 0;
	var goal = 16;
	while (total < goal) {
		var group_count = randomIntIn(4, 6);
		var type = pick(list);
		for (var i = 0; i < group_count; i ++)
			list.push(type);
		total += group_count;
	}
	return list;
}


// old spawn list function
function setup_spawn_list_old() {
	// set up the spawn list
	enemyGroupSpawnFrequency = 32;
	
	desiredEnemyCount = 8 + 2 * wave;
	desiredEnemyProjectileCount = 3 + 1 * (wave - 3);

	if (wave == 2) {
		// make a number of randomly sized groups 
		// containing a fixed total number of monsters
		var totalHunters = 16;
		while (totalHunters > 0) {
			var newGroup = [];
			var hunterCount = randomIntIn(4, 6);
			for (var i2 = 0; i2 < hunterCount; i2 ++) {
				if (totalHunters > 0) {
						newGroup.push("hunter");
					totalHunters --;
				}
			}
			enemyList.push(newGroup);
		}
	}

	// introduce projectile enemies
	else if (wave == 3) {
		// make a number of randomly sized groups 
		// containing a fixed total number of monsters
		var totalHunters = 16;
		var totalShy = 3;
		while ((totalHunters > 0) || (totalShy > 0)) {
			var newGroup = [];
			if ((enemyList.length > 1)
			&& (Math.random() > 0.5)) {
				var monsterCount = randomIntIn(4, 8);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalHunters > 0) {
						newGroup.push("hunter");
						totalHunters --;
					}
				}
			}
			else {
				var monsterCount = 1; //randomIntIn(1, 2);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalShy > 0) {
						newGroup.push("shy");
						totalShy --;
					}
				}
			}
			enemyList.push(newGroup);
		}
		console.log(enemyList);
	}

	// default
	else {
		// make a number of randomly sized groups 
		// containing a fixed total number of monsters
		var totalHunters = 16;
		var totalShy = 6;
		var totalMothers = 6;
		var totalSeekers = 4;
		var totalTurrets = 4;
		while ((totalHunters > 0) || (totalShy > 0) || (totalMothers > 0) || (totalSeekers > 0) || (totalTurrets > 0)) {
			var newGroup = [];
			var pick = randomInt(5);
			if ((enemyList.length > 0) 
			&& (pick == 0)) {
				var monsterCount = randomIntIn(4, 8);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalHunters > 0) {
						newGroup.push("hunter");
						totalHunters --;
					}
				}
			}
			else if ((enemyList.length > 0)
			&& (pick == 1)) {
				var monsterCount = 1; //randomIntIn(1, 2);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalShy > 0) {
						newGroup.push("shy");
						totalShy --;
					}
				}
			}
			else if ((enemyList.length > 0)
			&& (pick == 2)) {
				var monsterCount = randomIntIn(1, 4);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalSeekers > 0) {
						newGroup.push("seeker");
						totalSeekers --;
					}
				}
			}
			else if ((enemyList.length > 0)
			&& (pick == 3)) {
				var monsterCount = randomIntIn(1, 2);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalTurrets > 0) {
						newGroup.push("turret");
						totalTurrets --;
					}
				}
			}
			else {
				var monsterCount = 1; //randomIntIn(1, 2);
				for (var i2 = 0; i2 < monsterCount; i2 ++) {
					if (totalMothers > 0) {
						newGroup.push("STARFISH");
						totalMothers --;
					}
				}
			}
			enemyList.push(newGroup);
		}
		console.log(enemyList);
	}	
	
	enemyGroupSpawnTimer = 1;
	sowerTimer = 256;
	makingSowers = false;
	makingChewchews = false;
	sowerCount = 0;
}


function setup_spawn_list() {
	// set up the spawn list
	enemyGroupSpawnFrequency = 32;
	
	desiredEnemyCount = 8 + 2 * wave;
	desiredEnemyProjectileCount = 3 + 1 * (wave - 3);

	// old shape
	/*

	wave 2
	16 hunters

	wave 3
	16 hunters
	3 shy

	// default
	hunters = 16;
	shy = 6;
	mothers = 6;
	seekers = 4;
	turrets = 4;
	*/
	
	// special case: 4 crystals at top

	if (wave == 2) {
		crystalsEarned = 3;
		crystalSpawnTimer = 0;
	}
		
	console.log("entering wave " + wave);

	var t = Math.min(wave - 2, 6) / 6;

	function blend(low, high, t) {
		return Math.floor(low + (high - low) * t);
	}

	var total = [
		{type: "hunter", 	total: blend(16, 24, t), 	current: 0, min: blend(2, 8, t), max: blend(4, 12, t)}
	];
	// possibly swap in diggers later

	if (wave > 1) {
		total.push({type: "shy",		total: blend(2, 4, t), 	current: 0, min: 1, max: 1});
	}

	if (wave > 2) {
		total.push({type: "eater",		total: blend(2, 6, t), 	current: 0, min: 1, max: blend(1, 2, t)});
	}

	if (wave > 6) {
		total.push({type: "shuttle with turret",		total: 6, 	current: 0, min: 1, max: 1});
	}
	if (wave > 4) {
		total.push({type: "seeker",	total: blend(4, 12, t), 	current: 0, min: 1, max: blend(2, 4, t)});
	}
	if (wave > 3) {
		if (((wave % 2) == 0)
		|| (wave < 8))
			total.push({type: "STARFISH",	total: 4, 	current: 0, min: 1, max: 1});
		else
			total.push({type: "hen",	total: 4, 	current: 0, min: 1, max: 1});
		// {type: "hen",		total: 6, 	current: 0, min: 1, max: 1},
		// {type: "turret", 	total: 4, 	current: 0, min: 1, max: 2},
	}

	// by default everything ramps upward with the exception of hens + starfish, which alternate after 4

	// some levels, however, have specific hard coded compositions
	// first iron castle wave
	if (wave == 6) {
		remove_type("STARFISH", total);
	}

	// see if we have types which we still want to make more of
	// pick one type from remaining types
	// add a group of that type
	var adding = true;
	while (adding) {
		// see if we have any types we should remove
		adding = false;
		var candidate = [];
		for (var property in total) {
			if (total[property].current >= total[property].total) {
				// this one is done, remove it
			}
			else {
				adding = true;
				candidate.push(total[property]);
			}
			//property_count ++;
		}
		// pick a type
		//var selection = pick(candidate);
		if (candidate.length > 0) {
			var selection = candidate[Math.floor(Math.random() * candidate.length)];
			// get group count
			// same for every type right now
			var monsterCount = randomIntIn(selection.min, selection.max);
			var newGroup = [];
			for (var i = 0; i < monsterCount; i ++) {
				if (selection.total > selection.current) {
					newGroup.push(selection.type);
					selection.current ++;
				}
			}
			enemyList.push(newGroup);
		}
	}
	console.log(enemyList);
	
	enemyGroupSpawnTimer = 1;
	sowerTimer = 256;
	makingSowers = false;
	makingChewchews = false;
	sowerCount = 0;
}


// removes a type from a spawn list
function remove_type(name, total) {
	for (var i = 0; i < total.length; i ++) {
		if (total[i].type == name)
			total.splice(i, 1);
	}
}

// logging functions
var wave_history = [];

function initialize_wave_history() {
	wave_history = [];
}


function notify_begin_wave() {
	var new_wave_data = new wave_data();
	new_wave_data.start_time = clock;
	wave_history.push(new_wave_data);
}


function notify_end_wave() {
	if (wave_history.length > 0) {
		var current = wave_history[wave_history.length - 1];
		current.duration = clock - current.start_time; // totaly play time
		// player deaths
		current.lives_lost = current.initial_lives - lives; // this won't take into account extra lives yet

		// log other things-
		// total projectiles
		// total characters defeated
		// characters birthed (not from the spawn list)
	}
}


function wave_data() {
	this.start_time = clock;
	this.initial_lives = lives;
	this.duration = 0;
	this.lives_lost = 0;
}