PDA

View Full Version : Help me stabilize my poor script



bl3d3
11-07-2019, 01:38 PM
Hello guys, so i wanted to make a simple cooking script, using tutorials on this forum. I managed to make it loop 4-5 times, but since my scripting knowledge is very limited it would start clicking where it not supposted to.

The script cooks, withdraws and deposits, opens bank (poorly but eventualy does it). While walking to cooking range and while walking to bnak, after few runs it would start clicking on the main screen, while walking(it supposed to sleep, while player is moving).

Here is how the script looks like:
I've made some really long sleep/wait time, hoping it would fix the random clicking on main screen while walking.
Also i wasnt sure how to make it loop properly.


program new;
{$i AeroLib/AeroLib.Simba}
function IsCooking: Boolean;
var
PBox: TBox;
begin
PBox := IntToBox(560, 205, 734, 463);
if pixelShift(PBox, 2000) then
Result := true;
end;
function isRawFishInInv: boolean;
var
x,y : integer;

begin
if FindColorTolerance(x,y, 14271377, 555, 206, 729, 461, 15) then
result := true;
end;



procedure WalkToBank
var
pnt : TPoint
begin
if findSymbol(pnt, SYMBOL_Bank) then
humanMMouse(pnt,1,1);
wait(250);
fastClick(1);
sleep(2000);
while isPlayerWalking() do
wait(1000);
end;

procedure WalkToRange
var
color_White : TColEx;
foundPnt : TPoint;
begin
color_White.create(982782, 2, 0.00, 0.39);
if color_White.findIn(AREA_MM, foundPnt) then
humanMMouse(foundPnt,1,1);
wait(250);
fastClick(1);
wait(2000);
while isPlayerWalking() do
wait(1000);
end;


procedure Banking;
var
x,y : integer;
obj_Chest : TMSObject;
Pnts : TPointArray;

begin // Wooden color // Silver color
obj_Chest.create('Bank', ['Bank booth','booth'], [createCol(3618620, 16, 0.53, 2.97)], [createCol(3493981, 1, 0.27, 0.12)]);
if obj_Chest.findAll(10, MSCP, Pnts) then
findMSObjectSimple([3618620, 3493981], ['Bank booth','booth']);
fastClick(1);
Wait(500 + Random(500 + 1000));
mouseBox(387,295,407,310,1,1);
wait(800);
repeat
mouseBox(387,295,407,310,1,1);
until(isInvEmpty);

end;

procedure Withdraw;
begin
mouseBox(44,93,52,103,1,0);
wait(500);
waitOption('All', 500);
wait(500);
end;

procedure SelectFish;
var
x,y : integer;
begin
if FindColorTolerance(x,y,6907522, 7, 347, 511, 473, 15) then
MoveMouse(x,y);
wait(500);
fastClick(1);
end;

procedure LetsCook;
var
x,y : integer;
obj_Range : TMSObject;
Pnts : TPointArray;

begin // Wooden color // Silver color
obj_Range.create('Range', ['Ran','nge'], [createCol(1583821, 9, 0.55, 2.87)], [createCol(3618620, 12, 0.47, 3.13)]);
if obj_Range.findAll(10, MSCP, Pnts) then
findMSObjectSimple([1583821, 3618620], ['Range']);
fastClick(1);
Wait(500 + Random(500 + 1000));
end;

procedure DepositAndWithdraw;
begin
if (not(isRawFishInInv)) then
WalkToBank;
wait(1000);
Banking;
if (isInvEmpty) then
Withdraw;
end;
procedure WalkToRangeAndCook;
begin
if (isRawFishInInv) then
WalkToRange;
wait(1000);
LetsCook;
wait(500);
SelectFish;
while IsCooking do
wait(200);
end;

procedure loop;
begin
DepositAndWithdraw;
sleep(500);
WalkToRangeAndCook;
sleep(500);
end;


begin
initAL;
repeat
Loop;
until(false);
end.

Dan the man
11-08-2019, 05:15 AM
Bit of feedback - forgive the shitty code - I haven't used AL in forever.

isPlayerWalking may not be overly accurate as it was made a while ago, and it depends on your zoom settings etc. You would be better off playing with the pixelShift function, or using other flags like bank open etc.

The IsCooking function would be worth integrating a few extra checks:
- Create a loop delay timer between cooks (say 5000 ms). If exceeded, exits the function.
- Use the GetXPBarAmount function to see if the xp has changed (ie successfully cooked a fish). If a fish is cooked, it restarts the delay timer.
- Count the amount of raw fish in your inventory. If the number changes from the last count (means we cooked or burnt a fish), it restarts the delay timer.
- Add a check to see if there is any raw fish left, if not, it breaks the loop.

Also look at replacing your Rawfish check with a DTM.
You can remove the whole block if you just create a TItem. You can also remove the select fish block as RawFish.Interact works better.

The indents of your WalkToBank procedure don't appear to be formatted corretly. Here it is with proper formatting.
procedure WalkToBank
var
pnt : TPoint
begin
if findSymbol(pnt, SYMBOL_Bank) then
humanMMouse(pnt,1,1);
wait(250);
fastClick(1);
sleep(2000);
while isPlayerWalking() do
wait(1000);
end;
It would also be worth adding a failsafe timer to your potential infinite loop:
procedure WalkToBank
var
pnt : TPoint
t:timer;
begin
if findSymbol(pnt, SYMBOL_Bank) then
humanMMouse(pnt,1,1);
wait(250);
fastClick(1);
sleep(2000);
t.start();
while isPlayerWalking() do
if (t.TimeElapsed > 10000) then // if we exceed 10 seconds of walking, the wait loop breaks.
break;
end;
The same can be applied to the WalkToRange.

With the banking procedure, it would be worth adding a check or even just delays between your mousebox clicks, and adding a fastClick option if the mouse is in the box already.
You should also look at adding a IsBankOpen check before clicking on the mousebox area.
procedure Banking;
var
x,y : integer;
obj_Chest : TMSObject;
Pnts : TPointArray;
t:timer;
begin // Wooden color // Silver color
obj_Chest.create('Bank', ['Bank booth','booth'], [createCol(3618620, 16, 0.53, 2.97)], [createCol(3493981, 1, 0.27, 0.12)]);
if obj_Chest.findAll(10, MSCP, Pnts) then
findMSObjectSimple([3618620, 3493981], ['Bank booth','booth']);
fastClick(1);
t.start();
while not IsBankOpen do
if (t.timeElapsed > 10000) then
break;
Wait(500 + Random(500 + 1000));
if not MouseInBox(ToBox(387,295,407,310)) then
mouseBox(387,295,407,310,1,1);
wait(800);
t.start()
repeat
wait(randomrange(500, 2000));
if not MouseInBox(ToBox(387,295,407,310)) then
mouseBox(387,295,407,310,1,1)
else
FastClick(MOUSE_LEFT); // if mouse point is already in the bounds of the box, we just left click.
if (t.TimeElapsed > 5000) then
break;
until(isInvEmpty);
end;
Again, we used a timer to determine the amount of time to wait before moving on.

A few modifications to withdraw based on the use of a TItem.
function Withdraw:boolean;
var
p:tpoint;
t:timer;
begin
if RawFish.findIn(Area_BS, p) then
MissMouse(p, 5, 5);
if WaitUpTextMulti(['Withdraw'], 300) then
begin
FastClick(MOUSE_RIGHT);
wait(randomrange(75, 175));
ChooseOption('All');
end;
while not RawFish.InInventory do
if (t.TimeElapsed > 3000) then
exit(false);
result := true;
end;

Rather than grouping the Depo and Withdraw in separate blocks, I would just do them in your main loop.
It would be worth changing some of the procedures to functions with a boolean output to make your loop easier to build.
procedure loop;
var
t:timer;
begin
GameTab(TAB_INV);
if RawFish.InInventory then
if WalkToRange then
if RawFish.interact(MOUSE_LEFT, true) then
if LetsCook then
begin
t.start();
while IsCooking do
if (t.TimeElapsed > 60000) then
break;
end;
if not RawFish.InInventory then
if WalkToBank then
if Banking then
if Withdraw then
wait(randomrange(275, 525));
end;
Make a start with these suggestions and then feel free to ask me for some more feedback. Object finding can be a lot more accurate if you make your own finder function rather than using TMSObject.

Hope this helps.

bl3d3
11-09-2019, 09:03 AM
Thank you very much, it helped me alot to understand more about it. I've made a raw fish DTM and script runs much more smoothly, also running to bank and cooking range is much better now. But when i try running new Withdraw function, the .findIn wouldnt work with the RawFish DTM.

Edit: Forgot i had to create TItem first.

Dan the man
11-09-2019, 12:16 PM
Thank you very much, it helped me alot to understand more about it. I've made a raw fish DTM and script runs much more smoothly, also running to bank and cooking range is much better now. But when i try running new Withdraw function, the .findIn wouldnt work with the RawFish DTM.

Edit: Forgot i had to create TItem first.

You are best creating the TItem at the start of your script and freeing it when you finish.

You can achieve this by creating a procedure like so:
procedure StopScript;
begin
FreeDTM(RawFish.DTM);
end;
You then add the following to the main script block.
AddOnTerminate('StopScript');

So it should look something like this:
procedure StopScript;
begin
FreeDTM(RawFish.DTM);
end;

begin
RawFish.DTM := DTMFromString('Some string stuff');
AddOnTerminate('StopScript');
ScriptSetup;
Repeat
MainLoop;
until(false);
end.
This will unload the DTM when the script is terminated (whether it's called from the script or manually stopped).

When using the .findin function, be sure to declare the area that you are looking for the fish in the bank, not your inventory.

bl3d3
04-18-2020, 01:52 PM
Didnt want to open new thread, so ill ask here.
I want it to siply click on the tree, but it would just hover the mouse over it few times and wont click. I've tried changing uptext and colour, but it wouldnt click on the tree.


procedure ClickTree;
var
YewTree: TMSObject;
pnt:tpoint;

begin
if YewTree.Find(pnt) then
fastClick(mouse_left);

end;


begin
initAL;
YewTree.create('Yew', ['Chop down', 'Yew'], [createCol(5542796, 15, 0.20, 1.40)]);
ClickTree;
end;

chief herb
04-28-2020, 09:15 AM
Didnt want to open new thread, so ill ask here.
I want it to siply click on the tree, but it would just hover the mouse over it few times and wont click. I've tried changing uptext and colour, but it wouldnt click on the tree.


procedure ClickTree;
var
YewTree: TMSObject;
pnt:tpoint;

begin
if YewTree.Find(pnt) then
fastClick(mouse_left);

end;


begin
initAL;
YewTree.create('Yew', ['Chop down', 'Yew'], [createCol(5542796, 15, 0.20, 1.40)]);
ClickTree;
end;

check the thread you created. feel free to pm and ill try to help as best as i can when i can