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