Skip to content

Commit

Permalink
Added Lua VoiceModule to NPCs (#2932)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evil Puncker authored Apr 18, 2020
1 parent 7b7d4fb commit ec0fd32
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions data/npc/lib/npcsystem/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1203,4 +1203,50 @@ if Modules == nil then
end
return true
end

VoiceModule = {
voices = nil,
voiceCount = 0,
lastVoice = 0,
timeout = nil,
chance = nil
}

-- VoiceModule: makes the NPC say/yell random lines from a table, with delay, chance and yell optional
function VoiceModule:new(voices, timeout, chance)
local obj = {}
setmetatable(obj, self)
self.__index = self

obj.voices = voices
for i = 1, #obj.voices do
local voice = obj.voices[i]
if voice.yell then
voice.yell = nil
voice.talktype = TALKTYPE_YELL
else
voice.talktype = TALKTYPE_SAY
end
end

obj.voiceCount = #voices
obj.timeout = timeout or 10
obj.chance = chance or 10
return obj
end

function VoiceModule:init(handler)
return true
end

function VoiceModule:callbackOnThink()
if self.lastVoice < os.time() then
self.lastVoice = os.time() + self.timeout
if math.random(100) <= self.chance then
local voice = self.voices[math.random(self.voiceCount)]
Npc():say(voice.text, voice.talktype)
end
end
return true
end
end
3 changes: 3 additions & 0 deletions data/npc/scripts/runes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid)
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end

local voices = { {text = "Runes, wands, rods, health and mana potions! Have a look!"} }
npcHandler:addModule(VoiceModule:new(voices))

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

Expand Down

0 comments on commit ec0fd32

Please sign in to comment.