hedgewars/uMisc.pas
changeset 351 29bc9c36ad5f
parent 346 fc1e0a4f152c
child 370 c75410fe3133
equal deleted inserted replaced
350:c3ccec3834e8 351:29bc9c36ad5f
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 unit uMisc;
    19 unit uMisc;
    20 interface
    20 interface
    21 uses uConsts, SDLh;
    21 uses uConsts, SDLh, uFloat;
    22 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    23 var isCursorVisible : boolean = false;
    23 var isCursorVisible : boolean = false;
    24     isTerminated    : boolean = false;
    24     isTerminated    : boolean = false;
    25     isInLag         : boolean = false;
    25     isInLag         : boolean = false;
    26     isPaused        : boolean = false;
    26     isPaused        : boolean = false;
    35     cHedgehogTurnTime: Longword = 45000;
    35     cHedgehogTurnTime: Longword = 45000;
    36     cMaxAIThinkTime  : Longword = 5000;
    36     cMaxAIThinkTime  : Longword = 5000;
    37 
    37 
    38     cCloudsNumber    : integer = 9;
    38     cCloudsNumber    : integer = 9;
    39     cConsoleHeight   : integer = 320;
    39     cConsoleHeight   : integer = 320;
    40     cConsoleYAdd     : integer = 0; 
    40     cConsoleYAdd     : integer = 0;
    41     cScreenWidth     : integer = 1024;
    41     cScreenWidth     : integer = 1024;
    42     cScreenHeight    : integer = 768;
    42     cScreenHeight    : integer = 768;
    43     cBits            : integer = 16;
    43     cBits            : integer = 16;
    44     cBitsStr         : string[2] = '16';
    44     cBitsStr         : string[2] = '16';
    45 
    45 
    55     cWaterColor   : Longword = $005ACE;
    55     cWaterColor   : Longword = $005ACE;
    56     cWhiteColor   : Longword = $FFFFFF;
    56     cWhiteColor   : Longword = $FFFFFF;
    57     cConsoleSplitterColor : Longword = $FF0000;
    57     cConsoleSplitterColor : Longword = $FF0000;
    58     cColorNearBlack       : Longword = 16;
    58     cColorNearBlack       : Longword = 16;
    59     cExplosionBorderColor : LongWord = $808080;
    59     cExplosionBorderColor : LongWord = $808080;
    60 
       
    61     cDrownSpeed   : Double = 0.06;
       
    62     cMaxWindSpeed : Double = 0.0005;
       
    63     cWindSpeed    : Double = 0.0001;
       
    64     cGravity      : Double = 0.0005;
       
    65 
    60 
    66     cShowFPS      : boolean = true;
    61     cShowFPS      : boolean = true;
    67     cCaseFactor   : Longword = 3;  {1..10}
    62     cCaseFactor   : Longword = 3;  {1..10}
    68     cFullScreen   : boolean = true;
    63     cFullScreen   : boolean = true;
    69     cLocaleFName  : shortstring = 'en.txt';
    64     cLocaleFName  : shortstring = 'en.txt';
    70     cSeed         : shortstring = '';
    65     cSeed         : shortstring = '';
    71     cInitVolume   : integer = 128;
    66     cInitVolume   : integer = 128;
    72     cVolumeDelta  : integer = 0;
    67     cVolumeDelta  : integer = 0;
    73     cTimerInterval   : Longword = 5;
    68     cTimerInterval   : Longword = 5;
    74     cHasFocus     : boolean = true;   
    69     cHasFocus     : boolean = true;
    75 
    70 
    76 var
    71 var
    77     cSendEmptyPacketTime : LongWord = 2000;
    72     cSendEmptyPacketTime : LongWord = 2000;
    78     cSendCursorPosTime   : LongWord = 50;
    73     cSendCursorPosTime   : LongWord = 50;
    79     ShowCrosshair  : boolean;
    74     ShowCrosshair  : boolean;
       
    75     cDrownSpeed,
       
    76     cMaxWindSpeed,
       
    77     cWindSpeed,
       
    78     cGravity: hwFloat;
    80 
    79 
    81     flagMakeCapture: boolean = false;
    80     flagMakeCapture: boolean = false;
    82 
    81 
    83     InitStepsFlags: Longword = 0;
    82     InitStepsFlags: Longword = 0;
    84 
    83 
    85     AttackBar: integer = 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
    84     AttackBar: integer = 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP
    86 
    85 
    87 function hwSign(r: Double): integer;
    86 function hwSign(r: hwFloat): integer;
    88 function Min(a, b: integer): integer;
    87 function Min(a, b: integer): integer;
    89 function Max(a, b: integer): integer;
    88 function Max(a, b: integer): integer;
    90 procedure OutError(Msg: String; const isFatalError: boolean=false);
    89 function rndSign(num: hwFloat): hwFloat;
       
    90 procedure OutError(Msg: String; isFatalError: boolean);
    91 procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean);
    91 procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean);
    92 procedure SDLTry(Assert: boolean; isFatal: boolean);
    92 procedure SDLTry(Assert: boolean; isFatal: boolean);
    93 function IntToStr(n: LongInt): shortstring;
    93 function IntToStr(n: LongInt): shortstring;
    94 function FloatToStr(n: Double): shortstring;
    94 function FloatToStr(n: hwFloat): shortstring;
    95 function DxDy2Angle32(const _dY, _dX: Extended): integer;
    95 function DxDy2Angle32(const _dY, _dX: Extended): integer;
    96 function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
    96 function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
    97 procedure AdjustColor(var Color: Longword);
    97 procedure AdjustColor(var Color: Longword);
    98 {$IFDEF DEBUGFILE}
    98 {$IFDEF DEBUGFILE}
    99 procedure AddFileLog(s: shortstring);
    99 procedure AddFileLog(s: shortstring);
   100 function RectToStr(Rect: TSDL_Rect): shortstring;
   100 function RectToStr(Rect: TSDL_Rect): shortstring;
   101 {$ENDIF}
   101 {$ENDIF}
   102 procedure SetKB(n: Longword);
   102 procedure SetKB(n: Longword);
   103 procedure SendKB;
   103 procedure SendKB;
   104 procedure SetLittle(var r: Double);
   104 procedure SetLittle(var r: hwFloat);
   105 procedure SendStat(sit: TStatInfoType; s: shortstring);
   105 procedure SendStat(sit: TStatInfoType; s: shortstring);
       
   106 function Str2PChar(var s: shortstring): PChar;
   106 
   107 
   107 var CursorPoint: TPoint;
   108 var CursorPoint: TPoint;
   108     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   109     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   109 
   110 
   110 implementation
   111 implementation
   111 uses uConsole, uStore, uIO{$IFDEF FPC}, Math{$ENDIF};
   112 uses uConsole, uStore, uIO, Math, uRandom;
   112 var KBnum: Longword = 0;
   113 var KBnum: Longword = 0;
   113 {$IFDEF DEBUGFILE}
   114 {$IFDEF DEBUGFILE}
   114 var f: textfile;
   115 var f: textfile;
   115 {$ENDIF}
   116 {$ENDIF}
   116 
   117 
   117 function hwSign(r: Double): integer;
   118 function hwSign(r: hwFloat): integer;
   118 begin
   119 begin
   119 if r < 0 then Result:= -1 else Result:= 1
   120 if r.isNegative then hwSign:= -1 else hwSign:= 1
   120 end;
   121 end;
   121 
   122 
   122 function Min(a, b: integer): integer;
   123 function Min(a, b: integer): integer;
   123 begin
   124 begin
   124 if a < b then Result:= a else Result:= b
   125 if a < b then Min:= a else Min:= b
   125 end;
   126 end;
   126 
   127 
   127 function Max(a, b: integer): integer;
   128 function Max(a, b: integer): integer;
   128 begin
   129 begin
   129 if a > b then Result:= a else Result:= b
   130 if a > b then Max:= a else Max:= b
   130 end;
   131 end;
   131 
   132 
   132 procedure OutError(Msg: String; const isFatalError: boolean=false);
   133 procedure OutError(Msg: String; isFatalError: boolean);
   133 begin
   134 begin
   134 {$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF}
   135 {$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF}
   135 WriteLnToConsole(Msg);
   136 WriteLnToConsole(Msg);
   136 if isFatalError then
   137 if isFatalError then
   137    begin
   138    begin
   156 Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF)
   157 Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF)
   157 end;
   158 end;
   158 
   159 
   159 function IntToStr(n: LongInt): shortstring;
   160 function IntToStr(n: LongInt): shortstring;
   160 begin
   161 begin
   161 str(n, Result)
   162 str(n, IntToStr)
   162 end;
   163 end;
   163 
   164 
   164 function FloatToStr(n: Double): shortstring;
   165 function FloatToStr(n: hwFloat): shortstring;
   165 begin
   166 begin
   166 //str(n:5:5, Result)
   167 FloatToStr:= cstr(n)
   167 str(n, Result)
       
   168 end;
   168 end;
   169 
   169 
   170 {$IFNDEF FPC}
   170 {$IFNDEF FPC}
   171 function arctan2(const Y, X: Double): Double;
   171 function arctan2(const Y, X: hwFloat): hwFloat;
   172 asm
   172 asm
   173         fld     Y
   173         fld     Y
   174         fld     X
   174         fld     X
   175         fpatan
   175         fpatan
   176         fwait
   176         fwait
   178 {$ENDIF}
   178 {$ENDIF}
   179 
   179 
   180 function DxDy2Angle32(const _dY, _dX: Extended): integer;
   180 function DxDy2Angle32(const _dY, _dX: Extended): integer;
   181 const _16divPI: Extended = 16/pi;
   181 const _16divPI: Extended = 16/pi;
   182 begin
   182 begin
   183 Result:= trunc(arctan2(_dY, _dX) * _16divPI) and $1f
   183 DxDy2Angle32:= trunc(arctan2(_dY, _dX) * _16divPI) and $1f
   184 end;
   184 end;
   185 
   185 
   186 function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
   186 function DxDy2AttackAngle(const _dY, _dX: Extended): integer;
   187 const MaxAngleDivPI: Extended = cMaxAngle/pi;
   187 const MaxAngleDivPI: Extended = cMaxAngle/pi;
   188 begin
   188 begin
   189 Result:= trunc(arctan2(_dY, _dX) * MaxAngleDivPI) mod cMaxAngle
   189 DxDy2AttackAngle:= trunc(arctan2(_dY, _dX) * MaxAngleDivPI) mod cMaxAngle
   190 end;
   190 end;
   191 
   191 
   192 procedure SetKB(n: Longword);
   192 procedure SetKB(n: Longword);
   193 begin
   193 begin
   194 KBnum:= n
   194 KBnum:= n
   202    s:= 'K' + inttostr(KBnum);
   202    s:= 'K' + inttostr(KBnum);
   203    SendIPCRaw(@s, Length(s) + 1)
   203    SendIPCRaw(@s, Length(s) + 1)
   204    end
   204    end
   205 end;
   205 end;
   206 
   206 
   207 procedure SetLittle(var r: Double);
   207 procedure SetLittle(var r: hwFloat);
   208 begin
   208 begin
   209 if r >= 0 then r:= cLittle else r:= - cLittle 
   209 if not r.isNegative then r:= cLittle else r:= - cLittle
   210 end;
   210 end;
   211 
   211 
   212 procedure SendStat(sit: TStatInfoType; s: shortstring);
   212 procedure SendStat(sit: TStatInfoType; s: shortstring);
   213 const stc: array [TStatInfoType] of char = 'rDK';
   213 const stc: array [TStatInfoType] of char = 'rDK';
   214 begin
   214 begin
   215 SendIPC('i' + stc[sit] + s)
   215 SendIPC('i' + stc[sit] + s)
   216 end;
   216 end;
   217 
   217 
       
   218 function rndSign(num: hwFloat): hwFloat;
       
   219 begin
       
   220 num.isNegative:= getrandom(2) = 0;
       
   221 rndSign:= num
       
   222 end;
       
   223 
       
   224 function Str2PChar(var s: shortstring): PChar;
       
   225 const CharArray: array[byte] of Char = '';
       
   226 begin
       
   227 CharArray:= s;
       
   228 CharArray[Length(s)]:= #0;
       
   229 Str2PChar:= @CharArray
       
   230 end;
       
   231 
   218 {$IFDEF DEBUGFILE}
   232 {$IFDEF DEBUGFILE}
   219 procedure AddFileLog(s: shortstring);
   233 procedure AddFileLog(s: shortstring);
   220 begin
   234 begin
   221 writeln(f, GameTicks: 6, ': ', s);
   235 writeln(f, GameTicks: 6, ': ', s);
   222 flush(f)
   236 flush(f)
   223 end;
   237 end;
   224 
   238 
   225 function RectToStr(Rect: TSDL_Rect): shortstring;
   239 function RectToStr(Rect: TSDL_Rect): shortstring;
   226 begin
   240 begin
   227 Result:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')'
   241 RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')'
   228 end;
   242 end;
   229 
   243 
   230 
   244 
   231 var i: integer;
   245 var i: integer;
   232 
   246 
   233 initialization
   247 initialization
       
   248 cDrownSpeed.QWordValue:= 257698038;// 0.06
       
   249 cMaxWindSpeed.QWordValue:=   2147484;// 0.0005
       
   250 cWindSpeed.QWordValue:=    429496;// 0.0001
       
   251 cGravity:= cMaxWindSpeed;
       
   252 
       
   253 
   234 {$I-}
   254 {$I-}
   235 for i:= 0 to 7 do
   255 for i:= 0 to 7 do
   236     begin
   256     begin
   237     AssignFile(f, 'debug' + inttostr(i) + '.txt');
   257     Assign(f, 'debug' + inttostr(i) + '.txt');
   238     rewrite(f);
   258     rewrite(f);
   239     if IOResult = 0 then break
   259     if IOResult = 0 then break
   240     end;
   260     end;
   241 {$I+}
   261 {$I+}
   242 
   262 
   243 finalization
   263 finalization
   244 writeln(f, '-= halt at ',GameTicks,' ticks =-');
   264 writeln(f, '-= halt at ',GameTicks,' ticks =-');
   245 Flush(f);
   265 Flush(f);
   246 closefile(f)
   266 close(f)
   247 {$ENDIF}
   267 {$ENDIF}
   248 
   268 
   249 end.
   269 end.