4374
|
1 |
{$INCLUDE "options.inc"}
|
|
2 |
unit uUtils;
|
|
3 |
|
|
4 |
interface
|
|
5 |
uses uTypes, uFloat, GLunit;
|
|
6 |
|
|
7 |
procedure SplitBySpace(var a, b: shortstring);
|
|
8 |
procedure SplitByChar(var a, b: ansistring; c: char);
|
|
9 |
|
|
10 |
function EnumToStr(const en : TGearType) : shortstring; overload;
|
|
11 |
function EnumToStr(const en : TSound) : shortstring; overload;
|
|
12 |
function EnumToStr(const en : TAmmoType) : shortstring; overload;
|
|
13 |
function EnumToStr(const en : THogEffect) : shortstring; overload;
|
|
14 |
|
|
15 |
function Min(a, b: LongInt): LongInt; inline;
|
|
16 |
function Max(a, b: LongInt): LongInt; inline;
|
|
17 |
|
|
18 |
function IntToStr(n: LongInt): shortstring;
|
|
19 |
function FloatToStr(n: hwFloat): shortstring;
|
|
20 |
|
|
21 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
|
|
22 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
|
|
23 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
|
|
24 |
|
|
25 |
procedure SetLittle(var r: hwFloat);
|
|
26 |
|
|
27 |
function Str2PChar(const s: shortstring): PChar;
|
|
28 |
function DecodeBase64(s: shortstring): shortstring;
|
|
29 |
|
|
30 |
function isPowerOf2(i: Longword): boolean;
|
|
31 |
function toPowerOf2(i: Longword): Longword; inline;
|
|
32 |
|
|
33 |
function endian(independent: LongWord): LongWord; inline;
|
|
34 |
|
4380
|
35 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont;
|
|
36 |
|
4374
|
37 |
{$IFDEF DEBUGFILE}
|
|
38 |
procedure AddFileLog(s: shortstring);
|
|
39 |
{$ENDIF}
|
|
40 |
|
|
41 |
procedure initModule;
|
|
42 |
procedure freeModule;
|
|
43 |
|
|
44 |
implementation
|
|
45 |
uses typinfo, Math, uConsts, uVariables, SysUtils;
|
|
46 |
|
|
47 |
var
|
|
48 |
{$IFDEF DEBUGFILE}
|
|
49 |
f: textfile;
|
|
50 |
{$ENDIF}
|
|
51 |
|
|
52 |
// should this include "strtolower()" for the split string?
|
|
53 |
procedure SplitBySpace(var a, b: shortstring);
|
|
54 |
var i, t: LongInt;
|
|
55 |
begin
|
|
56 |
i:= Pos(' ', a);
|
|
57 |
if i > 0 then
|
|
58 |
begin
|
|
59 |
for t:= 1 to Pred(i) do
|
|
60 |
if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
|
|
61 |
b:= copy(a, i + 1, Length(a) - i);
|
|
62 |
byte(a[0]):= Pred(i)
|
|
63 |
end else b:= '';
|
|
64 |
end;
|
|
65 |
|
|
66 |
procedure SplitByChar(var a, b: ansistring; c: char);
|
|
67 |
var i: LongInt;
|
|
68 |
begin
|
|
69 |
i:= Pos(c, a);
|
|
70 |
if i > 0 then
|
|
71 |
begin
|
|
72 |
b:= copy(a, i + 1, Length(a) - i);
|
|
73 |
setlength(a, Pred(i));
|
|
74 |
end else b:= '';
|
|
75 |
end;
|
|
76 |
|
|
77 |
function EnumToStr(const en : TGearType) : shortstring; overload;
|
|
78 |
begin
|
|
79 |
EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en))
|
|
80 |
end;
|
|
81 |
|
|
82 |
function EnumToStr(const en : TSound) : shortstring; overload;
|
|
83 |
begin
|
|
84 |
EnumToStr:= GetEnumName(TypeInfo(TSound), ord(en))
|
|
85 |
end;
|
|
86 |
|
|
87 |
function EnumToStr(const en : TAmmoType) : shortstring; overload;
|
|
88 |
begin
|
|
89 |
EnumToStr:= GetEnumName(TypeInfo(TAmmoType), ord(en))
|
|
90 |
end;
|
|
91 |
|
|
92 |
function EnumToStr(const en: THogEffect) : shortstring; overload;
|
|
93 |
begin
|
|
94 |
EnumToStr := GetEnumName(TypeInfo(THogEffect), ord(en))
|
|
95 |
end;
|
|
96 |
|
|
97 |
|
|
98 |
function Min(a, b: LongInt): LongInt;
|
|
99 |
begin
|
|
100 |
if a < b then Min:= a else Min:= b
|
|
101 |
end;
|
|
102 |
|
|
103 |
function Max(a, b: LongInt): LongInt;
|
|
104 |
begin
|
|
105 |
if a > b then Max:= a else Max:= b
|
|
106 |
end;
|
|
107 |
|
|
108 |
|
|
109 |
function IntToStr(n: LongInt): shortstring;
|
|
110 |
begin
|
|
111 |
str(n, IntToStr)
|
|
112 |
end;
|
|
113 |
|
|
114 |
function FloatToStr(n: hwFloat): shortstring;
|
|
115 |
begin
|
|
116 |
FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue))
|
|
117 |
end;
|
|
118 |
|
|
119 |
|
|
120 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat;
|
|
121 |
var dY, dX: Extended;
|
|
122 |
begin
|
|
123 |
dY:= _dY.QWordValue / $100000000;
|
|
124 |
if _dY.isNegative then dY:= - dY;
|
|
125 |
dX:= _dX.QWordValue / $100000000;
|
|
126 |
if _dX.isNegative then dX:= - dX;
|
|
127 |
DxDy2Angle:= arctan2(dY, dX) * 180 / pi
|
|
128 |
end;
|
|
129 |
|
|
130 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt;
|
|
131 |
const _16divPI: Extended = 16/pi;
|
|
132 |
var dY, dX: Extended;
|
|
133 |
begin
|
|
134 |
dY:= _dY.QWordValue / $100000000;
|
|
135 |
if _dY.isNegative then dY:= - dY;
|
|
136 |
dX:= _dX.QWordValue / $100000000;
|
|
137 |
if _dX.isNegative then dX:= - dX;
|
|
138 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f
|
|
139 |
end;
|
|
140 |
|
|
141 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt;
|
|
142 |
const MaxAngleDivPI: Extended = cMaxAngle/pi;
|
|
143 |
var dY, dX: Extended;
|
|
144 |
begin
|
|
145 |
dY:= _dY.QWordValue / $100000000;
|
|
146 |
if _dY.isNegative then dY:= - dY;
|
|
147 |
dX:= _dX.QWordValue / $100000000;
|
|
148 |
if _dX.isNegative then dX:= - dX;
|
|
149 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI)
|
|
150 |
end;
|
|
151 |
|
|
152 |
|
|
153 |
procedure SetLittle(var r: hwFloat);
|
|
154 |
begin
|
|
155 |
r:= SignAs(cLittle, r)
|
|
156 |
end;
|
|
157 |
|
|
158 |
|
|
159 |
function isPowerOf2(i: Longword): boolean;
|
|
160 |
begin
|
|
161 |
if i = 0 then exit(true);
|
|
162 |
while not odd(i) do i:= i shr 1;
|
|
163 |
isPowerOf2:= (i = 1)
|
|
164 |
end;
|
|
165 |
|
|
166 |
function toPowerOf2(i: Longword): Longword;
|
|
167 |
begin
|
|
168 |
toPowerOf2:= 1;
|
|
169 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1
|
|
170 |
end;
|
|
171 |
|
|
172 |
|
|
173 |
function DecodeBase64(s: shortstring): shortstring;
|
|
174 |
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
175 |
var i, t, c: Longword;
|
|
176 |
begin
|
|
177 |
c:= 0;
|
|
178 |
for i:= 1 to Length(s) do
|
|
179 |
begin
|
|
180 |
t:= Pos(s[i], table);
|
|
181 |
if s[i] = '=' then inc(c);
|
|
182 |
if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0
|
|
183 |
end;
|
|
184 |
|
|
185 |
i:= 1;
|
|
186 |
t:= 1;
|
|
187 |
while i <= length(s) do
|
|
188 |
begin
|
|
189 |
DecodeBase64[t ]:= char((byte(s[i ]) shl 2) or (byte(s[i + 1]) shr 4));
|
|
190 |
DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2));
|
|
191 |
DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3]) ));
|
|
192 |
inc(t, 3);
|
|
193 |
inc(i, 4)
|
|
194 |
end;
|
|
195 |
|
|
196 |
if c < 3 then t:= t - c;
|
|
197 |
|
|
198 |
byte(DecodeBase64[0]):= t - 1
|
|
199 |
end;
|
|
200 |
|
|
201 |
|
|
202 |
function Str2PChar(const s: shortstring): PChar;
|
|
203 |
const CharArray: array[byte] of Char = '';
|
|
204 |
begin
|
|
205 |
CharArray:= s;
|
|
206 |
CharArray[Length(s)]:= #0;
|
|
207 |
Str2PChar:= @CharArray
|
|
208 |
end;
|
|
209 |
|
|
210 |
|
|
211 |
function endian(independent: LongWord): LongWord; inline;
|
|
212 |
begin
|
|
213 |
{$IFDEF ENDIAN_LITTLE}
|
|
214 |
endian:= independent;
|
|
215 |
{$ELSE}
|
|
216 |
endian:= (((independent and $FF000000) shr 24) or
|
|
217 |
((independent and $00FF0000) shr 8) or
|
|
218 |
((independent and $0000FF00) shl 8) or
|
|
219 |
((independent and $000000FF) shl 24))
|
|
220 |
{$ENDIF}
|
|
221 |
end;
|
|
222 |
|
|
223 |
|
|
224 |
{$IFDEF DEBUGFILE}
|
|
225 |
procedure AddFileLog(s: shortstring);
|
|
226 |
begin
|
|
227 |
writeln(f, GameTicks: 6, ': ', s);
|
|
228 |
flush(f)
|
|
229 |
end;
|
|
230 |
{$ENDIF}
|
|
231 |
|
|
232 |
|
4380
|
233 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont;
|
|
234 |
var l, i : LongInt;
|
|
235 |
u: WideChar;
|
|
236 |
tmpstr: array[0..256] of WideChar;
|
|
237 |
begin
|
|
238 |
|
|
239 |
{$IFNDEF IPHONEOS}
|
|
240 |
// remove chinese fonts for now
|
|
241 |
if (font >= CJKfnt16) or (length(s) = 0) then
|
|
242 |
{$ENDIF}
|
|
243 |
exit(font);
|
|
244 |
|
|
245 |
l:= Utf8ToUnicode(@tmpstr, Str2PChar(s), length(s))-1;
|
|
246 |
i:= 0;
|
|
247 |
while i < l do
|
|
248 |
begin
|
|
249 |
u:= tmpstr[i];
|
|
250 |
if (#$2E80 <= u) and (
|
|
251 |
(u <= #$2FDF ) or // CJK Radicals Supplement / Kangxi Radicals
|
|
252 |
((#$2FF0 <= u) and (u <= #$303F)) or // Ideographic Description Characters / CJK Radicals Supplement
|
|
253 |
((#$31C0 <= u) and (u <= #$31EF)) or // CJK Strokes
|
|
254 |
((#$3200 <= u) and (u <= #$4DBF)) or // Enclosed CJK Letters and Months / CJK Compatibility / CJK Unified Ideographs Extension A
|
|
255 |
((#$4E00 <= u) and (u <= #$9FFF)) or // CJK Unified Ideographs
|
|
256 |
((#$F900 <= u) and (u <= #$FAFF)) or // CJK Compatibility Ideographs
|
|
257 |
((#$FE30 <= u) and (u <= #$FE4F))) // CJK Compatibility Forms
|
|
258 |
then exit(THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) ));
|
|
259 |
inc(i)
|
|
260 |
end;
|
|
261 |
exit(font);
|
|
262 |
(* two more to check. pascal WideChar is only 16 bit though
|
|
263 |
((#$20000 <= u) and (u >= #$2A6DF)) or // CJK Unified Ideographs Extension B
|
|
264 |
((#$2F800 <= u) and (u >= #$2FA1F))) // CJK Compatibility Ideographs Supplement *)
|
|
265 |
end;
|
|
266 |
|
4374
|
267 |
procedure initModule;
|
|
268 |
{$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF}
|
|
269 |
begin
|
|
270 |
{$IFDEF DEBUGFILE}
|
|
271 |
{$I-}
|
|
272 |
{$IFDEF IPHONEOS}
|
|
273 |
Assign(f,'../Documents/hw-' + cLogfileBase + '.log');
|
|
274 |
Rewrite(f);
|
|
275 |
{$ELSE}
|
|
276 |
if (ParamStr(1) <> '') and (ParamStr(2) <> '') then
|
|
277 |
if (ParamCount <> 3) and (ParamCount <> cDefaultParamNum) then
|
|
278 |
begin
|
|
279 |
for i:= 0 to 7 do
|
|
280 |
begin
|
|
281 |
assign(f, ExtractFileDir(ParamStr(2)) + '/' + cLogfileBase + inttostr(i) + '.log');
|
|
282 |
rewrite(f);
|
|
283 |
if IOResult = 0 then break;
|
|
284 |
end;
|
|
285 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
|
|
286 |
end
|
|
287 |
else
|
|
288 |
begin
|
|
289 |
for i:= 0 to 7 do
|
|
290 |
begin
|
|
291 |
assign(f, ParamStr(1) + '/Logs/' + cLogfileBase + inttostr(i) + '.log');
|
|
292 |
rewrite(f);
|
|
293 |
if IOResult = 0 then break;
|
|
294 |
end;
|
|
295 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr
|
|
296 |
end
|
|
297 |
else
|
|
298 |
f:= stderr;
|
|
299 |
{$ENDIF}
|
|
300 |
{$I+}
|
|
301 |
{$ENDIF}
|
|
302 |
|
|
303 |
end;
|
|
304 |
|
|
305 |
procedure freeModule;
|
|
306 |
begin
|
|
307 |
recordFileName:= '';
|
|
308 |
|
|
309 |
{$IFDEF DEBUGFILE}
|
|
310 |
writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft);
|
|
311 |
flush(f);
|
|
312 |
close(f);
|
|
313 |
{$ENDIF}
|
|
314 |
end;
|
|
315 |
|
|
316 |
end. |