function doPlayerGiveItem(cid, itemid, amount, subType)
local item = 0
if(isItemStackable(itemid)) then
item = doCreateItemEx(itemid, amount)
if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
return false
end
else
for i = 1, amount do
item = doCreateItemEx(itemid, subType)
if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
return false
end
end
end
return true
end
function doPlayerTakeItem(cid, itemid, amount)
return getPlayerItemCount(cid, itemid) >= amount and doPlayerRemoveItem(cid, itemid, amount)
end
function doPlayerBuyItem(cid, itemid, count, cost, charges)
return doPlayerRemoveMoney(cid, cost) and doPlayerGiveItem(cid, itemid, count, charges)
end
function doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)
if(not doPlayerRemoveMoney(cid, cost)) then
return false
end
for i = 1, count do
local container = doCreateItemEx(containerid, 1)
for x = 1, getContainerCapById(containerid) do
doAddContainerItem(container, itemid, charges)
end
if(doPlayerAddItemEx(cid, container, true) ~= RETURNVALUE_NOERROR) then
return false
end
end
return true
end
function doPlayerSellItem(cid, itemid, count, cost)
if(not doPlayerTakeItem(cid, itemid, count)) then
return false
end
if(not doPlayerAddMoney(cid, cost)) then
error('[doPlayerSellItem] Could not add money to: ' .. getPlayerName(cid) .. ' (' .. cost .. 'gp).')
end
return true
end
function doPlayerWithdrawMoney(cid, amount)
if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
return false
end
local balance = getPlayerBalance(cid)
if(amount > balance or not doPlayerAddMoney(cid, amount)) then
return false
end
doPlayerSetBalance(cid, balance - amount)
return true
end
function doPlayerDepositMoney(cid, amount)
if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
return false
end
if(not doPlayerRemoveMoney(cid, amount)) then
return false
end
doPlayerSetBalance(cid, getPlayerBalance(cid) + amount)
return true
end
function isPremium(cid)
return (isPlayer(cid) and (getPlayerPremiumDays(cid) > 0 or getBooleanFromString(getConfigInfo('freePremium'))))
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"
end
return "th"
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
end
function doPlayerAddAddons(cid, addon)
for i = 0, table.maxn(maleOutfits) do
doPlayerAddOutfit(cid, maleOutfits, addon)
end
for i = 0, table.maxn(femaleOutfits) do
doPlayerAddOutfit(cid, femaleOutfits, addon)
end
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 minutes = getWorldTime()
local hours = 0
while (minutes > 60) do
hours = hours + 1
minutes = minutes - 60
end
return {hours = hours, minutes = minutes}
end
function doWriteLogFile(file, text)
local f = io.open(file, "a+")
if(not f) then
return false
end
f:write("[" .. os.date("%d/%m/%Y %H:%M:%S") .. "] " .. text .. "\n")
f:close()
return true
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 getPlayerGroupName(cid)
return getGroupInfo(getPlayerGroupId(cid)).name
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 getHouseOwner(houseId)
return getHouseInfo(houseId).owner
end
function getHouseName(houseId)
return getHouseInfo(houseId).name
end
function getHouseEntry(houseId)
return getHouseInfo(houseId).entry
end
function getHouseRent(houseId)
return getHouseInfo(houseId).rent
end
function getHousePrice(houseId)
return getHouseInfo(houseId).price
end
function getHouseTown(houseId)
return getHouseInfo(houseId).town
end
function getHouseTilesCount(houseId)
return getHouseInfo(houseId).tiles
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 getItemSpecialDescription(uid)
return getItemDescriptions(uid).special
end
function getItemWriter(uid)
return getItemDescriptions(uid).writer
end
function getItemDate(uid)
return getItemDescriptions(uid).date
end
function getTilePzInfo(pos)
return getTileInfo(pos).protection
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 doShutdown()
return doSetGameState(GAMESTATE_SHUTDOWN)
end
function doSummonCreature(name, pos)
local cid = doCreateMonster(name, pos)
if(not cid) then
cid = doCreateNpc(name, pos)
end
return cid
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) and cid or nil
end
function isPlayer(cid)
return isCreature(cid) and cid >= AUTOID_PLAYERS and cid < AUTOID_MONSTERS
end
function isPlayerGhost(cid)
if(not isPlayer(cid)) then
return false
end
return getCreatureCondition(cid, CONDITION_GAMEMASTER, GAMEMASTER_INVISIBLE) or getPlayerFlagValue(cid, PlayerFlag_CannotBeSeen)
end
function isMonster(cid)
return isCreature(cid) and cid >= AUTOID_MONSTERS and cid < AUTOID_NPCS
end
function isNpc(cid)
return isCreature(cid) and cid >= AUTOID_NPCS
end
function doPlayerSetExperienceRate(cid, value)
return doPlayerSetRate(cid, SKILL__LEVEL, value)
end
function doPlayerSetMagicRate(cid, value)
return doPlayerSetRate(cid, SKILL__MAGLEVEL, value)
end
function doPlayerAddLevel(cid, amount, round)
local experience, level = 0, getPlayerLevel(cid)
if(amount > 0) then
experience = getExperienceForLevel(level + amount) - (round and getPlayerExperience(cid) or getExperienceForLevel(level))
else
experience = -((round and getPlayerExperience(cid) or getExperienceForLevel(level)) - getExperienceForLevel(level + amount))
end
return doPlayerAddExperience(cid, experience)
end
function doPlayerAddMagLevel(cid, amount)
for i = 1, amount do
doPlayerAddSpentMana(cid, (getPlayerRequiredMana(cid, getPlayerMagLevel(cid, true) + 1) - getPlayerSpentMana(cid)) / getConfigInfo('rateMagic'))
end
return true
end
function doPlayerAddSkill(cid, skill, amount, round)
if(skill == SKILL__LEVEL) then
return doPlayerAddLevel(cid, amount, round)
elseif(skill == SKILL__MAGLEVEL) then
return doPlayerAddMagLevel(cid, amount)
end
return doPlayerAddSkillTry(cid, skill, (getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill)) / getConfigInfo('rateSkill'))
end
function getPartyLeader(cid)
local party = getPartyMembers(cid)
if(type(party) ~= 'table') then
return 0
end
return party[1]
end
function isInParty(cid)
return type(getPartyMembers(cid)) == 'table'
end
function isPrivateChannel(channelId)
return channelId >= CHANNEL_PRIVATE
end
function doPlayerResetIdleTime(cid)
return doPlayerSetIdleTime(cid, 0)
end
function doBroadcastMessage(text, class)
local class = class or MESSAGE_STATUS_WARNING
if(type(class) == 'string') then
local className = MESSAGE_TYPES[class]
if(className == nil) then
return false
end
class = className
elseif(class < MESSAGE_FIRST or class > MESSAGE_LAST) then
return false
end
local players = getPlayersOnline()
for _, pid in ipairs(players) do
doPlayerSendTextMessage(pid, class, text)
end
print("> Broadcasted message: \"" .. text .. "\".")
return true
end
function doPlayerBroadcastMessage(cid, text, class, checkFlag, ghost)
local checkFlag, ghost, class = checkFlag or true, ghost or false, class or TALKTYPE_BROADCAST
if(checkFlag and not getPlayerFlagValue(cid, PLAYERFLAG_CANBROADCAST)) then
return false
end
if(type(class) == 'string') then
local className = TALKTYPE_TYPES[class]
if(className == nil) then
return false
end
class = className
elseif(class < TALKTYPE_FIRST or class > TALKTYPE_LAST) then
return false
end
local players = getPlayersOnline()
for _, pid in ipairs(players) do
doCreatureSay(cid, text, class, ghost, pid)
end
print("> " .. getCreatureName(cid) .. " broadcasted message: \"" .. text .. "\".")
return true
end
function getBooleanFromString(str)
return (str:lower() == "yes" or str:lower() == "true" or (tonumber(str) and tonumber(str) > 0))
end
function doCopyItem(item, attributes)
local attributes = attributes or false
local ret = doCreateItemEx(item.itemid, item.type)
if(attributes) then
if(item.actionid > 0) then
doSetItemActionId(ret, item.actionid)
end
end
if(isContainer(item.uid)) 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
function doRemoveThing(uid)
if(isCreature(uid)) then
return doRemoveCreature(uid)
end
return doRemoveItem(uid)
end