hedgewars/uUtils.pas
changeset 6580 6155187bf599
parent 6444 eddc1e9bcd81
child 6700 e04da46ee43c
equal deleted inserted replaced
6579:fc52f7c22c9b 6580:6155187bf599
    80 begin
    80 begin
    81 i:= Pos(' ', a);
    81 i:= Pos(' ', a);
    82 if i > 0 then
    82 if i > 0 then
    83     begin
    83     begin
    84     for t:= 1 to Pred(i) do
    84     for t:= 1 to Pred(i) do
    85         if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
    85         if (a[t] >= 'A')and(a[t] <= 'Z') then
       
    86             Inc(a[t], 32);
    86     b:= copy(a, i + 1, Length(a) - i);
    87     b:= copy(a, i + 1, Length(a) - i);
    87     byte(a[0]):= Pred(i)
    88     byte(a[0]):= Pred(i)
    88     end else b:= '';
    89     end
       
    90 else
       
    91     b:= '';
    89 end;
    92 end;
    90 
    93 
    91 procedure SplitByChar(var a, b: ansistring; c: char);
    94 procedure SplitByChar(var a, b: ansistring; c: char);
    92 var i: LongInt;
    95 var i: LongInt;
    93 begin
    96 begin
   129 end;
   132 end;
   130 
   133 
   131 
   134 
   132 function Min(a, b: LongInt): LongInt;
   135 function Min(a, b: LongInt): LongInt;
   133 begin
   136 begin
   134 if a < b then Min:= a else Min:= b
   137 if a < b then
       
   138     Min:= a
       
   139 else
       
   140     Min:= b
   135 end;
   141 end;
   136 
   142 
   137 function Max(a, b: LongInt): LongInt;
   143 function Max(a, b: LongInt): LongInt;
   138 begin
   144 begin
   139 if a > b then Max:= a else Max:= b
   145 if a > b then
       
   146     Max:= a
       
   147 else
       
   148     Max:= b
   140 end;
   149 end;
   141 
   150 
   142 
   151 
   143 function IntToStr(n: LongInt): shortstring;
   152 function IntToStr(n: LongInt): shortstring;
   144 begin
   153 begin
   153 
   162 
   154 function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
   163 function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
   155 var dY, dX: Extended;
   164 var dY, dX: Extended;
   156 begin
   165 begin
   157 dY:= _dY.QWordValue / $100000000;
   166 dY:= _dY.QWordValue / $100000000;
   158 if _dY.isNegative then dY:= - dY;
   167 if _dY.isNegative then
       
   168     dY:= - dY;
   159 dX:= _dX.QWordValue / $100000000;
   169 dX:= _dX.QWordValue / $100000000;
   160 if _dX.isNegative then dX:= - dX;
   170 if _dX.isNegative then
       
   171     dX:= - dX;
   161 DxDy2Angle:= arctan2(dY, dX) * 180 / pi
   172 DxDy2Angle:= arctan2(dY, dX) * 180 / pi
   162 end;
   173 end;
   163 
   174 
   164 function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
   175 function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
   165 const _16divPI: Extended = 16/pi;
   176 const _16divPI: Extended = 16/pi;
   166 var dY, dX: Extended;
   177 var dY, dX: Extended;
   167 begin
   178 begin
   168 dY:= _dY.QWordValue / $100000000;
   179 dY:= _dY.QWordValue / $100000000;
   169 if _dY.isNegative then dY:= - dY;
   180 if _dY.isNegative then
       
   181     dY:= - dY;
   170 dX:= _dX.QWordValue / $100000000;
   182 dX:= _dX.QWordValue / $100000000;
   171 if _dX.isNegative then dX:= - dX;
   183 if _dX.isNegative then
       
   184     dX:= - dX;
   172 DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f
   185 DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f
   173 end;
   186 end;
   174 
   187 
   175 function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
   188 function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
   176 const MaxAngleDivPI: Extended = cMaxAngle/pi;
   189 const MaxAngleDivPI: Extended = cMaxAngle/pi;
   177 var dY, dX: Extended;
   190 var dY, dX: Extended;
   178 begin
   191 begin
   179 dY:= _dY.QWordValue / $100000000;
   192 dY:= _dY.QWordValue / $100000000;
   180 if _dY.isNegative then dY:= - dY;
   193 if _dY.isNegative then
       
   194     dY:= - dY;
   181 dX:= _dX.QWordValue / $100000000;
   195 dX:= _dX.QWordValue / $100000000;
   182 if _dX.isNegative then dX:= - dX;
   196 if _dX.isNegative then
       
   197     dX:= - dX;
   183 DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI)
   198 DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI)
   184 end;
   199 end;
   185 
   200 
   186 function DxDy2AttackAngle(const _dY, _dX: extended): LongInt; inline;
   201 function DxDy2AttackAngle(const _dY, _dX: extended): LongInt; inline;
   187 begin
   202 begin
   213 begin
   228 begin
   214 c:= 0;
   229 c:= 0;
   215 for i:= 1 to Length(s) do
   230 for i:= 1 to Length(s) do
   216     begin
   231     begin
   217     t:= Pos(s[i], table);
   232     t:= Pos(s[i], table);
   218     if s[i] = '=' then inc(c);
   233     if s[i] = '=' then
   219     if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0
   234         inc(c);
       
   235     if t > 0 then
       
   236         byte(s[i]):= t - 1
       
   237     else
       
   238         byte(s[i]):= 0
   220     end;
   239     end;
   221 
   240 
   222 i:= 1;
   241 i:= 1;
   223 t:= 1;
   242 t:= 1;
   224 while i <= length(s) do
   243 while i <= length(s) do
   228     DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3])      ));
   247     DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3])      ));
   229     inc(t, 3);
   248     inc(t, 3);
   230     inc(i, 4)
   249     inc(i, 4)
   231     end;
   250     end;
   232 
   251 
   233 if c < 3 then t:= t - c;
   252 if c < 3 then
       
   253     t:= t - c;
   234 
   254 
   235 byte(DecodeBase64[0]):= t - 1
   255 byte(DecodeBase64[0]):= t - 1
   236 end;
   256 end;
   237 
   257 
   238 
   258 
   307 end;
   327 end;
   308 
   328 
   309 
   329 
   310 function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
   330 function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt;
   311 begin
   331 begin
   312     GetLaunchX:= 0
   332 GetLaunchX:= 0
   313 (*
   333 (*
   314     if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then
   334     if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then
   315         GetLaunchX:= sign(dir) * (8 + hwRound(AngleSin(angle) * Ammoz[at].ejectX) + hwRound(AngleCos(angle) * Ammoz[at].ejectY))
   335         GetLaunchX:= sign(dir) * (8 + hwRound(AngleSin(angle) * Ammoz[at].ejectX) + hwRound(AngleCos(angle) * Ammoz[at].ejectY))
   316     else
   336     else
   317         GetLaunchX:= 0 *)
   337         GetLaunchX:= 0 *)
   318 end;
   338 end;
   319 
   339 
   320 function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
   340 function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt;
   321 begin
   341 begin
   322     GetLaunchY:= 0
   342 GetLaunchY:= 0
   323 (*
   343 (*
   324     if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then
   344     if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then
   325         GetLaunchY:= hwRound(AngleSin(angle) * Ammoz[at].ejectY) - hwRound(AngleCos(angle) * Ammoz[at].ejectX) - 2
   345         GetLaunchY:= hwRound(AngleSin(angle) * Ammoz[at].ejectY) - hwRound(AngleCos(angle) * Ammoz[at].ejectX) - 2
   326     else
   346     else
   327         GetLaunchY:= 0*)
   347         GetLaunchY:= 0*)
   347             i:= 0;
   367             i:= 0;
   348             while(i < 7) do
   368             while(i < 7) do
   349             begin
   369             begin
   350                 assign(f, UserPathPrefix + '/Logs/' + cLogfileBase + inttostr(i) + '.log');
   370                 assign(f, UserPathPrefix + '/Logs/' + cLogfileBase + inttostr(i) + '.log');
   351                 rewrite(f);
   371                 rewrite(f);
   352                 if IOResult = 0 then break;
   372                 if IOResult = 0 then
       
   373                     break;
   353                 inc(i)
   374                 inc(i)
   354             end;
   375             end;
   355             if i = 7 then f:= stderr; // if everything fails, write to stderr
   376             if i = 7 then
       
   377                 f:= stderr; // if everything fails, write to stderr
   356         end
   378         end
   357     else
   379     else
   358         f:= stderr;
   380         f:= stderr;
   359 {$ENDIF}
   381 {$ENDIF}
   360 {$I+}
   382 {$I+}
   362 
   384 
   363 end;
   385 end;
   364 
   386 
   365 procedure freeModule;
   387 procedure freeModule;
   366 begin
   388 begin
   367     recordFileName:= '';
   389 recordFileName:= '';
   368 
   390 
   369 {$IFDEF DEBUGFILE}
   391 {$IFDEF DEBUGFILE}
   370     writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   392     writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
   371     flush(f);
   393     flush(f);
   372     close(f);
   394     close(f);