quinta-feira, 31 de julho de 2008

Attack All

Const
List=['Dog','Sheep','Deer','Rabbit'];//enter the monsters(in your hunting area)that dont give exp
//so you avoid attacking them and wasting time.

var
x : integer;
Looting:boolean;
Checked: array of integer;
ScanX: integer;
ScanY: integer;
ScanItem: TItem;

function GetTileFromXYZ(X, Y, Z: integer): TTile;
begin
Result := nil;
if abs((Self.X - 7) - X) > 14 then Exit;
if abs((Self.Y - 5) - Y) > 11 then Exit;
if Self.Z <> Z then Exit;
Result := Screen.Tile[abs((Self.X - 7) - X), abs((Self.Y - 5) - Y)];
end;

function IsTileWalkable(Tile: TTile): boolean;
begin
Result := True;
for Z := 0 to Tile.Count - 1 do
begin
if Tile.Item[Z].Properties.Hole then
begin
Result := False;
end
else if Tile.Item[Z].Properties.Stairs then
begin
Result := False;
end
else if not Tile.Item[Z].Properties.Walkable then
begin
Result := False;
end;
end;
end;

function FindPath(FromX, FromY, FromZ, ToX, ToY, ToZ: integer; CheckLastTile: boolean): boolean;
var
Xloop, Yloop: integer;
begin
Result := False;
if FromZ <> ToZ then Exit;
Checked[abs(FromX - (Self.X - 7)), abs(FromY - (Self.Y - 5))] := True;
for Xloop := 0 to 2 do
begin
for Yloop := 0 to 2 do
begin
if (((FromX - 1) + Xloop) <> Self.X + 7) then Continue;
if (((FromY - 1) + Yloop) <> Self.Y + 5) then Continue;
if (((FromX - 1) + Xloop) = ToX) and (((FromY - 1) + Yloop) = ToY) then
begin
if IsTileWalkable(GetTileFromXYZ((FromX - 1) + Xloop, (FromY - 1) + Yloop, FromZ)) or (not CheckLastTile) then
begin
Result := True;
Exit;
end;
end;
if ((Xloop = 1) and (Yloop = 1)) then Continue;
if Checked[abs(((FromX - 1) + Xloop) - (Self.X - 7)), abs(((FromY - 1) + Yloop) - (Self.Y - 5))] then Continue;
if IsTileWalkable(GetTileFromXYZ((FromX - 1) + Xloop, (FromY - 1) + Yloop, FromZ)) then
begin
if FindPath((FromX - 1) + Xloop, (FromY - 1) + Yloop, FromZ, ToX, ToY, ToZ, CheckLastTile) then
begin
Result := True;
Exit;
end;
end;
end;
end;
end;

function IsReachable(X, Y, Z: integer);
begin
Checked := VarArrayCreate([0, 14, 0, 10], 11);
for InitX := 0 to 14 do
begin
for InitY := 0 to 1 do
begin
Checked[InitX, InitY] := False;
end;
end;
Result := FindPath(Self.X, Self.Y, Self.Z, X, Y, Z, False);
end;

function InList(Name:String):boolean;
begin
result:=false;
For x:=LOW(List) To High(List) do
if Name=List[x] then result:=true;
end;

function Attacking : boolean;
var x : integer;
begin
Result := False;
for x := 0 to Creatures.Count - 1 do
begin
if x >= Creatures.Count then Break;
if Creatures.Creature[x].Attacking then
begin
Result := True;
Exit;
end;
end;
end;

function PlayerOnScreen : Boolean;
var
x: integer;
begin
Result := False;
for x := 0 to Creatures.Count - 1 do
begin
if x >= Creatures.Count then Break;
if Creatures.Creature[x].z=self.z then
if Creatures.Creature[x].Name<>Self.Name then
if not Creatures.Creature[x].NPC then
begin
Result :=true;
Exit;
end;
end;
end;

function GetCreatureByID(ID: integer): TCreature;
var
x: integer;
begin
Result := nil;
for x := 0 to Creatures.Count - 1 do
begin
if x >= Creatures.Count then Break;
if Creatures.Creature[x].ID = ID then
begin
Result := Creatures.Creature[x];
Exit;
end;
end;
end;

function FindClosest : integer;
var i, l, closest_num, closest_range, last_closest_range : integer;
begin
if not attacking then
begin
closest_num := -1;
last_closest_range := 5000;
for i := 0 to Creatures.Count - 1 do
if (InList(Creatures.Creature[i].Name)=false) then
if (Creatures.Creature[i].Z = Self.Z) then
if (Creatures.Creature[i].NPC = true) then
if (IsReachable(Creatures.Creature[i].x,Creatures.Creature[i].y,Creatures.Creature[i].z)) then
begin
closest_range := Round(Sqrt(Sqr(Self.X - Creatures.Creature[i].X)) + Sqr(Self.Y - Creatures.Creature[i].Y));
if (closest_range < last_closest_range) then
begin
last_closest_range := closest_range;
closest_num := i;
end;
end;
Result := closest_num;
Exit;
end;
end;

var found, Bla,SleepTime, y : integer;
begin
updateworld;
Bla:=Self.Containers.Count;
Looting:=false;
while not terminated do
begin
UpdateWorld;
if Self.Containers.Count>Bla then Looting:=true;
if (Looting=true) then
begin
updateworld;
X:=self.x;
y:=self.y;
repeat
sleep(200);
updateworld;
until (X<>Self.x) or (y<>self.y);
Looting:=false;
Repeat sleep(100); updateworld; until not (Self.Containers.Count>Bla);
end;
updateworld;
found := FindClosest;
if (found <> -1) then
if (attacking=false) and (PlayerOnScreen=false) and (Looting=false) then
Creatures.Creature[found].Attacking := true;
Sleep(1000);
end;
end;

0 Comments: