• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua NPC that attack skulleds

brunolopes

New Member
Joined
Nov 8, 2009
Messages
49
Reaction score
1
Hello,

I am having problem with a script I made.

Lua:
local config = {
	maxDistance = 20,
	followTimeout = 10,
	range = 5,
	damage = {min = 500, max = 1000},
	messages = {
		onSpot = "We do not tolerate people like you, %s",
		onFlee = "I will catch you next time!",
		onTimeout = "You got me this time, but just wait, %s!",
		onClean = "Now behave in the future"
	}
}

local behavior = {target = {now = 0, last = 0}, position = 0, attack = 0}

function resetNpcBehavior()
	behavior.target.now = 0
	behavior.attack = 0
	selfFollow(0)
	doTeleportThing(getNpcId(), behavior.position)
end

function updateNpcBehavior()
	if not isCreature(behavior.target.now) then
		resetNpcBehavior()
	elseif getCreatureSkull(behavior.target.now) < 3 then
		selfSay(config.messages.onClean)
		resetNpcBehavior()
	end
	
	if behavior.target.now == 0 then
		local creatures = getSpectators(getNpcPos(), config.range, config.range, false)
		for i = 1, #creatures do
			local target = creatures[i]
			if target ~= 0 then
				if isCreature(target) and getCreatureSkull(target) >= 3 then
					if not getTilePzInfo(getCreaturePosition(target)) then
						if selfFollow(target) then
							behavior.target.now = target
							if target ~= behavior.target.last then
								selfSay(config.messages.onSpot:format(getCreatureName(target)))
							end
							behavior.target.last = target
						end
					end
				end
			end
		end
	end
end

function onCreatureAppear(cid)
	if cid == getNpcId() then
		behavior[getNpcId()] = {target = {now = 0, last = 0}, attack = 0, position = 0}
		behavior.position = getNpcPos()
	end
end

function onCreatureDisappear(cid)
	if(cid == target) then
		resetNpcBehavior()
	end
end

function onThink()
	updateNpcBehavior()

	local target = behavior.target.now
	
	if target == 0 then
		return true
	end

	if getCreaturePosition(target).z ~= getNpcPos().z then
		resetNpcBehavior()
		return true
	end

	if getNpcDistanceTo(target) > config.maxDistance then
		selfSay(config.messages.onFlee)
		resetNpcBehavior()
		return true
	end

	if behavior.attack == 0 then
		behavior.attack = os.clock()
	end

	if os.clock() - behavior.attack > config.followTimeout then
		selfSay(config.messages.onTimeout:format(getCreatureName(target)))
		resetNpcBehavior()
		return true
	end

	if getNpcDistanceTo(target) <= 1 then
		doTargetCombatHealth(getNpcCid(), target, COMBAT_LIFEDRAIN, -config.damage.min, -config.damage.max, CONST_ME_BLOCKHIT)
		behavior.attack = os.clock()
	end
end

The problem is simple: I summon two or more NPCs, if one spots a skulled creature the other two teleports to the one that spotted the creature.
 
Back
Top Bottom