Advertisement
Guest User

Untitled

a guest
Nov 24th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.31 KB | None | 0 0
  1. function isInArray(array, value, caseSensitive)
  2.     if(caseSensitive == nil or caseSensitive == false) and type(value) == "string" then
  3.         local lowerValue = value:lower()
  4.         for _, _value in ipairs(array) do
  5.             if type(_value) == "string" and lowerValue == _value:lower() then
  6.                 return true
  7.             end
  8.         end
  9.     else
  10.         for _, _value in ipairs(array) do
  11.             if (value == _value) then return true end
  12.         end
  13.     end
  14.  
  15.     return false
  16. end
  17.  
  18. function doSetItemActionId(lols, actionid)
  19. return doItemSetAttribute(lols, "aid", actionid)
  20. end
  21. function comparePos(pos1, pos2)
  22.     return (pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z)
  23. end
  24.  
  25. function doPlayerGiveItem(cid, itemid, amount, subType)
  26.     local item = 0
  27.     if(isItemStackable(itemid)) then
  28.         item = doCreateItemEx(itemid, amount)
  29.         if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
  30.             return false
  31.         end
  32.     else
  33.         for i = 1, amount do
  34.             item = doCreateItemEx(itemid, subType)
  35.             if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
  36.                 return false
  37.             end
  38.         end
  39.     end
  40.  
  41.     return true
  42. end
  43.  
  44. function doPlayerGiveItemContainer(cid, containerid, itemid, amount, subType)
  45.     for i = 1, amount do
  46.         local container = doCreateItemEx(containerid, 1)
  47.         for x = 1, getContainerCapById(containerid) do
  48.             doAddContainerItem(container, itemid, subType)
  49.         end
  50.  
  51.         if(doPlayerAddItemEx(cid, container, true) ~= RETURNVALUE_NOERROR) then
  52.             return false
  53.         end
  54.     end
  55.  
  56.     return true
  57. end
  58.  
  59. function doPlayerTakeItem(cid, itemid, amount)
  60.     return getPlayerItemCount(cid, itemid) >= amount and doPlayerRemoveItem(cid, itemid, amount)
  61. end
  62.  
  63. function doPlayerSellItem(cid, itemid, count, cost)
  64.     if(not doPlayerTakeItem(cid, itemid, count)) then
  65.         return false
  66.     end
  67.  
  68.     if(not doPlayerAddMoney(cid, cost)) then
  69.         error('[doPlayerSellItem] Could not add money to: ' .. getPlayerName(cid) .. ' (' .. cost .. 'gp).')
  70.     end
  71.  
  72.     return true
  73. end
  74.  
  75. function doPlayerWithdrawMoney(cid, amount)
  76.     if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
  77.         return false
  78.     end
  79.  
  80.     local balance = getPlayerBalance(cid)
  81.     if(amount > balance or not doPlayerAddMoney(cid, amount)) then
  82.         return false
  83.     end
  84.  
  85.     doPlayerSetBalance(cid, balance - amount)
  86.     return true
  87. end
  88.  
  89. function doPlayerDepositMoney(cid, amount)
  90.     if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
  91.         return false
  92.     end
  93.  
  94.     if(not doPlayerRemoveMoney(cid, amount)) then
  95.         return false
  96.     end
  97.  
  98.     doPlayerSetBalance(cid, getPlayerBalance(cid) + amount)
  99.     return true
  100. end
  101.  
  102. function doPlayerAddStamina(cid, minutes)
  103.     return doPlayerSetStamina(cid, getPlayerStamina(cid) + minutes)
  104. end
  105.  
  106. function isPremium(cid)
  107.     return (isPlayer(cid) and (getPlayerPremiumDays(cid) > 0 or getBooleanFromString(getConfigValue('freePremium'))))
  108. end
  109.  
  110. function getMonthDayEnding(day)
  111.     if(day == "01" or day == "21" or day == "31") then
  112.         return "st"
  113.     elseif(day == "02" or day == "22") then
  114.         return "nd"
  115.     elseif(day == "03" or day == "23") then
  116.         return "rd"
  117.     end
  118.  
  119.     return "th"
  120. end
  121.  
  122. function getMonthString(m)
  123.     return os.date("%B", os.time{year = 1970, month = m, day = 1})
  124. end
  125.  
  126. function getArticle(str)
  127.     return str:find("[AaEeIiOoUuYy]") == 1 and "an" or "a"
  128. end
  129.  
  130. function doNumberFormat(i)
  131.     local str, found = string.gsub(i, "(%d)(%d%d%d)$", "%1,%2", 1), 0
  132.     repeat
  133.         str, found = string.gsub(str, "(%d)(%d%d%d),", "%1,%2,", 1)
  134.     until found == 0
  135.     return str
  136. end
  137.  
  138. function doPlayerAddAddons(cid, addon)
  139.     for i = 0, table.maxn(maleOutfits) do
  140.         doPlayerAddOutfit(cid, maleOutfits[i], addon)
  141.     end
  142.  
  143.     for i = 0, table.maxn(femaleOutfits) do
  144.         doPlayerAddOutfit(cid, femaleOutfits[i], addon)
  145.     end
  146. end
  147.  
  148. function getTibiaTime(num)
  149.     local minutes, hours = getWorldTime(), 0
  150.     while (minutes > 60) do
  151.         hours = hours + 1
  152.         minutes = minutes - 60
  153.     end
  154.  
  155.     if(num) then
  156.         return {hours = hours, minutes = minutes}
  157.     end
  158.  
  159.     return {hours =  hours < 10 and '0' .. hours or '' .. hours, minutes = minutes < 10 and '0' .. minutes or '' .. minutes}
  160. end
  161.  
  162. function doWriteLogFile(file, text)
  163.     local f = io.open(file, "a+")
  164.     if(not f) then
  165.         return false
  166.     end
  167.  
  168.     f:write("[" .. os.date("%d/%m/%Y %H:%M:%S") .. "] " .. text .. "\n")
  169.     f:close()
  170.     return true
  171. end
  172.  
  173. function getExperienceForLevel(lv)
  174.     lv = lv - 1
  175.     return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
  176. end
  177.  
  178. function doMutePlayer(cid, time)
  179.     local condition = createConditionObject(CONDITION_MUTED, (time == -1 and time or time * 1000))
  180.     return doAddCondition(cid, condition, false)
  181.  
  182. end
  183.  
  184. function doSummonCreature(name, pos)
  185.     local cid = doCreateMonster(name, pos, false, false)
  186.     if(not cid) then
  187.         cid = doCreateNpc(name, pos)
  188.     end
  189.  
  190.     return cid
  191. end
  192.  
  193. function getPlayersOnlineEx()
  194.     local players = {}
  195.     for i, cid in ipairs(getPlayersOnline()) do
  196.         table.insert(players, getCreatureName(cid))
  197.     end
  198.  
  199.     return players
  200. end
  201.  
  202. function getPlayerByName(name)
  203.     local cid = getCreatureByName(name)
  204.     return isPlayer(cid) and cid or nil
  205. end
  206.  
  207. function isPlayer(cid)
  208.     return isCreature(cid) and cid >= AUTOID_PLAYERS and cid < AUTOID_MONSTERS
  209. end
  210.  
  211. function isPlayerGhost(cid)
  212.     return isPlayer(cid) and (getCreatureCondition(cid, CONDITION_GAMEMASTER, GAMEMASTER_INVISIBLE, CONDITIONID_DEFAULT) or getPlayerFlagValue(cid, PLAYERFLAG_CANNOTBESEEN))
  213. end
  214.  
  215. function isMonster(cid)
  216.     return isCreature(cid) and cid >= AUTOID_MONSTERS and cid < AUTOID_NPCS
  217. end
  218.  
  219. function isNpc(cid)
  220.     -- Npc IDs are over int32_t range (which is default for lua_pushnumber),
  221.     -- therefore number is always a negative value.
  222.     return isCreature(cid) and (cid < 0 or cid >= AUTOID_NPCS)
  223. end
  224.  
  225. function isUnderWater(cid)
  226.     return isInArray(underWater, getTileInfo(getCreaturePosition(cid)).itemid)
  227. end
  228.  
  229. function doPlayerAddLevel(cid, amount, round)
  230.     local experience, level, amount = 0, getPlayerLevel(cid), amount or 1
  231.     if(amount > 0) then
  232.         experience = getExperienceForLevel(level + amount) - (round and getPlayerExperience(cid) or getExperienceForLevel(level))
  233.     else
  234.         experience = -((round and getPlayerExperience(cid) or getExperienceForLevel(level)) - getExperienceForLevel(level + amount))
  235.     end
  236.  
  237.     return doPlayerAddExperience(cid, experience)
  238. end
  239.  
  240. function doPlayerAddMagLevel(cid, amount)
  241.     local amount = amount or 1
  242.     for i = 1, amount do
  243.         doPlayerAddSpentMana(cid, getPlayerRequiredMana(cid, getPlayerMagLevel(cid, true) + 1) - getPlayerSpentMana(cid), false)
  244.     end
  245.  
  246.     return true
  247. end
  248.  
  249. function doPlayerAddSkill(cid, skill, amount, round)
  250.     local amount = amount or 1
  251.     if(skill == SKILL__LEVEL) then
  252.         return doPlayerAddLevel(cid, amount, round)
  253.     elseif(skill == SKILL__MAGLEVEL) then
  254.         return doPlayerAddMagLevel(cid, amount)
  255.     end
  256.  
  257.     for i = 1, amount do
  258.         doPlayerAddSkillTry(cid, skill, getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill), false)
  259.     end
  260.  
  261.     return true
  262. end
  263.  
  264. function isPrivateChannel(channelId)
  265.     return channelId >= CHANNEL_PRIVATE
  266. end
  267.  
  268. function doBroadcastMessage(text, class)
  269.     local class = class or MESSAGE_STATUS_WARNING
  270.     if(type(class) == 'string') then
  271.         local className = MESSAGE_TYPES[class]
  272.         if(className == nil) then
  273.             return false
  274.         end
  275.  
  276.         class = className
  277.     elseif(class < MESSAGE_FIRST or class > MESSAGE_LAST) then
  278.         return false
  279.     end
  280.  
  281.     for _, pid in ipairs(getPlayersOnline()) do
  282.         doPlayerSendTextMessage(pid, class, text)
  283.     end
  284.  
  285.     print("> Broadcasted message: \"" .. text .. "\".")
  286.     return true
  287. end
  288.  
  289. function doPlayerBroadcastMessage(cid, text, class, checkFlag, ghost)
  290.     local checkFlag, ghost, class = checkFlag or true, ghost or false, class or TALKTYPE_BROADCAST
  291.     if(checkFlag and not getPlayerFlagValue(cid, PLAYERFLAG_CANBROADCAST)) then
  292.         return false
  293.     end
  294.  
  295.     if(type(class) == 'string') then
  296.         local className = TALKTYPE_TYPES[class]
  297.         if(className == nil) then
  298.             return false
  299.         end
  300.  
  301.         class = className
  302.     elseif(class < TALKTYPE_FIRST or class > TALKTYPE_LAST) then
  303.         return false
  304.     end
  305.  
  306.     for _, pid in ipairs(getPlayersOnline()) do
  307.         doCreatureSay(cid, text, class, ghost, pid)
  308.     end
  309.  
  310.     print("> " .. getCreatureName(cid) .. " broadcasted message: \"" .. text .. "\".")
  311.     return true
  312. end
  313.  
  314. function doCopyItem(item, attributes)
  315.     local attributes = ((type(attributes) == 'table') and attributes or { "aid" })
  316.  
  317.     local ret = doCreateItemEx(item.itemid, item.type)
  318.     for _, key in ipairs(attributes) do
  319.         local value = getItemAttribute(item.uid, key)
  320.         if(value ~= nil) then
  321.             doItemSetAttribute(ret, key, value)
  322.         end
  323.     end
  324.  
  325.     if(isContainer(item.uid)) then
  326.         for i = (getContainerSize(item.uid) - 1), 0, -1 do
  327.             local tmp = getContainerItem(item.uid, i)
  328.             if(tmp.itemid > 0) then
  329.                 doAddContainerItemEx(ret, doCopyItem(tmp, true).uid)
  330.             end
  331.         end
  332.     end
  333.  
  334.     return getThing(ret)
  335. end
  336.  
  337. function doSetItemText(uid, text, writer, date)
  338.     local thing = getThing(uid)
  339.     if(thing.itemid < 100) then
  340.         return false
  341.     end
  342.  
  343.     doItemSetAttribute(uid, "text", text)
  344.     if(writer ~= nil) then
  345.         doItemSetAttribute(uid, "writer", tostring(writer))
  346.         if(date ~= nil) then
  347.             doItemSetAttribute(uid, "date", tonumber(date))
  348.         end
  349.     end
  350.  
  351.     return true
  352. end
  353.  
  354. function getItemWeightById(itemid, count, precision)
  355.     local item, count, precision = getItemInfo(itemid), count or 1, precision or false
  356.     if(not item) then
  357.         return false
  358.     end
  359.  
  360.     if(count > 100) then
  361.         -- print a warning, as its impossible to have more than 100 stackable items without "cheating" the count
  362.         print('[Warning] getItemWeightById', 'Calculating weight for more than 100 items!')
  363.     end
  364.  
  365.     local weight = item.weight * count
  366.     return precission and weight or math.round(weight, 2)
  367. end
  368.  
  369. function choose(...)
  370.     local arg, ret = {...}
  371.  
  372.     if type(arg[1]) == 'table' then
  373.         ret = arg[1][math.random(#arg[1])]
  374.     else
  375.         ret = arg[math.random(#arg)]
  376.     end
  377.  
  378.     return ret
  379. end
  380.  
  381. function doPlayerAddExpEx(cid, amount)
  382.     if(not doPlayerAddExp(cid, amount)) then
  383.         return false
  384.     end
  385.  
  386.     local position = getThingPosition(cid)
  387.     doPlayerSendTextMessage(cid, MESSAGE_EXPERIENCE, "You gained " .. amount .. " experience.", amount, COLOR_WHITE, position)
  388.  
  389.     local spectators, name = getSpectators(position, 7, 7), getCreatureName(cid)
  390.     for _, pid in ipairs(spectators) do
  391.         if(isPlayer(pid) and cid ~= pid) then
  392.             doPlayerSendTextMessage(pid, MESSAGE_EXPERIENCE_OTHERS, name .. " gained " .. amount .. " experience.", amount, COLOR_WHITE, position)
  393.         end
  394.     end
  395.  
  396.     return true
  397. end
  398.  
  399. function getItemTopParent(uid)
  400.     local parent = getItemParent(uid)
  401.     if(not parent or parent.uid == 0) then
  402.         return nil
  403.     end
  404.  
  405.     while(true) do
  406.         local tmp = getItemParent(parent.uid)
  407.         if(tmp and tmp.uid ~= 0) then
  408.             parent = tmp
  409.         else
  410.             break
  411.         end
  412.     end
  413.  
  414.     return parent
  415. end
  416.  
  417. function getItemHolder(uid)
  418.     local parent = getItemParent(uid)
  419.     if(not parent or parent.uid == 0) then
  420.         return nil
  421.     end
  422.  
  423.     local holder = nil
  424.     while(true) do
  425.         local tmp = getItemParent(parent.uid)
  426.         if(tmp and tmp.uid ~= 0) then
  427.             if(tmp.itemid == 1) then -- a creature
  428.                 holder = tmp
  429.                 break
  430.             end
  431.  
  432.             parent = tmp
  433.         else
  434.             break
  435.         end
  436.     end
  437.  
  438.     return holder
  439. end
  440.  
  441. function valid(f)
  442.     return function(p, ...)
  443.         if(isCreature(p)) then
  444.             return f(p, ...)
  445.         end
  446.     end
  447. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement