Skip to content

Commit

Permalink
[Revnpcsys] Revisioned Npc System v1.3 (otland#4747)
Browse files Browse the repository at this point in the history
- Added `onSight` event, this has to be enabled with `NpcType:sight(x, y)` unless that is not set the event wont fire. This event triggers whenever a creature (monsters/npcs/players) steps into the sight range, will only re trigger once you walk out of idle range and back in again.
- fixed event callbacks, they're now moved into `NpcCallbacks` they can be set like: `NpcType:callbacks().onSay/onSight/etc.`
- fixed a crash which could occur if you had a npc disabled then enabled him and /reload npcs, this is now fixed
- fixed a crash that if a xml npc is invalid on server startup that it throws an error instead of crashing
- made all the functions in the lib overloadable, only the table data is protected, you can now change functions and they will be correctly changed on /reload npcs
- changed everything related to focus from seconds into ms
- changed focus so it works for all creatures not only players
- changed NpcTalkQueue it now works for creatures not only for players
- changed the onCreatureSay event, it now triggers for all creatures not only for players
- selfSay has a new parameter now called talkType which is optional and falls back to TALKTYPE_SAY
- added `onSpeechBubble` event, with this we can set the speech bubble for each player individual.
example:
Npc has a quest for the player he only shows for this player the quest speech bubble, once the quest is finished the npc wont have a quest speech bubble anymore

---------

Co-authored-by: Evil Puncker <EPuncker@users.noreply.github.com>
Co-authored-by: KrecikOnDexin <krecikondexin@gmail.com>
  • Loading branch information
3 people authored Jun 13, 2024
1 parent 00f4400 commit a490b34
Show file tree
Hide file tree
Showing 19 changed files with 1,774 additions and 1,573 deletions.
28 changes: 13 additions & 15 deletions data/cpplinter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,17 @@ Npc = {}
---@field maxHealth fun(self: NpcType, maxHealth?: number): any
---@field setMaxHealth fun(self: NpcType, maxHealth: number): boolean
---@field getMaxHealth fun(self: NpcType): number
---@field onSay fun(self: NpcType, callback: function): boolean
---@field onDisappear fun(self: NpcType, callback: function): boolean
---@field onAppear fun(self: NpcType, callback: function): boolean
---@field onMove fun(self: NpcType, callback: function): boolean
---@field onPlayerCloseChannel fun(self: NpcType, callback: function): boolean
---@field onPlayerEndTrade fun(self: NpcType, callback: function): boolean
---@field onThink fun(self: NpcType, callback: function): boolean
---@field onSayCallback fun(self: NpcType, callback: function): boolean
---@field onDisappearCallback fun(self: NpcType, callback: function): boolean
---@field onAppearCallback fun(self: NpcType, callback: function): boolean
---@field onMoveCallback fun(self: NpcType, callback: function): boolean
---@field onPlayerCloseChannelCallback fun(self: NpcType, callback: function): boolean
---@field onPlayerEndTradeCallback fun(self: NpcType, callback: function): boolean
---@field onThinkCallback fun(self: NpcType, callback: function): boolean
---@field sight fun(self: NpcType, x?: number, y?: number): number?, number?
---@field setSight fun(self: NpcType, x: number, y: number)
---@field getSight fun(self: NpcType): number, number
---@field onSay fun(self: NpcType, callback: function)
---@field onDisappear fun(self: NpcType, callback: function)
---@field onAppear fun(self: NpcType, callback: function)
---@field onMove fun(self: NpcType, callback: function)
---@field onPlayerCloseChannel fun(self: NpcType, callback: function)
---@field onPlayerEndTrade fun(self: NpcType, callback: function)
---@field onThink fun(self: NpcType, callback: function)
---@field onSight fun(self: NpcType, callback: function)
NpcType = {}

---@class Guild
Expand Down Expand Up @@ -1312,7 +1309,8 @@ function isScriptsInterface() end
---@alias isScriptsInterface fun(): boolean

function getNpcCid() end
function selfSay(message, player) end
function selfSay(message, player, talkType) end
---@alias selfSay fun(message: string, player?: number, talkType?: number)
function selfMove(direction) end
function selfMoveTo(...) end
function selfTurn(direction) end
Expand Down
8 changes: 8 additions & 0 deletions data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ do
self:eventType("think")
self:onThink(value)
return
elseif key == "onSight" then
self:eventType("sight")
self:onSight(value)
return
elseif key == "onSpeechBubble" then
self:eventType("speechbubble")
self:onSpeechBubble(value)
return
end
rawset(self, key, value)
end
Expand Down
50 changes: 50 additions & 0 deletions data/npc/lib/revnpcsys/callbacks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--[[
>> NpcCallbacks <<
Description:
- It is used to store the callbacks for a specific NPC, and to check if a callback exists for a specific NPC.
Functions:
- NpcCallbacks:hasCallback(callback)
- NpcCallbacks:onSay(npc, creature, messageType, message)
- NpcCallbacks:onThink(npc)
- NpcCallbacks:onMove(npc, oldPos, newPos)
- NpcCallbacks:onAppear(npc, creature)
- NpcCallbacks:onDisappear(npc, creature)
- NpcCallbacks:onSight(npc, creature)
- NpcCallbacks:onPlayerCloseChannel(npc, creature)
- NpcCallbacks:onPlayerEndTrade(npc, creature)
]]

---@class NpcCallbacks
---@field onSay fun(npc: Npc, creature: Creature, messageType: number, message: string)
---@field onThink fun(npc: Npc)
---@field onMove fun(npc: Npc, oldPos: Position, newPos: Position)
---@field onAppear fun(npc: Npc, creature: Creature)
---@field onDisappear fun(npc: Npc, creature: Creature)
---@field onSight fun(npc: Npc, creature: Creature)
---@field onPlayerCloseChannel fun(npc: Npc, creature: Creature)
---@field onPlayerEndTrade fun(npc: Npc, creature: Creature)
---@field hasCallback fun(callback: string): boolean

if not NpcCallbacks then
NpcCallbacks = {}

setmetatable(NpcCallbacks, {
__call = function(self, npc)
if not self[npc:getName()] then
self[npc:getName()] = {}
end
setmetatable(self[npc:getName()], {__index = NpcCallbacks})
-- The NpcCallbacks is returned
return self[npc:getName()]
end
})

end

---@param callback string The callback to check for.
---@return boolean True if the callback exists, false otherwise.
function NpcCallbacks:hasCallback(callback)
return self[callback] ~= nil
end
5 changes: 2 additions & 3 deletions data/npc/lib/revnpcsys/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ MESSAGE_LIST = {
sold = "You sold |ITEMCOUNT| |ITEMNAME|(s) for |TOTALCOST| gold.",
-- requirement cancel messages
storage = "You do not meet the storage requirement.",
-- bla
storageRange = "You do not meet the storage requirement.",
storageNotEqual = "You do not meet the storage requirement.",
storageToLow = "You do not meet the storage requirement.",
Expand Down Expand Up @@ -94,8 +93,8 @@ MESSAGES_FAREWELL = {
---@class FOCUS
---@type table<string, any>
FOCUS = {
-- how long the npc will focus the player in seconds
time = 60,
-- how long the npc will focus the player in ms
time = 60000,
-- how far the player can step away until the npc loses focus
distance = 5,
-- how near the player has to be to greet the npc
Expand Down
Loading

0 comments on commit a490b34

Please sign in to comment.