Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.46 KB | None | 0 0
  1. function doPlayerGiveItem(cid, itemid, amount, subType)
  2.     local item = 0
  3.     if(isItemStackable(itemid)) then
  4.         item = doCreateItemEx(itemid, amount)
  5.         if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
  6.             return false
  7.         end
  8.     else
  9.         for i = 1, amount do
  10.             item = doCreateItemEx(itemid, subType)
  11.             if(doPlayerAddItemEx(cid, item, true) ~= RETURNVALUE_NOERROR) then
  12.                 return false
  13.             end
  14.         end
  15.     end
  16.  
  17.     return true
  18. end
  19.  
  20. function doPlayerGiveItemContainer(cid, containerid, itemid, amount, subType)
  21.     for i = 1, amount do
  22.         local container = doCreateItemEx(containerid, 1)
  23.         for x = 1, getContainerCapById(containerid) do
  24.             doAddContainerItem(container, itemid, subType)
  25.         end
  26.  
  27.         if(doPlayerAddItemEx(cid, container, true) ~= RETURNVALUE_NOERROR) then
  28.             return false
  29.         end
  30.     end
  31.  
  32.     return true
  33. end
  34.  
  35. function doPlayerTakeItem(cid, itemid, amount)
  36.     return getPlayerItemCount(cid, itemid) >= amount and doPlayerRemoveItem(cid, itemid, amount)
  37. end
  38.  
  39. function doPlayerBuyItem(cid, itemid, count, cost, charges)
  40.     return doPlayerRemoveMoney(cid, cost) and doPlayerGiveItem(cid, itemid, count, charges)
  41. end
  42.  
  43. function doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)
  44.     return doPlayerRemoveMoney(cid, cost) and doPlayerGiveItemContainer(cid, containerid, itemid, count, charges)
  45. end
  46.  
  47. function doPlayerSellItem(cid, itemid, count, cost)
  48.     if(not doPlayerTakeItem(cid, itemid, count)) then
  49.         return false
  50.     end
  51.  
  52.     if(not doPlayerAddMoney(cid, cost)) then
  53.         error('[doPlayerSellItem] Could not add money to: ' .. getPlayerName(cid) .. ' (' .. cost .. 'gp).')
  54.     end
  55.  
  56.     return true
  57. end
  58.  
  59. function doPlayerWithdrawMoney(cid, amount)
  60.     if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
  61.         return false
  62.     end
  63.  
  64.     local balance = getPlayerBalance(cid)
  65.     if(amount > balance or not doPlayerAddMoney(cid, amount)) then
  66.         return false
  67.     end
  68.  
  69.     doPlayerSetBalance(cid, balance - amount)
  70.     return true
  71. end
  72.  
  73. function doPlayerDepositMoney(cid, amount)
  74.     if(not getBooleanFromString(getConfigInfo('bankSystem'))) then
  75.         return false
  76.     end
  77.  
  78.     if(not doPlayerRemoveMoney(cid, amount)) then
  79.         return false
  80.     end
  81.  
  82.     doPlayerSetBalance(cid, getPlayerBalance(cid) + amount)
  83.     return true
  84. end
  85.  
  86. function doPlayerAddStamina(cid, minutes)
  87.     return doPlayerSetStamina(cid, getPlayerStamina(cid) + minutes)
  88. end
  89.  
  90. function isPremium(cid)
  91.     return (isPlayer(cid) and (getPlayerPremiumDays(cid) > 0 or getBooleanFromString(getConfigValue('freePremium'))))
  92. end
  93.  
  94. function getMonthDayEnding(day)
  95.     if(day == "01" or day == "21" or day == "31") then
  96.         return "st"
  97.     elseif(day == "02" or day == "22") then
  98.         return "nd"
  99.     elseif(day == "03" or day == "23") then
  100.         return "rd"
  101.     end
  102.  
  103.     return "th"
  104. end
  105.  
  106. function getMonthString(m)
  107.     return os.date("%B", os.time{year = 1970, month = m, day = 1})
  108. end
  109.  
  110. function getArticle(str)
  111.     return str:find("[AaEeIiOoUuYy]") == 1 and "an" or "a"
  112. end
  113.  
  114. function isNumeric(str)
  115.     return tonumber(str) ~= nil
  116. end
  117.  
  118. function doNumberFormat(i)
  119.     local str, found = string.gsub(i, "(%d)(%d%d%d)$", "%1,%2", 1), 0
  120.     repeat
  121.         str, found = string.gsub(str, "(%d)(%d%d%d),", "%1,%2,", 1)
  122.     until found == 0
  123.     return str
  124. end
  125.  
  126. function doPlayerAddAddons(cid, addon)
  127.     for i = 0, table.maxn(maleOutfits) do
  128.         doPlayerAddOutfit(cid, maleOutfits[i], addon)
  129.     end
  130.  
  131.     for i = 0, table.maxn(femaleOutfits) do
  132.         doPlayerAddOutfit(cid, femaleOutfits[i], addon)
  133.     end
  134. end
  135.  
  136. function doPlayerWithdrawAllMoney(cid)
  137.     return doPlayerWithdrawMoney(cid, getPlayerBalance(cid))
  138. end
  139.  
  140. function doPlayerDepositAllMoney(cid)
  141.     return doPlayerDepositMoney(cid, getPlayerMoney(cid))
  142. end
  143.  
  144. function doPlayerTransferAllMoneyTo(cid, target)
  145.     return doPlayerTransferMoneyTo(cid, target, getPlayerBalance(cid))
  146. end
  147.  
  148. function playerExists(name)
  149.     return getPlayerGUIDByName(name) ~= nil
  150. end
  151.  
  152. function getTibiaTime()
  153.     local minutes, hours = getWorldTime(), 0
  154.     while (minutes > 60) do
  155.         hours = hours + 1
  156.         minutes = minutes - 60
  157.     end
  158.  
  159.     return {hours = hours, minutes = 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)
  180.     setConditionParam(condition, CONDITION_PARAM_TICKS, time == -1 and time or time * 1000)
  181.     return doAddCondition(cid, condition)
  182. end
  183.  
  184. function getPlayerGroupName(cid)
  185.     return getGroupInfo(getPlayerGroupId(cid)).name
  186. end
  187.  
  188. function getPlayerVocationName(cid)
  189.     return getVocationInfo(getPlayerVocation(cid)).name
  190. end
  191.  
  192. function getPromotedVocation(vid)
  193.     return getVocationInfo(vid).promotedVocation
  194. end
  195.  
  196. function doPlayerRemovePremiumDays(cid, days)
  197.     return doPlayerAddPremiumDays(cid, -days)
  198. end
  199.  
  200. function getPlayerMasterPos(cid)
  201.     return getTownTemplePosition(getPlayerTown(cid))
  202. end
  203.  
  204. function getHouseOwner(houseId)
  205.     return getHouseInfo(houseId).owner
  206. end
  207.  
  208. function getHouseName(houseId)
  209.     return getHouseInfo(houseId).name
  210. end
  211.  
  212. function getHouseEntry(houseId)
  213.     return getHouseInfo(houseId).entry
  214. end
  215.  
  216. function getHouseRent(houseId)
  217.     return getHouseInfo(houseId).rent
  218. end
  219.  
  220. function getHousePrice(houseId)
  221.     return getHouseInfo(houseId).price
  222. end
  223.  
  224. function getHouseTown(houseId)
  225.     return getHouseInfo(houseId).town
  226. end
  227.  
  228. function getHouseDoorsCount(houseId)
  229.     return table.maxn(getHouseInfo(houseId).doors)
  230. end
  231.  
  232. function getHouseBedsCount(houseId)
  233.     return table.maxn(getHouseInfo(houseId).beds)
  234. end
  235.  
  236. function getHouseTilesCount(houseId)
  237.     return table.maxn(getHouseInfo(houseId).tiles)
  238. end
  239.  
  240. function getItemNameById(itemid)
  241.     return getItemDescriptionsById(itemid).name
  242. end
  243.  
  244. function getItemPluralNameById(itemid)
  245.     return getItemDescriptionsById(itemid).plural
  246. end
  247.  
  248. function getItemArticleById(itemid)
  249.     return getItemDescriptionsById(itemid).article
  250. end
  251.  
  252. function getItemName(uid)
  253.     return getItemDescriptions(uid).name
  254. end
  255.  
  256. function getItemPluralName(uid)
  257.     return getItemDescriptions(uid).plural
  258. end
  259.  
  260. function getItemArticle(uid)
  261.     return getItemDescriptions(uid).article
  262. end
  263.  
  264. function getItemText(uid)
  265.     return getItemDescriptions(uid).text
  266. end
  267.  
  268. function getItemSpecialDescription(uid)
  269.     return getItemDescriptions(uid).special
  270. end
  271.  
  272. function doSetItemSpecialDescription(uid, str)
  273.     return doItemSetAttribute(uid, "description", str)
  274. end
  275.  
  276. function getItemWriter(uid)
  277.     return getItemDescriptions(uid).writer
  278. end
  279.  
  280. function getItemDate(uid)
  281.     return getItemDescriptions(uid).date
  282. end
  283.  
  284. function getTilePzInfo(pos)
  285.     return getTileInfo(pos).protection
  286. end
  287.  
  288. function getTileZoneInfo(pos)
  289.     local tmp = getTileInfo(pos)
  290.     if(tmp.pvp) then
  291.         return 2
  292.     end
  293.  
  294.     if(tmp.nopvp) then
  295.         return 1
  296.     end
  297.  
  298.     return 0
  299. end
  300.  
  301. function doShutdown()
  302.     return doSetGameState(GAMESTATE_SHUTDOWN)
  303. end
  304.  
  305. function doSummonCreature(name, pos, displayError)
  306.     local displayError, cid = displayError or true, doCreateMonster(name, pos, false, false, displayError)
  307.     if(not cid) then
  308.         cid = doCreateNpc(name, pos, displayError)
  309.     end
  310.  
  311.     return cid
  312. end
  313.  
  314. function getOnlinePlayers()
  315.     local players = {}
  316.     for i, cid in ipairs(getPlayersOnline()) do
  317.         table.insert(players, getCreatureName(cid))
  318.     end
  319.  
  320.     return players
  321. end
  322.  
  323. function getPlayerByName(name)
  324.     local cid = getCreatureByName(name)
  325.     return isPlayer(cid) and cid or nil
  326. end
  327.  
  328. function isPlayer(cid)
  329.     return isCreature(cid) and cid >= AUTOID_PLAYERS and cid < AUTOID_MONSTERS
  330. end
  331.  
  332. function isPlayerGhost(cid)
  333.     return isPlayer(cid) and (getCreatureCondition(cid, CONDITION_GAMEMASTER, GAMEMASTER_INVISIBLE) or getPlayerFlagValue(cid, PLAYERFLAG_CANNOTBESEEN))
  334. end
  335.  
  336. function isMonster(cid)
  337.     return isCreature(cid) and cid >= AUTOID_MONSTERS and cid < AUTOID_NPCS
  338. end
  339.  
  340. function isNpc(cid)
  341.     return isCreature(cid) and cid >= AUTOID_NPCS
  342. end
  343.  
  344. function doPlayerSetExperienceRate(cid, value)
  345.     return doPlayerSetRate(cid, SKILL__LEVEL, value)
  346. end
  347.  
  348. function doPlayerSetMagicRate(cid, value)
  349.     return doPlayerSetRate(cid, SKILL__MAGLEVEL, value)
  350. end
  351.  
  352. function doPlayerAddLevel(cid, amount, round)
  353.     local experience, level, amount = 0, getPlayerLevel(cid), amount or 1
  354.     if(amount > 0) then
  355.         experience = getExperienceForLevel(level + amount) - (round and getPlayerExperience(cid) or getExperienceForLevel(level))
  356.     else
  357.         experience = -((round and getPlayerExperience(cid) or getExperienceForLevel(level)) - getExperienceForLevel(level + amount))
  358.     end
  359.  
  360.     return doPlayerAddExperience(cid, experience)
  361. end
  362.  
  363. function doPlayerAddMagLevel(cid, amount)
  364.     local amount = amount or 1
  365.     for i = 1, amount do
  366.         doPlayerAddSpentMana(cid, getPlayerRequiredMana(cid, getPlayerMagLevel(cid, true) + 1) - getPlayerSpentMana(cid), false)
  367.     end
  368.  
  369.     return true
  370. end
  371.  
  372. function doPlayerAddSkill(cid, skill, amount, round)
  373.     local amount = amount or 1
  374.     if(skill == SKILL__LEVEL) then
  375.         return doPlayerAddLevel(cid, amount, round)
  376.     elseif(skill == SKILL__MAGLEVEL) then
  377.         return doPlayerAddMagLevel(cid, amount)
  378.     end
  379.  
  380.     for i = 1, amount do
  381.         doPlayerAddSkillTry(cid, skill, getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill), false)
  382.     end
  383.  
  384.     return true
  385. end
  386.  
  387. function getPartyLeader(cid)
  388.     local party = getPartyMembers(cid)
  389.     if(type(party) ~= 'table') then
  390.         return 0
  391.     end
  392.  
  393.     return party[1]
  394. end
  395.  
  396. function isInParty(cid)
  397.     return type(getPartyMembers(cid)) == 'table'
  398. end
  399.  
  400. function isPrivateChannel(channelId)
  401.     return channelId >= CHANNEL_PRIVATE
  402. end
  403.  
  404. function doPlayerResetIdleTime(cid)
  405.     return doPlayerSetIdleTime(cid, 0)
  406. end
  407.  
  408. function doBroadcastMessage(text, class)
  409.     local class = class or MESSAGE_STATUS_WARNING
  410.     if(type(class) == 'string') then
  411.         local className = MESSAGE_TYPES[class]
  412.         if(className == nil) then
  413.             return false
  414.         end
  415.  
  416.         class = className
  417.     elseif(class < MESSAGE_FIRST or class > MESSAGE_LAST) then
  418.         return false
  419.     end
  420.  
  421.     for _, pid in ipairs(getPlayersOnline()) do
  422.         doPlayerSendTextMessage(pid, class, text)
  423.     end
  424.  
  425.     print("> Broadcasted message: \"" .. text .. "\".")
  426.     return true
  427. end
  428.  
  429. function doPlayerBroadcastMessage(cid, text, class, checkFlag, ghost)
  430.     local checkFlag, ghost, class = checkFlag or true, ghost or false, class or TALKTYPE_BROADCAST
  431.     if(checkFlag and not getPlayerFlagValue(cid, PLAYERFLAG_CANBROADCAST)) then
  432.         return false
  433.     end
  434.  
  435.     if(type(class) == 'string') then
  436.         local className = TALKTYPE_TYPES[class]
  437.         if(className == nil) then
  438.             return false
  439.         end
  440.  
  441.         class = className
  442.     elseif(class < TALKTYPE_FIRST or class > TALKTYPE_LAST) then
  443.         return false
  444.     end
  445.  
  446.     for _, pid in ipairs(getPlayersOnline()) do
  447.         doCreatureSay(cid, text, class, ghost, pid)
  448.     end
  449.  
  450.     print("> " .. getCreatureName(cid) .. " broadcasted message: \"" .. text .. "\".")
  451.     return true
  452. end
  453.  
  454. function getBooleanFromString(input)
  455.     local tmp = type(input)
  456.     if(tmp == 'boolean') then
  457.         return input
  458.     end
  459.  
  460.     if(tmp == 'number') then
  461.         return input > 0
  462.     end
  463.  
  464.     local str = string.lower(tostring(input))
  465.     return (str == "yes" or str == "true" or (tonumber(str) ~= nil and tonumber(str) > 0))
  466. end
  467.  
  468. function doCopyItem(item, attributes)
  469.     local attributes = ((type(attributes) == 'table') and attributes or { "aid" })
  470.  
  471.     local ret = doCreateItemEx(item.itemid, item.type)
  472.     for _, key in ipairs(attributes) do
  473.         local value = getItemAttribute(item.uid, key)
  474.         if(value ~= nil) then
  475.             doItemSetAttribute(ret, key, value)
  476.         end
  477.     end
  478.  
  479.     if(isContainer(item.uid)) then
  480.         for i = (getContainerSize(item.uid) - 1), 0, -1 do
  481.             local tmp = getContainerItem(item.uid, i)
  482.             if(tmp.itemid > 0) then
  483.                 doAddContainerItemEx(ret, doCopyItem(tmp, true).uid)
  484.             end
  485.         end
  486.     end
  487.  
  488.     return getThing(ret)
  489. end
  490.  
  491. function doRemoveThing(uid)
  492.     if(isCreature(uid)) then
  493.         return doRemoveCreature(uid)
  494.     end
  495.  
  496.     return doRemoveItem(uid)
  497. end
  498.  
  499. function setAttackFormula(combat, type, minl, maxl, minm, maxm, min, max)
  500.     local min, max = min or 0, max or 0
  501.     return setCombatFormula(combat, type, -1, 0, -1, 0, minl, maxl, minm, maxm, -min, -max)
  502. end
  503.  
  504. function setHealingFormula(combat, type, minl, maxl, minm, maxm, min, max)
  505.     local min, max = min or 0, max or 0
  506.     return setCombatFormula(combat, type, 1, 0, 1, 0, minl, maxl, minm, maxm, min, max)
  507. end
  508.  
  509. function doChangeTypeItem(uid, subtype)
  510.     local thing = getThing(uid)
  511.     if(thing.itemid < 100) then
  512.         return false
  513.     end
  514.  
  515.     local subtype = subtype or 1
  516.     return doTransformItem(thing.uid, thing.itemid, subtype)
  517. end
  518.  
  519. function doSetItemText(uid, text, writer, date)
  520.     local thing = getThing(uid)
  521.     if(thing.itemid < 100) then
  522.         return false
  523.     end
  524.  
  525.     doItemSetAttribute(uid, "text", text)
  526.     if(writer ~= nil) then
  527.         doItemSetAttribute(uid, "writer", tostring(writer))
  528.         if(date ~= nil) then
  529.             doItemSetAttribute(uid, "date", tonumber(date))
  530.         end
  531.     end
  532.  
  533.     return true
  534. end
  535.  
  536. function doItemSetActionId(uid, aid)
  537.     return doItemSetAttribute(uid, "aid", aid)
  538. end
  539.  
  540. function getFluidSourceType(itemid)
  541.     local item = getItemInfo(itemid)
  542.     return item and item.fluidSource or false
  543. end
  544.  
  545. function getDepotId(uid)
  546.     return getItemAttribute(uid, "depotid") or false
  547. end
  548.  
  549. function getItemDescriptions(uid)
  550.     local thing = getThing(uid)
  551.     if(thing.itemid < 100) then
  552.         return false
  553.     end
  554.  
  555.     local item = getItemInfo(thing.itemid)
  556.     return {
  557.         name = getItemAttribute(uid, "name") or item.name,
  558.         plural = getItemAttribute(uid, "pluralname") or item.plural,
  559.         article = getItemAttribute(uid, "article") or item.article,
  560.         special = getItemAttribute(uid, "description") or "",
  561.         text = getItemAttribute(uid, "text") or "",
  562.         writer = getItemAttribute(uid, "writer") or "",
  563.         date = getItemAttribute(uid, "date") or 0
  564.     }
  565. end
  566.  
  567. function getItemWeightById(itemid, count, precision)
  568.     local item, count, precision = getItemInfo(itemid), count or 1, precision or false
  569.     if(not item) then
  570.         return false
  571.     end
  572.  
  573.     if(count > 100) then
  574.         -- print a warning, as its impossible to have more than 100 stackable items without "cheating" the count
  575.         print('[Warning] getItemWeightById', 'Calculating weight for more than 100 items!')
  576.     end
  577.  
  578.     local weight = item.weight * count
  579.     --[[if(precision) then
  580.         return weight
  581.     end
  582.  
  583.     local t = string.explode(tostring(weight), ".")
  584.     if(table.maxn(t) == 2) then
  585.         return tonumber(t[1] .. "." .. string.sub(t[2], 1, 2))
  586.     end]]--
  587.  
  588.     return weight
  589. end
  590.  
  591. function getItemWeaponType(uid)
  592.     local thing = getThing(uid)
  593.     if(thing.itemid < 100) then
  594.         return false
  595.     end
  596.  
  597.     return getItemInfo(thing.itemid).weaponType
  598. end
  599.  
  600. function getItemRWInfo(uid)
  601.     local thing = getThing(uid)
  602.     if(thing.itemid < 100) then
  603.         return false
  604.     end
  605.  
  606.     local item, flags = getItemInfo(thing.itemid), 0
  607.     if(item.readable) then
  608.         flags = 1
  609.     end
  610.  
  611.     if(item.writable) then
  612.         flags = flags + 2
  613.     end
  614.  
  615.     return flags
  616. end
  617.  
  618. function getItemLevelDoor(itemid)
  619.     local item = getItemInfo(itemid)
  620.     return item and item.levelDoor or false
  621. end
  622.  
  623. function isContainer(uid)
  624.     local thing = getThing(uid)
  625.     return thing.uid > 0 and thing.items ~= nil
  626. end
  627.  
  628. function isItemStackable(itemid)
  629.     local item = getItemInfo(itemid)
  630.     return item and item.stackable or false
  631. end
  632.  
  633. function isItemRune(itemid)
  634.     local item = getItemInfo(itemid)
  635.     return item and item.type == ITEM_TYPE_RUNE or false
  636. end
  637.  
  638. function isItemDoor(itemid)
  639.     local item = getItemInfo(itemid)
  640.     return item and item.type == ITEM_TYPE_DOOR or false
  641. end
  642.  
  643. function isItemContainer(itemid)
  644.     local item = getItemInfo(itemid)
  645.     return item and item.group == ITEM_GROUP_CONTAINER or false
  646. end
  647.  
  648. function isItemFluidContainer(itemid)
  649.     local item = getItemInfo(itemid)
  650.     return item and item.group == ITEM_GROUP_FLUID or false
  651. end
  652.  
  653. function isItemMovable(itemid)
  654.     local item = getItemInfo(itemid)
  655.     return item and item.movable or false
  656. end
  657.  
  658. function isCorpse(uid)
  659.     local thing = getThing(uid)
  660.     if(thing.itemid < 100) then
  661.         return false
  662.     end
  663.  
  664.     local item = getItemInfo(thing.itemid)
  665.     return item and item.corpseType ~= 0 or false
  666. end
  667.  
  668. function getContainerCapById(itemid)
  669.     local item = getItemInfo(itemid)
  670.     if(not item or item.group ~= 2) then
  671.         return false
  672.     end
  673.  
  674.     return item.maxItems
  675. end
  676.  
  677. function getMonsterAttackSpells(name)
  678.     local monster = getMonsterInfo(name)
  679.     return monster and monster.attacks or false
  680. end
  681.  
  682. function getMonsterHealingSpells(name)
  683.     local monster = getMonsterInfo(name)
  684.     return monster and monster.defenses or false
  685. end
  686.  
  687. function getMonsterLootList(name)
  688.     local monster = getMonsterInfo(name)
  689.     return monster and monster.loot or false
  690. end
  691.  
  692. function getMonsterSummonList(name)
  693.     local monster = getMonsterInfo(name)
  694.     return monster and monster.summons or false
  695. end
  696.  
  697. function choose(...)
  698.     local arg = {...}
  699.     return arg[math.random(1, table.maxn(arg))]
  700. end
  701.  
  702. function doSendAnimatedText(...)
  703.     error("doSendAnimatedText is now a deprecated function.")
  704. end
  705.  
  706. --Guild stuff
  707. function setGuildLevel(guild,lvl)
  708.     return db.executeQuery("UPDATE `guilds` SET `Level` = ".. lvl.." WHERE `id` = ".. guild .. " LIMIT 1;")
  709. end
  710.  
  711. function getGuildLevel(guild)
  712.     local res = db.getResult("SELECT `Level` FROM `guilds` WHERE `id` = ".. guild .. " LIMIT 1;")
  713.     local value = 0
  714.     if(res:getID() ~= -1) then
  715.         value = res:getDataInt("Level")
  716.         res:free()
  717.     end
  718.     return value
  719. end
  720.  
  721. function addGuildExp(guild,exp)
  722.     local curLevel =getGuildLevel(guild)
  723.     local nextLevel =curLevel+1
  724.     local nextExp =getGuildExpForLevel(nextLevel)
  725.     local curExp =getGuildExp(guild)   
  726.     local maxLevel = tonumber(getConfigValue('guildMaxLevel'))
  727.     local maxExp  =  getGuildExpForLevel(maxLevel)
  728.     local newExp = (curExp + exp ) > maxExp and math.abs((maxExp - exp) - exp) or exp
  729.     local levelAdd = false
  730.    
  731.     if nextLevel == maxLevel then return false end
  732.         db.executeQuery("UPDATE `guilds` SET `Experience` = Experience + ".. newExp.." WHERE `id` = ".. guild .. " LIMIT 1;")
  733.    
  734.     local newExpp = getGuildExp(guild)
  735.     if newExpp >= nextExp then
  736.         setGuildLevel(guild,nextLevel)
  737.         levelAdd =true
  738.     end
  739.     local strSame = "Guild system : your guild gained "..newExp.." experience points at level ".. getGuildLevel(guild)..". Needs "..getGuildExpForLevel(getGuildLevel(guild)+1) - getGuildExp(guild).." exp to next level!"
  740.     local strNew = "Guild system : your guild gained "..newExp.." experience points and advanced to level "..getGuildLevel(guild)..". Needs "..getGuildExpForLevel(getGuildLevel(guild)+1)  - getGuildExp(guild).." exp to next level!"
  741.    
  742.     for _,cid in ipairs(getPlayersOnline()) do
  743.         if getPlayerGuildId(cid) == guild then
  744.             doPlayerSendChannelMessage(cid,"", levelAdd and strNew or strSame, (levelAdd and TALKTYPE_GAMEMASTER_CHANNEL or TALKTYPE_CHANNEL_HIGHLIGHT), CHANNEL_GUILD)
  745.             doPlayerSendTextMessage(cid,(levelAdd and MESSAGE_INFO_DESCR or MESSAGE_EVENT_ORANGE),levelAdd and strNew or strSame)
  746.         end
  747.     end
  748.    
  749.     return true
  750. end
  751.  
  752. function getGuildExpForLevel(lvl)
  753.     local base = tonumber(getConfigValue('guildBaseExp'))
  754.     if lvl == 1 then return base end
  755.     return math.ceil(((base* lvl * lvl * lvl) + ((base-350) * lvl * lvl) + ((base-200) * lvl)) / 3)
  756. end
  757.  
  758. function getGuildExp(guild)
  759.     local res = db.getResult("SELECT `Experience` FROM `guilds` WHERE `id` = ".. guild .. " LIMIT 1;")
  760.     local value = 0
  761.     if(res:getID() ~= -1) then
  762.         value = res:getDataInt("Experience")
  763.         res:free()
  764.     end
  765.     return value
  766. end
  767.  
  768. function isGuild(guild)
  769.     local res = db.getResult("SELECT `Experience` FROM `guilds` WHERE `id` = ".. guild .. " LIMIT 1;")
  770.     local value = false
  771.     if(res:getID() ~= -1) then
  772.         value = true
  773.         res:free()
  774.     end
  775.  
  776.     return value
  777. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement