switch mapgen to enum. should still try and make sure the values are backwards compatible if possible.
authornemo
Tue, 02 Dec 2014 13:29:06 -0500
changeset 10603 bda5c7caf396
parent 10602 1ec0268f28af
child 10604 f2f9928ea2e7
switch mapgen to enum. should still try and make sure the values are backwards compatible if possible.
hedgewars/uCommandHandlers.pas
hedgewars/uConsts.pas
hedgewars/uLand.pas
hedgewars/uScript.pas
hedgewars/uTypes.pas
hedgewars/uUtils.pas
hedgewars/uVariables.pas
--- a/hedgewars/uCommandHandlers.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uCommandHandlers.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -666,7 +666,7 @@
 
 procedure chMapGen(var s: shortstring);
 begin
-cMapGen:= StrToInt(s)
+cMapGen:= TMapGen(StrToInt(s))
 end;
 
 procedure chTemplateFilter(var s: shortstring);
--- a/hedgewars/uConsts.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uConsts.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -318,12 +318,6 @@
 
     cMinPlayWidth = 200;
 
-    // MapGen
-    mgRandom = 0;
-    mgMaze   = 1;
-    mgPerlin = 2;
-    mgDrawn  = 3;
-
 implementation
 
 end.
--- a/hedgewars/uLand.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uLand.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -672,10 +672,10 @@
 begin
     WriteLnToConsole('Generating preview...');
     case cMapGen of
-        0: GenTemplated(EdgeTemplates[SelectTemplate]);
-        1: begin ResizeLand(4096,2048); GenMaze; end;
-        2: begin ResizeLand(4096,2048); GenPerlin; end;
-        3: GenDrawnMap;
+        mgRandom: GenTemplated(EdgeTemplates[SelectTemplate]);
+        mgMaze: begin ResizeLand(4096,2048); GenMaze; end;
+        mgPerlin: begin ResizeLand(4096,2048); GenPerlin; end;
+        mgDrawn: GenDrawnMap;
     else
         OutError('Unknown mapgen', true);
     end;
@@ -720,10 +720,10 @@
 begin
     WriteLnToConsole('Generating preview...');
     case cMapGen of
-        0: GenTemplated(EdgeTemplates[SelectTemplate]);
-        1: begin ResizeLand(4096,2048); GenMaze; end;
-        2: begin ResizeLand(4096,2048); GenPerlin; end;
-        3: GenDrawnMap;
+        mgRandom: GenTemplated(EdgeTemplates[SelectTemplate]);
+        mgMaze: begin ResizeLand(4096,2048); GenMaze; end;
+        mgPerlin: begin ResizeLand(4096,2048); GenPerlin; end;
+        mgDrawn: GenDrawnMap;
     else
         OutError('Unknown mapgen', true);
     end;
--- a/hedgewars/uScript.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uScript.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -307,6 +307,19 @@
         LuaToSpriteOrd:= i;
 end;
 
+function LuaToMapGenOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
+begin
+    if lua_isnoneornil(L, i) then i:= -1
+    else i:= lua_tointeger(L, i);
+    if (i < ord(Low(TMapGen))) or (i > ord(High(TMapGen))) then
+        begin
+        LuaCallError('Invalid mapgen id!', call, paramsyntax);
+        LuaToMapGenOrd:= -1;
+        end
+    else
+        LuaToMapGenOrd:= i;
+end;
+
 // wrapped calls //
 
 // functions called from Lua:
@@ -2313,7 +2326,7 @@
 ScriptSetString('Seed', cSeed);
 ScriptSetInteger('TemplateFilter', cTemplateFilter);
 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
-ScriptSetInteger('MapGen', cMapGen);
+ScriptSetInteger('MapGen', ord(cMapGen));
 
 ScriptCall('onPreviewInit');
 
@@ -2321,7 +2334,7 @@
 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
-cMapGen          := ScriptGetInteger('MapGen');
+cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
 end;
 
 procedure ScriptOnGameInit;
@@ -2337,7 +2350,7 @@
 ScriptSetString('Seed', cSeed);
 ScriptSetInteger('TemplateFilter', cTemplateFilter);
 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
-ScriptSetInteger('MapGen', cMapGen);
+ScriptSetInteger('MapGen', ord(cMapGen));
 ScriptSetInteger('ScreenHeight', cScreenHeight);
 ScriptSetInteger('ScreenWidth', cScreenWidth);
 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
@@ -2366,7 +2379,7 @@
 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
-cMapGen          := ScriptGetInteger('MapGen');
+cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
 GameFlags        := ScriptGetInteger('GameFlags');
 cHedgehogTurnTime:= ScriptGetInteger('TurnTime');
 cCaseFactor      := ScriptGetInteger('CaseFreq');
@@ -2700,6 +2713,7 @@
     he : THogEffect;
     cg : TCapGroup;
     spr: TSprite;
+    mg : TMapGen;
 begin
 // initialize lua
 luaState:= lua_open;
@@ -2791,6 +2805,8 @@
 for spr:= Low(TSprite) to High(TSprite) do
     ScriptSetInteger(EnumToStr(spr), ord(spr));
 
+for mg:= Low(TMapGen) to High(TMapGen) do
+    ScriptSetInteger(EnumToStr(mg), ord(mg));
 
 ScriptSetInteger('gstDrowning'      , gstDrowning);
 ScriptSetInteger('gstHHDriven'      , gstHHDriven);
@@ -2822,12 +2838,6 @@
 ScriptSetInteger('lfIce'           , lfIce);
 ScriptSetInteger('lfBouncy'        , lfBouncy);
 
-// mapgen
-ScriptSetInteger('mgRandom', mgRandom);
-ScriptSetInteger('mgMaze'  , mgMaze);
-ScriptSetInteger('mgPerlin', mgPerlin);
-ScriptSetInteger('mgDrawn' , mgDrawn);
-
 // register functions
 lua_register(luaState, _P'HideHog', @lc_hidehog);
 lua_register(luaState, _P'RestoreHog', @lc_restorehog);
--- a/hedgewars/uTypes.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uTypes.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -176,6 +176,8 @@
     TStereoMode = (smNone, smRedCyan, smCyanRed, smRedBlue, smBlueRed, smRedGreen, smGreenRed, smHorizontal, smVertical);
     TWorldEdge = (weNone, weWrap, weBounce, weSea, weSky);
     TUIDisplay = (uiAll, uiNoTeams, uiNone);
+    TMapGen = (mgRandom, mgMaze, mgPerlin, mgDrawn);
+
 
     THHFont = record
             Handle: PTTF_Font;
--- a/hedgewars/uUtils.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uUtils.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -35,6 +35,7 @@
 function  EnumToStr(const en : THogEffect) : shortstring; overload;
 function  EnumToStr(const en : TCapGroup) : shortstring; overload;
 function  EnumToStr(const en : TSprite) : shortstring; overload;
+function  EnumToStr(const en : TMapGen) : shortstring; overload;
 
 function  Min(a, b: LongInt): LongInt; inline;
 function  MinD(a, b: double) : double; inline;
@@ -186,6 +187,11 @@
 EnumToStr := GetEnumName(TypeInfo(TSprite), ord(en))
 end;
 
+function EnumToStr(const en: TMapGen) : shortstring; overload;
+begin
+EnumToStr := GetEnumName(TypeInfo(TMapGen), ord(en))
+end;
+
 
 function Min(a, b: LongInt): LongInt;
 begin
--- a/hedgewars/uVariables.pas	Tue Dec 02 12:02:20 2014 +0300
+++ b/hedgewars/uVariables.pas	Tue Dec 02 13:29:06 2014 -0500
@@ -99,7 +99,7 @@
     cMineDudPercent : LongWord;
     cTemplateFilter : LongInt;
     cFeatureSize    : LongInt;
-    cMapGen         : LongInt;
+    cMapGen         : TMapGen;
     cRopePercent    : LongWord;
     cGetAwayTime    : LongWord;
 
@@ -2562,7 +2562,7 @@
     cMineDudPercent     := 0;
     cTemplateFilter     := 0;
     cFeatureSize        := 50;
-    cMapGen             := 0;   // MAPGEN_REGULAR
+    cMapGen             := mgRandom;
     cHedgehogTurnTime   := 45000;
     cMinesTime          := 3000;
     cMaxAIThinkTime     := 9000;