hedgewars/uUtils.pas
author unC0Rr
Thu, 18 Nov 2010 11:32:47 +0300
changeset 4374 bcefeeabaa33
child 4380 b78638b36b89
permissions -rw-r--r--
Move some stuff from uMisc to uUtils
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4374
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     1
{$INCLUDE "options.inc"}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     2
unit uUtils;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     3
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     4
interface
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     5
uses uTypes, uFloat, GLunit;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     6
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     7
procedure SplitBySpace(var a, b: shortstring);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     8
procedure SplitByChar(var a, b: ansistring; c: char);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
     9
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    10
function  EnumToStr(const en : TGearType) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    11
function  EnumToStr(const en : TSound) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    12
function  EnumToStr(const en : TAmmoType) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    13
function  EnumToStr(const en : THogEffect) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    14
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    15
function  Min(a, b: LongInt): LongInt; inline;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    16
function  Max(a, b: LongInt): LongInt; inline;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    17
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    18
function  IntToStr(n: LongInt): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    19
function  FloatToStr(n: hwFloat): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    20
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    21
function  DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    22
function  DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    23
function  DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    24
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    25
procedure SetLittle(var r: hwFloat);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    26
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    27
function  Str2PChar(const s: shortstring): PChar;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    28
function  DecodeBase64(s: shortstring): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    29
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    30
function  isPowerOf2(i: Longword): boolean;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    31
function  toPowerOf2(i: Longword): Longword; inline;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    32
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    33
function  endian(independent: LongWord): LongWord; inline;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    34
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    35
{$IFDEF DEBUGFILE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    36
procedure AddFileLog(s: shortstring);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    37
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    38
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    39
procedure initModule;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    40
procedure freeModule;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    41
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    42
implementation
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    43
uses typinfo, Math, uConsts, uVariables, SysUtils;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    44
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    45
var
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    46
{$IFDEF DEBUGFILE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    47
    f: textfile;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    48
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    49
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    50
// should this include "strtolower()" for the split string?
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    51
procedure SplitBySpace(var a, b: shortstring);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    52
var i, t: LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    53
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    54
i:= Pos(' ', a);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    55
if i > 0 then
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    56
    begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    57
    for t:= 1 to Pred(i) do
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    58
        if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    59
    b:= copy(a, i + 1, Length(a) - i);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    60
    byte(a[0]):= Pred(i)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    61
    end else b:= '';
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    62
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    63
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    64
procedure SplitByChar(var a, b: ansistring; c: char);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    65
var i: LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    66
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    67
i:= Pos(c, a);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    68
if i > 0 then
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    69
    begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    70
    b:= copy(a, i + 1, Length(a) - i);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    71
    setlength(a, Pred(i));
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    72
    end else b:= '';
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    73
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    74
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    75
function EnumToStr(const en : TGearType) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    76
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    77
EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    78
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    79
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    80
function EnumToStr(const en : TSound) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    81
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    82
EnumToStr:= GetEnumName(TypeInfo(TSound), ord(en))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    83
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    84
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    85
function EnumToStr(const en : TAmmoType) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    86
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    87
EnumToStr:= GetEnumName(TypeInfo(TAmmoType), ord(en))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    88
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    89
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    90
function EnumToStr(const en: THogEffect) : shortstring; overload;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    91
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    92
    EnumToStr := GetEnumName(TypeInfo(THogEffect), ord(en))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    93
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    94
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    95
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    96
function Min(a, b: LongInt): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    97
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    98
if a < b then Min:= a else Min:= b
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
    99
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   100
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   101
function Max(a, b: LongInt): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   102
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   103
if a > b then Max:= a else Max:= b
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   104
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   105
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   106
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   107
function IntToStr(n: LongInt): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   108
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   109
str(n, IntToStr)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   110
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   111
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   112
function FloatToStr(n: hwFloat): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   113
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   114
FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   115
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   116
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   117
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   118
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   119
var dY, dX: Extended;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   120
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   121
dY:= _dY.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   122
if _dY.isNegative then dY:= - dY;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   123
dX:= _dX.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   124
if _dX.isNegative then dX:= - dX;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   125
DxDy2Angle:= arctan2(dY, dX) * 180 / pi
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   126
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   127
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   128
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   129
const _16divPI: Extended = 16/pi;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   130
var dY, dX: Extended;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   131
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   132
dY:= _dY.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   133
if _dY.isNegative then dY:= - dY;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   134
dX:= _dX.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   135
if _dX.isNegative then dX:= - dX;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   136
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   137
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   138
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   139
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   140
const MaxAngleDivPI: Extended = cMaxAngle/pi;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   141
var dY, dX: Extended;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   142
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   143
dY:= _dY.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   144
if _dY.isNegative then dY:= - dY;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   145
dX:= _dX.QWordValue / $100000000;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   146
if _dX.isNegative then dX:= - dX;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   147
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   148
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   149
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   150
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   151
procedure SetLittle(var r: hwFloat);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   152
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   153
r:= SignAs(cLittle, r)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   154
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   155
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   156
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   157
function isPowerOf2(i: Longword): boolean;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   158
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   159
if i = 0 then exit(true);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   160
while not odd(i) do i:= i shr 1;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   161
isPowerOf2:= (i = 1)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   162
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   163
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   164
function toPowerOf2(i: Longword): Longword;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   165
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   166
toPowerOf2:= 1;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   167
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   168
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   169
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   170
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   171
function DecodeBase64(s: shortstring): shortstring;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   172
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   173
var i, t, c: Longword;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   174
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   175
c:= 0;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   176
for i:= 1 to Length(s) do
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   177
    begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   178
    t:= Pos(s[i], table);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   179
    if s[i] = '=' then inc(c);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   180
    if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   181
    end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   182
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   183
i:= 1;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   184
t:= 1;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   185
while i <= length(s) do
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   186
    begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   187
    DecodeBase64[t    ]:= char((byte(s[i    ]) shl 2) or (byte(s[i + 1]) shr 4));
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   188
    DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2));
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   189
    DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3])      ));
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   190
    inc(t, 3);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   191
    inc(i, 4)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   192
    end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   193
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   194
if c < 3 then t:= t - c;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   195
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   196
byte(DecodeBase64[0]):= t - 1
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   197
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   198
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   199
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   200
function Str2PChar(const s: shortstring): PChar;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   201
const CharArray: array[byte] of Char = '';
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   202
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   203
CharArray:= s;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   204
CharArray[Length(s)]:= #0;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   205
Str2PChar:= @CharArray
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   206
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   207
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   208
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   209
function endian(independent: LongWord): LongWord; inline;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   210
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   211
{$IFDEF ENDIAN_LITTLE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   212
endian:= independent;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   213
{$ELSE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   214
endian:= (((independent and $FF000000) shr 24) or
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   215
          ((independent and $00FF0000) shr 8) or
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   216
          ((independent and $0000FF00) shl 8) or
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   217
          ((independent and $000000FF) shl 24))
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   218
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   219
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   220
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   221
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   222
{$IFDEF DEBUGFILE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   223
procedure AddFileLog(s: shortstring);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   224
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   225
writeln(f, GameTicks: 6, ': ', s);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   226
flush(f)
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   227
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   228
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   229
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   230
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   231
procedure initModule;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   232
{$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   233
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   234
{$IFDEF DEBUGFILE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   235
{$I-}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   236
{$IFDEF IPHONEOS}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   237
    Assign(f,'../Documents/hw-' + cLogfileBase + '.log');
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   238
    Rewrite(f);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   239
{$ELSE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   240
    if (ParamStr(1) <> '') and (ParamStr(2) <> '') then
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   241
        if (ParamCount <> 3) and (ParamCount <> cDefaultParamNum) then
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   242
        begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   243
            for i:= 0 to 7 do
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   244
            begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   245
                assign(f, ExtractFileDir(ParamStr(2)) + '/' + cLogfileBase + inttostr(i) + '.log');
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   246
                rewrite(f);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   247
                if IOResult = 0 then break;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   248
            end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   249
            if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   250
        end
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   251
        else
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   252
        begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   253
            for i:= 0 to 7 do
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   254
            begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   255
                assign(f, ParamStr(1) + '/Logs/' + cLogfileBase + inttostr(i) + '.log');
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   256
                rewrite(f);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   257
                if IOResult = 0 then break;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   258
            end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   259
            if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   260
        end
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   261
    else
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   262
        f:= stderr;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   263
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   264
{$I+}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   265
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   266
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   267
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   268
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   269
procedure freeModule;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   270
begin
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   271
    recordFileName:= '';
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   272
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   273
{$IFDEF DEBUGFILE}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   274
    writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   275
    flush(f);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   276
    close(f);
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   277
{$ENDIF}
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   278
end;
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   279
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents:
diff changeset
   280
end.