Coh3n
2010-03-14 23:40:49
After basically memorising the format of marpis's reflection walking functions, I think that Walking_Color should be organised in a very similar way. This would be the new Walking_Reflection:
I just renamed some of the functions, and added a few at the end.
What do you guys think?
EDIT: I've tested an committed this. :) There's another thing I thought of, however. From looking through marpis's walking, I don't really see the need to have a TWalk type or a CurrentLocation variable. For the path, we can just load the path, and the player locations are set using Players[CurrentPlayer].Integers[P_LOC] anyway. Thoughts?
{==============================================================================\
| MSI Group Scripting Include |
| Walking_Color.scar |
|==============================================================================|
| For all the walking functions that are color based. |
| |
\_____________________________________________________________________________}
{******************************************************************************}
{ function MSI_IsMoving: boolean; }
{ By: marpis }
{ Description: Returns True if moving }
{******************************************************************************}
function MSI_IsMoving: boolean;
begin
{$IFDEF REFLECTION}
Result := CharacterMoving;
{$ELSE}
Result := IsMoving;
{$ENDIF}
end;
{******************************************************************************}
{ procedure MSI_Flag(Distance: Integer); }
{ By: marpis }
{ Description: Flag wait }
{******************************************************************************}
procedure MSI_Flag(FlagDist: integer);
begin
if WaitFunc(@MSI_IsMoving, 1, 2500 + random(200)) then
while (MSI_IsMoving or FlagPresent) do
begin
FFlag(FlagDist);
if (FlagDistance <= FlagDist) then
Break;
end;
end;
procedure MSI_SortColors(var Colors: tIntegerArray; col: Integer);
var
tempB: tIntegerArray;
Tol: ShortInt;
I: Integer;
timer, L: LongInt;
begin
timer := getsystemtime;
tempB := Colors;
SetArrayLength(Colors, 0);
Tol := 0;
while Length(Colors) < (Length(tempB)/2 or 5) do
begin
if Getsystemtime-timer > 100 then Break;
for i := 0 to High(tempB) do
begin
if SimilarColors(tempB[i], col, Tol) then
begin
L := GetArrayLength(Colors);
SetArrayLength(Colors, L+1);
Colors[L] := tempB[i];
end;
if Tol > 10 then Break;
Inc(Tol);
end;
end;
end;
{******************************************************************************}
{ Function FindMMLocation(Var X, Y: Integer; Location: TMMLocation): Boolean; }
{ By: marpis }
{ Description: Finds a location in minimap. First tries to find DTM, then }
{ colors, and if still not found it will find it by symbol }
{******************************************************************************}
Function MSI_FindMMLocation(Var X, Y: Integer; Location: TMMLocation): Boolean;
Var
SA, EA, tmp_Ang: Extended;
N, I, II, III, H, HH, Score, Step: Integer;
ATPA: T2DPointArray;
tmp_TPA: TPointArray;
X1, Y1, X2, Y2: Integer;
Begin
If (Not LoggedIn) Then Exit;
X := -1;
Y := -1;
Step := (Location.W * Location.H) / 10;
{ DTM Search }
If (Location.DTM > -1) Then
While (N < 10) Do
Begin
If FindDTMRotated(Location.DTM, X, Y, MMX1, MMY1, MMX2, MMY2, SA, EA, 0.02, tmp_Ang) Then
Begin
Result := True;
Exit;
End;
Inc(N);
SA := SA - (N * 0.02);
EA := EA + (N * 0.02);
End;
{ Color search }
If (Length(Location.Colors) = 0) Then
Exit;
H := High(Location.Colors);
SetLength(ATPA, H + 1);
For I := 0 To H Do
Begin
FindColorsSpiralTolerance(MMCX, MMCY, ATPA[I], Location.Colors[I], MMX1, MMY1, MMX2, MMY2, Location.Tolerances[I]);
FilterPointsPie(ATPA[I], 0.0, 360.0, 0.0, 76.0, MMCX, MMCY);
End;
{ Go through every TPA that contains one color. }
For I := 0 To H Do
Begin
HH := High(ATPA[I]);
{ Go through every 10th point in ATPA[I] to avoid lag. }
For II := 0 To HH Do
Begin
If ((II Mod Step) <> 0) Then
Continue;
Score := 0;
X1 := ATPA[I][II].X - (Location.W / 2);
Y1 := ATPA[I][II].Y - (Location.H / 2);
X2 := ATPA[I][II].X + (Location.W / 2);
Y2 := ATPA[I][II].Y + (Location.H / 2);
{ Go through every TPA again, calculating surrounding color counts. }
For III := 0 To H Do
Begin
tmp_TPA := MSI_ReturnPointsInBox(ATPA[III], IntToBox(X1, Y1, X2, Y2));
If (Length(tmp_TPA) >= Location.Counts[III]) Then
Inc(Score);
End;
{ If there are enough points of all colors, the location has been found. }
If (Score = H) Then
Begin
Result := True;
X := (X1 + X2) shr 1;
Y := (Y1 + Y2) shr 1;
Exit;
End;
End;
End;
{ Symbol Search }
If (Location.Symbol <> '') Then
If FindSymbol(X, Y, Location.Symbol) Then
Begin
Result := True;
Exit;
End;
End;
function MSI_AutoColorMM(const color: Integer): Integer;
var
TPA: tPointArray;
List: tIntegerArray;
I: Integer;
begin
for i := 0 to 20 do
begin
FindColorsTolerance(TPA, color, MMx1, MMy1, MMx2, MMy2, 5+i);
if Length(TPA) > 0 then Break;
end;
if Length(TPA) <= 0 then Exit;
List := getColors(TPA);
MSI_SortColors(List, color);
if Length(List) <= 0 then Exit;
result := List[0];
end;
function MSI_FindAutoColor(Which: Integer): Integer;
begin
case Which of
AC_ROAD_VARROCK:
Result := MSI_AutoColorMM(VARROCK_ROAD_COLOR);
end;
end;
function MSI_CreateWalkTPA(x, y, color, width, height, tolerance: Integer): TpointArray;
var
TPA: tPointArray;
aTPA: t2DPointArray;
i: Integer;
L, P: Integer;
begin
FindColorsSpiralTolerance(x, y, TPA, color, MMx1, MMy1, MMx2, MMy2, tolerance);
aTPA := TPAtoATPAEx(TPA, width, height);
for i := 0 to High(aTPA) do
begin
if Length(aTPA) > 5 then
begin
l := getarraylength(Result);
SetArrayLength(Result, l+1);
Result[l] := MiddleTpa(aTPA[i]);
end;
end;
if Length(result) = 0 then Exit;
for i := 0 to High(Result) do
if rs_OnMiniMap(result[i].x, result[i].y) then Inc(p);
if p = 0 then SetArrayLength(Result, 0);
end;
function MSI_WalkTPAPoint(TPA: tPointArray; sortP: tPoint; flagDist: shortInt): Boolean;
var
i: Integer;
t: LongInt;
begin
t := getSystemTime;
if Length(TPA) <= 0 then Exit;
SortTPAFrom(TPA, sortP);
for i := 0 to High(TPA) do
begin
if rs_OnMiniMap(TPA[i].x, TPA[i].y) then
begin
Mouse(TPA[i].x, TPA[i].y, random(8), random(8), true);
wait(randomRange(1000, 2000));
while not FlagPresent do
begin
wait(250+randomRange(150, 350));
if getSystemTime-t > randomRange(2500, 3750) then
continue;
end;
FFlag(flagDist);
Result := True;
Exit;
end;
end;
end;
function MSI_CreateColorBox(Color: Integer): TBox;
var
TPA: tPointArray;
begin
FindColors(TPA, Color, MMx1, MMy1, MMx2, MMy2);
if Length(TPA) <= 0 then Exit;
Result := GetTPABounds(TPA);
end;
function MSI_FindColorInBox(Color: Integer; Box: TBox): Boolean;
var
TPA: tPointArray;
i: Integer;
begin
FindColorsTolerance(TPA, Color, Box.x1-5, Box.y1-5, Box.x2+5, Box.y2+5, 15);
if Length(TPA) <= 0 then Exit;
SortTPAFrom(TPA, Point(687, 50));
for i := 0 to High(TPA) do
begin
if not rs_OnMiniMap(TPA[i].x, TPA[i].y) then continue;
Mouse(TPA[i].x, TPA[i].y, 5, 5, True);
Wait(750+random(450));
if FlagPresent then
begin
FFlag(10);
Result := True;
Exit;
end;
end;
end;
function MSI_CreateWalkIndex(index: Integer): Boolean;
var
i: Integer;
aTPA: t2DPointArray;
TPA: tPointArray;
p: tPoint;
RockColor, TreeColor, BC: Integer;
t: LongInt;
begin
case index of
WALK_WC_VE_TREE_1:
begin
if countColor(RoadColor, MMx1, MMy1, MMx2, MMy2) <= 5 then RoadColor := MSI_FindAutoColor(AC_ROAD_VARROCK);
if MSI_FindColorInBox(RoadColor, MSI_CreateColorBox(FindRockColor)) then
begin
Result := True;
Exit;
end;
TPA := MSI_CreateWalkTPA(MMx2, MMy1, RoadColor, 2, 2, 8);
if Length(TPA) <= 0 then Exit;
Result := MSI_WalkTPAPoint(TPA, Point(MMx2-randomRange(5, 15), MMy1), random(10));
Exit;
end;
WALK_WC_VE_TREE_2:
begin
for i := 0 to 2 do
begin
RoadColor := MSI_FindAutoColor(AC_ROAD_VARROCK);
FindColorsTolerance(TPA, RoadColor, MMCx, MMy1, MMx2, MMCy, 8);
if Length(TPA) > 0 then
Break;
if i = 2 then
begin
MSI_Debug('WALK_WC_VE_TREE_2', 'Failed to find the road.', DEBUG_SMALL_DETAILS);
Exit;
end;
end;
SortTPAFrom(TPA, Point(MMCx, MMCy));
i := (High(TPA) - 1);
Mouse(TPA[i].x, TPA[i].y, 1, 1, True);
FFlag(4);
Result := True;
Exit;
end;
WALK_WC_VE_TREE_3:
begin
RockColor := MSI_AutoColorMM(ROCK_COLOR);
TPA := MSI_CreateWalkTPA(MMcx, MMy1, RockColor, 2, 2, 8);
if length(TPA) <= 0 then Exit;
for i := 0 to High(TPA) do
begin
if TPA[i].y > MMcy then continue;
Mouse(TPA[i].x, TPA[i].y, 5, 5, true);
t := getSystemTime;
while not flagPresent() do
begin
if getSystemTime-t > randomRange(2000, 2500) then
Exit;
wait(250+random(500));
end;
FFlag(random(8));
Result := True;
Exit;
end;
end;
WALK_WC_VE_TREE_4:
begin
TreeColor := MSI_AutoColorMM(TREE_COLOR);
writeLn('Tree AutoColor = '+IntToStr(TreeColor));
FindColorsTolerance(TPA, TreeColor, MMx1, MMy1, MMx2, MMy2, 8);
if Length(TPA) <= 0 then Exit;
aTPA := SplitTPAEx(TPA, 10, 10);
if Length(aTPA) <= 0 then Exit;
SortATPASize(aTPA, true);
p := MiddleTPA(aTPA[0]);
if (p.x < MMcx+20) and (p.y < MMcy) then
begin
Mouse(p.x, p.y, 5, 5, true);
Flag();
Result := True;
Exit;
end;
end;
WALK_WC_VE_BANK_1:
begin
RockColor := MSI_AutoColorMM(ROCK_COLOR);
TPA := MSI_CreateWalkTPA(MMcx, MMy2, RockColor, 2, 2, randomRange(5, 10));
if Length(TPA) <= 0 then Exit;
SortTPAFrom(TPA, point(700, 150));
for i := 0 to High(TPA) do
begin
if (TPA[i].y < MMcy-10) then continue;
Mouse(TPA[i].x-5, TPA[i].y, Random(8), Random(8), true);
t := getSystemTime;
while not flagPresent() do
begin
if getSystemTime-t > randomRange(2000, 2500) then
Exit;
wait(250+random(500));
end;
FFlag(RandomRange(5, 10));
Result := True;
Exit;
end;
end;
WALK_WC_VE_BANK_2:
begin
RoadColor := MSI_FindAutoColor(AC_ROAD_VARROCK);
FindColorsTolerance(TPA, RoadColor, MMx1, MMCy + 15, MMx2 - 20, MMy2, 4);
if Length(TPA) <= 0 then
Exit;
SortTPAFrom(TPA, Point(MMCx, MMCy));
for t := 0 to 4 do
begin
i := Length(TPA) - Random(2);
try
if rs_OnMiniMap(TPA[i].x, TPA[i].y) then
BC := 1;
except
Continue;
end;
if BC = 1 then
Break;
end;
if (t = 4) or (BC < 1) then
begin
MSI_Debug('CreatWalkIndex(WALK_WC_VE_BANK_2)', 'Failed to find road on minimap', Debug_Functions);
Exit;
end;
Mouse(TPA[i].x, TPA[i].y, 0, 0, True);
FFlag(5);
Result := True;
Exit;
end;
WALK_WC_VE_BANK_3:
begin
TPA := MSI_CreateWalkTPA(MMx1, MMcy, RoadColor, 3, 3, 8);
if Length(TPA) <= 0 then
Exit;
SortTPAFrom(TPA, point(MMx1-10, MMcy));
for i := 0 to High(TPA) do
begin
if not RS_onMinimap(TPA[i].x, TPA[i].y) then Continue;
Mouse(TPA[i].x, TPA[i].y, 5, 5, true);
t := getSystemTime;
while not flagPresent() do
begin
if getSystemTime-t > randomRange(2000, 2500) then
Exit;
wait(250+random(500));
end;
FFlag(RandomRange(3, 5));
Result := True;
Exit;
end;
end;
WALK_WC_VE_BANK_4:
begin
for i := 0 to 2 do
begin
BC := MSI_AutoColorMM(BANKER_COLOR);
FindColorsTolerance(TPA, BC, MMx1, MMCy, MMx2, MMy2, 5);
if Length(TPA) > 5 then
Break;
if i = 2 then
begin
MSI_Debug('MSI_CreateWalkTPA(WALK_BANK_Final);', 'Failed to find bankers on MiniMap.', Debug_Functions);
Exit;
end;
end;
aTPA := TPAToATPAEx(TPA, 25, 5);
SortATPASize(aTPA, True);
i := Length(aTPA[0]);
t := RandomRange(0, i);
Mouse(aTPA[0][t].x, aTPA[0][t].y - RandomRange(3, 7), 3, 3, True);
t := getSystemTime;
while not flagPresent() do
begin
if getSystemTime-t > randomRange(2000, 2500) then
Exit;
wait(250+random(500));
end;
FFlag(3);
Result := True;
end;
end;
end;
function MSI_CreateWalkFailsafe(index: byte): Boolean;
begin
if not MSI_CreateWalkIndex(index) then
begin
case index of
WALK_WC_VE_TREE_1:
begin
if not RadialWalkTolerance(RoadColor, 45, 90, randomRange(60, 70), random(8), random(8), 10) then
if not RadialWalkTolerance(findRockColor, 45, 90, randomRange(60, 70), 3, 3, 10) then
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_TREE_1', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_TREE_2:
begin
if not RadialWalkTolerance(RoadColor, 75, 105, randomRange(60, 70), random(5), random(5), 10) then
if not RadialWalkTolerance(RoadColor, 65, 115, randomRange(60, 70), random(5), random(5), 10) then
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_TREE_2', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_TREE_3:
begin
if not RadialWalkTolerance(RoadColor, 0, 35, randomRange(60, 70), 10, 10, 10) then
if not RadialWalkTolerance(findRockColor, 345, 35, 60, 2, 2, 10) then
if not RadialWalkTolerance(RoadColor, 0, 45, randomRange(60, 70), 5, 5, 10) then
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_TREE_3', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_TREE_4:
begin
Exit; // Not sure of any other failsafe for this. A (D)DTM perhaps?
end;
WALK_WC_VE_BANK_1:
begin
if not RadialWalkTolerance(findRockColor, 350, 50, 65, 5, 5, 10) then
begin
mouseBox(631, 114, 667, 130, 1); // random co-ord clicking
wait(randomRange(1000, 2000));
if FlagPresent then
begin
result := true;
FFlag(randomRange(8, 14));
end;
end else
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_BANK_1', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_BANK_2:
begin
if not RadialWalkTolerance(RoadColor, 180, 200, randomRange(60, 70), random(5), random(5), 10) then
if not RadialWalkTolerance(RoadColor, 170, 220, randomRange(60, 70), 3, 3, 10) then
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_BANK_2', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_BANK_3:
begin
if not RadialWalkTolerance(RoadColor, 255, 280, randomRange(60, 70), random(8), random(8), 10) then
if not RadialWalkTolerance(RoadColor, 245, 290, randomRange(60, 70), 5, 5, 10) then
begin
MSI_Debug('MSI_CreateWalkFailsafe('+inttoStr(index)+');', 'Failed to WALK_WC_VE_BANK_3', Debug_Functions);
Exit;
end;
end;
WALK_WC_VE_BANK_4:
begin
Exit; // add a DTM of Bankers here.
end;
end;
end;
result := true;
end;
{******************************************************************************}
{ function MSI_CreateColorWalk(City, Loc: Integer; Path: TIntegerArray): TWalk;}
{ By: Coh3n }
{ Description: Forms TWalk from parameters }
{******************************************************************************}
function MSI_CreateColorWalk(City, Loc: Integer; Path: TIntegerArray): TWalk;
begin
Result.City := City;
Result.Loc := Loc;
Result.ColPath := Path;
end;
{******************************************************************************}
{ function MSI_LoadColorPath(StartLoc, EndLoc: Integer): TIntegerArray; }
{ By: The MSI Team }
{ Description: Loads the specified color walking path }
{******************************************************************************}
function MSI_LoadColorPath(StartLoc, EndLoc: Integer): TIntegerArray;
begin
case StartLoc of
LOC_VE_BANK:
case EndLoc of
LOC_VE_TREES:
Result := [ WALK_WC_VE_TREE_1, WALK_WC_VE_TREE_2, WALK_WC_VE_TREE_3,
WALK_WC_VE_TREE_4 ];
end;
LOC_VE_TREES:
case EndLoc of
LOC_VE_BANK:
Result := [ WALK_WC_VE_BANK_1, WALK_WC_VE_BANK_2, WALK_WC_VE_BANK_3,
WALK_WC_VE_BANK_4 ];
end;
end;
end;
{******************************************************************************}
{ function MSI_LoadColorWalk(StartLoc, EndLoc: Integer): TWalk; }
{ By: The MSI Team }
{ Description: Loads TWalk from A to B }
{******************************************************************************}
function MSI_LoadColorWalk(StartLoc, EndLoc: Integer): TWalk;
begin
case StartLoc of
LOC_VE_BANK:
case EndLoc of
LOC_VE_TREES:
Result := MSI_CreateColorWalk(CITY_VARROCK, EndLoc, MSI_LoadColorPath(StartLoc, EndLoc));
end;
LOC_VE_TREES:
case EndLoc of
LOC_VE_BANK:
Result := MSI_CreateColorWalk(CITY_VARROCK, EndLoc, MSI_LoadColorPath(StartLoc, EndLoc));
end;
end;
end;
{******************************************************************************}
{ function MSI_ColorWalk(startLoc, endLoc, colorIndex: Integer): Boolean; }
{ By: Coh3n }
{ Description: Walks from startLoc to endLoc }
{******************************************************************************}
function MSI_ColorWalk(startLoc, endLoc: Integer): Boolean;
var
i: Integer;
theWalk: TWalk;
begin
if (not LoggedIn) then
Exit;
MakeCompass('n');
RunEnergy(20);
theWalk := MSI_LoadColorWalk(startLoc, endLoc);
for i := 0 to High(theWalk.ColPath) do
begin
if (not MSI_CreateWalkFailsafe(theWalk.ColPath[i])) then
begin
MSI_Debug('MSI_ColorWalk', 'Failed to walk to point: ' + IntToStr(i), DEBUG_SMALL_DETAILS);
Exit;
end;
Result := (i = High(theWalk.ColPath));
end;
if (Result) then
begin
MSI_Debug('MSI_ColorWalk', 'Successfully walked to location', DEBUG_SMALL_DETAILS);
Players[CurrentPlayer].Integers[P_LOC] := theWalk.Loc;
Wait(RandomRange(750, 1500));
end;
end;
I think this makes it much better for organizational purposes, and also easier to follow if we have both the walking systems formatted the same. In addition to this, it will shorten the mainloop and make it a little more understandable.I just renamed some of the functions, and added a few at the end.
What do you guys think?
EDIT: I've tested an committed this. :) There's another thing I thought of, however. From looking through marpis's walking, I don't really see the need to have a TWalk type or a CurrentLocation variable. For the path, we can just load the path, and the player locations are set using Players[CurrentPlayer].Integers[P_LOC] anyway. Thoughts?