procedure TAIGroup.AddUnit(Figure: TGameFigure);
var
Pos : TPoint;
Dummy : Integer;
procedure GotoNextSpawnRoom;
var
Dummy: Integer;
begin
for Dummy:=high(fSpawnPoints) downto 0 do
begin
if (fSpawnPoints[Dummy].RoomNr=fLastSpawnRoom) then
begin
SetLength(fNotUsedSpawns,length(fNotUsedSpawns)+1);
fNotUsedSpawns[high(fNotUsedSpawns)]:=fSpawnPoints[Dummy];
DeleteArray(Addr(fSpawnPoints),TypeInfo(TSpawnPoints),Dummy);
end;
end;
fLastSpawnRoom:=-1;
end;
function GetSpawnPointInRoom(Room: Integer; var Point: TPoint): Boolean;
var
Dummy: Integer;
Points : Array of Integer;
Index : Integer;
begin
SetLength(Points,length(fSpawnPoints));
Index:=0;
for Dummy:=0 to high(fSpawnPoints) do
begin
if fSpawnPoints[Dummy].RoomNr=Room then
begin
Points[Index]:=Dummy;
inc(Index);
end;
end;
if Index=0 then
result:=false
else
begin
result:=true;
Index:=random(Index);
Point:=fSpawnPoints[Points[Index]].Point;
DeleteArray(Addr(fSpawnPoints),TypeInfo(TSpawnPoints),Points[Index]);
end;
end;
function CheckPosition(Point:TPoint): Boolean;
var
Tile : TMapFieldData;
TileState: PTileState;
TmpPoint : TPoint;
ISOMap : TIsoMap;
begin
ISOMap:=TBodenEinsatz(gui_api_GetPage(PageBodenEinsatz)).Engine.ISOMap ;
TileState:=Addr(IsoMap.Figures[Point.X,Point.Y]);
Tile:=IsoMap.Tiles[Point.X,Point.Y];
if Figure<>nil then
begin
if TileState.Figure=Figure then
begin
result:=true;
exit;
end;
if (TileState.Figure<>Figure) and (TileState.Figure<>nil) then
begin
result:=false;
exit;
end;
if (not Tile.Begehbar) {or (Tile.Obj<>-1)} then
begin
result:=false;
exit;
end;
end;
result:=true;
end;
begin
fUnitsInGroupList.Add(Figure);
Figure.AIGroup:=Self;
if not GetSpawnPointInRoom(fLastSpawnRoom,Pos) then
begin
// Keine Spawnpunkte mehr vorhanden
if length(fSpawnPoints)=0 then
begin
game_api_MessageBox('Nicht genug Spawnpunkte vorhanden','Fehler');
if length(fNotUsedSpawns)=0 then
begin
Dummy:=5000;
repeat
Pos:=Point(random(fMapSize.cx),random(fMapSize.cy));
dec(Dummy);
if Dummy=0 then
begin
game_api_MessageBox('kein zufälliger Spawnpunkt gefunden','Fehler');
break;
end;
until CheckPosition(Pos);
end
else
begin
Dummy:=random(high(fNotUsedSpawns));
Assert((Dummy>=0) and (Dummy<=high(fNotUsedSpawns)));
Pos:=fNotUsedSpawns[Dummy].Point;
DeleteArray(Addr(fNotUsedSpawns),TypeInfo(TSpawnPoints),Dummy);
end;
end
else
begin
Dummy:=random(high(fSpawnPoints));
Assert((Dummy>=0) and (Dummy<=high(fSpawnPoints)));
fLastSpawnRoom:=fSpawnPoints[Dummy].RoomNr;
Assert(GetSpawnPointInRoom(fLastSpawnRoom,Pos));
end;
end;
repeat
if not PtInRect(Rect(0,0,fMapSize.cx,fMapSize.cy),Pos) then
begin
Pos.X:=random(fMapSize.cx);
Pos.Y:=random(fMapSize.cy);
end;
Figure.XPos:=Pos.X;
Figure.YPos:=Pos.Y;
// Beim nächsten durchlauf auf jeden Fall eine neue Position ermitteln
Pos.X:=-1;
until Figure.SetToGameField;
end;
Beim ersten überfliegen bin ich mir aber nicht sicher, ob das noch so arbeitet wie es mal sollte. GoToNextSpawnRoom wird jedenfalls nirgends aufgerufen.