Advertisement
Guest User

Untitled

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