SRL Archive - General

Inefficiency in MSI


Sir R. M8gic1an

2010-12-05 17:31:48

I found an inefficiency and I decided to post it here for learning purposes:

function MSI_AutoInvFull(Func: function(a: TIntegerArray; var b: Boolean): Boolean): Boolean;
var
t, waitTime, tTime: Integer;
tObjects, newObjects: TIntegerArray;
forceFail: Boolean;
begin
t := (GetSystemTime + (5 * 60000));

repeat
while (not InvFull) and (LoggedIn) do
with MSI_Players[CurrentPlayer] do
begin
case (MSI_Scripts[Scripts[CurrentScript].Name].SkillConst) of
SKILL_WOODCUTTING: tObjects := Scripts[CurrentScript].Trees;
SKILL_MINING: tObjects := Scripts[CurrentScript].Rocks;
SKILL_FISHING: tObjects := Scripts[CurrentScript].FishSpots;
end;


Why are you assigning an array in a loop?
It's looping the assignment of a variable that is not changing, every time it loops, accessing the memory every time it loops, etc.


case (MSI_Scripts[Scripts[CurrentScript].Name].SkillConst) of
SKILL_WOODCUTTING: tObjects := Scripts[CurrentScript].Trees;
SKILL_MINING: tObjects := Scripts[CurrentScript].Rocks;
SKILL_FISHING: tObjects := Scripts[CurrentScript].FishSpots;
end;

repeat
while (not InvFull) do
begin
if not LoggedIn then break;

@Simba: Does simba have short circuit evaluation? Is PascalScript the limitation on it?


And yeh.. I'm currently looking for a bug in MSI :p (pickaxe drop)

~RM

Coh3n

2010-12-05 19:47:06

Nice find. There's probably more. That didn't even cross my mind when I wrote it.

Also, since when does it drop the pickaxe?

E: http://villavu.com/forum/showthread.php?p=755516#post755516 ;)

Gu3st...

2010-12-06 02:05:29

if (Condition1) and (Condition2) and (Condition3) then
is the same as
if Condition1 then
if Condition2 then
if Condition3 then

Coh3n

2010-12-06 03:43:34

From: Zyt3x
if (Condition1) and (Condition2) and (Condition3) then
is the same as
if Condition1 then
if Condition2 then
if Condition3 then

What does that have to do with this thread? :o

Coh3n

2010-12-06 05:20:55

Committed this fix, RM.

mastaraymond

2010-12-06 12:20:57


program new;
function shortcutelev(num : integer) : boolean;
begin
writeln(num);
result := false;
end;
begin
if shortcutelev(1) or not shortcutelev(2) or shortcutelev(3) then
writeln('test');
end.
Test and see ;-).

Coh3n

2010-12-06 12:31:12

That's different than what he posted. Yours doesn't call the third command because of the first two returns true. In his, they all have to be true. I still don't understand his point. :o

Nava2

2010-12-06 12:43:54

Okay, what raymond is trying to explain is Short-circuit evalation of booleans.

So, with ``and'', all the values placed into it MUST be true, otherwise it will return false. So what short-circuit evaluation does is this:

Assume the following statement:
Simba Code:
[LEFT]if (a and b and c) then
[/LEFT]


So, the evaluation would work as follows:
Is `a' true then continue. If `a' is FALSE then we know the statement will be FALSE regardless of the other values. Thus we end here and progress into the rest of the code.

Gu3st...

2010-12-06 13:57:55

From: Coh3n
What does that have to do with this thread? :o
This has to do about "@Simba: Does simba have short circuit evaluation? Is PascalScript the limitation on it?"

EDIT: I don't think I understand what you are asking… :p

Sir R. M8gic1an

2010-12-06 14:03:11


Compiled succesfully in 33 ms.
1
Successfully executed.


program new;

function name(int: integer): boolean;
begin
Result := false;
writeln(int);
end;

begin
if name(1) and name(2) and name(3) then
writeln('long circuit');
end.

It does have short-circuit evaluation;

this basically means that when it reaches a 'false' in an 'and' statement, it won't check any further.

Scar would've printed out
'123';

~RM

Coh3n

2010-12-06 15:52:32

I know what short circuit evaluation is. :p

All I wanted to know is why Zyt3x posted that. It didn't come with any sort of explanation. Lol.

Gu3st...

2010-12-06 16:01:02

From: Coh3n
I know what short circuit evaluation is. :p

All I wanted to know is why Zyt3x posted that. It didn't come with any sort of explanation. Lol.
Well.. Basically that post was aimed towards RM, because I though that he'd get the meaning behind it, which was, as RM later posted, that Simba does indeed have short circuit evaluation.

I just like to be short and precise… That's why I suck at writing tutorials

Coh3n

2010-12-06 17:51:41

From: Zyt3x
Well.. Basically that post was aimed towards RM, because I though that he'd get the meaning behind it, which was, as RM later posted, that Simba does indeed have short circuit evaluation.

I just like to be short and precise… That's why I suck at writing tutorials

But what did that have to do with the thread? I understood what you posted, I'm just so confused. I guess I'm just trying to say the post was out of context. You were just stating something that came out of nowhere.

E: Also, RM, I switched it back to the way it was. The setting of those variables need to be set in the loop because if the objects in the player's array were to be reset somewhere, tObjects wouldn't be. For example, when fishing, if the player is net and bait fishing and he runs out of bait, MSI_Players[].Scripts[].FishSpots is reset so it doesn't include bait fishing (since there is no bait left). If the setting of tObjects is outside the loop, FISHSPOT_BAIT will always be in the array, so the script will still try to bait fish even though there's no bait.

Gu3st...

2010-12-07 14:08:19

From: Coh3n
But what did that have to do with the thread? I understood what you posted, I'm just so confused. I guess I'm just trying to say the post was out of context. You were just stating something that came out of nowhere.
…

Let's take this over MSN.. I don't want this thread to be overflowed because of this… :o

Coh3n

2010-12-07 14:40:23

From: Zyt3x
…

Let's take this over MSN.. I don't want this thread to be overflowed because of this… :o

Alright. :p Apparently I'm completely missing something.