Forgee_XenoBot

Forgee library 1.1.0

Oct 19th, 2012
9,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.74 KB | None | 0 0
  1. --[[
  2. Forgee library v 1.1.0
  3. To use these functions paste: dofile("Forgee.lua")
  4. at the top of your script.
  5.  
  6. Forum thread: http://forums.xenobot.net/showthread.php?2392-Lua-functions-by-Forgee
  7. Facebook: http://facebook.com/forgee.xenobot
  8. ]]
  9.  
  10. ----------------------------------------------------------------------------
  11. ------------------------------ Self Class ----------------------------------
  12. ----------------------------------------------------------------------------
  13.  
  14. -- getMonstersAround(4, "Frost Dragon", "Frost Dragon Hatchling")
  15. function getMonstersAround(radius, ...)
  16.     local t = {...}
  17.     local monsters = {}
  18.     for i = CREATURES_LOW, CREATURES_HIGH do
  19.         local creature = Creature.GetFromIndex(i) -- Let's find us a creature //TODO: Find a way to read additional info correctly (hp, speed, outfit etc).
  20.         if (creature:isValid()) and creature:ID() ~= Self.ID() then -- Make sure it's valid and don't count ourselves.
  21.             if (creature:isOnScreen() and creature:isVisible() and creature:isAlive()) then -- We want to see the creature.
  22.                 if not creature:isPlayer() then -- And it must not be a player, aren't we picky?
  23.                     local name = creature:Name()
  24.                     if (creature:DistanceFromSelf() <= radius) then -- Is the creature close enough to be considered?
  25.                         if (#t > 0 and table.contains(t, name)) or (#t == 0) then -- If we have a monster list, we look for our creature there.
  26.                             table.insert(monsters, creature)
  27.                         end
  28.                     end
  29.                 end
  30.             end
  31.         end
  32.     end
  33.     return monsters
  34. end
  35. --[[
  36. Example use for exori: (Note: Added exori() function to library.)
  37.  
  38. dofile("Forgee.lua")
  39. local radius = 1 -- Radius to consider
  40. local min = 2 -- Minimum number of monsters to hit.
  41. local spell = "exori" -- Spell to cast.
  42. local mana = 120 -- Minimum mana to cast spell.
  43. local monsters = {"Frost Dragon", "Frost Dragon Hatchling"}
  44. function exori()
  45.     local m = getMonstersAround(radius, unpack(monsters))
  46.     if #m >= min then
  47.         Self.Cast(spell, mana)
  48.         wait(1100, 1800)
  49.     end
  50. end
  51.  
  52. while (true) do
  53.     exori()
  54. end
  55. ]]
  56.  
  57. -- getPlayersAround(7, "DarkstaR", "Syntax")
  58. function getPlayersAround(radius, ...)
  59.     local t = {...}
  60.     local players = {}
  61.     if (radius == 0) then
  62.         radius = 8
  63.     end
  64.     for i = CREATURES_LOW, CREATURES_HIGH do
  65.         local creature = Creature.GetFromIndex(i)
  66.         if (creature:isValid()) and creature:ID() ~= Self.ID() then
  67.             if (creature:isOnScreen() and creature:isVisible() and creature:isAlive()) then
  68.                 if creature:isPlayer() then
  69.                     local name = creature:Name()
  70.                     if (creature:DistanceFromSelf() <= radius) then
  71.                         if (not table.contains(t, name)) then -- If we have a white list, we look for the player there.
  72.                             table.insert(players, creature)
  73.                         end
  74.                     end
  75.                 end
  76.             end
  77.         end
  78.     end
  79.     return players
  80. end
  81. --[[
  82. Usage:
  83. We can find all info the bot can get about a creature.
  84. If you enter names after the radius it will work as a "whitelist".
  85.  
  86. players = getPlayersAround(8, "DarkstaR", "Syntax")
  87. if #players > 0 then
  88.     for i = #players do
  89.         creature = players[i]
  90.         if creature:HealthPercent() < 60 then
  91.             ~do something
  92.         end
  93.     end
  94. end
  95. ]]
  96.  
  97. -- Same as getPlayersAround, but gets players from other floors too.
  98. -- xrayPlayersAround(6)
  99. function xrayPlayersAround(radius, ...)
  100.     local t = {...}
  101.     local players = {}
  102.     if (radius == 0) then
  103.         radius = 8
  104.     end
  105.     for i = CREATURES_LOW, CREATURES_HIGH do
  106.         local creature = Creature.GetFromIndex(i)
  107.         if (creature:isValid()) and creature:ID() ~= Self.ID() then
  108.             if creature:isVisible() and creature:isAlive() then
  109.                 if creature:isPlayer() then
  110.                     local name = creature:Name()
  111.                     local selfpos = Self.Position()
  112.                     local creaturepos = creature:Position()
  113.                     local distance = math.max(math.abs(selfpos.x - creaturepos.x), math.abs(selfpos.y - creaturepos.y))
  114.                     if (distance <= radius) then
  115.                         if not table.contains(t, name) then
  116.                             table.insert(players, creature)
  117.                         end
  118.                     end
  119.                 end
  120.             end
  121.         end
  122.     end
  123.     return players
  124. end
  125.  
  126.  
  127. function monstersAround(radius, ...) -- Count monsters around. monstersAround(5, "Frost Dragon") will count only Frost Dragons within a 5 sqm radius.
  128.     if getMonstersAround(radius, ...) then
  129.         return #getMonstersAround(radius, ...)
  130.     else
  131.         return 0
  132.     end
  133. end
  134.  
  135. function playersAround(radius, ...) -- Count players around. Names are excluded from the count.
  136.     if getPlayersAround(radius, ...) then
  137.         return #getPlayersAround(radius, ...)
  138.     else
  139.         return 0
  140.     end
  141. end
  142.  
  143.  
  144. -- Can be used for any circular aoe spell such as UE, Divine Caldera, etc.
  145. -- exori(1, 3, "exori", 120, false, "Frost Dragon", "Frost Dragon Hatchling")
  146. function exori(radius, num, spell, mana, hitPlayers, ...)
  147.     local monsters = {...}
  148.     if monstersAround(radius, unpack(monsters)) >= num then
  149.         if not hitPlayers then
  150.             if playersAround(radius + 2) == 0 then
  151.                 Self.Cast(spell, mana)
  152.             end
  153.         else
  154.             Self.Cast(spell, mana)
  155.         end
  156.         wait(1100, 1800)
  157.     end
  158.     wait(200,500)
  159. end
  160. --[[Example Script ] --
  161. dofile("Forgee.lua")
  162. local radius = 1 -- Radius to consider
  163. local num = 2 -- Minimum number of monsters to hit.
  164. local spell = "exori" -- Spell to cast.
  165. local mana = 120 -- Minimum mana to cast spell.
  166. local monsters = {"Frost Dragon", "Frost Dragon Hatchling"}
  167. local hitPlayers = false -- false will not cast the spell if there are players in the affected nearby (radius+1). true will execute regardless of other players, only recommended for Optional pvp.
  168.  
  169. while (true) do
  170.     exori(radius, num, spell, mana, hitPlayers, monsters) -- All waiting is handled by the function.
  171. end
  172. ]]
  173.  
  174. Self.GetMonstersAround = getMonstersAround
  175. Self.MonstersAround = monstersAround
  176. Self.GetPlayersAround = getPlayersAround
  177. Self.PlayersAround = playersAround
  178. Self.Exori = exori
  179.  
  180. --[ Container functions ]--
  181. --[[
  182. Since we now can close a container instead of using hotkeys, cascaded backpacks no longer has to be the same colour. Yay!
  183. You can close specific backpacks by entering the index, id or name of backpacks to close.
  184. ]]
  185. function closeBackpacks(...)
  186.     local backpacks = {...}
  187.     local indexes = Container.GetIndexes()
  188.     if #backpacks > 0 then
  189.         for i = #backpacks, 1, -1 do
  190.             if type(backpacks[i]) == "string" then
  191.                 tmpBP = Container.GetByName(backpacks[i])
  192.             elseif backpacks[i] < 18 then
  193.                 tmpBP = Container.GetFromIndex(backpacks[i])
  194.             else
  195.                 tmpBP = Container.GetByName(Item.GetName(backpacks[i]))
  196.             end
  197.             tmpBP:Close()
  198.             wait(200, 500)
  199.         end
  200.     else
  201.         Self.CloseContainers()
  202.     end
  203. end
  204.  
  205. --[[
  206. openBackpacks() -- Will open the bp in your backpack slot.
  207. openBackpacks({9602, 0}) -- Will open an orange bp (9602) inside a your main (first open) backpack.
  208. openBackpacks(9602, {8860, 1}) -- Will first open an orage bp inside the first container/main bp if open. If Main bp is not open it will open that first. Then opens a Brocade Backpack in the second open container ().
  209. ]]
  210. function openBackpacks(...)
  211.     local backpacks = {...} -- List of backpacks to open.
  212.     local open = Container.GetIndexes() -- List of already open backpacks.
  213.     local main = Container.GetFirst() -- First open container is assumed to be main backpack (will be 0 if no container is open).
  214.     local toOpen = #backpacks -- Number of backpacks we need to open (used to check success).
  215.     local defaultOpen = {} -- A place to store the backpacks to open by container:OpenChildren(), if any.
  216.     local bps = {} -- A place to store the backpacks to open by container:UseItem(), if any. Why not always use :OpenChildren()? This way I can open bps by their relative position instead of id, so you don't have to worry about colours anymore.
  217.     if main:ID() == 0 then -- If no backpack is open, we need to open main.
  218.         if backpacks[1] ~= Self.Backpack().id then
  219.             toOpen = toOpen + 1
  220.         end
  221.         repeat -- Open main backpack.
  222.             wait(200, 600)
  223.         until Self.UseItemFromEquipment("backpack") > 0
  224.         wait(500)
  225.         main = Container.GetFirst()
  226.     end
  227.     for i = 1, #backpacks do
  228.         if type(backpacks[i]) == "table" then
  229.             if backpacks[i][1] > 16 then
  230.                 if backpacks[i][2] ~= main:Index() then
  231.                     main = Container.GetFromIndex(backpacks[i][2])
  232.                     for spot = 0, main:ItemCount()-1 do
  233.                         local item = main:GetItemData(spot)
  234.                         if Item.isContainer(item.id) then
  235.                             table.insert(bps, spot)
  236.                         end
  237.                     end
  238.                     main:UseItem(bps[backpacks[i][1]])
  239.                     wait(500, 900)
  240.                 end
  241.             elseif backpacks[i][1] ~= main:ID() then
  242.                 main = Container.GetFromIndex(backpacks[i][2])
  243.                 main:OpenChildren(backpacks[i][1])
  244.                 wait(500, 900)
  245.             end
  246.         elseif backpacks[i] < 16 then
  247.             if i == 1 then
  248.                 for spot = 0, main:ItemCount()-1 do
  249.                     local item = main:GetItemData(spot)
  250.                     if Item.isContainer(item.id) then
  251.                         table.insert(bps, spot)
  252.                     end
  253.                 end
  254.                 for _, num in ipairs(backpacks) do
  255.                     main:UseItem(bps[num])
  256.                     wait(500, 900)
  257.                 end
  258.                 break
  259.             end
  260.         elseif backpacks[i] ~= main:ID() then
  261.             table.insert(defaultOpen, backpacks[i])
  262.         end
  263.     end
  264.     if #defaultOpen > 0 then
  265.         main:OpenChildren(unpack(defaultOpen))
  266.     end
  267.     wait(400)
  268.     if #open + toOpen == #Container.GetIndexes() then
  269.         return true
  270.     end
  271.     return false
  272. end
  273.  
  274. --[[
  275. Returns a table containing the ids of opened backpacks in order.
  276. open = getOpenBackpacks()
  277. open[1] -> first backpack
  278. open[2] -> second backpack, etc.
  279. ]]
  280. function getOpenBackpacks()
  281.     local indexes = Container.GetIndexes() -- Find index for all open backpacks
  282.     local open = {} -- Create a place to store our open backpacks
  283.     for i = 1, #indexes do -- Search all open backpacks
  284.         tmpBP = Container.GetFromIndex(indexes[i])
  285.         table.insert(open, tmpBP:ID()) -- Store this backpack
  286.     end
  287.     return open -- Return found backpacks
  288. end
  289.  
  290. --[[
  291. resetBackpacks(reset)
  292. If reset is specified it will reset the number of backpacks specified. resetBackpacks(1) will reset the last opened backpack, resetBackpacks(2) the last 2 etc.
  293. Empty argument resets all open backpacks.
  294. Checks if all backpacks were reopened correctly. If not it will start over up to 3 times. If it failed after 3 tries it returns false (true if it succeeds).
  295. ]]
  296. function resetBackpacks(reset)
  297.     setWalkerEnabled(false)
  298.     local open = {} -- Create a table to hold backpack list
  299.     local close = {}
  300.     local indexes = Container.GetIndexes() -- Get ids of open backpacks.
  301.     if reset then
  302.         if reset > 0 and reset < #indexes then
  303.             for i = (#indexes-reset)+1, #indexes do
  304.                 table.insert(open, indexes[i])
  305.             end
  306.         else
  307.             for i = 2, #indexes do -- If 'reset' is above the number of open backpacks we should reset all.
  308.                 table.insert(open, indexes[i])
  309.             end
  310.         end
  311.     else
  312.         for i = 2, #indexes do -- If 'reset' is nil (empty argument) we should reset all backpacks.
  313.             table.insert(open, indexes[i])
  314.         end
  315.     end
  316.     local tries = 3 -- Give the script 3 tries to achieve a successful reset.
  317.     repeat
  318.         if reset then
  319.             if reset > 0 and reset < #indexes then
  320.                 closeBackpacks(unpack(open)) -- Close selected backpacks
  321.             else
  322.                 Self.CloseContainers() -- Close all backpacks.
  323.             end
  324.         else
  325.             Self.CloseContainers() -- Close all backpacks
  326.         end
  327.         wait(600, 800) -- wait a little while
  328.         local reopen = openBackpacks(unpack(open)) -- I store the return from openBackpacks, true if successfull, false otherwise
  329.         if tries == 0 then -- Check how many tries the function has left
  330.             print("BackpackReset failed!") -- Tell the user that reset has failed
  331.             return false
  332.         end
  333.         tries = tries - 1 -- One try has been spent
  334.         wait(100,200)
  335.     until reopen -- If reopen is true it means all backpacks were opened successfully and function is done
  336.     setWalkerEnabled(true)
  337.     return true
  338. end
  339.  
  340. Self.SellItemsDownTo = function(item, count)
  341.     return Self.ShopSellItem(Self.ShopGetItemSaleCount(item) - count)
  342. end
  343.  
  344. Self.BuyItemsUpTo = Self.ShopBuyItemsUpTo
  345. Self.ShopSellItemsDownTo = Self.SellItemsDownTo
  346.  
  347. ----------------------------------------------------
  348. -----------[ Working with ground items ]------------
  349. ----------------------------------------------------
  350.  
  351. -- openDoor(direction[, key])
  352. -- also accepts position as a table, openDoor({x, y, z}) or as normal x, y, z
  353. function openDoor(x, y, z, keyid)
  354.     if not x then -- assumes doorpos is 1sqm ahead in the direction the char is facing
  355.         pos = Self.LookPos(1)
  356.     elseif type(x) == "table" then
  357.         pos = {x = x[1], x = x[2], z = x[3]}
  358.         keyid = y
  359.     elseif x < 50 then
  360.         pos = getPositionFromDirection(Self.Position(), x, 1)
  361.         keyid = y
  362.     else
  363.         pos = {x = x, y = y, z = z}
  364.     end
  365.     local door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  366.     if table.contains(CLOSED_DOORS, door.id) then -- door is closed
  367.         local tries = 0
  368.         if keyid then
  369.             local spot = getSpotByID(keyid)
  370.             if spot then
  371.                 repeat
  372.                     if tries >= 3 then
  373.                         print("OpenDoor: Failed to open door!")
  374.                         return false
  375.                     end
  376.                     Container:UseItemWithGround(spot, pos.x, pos.y, pos.z)
  377.                     wait(500, 800)
  378.                     door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  379.                     tries = tries + 1
  380.                 until table.contains(OPENED_DOORS, door.id)
  381.             else
  382.                 print("OpenDoor: Could not find key %s.", keyid)
  383.                 return false
  384.             end
  385.         else
  386.             repeat
  387.                 if tries >= 3 then
  388.                     print("OpenDoor: Failed to open door!  Is it locked?")
  389.                     return false
  390.                 end
  391.                 Self.UseItemFromGround(pos.x, pos.y, pos.z)
  392.                 wait(500, 800)
  393.                 door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  394.                 tries = tries + 1
  395.             until table.contains(OPENED_DOORS, door.id)
  396.         end
  397.     end
  398.     return true
  399. end
  400.  
  401. -- Same usage as openDoor
  402. function closeDoor(x, y, z, keyid)
  403.     if not x then -- assumes doorpos is 1sqm ahead in the direction the char is facing
  404.         pos = Self.LookPos(1)
  405.     elseif type(x) == "table" then
  406.         pos = {x = x[1], x = x[2], z = x[3]}
  407.         keyid = y
  408.     elseif x < 50 then
  409.         pos = getPositionFromDirection(Self.Position(), x, 1)
  410.         keyid = y
  411.     else
  412.         pos = {x = x, y = y, z = z}
  413.     end
  414.     local door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  415.     if table.contains(OPENED_DOORS, door.id) then -- door is open
  416.         if keyid then
  417.             spot = getSpotByID(keyid)
  418.             repeat
  419.                 Container:UseItemWithGround(spot, pos.x, pos.y, pos.z)
  420.                 wait(500, 800)
  421.                 door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  422.             until table.contains(CLOSED_DOORS, door.id)
  423.         else
  424.             repeat
  425.                 Self.UseItemFromGround(pos.x, pos.y, pos.z)
  426.                 wait(500, 800)
  427.                 door = Map.GetTopUseItem(pos.x, pos.y, pos.z)
  428.             until table.contains(CLOSED_DOORS, door.id)
  429.         end
  430.     end
  431.     return true
  432. end
  433.  
  434. function skinCreature(tool, range, ...)
  435.     if type(tool) == "string" then
  436.         tool = Item.GetID(tool)
  437.     end
  438.     if Self.ItemCount(tool) == 0 then
  439.         print("Could not find %s in any open container.", Item.GetName(tool))
  440.         return false
  441.     end
  442.     local bodies = {...}
  443.     for i = 1, #bodies do
  444.         local pos = Map.FindUseItem(bodies[i])
  445.         if pos ~= 0 then
  446.             if Self.DistanceFromPosition(pos.x, pos.y, pos.z) <= range then
  447.                 setWalkerEnabled(false)
  448.                 setLooterEnabled(false)
  449.                 while getDistanceBetween(Self.Position(), pos) > 1 do
  450.                     Self.UseItemWithGround(tool, pos.x, pos.y, pos.z)
  451.                     wait(1500, 2500)
  452.                 end
  453.                 setWalkerEnabled(true)
  454.                 setLooterEnabled(true)
  455.                 return 1
  456.             end
  457.         end
  458.     end
  459.     return 0
  460. end
  461.  
  462. Self.Skin = function (range, ...)
  463.     return skinCreature(5908, range, ...)
  464. end
  465.  
  466. Self.Stake = function (range, ...)
  467.     return skinCreature(5942, range, ...)
  468. end
  469.  
  470. Self.Fish = function (range, ...)
  471.     return skinCreature(3483, range, ...)
  472. end
  473.  
  474. Self.Target = function ()
  475.     return Creature.GetByID(Self.TargetID())
  476. end
  477.  
  478. Self.AutoFish = function (miss)
  479.     local function findWater()
  480.         local FISH_POS = {}
  481.         local EMPTY_POS = {}
  482.         local WATER_FISH = {4597, 4598, 4599, 4601, 4602}
  483.         local WATER_EMPTY = {4603, 4604, 4605, 4606, 4607, 4608, 4609, 4610, 4611, 4612, 4613}
  484.         for x = -7, 7 do
  485.             for y = -5, 5 do
  486.                 tile = Map.GetTopUseItem(Self.Position().x + x, Self.Position().y + y, Self.Position().z)
  487.                 if table.contains(WATER_FISH, tile.id) then
  488.                     table.insert(FISH_POS, {x = Self.Position().x + x, y = Self.Position().y + y, z = Self.Position().z})
  489.                 elseif table.contains(WATER_EMPTY, tile.id) then
  490.                     table.insert(EMPTY_POS, {x = Self.Position().x + x, y = Self.Position().y + y, z = Self.Position().z})
  491.                 end
  492.             end
  493.         end
  494.         return FISH_POS, EMPTY_POS
  495.     end
  496.    
  497.     if Self.ItemCount(3492) > 0 then
  498.         local oldCount = Self.ItemCount(3492)
  499.         if not pos or pos.x ~= Self.Position().x or pos.y ~= Self.Position().y then
  500.             fish, empty = findWater()
  501.             pos = Self.Position()
  502.         end
  503.         if miss and type(miss) ~= 'boolean' then
  504.             print("AutoFish: Invalid agument. Valid arguments are 'true' or 'false'.")
  505.             return false
  506.         end
  507.         if miss then
  508.             for i = 1, #empty do
  509.                 table.insert(fish, empty[i])
  510.             end
  511.         end
  512.         if #fish > 0 then
  513.             local spot = math.random(1, #fish)
  514.             local tile = Map.GetTopUseItem(fish[spot].x, fish[spot].y, fish[spot].z)
  515.             Self.UseItemWithGround(Item.GetID("Fishing rod"), fish[spot].x, fish[spot].y, fish[spot].z)
  516.             sleep(500)
  517.             if Self.ItemCount(3492) < oldCount then
  518.                 result = 2
  519.             else
  520.                 result = 1
  521.             end
  522.         end
  523.         wait(500, 800)
  524.     end
  525.     return result or 0
  526. end
  527.  
  528. Self.ReachDepot = function (tries)
  529.     local tries = tries or 3
  530.     setWalkerEnabled(false)
  531.     local DepotIDs = {3497, 3498, 3499, 3500}
  532.     local DepotPos = {}
  533.     for i = 1, #DepotIDs do
  534.         local dps = Map.GetUseItems(DepotIDs[i])
  535.         for j = 1, #dps do
  536.             table.insert(DepotPos, dps[j])
  537.         end
  538.     end
  539.     local function gotoDepot()
  540.         local pos = Self.Position()
  541.         print("Depots found: " .. tostring(#DepotPos))
  542.         for i = 1, #DepotPos do
  543.             location = DepotPos[i]
  544.             Self.UseItemFromGround(location.x, location.y, location.z)
  545.             wait(1000, 2000)
  546.             if Self.DistanceFromPosition(pos.x, pos.y, pos.z) >= 1 then
  547.                 wait(5000, 6000)
  548.                 if Self.DistanceFromPosition(location.x, location.y, location.z) == 1 then
  549.                     setWalkerEnabled(true)
  550.                     return true
  551.                 end
  552.             else
  553.                 print("Something is blocking the path. Trying next depot.")
  554.             end
  555.         end
  556.         return false
  557.     end
  558.    
  559.     repeat
  560.         reachedDP = gotoDepot()
  561.         if reachedDP then
  562.             return true
  563.         end
  564.         tries = tries - 1
  565.         sleep(100)
  566.         print("Attempt to reach depot was unsuccessfull. " .. tries .. " tries left.")
  567.     until tries <= 0
  568.  
  569.     return false
  570. end
  571.  
  572. Map.FindUseItem = function (id)
  573.     if type(id) == "string" then
  574.         id = Item.GetID(id)
  575.     end
  576.     local pos = Self.Position()
  577.     for x = -7, 7 do
  578.         for y = -5, 5 do
  579.             if Map.GetTopUseItem(pos.x + x, pos.y + y, pos.z).id == id then
  580.                 itemPos = {x = pos.x + x, y = pos.y + y, z = pos.z}
  581.                 return itemPos
  582.             end
  583.         end
  584.     end
  585.     return 0
  586. end
  587.  
  588. Map.FindMoveItem = function (id)
  589.     if type(id) == "string" then
  590.         id = Item.GetID(id)
  591.     end
  592.     local pos = Self.Position()
  593.     for x = -7, 7 do
  594.         for y = -5, 5 do
  595.             if Map.GetTopMoveItem(pos.x + x, pos.y + y, pos.z).id == id then
  596.                 itemPos = {x = pos.x + x, y = pos.y + y, z = pos.z}
  597.                 return itemPos
  598.             end
  599.         end
  600.     end
  601.     return 0
  602. end
  603.  
  604. Map.GetUseItems = function (id)
  605.     if type(id) == "string" then
  606.         id = Item.GetID(id)
  607.     end
  608.     local pos = Self.Position()
  609.     local store = {}
  610.     for x = -7, 7 do
  611.         for y = -5, 5 do
  612.             if Map.GetTopUseItem(pos.x + x, pos.y + y, pos.z).id == id then
  613.                 itemPos = {x = pos.x + x, y = pos.y + y, z = pos.z}
  614.                 table.insert(store, itemPos)
  615.             end
  616.         end
  617.     end
  618.     return store
  619. end
  620.  
  621. Map.GetMoveItems = function (id)
  622.     if type(id) == "string" then
  623.         id = Item.GetID(id)
  624.     end
  625.     local pos = Self.Position()
  626.     local store = {}
  627.     for x = -7, 7 do
  628.         for y = -5, 5 do
  629.             if Map.GetTopMoveItem(pos.x + x, pos.y + y, pos.z).id == id then
  630.                 itemPos = {x = pos.x + x, y = pos.y + y, z = pos.z}
  631.                 table.insert(store, itemPos)
  632.             end
  633.         end
  634.     end
  635.     return store
  636. end
  637.  
  638. ----------------------------------------------------
  639. -------------------[ Item Tools ]-------------------
  640. ----------------------------------------------------
  641.  
  642. -- getSpotByID(id[, container]) Container name/index is optional.
  643. function getSpotByID(id, container)
  644.     if container then
  645.         if type(container) == "string" then
  646.             cont = Container.GetByName(container)
  647.         elseif type(container) == "number" then
  648.             cont = Container.GetFromIndex(container)
  649.         end
  650.     else
  651.         cont = Container.GetFirst()
  652.     end
  653.     while cont:isOpen() do
  654.         for spot = 0, cont:ItemCount() do
  655.             local item = cont:GetItemData(spot)
  656.             if item.id == id then
  657.                 return spot, cont
  658.             end
  659.         end
  660.         if container then
  661.             --print("GetSpotByID: Item of id " .. id .. " could not be found in " .. cont:Name() .. ".")
  662.             return false
  663.         else
  664.             cont = cont:GetNext()
  665.         end
  666.     end
  667.     --print("GetSpotByID: Item of id " .. id .. " could not be found in any open container.")
  668.     return false
  669. end
  670.  
  671. Item.GetSpotByID = getSpotByID
  672.  
  673. -------------------------------------------------------------
  674. -----------------------[ War tools ]-------------------------
  675. -------------------------------------------------------------
  676. -- [NOT TESTED] --
  677. function warEnemies(range)
  678.     p = getPlayersAround(range)
  679.     enemies = {}
  680.     for i = 1, #p do
  681.         if p[i]:isWarEnemy() then
  682.             table.insert(enemies, p[i])
  683.         end
  684.     end
  685.     return #enemies
  686. end
  687.  
  688. function warAllies(range)
  689.     p = getPlayersAround(range)
  690.     allies = {}
  691.     for i = 1, #p do
  692.         if p[i]:isWarAlly() then
  693.             table.insert(allies, p[i])
  694.         end
  695.     end
  696.     return #allies
  697. end
  698.  
  699. function getWarAllies(range)
  700.     p = getPlayersAround(range)
  701.     allies = {}
  702.     for i = 1, #p do
  703.         if p[i]:isWarAlly() then
  704.             table.insert(allies, p[i])
  705.         end
  706.     end
  707.     if #allies > 0 then
  708.         return allies
  709.     else
  710.         return false
  711.     end
  712. end
  713.  
  714. function getWarEnemies(range)
  715.     p = getPlayersAround(range)
  716.     enemies = {}
  717.     for i = 1, #p do
  718.         local player = p[i]
  719.         if player:isWarEnemy() then
  720.             table.insert(enemies, player)
  721.         end
  722.     end
  723.     if #enemies > 0 then
  724.         return enemies
  725.     else
  726.         return false
  727.     end
  728. end
  729.  
  730. Self.WarEnemies = warEnemies
  731. Self.WarAllies = warAllies
  732. Self.GetWarEnemies = getWarEnemies
  733. Self.GetWarAllies = getWarAllies
Advertisement
Add Comment
Please, Sign In to add comment