hedgewars/uLand.pas
branchwebgl
changeset 8330 aaefa587e277
parent 8096 453917e94e55
parent 8266 927da572bcdc
child 8444 75db7bb8dce8
equal deleted inserted replaced
8116:d24257910f8d 8330:aaefa587e277
    35 
    35 
    36 var digest: shortstring;
    36 var digest: shortstring;
    37 
    37 
    38 procedure ResizeLand(width, height: LongWord);
    38 procedure ResizeLand(width, height: LongWord);
    39 var potW, potH: LongInt;
    39 var potW, potH: LongInt;
    40 begin 
    40 begin
    41 potW:= toPowerOf2(width);
    41 potW:= toPowerOf2(width);
    42 potH:= toPowerOf2(height);
    42 potH:= toPowerOf2(height);
    43 if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
    43 if (potW <> LAND_WIDTH) or (potH <> LAND_HEIGHT) then
    44     begin
    44     begin
    45     LAND_WIDTH:= potW;
    45     LAND_WIDTH:= potW;
   238     playWidth:= Template.TemplateWidth;
   238     playWidth:= Template.TemplateWidth;
   239     leftX:= ((LAND_WIDTH - playWidth) div 2);
   239     leftX:= ((LAND_WIDTH - playWidth) div 2);
   240     rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
   240     rightX:= (playWidth + ((LAND_WIDTH - playWidth) div 2)) - 1;
   241     topY:= LAND_HEIGHT - playHeight;
   241     topY:= LAND_HEIGHT - playHeight;
   242 
   242 
   243     
   243 
   244     // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
   244     // HACK: force to only cavern even if a cavern map is invertable if cTemplateFilter = 4 ?
   245     if (cTemplateFilter = 4)
   245     if (cTemplateFilter = 4)
   246     or (Template.canInvert and (getrandom(2) = 0))
   246     or (Template.canInvert and (getrandom(2) = 0))
   247     or (not Template.canInvert and Template.isNegative) then
   247     or (not Template.canInvert and Template.isNegative) then
   248         begin
   248         begin
   338 
   338 
   339     LandSurface2LandPixels(tmpsurf);
   339     LandSurface2LandPixels(tmpsurf);
   340     SDL_FreeSurface(tmpsurf);
   340     SDL_FreeSurface(tmpsurf);
   341     for x:= leftX+2 to rightX-2 do
   341     for x:= leftX+2 to rightX-2 do
   342         for y:= topY+2 to LAND_HEIGHT-3 do
   342         for y:= topY+2 to LAND_HEIGHT-3 do
   343             if (Land[y, x] = 0) and 
   343             if (Land[y, x] = 0) and
   344                (((Land[y, x-1] = lfBasic) and ((Land[y+1,x] = lfBasic)) or (Land[y-1,x] = lfBasic)) or
   344                (((Land[y, x-1] = lfBasic) and ((Land[y+1,x] = lfBasic)) or (Land[y-1,x] = lfBasic)) or
   345                ((Land[y, x+1] = lfBasic) and ((Land[y-1,x] = lfBasic) or (Land[y+1,x] = lfBasic)))) then
   345                ((Land[y, x+1] = lfBasic) and ((Land[y-1,x] = lfBasic) or (Land[y+1,x] = lfBasic)))) then
   346             begin
   346             begin
   347                 if (cReducedQuality and rqBlurryLand) = 0 then
   347                 if (cReducedQuality and rqBlurryLand) = 0 then
   348                     begin
   348                     begin
   349                     if (Land[y, x-1] = lfBasic) and (LandPixels[y, x-1] and AMask <> 0) then
   349                     if (Land[y, x-1] = lfBasic) and (LandPixels[y, x-1] and AMask <> 0) then
   350                         LandPixels[y, x]:= LandPixels[y, x-1]
   350                         LandPixels[y, x]:= LandPixels[y, x-1]
   351                         
   351 
   352                     else if (Land[y, x+1] = lfBasic) and (LandPixels[y, x+1] and AMask <> 0) then
   352                     else if (Land[y, x+1] = lfBasic) and (LandPixels[y, x+1] and AMask <> 0) then
   353                         LandPixels[y, x]:= LandPixels[y, x+1]
   353                         LandPixels[y, x]:= LandPixels[y, x+1]
   354                         
   354 
   355                     else if (Land[y-1, x] = lfBasic) and (LandPixels[y-1, x] and AMask <> 0) then
   355                     else if (Land[y-1, x] = lfBasic) and (LandPixels[y-1, x] and AMask <> 0) then
   356                         LandPixels[y, x]:= LandPixels[y-1, x]
   356                         LandPixels[y, x]:= LandPixels[y-1, x]
   357                         
   357 
   358                     else if (Land[y+1, x] = lfBasic) and (LandPixels[y+1, x] and AMask <> 0) then
   358                     else if (Land[y+1, x] = lfBasic) and (LandPixels[y+1, x] and AMask <> 0) then
   359                         LandPixels[y, x]:= LandPixels[y+1, x];
   359                         LandPixels[y, x]:= LandPixels[y+1, x];
   360                         
   360 
   361                     if (((LandPixels[y,x] and AMask) shr AShift) > 10) then
   361                     if (((LandPixels[y,x] and AMask) shr AShift) > 10) then
   362                         LandPixels[y,x]:= (LandPixels[y,x] and (not AMask)) or (128 shl AShift)
   362                         LandPixels[y,x]:= (LandPixels[y,x] and (not AMask)) or (128 shl AShift)
   363                     end;
   363                     end;
   364                 Land[y,x]:= lfObject
   364                 Land[y,x]:= lfObject
   365             end
   365             end
   370                     ((Land[y, x+1] = lfBasic) and (Land[y-1,x+1] = lfBasic) and (Land[y-2,x] = lfBasic)) or
   370                     ((Land[y, x+1] = lfBasic) and (Land[y-1,x+1] = lfBasic) and (Land[y-2,x] = lfBasic)) or
   371                     ((Land[y+1, x] = lfBasic) and (Land[y+1,x+1] = lfBasic) and (Land[y,x+2] = lfBasic)) or
   371                     ((Land[y+1, x] = lfBasic) and (Land[y+1,x+1] = lfBasic) and (Land[y,x+2] = lfBasic)) or
   372                     ((Land[y-1, x] = lfBasic) and (Land[y-1,x+1] = lfBasic) and (Land[y,x+2] = lfBasic)) or
   372                     ((Land[y-1, x] = lfBasic) and (Land[y-1,x+1] = lfBasic) and (Land[y,x+2] = lfBasic)) or
   373                     ((Land[y+1, x] = lfBasic) and (Land[y+1,x-1] = lfBasic) and (Land[y,x-2] = lfBasic)) or
   373                     ((Land[y+1, x] = lfBasic) and (Land[y+1,x-1] = lfBasic) and (Land[y,x-2] = lfBasic)) or
   374                     ((Land[y-1, x] = lfBasic) and (Land[y-1,x-1] = lfBasic) and (Land[y,x-2] = lfBasic))) then
   374                     ((Land[y-1, x] = lfBasic) and (Land[y-1,x-1] = lfBasic) and (Land[y,x-2] = lfBasic))) then
   375                     
   375 
   376                 begin
   376                 begin
   377                 
   377 
   378                 if (cReducedQuality and rqBlurryLand) = 0 then
   378                 if (cReducedQuality and rqBlurryLand) = 0 then
   379                 
   379 
   380                     begin
   380                     begin
   381                     
   381 
   382                     if (Land[y, x-1] = lfBasic) and (LandPixels[y,x-1] and AMask <> 0) then
   382                     if (Land[y, x-1] = lfBasic) and (LandPixels[y,x-1] and AMask <> 0) then
   383                         LandPixels[y, x]:= LandPixels[y, x-1]
   383                         LandPixels[y, x]:= LandPixels[y, x-1]
   384                         
   384 
   385                     else if (Land[y, x+1] = lfBasic) and (LandPixels[y,x+1] and AMask <> 0) then
   385                     else if (Land[y, x+1] = lfBasic) and (LandPixels[y,x+1] and AMask <> 0) then
   386                         LandPixels[y, x]:= LandPixels[y, x+1]
   386                         LandPixels[y, x]:= LandPixels[y, x+1]
   387                         
   387 
   388                     else if (Land[y+1, x] = lfBasic) and (LandPixels[y+1,x] and AMask <> 0) then
   388                     else if (Land[y+1, x] = lfBasic) and (LandPixels[y+1,x] and AMask <> 0) then
   389                         LandPixels[y, x]:= LandPixels[y+1, x]
   389                         LandPixels[y, x]:= LandPixels[y+1, x]
   390                         
   390 
   391                     else if (Land[y-1, x] = lfBasic) and (LandPixels[y-1,x] and AMask <> 0) then
   391                     else if (Land[y-1, x] = lfBasic) and (LandPixels[y-1,x] and AMask <> 0) then
   392                         LandPixels[y, x]:= LandPixels[y-1, x];
   392                         LandPixels[y, x]:= LandPixels[y-1, x];
   393                         
   393 
   394                     if (((LandPixels[y,x] and AMask) shr AShift) > 10) then
   394                     if (((LandPixels[y,x] and AMask) shr AShift) > 10) then
   395                         LandPixels[y,x]:= (LandPixels[y,x] and (not AMask)) or (64 shl AShift)
   395                         LandPixels[y,x]:= (LandPixels[y,x] and (not AMask)) or (64 shl AShift)
   396                     end;
   396                     end;
   397                 Land[y,x]:= lfObject
   397                 Land[y,x]:= lfObject
   398             end;
   398             end;
   486         for y:= 0 to Pred(tmpsurf^.h) do
   486         for y:= 0 to Pred(tmpsurf^.h) do
   487         begin
   487         begin
   488             for x:= 0 to Pred(tmpsurf^.w) do
   488             for x:= 0 to Pred(tmpsurf^.w) do
   489             begin
   489             begin
   490                 // this an if instead of masking colours to avoid confusing map creators
   490                 // this an if instead of masking colours to avoid confusing map creators
   491                 if ((AMask and p^[x]) = 0) then 
   491                 if ((AMask and p^[x]) = 0) then
   492                     Land[cpY + y, cpX + x]:= 0
   492                     Land[cpY + y, cpX + x]:= 0
   493                 else if p^[x] = $FFFFFFFF then                  // white
   493                 else if p^[x] = $FFFFFFFF then                  // white
   494                     Land[cpY + y, cpX + x]:= lfObject
   494                     Land[cpY + y, cpX + x]:= lfObject
   495                 else if p^[x] = AMask then                      // black
   495                 else if p^[x] = AMask then                      // black
   496                     begin
   496                     begin
   522 tmpsurf:= nil;
   522 tmpsurf:= nil;
   523 end;
   523 end;
   524 
   524 
   525 procedure LoadMap;
   525 procedure LoadMap;
   526 var tmpsurf : PSDL_Surface;
   526 var tmpsurf : PSDL_Surface;
   527     s	    : shortstring;
   527     s       : shortstring;
   528     f	    : textfile;
   528     f       : textfile;
   529     mapName : shortstring = '';
   529     mapName : shortstring = '';
   530 
   530 
   531 begin
   531 begin
   532 WriteLnToConsole('Loading land from file...');
   532 WriteLnToConsole('Loading land from file...');
   533 AddProgress;
   533 AddProgress;
   554 BlitImageAndGenerateCollisionInfo(
   554 BlitImageAndGenerateCollisionInfo(
   555     (LAND_WIDTH - tmpsurf^.w) div 2,
   555     (LAND_WIDTH - tmpsurf^.w) div 2,
   556     LAND_HEIGHT - tmpsurf^.h,
   556     LAND_HEIGHT - tmpsurf^.h,
   557     tmpsurf^.w,
   557     tmpsurf^.w,
   558     tmpsurf);
   558     tmpsurf);
   559    
   559 
   560 SDL_FreeSurface(tmpsurf);
   560 SDL_FreeSurface(tmpsurf);
   561 
   561 
   562 LoadMask;
   562 LoadMask;
   563 
   563 
   564 end;
   564 end;
   599     if (GameFlags and gfForts) = 0 then
   599     if (GameFlags and gfForts) = 0 then
   600         if cPathz[ptMapCurrent] <> '' then
   600         if cPathz[ptMapCurrent] <> '' then
   601             begin
   601             begin
   602             map:= cPathz[ptMapCurrent] + '/map.png';
   602             map:= cPathz[ptMapCurrent] + '/map.png';
   603             mask:= cPathz[ptMapCurrent] + '/mask.png';
   603             mask:= cPathz[ptMapCurrent] + '/mask.png';
   604             if (not(FileExists(map)) and FileExists(mask)) then
   604             if (not(pfsExists(map)) and pfsExists(mask)) then
   605                 begin
   605                 begin
   606                 maskOnly:= true;
   606                 maskOnly:= true;
   607                 LoadMask;
   607                 LoadMask;
   608                 GenLandSurface
   608                 GenLandSurface
   609                 end
   609                 end
   633     for y:= topY to topY + 5 do
   633     for y:= topY to topY + 5 do
   634         for x:= leftX to rightX do
   634         for x:= leftX to rightX do
   635             if Land[y, x] <> 0 then
   635             if Land[y, x] <> 0 then
   636                 begin
   636                 begin
   637                 inc(c);
   637                 inc(c);
   638                 if c > 1000 then // avoid accidental triggering
   638                 if c > (LAND_WIDTH div 2) then // avoid accidental triggering
   639                     begin
   639                     begin
   640                     hasBorder:= true;
   640                     hasBorder:= true;
   641                     break;
   641                     break;
   642                     end;
   642                     end;
   643                 end;
   643                 end;
   695 if (GameFlags and gfDisableGirders) <> 0 then
   695 if (GameFlags and gfDisableGirders) <> 0 then
   696     hasGirders:= false;
   696     hasGirders:= false;
   697 
   697 
   698 if (GameFlags and gfForts = 0) and (maskOnly or (cPathz[ptMapCurrent] = '')) then
   698 if (GameFlags and gfForts = 0) and (maskOnly or (cPathz[ptMapCurrent] = '')) then
   699     AddObjects
   699     AddObjects
   700     
   700 
   701 else
   701 else
   702     AddProgress();
   702     AddProgress();
   703 
   703 
   704 FreeLandObjects;
   704 FreeLandObjects;
   705 
   705 
   751     if rw < rh*2 then
   751     if rw < rh*2 then
   752         begin
   752         begin
   753         rw:= rh*2;
   753         rw:= rh*2;
   754         end;
   754         end;
   755     if rh < rw div 2 then rh:= rw * 2;
   755     if rh < rw div 2 then rh:= rw * 2;
   756     
   756 
   757     ox:= (rw-LAND_WIDTH) div 2;
   757     ox:= (rw-LAND_WIDTH) div 2;
   758     oy:= rh-LAND_HEIGHT;
   758     oy:= rh-LAND_HEIGHT;
   759 
   759 
   760     lh:= rh div 128;
   760     lh:= rh div 128;
   761     lw:= rw div 32;
   761     lw:= rw div 32;
   767             begin
   767             begin
   768                 t:= 0;
   768                 t:= 0;
   769                 cbit:= bit * 8;
   769                 cbit:= bit * 8;
   770                 for yy:= y * lh to y * lh + 7 do
   770                 for yy:= y * lh to y * lh + 7 do
   771                     for xx:= x * lw + cbit to x * lw + cbit + 7 do
   771                     for xx:= x * lw + cbit to x * lw + cbit + 7 do
   772                         if ((yy-oy) and LAND_HEIGHT_MASK = 0) and ((xx-ox) and LAND_WIDTH_MASK = 0) 
   772                         if ((yy-oy) and LAND_HEIGHT_MASK = 0) and ((xx-ox) and LAND_WIDTH_MASK = 0)
   773                            and (Land[yy-oy, xx-ox] <> 0) then
   773                            and (Land[yy-oy, xx-ox] <> 0) then
   774                             inc(t);
   774                             inc(t);
   775                 if t > 8 then
   775                 if t > 8 then
   776                     Preview[y, x]:= Preview[y, x] or ($80 shr bit);
   776                     Preview[y, x]:= Preview[y, x] or ($80 shr bit);
   777             end;
   777             end;