Post by nefariis on Mar 16, 2021 23:26:50 GMT
If you've looked at a few of my scripts you will notice that there are a few common methods that are in most of them that have been adapted for that particular script.
I personally have a helper library that I import into my personal scripts to help me with common functions, I thought I would share it in case it might help any of you in your scripting.
to import it into your projects, just put this at the top of the file -
helper = require "helper"
helper.lua
local helper = {}
function helper.haveTool(tool)
if RIGHTHANDNAME and string.match(RIGHTHANDNAME, tool) then
return true
else
FindItem(tool, BACKPACKID)
if FINDITEM then
ContextMenu(FINDITEM[1].ID, "Equip")
return true
end
end
print(string.format("%s is not available", tool))
return false
end
function helper.moveTo(X, Z)
Move(X, CHARPOSY, Z)
start = TIME
repeat
sleep(1000)
xmin, xmax = math.min(CHARPOSX, X), math.max(CHARPOSX, X)
zmin, zmax = math.min(CHARPOSZ, Z), math.max(CHARPOSZ, Z)
xtrue, ztrue = xmax - xmin < 2, zmax - zmin < 2
until (xtrue and ztrue) or (TIME - start > 15)
return true
end
function helper.scanJournal(startT, endT, search)
for i = startT, endT, .45 do
ScanJournal(i)
for str in search do
if string.match(SCANJOURNALMESSAGE, str) then
return true
end
end
end
return false
end
function helper.dropItems(items)
for item in items do
FindItem(item, BACKPACKID)
if FINDITEM then
Drag(FINDITEM[1].ID)
Dropg(CHARPOSX, CHARPOSY, CHARPOSZ)
sleep(1000)
end
end
end
function helper.moveItemsToHorse(items, packHorse)
for item in items do
FindItem(item, BACKPACKID)
if FINDITEM then
Drag(item.ID)
Dropc(packHorse)
sleep(1000)
end
end
end
function helper.moveResourcesToHorse(packHorse)
for resource in {"fish", "logs", "ore", "ingots"} do
FindItem(resource, BACKPACKID)
if FINDITEM then
for item in FINDITEM do
Drag(item.ID)
Dropc(packHorse)
sleep(1000)
end
end
end
end
function helper.findMonsters()
FindMobile("F04646")
monsters, corpses = {}, {}
if FINDMOBILE then
for monster in FINDMOBILE do
if not string.match(monster.NAME, "Corpse")
and monster.DISTANCE < 15.5 then
table.insert(monsters, monster)
elseif string.match(monster.NAME, "Corpse")
and monster.DISTANCE < 5 then
table.insert(corpses, monster)
end
end
end
return monsters, corpses
end
return helper
Explanation of all the Methods
This will look for and equip a particular tool or item, where it is useful is in for/repeat loops as a way of breaking the loop such as -
or
- function haveTool(tool)
This will look for and equip a particular tool or item, where it is useful is in for/repeat loops as a way of breaking the loop such as -
while haveTool("hatchet") do
hatchetThings()
end
returnHome()
or
repeat
fishingThings()
until not haveTool("fishing rod")
doSomethingAfter()
- function moveTo(X, Z)
Moves to a particular location within a distance of ~2 - what's important is the true/false that tells you if you made it there or not so your script can act accordingly.
I made a custom scanJournal because the default one doesn't always work the way it should - also by combining it with a built in search function, you can save yourself a lot of duplicate code.
The method takes a Start Time, an End Time, and a Search Array.
or now you can complete a task/animation and look for a success/failure message during that time
what makes it great is that it will scan all the journal messages, instead of just the most recent one, so it will never miss an event - and as a bonus, you can give it multiple search items in the form of an array.
- function scanJournal(startT, endT, search)
I made a custom scanJournal because the default one doesn't always work the way it should - also by combining it with a built in search function, you can save yourself a lot of duplicate code.
The method takes a Start Time, an End Time, and a Search Array.
start = TIME
doSomeMethod()
doAnotherMethod()
if scanJournal(start, TIME, {"search1", "search2"}) then
doSomething()
end
or now you can complete a task/animation and look for a success/failure message during that time
repeat
start = TIME
doSomeMethod()
sleep(3000)
until scanJournal(start, TIME, {"search1", "search2"})
what makes it great is that it will scan all the journal messages, instead of just the most recent one, so it will never miss an event - and as a bonus, you can give it multiple search items in the form of an array.
function dropItems(items)
Simple function that drops an array of items to the ground
items = {"item1", "item2"}
- function moveItemsToHorse(items, packHorse)
Moves an array of items to your packhorse
- function moveResourcesToHorse(packHorse)
Moves all the resources from your person to your packhorse, this will move all the logs, fish, ore, ingots, etc
- function findMonsters()
This one is less intuitive - but if you have ever used NEARBYMONSTER or wanted a way to a way to blast an enemy when is in range (like a checkAndDefend()) or even wanted to setup a looting script - this is how I do it.
It returns two arrays, one of monsters that are close enough to use things like spells and provocation on, and another array that brings back a list of corpses that are within looting distance.