Meanwhile, I made some good progress and guess what?... It WORKS!! ... Almost.
Code: Select all
{
class = "Monster",
meshName = "undead_mesh",
footstepSound = "skeleton_footstep",
hitSound = "skeleton_hit",
dieSound = "undead_die",
hitEffect = "hit_dust",
capsuleHeight = 0.7,
capsuleRadius = 0.25,
collisionRadius = 0.6,
health = 125,
immunities = { "sleep" },
resistances = { ["poison"] = "immune" },
traits = { "undead" },
evasion = 0,
exp = 0,
onInit = function(self)
self:performAction("rise")
end,
onPerformAction = function(self,basicAttack)
playSound("spell_fizzle") -- used to check how often onPerformAction is called
local bx,by = getForward(bodyguard.facing)
for each in party.map:entitiesAt(bodyguard.x+bx,bodyguard.y+by) do
if each:getComponent("monster") ~= nil and each.elevation == bodyguard.elevation then
local sound = each.name
local strike = math.random(-100,100)
if strike >= each.monster:getEvasion() then
damageTile(bodyguard.level,bodyguard.x+bx,bodyguard.y+by,bodyguard.facing,bodyguard.elevation,0,"physical",19)
playSoundAt(sound.."_hit",bodyguard.level,bodyguard.x+bx,bodyguard.y+by)
end
end
end
end,
headRotation = vec(0, 0, 90),
},
{
class = "Brain",
sight = 5,
morale = 100,
onThink = function(self)
local danger = false
local bx,by = getForward(bodyguard.facing)
--checks if the bodyguard is in front of a foe
for each in party.map:entitiesAt(bodyguard.x+bx,bodyguard.y+by) do
if each:getComponent("monster") ~= nil and each.elevation == bodyguard.elevation then
danger = true
self:performAction("basicAttack")
self:wait()
else danger = false
end
end
local h = bodyguard.x - 3
local v = bodyguard.y - 3
local hsup = bodyguard.x + 3
local vsup = bodyguard.y + 3
--checks if there is a foe nearby and moves toward it
for x = h,hsup do
for y = v,vsup do
for each in party.map:entitiesAt(x,y) do
if each:getComponent("monster") ~= nil and each.name~="bodyguard" then
danger = true
local target = each.id
self:goTo(target)
--local d = each.facing + 2 -- this part doesn't work well
--self:turnTowardsDirection(d)
end
end
end
end
--if there is no foe, the bodyguard comes closer to the party
if self.partyOnLevel and danger == false then
self:moveTowardsParty()
end
end,
Now, I do have a companion who checks the presence of ennemies and attack them or just come closer to the party if the area is safe. Still two minor and one major issues :
- I don't know why, the onPerformAction() is called quite often, even if there is no foe at reach.
- I can't find a way to make the Bodyguard turn towards the direction of the target, unless it is just next to it. I tried with turnTowardsDirection() (and would have to script all the possibilities to get right) but I hope there is an easier way. Any help would be warmly welcomed
- And most importantly, as presumed by Zimberzimber, the other monsters simply ignore my bodyguard. I tried by adding a "party" Component to the bodyguard, but then the game crashes. I'm still sratching my head to find a way to go through it . Or I will have to find a way to implement that in my mod in a realistic way (an invisible bodyguard??? Only visible to its Master???).
K.