function doPlayerGiveItem(cid, itemid, amount, subType)
local item = 0
if(isItemStackable(itemid) == TRUE) then
item = doCreateItemEx(itemid, amount)
if(doPlayerAddItemEx(cid, item) ~= RETURNVALUE_NOERROR) then
return LUA_ERROR
end
else
for i = 1, amount do
item = doCreateItemEx(itemid, subType)
if(doPlayerAddItemEx(cid, item) ~= RETURNVALUE_NOERROR) then
return LUA_ERROR
end
end
end
return LUA_NO_ERROR
end
function doPlayerTakeItem(cid, itemid, amount)
if(getPlayerItemCount(cid, itemid) >= amount) then
if(doPlayerRemoveItem(cid, itemid, amount) == TRUE) then
return LUA_NO_ERROR
end
end
return LUA_ERROR
end
function doPlayerBuyItem(cid, itemid, count, cost, charges)
if(doPlayerRemoveMoney(cid, cost) == TRUE) then
return doPlayerGiveItem(cid, itemid, count, charges)
end
return LUA_ERROR
end
function doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)
if(doPlayerRemoveMoney(cid, cost) == TRUE) then
for i = 1, count do
local container = doCreateItemEx(containerid, 1)
for x = 1, getContainerCapById(containerid) do
doAddContainerItem(container, itemid, charges)
end
doPlayerAddItemEx(cid, container)
end
return LUA_NO_ERROR
end
return LUA_ERROR
end
function doPlayerSellItem(cid, itemid, count, cost)
if(doPlayerTakeItem(cid, itemid, count) == LUA_NO_ERROR) then
if(doPlayerAddMoney(cid, cost) ~= TRUE) then
error('Could not add money to ' .. getPlayerName(cid) .. ' (' .. cost .. 'gp)')
end
return LUA_NO_ERROR
end
return LUA_ERROR
end
function isInRange(pos, fromPos, toPos)
return (pos.x >= fromPos.x and pos.y >= fromPos.y and pos.z >= fromPos.z and pos.x <= toPos.x and pos.y <= toPos.y and pos.z <= toPos.z) and TRUE or FALSE
end
function isPremium(cid)
return (isPlayer(cid) == TRUE and (getPlayerPremiumDays(cid) > 0 or getConfigInfo('freePremium') == "yes")) and TRUE or FALSE
end
function getMonthDayEnding(day)
if day == "01" or day == "21" or day == "31" then
return "st"
elseif day == "02" or day == "22" then
return "nd"
elseif day == "03" or day == "23" then
return "rd"
else
return "th"
end
end
function getMonthString(m)
return os.date("%B", os.time{year = 1970, month = m, day = 1})
end
function getArticle(str)
return str:find("[AaEeIiOoUuYy]") == 1 and "an" or "a"
end
function isNumber(str)
return tonumber(str) ~= nil and TRUE or FALSE
end
function getDistanceBetween(firstPosition, secondPosition)
local xDif = math.abs(firstPosition.x - secondPosition.x)
local yDif = math.abs(firstPosition.y - secondPosition.y)
local posDif = math.max(xDif, yDif)
if(firstPosition.z ~= secondPosition.z) then
posDif = posDif + 9 + 6
end
return posDif
end
function doPlayerAddAddons(cid, addon)
for i = 0, table.maxn(maleOutfits) do
doPlayerAddOutfit(cid, maleOutfits[i], addon)
end
for i = 0, table.maxn(femaleOutfits) do
doPlayerAddOutfit(cid, femaleOutfits[i], addon)
end
end
function isSorcerer(cid)
if(isPlayer(cid) == FALSE) then
debugPrint("isSorcerer: Player not found.")
return false
end
return (isInArray({1,5}, getPlayerVocation(cid)) == TRUE)
end
function isDruid(cid)
if(isPlayer(cid) == FALSE) then
debugPrint("isDruid: Player not found.")
return false
end
return (isInArray({2,6}, getPlayerVocation(cid)) == TRUE)
end
function isPaladin(cid)
if(isPlayer(cid) == FALSE) then
debugPrint("isPaladin: Player not found.")
return false
end
return (isInArray({3,7}, getPlayerVocation(cid)) == TRUE)
end
function isKnight(cid)
if(isPlayer(cid) == FALSE) then
debugPrint("isKnight: Player not found.")
return false
end
return (isInArray({4,8}, getPlayerVocation(cid)) == TRUE)
end
function isRookie(cid)
if(isPlayer(cid) == FALSE) then
debugPrint("isRookie: Player not found.")
return false
end
return (isInArray({0}, getPlayerVocation(cid)) == TRUE)
end
function getConfigInfo(info)
if(type(info) ~= 'string') then return nil end
dofile(getConfigFile())
return _G[info]
end
function getDirectionTo(pos1, pos2)
local dir = NORTH
if(pos1.x > pos2.x) then
dir = WEST
if(pos1.y > pos2.y) then
dir = NORTHWEST
elseif(pos1.y < pos2.y) then
dir = SOUTHWEST
end
elseif(pos1.x < pos2.x) then
dir = EAST
if(pos1.y > pos2.y) then
dir = NORTHEAST
elseif(pos1.y < pos2.y) then
dir = SOUTHEAST
end
else
if(pos1.y > pos2.y) then
dir = NORTH
elseif(pos1.y < pos2.y) then
dir = SOUTH
end
end
return dir
end
function getPlayerLookPos(cid)
return getPosByDir(getThingPos(cid), getPlayerLookDir(cid))
end
function getPosByDir(fromPosition, direction, size)
local n = size or 1
local pos = fromPosition
if(direction == NORTH) then
pos.y = pos.y - n
elseif(direction == SOUTH) then
pos.y = pos.y + n
elseif(direction == WEST) then
pos.x = pos.x - n
elseif(direction == EAST) then
pos.x = pos.x + n
elseif(direction == NORTHWEST) then
pos.y = pos.y - n
pos.x = pos.x - n
elseif(direction == NORTHEAST) then
pos.y = pos.y - n
pos.x = pos.x + n
elseif(direction == SOUTHWEST) then
pos.y = pos.y + n
pos.x = pos.x - n
elseif(direction == SOUTHEAST) then
pos.y = pos.y + n
pos.x = pos.x + n
end
return pos
end
function getPlayerMoney(cid)
return ((getPlayerItemCount(cid, ITEM_CRYSTAL_COIN) * 10000) + (getPlayerItemCount(cid, ITEM_PLATINUM_COIN) * 100) + getPlayerItemCount(cid, ITEM_GOLD_COIN))
end
function doPlayerWithdrawAllMoney(cid)
return doPlayerWithdrawMoney(cid, getPlayerBalance(cid))
end
function doPlayerDepositAllMoney(cid)
return doPlayerDepositMoney(cid, getPlayerMoney(cid))
end
function doPlayerTransferAllMoneyTo(cid, target)
return doPlayerTransferMoneyTo(cid, target, getPlayerBalance(cid))
end
function playerExists(name)
return (getPlayerGUIDByName(name) ~= 0)
end
function getTibiaTime()
local worldTime = getWorldTime()
local hours = 0
while (worldTime > 60) do
hours = hours + 1
worldTime = worldTime - 60
end
return {hours = hours, minutes = worldTime}
end
function doWriteLogFile(file, text)
local file = io.open(file, "a+")
file:write("[" .. os.date("%d/%m/%Y %H:%M:%S") .. "] " .. text .. "\n")
file:close()
end
function isInArea(pos, fromPos, toPos)
if pos.x >= fromPos.x and pos.x <= toPos.x then
if pos.y >= fromPos.y and pos.y <= toPos.y then
if pos.z >= fromPos.z and pos.z <= toPos.z then
return TRUE
end
end
end
return FALSE
end
function getExperienceForLevel(lv)
lv = lv - 1
return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
end
function doMutePlayer(cid, time)
local condition = createConditionObject(CONDITION_MUTED)
setConditionParam(condition, CONDITION_PARAM_TICKS, time * 1000)
return doAddCondition(cid, condition)
end
function getPlayerVocationName(cid)
return getVocationInfo(getPlayerVocation(cid)).name
end
function getPromotedVocation(vid)
return getVocationInfo(vid).promotedVocation
end
function doPlayerRemovePremiumDays(cid, days)
return doPlayerAddPremiumDays(cid, -days)
end
function getPlayerMasterPos(cid)
return getTownTemplePosition(getPlayerTown(cid))
end
function getItemNameById(itemid)
return getItemDescriptionsById(itemid).name
end
function getItemPluralNameById(itemid)
return getItemDescriptionsById(itemid).plural
end
function getItemArticleById(itemid)
return getItemDescriptionsById(itemid).article
end
function getItemName(uid)
return getItemDescriptions(uid).name
end
function getItemPluralName(uid)
return getItemDescriptions(uid).plural
end
function getItemArticle(uid)
return getItemDescriptions(uid).article
end
function getItemText(uid)
return getItemDescriptions(uid).text
end
function getItemWriter(uid)
return getItemDescriptions(uid).writer
end
function getItemDate(uid)
return getItemDescriptions(uid).date
end
function getTilePzInfo(pos)
return getTileInfo(pos).protection and TRUE or FALSE
end
function getTileHouseInfo(pos)
return getTileInfo(pos).house and TRUE or FALSE
end
function getTileZoneInfo(pos)
local tmp = getTileInfo(pos)
if(tmp.pvp) then
return 2
end
if(tmp.nopvp) then
return 1
end
return 0
end
function getOnlinePlayers()
local tmp = getPlayersOnline()
local players = {}
for i, cid in ipairs(tmp) do
table.insert(players, getCreatureName(cid))
end
return players
end
function getPlayerByName(name)
local cid = getCreatureByName(name)
return isPlayer(cid) == TRUE and cid or nil
end
function getPlayerFrags(cid)
return math.ceil((getPlayerRedSkullTicks(cid) / getConfigInfo('timeToDecreaseFrags')) + 1)
end
function doConvertIntegerToIp(int, mask)
local b4 = bit.urshift(bit.uband(int, 4278190080), 24)
local b3 = bit.urshift(bit.uband(int, 16711680), 16)
local b2 = bit.urshift(bit.uband(int, 65280), 8)
local b1 = bit.urshift(bit.uband(int, 255), 0)
if(mask ~= nil) then
local m4 = bit.urshift(bit.uband(mask, 4278190080), 24)
local m3 = bit.urshift(bit.uband(mask, 16711680), 16)
local m2 = bit.urshift(bit.uband(mask, 65280), 8)
local m1 = bit.urshift(bit.uband(mask, 255), 0)
if((m1 == 255 or m1 == 0) and (m2 == 255 or m2 == 0) and (m3 == 255 or m3 == 0) and (m4 == 255 or m4 == 0)) then
if m1 == 0 then b1 = "x" end
if m2 == 0 then b2 = "x" end
if m3 == 0 then b3 = "x" end
if m4 == 0 then b4 = "x" end
elseif(m1 ~= 255 or m2 ~= 255 or m3 ~= 255 or m4 ~= 255) then
return b1 .. "." .. b2 .. "." .. b3 .. "." .. b4 .. " : " .. m1 .. "." .. m2 .. "." .. m3 .. "." .. m4
end
end
return b1 .. "." .. b2 .. "." .. b3 .. "." .. b4
end
function getBooleanFromString(str)
return (str:lower() == "yes" or str:lower() == "true" or (tonumber(str) and tonumber(str) > 0)) and TRUE or FALSE
end
function doCopyItem(item, attributes)
local attributes = attributes or FALSE
local ret = doCreateItemEx(item.itemid, item.type)
if(attributes == TRUE) then
if(item.actionid > 0) then
doSetItemActionId(ret, item.actionid)
end
end
if(isContainer(item.uid) == TRUE) then
for i = (getContainerSize(item.uid) - 1), 0, -1 do
local tmp = getContainerItem(item.uid, i)
if(tmp.itemid > 0) then
doAddContainerItemEx(ret, doCopyItem(tmp, TRUE).uid)
end
end
end
return getThing(ret)
end
table.find = function (table, value)
for i, v in pairs(table) do
if(v == value) then
return i
end
end
return nil
end
table.isStrIn = function (txt, str)
for i, v in pairs(str) do
if(txt:find(v) and not txt:find('(%w+)' .. v) and not txt:find(v .. '(%w+)')) then
return true
end
end
return false
end
table.countElements = function (table, item)
local count = 0
for i, n in pairs(table) do
if(item == n) then
count = count + 1
end
end
return count
end
table.getCombinations = function (table, num)
local a, number, select, newlist = {}, #table, num, {}
for i = 1, select do
a[#a + 1] = i
end
local newthing = {}
while(true) do
local newrow = {}
for i = 1, select do
newrow[#newrow + 1] = table[a[i]]
end
newlist[#newlist + 1] = newrow
i = select
while(a[i] == (number - select + i)) do
i = i - 1
end
if(i < 1) then
break
end
a[i] = a[i] + 1
for j = i, select do
a[j] = a[i] + j - i
end
end
return newlist
end
string.split = function (str)
local t = {}
local function helper(word)
table.insert(t, word)
return ""
end
if(not str:gsub("%w+", helper):find("%S")) then
return t
end
end
string.trim = function (str)
return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
end
string.explode = function (str, sep)
local pos, t = 1, {}
if #sep == 0 or #str == 0 then
return
end
for s, e in function() return str:find(sep, pos) end do
table.insert(t, str:sub(pos, s - 1):trim())
pos = e + 1
end
table.insert(t, str:sub(pos):trim())
return t
end
function isInArea(pos, fromPos, toPos)
if pos.x >= fromPos.x and pos.x <= toPos.x then
if pos.y >= fromPos.y and pos.y <= toPos.y then
if pos.z >= fromPos.z and pos.z <= toPos.z then
return true
end
end
end
return false
end
-- Premium Points
function getAccountPoints(cid)
local Info = db.getResult("SELECT `premium_points` FROM `accounts` WHERE `id` = " .. getPlayerAccountId(cid) .. " LIMIT 1")
if Info:getID() ~= LUA_ERROR then
local Points= Info:getDataInt("premium_points")
Info:free()
return Points
end
return LUA_ERROR
end
function getAccountPointsByAccountId(id)
local Info = db.getResult("SELECT `premium_points` FROM `accounts` WHERE `id` = " .. id .. " LIMIT 1")
if Info:getID() ~= LUA_ERROR then
local Points= Info:getDataInt("premium_points")
Info:free()
return Points
end
return LUA_ERROR
end
function doAccountRemovePoints(cid, points)
local dif = getAccountPoints(cid) - points
if dif >= 0 then
db.executeQuery("UPDATE `accounts` SET `premium_points` = `premium_points` - " .. points .. " WHERE `id` = " .. getPlayerAccountId(cid) .. ";")
return TRUE
end
return FALSE
end
function doAccountAddPoints(cid, points)
local dif = getAccountPoints(cid) + points
if dif >= 0 then
db.executeQuery("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. points .. " WHERE `id` = " .. getPlayerAccountId(cid) .. ";")
return TRUE
end
return FALSE
end
function doAccountAddPointsByAccountId(id, points)
local dif = getAccountPointsByAccountId(id) + points
if dif >= 0 then
db.executeQuery("UPDATE `accounts` SET `premium_points` = `premium_points` + " .. points .. " WHERE `id` = " .. id .. ";")
return TRUE
end
return FALSE
end
-- shop system
dofile("./config.lua")
function getOfferOfPlayer(name)
local offer = {}
local result_items = db.getResult("SELECT `id`, `name`, `item_name`, `itemid`, `subType`, `count`, `price` FROM `playersshopsystem` WHERE `name` = " .. db.escapeString(name) .. " AND `count` > '0' AND `itemid` != '66'")
if(result_items:getID() ~= -1) then
while(true) do
table.insert(offer, {id = result_items:getDataInt("id"), name = result_items:getDataString("name"), item_name = result_items:getDataString("item_name"), itemid = result_items:getDataInt("itemid"), subType = result_items:getDataInt("subType"), count = result_items:getDataInt("count"), price = result_items:getDataInt("price")})
if not(result_items:next()) then
break
end
end
result_items:free()
end
return offer
end
function getOfferOfItem(item_name)
local offer = {}
result_items = db.getResult("SELECT `id`, `name`, `item_name`, `itemid`, `subType`, `count`, `price` FROM `playersshopsystem` WHERE `item_name` = " .. db.escapeString(item_name) .. " AND `count` > '0' AND `itemid` != '66' ORDER BY `price` ASC LIMIT 5")
if(result_items:getID() ~= -1) then
while(true) do
table.insert(offer, {id = result_items:getDataInt("id"), name = result_items:getDataString("name"), item_name = result_items:getDataString("item_name"), itemid = result_items:getDataInt("itemid"), subType = result_items:getDataInt("subType"), count = result_items:getDataInt("count"), price = result_items:getDataInt("price")})
if not(result_items:next()) then
break
end
end
result_items:free()
end
return offer
end
function addItemToOffer(name, itemid, subType, count)
local inDB_id = 0
local result_offers = db.getResult("SELECT `id`, `count` FROM `playersshopsystem` WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '" .. itemid .. "' AND `subType` = '" .. subType .. "' AND `itemid` != '66'")
if(result_offers:getID() ~= -1) then
inDB_id = result_offers:getDataInt("id")
result_offers:free()
end
if inDB_id ~= 0 then
db.executeQuery("UPDATE `playersshopsystem` SET `count` = `playersshopsystem`.`count` + " .. count .." WHERE `id` = '" .. inDB_id .. "' AND `subType` = '" .. subType .. "' AND `itemid` != '66'")
else
db.executeQuery("INSERT INTO `playersshopsystem` (`id`, `name`, `item_name`, `itemid`, `subType`, `count`, `price`) VALUES (NULL, " .. db.escapeString(name) .. ", " .. db.escapeString(getItemNameById(itemid)) .. ", '" .. itemid .. "', '" .. subType .. "', '" .. count .. "', 1000000)")
end
return true
end
function removeItemsFromOffer(name, itemid, subType, count)
db.executeQuery("UPDATE `playersshopsystem` SET `count` = `playersshopsystem`.`count` - " .. count .." WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '" .. itemid .. "' AND `subType` = '" .. subType .. "' AND `itemid` != '66'")
return true
end
function changePriceOfItem(name, itemid, subType, price)
db.executeQuery("UPDATE `playersshopsystem` SET `price` = " .. price .." WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '" .. itemid .. "' AND `subType` = '" .. subType .. "' AND `itemid` != '66'")
return true
end
function getItemWithId(id)
local item = {id=0}
local result_items = db.getResult("SELECT `id`, `name`, `item_name`, `itemid`, `subType`, `count`, `price` FROM `playersshopsystem` WHERE `id` = '" .. id .. "' AND `count` > 0 AND `itemid` != '66'")
if (result_items:getID() ~= -1) then
item = {id = result_items:getDataInt("id"), name = result_items:getDataString("name"), item_name = result_items:getDataString("item_name"), itemid = result_items:getDataInt("itemid"), subType = result_items:getDataInt("subType"), count = result_items:getDataInt("count"), price = result_items:getDataInt("price")}
result_items:free()
end
return item
end
function getItemWithItemId(name, itemid, subType)
local item = {id=0}
local result_items = db.getResult("SELECT `id`, `name`, `item_name`, `itemid`, `subType`, `count`, `price` FROM `playersshopsystem` WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '" .. itemid .. "' AND `subType` = '" .. subType .. "' AND `count` > 0 AND `itemid` != '66'")
if(result_items:getID() ~= -1) then
item = {id = result_items:getDataInt("id"), name = result_items:getDataString("name"), item_name = result_items:getDataString("item_name"), itemid = result_items:getDataInt("itemid"), subType = result_items:getDataInt("subType"), count = result_items:getDataInt("count"), price = result_items:getDataInt("price")}
result_items:free()
end
return item
end
function getCash(name)
local cash = 0
local result_items = db.getResult("SELECT `id`, `count` FROM `playersshopsystem` WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '66'")
if(result_items:getID() ~= -1) then
cash = result_items:getDataInt("count")
result_items:free()
else
db.executeQuery("INSERT INTO `playersshopsystem` (`id`, `name`, `item_name`, `itemid`, `count`, `price`) VALUES ('', " .. db.escapeString(name) .. ", 'Player Cash', '66', '0', '0')")
end
return cash
end
function setCash(name, count)
db.executeQuery("UPDATE `playersshopsystem` SET `count` = " .. count ..", `price` = " .. count .." WHERE `name` = " .. db.escapeString(name) .. " AND `itemid` = '66'")
return true
end
function addPlayerItemFromShop(cid, itemid, amount, subType)
local amount = amount or 1
local subType = subType or 0
if(isItemStackable(itemid) == TRUE) then
local item = doCreateItemEx(itemid, amount)
local ret = doPlayerAddItemEx(cid, item)
if(ret ~= RETURNVALUE_NOERROR) then
return {}, 0
end
return {item}, amount
end
local items = {}
local ret = 0
local a = 0
for i = 1, amount do
items[i] = doCreateItemEx(itemid, subType)
ret = doPlayerAddItemEx(cid, items[i])
if(ret ~= RETURNVALUE_NOERROR) then
break
end
a = a + 1
end
return items, a
end
-- marriages
function getPlayerMarriage(player)
local rows = db.getResult("SELECT `marriage` FROM `players` WHERE `id` = " .. player .. ";")
local marry = rows:getDataInt("marriage")
if marry ~= 0 then
return marry
else
return 0
end
end
function addMarryStatus(player,partner)
db.executeQuery("UPDATE `players` SET `marrystatus` = " .. partner .. " WHERE `id` = " .. player .. ";")
end
function doCancelMarryStatus(player)
db.executeQuery("UPDATE `players` SET `marrystatus` = 0 WHERE `id` = " .. player .. ";")
end
function getMarryStatus(player)
local stat = db.getResult("SELECT `id` FROM `players` WHERE `marrystatus` = " .. player .. ";")
if(stat:getID() == -1) then
return 0
else
local info = stat:getDataInt("id")
return info
end
end
function getOwnMarryStatus(player)
local stat = db.getResult("SELECT `marrystatus` FROM `players` WHERE `id` = " .. player .. ";")
if(stat:getID() == -1) then
return 0
else
local info = stat:getDataInt("marrystatus")
return info
end
end
function isOnline(player)
local rows = db.getResult("SELECT `online` FROM `players` WHERE `id` = " .. player .. ";")
local on = rows:getDataInt("online")
if on ~= 0 then
return 1
else
return 0
end
end