PDA

View Full Version : Some old snippet stuffs for GE



Le Jingle
01-31-2013, 06:51 AM
Figured I'd dump this snippet onto the SRL repo before completely deleting it / forgetting about it.

Snippet handles some GE functionality. since it's just a new born snippet, I CBA to explain anything, hopefully names will do most the explaining.
Note:

there are some incomplete functions/yet to be coded functions
USED some functions from previous users work (EXAMPLE = ZMON's FUZZY UPTEXT MATCHING, more if I've forgotten) .. General thanks to SRL Community :]



Cheers
-Lj


{
slot = slot integer
bData = box coordinates for 3 boxes, pertaining to;
[0] = entire slot coords
[1] = slot 'buy' box (used when no offer in slot)
[2] = slot 'sell' box (" ")
}
type GEBoxData = record
slot: integer;
status: TBox;
bData: array[0..2] of TBox;
end;

type GETrading = record // they stay the same
exactAmountButton: TBox;
QuantityCoord: array[0..4] of TBox;
end;

type GEData = record // combine to one
slotInfo: array[0..6] of GEBoxData;
tradeInfo: GETrading;
end;

const
GE_OFFER_GREEN = 1999423;
GE_OFFER_GREEN_SHADOW = 1533489;
GE_OFFER_RED = 1048714;
GE_OFFER_RED_SHADOW = 786540;

// Loads Data used for navigating the G.E. interface
function getGEData: GEData;
var
i: integer;
begin
for i := 0 to 2 do
begin
result.slotInfo[i].bData[0] := IntToBox(35+(i*156), 85, 171+(i*156), 191);
result.slotInfo[i].bData[1] := IntToBox(49+(i*156), 129, 93+(i*156), 170);
result.slotInfo[i].bData[2] := IntToBox(112+(i*156), 129, 156+(i*156), 170);
result.slotInfo[i].status := IntToBox(40+(i*156), 163, 167+(i*156), 179);
result.slotInfo[i].slot := i + 1;
end;
for i := 3 to 5 do
begin
result.slotInfo[i].bData[0] := IntToBox(35+((i-3)*156), 205, 171+((i-3)*156), 311);
result.slotInfo[i].bData[1] := IntToBox(49+((i-3)*156), 249, 93+((i-3)*156), 290);
result.slotInfo[i].bData[2] := IntToBox(112+((i-3)*156), 249, 156+((i-3)*156), 290);
result.slotInfo[i].status := IntToBox(40+((i-3)*156), 283, 167+((i-3)*156), 299);
result.slotInfo[i].slot := i + 1;
end;
for i := 0 to 4 do
result.tradeInfo.QuantityCoord[i] := IntToBox(57+(i*41), 210, 89+(i*41), 232);
result.tradeInfo.exactAmountButton := IntToBox(371, 210, 403, 232);
end;

function GE_Screen: boolean;
begin
result := Pos('rand', GetTextAtExWrap(208, 35, 313, 51, 0, 5, 2, 39116, 15, 'UpCharsEx')) > 0;
end;

function GE_Buy_Screen: boolean;
begin
result := Pos('Buy', GetTextAtExWrap(90, 70, 156, 86, 0, 5, 2, 39116, 15, 'UpCharsEx')) > 0;
end;

function GE_Sell_Screen: boolean;
begin
result := Pos('Sell', GetTextAtExWrap(90, 70, 156, 86, 0, 5, 2, 39116, 15, 'UpCharsEx')) > 0;
end;

// looks for the white in the arrow in the lower left corner to go 'Back'
function goBackPage: boolean;
var
p: TPoint;
TPA: TPointArray;
begin
result := false;
if not GE_Screen then
exit;
if (GE_Buy_Screen) or (GE_Sell_Screen) then
begin
if FindColorsTolerance(TPA, 11777988, 33, 335-50, 61, 306, 20) then
begin
p := MiddleTPA(TPA);
Mouse(p.x, p.y, 2, 2, mouse_left);
result := (not(GE_Buy_Screen)) or (not(GE_Sell_Screen));
end;
end;
end;

function getOfferState(color, slot: integer): boolean;
var
x, y: integer;
aBox: TBox;
tooMuchData: GEData;
begin
if not GE_Screen then exit;
goBackPage;

tooMuchData := getGEData;

aBox := tooMuchData.slotInfo[slot].status;

result := FindColorTolerance(x, y, color, aBox.X1, aBox.Y1, aBox.X2, aBox.Y2, 5);
end;

function getOfferStates(color: integer): TBooleanArray;
var
i: integer;
begin
SetLength(result, 6);
for i := 0 to 5 do
if getOfferState(color, i) then
result[i] := true;
end;

function getTotalEmptyOffers: integer;
var
i : integer;
begin
if not GE_Screen then exit;
goBackPage;
result := 0;
for i := 1 to 3 do
if (Pos('mpty', GetTextAtExWrap(84+((i-1)*156), 88,
123+((i-1)*156), 103,
0, 5, 2, 39372, 15, 'UpCharsEx')) > 0) then
result := result + i;
for i := 1 to 3 do
if (Pos('mpty', GetTextAtExWrap(84+((i-1)*156), 208,
123+((i-1)*156), 223,
0, 5, 2, 39372, 15, 'UpCharsEx')) > 0) then
result := result + i;
end;

function getTotalBuyOffers: integer;
begin
result := 6 - getTotalEmptyOffers;
end;

// buy = true; will click buy item box
// buy = false; will click sell item box
// waitPer = wait per loop for result to wait for function GE_Buy_Screen: boolean;
// maxWait = max time to wait before stopping search for function GE_Buy_Screen: boolean;
function getToFirstEmptySlot(buy: boolean; waitPer, maxWait:integer): boolean;
var
i: integer;
TBA: TBooleanArray;
tooMuchData: GEData;
begin
result := false;
if not GE_Screen then
exit;

if (buy and GE_Buy_Screen) or ((not buy) and GE_Sell_Screen) then
begin
result := true;
exit;
end;
goBackPage;

if not inRange(getTotalEmptyOffers, 1, 6) then
exit;

tooMuchData := getGEData;
SetLength(TBA, 6);

TBA := getOfferStates(0); // look for 0, or Black, for any offer status.

if buy then
begin
for i := 0 to 5 do
if not TBA[i] then
begin
MouseBox(tooMuchData.slotInfo[i].bData[1].X1,
tooMuchData.slotInfo[i].bData[1].Y1,
tooMuchData.slotInfo[i].bData[1].X2,
tooMuchData.slotInfo[i].bData[1].Y2,
mouse_left);
result := WaitFunc(@GE_Buy_Screen, waitPer, maxWait);
if (result) then
break;
end;
end else
begin
for i := 0 to 5 do
if not TBA[i] then
begin
MouseBox(tooMuchData.slotInfo[i].bData[2].X1,
tooMuchData.slotInfo[i].bData[2].Y1,
tooMuchData.slotInfo[i].bData[2].X2,
tooMuchData.slotInfo[i].bData[2].Y2,
mouse_left);
result := WaitFunc(@GE_Sell_Screen, waitPer, maxWait);
if (result) then
break;
end;
end;
end;

// Looks for text that signify's we're ready to send text for 'buy' offer
function GE_ItemSearch: boolean;
begin
result := Pos('rand', GetTextAtExWrap(208, 35, 313, 51, 0, 5, 2, 23200, 15, 'UpCharsEx')) > 0;
end;

procedure Type_GE_SendEx(Text: string; PressEnter: Boolean);
var
I: Integer;
begin
for i := 1 to Length(Text) do
begin
{$IFDEF SIMBAMAJOR980}
SendKeys(Text[i], 40 + Random(40));
{$ELSE}
SendKeys(Text[i], 30 + Random(30), 30 + Random(30));
{$ENDIF}
Wait(40 + Random(40));
end;
if (PressEnter) then TypeByte(VK_ENTER);
end;

type TMatchDegree = (MATCH_EXACT, MATCH_CASE_INSENSITIVE, MATCH_GOOD, MATCH_LOOSE);

function MatchTextFuzzyEx(MATCHSTRING,TARGETTEXT: String; DegreeOfMatch: TMatchDegree): Boolean; forward;
function SubStringMatchFuzzyEx(ONE, TWO: String; DegreeOfMatch: TMatchDegree): Integer; forward;

function MatchTextFuzzyEx(MATCHSTRING, TARGETTEXT: String; DegreeOfMatch: TMatchDegree): Boolean;
var
COUNT: Integer;
begin
Result := False;
if (Pos(MATCHSTRING, TARGETTEXT) > 0) then
begin
Result := True;
Exit;
end;
if DegreeOfMatch = MATCH_EXACT then Exit;
if DegreeOfMatch = MATCH_CASE_INSENSITIVE then
begin
if (Pos(Uppercase(MATCHSTRING), Uppercase(TARGETTEXT)) > 0) then
begin
Result := True;
Exit;
end;
end;
if (DegreeOfMatch = MATCH_CASE_INSENSITIVE) then Exit;
COUNT := SubStringMatchFuzzyEx(MATCHSTRING, TARGETTEXT, DegreeOfMatch);
if (COUNT = 0) then Exit; // no match
if (COUNT * 4 >= Length(MATCHSTRING) * 3) then
begin
Result := True;
Exit;
end;
if (DegreeOfMatch = MATCH_GOOD) then Exit;
if (COUNT * 2 >= Length(MATCHSTRING)) then
Result := True;
end;

var
___ACC : Integer;

function SubStringMatchFuzzyRec(__ONE, __TWO: String; OffsetOne, OffsetTwo: Integer; DeadStop: Integer): Integer;
var
MatchCountOne,MatchCountTwo,Index,ADJUSTMENT,ADJOf fsetOne,ADJOffsetTwo: Integer;
begin
Inc(___ACC);
Result := 0;
ADJUSTMENT := 0;
ADJOffsetOne := OffsetOne;
ADJOffsetTwo := OffsetTwo;
if (Length(__ONE) <= ADJOffsetOne) or (Length(__TWO) <= ADJOffsetTwo) or
(Length(__ONE) - ADJOffsetOne < DeadStop) or (Length(__TWO) - ADJOffsetTwo < DeadStop) then Exit;
while (__ONE[ADJOffsetOne + 1] = __TWO[ADJOffsetTwo + 1]) do
begin
Inc(ADJUSTMENT);
Inc(ADJOffsetOne);
Inc(ADJOffsetTwo);
if (Length(__ONE) <= ADJOffsetOne) or (Length(__TWO) <= ADJOffsetTwo) then
begin
Result := ADJUSTMENT;
Exit;
end;
end;
MatchCountOne := SubStringMatchFuzzyRec(__ONE, __TWO, ADJOffsetOne + 1, ADJOffsetTwo, DeadStop - ADJUSTMENT);
MatchCountTwo := 0;
for Index := ADJOffsetTwo + 2 to Length(__TWO) do
begin
if (__ONE[ADJOffsetOne + 1] = __TWO[Index]) then
begin
if (Index < Length(__TWO)) then
begin
if (Length(__ONE) > ADJOffsetOne + 1) then
begin
MatchCountTwo := SubStringMatchFuzzyRec(__ONE, __TWO,
ADJOffsetOne + 1, Index, DeadStop - ADJUSTMENT);
end;
end;
Inc(MatchCountTwo);
break;
end;
end;
Result := MatchCountOne;
if (MatchCountTwo > Result) then Result := MatchCountTwo;
Result := Result + ADJUSTMENT;
end;

function SubStringMatchFuzzy(ONE, TWO: String): Integer;
begin
Result := SubStringMatchFuzzyEx(ONE, TWO, MATCH_LOOSE);
end;

function SubStringMatchFuzzyEx(ONE, TWO : String; DegreeOfMatch : TMatchDegree) : Integer;
var
DeadStop,__LONE: Integer;
__ONE,__TWO: String;
begin
if (Length(ONE) > Length(TWO)) then
begin
__ONE := TWO;
__TWO := ONE;
end else
begin
__ONE := ONE;
__TWO := TWO;
end;
__LONE := Length(__ONE);
if (DegreeOfMatch = MATCH_LOOSE) then
DeadStop := (__LONE + 1) shr 1
else
DeadStop := ((__LONE shl 1) + __LONE + 3) shr 2;
Result := SubStringMatchFuzzyRec(__ONE, __TWO, 0, 0, DeadStop);
end;

function makeOffer(itemName:string): boolean;
var
t, tt: integer;
begin
result := false;
if not GE_Screen then
exit;
if not GE_Buy_Screen then
getToFirstEmptySlot(true, 100+random(100), 800);

Type_GE_SendEx(itemName, true);

MarkTime(t);
while (not result) or (timeFromMark(t) < 1500) do
begin // Used Fuzzy Uptext matching because OCR doesn't always get it right
if MatchTextFuzzyEx(GetTextAtExWrap(65, 344, 300, 358,
0, 5, 2, 23200, 15,
'SmallChars'), itemName, MATCH_GOOD) then
begin
MouseBox(65, 344, 300, 358, mouse_left);;
MarkTime(tt);
while (not result) or ( TimeFromMark(tt) < 800 ) do
begin
result := Pos(itemName, GetTextAtExWrap(214, 71, 450, 86,
0, 5, 2, 39372, 15,
'UpCharsEx')) > 0;
if (result) then break;
wait(100+random(100));
end;
end;
end;
end;

function enterPriceChat: boolean;
var
s, v: string;
begin
result := false;
s := GetTextAtExWrap(142, 395, 372, 409, 0, 5, 2, 0, 15, 'UpCharsEx');
v := 'Enter the price you wish to buy for';
result := MatchTextFuzzyEx(s, v, MATCH_GOOD);
end;

function makeOfferPrice(itemName:string; price:integer): boolean;
var
data: GEData;
b: TBox;
pre_s, s: string;
num: integer;
begin
result := false;
if not GE_Screen then
exit;
data := getGEData;
if makeOffer(itemName) then
begin
b := data.tradeInfo.exactAmountButton;
MouseTBox(b, mouse_left);
if WaitFunc(@enterPriceChat, 50+random(50), 800) then
Type_GE_SendEx(IntToStr(price), true);
//static wait
wait(800+random(200));
end;
s := GetTextAtExWrap(295, 185, 440, 200, 0, 5, 2, 6400255, 15, 'UpCharsEx');
s := GetNumbers(s);
num := StrToInt(s);
result := (num = price);
end;

function doCollectingFromOffer: boolean;
begin
// hard code for the actual slot, add a check for something there, click it
// may also help to make sure our inventory has room if it's not gold/gp collecting

end;

function viewOffer(): boolean;
begin
// views an open offer in the GE screen [applies to buying/selling]
end;

function collectOpenOffer(slot: integer): boolean;
begin
// just look for the black color in status, that way we don't need to
// worry about colors in the bar.
//
// collects anything in offer indicated via slot i.
end;

function collectAllSlotsPossible(): boolean;
begin
// same as 'function collectOpenOffer(slot: integer): boolean;', above,
// but attempts every slot
end;

function cancelOffer(color: integer): boolean;
begin
end;

function cancelAllOffers(): boolean;
begin
end;