SRL Archive - MSI Suggestions

MSI_FindMSObject()


marpis

2009-12-08 07:59:16

Here's the base I made for my "own include".
Note that it has a new TMSObject with new properties.

I don't have time to change the M_ to MSI_ now, and there are still some M_Debug() things too.

I haven't been able to test the whole animation thing through, you guys try this out?

Actions in M_FindObject()
  • Nothing - Do not even move the mouse
  • Move - Hover object
  • ClickRight - Click right OR hover but not click, depending on LeftOnly
  • ClickLeft - Click left and chooseoption OR click right, depending on LeftOnly



Type
TMSObject = Record
UpText : String;
Option : String;
Colors : TIntegerArray;
FindAllColors : Boolean; // Result False if not ALL colors are found
Tol : Integer;
Hue : Extended;
Sat : Extended;
W, H : Integer;
Accuracy : Integer; // Min pixels to be found
ExcludeSelf : Boolean; // Exclude pixels in a box, the box might need revamping / making it dynamic
LeftOnly : Boolean;
Tries : Integer;
Anim : Boolean; // Result False if not animating
WaitToMove : Boolean; // When ClickLeft, wait till we have stopped moving?
BadColors : TIntegerArray;
BadColorTolerance: Integer;
End;
TMSObjectArray = Array Of TMSObject;

{*******************************************************************************
Function M_FindObjectsTPA( ... ): Boolean;
By: marpis
Description: Finds objects in mainscreen, saves co-ords in TPA
*******************************************************************************}
Function M_FindObjectsTPA(Var Objs: TPointArray; Width, Height: Integer;
Colors: TIntegerArray; Accuracy, Tolerance: Integer; HueMod, SatMod: Extended;
FindAllColors, ExcludeSelf, CheckAnimation: Boolean;
BadColors: TIntegerArray; BadColorTolerance: Integer): Boolean;
Var
TPA, TempTPA: TPointArray;
ATPA: T2DPointArray;
I, II, CTS, TMP, H: Integer;
Box: TBox;
Boxes: TBoxArray;
Animating2: TBooleanArray;
BadPoint: Boolean;
Begin
If (Not LoggedIn) Then Exit;

CTS := GetColorToleranceSpeed;
ColorToleranceSpeed(2);
SetColorSpeed2Modifiers(HueMod, SatMod);
Box := IntToBox(228, 150, 275, 200);

H := High(Colors);
SetLength(ATPA, H + 1);

For I := 0 To H Do
FindColorsSpiralTolerance(MSCX, MSCY, ATPA[I], Colors[I], MSX1, MSY1, MSX2, MSY2, Tolerance);

ColorToleranceSpeed(CTS);
SetColorSpeed2Modifiers(0.2, 0.2);

{ Check if some of the colors was not on MS. }
If FindAllColors Then
For I := 0 To H Do
If (Length(ATPA) < 1) Then
Begin
Objs := [];
Exit;
End;

{ Check if we have enough pixels on MS. }
TPA := MergeATPA(ATPA);
If (Length(TPA) < Accuracy) Then
Begin
M_Debug('No colors found!');
Objs := [];
Exit;
End;

SortATPAFrom(ATPA, Point(MMCX, MMCY));
SetLength(TPA, H + 1);

For I := 0 To H Do
TPA[I] := MiddleTPA(ATPA[I]);

H := High(TPA);
SetLength(Boxes, H + 1);

For I := 0 To H Do
Begin
Boxes[I].X1 := TPA[I].X - (Width / 2);
Boxes[I].Y1 := TPA[I].Y - (Height / 2);
Boxes[I].X2 := TPA[I].X + (Width / 2);
Boxes[I].Y2 := TPA[I].Y + (Width / 2);
End;

If CheckAnimation Then
Animating2 := M_AnimatingMulti(Boxes, 100, 1);

For I := 0 To High(TPA) Do
Begin
BadPoint := False;

{ Animation }
If (Not BadPoint) Then
If CheckAnimation Then
If (Not Animating2[I]) Then
Begin
M_Debug('Not animating!');
BadPoint := True;
End;

{ Exclude own character }
If (Not BadPoint) Then
If ExcludeSelf Then
If PointInBox(TPA[I], Box) Then
Begin
M_Debug('Own character!');
BadPoint := True;
End;

{ Invalid point }
If (Not BadPoint) Then
If ((TPA[I].X + TPA[I].Y) < 1) Then
Begin
M_Debug('Invalid point!');
BadPoint := True;
End;

{ Accuracy }
If (Not BadPoint) Then
If (Length(ATPA[I]) < Accuracy) Then
Begin
M_Debug('Not enough points!');
BadPoint := True;
End;

{ Bad colors }
If (Not BadPoint) Then
For II := 0 To High(BadColors) Do
Begin
M_Debug('Looking for bad color: '+IntToStr(BadColors[II]));
If FindColorSpiralTolerance(TMP, TMP, BadColors[II], TPA[I].X - (Width / 2), TPA[I].Y - (Height / 2), TPA[I].X + (Width / 2), TPA[I].Y + (Height / 2), BadColorTolerance) Then
Begin
M_Debug('Found bad colors!');
BadPoint := True;
Break;
End;
End;

{ FindAllColors }
If (Not BadPoint) Then
If FindAllColors Then
For II := 0 To H Do
Begin
TempTPA := M_ReturnPointsInBox(ATPA[II], IntToBox(TPA[I].X - (Width / 2), TPA[I].Y - (Height / 2), TPA[I].X + (Width / 2), TPA[I].Y + (Height / 2)));
If (Length(TempTPA) < 1) Then
Begin
M_Debug('Didn''t find all colors!');
BadPoint := True;
Break;
End;
End;

{ Valid point! }
If (Not BadPoint) Then
Begin
SetLength(Objs, Length(Objs) + 1);
Objs[High(Objs)] := TPA[I];
End;
End;

Result := (Length(Objs) > 0);
End;


{*******************************************************************************
Function M_FindObjects(Objs: TIntegerArray; Action: fnct_ActionOptions): Boolean;
By: marpis
Description: Finds objects in mainscreen
*******************************************************************************}
Function M_FindObjects(Objs: TMSObjectArray; Action: fnct_ActionOptions): Boolean;
Var
TPA: TPointArray;
Options, UpTexts: TStringArray;
I, II, III, X, Y, HH, HHH, Hi: Integer;
CheckUpText: Boolean;
Begin
Hi := High(Objs);

SetLength(Options, Hi + 1);
SetLength(UpTexts, Hi + 1);
For I := 0 To Hi Do
Begin
UpTexts[I] := Objs[I].UpText;
Options[I] := Objs[I].Option;
End;

For II := 0 To Hi Do
With Objs[II] Do
If M_FindObjectsTPA(TPA, W, H, Colors, Accuracy, Tol, Hue, Sat, FindAllColors, ExcludeSelf, Anim, BadColors, BadColorTolerance) Then
Begin
HH := Min((Objs[II].Tries - 1), High(TPA));

For I := 0 to HH Do
Begin
M_Debug('Object found!');

If (Action = Nothing) Then
Begin
Result := True;
Exit;
End;

MMouse(TPA[I].X, TPA[I].Y, 2, 2);
If (Action = Move) Then
Begin
Result := True;
Exit;
End;

HHH := High(UpTexts);
CheckUpText := True;
For III := 0 To HHH Do
If (UpTexts[III] = '') Then
Begin
CheckUpText := False;
Break;
End;

If CheckUpText Then
Result := WaitUpTextMulti(UpTexts, 50 + Random(20))
Else
Result := True;

If Result Then
Begin
GetMousePos(X, Y);
If (Objs[II].LeftOnly) Or InRange(Random(100), 0, 80) Then
Begin
Mouse(X, Y, 0, 0, False);;
If (Action = ClickLeft) Then
Result := M_WaitOptions(Options, 500 + Random(100))
Else
Result := True;
End Else
Begin
Mouse(X, Y, 0, 0, True);
Result := DidRedClick;
End;

If Objs[II].WaitToMove Then
If WaitFunc(@M_IsMoving, 15, 2000 + Random(200)) Then
While M_IsMoving Do
Wait(10 + Random(5));

Exit;
End Else
M_Debug('...but no UpText! ('+GetUpText+')');
End;
End;

M_Debug('Objects not found!');
End;

TEST SCRIPT

Program TestMSObj;
{.include srl/srl.scar}

Var
MyObject: TMSObject;

Begin
SetupSRL;
With MyObject Do
Begin
Colors := [];
FindAllColors := False;
Tol := 10;
Hue := 0.2;
Sat := 0.2;
W := 10;
H := 10;
Accuracy := 20;
UpText := 'uptext';
Option := 'option';
LeftOnly := True;
Tries := 3;
ExcludeSelf := True;
Anim := False;
WaitToMove := True;
BadColors := [];
BadColorTolerance := 10;
End;

If M_FindObjects([MyObject], ClickLeft) Then
WriteLn('Groovy babe!');

Sir R. M8gic1an

2009-12-08 08:03:00

UpText and Options should be Arrays. Other than that, I love you.

edit: I see too mny hard coded values. 0, 1 and 2 are fine, the others need constants.

~RM

marpis

2009-12-08 08:07:12

From: Rasta Magician
UpText and Options should be Arrays. Other than that, I love you.

edit: I see too mny hard coded values. 0, 1 and 2 are fine, the others need constants.

~RM


Yea well, I have only like 15 minutes time here before I go to class so I didn't even try to make it MSI compatible yet :) But that's my idea though… Too complicated?

Sir R. M8gic1an

2009-12-08 08:09:28

From: marpis
Yea well, I have only like 15 minutes time here before I go to class so I didn't even try to make it MSI compatible yet :) But that's my idea though… Too complicated?


No, looks great, as I said, 'I love you' :p

~RM

Coh3n

2009-12-08 11:17:58

Looks awesome. RM, do you want all the waits as constants too?

NCDS

2009-12-08 18:20:59

I've spent countless hours at work just brainstorming idea's for an MSObj finding function for MSI as I never was satisfied with what I posted for it.

I really like what you've done with thing's though :)