Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lua VoiceModule to NPCs #2932

Merged
merged 5 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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