Engine can generate land preview and send it via IPC
authorunc0rr
Wed, 20 Sep 2006 18:24:13 +0000
changeset 155 401f4ea24715
parent 154 5667e6f38704
child 156 3d6e89bba384
Engine can generate land preview and send it via IPC
hedgewars/hwengine.dpr
hedgewars/uConsts.pas
hedgewars/uIO.pas
hedgewars/uLand.pas
hedgewars/uRandom.pas
--- a/hedgewars/hwengine.dpr	Wed Sep 20 15:33:47 2006 +0000
+++ b/hedgewars/hwengine.dpr	Wed Sep 20 18:24:13 2006 +0000
@@ -184,17 +184,25 @@
 for i:= 0 to ParamCount do
     AddFileLog(inttostr(i) + ': ' + ParamStr(i));
 {$ENDIF}
-if ParamCount = 7 then
-   begin
-   val(ParamStr(1), cScreenWidth, c);
-   val(ParamStr(2), cScreenHeight, c);
-   cBitsStr:= ParamStr(3);
-   val(cBitsStr, cBits, c);
-   val(ParamStr(4), ipcPort, c);
-   cFullScreen:= ParamStr(5) = '1';
-   isSoundEnabled:= ParamStr(6) = '1';
-   cLocaleFName:= ParamStr(7);
-   end else OutError(errmsgShouldntRun, true)
+case ParamCount of
+  7: begin
+     val(ParamStr(1), cScreenWidth, c);
+     val(ParamStr(2), cScreenHeight, c);
+     cBitsStr:= ParamStr(3);
+     val(cBitsStr, cBits, c);
+     val(ParamStr(4), ipcPort, c);
+     cFullScreen:= ParamStr(5) = '1';
+     isSoundEnabled:= ParamStr(6) = '1';
+     cLocaleFName:= ParamStr(7);
+     end;
+  2: begin
+     val(ParamStr(1), ipcPort, c);
+     GameType:= gmtLandPreview;
+     if ParamStr(2) <> 'landpreview' then OutError(errmsgShouldntRun, true);
+     end
+   else
+   OutError(errmsgShouldntRun, true)
+   end
 end;
 
 procedure ShowMainWindow;
@@ -208,16 +216,10 @@
 PixelFormat:= SDLPrimSurface.format;
 SDL_ShowCursor(0);
 end;
+
 ////////////////////////////////////////////////////////////////////////////////
-/////////////////////////////// m a i n ////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////
-
+procedure Game;
 begin
-WriteLnToConsole('-= HedgeWars 0.2 =-');
-WriteLnToConsole('  -= by unC0Rr =-  ');
-GetParams;
-Randomize;
-
 WriteToConsole('Init SDL... ');
 SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
 WriteLnToConsole(msgOK);
@@ -249,5 +251,30 @@
       true);
 
 MainLoop
+end;
 
+procedure GenLandPreview;
+begin
+InitIPC;
+SendIPCAndWaitReply('C');
+TryDo(InitStepsFlags = cifRandomize,
+      'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')',
+      true);
+GenPreview;
+SendIPCRaw(@Preview, sizeof(Preview));
+SendIPCAndWaitReply('+');
+end;
+
+////////////////////////////////////////////////////////////////////////////////
+/////////////////////////////// m a i n ////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////
+
+begin
+WriteLnToConsole('-= HedgeWars 0.2 =-');
+WriteLnToConsole('  -= by unC0Rr =-  ');
+GetParams;
+Randomize;
+
+if GameType = gmtLandPreview then GenLandPreview
+                             else Game
 end.
--- a/hedgewars/uConsts.pas	Wed Sep 20 15:33:47 2006 +0000
+++ b/hedgewars/uConsts.pas	Wed Sep 20 18:24:13 2006 +0000
@@ -38,7 +38,7 @@
 type TStuff     = (sConsoleBG, sPowerBar, sQuestion, sWindBar,
                    sWindL, sWindR, sRopeNode);
      TGameState = (gsLandGen, gsStart, gsGame, gsConsole, gsExit);
-     TGameType  = (gmtLocal, gmtDemo, gmtNet, gmtSave);
+     TGameType  = (gmtLocal, gmtDemo, gmtNet, gmtSave, gmtLandPreview);
      TPathType  = (ptNone, ptData, ptGraphics, ptThemes, ptCurrTheme, ptTeams, ptMaps,
                    ptMapCurrent, ptDemos, ptSounds, ptGraves, ptFonts, ptForts,
                    ptLocale);
--- a/hedgewars/uIO.pas	Wed Sep 20 15:33:47 2006 +0000
+++ b/hedgewars/uIO.pas	Wed Sep 20 18:24:13 2006 +0000
@@ -40,6 +40,7 @@
 
 procedure SendIPC(s: shortstring);
 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
+procedure SendIPCRaw(p: pointer; len: Longword);
 procedure SendIPCAndWaitReply(s: shortstring);
 procedure IPCCheckSock;
 procedure InitIPC;
@@ -145,6 +146,11 @@
    end
 end;
 
+procedure SendIPCRaw(p: pointer; len: Longword);
+begin
+SDLNet_TCP_Send(IPCSock, p, len)
+end;
+
 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
 var s: shortstring;
 begin
--- a/hedgewars/uLand.pas	Wed Sep 20 15:33:47 2006 +0000
+++ b/hedgewars/uLand.pas	Wed Sep 20 18:24:13 2006 +0000
@@ -33,17 +33,21 @@
 
 unit uLand;
 interface
-uses SDLh, uGears;
+uses SDLh, uGears, uLandTemplates;
 {$include options.inc}
 type TLandArray = packed array[0..1023, 0..2047] of LongWord;
+     TPreview = packed array[0..127, 0..31] of byte;
 
 var  Land: TLandArray;
      LandSurface: PSDL_Surface;
+     Preview: TPreview;
 
 procedure GenMap;
+procedure GenPreview;
+
 
 implementation
-uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uIO, uLandTemplates, uLandObjects;
+uses uConsole, uStore, uMisc, uConsts, uRandom, uTeams, uIO, uLandObjects;
 
 type TPixAr = record
               Count: Longword;
@@ -420,7 +424,12 @@
 procedure GenBlank(var Template: TEdgeTemplate);
 var pa: TPixAr;
     i: Longword;
+    y, x: Longword;
 begin
+for y:= 0 to 1023 do
+    for x:= 0 to 2047 do
+        Land[y, x]:= COLOR_LAND;
+
 with Template do
      begin
      if canMirror then
@@ -462,12 +471,9 @@
 
 procedure GenLandSurface;
 var tmpsurf: PSDL_Surface;
-    y, x: Longword;
 begin
 WriteLnToConsole('Generating land...');
-for y:= 0 to 1023 do
-    for x:= 0 to 2047 do
-        Land[y, x]:= COLOR_LAND;
+
 GenBlank(EdgeTemplates[getrandom(Succ(High(EdgeTemplates)))]);
 
 AddProgress;
@@ -562,6 +568,26 @@
 {$IFDEF DEBUGFILE}LogLandDigest{$ENDIF}
 end;
 
+procedure GenPreview;
+var x, y, xx, yy, t, bit: integer;
+begin
+GenBlank(EdgeTemplates[getrandom(Succ(High(EdgeTemplates)))]);
+
+for y:= 0 to 127 do
+    for x:= 0 to 31 do
+        begin
+        Preview[y, x]:= 0;
+        for bit:= 0 to 7 do
+            begin
+            t:= 0;
+            for yy:= y * 8 to y * 8 + 7 do
+                for xx:= x * 64 + bit * 8 to x * 64 + bit * 8 + 7 do
+                    if Land[yy, xx] <> 0 then inc(t);
+            if t > 31 then Preview[y, x]:= Preview[y, x] or ($80 shr bit) 
+            end
+        end
+end;
+
 initialization
 
 end.
--- a/hedgewars/uRandom.pas	Wed Sep 20 15:33:47 2006 +0000
+++ b/hedgewars/uRandom.pas	Wed Sep 20 18:24:13 2006 +0000
@@ -57,6 +57,8 @@
 procedure SetRandomSeed(Seed: shortstring);
 var i: Longword;
 begin
+n:= 54;
+
 if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
 
 for i:= 0 to pred(Length(Seed)) do