Ignore:
Timestamp:
Feb 23, 2021, 10:47:47 AM (3 years ago)
Author:
chronos
Message:
  • Modified: Optimized TurnToYear function to eliminate unnecessary loop.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/CevoComponents/ScreenTools.pas

    r256 r286  
    263263
    264264function TurnToYear(Turn: Integer): Integer;
    265 var
    266   I: Integer;
    267265begin
    268266  Result := -4000;
    269   for I := 1 to Turn do
    270     if Result < -1000 then Inc(Result, 50) // 0..60
    271     else if Result < 0 then Inc(Result, 25) // 60..100
    272     else if Result < 1500 then Inc(Result, 20) // 100..175
    273     else if Result < 1750 then Inc(Result, 10) // 175..200
    274     else if Result < 1850 then Inc(Result, 2) // 200..250
    275     else Inc(Result);
     267  if Turn <= 0 then Exit;
     268
     269  // Year -4000..-1000, Turn 0..60
     270  Inc(Result, Min(60, Turn) * 50);
     271  Dec(Turn, Min(60, Turn));
     272  if Turn = 0 then Exit;
     273
     274  // Year -1000..0, Turn 60..100
     275  Inc(Result, Min(40, Turn) * 25);
     276  Dec(Turn, Min(40, Turn));
     277  if Turn = 0 then Exit;
     278
     279  // Year 0..1500, Turn 100..175
     280  Inc(Result, Min(75, Turn) * 20);
     281  Dec(Turn, Min(75, Turn));
     282  if Turn = 0 then Exit;
     283
     284  // Year 1500..1750, Turn 175..200
     285  Inc(Result, Min(25, Turn) * 10);
     286  Dec(Turn, Min(25, Turn));
     287  if Turn = 0 then Exit;
     288
     289  // Year 1750..1850, Turn 200..250
     290  Inc(Result, Min(50, Turn) * 2);
     291  Dec(Turn, Min(50, Turn));
     292  if Turn = 0 then Exit;
     293
     294  // Year 1850.., Turn 250..
     295  Inc(Result, Turn);
    276296end;
    277297
Note: See TracChangeset for help on using the changeset viewer.