Runaway
2012-09-11 16:03:23
I've been working on some functions lately to handle the crafting menu. It's very similar to ChooseOption in that it grabs the option text and parses through it for the desired string. I put it through some testing and everything seems to be working fast and efficient :)
function CraftingScreen: Boolean;
begin
Result := CountColorTolerance(1131144, MCX1, MCY1, MCX2, MCY2, 3) >= 100;
end;
function ScrollCraftingMenu(Left: Boolean): Boolean;
var
Box: TBox;
P: TPoint;
t: Integer;
begin
Result := False;
if not CraftingScreen then
Exit;
case Left of
True: P := Point(12, 394);
False: P := Point(493, 394);
end;
Box := IntToBox(P.x, P.y, P.x + 12, P.y + 69);
Result := WaitColor(P.x, P.y, 791065, 1, 100);
if not Result then
Exit;
MouseTBox(Box, mouse_move);
MarkTime(t);
while not SimilarColors(GetColor(P.x, P.y), 1446924, 1) and (TimeFromMark(t) < 2000) do
Wait(100+Random(50));
end;
function GetCraftingOptions(var Options: TStringArray; var Points: TPointArray): Integer;
var
aTPA: T2DPointArray;
TPA: TPointArray;
Bounds: TBox;
P: TPoint;
lTPA, i: Integer;
begin
Result := 0;
if not CraftingScreen then
Exit;
SetLength(Options, 0);
SetLength(Points, 0);
FindColorsTolerance(TPA, 2070783, MCX1, MCY1 + 40, MCX2, MCY2, 0);
if (Length(TPA) < 1) then
Exit;
aTPA := SplitTPAEx(TPA, 15, 10);
SortATPAFromMidPoint(aTPA, Point(MCX1, MCCY));
lTPA := Length(aTPA);
SetLength(Options, lTPA);
SetLength(Points, lTPA);
for i := 0 to (lTPA - 1) do
begin
Bounds := GetTPABounds(aTPA[i]);
P := MiddleTPA(aTPA[i]);
Options[i] := GetTextAtExWrap(Bounds.x1, Bounds.y1, Bounds.x2, Bounds.y2, 0, 4, 2, 2070783, 0, statChars);
Points[i] := Point(P.x, P.y - 15);
if (Options[i] <> '') then
Inc(Result);
end;
end;
function CraftingOptionMulti(Text: TStringArray; Scroll: Boolean): Boolean;
var
Options: TStringArray;
Points: TPointArray;
Box: TBox;
M: TPoint;
hOption, hText, t, i, ii: Integer;
Left: Boolean;
begin
Result := False;
if not CraftingScreen then
Exit;
hText := High(Text);
t := 0;
repeat
if InRange(t, 1, 2) and Scroll then
begin
case t of
1: Left := True;
2: Left := False;
end;
ScrollCraftingMenu(Left);
end;
GetCraftingOptions(Options, Points);
if (Length(Options) < 1) then
Exit;
hOption := High(Options);
for i := 0 to hText do
begin
for ii := 0 to hOption do
begin
if (Pos(Text[i], Options[ii]) <> 0) then
begin
Result := True;
Box := IntToBox(Points[ii].x - 15, Points[ii].y - 15, Points[ii].x + 15, Points[ii].y + 15);
GetMousePos(M.x, M.y);
if PointInBox(M, Box) then
ClickMouse2(mouse_left)
else
MouseTBox(Box, mouse_left);
Wait(500+Random(250));
Exit;
end;
end;
end;
if not Scroll then
Exit;
Inc(t);
until(t > 2);
end;
function CraftingOption(Text: String; Scroll: Boolean): Boolean;
begin
Result := CraftingOptionMulti([Text], Scroll);
end;
function WaitCraftingOptionMulti(Time: Integer; Text: TStringArray; Scroll: Boolean): Boolean;
var
t: Integer;
begin
Result := False;
MarkTime(t);
while not Result and (TimeFromMark(t) < Time) do
Result := CraftingOptionMulti(Text, Scroll);
end;
function WaitCraftingOption(Time: Integer; Text: String; Scroll: Boolean): Boolean;
begin
Result := WaitCraftingOptionMulti(Time, [Text], Scroll);
end;