function coconutshy() --[[ -- ============================================= -- coconutshy() by Rickster (16.02.2013 / mm.dd.yyyy) . -- Version 0.03 beta (17.02.2013) -- last edited by: Rickster -- -- Description: -- This function does the coconutshy from the flower event on varanas bridge -- !!! Take care, that the userfunction actually does not do the questhandling for you. -- The event starts at the npc Waysie Fleckstein. -- Start this function manually or from a wp file and then start the event manually. -- As soon as the mobs appear it automatically starts to throw at them. After the -- event has finished, you manually have to break the script. -- ============================================= --]] --== Useroptions ==-- -- do you want to see a progress bar, when searching for attackable mobs? -- default: true local showTargetSearching = true -- wait this time in ms after the start of casting extra action -- the extra action cast needs 1000ms (1 second) to finish and throw -- shorter times than 1000 means to retry, in case the cast did not start, -- but can result in a "cast failure" because we are still casting the last one -- larger times mean to wait the whole cast time and more before retrying, -- but can end in a long break, when the last cast failed -- set this to 0 to automatically wait until the cast finishes -- default: 0 local timeCast = 0 -- if no mobs to throw at were found, wait this time before looking for new ones -- default: 1000 local timeUpdateObjects = 1000 -- if a mob was thrown at, we can add an additional time to wait, before -- looking for the others and new ones -- usually not needed, just for tweaking purposes to add an extra pause -- default: 0 local timeUpdateObjectsAfterKill = 0 -- !!!CAUTION!!! the next to options are the most tweaking ones -- -- seconds, how long a killed mob is remembered as killed -- killed mobs (objects) do not disapear immediatelly, nor do they have a unique address, -- so a new mob can get the address of one that we just killed -- so we have to remember a killed mob (his address), not to attack it again if we already did, -- but not for too long, so we can kill it again, if a new one respawns with the same address -- default: 4 local tKeepObjectFor = 4 -- seconds, after which we clean up mobs, that no longer exist -- maximum should be tKeepObjectFor, because at the latest they should have disappeared then -- default: 2 local tCleanupUsedObjectsEvery = 2 --== END - Useroptions ==-- --== DO NOT CHANGE ANYTHING BELOW THIS LINE UNTIL YOU KNOW WHAT YOU DO ==-- debug_mode = false; local objectList = CObjectList() -- hold all nearby objects local foundMobs2Kill = false -- used to remember if we killed mobs (hit them!) local IDs2kill = { -- mobs we want to attack in ordered priority 120386, -- Kokomo 120385, -- Cavia 120384, -- Sheep } local usedObjects = {} -- table to remember the mobs we have thrown at local tBaseCleanup = os.time() -- base time to calculate if a cleanup is needed local tLastCleanup = tBaseCleanup -- the time usedObjects was cleaned last time -- see description of tCleanupUsedObjectsEvery above if (tCleanupUsedObjectsEvery > tKeepObjectFor) then tCleanupUsedObjectsEvery = tKeepObjectFor end cprintf_ex("|lightgreen| Searching for nearby objects to throw at ...\n") while RoMScript("ExtraActionBarFrame:IsVisible()") do if debug_mode then cprintf_ex("|purple| 1 \n") end foundMobs2Kill = false -- remember the found mob objectList:update() -- get objects around local objSize = objectList:size() -- number of objects in the list if debug_mode then cprintf_ex("|purple| 2 ") cprintf_ex("|purple| objSize: %s\n", objSize) end for j,k in ipairs(IDs2kill) do if debug_mode then cprintf_ex("|purple| 3 ") end if debug_mode or showTargetSearching then printf("\n%-10s: ", GetIdName(k)) end -- compare to every objectList ID for i=0,objSize do -- get one object from the list after the other obj = objectList:getObject(i); if (obj.Id == k and not usedObjects[obj.Address]) then -- use extraskill actual mob found by id from killtable usedObjects[obj.Address] = {} -- add objects address to table usedObjects[obj.Address].tCreated = os.time() -- remember the time we attacked this mob if debug_mode or showTargetSearching then -- show atacked mob as red dot in the progress bar cprintf_ex("|lightred|.") end if debug_mode then cprintf_ex("|purple| 4 \n") end player:target(obj) -- target the found mob -- yrest(150) RoMScript("UseExtraAction(1)") -- use extra action on target if timeCast > 0 then yrest(timeCast) else repeat yrest(50) player:update() until not player.Casting end foundMobs2Kill = true -- remember the found mob else if debug_mode or showTargetSearching then -- show progress bar -- scale to max 70 search dots (.) per line local x if debug_mode then -- print the for every object to be killed x = math.ceil(objSize/65) else -- print the whole search list for all objects to kill in one line x = math.ceil(objSize*table.getn(IDs2kill)/65) --print(x) end if i%x==0 then printf(".") end end end -- break loop, to update objectList if foundMobs2Kill == true then break end end -- break loop, to update objectList if foundMobs2Kill == true then break end end printf("\n") if debug_mode then cprintf_ex("|purple| 5 \n") end if not foundMobs2Kill then yrest(timeUpdateObjects) else yrest(timeUpdateObjectsAfterKill) end -- clean up usedObjects table, to keep it small tBaseCleanup = os.time() nCleanedObjects = 0 if (tBaseCleanup - tLastCleanup >= tCleanupUsedObjectsEvery) then if debug_mode then cprintf_ex("|purple| Cleaning usedObjects ... ") end tLastCleanup = tBaseCleanup -- set the new time usedObjects was cleaned for i,v in pairs(usedObjects) do if ((tBaseCleanup - v.tCreated) >= tKeepObjectFor) then usedObjects[i] = nil nCleanedObjects = nCleanedObjects + 1 end end if debug_mode then cprintf_ex("|purple| %s objects deleted.\n", nCleanedObjects) end end -- END - clean up usedObjects table end end