
AI_TACTICS_TEMPLATES = {
	idle = {
		min_delay = 8,
		max_delay = 16,
		list = {
			"walk_randomly",
			"walk_randomly",
			"walk_home",
			"idle",	
		}
	},
	target_engage = {
		min_delay = 0.5,
		max_delay = 5,
		list = {
			"get_in_range",
			"close_distance",
			"strafe_ranged",
		}
	},
	target_careful = {
		min_delay = 2,
		max_delay = 8,
		list = {
			"get_in_range",			
			"strafe_ranged",
		}
	},
	target_scared = {
		min_delay = 2,
		max_delay = 4,
		list = {
			"close_distance",
			"flee",
			"flee",
		}
	},
	target_random = {
		min_delay = 2,
		max_delay = 8,
		list = {
			"follow_offset_angle",
			"keep_distance",
			"get_in_range",
			"close_distance",
			"strafe_dodge",
			"scatter_randomly",
		}
	},
}

AI_TACTICS = {
	follow_offset_angle = {
		require_target = true,
		set = function(self)
			self.follow_angle_offset = math.randomGaussian()*math.pi*0.1 + ((math.random() > 0.5) and 1 or -1)*math.pi*0.25
		end,
		step = function(self, target, dt)
			local ang = math.angleBetweenPoints(self.x, self.y, target.x, target.y)			
			self:set_desired_vector(ang+ self.follow_angle_offset, 1)
			self:set_desired_flip(target.x < self.x)
		end,
		remove = function(self)
			self.follow_angle_offset = nil
		end,
	},
	keep_distance = {
		require_target = true,
		set = function(self, target)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			self.keep_distance = dist*0.9
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			if dist < self.keep_distance then
				local ang = math.angleBetweenPoints(target.x, target.y, self.x, self.y)
				self:set_desired_vector(ang, 1)	
				self:set_desired_flip(target.x < self.x)
			end
		end,
		remove = function(self)
			self.keep_distance = nil
		end,
	},
	flee = {
		require_target = true,
		set = function(self, target)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			self.keep_distance = dist*1.5
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			if dist < self.keep_distance then
				local ang = math.angleBetweenPoints(target.x, target.y, self.x, self.y)
				self:set_desired_vector(ang, 1)	
				self:set_desired_flip(target.x < self.x)
			end
		end,
		remove = function(self)
			self.keep_distance = nil
		end,
	},
	get_in_range = {
		require_target = true,
		set = function(self, target)
			local best = self:get_best_attack_range()
			local worst = self:get_worst_attack_range()
			self.max_distance = best
			self.min_distance = worst			
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			if dist < self.min_distance then
				local ang = math.angleBetweenPoints(target.x, target.y, self.x, self.y)
				self:set_desired_vector(ang, 1)
				self:set_desired_flip(target.x < self.x)
			elseif dist > self.max_distance then
				local ang = math.angleBetweenPoints(self.x, self.y, target.x, target.y)
				self:set_desired_vector(ang, 1)
				self:set_desired_flip(target.x < self.x)
			end
		end,
		remove = function(self)
			self.keep_distance = nil
		end,
	},
	close_distance = {
		require_target = true,
		set = function(self, target)			
			self.get_to_distance = self:get_worst_attack_range()
			self.get_to_delay = 0
		end,
		step = function(self, target, dt)
			if self.get_to_delay > 0 then
				self.get_to_delay = self.get_to_delay -dt
			else
				local dist = math.distance(self.x, self.y, target.x, target.y)
				if dist > self.get_to_distance then
					local ang = math.angleBetweenPoints(self.x, self.y, target.x, target.y)
					self:set_desired_vector(ang, 1)
					self:set_desired_flip(target.x < self.x)		
				else
					self.get_to_delay = 2
				end
			end
		end,
		remove = function(self)
			self.get_to_distance = nil
			self.get_to_delay = nil
		end,
	},
	strafe_ranged = {
		require_target = true,
		set = function(self, target)			
			self.strafe_distance = self:get_best_attack_range()
			self.strage_angle_offset = (math.random() > 0.5) and math.pi*0.4 or -math.pi*0.4
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			if dist < self.strafe_distance then
				local ang = math.angleBetweenPoints(self.x, self.y, target.x, target.y)
				self:set_desired_vector(ang+ self.strage_angle_offset, 1)		
				self:set_desired_flip(target.x < self.x)
			end
		end,
		remove = function(self)
			self.strafe_distance = nil
			self.strage_angle_offset = nil
		end,	
	},
	strafe_dodge = {
		require_target = true,
		set = function(self, target)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			self.strafe_distance = dist*0.9
			self.strage_angle_offset = (math.random() > 0.5) and math.pi*0.5 or -math.pi*0.5
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, target.x, target.y)
			if dist < self.strafe_distance then
				local ang = math.angleBetweenPoints(self.x, self.y, target.x, target.y)
				self:set_desired_vector(ang+ self.strage_angle_offset, 1)		
				self:set_desired_flip(target.x < self.x)
			end
		end,
		remove = function(self)
			self.strafe_distance = nil
			self.strage_angle_offset = nil
		end,	
	},
	scatter_randomly = {
		require_target = true,
		set = function(self, target)
			self.scatter_delay = 1+ math.random()
			self.scatter_angle = math.randomAngle()
		end,
		step = function(self, target, dt)
			self.scatter_delay = self.scatter_delay - dt
			if self.scatter_delay <= 1 then				
				self:set_desired_vector(self.scatter_angle, 1)	
				self:set_desired_flip(math.cos(self.scatter_angle) < 0)
			end
			if self.scatter_delay <= 0 then
				self.scatter_delay = 1+math.random()
				self.scatter_angle = math.randomAngle()
			end			
		end,
		remove = function(self)
			self.scatter_delay = nil
			self.scatter_angle = nil
		end,
	},
	walk_randomly = {		
		set = function(self, target)
			self.idle_walk_delay = 4+ math.random()*2
			self.idle_walk_angle = math.randomAngle()
		end,
		step = function(self, target, dt)
			self.idle_walk_delay = self.idle_walk_delay - dt
			if self.idle_walk_delay <= 1 then				
				self:set_desired_vector(self.idle_walk_angle, 0.25)	
				self:set_desired_flip(math.cos(self.idle_walk_angle) < 0)
			end
			if self.idle_walk_delay <= 0 then
				self.idle_walk_delay = 1+ math.random()*6
				self.idle_walk_angle = math.randomAngle()
			end
		end,
		remove = function(self)
			self.idle_walk_delay = nil
			self.idle_walk_angle = nil
		end,
	},
	walk_home = {		
		set = function(self, target)			
		end,
		step = function(self, target, dt)
			local dist = math.distance(self.x, self.y, self.home_x, self.home_y)
			if dist > self.home_radius then
				local angle = math.angleBetweenPoints(self.x, self.y, self.home_x, self.home_y)
				self:set_desired_vector(angle, 0.5)
				self:set_desired_flip(self.home_x < self.x)
			end		
		end,
		remove = function(self)
			self.get_to_distance = nil
			self.get_to_delay = nil
		end,
	},
	idle = {		
		set = function(self, target)			
		end,
		step = function(self, target, dt)
		end,
		remove = function(self)			
		end,
	},
}