SRL Archive - SRL Snippets

worldSwitcher


KeepBotting

2014-06-07 15:11:58

Made this for a script I'm writing, was really pleased. Decided to release.

It's better than SRL's inbuilt worldswitcher imo.

It even compensates for the weird error where SRL will scroll the whole world list 2-3 times, mouse up to (0, 0) and then just click play without selecting a world.

function worldSwitcher(worldNum:integer):boolean;
var
theWorld:integer;
p:TPoint;
begin
writedebug('worldSwitcher: preparing to switch worlds!');
players[currentPlayer].exitToLobby();
lobby.openTab(LOBBY_WORLDS);
lastWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: lastWorld is ' + toStr(lastWorld) + '.');
if worldNum > 0 then
begin
writeDebug('worldSwitcher: switching to world ' + toStr(worldNum) + '.');
lobbyWorlds.selectWorld(worldNum);
login2;
exit(true);
end;
if (not lobbyWorlds._isWorldValid(worldNum) or worldNum = 0) then //srl thinks 0 is invalid
begin
writeError('*** FATAL ERROR: worldSwitcher: worldNum is not a valid world!');
exit(false);
terminateScript();
end else
begin
if worldNum <= 0 then
begin
writeDebug('worldSwitcher: switching to random world.');
lobbyWorlds.selectRandomWorld(isMembers);
theWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: theWorld is ' + toStr(theWorld) + '.');
if (theWorld = lastWorld) then
begin
writeWarn('worldSwitcher: Oh no! theWorld = lastWorld! Choosing again...');
lobbyWorlds.selectRandomWorld(isMembers); //select another
end;
writeDebug('worldSwitcher: selected random world ' + toStr(theWorld) + '.');
login2;
exit(true);
end;
end;
end;

lastWorld is declared globally. Can't remember why.

isMembers is declared as a global boolean that users should set true/false, depending on if they're a member.

procedure login2();
var
p:TPoint;
begin
lobby.findPlayButton(p);
writeDebug('worldSwitcher: clicking play.');
mouse(p.rand(randomRange(-50, 50), randomRange(-10, 10)), MOUSE_LEFT);
repeat
wait(500);
until isLoggedIn();
end;

All login2 does is press the play button at the lobby. I had a reason for isolating it from SRL but can't remember what it was. Use login2, make your own, or let SRL do it.


procedure writeDebug(text:string);
begin
{$IFDEF debug_on} writeLn('[DEBUG] : ' + text); {$ENDIF}
end;

procedure writeError(text:string);
begin
{$IFDEF debug_on}
writeln('');
writeLn('[ERROR] : ' + text);
writeln('');
{$ENDIF}
end;

writeDebug and writeError are procedures specific to my script, use them or replace with writeLn.

Usage:

worldSwitcher(0); //switches to random world
worldSwitcher(69); //switches to world 69

Cheers. If you've any bugs or suggestions, please post them.

Sawyer

2014-06-09 19:23:16

From: KeepBotting
Made this for a script I'm writing, was really pleased. Decided to release.

It's better than SRL's inbuilt worldswitcher imo.

It even compensates for the weird error where SRL will scroll the whole world list 2-3 times, mouse up to (0, 0) and then just click play without selecting a world.

function worldSwitcher(worldNum:integer):boolean;
var
theWorld:integer;
p:TPoint;
begin
writedebug('worldSwitcher: preparing to switch worlds!');
players[currentPlayer].exitToLobby();
lobby.openTab(LOBBY_WORLDS);
lastWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: lastWorld is ' + toStr(lastWorld) + '.');
if worldNum > 0 then
begin
writeDebug('worldSwitcher: switching to world ' + toStr(worldNum) + '.');
lobbyWorlds.selectWorld(worldNum);
login2;
exit(true);
end;
if (not lobbyWorlds._isWorldValid(worldNum) or worldNum = 0) then //srl thinks 0 is invalid
begin
writeError('*** FATAL ERROR: worldSwitcher: worldNum is not a valid world!');
exit(false);
terminateScript();
end else
begin
if worldNum <= 0 then
begin
writeDebug('worldSwitcher: switching to random world.');
lobbyWorlds.selectRandomWorld(isMembers);
theWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: theWorld is ' + toStr(theWorld) + '.');
if (theWorld = lastWorld) then
begin
writeWarn('worldSwitcher: Oh no! theWorld = lastWorld! Choosing again...');
lobbyWorlds.selectRandomWorld(isMembers); //select another
end;
writeDebug('worldSwitcher: selected random world ' + toStr(theWorld) + '.');
login2;
exit(true);
end;
end;
end;

lastWorld is declared globally. Can't remember why.

isMembers is declared as a global boolean that users should set true/false, depending on if they're a member.

procedure login2();
var
p:TPoint;
begin
lobby.findPlayButton(p);
writeDebug('worldSwitcher: clicking play.');
mouse(p.rand(randomRange(-50, 50), randomRange(-10, 10)), MOUSE_LEFT);
repeat
wait(500);
until isLoggedIn();
end;

All login2 does is press the play button at the lobby. I had a reason for isolating it from SRL but can't remember what it was. Use login2, make your own, or let SRL do it.


procedure writeDebug(text:string);
begin
{$IFDEF debug_on} writeLn('[DEBUG] : ' + text); {$ENDIF}
end;

procedure writeError(text:string);
begin
{$IFDEF debug_on}
writeln('');
writeLn('[ERROR] : ' + text);
writeln('');
{$ENDIF}
end;

writeDebug and writeError are procedures specific to my script, use them or replace with writeLn.

Usage:

worldSwitcher(0); //switches to random world
worldSwitcher(69); //switches to world 69

Cheers. If you've any bugs or suggestions, please post them.


Nice job. I will test it out and tell you any bugs I find.

Turpinator

2014-06-09 20:39:03

function worldSwitcher(worldNum:integer):boolean;
...
end {else //Redundant
begin //Redundant
if worldNum <= 0 then //Redundant
begin} //Redundant
writeDebug('worldSwitcher: switching to random world.');
lobbyWorlds.selectRandomWorld(isMembers);
theWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: theWorld is ' + toStr(theWorld) + '.');
if (theWorld = lastWorld) then
begin
writeWarn('worldSwitcher: Oh no! theWorld = lastWorld! Choosing again...');
lobbyWorlds.selectRandomWorld(isMembers); //select another
end;
writeDebug('worldSwitcher: selected random world ' + toStr(theWorld) + '.');
login2;
exit(true);
{end; //not req
end;} //not req
end;
and those else/if thens are redundant as the only way to reach them is for them to be true.
so. not needed.


procedure login2();
...
repeat
wait(500);
until isLoggedIn();
end;
Infinite loop.

Instead of replacing all the login funcs with login2() in a script, you can simply do
procedure login; override;
begin
codestuffhere;
end;
and no functions need to be replaced throughout a script.

but isnt it TPlayers.login() anyway?
so it should really just be
function TPlayers.login(): boolean; override;
begin
codestuffhere;
end;

or something of that sort.

KeepBotting

2014-06-09 21:23:47

From: Turpinator
function worldSwitcher(worldNum:integer):boolean;
...
end {else //Redundant
begin //Redundant
if worldNum <= 0 then //Redundant
begin} //Redundant
writeDebug('worldSwitcher: switching to random world.');
lobbyWorlds.selectRandomWorld(isMembers);
theWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: theWorld is ' + toStr(theWorld) + '.');
if (theWorld = lastWorld) then
begin
writeWarn('worldSwitcher: Oh no! theWorld = lastWorld! Choosing again...');
lobbyWorlds.selectRandomWorld(isMembers); //select another
end;
writeDebug('worldSwitcher: selected random world ' + toStr(theWorld) + '.');
login2;
exit(true);
{end; //not req
end;} //not req
end;
and those else/if thens are redundant as the only way to reach them is for them to be true.
so. not needed.
That part is for distinguishing between switching to a specific world and switching to a random world. If I removed them, the function wouldn't know what 0 meant. Right?



From: Turpinator

procedure login2();
...
repeat
wait(500);
until isLoggedIn();
end;
Infinite loop.
Good point. I'll fix that.

From: Turpinator

Instead of replacing all the login funcs with login2() in a script, you can simply do
procedure login; override;
begin
codestuffhere;
end;
and no functions need to be replaced throughout a script.

but isnt it TPlayers.login() anyway?
so it should really just be
function TPlayers.login(): boolean; override;
begin
codestuffhere;
end;

or something of that sort.
Sounds sensible but I'm not familiar with overridden or overloaded functions :P I'll have to look into them before I attempt that.

Thanks for the feedback :)

Turpinator

2014-06-09 21:54:34

From: KeepBotting
That part is for distinguishing between switching to a specific world and switching to a random world. If I removed them, the function wouldn't know what 0 meant. Right?

Nope. lets break it down into a flow chart.
forgive my crudely drawn chart.

so we start out and check if its greater than 0. if so, login and exit the func.

check if its an invalid world or if its not equal to 0. if so, exit the func, then terminate?

now we check if its greater or equal to 0. but wait. after our first check, we know the number is 0 or less.
after our second check, we confirm the number is 0.
and our final check sees if it is less than or equal to 0. which we already knew the answer to after our first if statement.

(the red circled branch can never be reached)

so… just an idea, but instead of making your second check be for an invalid world or not equal to 0, make it invalid or less than or equal to 0. so then you can type in -1 for a world and it will still do random.


If… you didnt have exits after each world number check, you would need the part i noted. but since you exit, you dont.

KeepBotting

2014-06-09 22:04:57

From: Turpinator
Nope. lets break it down into a flow chart.
forgive my crudely drawn chart.

so we start out and check if its greater than 0. if so, login and exit the func.

check if its an invalid world or if its not equal to 0. if so, exit the func, then terminate?

now we check if its greater or equal to 0. but wait. after our first check, we know the number is 0 or less.
after our second check, we confirm the number is 0.
and our final check sees if it is less than or equal to 0. which we already knew the answer to after our first if statement.

(the red circled branch can never be reached)

so… just an idea, but instead of making your second check be for an invalid world or not equal to 0, make it invalid or less than or equal to 0. so then you can type in -1 for a world and it will still do random.


If… you didnt have exits after each world number check, you would need the part i noted. but since you exit, you dont.

So the only purpose of adding an entire additional branch of logic is to make the function accept numbers less than 0 as random?

why not just type in 0

;-;

Turpinator

2014-06-09 22:16:55

From: KeepBotting
So the only purpose of adding an entire additional branch of logic is to make the function accept numbers less than 0 as random?

why not just type in 0

;-;


lets remember, people are idiots.

function worldSwitcher(worldNum:integer):boolean;
var
theWorld:integer;
p:TPoint;
begin
writedebug('worldSwitcher: preparing to switch worlds!');
players[currentPlayer].exitToLobby();
lobby.openTab(LOBBY_WORLDS);
lastWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: lastWorld is ' + toStr(lastWorld) + '.');
if worldNum > 0 then
begin
writeDebug('worldSwitcher: switching to world ' + toStr(worldNum) + '.');
lobbyWorlds.selectWorld(worldNum);
login2;
exit(true);
end;
if (not (lobbyWorlds._isWorldValid(worldNum) or (worldNum => 0))) then //srl thinks 0 is invalid /// this is the change
begin
writeError('*** FATAL ERROR: worldSwitcher: worldNum is not a valid world!');
exit(false);
terminateScript();
end else
begin
if worldNum <= 0 then
begin
writeDebug('worldSwitcher: switching to random world.');
lobbyWorlds.selectRandomWorld(isMembers);
theWorld := lobbyWorlds.getCurrentWorld();
writeDebug('worldSwitcher: theWorld is ' + toStr(theWorld) + '.');
if (theWorld = lastWorld) then
begin
writeWarn('worldSwitcher: Oh no! theWorld = lastWorld! Choosing again...');
lobbyWorlds.selectRandomWorld(isMembers); //select another
end;
writeDebug('worldSwitcher: selected random world ' + toStr(theWorld) + '.');
login2;
exit(true);
end;
end;
end;
done.