SRL Archive - OSR Help

I need help with an advanced script skeleton.


Eduard

2014-08-20 14:54:25

Hi guys,

What kind of scipt skeleton are the best to use for pascal/simba?

I'm currently using a failsafe in every procedure, but it looks kinda akward:


procedure bank;
begin
if not(BankScreen) then
begin
// open the banks..
end;
end;

procedure fletch;
begin
if not (Bankscreen) then
begin
// not at bankscreen, we can fletch now.
end;
end;

procedure mainloop;
begin
while(Pixelshiftinv > 100) do //psuedo -> we are busy doing something in inventory, perhaps fletching?
Wait(1000);
if(InvLog < 1) then
bank;
if(InvLog > 1) then
Fletch;
end;


What could I use to code more efficient?

I'm thinking off a having a State loop that returns a value. Then in the mainloop use a case or if loops too see what the current status is:

For example:

if(StateFunction = 'Fletching') then
Wait(1000);
if(StateFunction = 'NoLogs') then
Bank;
if(StateFunction = 'Logs') then
Fletch;

What is good way to program this? It's not really a question regarding a particulair function, it's a way of programming logic that I lack off.

Sin

2014-08-20 15:14:38

Here's my fletching script, feel free to take snippets

program new;
{$DEFINE SMART}
{$I SRL-6/SRL.Simba}
{$I Socks.Simba}

const PIN = '';


var
fletched, fph:Integer;
xp,xph:Integer;


procedure declarePlayers;
begin
setLength(players, 1);
currentPlayer := 0;
with players[0] do
begin
//Basic Information
loginName := '';
password := '';
world := 100;
isactive := true;
end;
end

procedure SetupLogin;
begin
ClearDebug;
DeclarePlayers;
smartEnableDrawing := True;
SetupSRL;
end;

function bankIsOpen:Boolean;
var
TPA:TPointArray;
t:TPoint;
begin
colorSetting(2, 0.01, 0.61);
SetLength(TPA, 0);
while length(TPA) = 0 do
begin
FindColorsSpiralTolerance(T.y,T.y,TPA,307678,IntToBox(225,17,380,30), 7);
writeln(Length(TPA));
end;
Result := Length(TPA) > 20;
end;

function openSBankChest:Boolean;
var
p:TPoint;
tick:Integer;
begin
Result := false;
if bankScreen.open(BANK_CHEST_SHANTAY) then
begin
_waitBankOrPinScreen(45000);
if (pinScreen.isOpen()) then
begin
if pinScreen.enter(PIN) then
Result := true;
end;
if (bankIsOpen) then
Result := true;
end else
begin
MouseBox(IntToBox(755,328,791,561), MOUSE_MOVE);
if random(3) = 1 then mainScreen.setAngle(MS_ANGLE_HIGH);
minimap.setAngle(RandomRange(0,359));
end;
end;

function withdrawIngs:Boolean;
var
i:Integer;
begin
if openSBankChest then
begin
typeSend('2', false);
bankScreen.close();
tabBackpack.waitWhileLocked();
Result := tabBackpack.isFull();
end;
end;

Function Antiban: Boolean;
Var
I: Integer;
Begin
I := Random(600);
Case I Of
2..7:
Begin
hoverSkill(Skill_Fletching);
Wait(GaussRangeInt(600,2000));
End;
8..50: minimap.setAngle(RandomRange(0,265));
51..100: SleepAndMoveMouse(GaussRangeInt(100,5000));
101..400:
Begin
MouseOffClient(Random(4));
Wait(GaussRangeInt(0, 5000));
End;
501..600: Wait(GaussRangeInt(0, 2000));
End;
Wait(GaussRangeInt(0, 3000));
End;

procedure LoadPaint();
begin
fph := Round(fletched * (3600000/gettimerunning()));
xph := Round(xp * (3600000/gettimerunning()));
smartImage.clear();
smartImage.drawBitmap('C:\Simba\Scripts\S1NFletcher\paint.png', Point(0,360));
smartImage.drawText(timeRunning, Point(264,475), clBlack);
smartImage.drawText(ToStr(xp) + ' experience gained (' + ToStr(xph) + ' per hour)', Point(264,515), clBlack);
smartimage.drawText(ToStr(fletched) + ' logs fletched(' + ToStr(fph) + ' per hour)', Point(264,555), clBlack);
end;

function fletch:boolean;
var
m:TTimeMarker;
begin
tabBackpack.mouseSlot(RandomRange(1,25), MOUSE_MOVE);
if isMouseOverText(['Craft M', 'Craft Map', 'Craft'], 500) then
FastClick(MOUSE_LEFT) else
exit;
m.start();
while (not (productionScreen.isOpen())) do
begin
Wait(250);
if toolScreen.isOpen() then
if toolScreen.select('Knife') then
Wait(3000);
if m.getTime() > 10000 then
exit;
end;
productionScreen.clickStart();
m.reset();
m.start()
while (progressScreen.isOpen(500)) do
begin
Wait(RandomRange(250,400));
Antiban;
if m.getTime() > 120000 then
exit;
end;
Result := tabBackpack.count() < 28;
fletched := fletched + 28;
xp := 58 * fletched;
loadPaint();
end;

procedure switchW100;
begin
if players[0].loginToLobby then
begin
MouseBox(IntToBox(177,119,241,128), MOUSE_LEFT);
Wait(2000);
MouseBox(IntToBox(636,549,665,557), MOUSE_LEFT);
repeat
wait(1000);
until IsLoggedIn;
end;
end;
var
MS:MAIL;

procedure composeMessage;
var
s:String;
begin
s :='Time Running: ' + TimeRunning + ', Gained ' + ToStr(xp) + ' xp, and fletched ' +
ToStr(fletched) + ' bows';
MS.Create('smtp.gmail.com', 465, '', '', '', 'Fletching Script');
MS.SetMessage(s);
MS.SetFromName('Sin');
MS.SetToName('Sin');
MS.SendMail;
MS.Free;
end;

var
emailMarker:TTimeMarker;
begin
SetupLogin;
emailMarker.start();
loadPaint();
repeat
if not players[0].isActive then
players[0].isActive := true;
if not isLoggedIn() then
begin
players[0].login();
end;
repeat
if emailMarker.getTime() > 3600000 then
begin
composeMessage();
emailMarker.reset();
emailMarker.start();
end;
if withdrawIngs then
fletch;
until not IsLoggedIn();
until false;
end.

This is REALLY rudimentary, with lots of bad code practices (infinite loops etc), but it gets the job done very effectively.

Eduard

2014-08-20 15:55:26

Thanks for the effort sir! I really appreciate it.
Here's my (badly coded also lol) proggy.


With other words, I'm just trying to improve my script efficienty and having a better and more structured code.

For example, this explains what i'm trying to simpifly:



procedure StartFletching;
begin
if not Bankscreen then
begin
if GameTab(tab_Inv) then
begin
if(Knife) then
begin
if not (PixelshiftInventory > 0) then
begin
if(LogsInInv) then
begin
//BeginFletching!;
end;
end;
end;
end;
end else
CloseBank;
end;

procedure StartBanking;
begin
if GameTab(tab_Inv) then
begin
if(Knife) then
begin
if not (PixelshiftInventory > 0) then
begin
if not (LogsInInv) then
begin
if not BankScreen then
begin
//openbankprocedure
end else
//deposit inventory
end else
begin
if not Bankscreen then
begin
//fletch logs
end else
CloseBank;
end;
end;
end;
end;
end;

procedure MainLoop;
begin
StartFletching;
StartBanking;
end;

As you can see, i'm calling all the failsafes at every procedure. But honestly, i think another style of progragmming is better. This kind of loops can mess up so easily. Any idea's?

Sin

2014-08-20 16:51:33

Your state idea is great. You could use case statements and create it into a FSM-type (Finite state machine) program.

The Mayor

2014-08-20 16:59:01

Just to further Sin'a point, there was a thread about FSM style scripts a while back, if you want to take a look.

bonsai

2014-08-20 17:07:21

There's a state machine style skeleton in my walkthrough thread.

One thing I learned from using this style is that it's real easy to get into the situation where it tests the same thing more than once. The coding is easy and the script starts right up no matter where you leave off. But it can act kind of strange depending on what you put into the state checks, which order, etc.

Eduard

2014-08-20 17:17:29

Yeah, it's a totaly different way of coding / thinking. But indeed, it can pick up everywhere due to the state checking script flow. Also, it's much easier to create a big reset if something messes up. Like, for a fletcher at PC, which almost cannot go wrong (no randoms etc). it would be awesome if the script logged out, or it messes up due too lagg it would completely reset itself. Start over, etc.

NKN

2014-08-20 17:24:48

From: Eduard
Yeah, it's a totaly different way of coding / thinking. But indeed, it can pick up everywhere due to the state checking script flow. Also, it's much easier to create a big reset if something messes up. Like, for a fletcher at PC, which almost cannot go wrong (no randoms etc). it would be awesome if the script logged out, or it messes up due too lagg it would completely reset itself. Start over, etc.


Finite state is good for small projects, but I wouldn't advise it for much larger ones.

Eduard

2014-08-20 17:35:51

Let's say you would need to make a script that is so water proof, almost atrificial intelligence look-a-like. Would u suggest just nailing every possible scenario in a big function that you would call before every procedure?

What is the designer plan for a waterproof all functioning intelligent script?

NKN

2014-08-20 17:51:04

From: Eduard
Let's say you would need to make a script that is so water proof, almost atrificial intelligence look-a-like. Would u suggest just nailing every possible scenario in a big function that you would call before every procedure?

What is the designer plan for a waterproof all functioning intelligent script?


procedure MainLoop()
begin
if not isLoggedIn() then
exit;
if(not(tabBackpack.isOpen())) then
tabBackpack.open();
if(tabBackpack.count >= 3) then
begin
GotoBank();
Bank();
end;
if(Distance(sps.getPlayerPos(),Point(462, 385)) > 10) then
begin
WalkToWine();
end else
begin
fixPlayerPos();
mainscreen.setZoom(false);
while(not(tabBackpack.isFull())and(isLoggedIn())) do
begin
getWine(savedX,savedY);
wait(1000+random(1000));
end;
mainscreen.setZoom(true);
end;
if((not tabBackpack.isItemInSlot(1)) or (not tabBackpack.isItemInSlot(2))) then
begin
if not isLoggedIn() then
exit;
Writeln('Ran out of runes!');
TerminateScript;
end;
savedX := 0;
savedY := 0;
end;

My mainloop, I add failsafes at the start of EVERY procedure to exit the method if not logged in, if missing supplies, etc, and my mainloop will fix it.

Runs for days.