function doPlayerGiveItem(cid, itemid, count, charges)
local hasCharges = (isItemRune(itemid) == TRUE or isItemFluidContainer(itemid) == TRUE)
if(hasCharges and charges == nil) then
charges = 1
end
while count > 0 do
local tempcount = 1
if(hasCharges) then
tempcount = charges
end
if(isItemStackable(itemid) == TRUE) then
tempcount = math.min (100, count)
end
local ret = doPlayerAddItem(cid, itemid, tempcount)
if(ret == LUA_ERROR) then
ret = doCreateItem(itemid, tempcount, getPlayerPosition(cid))
end
if(ret ~= LUA_ERROR) then
if(hasCharges) then
count = count - 1
else
count = count - tempcount
end
else
return LUA_ERROR
end
end
return LUA_NO_ERROR
end
function doPlayerTakeItem(cid, itemid, count)
if(getPlayerItemCount(cid,itemid) >= count) then
while count > 0 do
local tempcount = 0
if(isItemStackable(itemid) == TRUE) then
tempcount = math.min (100, count)
else
tempcount = 1
end
local ret = doPlayerRemoveItem(cid, itemid, tempcount)
if(ret ~= LUA_ERROR) then
count = count-tempcount
else
return LUA_ERROR
end
end
if(count == 0) then
return LUA_NO_ERROR
end
else
return LUA_ERROR
end
end
function doPlayerAddMoney(cid, amount)
local crystals = math.floor(amount/10000)
amount = amount - crystals*10000
local platinum = math.floor(amount/100)
amount = amount - platinum*100
local gold = amount
local ret = 0
if(crystals > 0) then
ret = doPlayerGiveItem(cid, ITEM_CRYSTAL_COIN, crystals)
if(ret ~= LUA_NO_ERROR) then
return LUA_ERROR
end
end
if(platinum > 0) then
ret = doPlayerGiveItem(cid, ITEM_PLATINUM_COIN, platinum)
if(ret ~= LUA_NO_ERROR) then
return LUA_ERROR
end
end
if(gold > 0) then
ret = doPlayerGiveItem(cid, ITEM_GOLD_COIN, gold)
if(ret ~= LUA_NO_ERROR) then
return LUA_ERROR
end
end
return LUA_NO_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 doPlayerSellItem(cid, itemid, count, cost)
if(doPlayerTakeItem(cid, itemid, count) == LUA_NO_ERROR) then
if(doPlayerAddMoney(cid, cost) ~= LUA_NO_ERROR) 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)
if 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 then
return TRUE
end
return FALSE
end
function isNumber(p)
local m = ""
for i = 1, string.len(p) do
m = string.sub(p, i, i)
if m == "0" or m == "1" or m == "2" or m == "3" or m == "4" or m == "5" or m == "6" or m == "7" or m == "8" or m == "9" then
-- continue
else
return FALSE
end
end
return TRUE
end
function isPremium(cid)
if isPlayer(cid) == TRUE and getPlayerPremiumDays(cid) > 0 then
return TRUE
end
return FALSE
end
function rows(connection, sql_statement)
local cursor = assert(connection:execute(sql_statement))
return function ()
return cursor:fetch()
end
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(month)
if month == 1 then
return "January"
elseif month == 2 then
return "February"
elseif month == 3 then
return "March"
elseif month == 4 then
return "April"
elseif month == 5 then
return "May"
elseif month == 6 then
return "June"
elseif month == 7 then
return "July"
elseif month == 8 then
return "August"
elseif month == 9 then
return "September"
elseif month == 10 then
return "October"
elseif month == 11 then
return "November"
elseif month == 12 then
return "December"
end
end
function getArticle(str)
if string.find(str, "[A,a,E,e,I,i,O,o,U,u,Y,y]") == TRUE then
return "an"
end
return "a"
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 numRows(cursor)
local row = cursor:fetch()
local rows = 0
while row do
rows = rows + 1
row = cursor:fetch()
end
cursor:close()
return rows
end
local saveDelay = 1 * 60 * 1000
local storageValue = 2342
if (getGlobalStorageValue(storageValue) == -1) then
function save(saveDelay)
saveData()
print("**!**Server Saved**!**")
addEvent(save, saveDelay, saveDelay)
end
addEvent(save, saveDelay, saveDelay)
setGlobalStorageValue(storageValue, 1)
end
function getConfigInfo(info)
if (type(info) ~= 'string') then return nil end
dofile('config.lua')
return _G[info]
end
function hasCondition(cid, condition)
return getCreatureCondition(cid, condition)
end
function exhaust(cid, storevalue, exhausttime)
-- Exhaustion function by Alreth, v1.1 2006-06-24 01:31
-- Returns 1 if not exhausted and 0 if exhausted
newExhaust = os.time()
oldExhaust = getPlayerStorageValue(cid, storevalue)
if (oldExhaust == nil or oldExhaust < 0) then
oldExhaust = 0
end
if (exhausttime == nil or exhausttime < 0) then
exhausttime = 1
end
diffTime = os.difftime(newExhaust, oldExhaust)
if (diffTime >= exhausttime or diffTime < 0) then
setPlayerStorageValue(cid, storevalue, newExhaust)
return 1
else
return 0
end
end
function repeatEffect(cid, effect, delay, repeatTime)
if repeatTime > 0 then
doSendMagicEffect(getCreaturePos(cid), effect)
repeatTime = repeatTime - 1
addEvent(repeatEffect, delay, pos, effect, delay, repeatTime)
end
end