hedgewars/uLand.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 9127 e350500c4edb
parent 9387 6478ed9ead25
child 9950 2759212a27de
--- a/hedgewars/uLand.pas	Fri Oct 11 11:55:31 2013 +0200
+++ b/hedgewars/uLand.pas	Fri Oct 11 17:43:13 2013 +0200
@@ -20,7 +20,7 @@
 
 unit uLand;
 interface
-uses SDLh, uLandTemplates, uFloat, uConsts, GLunit, uTypes, uAILandMarks;
+uses SDLh, uLandTemplates, uFloat, uConsts, uTypes, uAILandMarks;
 
 procedure initModule;
 procedure freeModule;
@@ -60,29 +60,12 @@
     end;
 end;
 
-procedure ColorizeLand(Surface: PSDL_Surface);
+
+procedure DrawBorderFromImage(Surface: PSDL_Surface);
 var tmpsurf: PSDL_Surface;
     r, rr: TSDL_Rect;
     x, yd, yu: LongInt;
 begin
-    tmpsurf:= LoadDataImage(ptCurrTheme, 'LandTex', ifCritical or ifIgnoreCaps);
-    r.y:= 0;
-    while r.y < LAND_HEIGHT do
-    begin
-        r.x:= 0;
-        while r.x < LAND_WIDTH do
-        begin
-            SDL_UpperBlit(tmpsurf, nil, Surface, @r);
-            inc(r.x, tmpsurf^.w)
-        end;
-        inc(r.y, tmpsurf^.h)
-    end;
-    SDL_FreeSurface(tmpsurf);
-
-    // freed in freeModule() below
-    LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
-    if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
-
     tmpsurf:= LoadDataImage(ptCurrTheme, 'Border', ifCritical or ifIgnoreCaps or ifTransparent);
     for x:= 0 to LAND_WIDTH - 1 do
     begin
@@ -127,6 +110,106 @@
     SDL_FreeSurface(tmpsurf);
 end;
 
+
+procedure DrawShoppaBorder;
+var x, y, s, i: Longword;
+    c1, c2, c: Longword;
+begin
+    c1:= AMask;
+    c2:= AMask or RMask or GMask;
+
+    // vertical
+    s:= LAND_HEIGHT;
+    
+    for x:= 0 to LAND_WIDTH - 1 do
+        for y:= 0 to LAND_HEIGHT - 1 do
+            if LandPixels[y, x] = 0 then
+                if s < y then
+                    begin
+                    for i:= max(s, y - 8) to y - 1 do
+                        begin
+                        if ((x + i) and 16) = 0 then c:= c1 else c:= c2;
+                        
+                        if (cReducedQuality and rqBlurryLand) = 0 then
+                            LandPixels[i, x]:= c
+                        else
+                            LandPixels[i div 2, x div 2]:= c
+                        end;
+                    s:= LAND_HEIGHT
+                    end
+                else
+            else
+                begin
+                if s > y then s:= y;
+                if s + 8 > y then
+                    begin
+                    if ((x + y) and 16) = 0 then c:= c1 else c:= c2;
+                    
+                    if (cReducedQuality and rqBlurryLand) = 0 then
+                        LandPixels[y, x]:= c
+                    else
+                        LandPixels[y div 2, x div 2]:= c
+                    end;            
+                end;
+                
+    // horizontal
+    s:= LAND_WIDTH;
+    
+    for y:= 0 to LAND_HEIGHT - 1 do
+        for x:= 0 to LAND_WIDTH - 1 do
+            if LandPixels[y, x] = 0 then
+                if s < x then
+                    begin
+                    for i:= max(s, x - 8) to x - 1 do
+                        begin
+                        if ((y + i) and 16) = 0 then c:= c1 else c:= c2;
+                        
+                        if (cReducedQuality and rqBlurryLand) = 0 then
+                            LandPixels[y, i]:= c
+                        else
+                            LandPixels[y div 2, i div 2]:= c
+                        end;
+                    s:= LAND_WIDTH
+                    end
+                else
+            else
+                begin
+                if s > x then s:= x;
+                if s + 8 > x then
+                    begin
+                    if ((x + y) and 16) = 0 then c:= c1 else c:= c2;
+                    
+                    if (cReducedQuality and rqBlurryLand) = 0 then
+                        LandPixels[y, x]:= c
+                    else
+                        LandPixels[y div 2, x div 2]:= c
+                    end;            
+                end
+end;
+
+procedure ColorizeLand(Surface: PSDL_Surface);
+var tmpsurf: PSDL_Surface;
+    r: TSDL_Rect;
+begin
+    tmpsurf:= LoadDataImage(ptCurrTheme, 'LandTex', ifCritical or ifIgnoreCaps);
+    r.y:= 0;
+    while r.y < LAND_HEIGHT do
+    begin
+        r.x:= 0;
+        while r.x < LAND_WIDTH do
+        begin
+            SDL_UpperBlit(tmpsurf, nil, Surface, @r);
+            inc(r.x, tmpsurf^.w)
+        end;
+        inc(r.y, tmpsurf^.h)
+    end;
+    SDL_FreeSurface(tmpsurf);
+
+    // freed in freeModule() below
+    LandBackSurface:= LoadDataImage(ptCurrTheme, 'LandBackTex', ifIgnoreCaps or ifTransparent);
+    if (LandBackSurface <> nil) and GrayScale then Surface2GrayScale(LandBackSurface);
+end;
+
 procedure SetPoints(var Template: TEdgeTemplate; var pa: TPixAr; fps: PPointArray);
 var i: LongInt;
 begin
@@ -276,22 +359,34 @@
 end;
 
 function SelectTemplate: LongInt;
+var l: LongInt;
 begin
     if (cReducedQuality and rqLowRes) <> 0 then
         SelectTemplate:= SmallTemplates[getrandom(Succ(High(SmallTemplates)))]
     else
+    begin
+        if cTemplateFilter = 0 then
+            begin
+            l:= getRandom(GroupedTemplatesCount);
+            repeat
+                inc(cTemplateFilter);
+                dec(l, TemplateCounts[cTemplateFilter]);
+            until l < 0;
+            end else getRandom(1);
+        
         case cTemplateFilter of
-        0: SelectTemplate:= getrandom(Succ(High(EdgeTemplates)));
-        1: SelectTemplate:= SmallTemplates[getrandom(Succ(High(SmallTemplates)))];
-        2: SelectTemplate:= MediumTemplates[getrandom(Succ(High(MediumTemplates)))];
-        3: SelectTemplate:= LargeTemplates[getrandom(Succ(High(LargeTemplates)))];
-        4: SelectTemplate:= CavernTemplates[getrandom(Succ(High(CavernTemplates)))];
-        5: SelectTemplate:= WackyTemplates[getrandom(Succ(High(WackyTemplates)))];
+        0: OutError('Ask unC0Rr about what you did wrong', true);
+        1: SelectTemplate:= SmallTemplates[getrandom(TemplateCounts[cTemplateFilter])];
+        2: SelectTemplate:= MediumTemplates[getrandom(TemplateCounts[cTemplateFilter])];
+        3: SelectTemplate:= LargeTemplates[getrandom(TemplateCounts[cTemplateFilter])];
+        4: SelectTemplate:= CavernTemplates[getrandom(TemplateCounts[cTemplateFilter])];
+        5: SelectTemplate:= WackyTemplates[getrandom(TemplateCounts[cTemplateFilter])];
 // For lua only!
         6: begin
            SelectTemplate:= min(LuaTemplateNumber,High(EdgeTemplates));
            GetRandom(2) // burn 1
            end;
+        end
     end;
 
     WriteLnToConsole('Selected template #'+inttostr(SelectTemplate)+' using filter #'+inttostr(cTemplateFilter));
@@ -334,10 +429,14 @@
 
     TryDo(tmpsurf <> nil, 'Error creating pre-land surface', true);
     ColorizeLand(tmpsurf);
+    if gameFlags and gfShoppaBorder = 0 then DrawBorderFromImage(tmpsurf);
     AddOnLandObjects(tmpsurf);
 
     LandSurface2LandPixels(tmpsurf);
     SDL_FreeSurface(tmpsurf);
+    
+    if gameFlags and gfShoppaBorder <> 0 then DrawShoppaBorder;
+    
     for x:= leftX+2 to rightX-2 do
         for y:= topY+2 to LAND_HEIGHT-3 do
             if (Land[y, x] = 0) and
@@ -518,7 +617,7 @@
     tmpsurf:= LoadDataImage(ptMissionMaps, mapName + '/map', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
     end;
 // (bare) Sanity check. Considering possible LongInt comparisons as well as just how much system memoery it would take
-TryDo((tmpsurf^.w < $40000000) and (tmpsurf^.h < $40000000) and (tmpsurf^.w * tmpsurf^.h < 6*1024*1024*1024), 'Map dimensions too big!', true);
+TryDo((tmpsurf^.w < $40000000) and (tmpsurf^.h < $40000000) and (QWord(tmpsurf^.w) * tmpsurf^.h < 6*1024*1024*1024), 'Map dimensions too big!', true);
 
 ResizeLand(tmpsurf^.w, tmpsurf^.h);
 LoadMapConfig;