hedgewars/uLand.pas
changeset 10198 e9cbe111c0df
parent 10197 c57798251b55
child 10249 b47ac2c19de3
equal deleted inserted replaced
10197:c57798251b55 10198:e9cbe111c0df
    30 procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
    30 procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
    31 
    31 
    32 implementation
    32 implementation
    33 uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture, SysUtils,
    33 uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture, SysUtils,
    34      uVariables, uUtils, uCommands, adler32, uDebug, uLandPainted, uTextures,
    34      uVariables, uUtils, uCommands, adler32, uDebug, uLandPainted, uTextures,
    35      uLandGenMaze, uLandOutline, uPhysFSLayer, uScript, uLandGenPerlin;
    35      uLandGenMaze, uLandOutline, uPhysFSLayer, uScript, uLandGenPerlin,
       
    36      uLandGenTemplateBased, uLandUtils;
    36 
    37 
    37 var digest: shortstring;
    38 var digest: shortstring;
    38 
    39 
    39 procedure ResizeLand(width, height: LongWord);
       
    40 var potW, potH: LongInt;
       
    41 begin
       
    42 potW:= toPowerOf2(width);
       
    43 potH:= toPowerOf2(height);
       
    44 if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
       
    45     begin
       
    46     LAND_WIDTH:= potW;
       
    47     LAND_HEIGHT:= potH;
       
    48     LAND_WIDTH_MASK:= not(LAND_WIDTH-1);
       
    49     LAND_HEIGHT_MASK:= not(LAND_HEIGHT-1);
       
    50     cWaterLine:= LAND_HEIGHT;
       
    51     if (cReducedQuality and rqBlurryLand) = 0 then
       
    52         SetLength(LandPixels, LAND_HEIGHT, LAND_WIDTH)
       
    53     else
       
    54         SetLength(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2);
       
    55 
       
    56     SetLength(Land, LAND_HEIGHT, LAND_WIDTH);
       
    57     SetLength(LandDirty, (LAND_HEIGHT div 32), (LAND_WIDTH div 32));
       
    58     // 0.5 is already approaching on unplayable
       
    59     if (width div 4096 >= 2) or (height div 2048 >= 2) then cMaxZoomLevel:= 0.5;
       
    60     cMinMaxZoomLevelDelta:= cMaxZoomLevel - cMinZoomLevel
       
    61     end;
       
    62 end;
       
    63 
    40 
    64 procedure PrettifyLandAlpha();
    41 procedure PrettifyLandAlpha();
    65 begin
    42 begin
    66     if (cReducedQuality and rqBlurryLand) <> 0 then
    43     if (cReducedQuality and rqBlurryLand) <> 0 then
    67         PrettifyAlpha2D(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2)
    44         PrettifyAlpha2D(LandPixels, LAND_HEIGHT div 2, LAND_WIDTH div 2)
   219     // freed in freeModule() below
   196     // freed in freeModule() below
   220     LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
   197     LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
   221     if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
   198     if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
   222 end;
   199 end;
   223 
   200 
   224 procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
       
   225 var i: LongInt;
       
   226 begin
       
   227 with Template do
       
   228     begin
       
   229     pa.Count:= BasePointsCount;
       
   230     for i:= 0 to pred(pa.Count) do
       
   231         begin
       
   232         pa.ar[i].x:= BasePoints^[i].x + LongInt(GetRandom(BasePoints^[i].w));
       
   233         if pa.ar[i].x <> NTPX then
       
   234            pa.ar[i].x:= pa.ar[i].x + ((LAND_WIDTH - Template.TemplateWidth) div 2);
       
   235         pa.ar[i].y:= BasePoints^[i].y + LongInt(GetRandom(BasePoints^[i].h)) + LAND_HEIGHT - LongInt(Template.TemplateHeight)
       
   236         end;
       
   237 
       
   238     if canMirror then
       
   239         if getrandom(2) = 0 then
       
   240             begin
       
   241             for i:= 0 to pred(BasePointsCount) do
       
   242                if pa.ar[i].x <> NTPX then
       
   243                    pa.ar[i].x:= LAND_WIDTH - 1 - pa.ar[i].x;
       
   244             for i:= 0 to pred(FillPointsCount) do
       
   245                 fps^[i].x:= LAND_WIDTH - 1 - fps^[i].x;
       
   246             end;
       
   247 
       
   248 (*  Experiment in making this option more useful
       
   249      if ((not isNegative) and (cTemplateFilter = 4)) or
       
   250         (canFlip and (getrandom(2) = 0)) then
       
   251            begin
       
   252            for i:= 0 to pred(BasePointsCount) do
       
   253                begin
       
   254                pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
       
   255                if pa.ar[i].y > LAND_HEIGHT - 1 then
       
   256                    pa.ar[i].y:= LAND_HEIGHT - 1;
       
   257                end;
       
   258            for i:= 0 to pred(FillPointsCount) do
       
   259                begin
       
   260                FillPoints^[i].y:= LAND_HEIGHT - 1 - FillPoints^[i].y + (LAND_HEIGHT - TemplateHeight) * 2;
       
   261                if FillPoints^[i].y > LAND_HEIGHT - 1 then
       
   262                    FillPoints^[i].y:= LAND_HEIGHT - 1;
       
   263                end;
       
   264            end;
       
   265      end
       
   266 *)
       
   267 // template recycling.  Pull these off the floor a bit
       
   268     if (not isNegative) and (cTemplateFilter = 4) then
       
   269         begin
       
   270         for i:= 0 to pred(BasePointsCount) do
       
   271             begin
       
   272             dec(pa.ar[i].y, 100);
       
   273             if pa.ar[i].y < 0 then
       
   274                 pa.ar[i].y:= 0;
       
   275             end;
       
   276         for i:= 0 to pred(FillPointsCount) do
       
   277             begin
       
   278             dec(fps^[i].y, 100);
       
   279             if fps^[i].y < 0 then
       
   280                 fps^[i].y:= 0;
       
   281             end;
       
   282         end;
       
   283 
       
   284     if (canFlip and (getrandom(2) = 0)) then
       
   285         begin
       
   286         for i:= 0 to pred(BasePointsCount) do
       
   287             pa.ar[i].y:= LAND_HEIGHT - 1 - pa.ar[i].y;
       
   288         for i:= 0 to pred(FillPointsCount) do
       
   289             fps^[i].y:= LAND_HEIGHT - 1 - fps^[i].y;
       
   290         end;
       
   291     end
       
   292 end;
       
   293 
       
   294 
       
   295 procedure GenBlank(var Template: TEdgeTemplate);
       
   296 var pa: TPixAr;
       
   297     i: Longword;
       
   298     y, x: Longword;
       
   299     fps: TPointArray;
       
   300 begin
       
   301     fps:=Template.FillPoints^;
       
   302     ResizeLand(Template.TemplateWidth, Template.TemplateHeight);
       
   303     for y:= 0 to LAND_HEIGHT - 1 do
       
   304         for x:= 0 to LAND_WIDTH - 1 do
       
   305             Land[y, x]:= lfBasic;
       
   306     {$HINTS OFF}
       
   307     SetPoints(Template, pa, @fps);
       
   308     {$HINTS ON}
       
   309     
       
   310     for i:= 1 to Template.BezierizeCount do
       
   311         begin
       
   312         BezierizeEdge(pa, _0_5);
       
   313         RandomizePoints(pa);
       
   314         RandomizePoints(pa)
       
   315         end;
       
   316     for i:= 1 to Template.RandPassesCount do
       
   317         RandomizePoints(pa);
       
   318     BezierizeEdge(pa, _0_1);
       
   319 
       
   320 
       
   321     DrawEdge(pa, 0);
       
   322 
       
   323     with Template do
       
   324         for i:= 0 to pred(FillPointsCount) do
       
   325             with fps[i] do
       
   326                 FillLand(x, y, 0, 0);
       
   327 
       
   328     DrawEdge(pa, lfBasic);
       
   329 
       
   330     MaxHedgehogs:= Template.MaxHedgehogs;
       
   331     hasGirders:= Template.hasGirders;
       
   332     playHeight:= Template.TemplateHeight;
       
   333     playWidth:= Template.TemplateWidth;
       
   334     leftX:= ((LAND_WIDTH - playWidth) div 2);
       
   335     rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
       
   336     topY:= LAND_HEIGHT - playHeight;
       
   337 
       
   338     // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
       
   339     if (cTemplateFilter = 4)
       
   340     or (Template.canInvert and (getrandom(2) = 0))
       
   341     or (not Template.canInvert and Template.isNegative) then
       
   342         begin
       
   343         hasBorder:= true;
       
   344         for y:= 0 to LAND_HEIGHT - 1 do
       
   345             for x:= 0 to LAND_WIDTH - 1 do
       
   346                 if (y < topY) or (x < leftX) or (x > rightX) then
       
   347                     Land[y, x]:= 0
       
   348                 else
       
   349                     begin
       
   350                     if Land[y, x] = 0 then
       
   351                         Land[y, x]:= lfBasic
       
   352                     else if Land[y, x] = lfBasic then
       
   353                         Land[y, x]:= 0;
       
   354                     end;
       
   355         end;
       
   356 end;
       
   357 
   201 
   358 procedure GenDrawnMap;
   202 procedure GenDrawnMap;
   359 begin
   203 begin
   360     ResizeLand(4096, 2048);
   204     ResizeLand(4096, 2048);
   361     uLandPainted.Draw;
   205     uLandPainted.Draw;
   699             end
   543             end
   700         else
   544         else
   701             begin
   545             begin
   702             WriteLnToConsole('Generating land...');
   546             WriteLnToConsole('Generating land...');
   703             case cMapGen of
   547             case cMapGen of
   704                 0: GenBlank(EdgeTemplates[SelectTemplate]);
   548                 0: GenTemplated(EdgeTemplates[SelectTemplate]);
   705                 //1: begin ResizeLand(4096,2048); GenMaze; end;
   549                 //1: begin ResizeLand(4096,2048); GenMaze; end;
   706                 1: begin ResizeLand(4096,2048); GenPerlin; end;
   550                 1: begin ResizeLand(4096,2048); GenPerlin; end;
   707                 2: GenDrawnMap;
   551                 2: GenDrawnMap;
   708             else
   552             else
   709                 OutError('Unknown mapgen', true);
   553                 OutError('Unknown mapgen', true);
   828 procedure GenPreview(out Preview: TPreview);
   672 procedure GenPreview(out Preview: TPreview);
   829 var rh, rw, ox, oy, x, y, xx, yy, t, bit, cbit, lh, lw: LongInt;
   673 var rh, rw, ox, oy, x, y, xx, yy, t, bit, cbit, lh, lw: LongInt;
   830 begin
   674 begin
   831     WriteLnToConsole('Generating preview...');
   675     WriteLnToConsole('Generating preview...');
   832     case cMapGen of
   676     case cMapGen of
   833         0: GenBlank(EdgeTemplates[SelectTemplate]);
   677         0: GenTemplated(EdgeTemplates[SelectTemplate]);
   834         //1: begin ResizeLand(4096,2048); GenMaze; end;
   678         //1: begin ResizeLand(4096,2048); GenMaze; end;
   835         1: begin ResizeLand(4096,2048); GenPerlin; end;
   679         1: begin ResizeLand(4096,2048); GenPerlin; end;
   836         2: GenDrawnMap;
   680         2: GenDrawnMap;
   837     else
   681     else
   838         OutError('Unknown mapgen', true);
   682         OutError('Unknown mapgen', true);
   876 procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
   720 procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
   877 var rh, rw, ox, oy, x, y, xx, yy, t, lh, lw: LongInt;
   721 var rh, rw, ox, oy, x, y, xx, yy, t, lh, lw: LongInt;
   878 begin
   722 begin
   879     WriteLnToConsole('Generating preview...');
   723     WriteLnToConsole('Generating preview...');
   880     case cMapGen of
   724     case cMapGen of
   881         0: GenBlank(EdgeTemplates[SelectTemplate]);
   725         0: GenTemplated(EdgeTemplates[SelectTemplate]);
   882         //1: begin ResizeLand(4096,2048); GenMaze; end;
   726         //1: begin ResizeLand(4096,2048); GenMaze; end;
   883         1: begin ResizeLand(4096,2048); GenPerlin; end;
   727         1: begin ResizeLand(4096,2048); GenPerlin; end;
   884         2: GenDrawnMap;
   728         2: GenDrawnMap;
   885     else
   729     else
   886         OutError('Unknown mapgen', true);
   730         OutError('Unknown mapgen', true);