hedgewars/uUtils.pas
branchqmlfrontend
changeset 10606 8e95911cb86b
parent 10515 7705784902e1
parent 10605 df7a73db2c43
child 10748 dc587913987c
--- a/hedgewars/uUtils.pas	Sat Nov 22 00:55:01 2014 +0300
+++ b/hedgewars/uUtils.pas	Wed Dec 03 00:07:33 2014 +0300
@@ -35,8 +35,10 @@
 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;
 function  Max(a, b: LongInt): LongInt; inline;
 
 function  IntToStr(n: LongInt): shortstring;
@@ -95,7 +97,7 @@
 uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils;
 
 {$IFDEF DEBUGFILE}
-var f: textfile;
+var logFile: textfile;
 {$IFDEF USE_VIDEO_RECORDING}
     logMutex: TRTLCriticalSection; // mutex for debug file
 {$ENDIF}
@@ -185,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
@@ -194,6 +201,14 @@
     Min:= b
 end;
 
+function MinD(a, b: double): double;
+begin
+if a < b then
+    MinD:= a
+else
+    MinD:= b
+end;
+
 function Max(a, b: LongInt): LongInt;
 begin
 if a > b then
@@ -217,7 +232,7 @@
 val(s, StrToInt, c);
 {$IFDEF DEBUGFILE}
 if c <> 0 then
-    writeln(f, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
+    writeln(logFile, 'Error at position ' + IntToStr(c) + ' : ' + s[c])
 {$ENDIF}
 {$ENDIF}
 end;
@@ -280,7 +295,7 @@
 
 function DecodeBase64(s: shortstring): shortstring;
 const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
-var i, t, c: Longword;
+var i, t, c: LongInt;
 begin
 c:= 0;
 for i:= 1 to Length(s) do
@@ -345,8 +360,8 @@
 {$IFDEF USE_VIDEO_RECORDING}
 EnterCriticalSection(logMutex);
 {$ENDIF}
-writeln(f, inttostr(GameTicks)  + ': ' + s);
-flush(f);
+writeln(logFile, inttostr(GameTicks)  + ': ' + s);
+flush(logFile);
 
 {$IFDEF USE_VIDEO_RECORDING}
 LeaveCriticalSection(logMutex);
@@ -363,8 +378,8 @@
 {$IFDEF USE_VIDEO_RECORDING}
 EnterCriticalSection(logMutex);
 {$ENDIF}
-write(f, s);
-flush(f);
+write(logFile, s);
+flush(logFile);
 {$IFDEF USE_VIDEO_RECORDING}
 LeaveCriticalSection(logMutex);
 {$ENDIF}
@@ -505,6 +520,7 @@
 {$IFDEF DEBUGFILE}
 var logfileBase: shortstring;
     i: LongInt;
+    rwfailed: boolean;
 {$ENDIF}
 begin
 {$IFDEF DEBUGFILE}
@@ -529,10 +545,8 @@
     InitCriticalSection(logMutex);
 {$ENDIF}
 {$I-}
-{$IFNDEF PAS2C}
-    f:= stderr; // if everything fails, write to stderr
-{$ENDIF}
-(*    if (length(UserPathPrefix) > 0) then
+    rwfailed:= false;
+    (*if (length(UserPathPrefix) > 0) then
         begin
         {$IFNDEF PAS2C}
         // create directory if it doesn't exist
@@ -543,13 +557,21 @@
         i:= 0;
         while(i < 7) do
             begin
-            assign(f, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
-            if IOResult = 0 then
+            assign(logFile, shortstring(UserPathPrefix) + '/Logs/' + logfileBase + inttostr(i) + '.log');
+            Rewrite(logFile);
+            // note: IOResult is a function in pascal and a variable in pas2c
+            rwfailed:= (IOResult <> 0);
+            if (not rwfailed) then
                 break;
             inc(i)
             end;
-        end;*)
-    Rewrite(f);
+        end;
+    *)
+{$IFNDEF PAS2C}
+    // if everything fails, write to stderr
+    //if (length(UserPathPrefix) = 0) or (rwfailed) then
+        logFile:= stderr;
+{$ENDIF}
 {$I+}
 {$ENDIF}
 
@@ -571,9 +593,9 @@
 procedure freeModule;
 begin
 {$IFDEF DEBUGFILE}
-    writeln(f, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
-    flush(f);
-    close(f);
+    writeln(logFile, 'halt at ' + inttostr(GameTicks) + ' ticks. TurnTimeLeft = ' + inttostr(TurnTimeLeft));
+    flush(logFile);
+    close(logFile);
 {$IFDEF USE_VIDEO_RECORDING}
     DoneCriticalSection(logMutex);
 {$ENDIF}