Changeset 193 for trunk/UGame.pas
- Timestamp:
- May 14, 2018, 5:02:00 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r191 r193 12 12 const 13 13 DefaultPlayerStartUnits = 5; 14 HexCellMulX = 1.12;15 HexCellMulY = 1.292;16 14 SquareCellMulX = 1.05; 17 15 SquareCellMulY = 1.05; … … 414 412 TGrowAmount = (gaByOne, gaBySquareRoot); 415 413 TGrowCells = (gcNone, gcPlayerCities, gcPlayerAll); 416 TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle, mtVoronoi );414 TMapType = (mtNone, mtHexagon, mtSquare, mtTriangle, mtVoronoi, mtIsometric); 417 415 TWinObjective = (woDefeatAllOponents, woDefeatAllOponentsCities, 418 416 woSpecialCaptureCell, woStayAliveForDefinedTurns); … … 636 634 begin 637 635 for I := 0 to Cells.Count - 1 do begin 638 if TCell(Cells[I]).Neighbors.Remove(TCell(Cells[1 - I])) = -1 then636 if Cells[I].Neighbors.Remove(Cells[1 - I]) = -1 then 639 637 raise Exception.Create(SCellRemoveNeighborError); 640 if TCell(Cells[I]).Links.Remove(Self) = -1 then638 if Cells[I].Links.Remove(Self) = -1 then 641 639 raise Exception.Create(SCellRemoveNeighborError); 642 640 end; … … 756 754 begin 757 755 I := 0; 758 while (I < Count) and ( TPlayerCell(Items[I]).MapCell <> Cell) do Inc(I);759 if I < Count then Result := TPlayerCell(Items[I])756 while (I < Count) and (Items[I].MapCell <> Cell) do Inc(I); 757 if I < Count then Result := Items[I] 760 758 else Result := nil; 761 759 end; … … 785 783 NewNode := Node.OwnerDocument.CreateElement('Cell'); 786 784 Node.AppendChild(NewNode); 787 TPlayerCell(Items[I]).SaveToNode(NewNode);785 Items[I].SaveToNode(NewNode); 788 786 end; 789 787 end; … … 825 823 826 824 for I := 0 to Cells.Count - 1 do 827 with TPlayerCell(Cells[I])do begin825 with Cells[I] do begin 828 826 List := Cells; 829 827 Explored := False; 830 828 InVisibleRange := False; 831 MapCell := TCell(Player.Game.Map.Cells[I]);829 MapCell := Player.Game.Map.Cells[I]; 832 830 end; 833 831 end; … … 852 850 begin 853 851 for I := 0 to Cells.Count - 1 do 854 with TPlayerCell(Cells[I])do begin852 with Cells[I] do begin 855 853 NeighCount := 0; 856 854 for C := 0 to MapCell.Neighbors.Count - 1 do 857 if TCell(MapCell.Neighbors[C]).Player = Player then855 if MapCell.Neighbors[C].Player = Player then 858 856 Inc(NeighCount); 859 857 860 InVisibleRange := (NeighCount > 0) or ( TCell(MapCell).Player = Player);858 InVisibleRange := (NeighCount > 0) or (MapCell.Player = Player); 861 859 if InVisibleRange and not Explored then Explored := True; 862 860 end; … … 999 997 begin 1000 998 for I := 0 to Count - 1 do 1001 TCell(Items[I]).FixRefId;999 Items[I].FixRefId; 1002 1000 end; 1003 1001 … … 1007 1005 begin 1008 1006 I := 0; 1009 while (I < Count) and ( TCell(Items[I]).Id <> Id) do Inc(I);1010 if I < Count then Result := TCell(Items[I])1007 while (I < Count) and (Items[I].Id <> Id) do Inc(I); 1008 if I < Count then Result := Items[I] 1011 1009 else Result := nil; 1012 1010 end; … … 1044 1042 begin 1045 1043 for I := 0 to Count - 1 do 1046 with TCell(Items[I])do begin1044 with Items[I] do begin 1047 1045 NewNode2 := Node.OwnerDocument.CreateElement('Cell'); 1048 1046 Node.AppendChild(NewNode2); … … 1090 1088 begin 1091 1089 I := 0; 1092 while (I < Count) and ( TPlayer(Items[I]).Id <> Id) do Inc(I);1093 if I < Count then Result := TPlayer(Items[I])1090 while (I < Count) and (Items[I].Id <> Id) do Inc(I); 1091 if I < Count then Result := Items[I] 1094 1092 else Result := nil; 1095 1093 end; … … 1140 1138 NewNode := Node.OwnerDocument.CreateElement('Player'); 1141 1139 Node.AppendChild(NewNode); 1142 TPlayer(Items[I]).SaveToNode(NewNode);1140 Items[I].SaveToNode(NewNode); 1143 1141 end; 1144 1142 end; … … 1155 1153 begin 1156 1154 I := 0; 1157 while (I < Count) and ( TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);1158 if I < Count then Result := TPlayer(Items[I])1155 while (I < Count) and (Items[I].Mode <> pmHuman) do Inc(I); 1156 if I < Count then Result := Items[I] 1159 1157 else Result := nil; 1160 1158 end; … … 1169 1167 Add(TPlayer.Create); 1170 1168 for I := 0 to Count - 1 do begin 1171 TPlayer(Items[I]).Assign(TPlayer(Source[I]));1172 TPlayer(Items[I]).Game := Game;1169 Items[I].Assign(Source[I]); 1170 Items[I].Game := Game; 1173 1171 end; 1174 1172 NewPlayerId := Source.NewPlayerId; … … 1188 1186 for I := 0 to Count - 1 do begin 1189 1187 Items[I] := TPlayer.Create; 1190 TPlayer(Items[I]).Id := GetNewPlayerId;1191 TPlayer(Items[I]).Game := Game;1192 TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I));1188 Items[I].Id := GetNewPlayerId; 1189 Items[I].Game := Game; 1190 Items[I].LoadConfig(Config, Path + '/Player' + IntToStr(I)); 1193 1191 end; 1194 1192 end; … … 1201 1199 begin 1202 1200 for I := 0 to Count - 1 do 1203 TPlayer(Items[I]).SaveConfig(Config, Path + '/Player' + IntToStr(I));1201 Items[I].SaveConfig(Config, Path + '/Player' + IntToStr(I)); 1204 1202 with Config do begin 1205 1203 SetValue(DOMString(Path + '/Count'), Count); … … 1385 1383 for I := 0 to Cells.Count - 1 do begin 1386 1384 Cells[I] := TCell.Create; 1387 TCell(Cells[I]).Map := Self;1388 TCell(Cells[I]).Assign(TCell(Source.Cells[I]));1385 Cells[I].Map := Self; 1386 Cells[I].Assign(Source.Cells[I]); 1389 1387 end; 1390 1388 } … … 1442 1440 Result := nil; 1443 1441 for I := 0 to Cells.Count - 1 do 1444 if TCell(Cells[I]).Terrain <> ttVoid then begin1445 if TCell(Cells[I]).Polygon.IsPointInside(Pos) then begin1446 Result := TCell(Cells[I]);1442 if Cells[I].Terrain <> ttVoid then begin 1443 if Cells[I].Polygon.IsPointInside(Pos) then begin 1444 Result := Cells[I]; 1447 1445 Exit; 1448 1446 end; … … 1462 1460 begin 1463 1461 for I := 0 to Cells.Count - 1 do 1464 with TCell(Cells[I])do begin1462 with Cells[I] do begin 1465 1463 if (Player = APlayer) and ((Game.GrowCells = gcPlayerAll) or 1466 1464 ((Game.GrowCells = gcPlayerCities) and (Terrain = ttCity))) then begin … … 1588 1586 // Generic way to create two sides symetric map independent to shape 1589 1587 for C := 0 to (Cells.Count div 2) - 1 do begin 1590 TCell(Cells[C]).Terrain := TCell(Cells[Cells.Count - 1 - C]).Terrain;1591 TCell(Cells[C]).Power := TCell(Cells[Cells.Count - 1 - C]).Power;1592 1593 for I := TCell(Cells[C]).Links.Count - 1 downto 0 do begin1594 CellLink := TCell(Cells[C]).Links[I];1588 Cells[C].Terrain := Cells[Cells.Count - 1 - C].Terrain; 1589 Cells[C].Power := Cells[Cells.Count - 1 - C].Power; 1590 1591 for I := Cells[C].Links.Count - 1 downto 0 do begin 1592 CellLink := Cells[C].Links[I]; 1595 1593 1596 1594 // Remove cells on first half of the map … … 1624 1622 for C := 0 to (Cells.Count div 2) - 1 do begin 1625 1623 // Make copy of links from second half 1626 OppositeCell := TCell(Cells[Cells.Count - 1 - C]);1624 OppositeCell := Cells[Cells.Count - 1 - C]; 1627 1625 for CellLink in OppositeCell.Links do 1628 1626 if (Cells.IndexOf(CellLink.Cells[0]) > (Cells.Count div 2)) and … … 1687 1685 // This is generic algorithm to determine pixel size of entire map 1688 1686 for I := 0 to Cells.Count - 1 do begin 1689 CellRect := TCell(Cells[I]).Polygon.GetRect;1687 CellRect := Cells[I].Polygon.GetRect; 1690 1688 if I = 0 then Result := CellRect 1691 1689 else begin … … 1935 1933 NewNode2 := NewNode.OwnerDocument.CreateElement('Neighbour'); 1936 1934 NewNode.AppendChild(NewNode2); 1937 WriteInteger(NewNode2, 'Id', TCell(Neighbors[I]).Id);1935 WriteInteger(NewNode2, 'Id', Neighbors[I].Id); 1938 1936 end; 1939 1937 NewNode := Node.OwnerDocument.CreateElement('Polygon'); … … 2028 2026 FreeAndNil(Links); 2029 2027 for I := Neighbors.Count - 1 downto 0 do 2030 if TCell(Neighbors[I]).Neighbors.Remove(Self) = -1 then2028 if Neighbors[I].Neighbors.Remove(Self) = -1 then 2031 2029 raise Exception.Create(SCellRemoveNeighborError); 2032 2030 FreeAndNil(Neighbors); … … 2435 2433 // Get list of all enemy border cells 2436 2434 for C := 0 to AllCells.Count - 1 do 2437 with TCell(AllCells[C])do begin2435 with AllCells[C] do begin 2438 2436 if (Player <> Game.CurrentPlayer) and (Player <> nil) and (Terrain <> ttVoid) then begin 2439 2437 CanAttack := 0; 2440 2438 for I := 0 to Neighbors.Count - 1 do 2441 if (( TCell(Neighbors[I]).Player = Game.CurrentPlayer) or2442 ( TCell(Neighbors[I]).Player = nil)) and (TCell(Neighbors[I]).Terrain <> ttVoid) then begin2439 if ((Neighbors[I].Player = Game.CurrentPlayer) or 2440 (Neighbors[I].Player = nil)) and (Neighbors[I].Terrain <> ttVoid) then begin 2443 2441 Inc(CanAttack); 2444 2442 end; … … 2458 2456 // Set mark for selected border cells 2459 2457 for C := 0 to TargetCells.Count - 1 do 2460 T Cell(TargetCells[C]).Mark := True;2458 TargetCells[C].Mark := True; 2461 2459 2462 2460 // Move all power from unmarked cells and mark them 2463 2461 NewTargetCells.Count := 0; 2464 2462 for C := 0 to TargetCells.Count - 1 do 2465 with T Cell(TargetCells[C])do begin2463 with TargetCells[C] do begin 2466 2464 for I := 0 to Neighbors.Count - 1 do begin 2467 if ( TCell(Neighbors[I]).Terrain <> ttVoid) and (not TCell(Neighbors[I]).Mark) then begin2468 if (T Cell(TargetCells[C]).Player = Game.CurrentPlayer) and2469 ( TCell(Neighbors[I]).Player = Game.CurrentPlayer) then begin2465 if (Neighbors[I].Terrain <> ttVoid) and (not Neighbors[I].Mark) then begin 2466 if (TargetCells[C].Player = Game.CurrentPlayer) and 2467 (Neighbors[I].Player = Game.CurrentPlayer) then begin 2470 2468 // Do not take units from front line 2471 Cells2 := TCell(Neighbors[I]).Neighbors;2469 Cells2 := Neighbors[I].Neighbors; 2472 2470 CanAttack := 0; 2473 2471 for J := 0 to Cells2.Count - 1 do 2474 if (( TCell(Cells2[J]).Player <> Game.CurrentPlayer) or (TCell(Cells2[J]).Player = nil))2475 and ( TCell(Cells2[J]).Terrain <> ttVoid) then begin2472 if ((Cells2[J].Player <> Game.CurrentPlayer) or (Cells2[J].Player = nil)) 2473 and (Cells2[J].Terrain <> ttVoid) then begin 2476 2474 Inc(CanAttack); 2477 2475 end; 2478 2476 if CanAttack = 0 then begin 2479 MovedPower := TCell(Neighbors[I]).GetAvialPower;2480 if (T Cell(TargetCells[C]).GetAvialPower + TCell(TargetCells[C]).GetAttackPower + MovedPower) > Game.Map.MaxPower then2481 MovedPower := Game.Map.MaxPower - T Cell(TargetCells[C]).GetAvialPower - TCell(TargetCells[C]).GetAttackPower;2482 Player.SetMove( TCell(Neighbors[I]), TCell(TargetCells[C]), MovedPower, False);2477 MovedPower := Neighbors[I].GetAvialPower; 2478 if (TargetCells[C].GetAvialPower + TargetCells[C].GetAttackPower + MovedPower) > Game.Map.MaxPower then 2479 MovedPower := Game.Map.MaxPower - TargetCells[C].GetAvialPower - TargetCells[C].GetAttackPower; 2480 Player.SetMove(Neighbors[I], TargetCells[C], MovedPower, False); 2483 2481 end; 2484 2482 end; 2485 TCell(Neighbors[I]).Mark := True;2486 NewTargetCells.Add( TCell(Neighbors[I]));2483 Neighbors[I].Mark := True; 2484 NewTargetCells.Add(Neighbors[I]); 2487 2485 end; 2488 2486 end; … … 2539 2537 // Get list of border cells 2540 2538 for C := 0 to AllCells.Count - 1 do 2541 with TCell(AllCells[C])do begin2539 with AllCells[C] do begin 2542 2540 if (Terrain <> ttVoid) and (Player = Game.CurrentPlayer) then begin 2543 if AttackersCount( TCell(AllCells[C])) > 0 then2541 if AttackersCount(AllCells[C]) > 0 then 2544 2542 BorderCells.Add(AllCells[C]); 2545 2543 end; … … 2548 2546 // Move all units back to inner area from weak border cells 2549 2547 for C := 0 to BorderCells.Count - 1 do 2550 with TCell(BorderCells[C])do begin2548 with BorderCells[C] do begin 2551 2549 // Calculate enemy power 2552 2550 // TODO: Do not sum different enemy power to one value 2553 2551 EnemyPower := 0; 2554 2552 for I := 0 to Neighbors.Count - 1 do 2555 if ( TCell(Neighbors[I]).Player <> Game.CurrentPlayer) and (TCell(Neighbors[I]).Player <> nil) then begin2556 Inc(EnemyPower, TCell(Neighbors[I]).Power);2553 if (Neighbors[I].Player <> Game.CurrentPlayer) and (Neighbors[I].Player <> nil) then begin 2554 Inc(EnemyPower, Neighbors[I].Power); 2557 2555 end; 2558 2556 if EnemyPower > (GetAvialPower + GetAttackPower) then begin … … 2561 2559 Player.Moves.Remove(MovesTo[I]); 2562 2560 for I := 0 to Neighbors.Count - 1 do 2563 if ( TCell(Neighbors[I]).Player = Player) and (AttackersCount(TCell(Neighbors[I])) = 0) then begin2564 Player.SetMove( TCell(BorderCells[C]), TCell(Neighbors[I]), GetAvialPower, False);2561 if (Neighbors[I].Player = Player) and (AttackersCount(Neighbors[I]) = 0) then begin 2562 Player.SetMove(BorderCells[C], Neighbors[I], GetAvialPower, False); 2565 2563 Break; 2566 2564 end; … … 2577 2575 Result := 0; 2578 2576 for I := 0 to Cell.Neighbors.Count - 1 do 2579 if ( TCell(Cell.Neighbors[I]).Player <> Game.CurrentPlayer) and2580 ( TCell(Cell.Neighbors[I]).Player <> nil) then begin2577 if (Cell.Neighbors[I].Player <> Game.CurrentPlayer) and 2578 (Cell.Neighbors[I].Player <> nil) then begin 2581 2579 Inc(Result); 2582 2580 end; … … 2828 2826 2829 2827 for C := 0 to List.Count - 1 do 2830 with TCell(List[C])do begin2828 with List[C] do begin 2831 2829 for I := 0 to Neighbors.Count - 1 do 2832 with TCell(Neighbors[I])do2830 with Neighbors[I] do 2833 2831 if (not Mark) and (Terrain <> ttVoid) and (Area <> SourceArea) and ((DestArea = nil) or (DestArea = Area)) then begin 2834 NewList.Add( TCell(TCell(List[C]).Neighbors[I]));2832 NewList.Add(List[C].Neighbors[I]); 2835 2833 Mark := True; 2836 2834 end else 2837 2835 if (not Mark) and (Terrain = ttVoid) then begin 2838 NewListVoid.Add( TCell(TCell(List[C]).Neighbors[I]));2836 NewListVoid.Add(List[C].Neighbors[I]); 2839 2837 Mark := True; 2840 2838 end; … … 2843 2841 if NewList.Count > 0 then begin 2844 2842 // We found cell with different area 2845 Result := TCell(NewList[Random(NewList.Count)]);2843 Result := NewList[Random(NewList.Count)]; 2846 2844 end else 2847 2845 if NewListVoid.Count > 0 then begin … … 2878 2876 for J := 0 to 4 do begin 2879 2877 2880 Cell := TCell(BorderList[Random(BorderList.Count)]);2878 Cell := BorderList[Random(BorderList.Count)]; 2881 2879 List.Clear; 2882 2880 List.Add(Cell); … … 2913 2911 begin 2914 2912 for C := 0 to Cells.Count - 1 do 2915 with TCell(Cells[C])do2913 with Cells[C] do 2916 2914 Area := nil; 2917 2915 Areas.Clear; 2918 2916 for C := 0 to Cells.Count - 1 do 2919 with TCell(Cells[C])do2917 with Cells[C] do 2920 2918 if (Terrain <> ttVoid) and (not Assigned(Area)) then begin 2921 2919 NewArea := TMapArea.Create; … … 3060 3058 mtTriangle: Map := TTriangleMap.Create; 3061 3059 mtVoronoi: Map := TVoronoiMap.Create; 3060 mtIsometric: Map := TIsometricMap.Create; 3062 3061 else Map := TMap.Create; 3063 3062 end; … … 3128 3127 // Remove cells already allocated to different player 3129 3128 for I := List.Count - 1 downto 0 do 3130 if Assigned( TCell(List[I]).Player) then3129 if Assigned(List[I].Player) then 3131 3130 List.Delete(I); 3132 3131 … … 3277 3276 if Assigned(NewNode) then 3278 3277 Players.LoadFromNode(NewNode); 3279 if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])3278 if Players.Count > 0 then CurrentPlayer := Players[0] 3280 3279 else CurrentPlayer := nil; 3281 3280 … … 3283 3282 3284 3283 for I := 0 to Players.Count - 1 do begin 3285 TPlayer(Players[I]).PlayerMap.Update;3286 TPlayer(Players[I]).PlayerMap.CheckVisibility;3284 Players[I].PlayerMap.Update; 3285 Players[I].PlayerMap.CheckVisibility; 3287 3286 end; 3288 3287 Running := ReadBoolean(RootNode, 'Running', True); … … 3341 3340 begin 3342 3341 for I := 0 to Players.Count - 1 do 3343 with TPlayer(Players[I])do begin3342 with Players[I] do begin 3344 3343 TotalUnits := 0; 3345 3344 TotalCells := 0; … … 3348 3347 TotalDiscovered := 0; 3349 3348 for J := 0 to PlayerMap.Cells.Count - 1 do 3350 with TPlayerCell(PlayerMap.Cells[J])do begin3349 with PlayerMap.Cells[J] do begin 3351 3350 if Explored then Inc(TotalDiscovered); 3352 3351 end; … … 3362 3361 begin 3363 3362 for I := 0 to Players.Count - 1 do 3364 with TPlayer(Players[I])do begin3363 with Players[I] do begin 3365 3364 NewStat := TGameTurnStat.Create; 3366 3365 NewStat.DiscoveredCells := TotalDiscovered; … … 3384 3383 // Skip dead players 3385 3384 repeat 3386 CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);3385 CurrentPlayer := Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]; 3387 3386 if Assigned(FOnPlayerChange) then 3388 3387 FOnPlayerChange(Self); … … 3412 3411 AlivePlayers := Players.GetAlivePlayers; 3413 3412 if (Length(AlivePlayers) <= 1) then begin 3414 if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);3413 if Length(AlivePlayers) > 0 then Winner := AlivePlayers[0]; 3415 3414 EndGame(Winner); 3416 3415 end; … … 3419 3418 AlivePlayers := Players.GetAlivePlayersWithCities; 3420 3419 if (Length(AlivePlayers) <= 1) then begin 3421 if Length(AlivePlayers) > 0 then Winner := TPlayer(AlivePlayers[0]);3420 if Length(AlivePlayers) > 0 then Winner := AlivePlayers[0]; 3422 3421 EndGame(Winner); 3423 3422 end; … … 3498 3497 if Assigned(Player.StartCell) then begin 3499 3498 if SymetricMap and (I = 1) then 3500 StartCell := TCell(Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(TPlayer(Players[0]).StartCell)]);3499 StartCell := Map.Cells[Map.Cells.Count - 1 - Map.Cells.IndexOf(Players[0].StartCell)]; 3501 3500 3502 3501 StartCell.Terrain := ttCity; … … 3508 3507 Inc(I); 3509 3508 end; 3510 if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])3509 if Players.Count > 0 then CurrentPlayer := Players[0] 3511 3510 else CurrentPlayer := nil; 3512 3511
Note:
See TracChangeset
for help on using the changeset viewer.