• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

NPC to Attack other NPC

bomba

Member
Joined
Feb 26, 2008
Messages
635
Reaction score
7
Location
Brazil
It's possible to make a NPC that attacks the other NPC?
Base of guard npc:

lib:
Code:
Guard = {
        config = {            
                attackspeed = 1000,
        },
        combat = {type = COMBAT_PHYSICALDAMAGE, min = 100, max = 200}
}

function Guard:new()
        local ret = {}
        setmetatable({}, {__index = self.combat})
        setmetatable(ret, {__index = self})
        return ret
end

function Guard:reset()
        self.config = Guard.config
        self.target = 0
        selfFollow(0)
        doSteerCreature(self.id, self.position)
end
      

function Guard:updateTarget()
        if self.target ~= 0 then
                return
        end
      
        local creatures = getSpectators(getThingPosition(self.id), self.range, self.range, false)
        for i = 1, #creatures do
                local target = creatures[i]
                if isCreature(target) and not isNpc(target) and getCreatureSkull(target) >= 3 then
                        if not getTilePzInfo(getThingPosition(target)) then
                                if selfFollow(target) then
                                        selfSay("I don't tolerate people like you, ".. getCreatureName(target))
                                        self.target = target
                                        self:attack()
                                        break
                                end
                        end
                else
                        self:reset()
                end
        end
end

function Guard:attack()
        if self.target == 0 then
                self:reset()
                return
        end
      
        self.time = self.time or os.clock()
        if self.time < os.clock() then
                if getDistanceBetween(getThingPosition(self.id), getThingPosition(self.target)) == 1 then
                        doTargetCombatHealth(self.id, self.target, self.combat.type, -self.combat.min, -self.combat.max, CONST_ME_DRAWBLOOD)
                end
                self.time = self.time + (self.config.attackspeed/1000)
        end
end

npc:
Code:
local guard = Guard:new()

function onCreatureAppear(cid)
        if cid == getNpcId() then
                guard.id = getNpcId()
                guard.target = 0
                guard.position = getNpcPos()
        end
end

function onCreatureDisappear(cid)
        if cid == guard.target then
                guard:reset()
        end
end

function onCreatureSay(cid, type, msg)
        return false
end

function onThink()
        guard:updateTarget()
        if guard.target ~= 0 then
                if isCreature(guard.target) then
                        guard:attack()
                else
                        guard:reset()
                end
        else
                guard:reset()
        end
end

The idea is for war of npcs in determined place...
 
npc.h:

Code:
Code:
bool isImmune(CombatType_t) const {return true;}
bool isImmune(ConditionType_t) const {return true;}

NPCs can't lose health.
 
Back
Top