Advertisement
Sekk

Untitled

Dec 5th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.83 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 isPremium(cid)
  87. return (isPlayer(cid) and (getPlayerPremiumDays(cid) > 0 or getBooleanFromString(getConfigInfo('freePremium'))))
  88. end
  89.  
  90. function getMonthDayEnding(day)
  91. if(day == "01" or day == "21" or day == "31") then
  92. return "st"
  93. elseif(day == "02" or day == "22") then
  94. return "nd"
  95. elseif(day == "03" or day == "23") then
  96. return "rd"
  97. end
  98.  
  99. return "th"
  100. end
  101.  
  102. function getMonthString(m)
  103. return os.date("%B", os.time{year = 1970, month = m, day = 1})
  104. end
  105.  
  106. function getArticle(str)
  107. return str:find("[AaEeIiOoUuYy]") == 1 and "an" or "a"
  108. end
  109.  
  110. function isNumber(str)
  111. return tonumber(str) ~= nil
  112. end
  113.  
  114. function doPlayerAddAddons(cid, addon)
  115. for i = 0, table.maxn(maleOutfits) do
  116. doPlayerAddOutfit(cid, maleOutfits[i], addon)
  117. end
  118.  
  119. for i = 0, table.maxn(femaleOutfits) do
  120. doPlayerAddOutfit(cid, femaleOutfits[i], addon)
  121. end
  122. end
  123.  
  124. function doPlayerWithdrawAllMoney(cid)
  125. return doPlayerWithdrawMoney(cid, getPlayerBalance(cid))
  126. end
  127.  
  128. function doPlayerDepositAllMoney(cid)
  129. return doPlayerDepositMoney(cid, getPlayerMoney(cid))
  130. end
  131.  
  132. function doPlayerTransferAllMoneyTo(cid, target)
  133. return doPlayerTransferMoneyTo(cid, target, getPlayerBalance(cid))
  134. end
  135.  
  136. function playerExists(name)
  137. return getPlayerGUIDByName(name) ~= 0
  138. end
  139.  
  140. function getTibiaTime()
  141. local minutes = getWorldTime()
  142. local hours = 0
  143. while (minutes > 60) do
  144. hours = hours + 1
  145. minutes = minutes - 60
  146. end
  147.  
  148. return {hours = hours, minutes = minutes}
  149. end
  150.  
  151. function doWriteLogFile(file, text)
  152. local f = io.open(file, "a+")
  153. if(not f) then
  154. return false
  155. end
  156.  
  157. f:write("[" .. os.date("%d/%m/%Y %H:%M:%S") .. "] " .. text .. "\n")
  158. f:close()
  159. return true
  160. end
  161.  
  162. function getExperienceForLevel(lv)
  163. lv = lv - 1
  164. return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
  165. end
  166.  
  167. function doMutePlayer(cid, time)
  168. local condition = createConditionObject(CONDITION_MUTED)
  169. setConditionParam(condition, CONDITION_PARAM_TICKS, time * 1000)
  170. return doAddCondition(cid, condition)
  171. end
  172.  
  173. function getPlayerGroupName(cid)
  174. return getGroupInfo(getPlayerGroupId(cid)).name
  175. end
  176.  
  177. function getPlayerVocationName(cid)
  178. return getVocationInfo(getPlayerVocation(cid)).name
  179. end
  180.  
  181. function getPromotedVocation(vid)
  182. return getVocationInfo(vid).promotedVocation
  183. end
  184.  
  185. function doPlayerRemovePremiumDays(cid, days)
  186. return doPlayerAddPremiumDays(cid, -days)
  187. end
  188.  
  189. function getPlayerMasterPos(cid)
  190. return getTownTemplePosition(getPlayerTown(cid))
  191. end
  192.  
  193. function getHouseOwner(houseId)
  194. return getHouseInfo(houseId).owner
  195. end
  196.  
  197. function getHouseName(houseId)
  198. return getHouseInfo(houseId).name
  199. end
  200.  
  201. function getHouseEntry(houseId)
  202. return getHouseInfo(houseId).entry
  203. end
  204.  
  205. function getHouseRent(houseId)
  206. return getHouseInfo(houseId).rent
  207. end
  208.  
  209. function getHousePrice(houseId)
  210. return getHouseInfo(houseId).price
  211. end
  212.  
  213. function getHouseTown(houseId)
  214. return getHouseInfo(houseId).town
  215. end
  216.  
  217. function getHouseTilesCount(houseId)
  218. return getHouseInfo(houseId).tiles
  219. end
  220.  
  221. function getItemNameById(itemid)
  222. return getItemDescriptionsById(itemid).name
  223. end
  224.  
  225. function getItemPluralNameById(itemid)
  226. return getItemDescriptionsById(itemid).plural
  227. end
  228.  
  229. function getItemArticleById(itemid)
  230. return getItemDescriptionsById(itemid).article
  231. end
  232.  
  233. function getItemName(uid)
  234. return getItemDescriptions(uid).name
  235. end
  236.  
  237. function getItemPluralName(uid)
  238. return getItemDescriptions(uid).plural
  239. end
  240.  
  241. function getItemArticle(uid)
  242. return getItemDescriptions(uid).article
  243. end
  244.  
  245. function getItemText(uid)
  246. return getItemDescriptions(uid).text
  247. end
  248.  
  249. function getItemSpecialDescription(uid)
  250. return getItemDescriptions(uid).special
  251. end
  252.  
  253. function getItemWriter(uid)
  254. return getItemDescriptions(uid).writer
  255. end
  256.  
  257. function getItemDate(uid)
  258. return getItemDescriptions(uid).date
  259. end
  260.  
  261. function getTilePzInfo(pos)
  262. return getTileInfo(pos).protection
  263. end
  264.  
  265. function getTileZoneInfo(pos)
  266. local tmp = getTileInfo(pos)
  267. if(tmp.pvp) then
  268. return 2
  269. end
  270.  
  271. if(tmp.nopvp) then
  272. return 1
  273. end
  274.  
  275. return 0
  276. end
  277.  
  278. function doShutdown()
  279. return doSetGameState(GAMESTATE_SHUTDOWN)
  280. end
  281.  
  282. function doSummonCreature(name, pos, displayError)
  283. local displayError, cid = displayError or true, doCreateMonster(name, pos, displayError)
  284. if(not cid) then
  285. cid = doCreateNpc(name, pos, displayError)
  286. end
  287.  
  288. return cid
  289. end
  290.  
  291. function getOnlinePlayers()
  292. local tmp = getPlayersOnline()
  293. local players = {}
  294. for i, cid in ipairs(tmp) do
  295. table.insert(players, getCreatureName(cid))
  296. end
  297.  
  298. return players
  299. end
  300.  
  301. function getPlayerByName(name)
  302. local cid = getCreatureByName(name)
  303. return isPlayer(cid) and cid or nil
  304. end
  305.  
  306. function isPlayer(cid)
  307. return isCreature(cid) and cid >= AUTOID_PLAYERS and cid < AUTOID_MONSTERS
  308. end
  309.  
  310. function isPlayerGhost(cid)
  311. if(not isPlayer(cid)) then
  312. return false
  313. end
  314.  
  315. return getCreatureCondition(cid, CONDITION_GAMEMASTER, GAMEMASTER_INVISIBLE) or getPlayerFlagValue(cid, PLAYERFLAG_CANNOTBESEEN)
  316. end
  317.  
  318. function isMonster(cid)
  319. return isCreature(cid) and cid >= AUTOID_MONSTERS and cid < AUTOID_NPCS
  320. end
  321.  
  322. function isNpc(cid)
  323. return isCreature(cid) and cid >= AUTOID_NPCS
  324. end
  325.  
  326. function doPlayerSetExperienceRate(cid, value)
  327. return doPlayerSetRate(cid, SKILL__LEVEL, value)
  328. end
  329.  
  330. function doPlayerSetMagicRate(cid, value)
  331. return doPlayerSetRate(cid, SKILL__MAGLEVEL, value)
  332. end
  333.  
  334. function doPlayerAddLevel(cid, amount, round)
  335. local experience, level = 0, getPlayerLevel(cid)
  336. if(amount > 0) then
  337. experience = getExperienceForLevel(level + amount) - (round and getPlayerExperience(cid) or getExperienceForLevel(level))
  338. else
  339. experience = -((round and getPlayerExperience(cid) or getExperienceForLevel(level)) - getExperienceForLevel(level + amount))
  340. end
  341.  
  342. return doPlayerAddExperience(cid, experience)
  343. end
  344.  
  345. function doPlayerAddMagLevel(cid, amount)
  346. for i = 1, amount do
  347. doPlayerAddSpentMana(cid, (getPlayerRequiredMana(cid, getPlayerMagLevel(cid, true) + 1) - getPlayerSpentMana(cid)) / getConfigInfo('rateMagic'))
  348. end
  349. return true
  350. end
  351.  
  352. function doPlayerAddSkill(cid, skill, amount, round)
  353. local amount = amount or 1
  354. if(skill == SKILL__LEVEL) then
  355. return doPlayerAddLevel(cid, amount, round)
  356. elseif(skill == SKILL__MAGLEVEL) then
  357. return doPlayerAddMagLevel(cid, amount)
  358. end
  359.  
  360. for i = 1, amount do
  361. doPlayerAddSkillTry(cid, skill, getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill), false)
  362. end
  363.  
  364. return true
  365. end
  366.  
  367.  
  368. function getPartyLeader(cid)
  369. local party = getPartyMembers(cid)
  370. if(type(party) ~= 'table') then
  371. return 0
  372. end
  373.  
  374. return party[1]
  375. end
  376.  
  377. function isInParty(cid)
  378. return type(getPartyMembers(cid)) == 'table'
  379. end
  380.  
  381. function isPrivateChannel(channelId)
  382. return channelId >= CHANNEL_PRIVATE
  383. end
  384.  
  385. function doPlayerResetIdleTime(cid)
  386. return doPlayerSetIdleTime(cid, 0)
  387. end
  388.  
  389. function doBroadcastMessage(text, class)
  390. local class = class or MESSAGE_STATUS_WARNING
  391. if(type(class) == 'string') then
  392. local className = MESSAGE_TYPES[class]
  393. if(className == nil) then
  394. return false
  395. end
  396.  
  397. class = className
  398. elseif(class < MESSAGE_FIRST or class > MESSAGE_LAST) then
  399. return false
  400. end
  401.  
  402. local players = getPlayersOnline()
  403. for _, pid in ipairs(players) do
  404. doPlayerSendTextMessage(pid, class, text)
  405. end
  406.  
  407. print("> Broadcasted message: \"" .. text .. "\".")
  408. return true
  409. end
  410.  
  411. function doPlayerBroadcastMessage(cid, text, class, checkFlag, ghost)
  412. local checkFlag, ghost, class = checkFlag or true, ghost or false, class or TALKTYPE_BROADCAST
  413. if(checkFlag and not getPlayerFlagValue(cid, PLAYERFLAG_CANBROADCAST)) then
  414. return false
  415. end
  416.  
  417. if(type(class) == 'string') then
  418. local className = TALKTYPE_TYPES[class]
  419. if(className == nil) then
  420. return false
  421. end
  422.  
  423. class = className
  424. elseif(class < TALKTYPE_FIRST or class > TALKTYPE_LAST) then
  425. return false
  426. end
  427.  
  428. local players = getPlayersOnline()
  429. for _, pid in ipairs(players) do
  430. doCreatureSay(cid, text, class, ghost, pid)
  431. end
  432.  
  433. print("> " .. getCreatureName(cid) .. " broadcasted message: \"" .. text .. "\".")
  434. return true
  435. end
  436.  
  437. function getBooleanFromString(input)
  438. local tmp = type(input)
  439. if(tmp == 'boolean') then
  440. return input
  441. end
  442.  
  443. if(tmp == 'number') then
  444. return input > 0
  445. end
  446.  
  447. local str = string.lower(tostring(input))
  448. return (str == "yes" or str == "true" or (tonumber(str) ~= nil and tonumber(str) > 0))
  449. end
  450.  
  451. function doCopyItem(item, attributes)
  452. local attributes = attributes or false
  453.  
  454. local ret = doCreateItemEx(item.itemid, item.type)
  455. if(attributes) then
  456. if(item.actionid > 0) then
  457. doItemSetAttribute(ret, "aid", item.actionid)
  458. end
  459. end
  460.  
  461. if(isContainer(item.uid)) then
  462. for i = (getContainerSize(item.uid) - 1), 0, -1 do
  463. local tmp = getContainerItem(item.uid, i)
  464. if(tmp.itemid > 0) then
  465. doAddContainerItemEx(ret, doCopyItem(tmp, true).uid)
  466. end
  467. end
  468. end
  469.  
  470. return getThing(ret)
  471. end
  472.  
  473. function doRemoveThing(uid)
  474. if(isCreature(uid)) then
  475. return doRemoveCreature(uid)
  476. end
  477.  
  478. return doRemoveItem(uid)
  479. end
  480.  
  481. function setAttackFormula(combat, type, minl, maxl, minm, maxm, min, max)
  482. local min, max = min or 0, max or 0
  483. return setCombatFormula(combat, type, -1, 0, -1, 0, minl, maxl, minm, maxm, min, max)
  484. end
  485.  
  486. function setHealingFormula(combat, type, minl, maxl, minm, maxm, min, max)
  487. local min, max = min or 0, max or 0
  488. return setCombatFormula(combat, type, 1, 0, 1, 0, minl, maxl, minm, maxm, min, max)
  489. end
  490.  
  491. function doChangeTypeItem(uid, subtype)
  492. local thing = getThing(uid)
  493. if(thing.itemid < 100) then
  494. return false
  495. end
  496.  
  497. local subtype = subtype or 1
  498. return doTransformItem(thing.uid, thing.itemid, subtype)
  499. end
  500.  
  501. function doSetItemText(uid, text, writer, date)
  502. local thing = getThing(uid)
  503. if(thing.itemid < 100) then
  504. return false
  505. end
  506.  
  507. doItemSetAttribute(uid, "text", text)
  508. if(writer ~= nil) then
  509. doItemSetAttribute(uid, "writer", tostring(writer))
  510. if(date ~= nil) then
  511. doItemSetAttribute(uid, "date", tonumber(date))
  512. end
  513. end
  514.  
  515. return true
  516. end
  517.  
  518. function getFluidSourceType(itemid)
  519. local item = getItemInfo(itemid)
  520. return item and item.fluidSource or false
  521. end
  522.  
  523. function getDepotId(uid)
  524. return getItemAttribute(uid, "depotid") or false
  525. end
  526.  
  527. function getItemDescriptions(uid)
  528. local thing = getThing(uid)
  529. if(thing.itemid < 100) then
  530. return false
  531. end
  532.  
  533. local item = getItemInfo(thing.itemid)
  534. return {
  535. name = getItemAttribute(uid, "name") or item.name,
  536. plural = getItemAttribute(uid, "pluralname") or item.plural,
  537. article = getItemAttribute(uid, "article") or item.article,
  538. special = getItemAttribute(uid, "description") or "",
  539. text = getItemAttribute(uid, "text") or "",
  540. writer = getItemAttribute(uid, "writer") or "",
  541. date = getItemAttribute(uid, "date") or 0
  542. }
  543. end
  544.  
  545. function getItemWeightById(itemid, count, precision)
  546. local item, count, precision = getItemInfo(itemid), count or 1, precision or false
  547. if(not item) then
  548. return false
  549. end
  550.  
  551. if(count > 100) then
  552. -- print a warning, as its impossible to have more than 100 stackable items without "cheating" the count
  553. print('[Warning] getItemWeightById', 'Calculating weight for more than 100 items!')
  554. end
  555.  
  556. local weight = item.weight * count
  557. --[[if(precision) then
  558. return weight
  559. end
  560.  
  561. local t = string.explode(tostring(weight), ".")
  562. if(table.maxn(t) == 2) then
  563. return tonumber(t[1] .. "." .. string.sub(t[2], 1, 2))
  564. end]]--
  565.  
  566. return weight
  567. end
  568.  
  569. function getItemWeaponType(uid)
  570. local thing = getThing(uid)
  571. if(thing.itemid < 100) then
  572. return false
  573. end
  574.  
  575. return getItemInfo(thing.itemid).weaponType
  576. end
  577.  
  578. function getItemRWInfo(uid)
  579. local thing = getThing(uid)
  580. if(thing.itemid < 100) then
  581. return false
  582. end
  583.  
  584. local item, flags = getItemInfo(thing.itemid), 0
  585. if(item.readable) then
  586. flags = 1
  587. end
  588.  
  589. if(item.writable) then
  590. flags = flags + 2
  591. end
  592.  
  593. return flags
  594. end
  595.  
  596. function getItemLevelDoor(itemid)
  597. local item = getItemInfo(itemid)
  598. return item and item.levelDoor or false
  599. end
  600.  
  601. function isItemStackable(itemid)
  602. local item = getItemInfo(itemid)
  603. return item and item.stackable or false
  604. end
  605.  
  606. function isItemRune(itemid)
  607. local item = getItemInfo(itemid)
  608. return item and item.clientCharges or false
  609. end
  610.  
  611. function isItemDoor(itemid)
  612. local item = getItemInfo(itemid)
  613. return item and item.type == 5 or false
  614. end
  615.  
  616. function isItemContainer(itemid)
  617. local item = getItemInfo(itemid)
  618. return item and item.group == 2 or false
  619. end
  620.  
  621. function isItemFluidContainer(itemid)
  622. local item = getItemInfo(itemid)
  623. return item and item.group == 12 or false
  624. end
  625.  
  626. function isItemMovable(itemid)
  627. local item = getItemInfo(itemid)
  628. return item and item.movable or false
  629. end
  630.  
  631. function isCorpse(uid)
  632. local thing = getThing(uid)
  633. if(thing.itemid < 100) then
  634. return false
  635. end
  636.  
  637. local item = getItemInfo(thing.itemid)
  638. return item and item.corpseType ~= 0 or false
  639. end
  640.  
  641. function getContainerCapById(itemid)
  642. local item = getItemInfo(itemid)
  643. if(not item or item.group ~= 2) then
  644. return false
  645. end
  646.  
  647. return item.maxItems
  648. end
  649.  
  650. function getMonsterAttackSpells(name)
  651. local monster = getMonsterInfo(name)
  652. return monster and monster.attacks or false
  653. end
  654.  
  655. function getMonsterHealingSpells(name)
  656. local monster = getMonsterInfo(name)
  657. return monster and monster.defenses or false
  658. end
  659.  
  660. function getMonsterLootList(name)
  661. local monster = getMonsterInfo(name)
  662. return monster and monster.loot or false
  663. end
  664.  
  665. function getMonsterSummonList(name)
  666. local monster = getMonsterInfo(name)
  667. return monster and monster.summons or false
  668. end
  669.  
  670. function getPlayerMarriage(player)
  671. local rows = db.getResult("SELECT `marriage` FROM `players` WHERE `id` = " .. player .. ";")
  672. local marry = rows:getDataInt("marriage")
  673. if marry ~= 0 then
  674. return marry
  675. else
  676. return FALSE
  677. end
  678. end
  679.  
  680. function addMarryStatus(player,partner)
  681. db.Query("UPDATE `players` SET `marrystatus` = " .. partner .. " WHERE `id` = " .. player .. ";")
  682. return TRUE
  683. end
  684.  
  685. function doCancelMarryStatus(player)
  686. db.Query("UPDATE `players` SET `marrystatus` = 0 WHERE `id` = " .. player .. ";")
  687. return TRUE
  688. end
  689.  
  690. function getMarryStatus(player)
  691. local stat = db.getResult("SELECT `id` FROM `players` WHERE `marrystatus` = " .. player .. ";")
  692. if(stat:getID() == -1) then
  693. return FALSE
  694. else
  695. local info = stat:getDataInt("id")
  696. return info
  697. end
  698. end
  699.  
  700. function getOwnMarryStatus(player)
  701. local stat = db.getResult("SELECT `marrystatus` FROM `players` WHERE `id` = " .. player .. ";")
  702. if(stat:getID() == -1) then
  703. return FALSE
  704. else
  705. local info = stat:getDataInt("marrystatus")
  706. return info
  707. end
  708. end
  709.  
  710. function isOnline(player)
  711. local rows = db.getResult("SELECT `online` FROM `players` WHERE `id` = " .. player .. ";")
  712. local on = rows:getDataInt("online")
  713. if on ~= 0 then
  714. return TRUE
  715. else
  716. return FALSE
  717. end
  718. end
  719.  
  720. function doPlayerSetSkill(cid, skill, amount)
  721. local pid = getPlayerGUID(cid)
  722. doRemoveCreature(cid,true)
  723. db.Query("UPDATE `player_skills` SET `value` = ".. amount .." WHERE `player_id` = ".. pid .. " and `skillid` = ".. skill ..";")
  724. return TRUE
  725. end
  726.  
  727. function doPlayerSetMagic(cid, amount)
  728. local pid = getPlayerGUID(cid)
  729. doRemoveCreature(cid,true)
  730. db.Query("UPDATE `players` SET `maglevel` = " .. amount .. " WHERE `id` = "..pid)
  731. return TRUE
  732. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement