hedgewars/uLandGenTemplateBased.pas
author unc0rr
Fri, 14 Mar 2014 19:59:34 +0400
changeset 10198 e9cbe111c0df
child 10199 fdb689b57b1b
permissions -rw-r--r--
Move template-based generator into its own file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10198
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     1
unit uLandGenTemplateBased;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     2
interface
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     3
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     4
uses uLandTemplates;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     5
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     6
procedure GenTemplated(var Template: TEdgeTemplate);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     7
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     8
implementation
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
     9
uses uTypes, uVariables, uConsts, uFloat, uLandOutline, uLandUtils, uRandom;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    10
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    11
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    12
procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    13
var i: LongInt;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    14
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    15
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    16
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    17
        pa.Count:= BasePointsCount;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    18
        for i:= 0 to pred(pa.Count) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    19
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    20
            pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    21
            if pa.ar[i].x <> NTPX then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    22
            pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    23
            pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    24
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    25
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    26
        if canMirror then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    27
            if getrandom(2) = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    28
                begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    29
                for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    30
                if pa.ar[i].x <> NTPX then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    31
                    pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    32
                for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    33
                    fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    34
                end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    35
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    36
(*  Experiment in making this option more useful
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    37
     if ((not isNegative) and (cTemplateFilter = 4)) or
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    38
        (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    39
           begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    40
           for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    41
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    42
               pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    43
               if pa.ar[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    44
                   pa.ar[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    45
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    46
           for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    47
               begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    48
               FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    49
               if FillPoints^[i].y > LAND_HEIGHT - 1 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    50
                   FillPoints^[i].y:= LAND_HEIGHT - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    51
               end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    52
           end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    53
     end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    54
*)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    55
// template recycling.  Pull these off the floor a bit
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    56
    if (not isNegative) and (cTemplateFilter = 4) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    57
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    58
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    59
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    60
            dec(pa.ar[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    61
            if pa.ar[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    62
                pa.ar[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    63
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    64
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    65
            begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    66
            dec(fps^[i].y, 100);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    67
            if fps^[i].y < 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    68
                fps^[i].y:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    69
            end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    70
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    71
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    72
    if (canFlip and (getrandom(2) = 0)) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    73
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    74
        for i:= 0 to pred(BasePointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    75
            pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    76
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    77
            fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    78
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    79
    end
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    80
end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    81
    
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    82
procedure GenTemplated(var Template: TEdgeTemplate);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    83
var pa: TPixAr;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    84
    i: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    85
    y, x: Longword;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    86
    fps: TPointArray;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    87
begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    88
    fps:=Template.FillPoints^;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    89
    ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    90
    for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    91
        for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    92
            Land[y, x]:= lfBasic;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    93
    {$HINTS OFF}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    94
    SetPoints(Template, pa, @fps);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    95
    {$HINTS ON}
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    96
    
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    97
    for i:= 1 to Template.BezierizeCount do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    98
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
    99
        BezierizeEdge(pa, _0_5);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   100
        RandomizePoints(pa);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   101
        RandomizePoints(pa)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   102
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   103
    for i:= 1 to Template.RandPassesCount do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   104
        RandomizePoints(pa);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   105
    BezierizeEdge(pa, _0_1);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   106
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   107
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   108
    DrawEdge(pa, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   109
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   110
    with Template do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   111
        for i:= 0 to pred(FillPointsCount) do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   112
            with fps[i] do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   113
                FillLand(x, y, 0, 0);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   114
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   115
    DrawEdge(pa, lfBasic);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   116
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   117
    MaxHedgehogs:= Template.MaxHedgehogs;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   118
    hasGirders:= Template.hasGirders;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   119
    playHeight:= Template.TemplateHeight;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   120
    playWidth:= Template.TemplateWidth;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   121
    leftX:= ((LAND_WIDTH - playWidth) div 2);
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   122
    rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   123
    topY:= LAND_HEIGHT - playHeight;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   124
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   125
    // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   126
    if (cTemplateFilter = 4)
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   127
    or (Template.canInvert and (getrandom(2) = 0))
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   128
    or (not Template.canInvert and Template.isNegative) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   129
        begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   130
        hasBorder:= true;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   131
        for y:= 0 to LAND_HEIGHT - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   132
            for x:= 0 to LAND_WIDTH - 1 do
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   133
                if (y < topY) or (x < leftX) or (x > rightX) then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   134
                    Land[y, x]:= 0
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   135
                else
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   136
                    begin
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   137
                    if Land[y, x] = 0 then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   138
                        Land[y, x]:= lfBasic
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   139
                    else if Land[y, x] = lfBasic then
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   140
                        Land[y, x]:= 0;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   141
                    end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   142
        end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   143
end;
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   144
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   145
e9cbe111c0df Move template-based generator into its own file
unc0rr
parents:
diff changeset
   146
end.