<commit_before>local Class = require "shared.middleclass"
local GameMath = require "shared.gamemath"
local LogicCore = require "hymn.logiccore"

local BehaviorTree = require "shared.behaviortree"
local Behavior = BehaviorTree.Behavior
local STATUS = Behavior.static.STATUS

local SearchEnemy = Class("SearchEnemy", Behavior)

function SearchEnemy:update(dt, context)
	local object = context.object

	local function isEnemy(entity)
		return entity.player ~= object.player
	end

	local entity, distance = LogicCore.entityManager:findClosestEntity(object.position, isEnemy)

	self.status = STATUS.FAILURE

	if entity then
		if distance > 40 then
			object:moveTo(entity.position.x, entity.position.y)
			self.status = STATUS.SUCCESS
		else
			object.attackTarget = entity.id
			self.status = STATUS.SUCCESS
		end
	end

	dbgprint("SearchEnemy", self.status)
	return self.status
end

local AttackEnemy = Class("AttackEnemy", Behavior)

function AttackEnemy:update(dt, context)
	local targetId = context.object.attackTarget
	local entity = LogicCore.entityManager:entity(targetId)
	self.status = STATUS.FAILURE
	if entity then
		entity:takeDamage(1)
		self.status = STATUS.RUNNING
	else
		self.attackTarget = false
	end

	dbgprint("AttackEnemy", self.status)<commit_msg>Improved attack behavior<commit_after>local Class = require "shared.middleclass"
local GameMath = require "shared.gamemath"
local LogicCore = require "hymn.logiccore"

local BehaviorTree = require "shared.behaviortree"
local Behavior = BehaviorTree.Behavior
local STATUS = Behavior.static.STATUS

local SearchEnemy = Class("SearchEnemy", Behavior)

function SearchEnemy:update(dt, context)
	local object = context.object

	local function isEnemy(entity)
		return entity.player ~= object.player
	end

	local target = object.attackTarget and LogicCore.entityManager:entity(object.attackTarget)
	local distance

	self.status = STATUS.FAILURE

	if target then
		distance = 42
	else
		target, distance = LogicCore.entityManager:findClosestEntity(object.position, isEnemy)
	end

	if target then
		if distance > 40 then
			object:moveTo(target.position.x, target.position.y)
			self.status = STATUS.SUCCESS
		else
			object.attackTarget = target.id
			self.status = STATUS.SUCCESS
		end
	end

	dbgprint("SearchEnemy", self.status)
	return self.status
end

local AttackEnemy = Class("AttackEnemy", Behavior)

function AttackEnemy:update(dt, context)
	local targetId = context.object.attackTarget
	local entity = LogicCore.entityManager:entity(targetId)
	self.status = STATUS.FAILURE
	if entity then
		entity:takeDamage(dt * 0.2)
		self.status = STATUS.RUNNING
	else
		self.attackTarget = false
	end

	dbgprint("AttackEnemy", self.status)
{'fraction_non_alphanumeric': 0.08763460824359451, 'fraction_numerical': 0.0033419977720014855, 'mean_word_length': 5.108843537414966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306406', 'n_tokens_mistral': 912, 'n_tokens_neox': 832, 'n_words': 207}