Shannon V. OKeets
Posts: 22095
Joined: 5/19/2005 From: Honolulu, Hawaii Status: offline
|
By way of comparison, here is the code for the same routine as I received it from CWIF. There were no comments! ===
function LandCombatColumn(const IsAssault: Boolean; A, D: Extended;
out DieRollMod: Integer; out DRM: Extended; const ColMod: Integer;
out Column: Byte; const Hex: TSmallPoint;
AUseSnowUnits, DUseSnowUnits: Boolean;
const CheckFraction: Boolean; const ForceInvading: Boolean = False): Boolean;
var
Res: Extended;
TempRes: Integer;
AnyInvading: Boolean;
Index: Longint;
CR: TLandCombatHex;
Counter: Integer;
ColAmt: Integer;
IsCombatHex: Boolean;
LCTerrain: TTerrainTypes;
LCWeather: TWeatherTypes;
InCity: Boolean;
AttackingStack: TUnitStack;
DefendingStack: TUnitStack;
AHasArmor: Boolean;
CityDRM: Extended;
function AttackLocation(const U: TUnit): TSmallPoint;
begin
with U do
begin
if Column = MovingStackValue then
Result := MovingStack.OldLocation
else
Result := Location;
end;
end;
function AttackModifier(const U: TUnit): Integer;
begin
with TLandUnit(U) do
begin
if AttackLocation(U).X = SeaAreaValue then
Result := 1
else
Result := HexsideAttackModifier(AttackLocation(U),
THexArea.ConnectingHexsideRange(AttackLocation(U), Hex), True);
end;
end;
procedure CheckAttacker(var U: TUnit);
var
HS: THexsideRange;
HSAM: Integer;
begin
with TLandUnit(U) do
begin
if (U is TLandUnit) and not Invading and
(AttackLocation(U).X <> SeaAreaValue) then
begin
THexArea.ConnectingHexsideRange(AttackLocation(U), Hex, HS);
HSAM := AttackModifier(U);
if HSAM <> 0 then
begin
if (LCTerrain = teJungle) and Elite and
((Country = Japan.ID) or (Country = Australia.ID) or
((Country = UnitedStates.ID) and (UnitType in MarineSet))) then
DRM := DRM + (1 / HSAM);
if not IsAssault and (UnitType in ArmoredSet) and not InCity and
(LCTerrain in [teClear, teDesert]) and
(LCWeather = wFine) and
(Map.HexsideTerrainSet[Column, Row, HS] * FortHexsideSet = []) then
DRM := DRM + (1 / HSAM);
if AUseSnowUnits and SnowUnit then
DRM := DRM + (1 / HSAM);
end;
end;
if (U is TLandUnit) and Paradropping then
DRM := DRM + 1;
end;
end;
function CheckDefender(var U: TUnit): Boolean;
begin
with Options, TLandUnit(U) do
begin
if U is TLandUnit then
begin
Result := True;
if Disrupted then
begin
if TwoD10LandCRT then
begin
if TLandUnit(U).Small then
DRM := DRM + 1
else
DRM := DRM + 2;
end
else
DRM := DRM + 1;
end;
if TwoD10LandCRT then
begin
if DUseSnowUnits and SnowUnit then
DRM := DRM - 2;
if (UnitType in ArmoredSet) and not InCity and
(LCTerrain in [teClear, teDesert]) and
(LCWeather = wFine) then
DRM := DRM - Succ(Ord(not Small));
if AHasArmor and (UnitType in AntiTankSet) then
DRM := DRM - 1;
end;
end
else
Result := False;
end;
end;
procedure IsHQ(var U: TUnit);
begin
if U.UnitType in HeadquartersSet then
CityDRM := MaxExtended([CityDRM, 1 / AttackModifier(U)]);
end;
procedure AddAttacker(var U: TUnit);
begin
if U.Side = PhasingSide then
AttackingStack.Add(U);
end;
procedure CheckEngineerA(var U: TUnit);
begin
with TLandUnit(U) do
begin
if UnitType in EngineerSet then
CityDRM := CityDRM + (Combat / AttackModifier(U));
end;
end;
procedure CheckEngineerD(var U: TUnit);
begin
with TLandUnit(U) do
begin
if UnitType in EngineerSet then
CityDRM := CityDRM - Combat;
end;
end;
procedure GetHQBonus;
begin
if Options.TwoD10LandCRT and IsCombatHex then
begin
with CR do
begin
if HQA <> nil then
DRM := DRM + (HQA.Reorg / 2);
if HQD <> nil then
DRM := DRM - (HQD.Reorg / 2);
end;
end;
end;
begin
AttackingStack := TUnitStack.Create;
DefendingStack := TUnitStack.Create;
try
with Map, Hex do
begin
LCTerrain := Terrain[X, Y];
LCWeather := HexWeather[X, Y];
InCity := City[X, Y] <> cyNone;
end;
if AUseSnowUnits then
AUseSnowUnits := LCWeather in SnowWeather;
if DUseSnowUnits then
DUseSnowUnits := LCWeather in SnowWeather;
if not Options.FractionalOdds then
begin
A := Util.Round(A);
D := Util.Round(D);
end;
DRM := 0;
IsCombatHex := LandCombatHexes.Search(Hex, Index, CR);
if (A = 0) and (not (Phase in [pHQSupportD, pAdvanceAfterCombat]) or
(D > 0)) then
begin
Column := 0;
GetHQBonus;
Result := False;
Exit;
end;
if IsCombatHex then
CR.Units.ForEach(@AddAttacker);
if not (Phase in HQSupportPhases + [pShoreBombardmentD]) and
((Phase <> pGroundSupport) or
((SubPhase = spFlyA) and MovingStack.HasLandUnit)) then
AttackingStack.AddStack(MovingStack);
AHasArmor := AttackingStack.HasUnit(UFilterArmoredUnit);
with Hex do
DefendingStack.AddStack(UnitStacks[X, Y], UFilterLandUnitNotInvading);
AnyInvading := ForceInvading or AttackingStack.HasUnit(UFilterInvading);
if (Phase = pGroundSupport) and (SubPhase = spFlyD) and
MovingStack.HasLandUnit then
DefendingStack.AddStack(MovingStack);
if Options.AlliedCombatFriction then
DRM := DRM - Pred(MajorCountryCount(AttackingStack.MajorCountrySet));
if AnyInvading then
begin
if not IsCombatHex or not CR.IgnoreNotional then
DRM := DRM + 1;
end;
with Options do
begin
if AttackingStack.HasAllUnit(UFilterTerritorialUnit) then
DRM := DRM - Succ(Ord(TwoD10LandCRT));
if DefendingStack.HasLandUnit and
not DefendingStack.HasUnit(UFilterNotTerritorialUnitDefending) then
DRM := DRM + Succ(Ord(TwoD10LandCRT));
end;
if Options.TwoD10LandCRT then
begin
if D = 0 then
DRM := AutoVictoryModifier
else begin
with CR do
begin
AttackingStack.ForEach(@CheckAttacker);
DefendingStack.ForEach(@CheckDefender);
GetHQBonus;
if InCity then
begin
CityDRM := 0;
AttackingStack.ForEach(@IsHQ);
AttackingStack.ForEach(@CheckEngineerA);
DefendingStack.ForEach(@CheckEngineerD);
end;
end;
if LCTerrain = teJungle then
DRM := DRM - 4;
if InCity then
begin
CityDRM := CityDRM - 1;
with Map, Hex do
CityDRM := CityDRM - FactoryCount[X, Y];
DRM := DRM + MinExtended([0, CityDRM]);
end;
with Hex do
DRM := DRM - (Map.HexWeatherCombatMod[X, Y] * 2);
Res := A / D;
for Counter := 16 downto 0 do
begin
if Res >= OddsColumnFraction[Counter] then
begin
Column := Counter;
Break;
end;
end;
if Options.FractionalOdds then
begin
if Res >= 1 then
DRM := DRM + Trunc((Res * 2) + ColMod)
else
DRM := DRM + Trunc(4 - (1/Res * 2) + ColMod);
end
else begin
Column := Range(Column + ColMod, 0, 16);
DRM := DRM + OddsModifier[Column];
end;
end;
end
else begin
DefendingStack.ForEach(@CheckDefender);
if D = 0 then
Column := AutoVictoryColumn
else begin
Res := A / D;
with Hex, Map do
begin
ColAmt := ColMod - HexWeatherCombatMod[X, Y] +
(Ord(AUseSnowUnits) * 2) + Ord(CheckFraction);
for Counter := 1 to Abs(ColAmt) do
begin
if Res >= 2 then
begin
if ColAmt > 0 then
A := A + D
else if Res < 3 then
A := A - (D / 2)
else
A := A - D;
end
else if (Res >= 1) and (Res < 2) then
begin
if ColAmt > 0 then
A := A + (D / 2)
else
A := A - (D / 2);
end
else begin
if A < D then
DRM := DRM - 1
else
A := D;
end;
Res := A / D;
end;
end;
if IsAssault and (Res >= 5) then
begin
if Res >= 10 then
TempRes := Trunc(Res) - 2
else if Res >= 7 then
TempRes := 7
else
TempRes := 6;
end
else if Res >= 2 then
TempRes := Succ(Trunc(Res))
else if Res >= 1.5 then
TempRes := 2
else if Res >= 1 then
TempRes := 1
else if Res >= 0.5 then
TempRes := 0
else begin
Column := 0;
DRM := DRM - Trunc(D / A) + Ord(Frac(D /A) > 0) - 2;
Result := True;
Exit;
end;
Column := Min([8, TempRes]);
end;
end;
Result := True;
finally
AttackingStack.Free;
DefendingStack.Free;
if Options.TwoD10LandCRT then
DieRollMod := Range(Util.Round(DRM), -21, 21)
else
DieRollMod := Util.Round(DRM);
end;
end;
_____________________________
Steve Perfection is an elusive goal.
|