Skip to content

Commit

Permalink
[Revscriptsys] RevNpcSys version 1.0 (lua npcs / NpcType / new npc sy…
Browse files Browse the repository at this point in the history
…stem) (#4671)

Co-authored-by: Sarah Wesker <sarahelizabetwesker@gmail.com>
  • Loading branch information
EvilHero90 and MillhioreBT committed May 30, 2024
1 parent 93f375f commit 4a89502
Show file tree
Hide file tree
Showing 32 changed files with 3,066 additions and 129 deletions.
96 changes: 78 additions & 18 deletions data/cpplinter.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
---@alias os.mtime fun(): number
---@class os
---@field mtime fun(): number
os = {}

---@alias table.create fun(arrayLength: number, keyLength: number): table
---@alias table.pack fun(...): table
---@class table
---@field create fun(arrayLength: number, keyLength: number): table
---@field pack fun(...): table
table = {}

---@class rawgetmetatable
---@field __index fun(self: table, key: any): any
Expand Down Expand Up @@ -92,6 +96,7 @@ configManager = {}
---@field createNpc fun(name: string, position: Position, extended?: boolean, force?: boolean, magicEffect?: MagicEffect_t): Npc
---@field createTile fun(position: Position): Tile
---@field createMonsterType fun(name: string): MonsterType
---@field createNpcType fun(name: string): NpcType
---@field startEvent fun(eventName: string): boolean
---@field getClientVersion fun(): string
---@field reload fun(reloadType: number): boolean
Expand Down Expand Up @@ -389,7 +394,7 @@ Creature = {}
---@field addOfflineTrainingTries fun(self: Player, skill: number, amount: number)
---@field getOfflineTrainingSkill fun(self: Player): number
---@field setOfflineTrainingSkill fun(self: Player, skill: number)
---@field getItemCount fun(self: Player, itemId: number): number
---@field getItemCount fun(self: Player, itemId: number, subType?: number, ignoreEquipped?: boolean): number
---@field getItemById fun(self: Player, itemId: number, subType?: number): Item
---@field getVocation fun(self: Player): Vocation
---@field setVocation fun(self: Player, vocationId: number)
Expand All @@ -414,7 +419,7 @@ Creature = {}
---@field setBankBalance fun(self: Player, balance: number)
---@field addItem fun(self: Player, itemId: number, count?: number): Item
---@field addItemEx fun(self: Player, item: Item, copyItem?: boolean): boolean
---@field removeItem fun(self: Player, itemId: number, count?: number, subtype?: number): boolean
---@field removeItem fun(self: Player, itemId: number, count?: number, subtype?: number, ignoreEquipped?: boolean): boolean
---@field sendSupplyUsed fun(self: Player, item: Item)
---@field getMoney fun(self: Player): number
---@field addMoney fun(self: Player, amount: number)
Expand Down Expand Up @@ -521,6 +526,62 @@ Monster = {}
---@field getSpectators fun(self: Npc, centerPos: Position, rangeX: number, rangeY: number, multifloor: boolean): table
Npc = {}

---@class NpcType
---@field create fun(): NpcType
---@field name fun(self: NpcType, name?: string): any
---@field setName fun(self: NpcType, name: string): boolean
---@field getName fun(self: NpcType): string
---@field eventType fun(self: NpcType, eventType?: string): any
---@field setEventType fun(self: NpcType, type: string): boolean
---@field getEventType fun(self: NpcType): string
---@field speechBubble fun(self: NpcType, speechBubble?: number): any
---@field setSpeechBubble fun(self: NpcType, bubble: number): boolean
---@field getSpeechBubble fun(self: NpcType): number
---@field walkInterval fun(self: NpcType, interval?: number): any
---@field setWalkInterval fun(self: NpcType, interval: number): boolean
---@field getWalkInterval fun(self: NpcType): number
---@field walkSpeed fun(self: NpcType, speed?: number): any
---@field setWalkSpeed fun(self: NpcType, speed: number): boolean
---@field getWalkSpeed fun(self: NpcType): number
---@field spawnRadius fun(self: NpcType, radius?: number): any
---@field setSpawnRadius fun(self: NpcType, radius: number): boolean
---@field getSpawnRadius fun(self: NpcType): number
---@field floorChange fun(self: NpcType, floorChange?: boolean): any
---@field setFloorChange fun(self: NpcType, floorChange: boolean): boolean
---@field getFloorChange fun(self: NpcType): boolean
---@field attackable fun(self: NpcType, attackable?: boolean): any
---@field setAttackable fun(self: NpcType, attackable: boolean): boolean
---@field getAttackable fun(self: NpcType): boolean
---@field ignoreHeight fun(self: NpcType, ignoreHeight?: boolean): any
---@field setIgnoreHeight fun(self: NpcType, ignoreHeight: boolean): boolean
---@field getIgnoreHeight fun(self: NpcType): boolean
---@field isIdle fun(self: NpcType, isIdle?: boolean): any
---@field setIsIdle fun(self: NpcType, isIdle: boolean): boolean
---@field getIsIdle fun(self: NpcType): boolean
---@field pushable fun(self: NpcType, pushable?: boolean): any
---@field setPushable fun(self: NpcType, pushable: boolean): boolean
---@field getPushable fun(self: NpcType): boolean
---@field outfit fun(self: NpcType, outfit?: Outfit|table): any
---@field setOutfit fun(self: NpcType, outfit: Outfit): boolean
---@field getOutfit fun(self: NpcType): Outfit
---@field parameters fun(self: NpcType, key?: any, value?: any): any
---@field setParameters fun(self: NpcType, key: any, value: any): boolean
---@field getParameters fun(self: NpcType, key: any): table
---@field health fun(self: NpcType, health?: number): any
---@field setHealth fun(self: NpcType, health: number): boolean
---@field getHealth fun(self: NpcType): number
---@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
NpcType = {}

---@class Guild
---@field create fun(): Guild
---@field __eq fun(self: Guild, other: Guild): boolean
Expand Down Expand Up @@ -718,19 +779,18 @@ Combat = {}
Condition = {}

---@class Outfit
---@field create fun(): Outfit
---@field lookType number
---@field lookHead number
---@field lookBody number
---@field lookLegs number
---@field lookFeet number
---@field lookAddons number
---@field lookMount number
---@field lookMountHead number
---@field lookMountBody number
---@field lookMountLegs number
---@field lookMountFeet number
---@field lookTypeEx number
---@field lookType number|nil
---@field lookHead number|nil
---@field lookBody number|nil
---@field lookLegs number|nil
---@field lookFeet number|nil
---@field lookAddons number|nil
---@field lookMount number|nil
---@field lookMountHead number|nil
---@field lookMountBody number|nil
---@field lookMountLegs number|nil
---@field lookMountFeet number|nil
---@field lookTypeEx number|nil
Outfit = {}

---@class MonsterType
Expand Down
38 changes: 36 additions & 2 deletions data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ do
rawgetmetatable("Action").__newindex = ActionNewIndex
end

do
local function NpcTypeNewIndex(self, key, value)
if key == "onSay" then
self:eventType("say")
self:onSay(value)
return
elseif key == "onDisappear" then
self:eventType("disappear")
self:onDisappear(value)
return
elseif key == "onAppear" then
self:eventType("appear")
self:onAppear(value)
return
elseif key == "onMove" then
self:eventType("move")
self:onMove(value)
return
elseif key == "onPlayerCloseChannel" then
self:eventType("closechannel")
self:onPlayerCloseChannel(value)
return
elseif key == "onPlayerEndTrade" then
self:eventType("endtrade")
self:onPlayerEndTrade(value)
return
elseif key == "onThink" then
self:eventType("think")
self:onThink(value)
return
end
rawset(self, key, value)
end
rawgetmetatable("NpcType").__newindex = NpcTypeNewIndex
end

do
local function TalkActionNewIndex(self, key, value)
if key == "onSay" then
Expand Down Expand Up @@ -588,8 +624,6 @@ function isPremium(cid) local p = Player(cid) return p and p:isPremium() or fals

STORAGEVALUE_EMPTY = -1
function Player:getStorageValue(key)
print("[Warning - " .. debug.getinfo(2).source:match("@?(.*)") .. "] Invoking Creature:getStorageValue will return nil to indicate absence in the future. Please update your scripts accordingly.")

local v = Creature.getStorageValue(self, key)
return v or STORAGEVALUE_EMPTY
end
Expand Down
12 changes: 12 additions & 0 deletions data/lib/core/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,14 @@ function Player.transferMoneyTo(self, target, amount)
local targetPlayer = Player(target.guid)
if targetPlayer then
targetPlayer:setBankBalance(targetPlayer:getBankBalance() + amount)
db.query("UPDATE `players` SET `balance` = " .. targetPlayer:getBankBalance() .. " WHERE `id` = '" .. targetPlayer:getGuid() .. "'")
else
db.query("UPDATE `players` SET `balance` = `balance` + " .. amount .. " WHERE `id` = '" .. target.guid .. "'")
end

self:setBankBalance(self:getBankBalance() - amount)
-- incase server crashes that we do not duplicate money
self:save()
return true
end

Expand Down Expand Up @@ -737,3 +740,12 @@ function Player.disableLoginMusic(self)
msg:delete()
return true
end

function Player.sendInboxItems(self, items, containerId)
local inbox = self:getInbox()
local container = Game.createItem(containerId, 1)
for _, item in pairs(items) do
container:addItem(item.item, item.count)
end
container:moveTo(inbox)
end
1 change: 1 addition & 0 deletions data/npc/lib/npc.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Including the Advanced NPC System
dofile('data/npc/lib/npcsystem/npcsystem.lua')
dofile("data/npc/lib/revnpcsys/npc.lua")

function msgcontains(message, keyword)
local message, keyword = message:lower(), keyword:lower()
Expand Down
123 changes: 123 additions & 0 deletions data/npc/lib/revnpcsys/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
--[[
>> Constants <<
Description:
- This file contains all the constants used in the NPC System.
]]

-- MESSAGE_TAGS
---@class MESSAGE_TAGS
---@type table<string, table<string, string|number|function>>
MESSAGE_TAGS = {
playerName = { tag = "|PLAYERNAME|", func = function(params) return params.playerName or "" end },
playerLevel = { tag = "|PLAYERLEVEL|", func = function(params) return params.playerLevel or "" end },
playerLevel2 = { tag = "|PLAYERLEVEL2|", func = function(params) return params.playerLevel2 or "" end },
itemCount = { tag = "|ITEMCOUNT|", func = function(params) return params.amount or "" end },
totalCost = { tag = "|TOTALCOST|", func = function(params) return params.total or "" end },
itemName = { tag = "|ITEMNAME|", func = function(params) return params.itemName or "" end },
storageKey = { tag = "|STORAGEKEY|", func = function(params) return params.storage and params.storage.key or "" end },
storageValue = { tag = "|STORAGEVALUE|", func = function(params) return params.storage and params.storage.value or "" end },
vocationName = { tag = "|VOCATIONNAME|", func = function(params) return params.vocation or "" end }
}

-- MESSAGE_LIST
---@class MESSAGE_LIST
---@type table<string, string>
MESSAGE_LIST = {
-- shop messages
needMoney = "You need more money",
needSpace = "You do not have enough capacity.",
needMoreSpace = "You do not have enough capacity for all items.",
bought = "You bought |ITEMCOUNT| |ITEMNAME|(s) for |TOTALCOST| gold.",
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.",
storageToHigh = "You do not meet the storage requirement.",
level = "You need to be exactly level |PLAYERLEVEL|.",
levelRange1 = "You need to be higher then level |PLAYERLEVEL| and below |PLAYERLEVEL2|.", -- > <
levelRange2 = "You need to be level |PLAYERLEVEL| or higher and below |PLAYERLEVEL2|.", -- >= <
levelRange3 = "You need to be higher then level |PLAYERLEVEL| and below or equal |PLAYERLEVEL2|.", -- > <=
levelRange4 = "You need to be between level |PLAYERLEVEL| and |PLAYERLEVEL2|.", -- >= <=
levelNotEqual = "You can't be exactly level |PLAYERLEVEL|.",
levelToLow = "You need to be atleast level |PLAYERLEVEL|.",
levelToHigh = "You need to be below level |PLAYERLEVEL|.",
premium = "You need to be premium to do this.",
vocation = "You need to be a |VOCATIONNAME| to do this.",
money = "You do not have enough money, it costs |TOTALCOST| gold.",
item = "You do not have atleast |ITEMCOUNT| |ITEMNAME|(s).",
infight = "You need to be in fight.",
notInfight = "You have to be out of fight.",
pzLocked = "You need to be pz locked",
notPzLocked = "You are not allowed to be pz locked"
}

-- KEYWORDS_GREET
---@type string[]
KEYWORDS_GREET = {
"hi",
"hello",
"hey",
"greetings"
}

-- MESSAGES_GREET
---@type string[]
MESSAGES_GREET = {
"Hello |PLAYERNAME| how can I help you?",
"Greetings |PLAYERNAME|, what can I do for you?",
"Hi |PLAYERNAME|, what's your desire today?"
}

-- KEYWORDS_FAREWELL
---@type string[]
KEYWORDS_FAREWELL = {
"bye",
"goodbye",
"farewell",
"cya",
"ciao"
}

-- MESSAGES_FAREWELL
---@type string[]
MESSAGES_FAREWELL = {
"Goodbye |PLAYERNAME|, have a nice day!",
"Farewell |PLAYERNAME|, see you soon!",
"See you later |PLAYERNAME|, take care!"
}

-- FOCUS
---@class FOCUS
---@type table<string, any>
FOCUS = {
-- how long the npc will focus the player in seconds
time = 60,
-- 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
greetDistance = 3
}

-- TALK
---@class TALK
---@type table<string, any>
TALK = {
-- how long in ms the npc will wait before responding
defaultDelay = 1000
}

REQUIREMENTS = {
storage = 1,
level = 2,
premium = 3,
vocation = 4,
money = 5,
item = 6,
inFight = 7,
pzLocked = 8,
removeItem = 9
}
Loading

0 comments on commit 4a89502

Please sign in to comment.