author | sheepluva |
Tue, 01 Feb 2011 19:49:10 +0100 | |
changeset 4901 | d1e2d82d9ccc |
parent 4900 | 8ad0e23e6d63 |
child 4976 | 088d40d8aba2 |
permissions | -rw-r--r-- |
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; |
|
4453 | 11 |
function EnumToStr(const en : TVisualGearType) : shortstring; overload; |
4374 | 12 |
function EnumToStr(const en : TSound) : shortstring; overload; |
13 |
function EnumToStr(const en : TAmmoType) : shortstring; overload; |
|
14 |
function EnumToStr(const en : THogEffect) : shortstring; overload; |
|
15 |
||
16 |
function Min(a, b: LongInt): LongInt; inline; |
|
17 |
function Max(a, b: LongInt): LongInt; inline; |
|
18 |
||
19 |
function IntToStr(n: LongInt): shortstring; |
|
20 |
function FloatToStr(n: hwFloat): shortstring; |
|
21 |
||
22 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
|
23 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
|
24 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
25 |
||
26 |
procedure SetLittle(var r: hwFloat); |
|
27 |
||
28 |
function Str2PChar(const s: shortstring): PChar; |
|
29 |
function DecodeBase64(s: shortstring): shortstring; |
|
30 |
||
31 |
function isPowerOf2(i: Longword): boolean; |
|
32 |
function toPowerOf2(i: Longword): Longword; inline; |
|
33 |
||
34 |
function endian(independent: LongWord): LongWord; inline; |
|
35 |
||
4380 | 36 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont; |
37 |
||
4374 | 38 |
procedure AddFileLog(s: shortstring); |
39 |
||
4900 | 40 |
function CheckNoTeamOrHH: boolean; inline; |
4398
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4385
diff
changeset
|
41 |
|
4403 | 42 |
function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt; |
43 |
function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt; |
|
44 |
||
4374 | 45 |
procedure initModule; |
46 |
procedure freeModule; |
|
47 |
||
4385 | 48 |
|
4374 | 49 |
implementation |
50 |
uses typinfo, Math, uConsts, uVariables, SysUtils; |
|
51 |
||
52 |
{$IFDEF DEBUGFILE} |
|
4437
05192cdbce9b
un-break build (wrong merge in uConsole) and update project file with the new sources
koda
parents:
4403
diff
changeset
|
53 |
var f: textfile; |
4374 | 54 |
{$ENDIF} |
55 |
||
56 |
// should this include "strtolower()" for the split string? |
|
57 |
procedure SplitBySpace(var a, b: shortstring); |
|
58 |
var i, t: LongInt; |
|
59 |
begin |
|
60 |
i:= Pos(' ', a); |
|
61 |
if i > 0 then |
|
62 |
begin |
|
63 |
for t:= 1 to Pred(i) do |
|
64 |
if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32); |
|
65 |
b:= copy(a, i + 1, Length(a) - i); |
|
66 |
byte(a[0]):= Pred(i) |
|
67 |
end else b:= ''; |
|
68 |
end; |
|
69 |
||
70 |
procedure SplitByChar(var a, b: ansistring; c: char); |
|
71 |
var i: LongInt; |
|
72 |
begin |
|
73 |
i:= Pos(c, a); |
|
74 |
if i > 0 then |
|
75 |
begin |
|
76 |
b:= copy(a, i + 1, Length(a) - i); |
|
77 |
setlength(a, Pred(i)); |
|
78 |
end else b:= ''; |
|
79 |
end; |
|
80 |
||
81 |
function EnumToStr(const en : TGearType) : shortstring; overload; |
|
82 |
begin |
|
83 |
EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en)) |
|
84 |
end; |
|
4453 | 85 |
function EnumToStr(const en : TVisualGearType) : shortstring; overload; |
86 |
begin |
|
87 |
EnumToStr:= GetEnumName(TypeInfo(TVisualGearType), ord(en)) |
|
88 |
end; |
|
4374 | 89 |
|
90 |
function EnumToStr(const en : TSound) : shortstring; overload; |
|
91 |
begin |
|
92 |
EnumToStr:= GetEnumName(TypeInfo(TSound), ord(en)) |
|
93 |
end; |
|
94 |
||
95 |
function EnumToStr(const en : TAmmoType) : shortstring; overload; |
|
96 |
begin |
|
97 |
EnumToStr:= GetEnumName(TypeInfo(TAmmoType), ord(en)) |
|
98 |
end; |
|
99 |
||
100 |
function EnumToStr(const en: THogEffect) : shortstring; overload; |
|
101 |
begin |
|
102 |
EnumToStr := GetEnumName(TypeInfo(THogEffect), ord(en)) |
|
103 |
end; |
|
104 |
||
105 |
||
106 |
function Min(a, b: LongInt): LongInt; |
|
107 |
begin |
|
108 |
if a < b then Min:= a else Min:= b |
|
109 |
end; |
|
110 |
||
111 |
function Max(a, b: LongInt): LongInt; |
|
112 |
begin |
|
113 |
if a > b then Max:= a else Max:= b |
|
114 |
end; |
|
115 |
||
116 |
||
117 |
function IntToStr(n: LongInt): shortstring; |
|
118 |
begin |
|
119 |
str(n, IntToStr) |
|
120 |
end; |
|
121 |
||
122 |
function FloatToStr(n: hwFloat): shortstring; |
|
123 |
begin |
|
124 |
FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue)) |
|
125 |
end; |
|
126 |
||
127 |
||
128 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
|
129 |
var dY, dX: Extended; |
|
130 |
begin |
|
131 |
dY:= _dY.QWordValue / $100000000; |
|
132 |
if _dY.isNegative then dY:= - dY; |
|
133 |
dX:= _dX.QWordValue / $100000000; |
|
134 |
if _dX.isNegative then dX:= - dX; |
|
135 |
DxDy2Angle:= arctan2(dY, dX) * 180 / pi |
|
136 |
end; |
|
137 |
||
138 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
|
139 |
const _16divPI: Extended = 16/pi; |
|
140 |
var dY, dX: Extended; |
|
141 |
begin |
|
142 |
dY:= _dY.QWordValue / $100000000; |
|
143 |
if _dY.isNegative then dY:= - dY; |
|
144 |
dX:= _dX.QWordValue / $100000000; |
|
145 |
if _dX.isNegative then dX:= - dX; |
|
146 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f |
|
147 |
end; |
|
148 |
||
149 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
150 |
const MaxAngleDivPI: Extended = cMaxAngle/pi; |
|
151 |
var dY, dX: Extended; |
|
152 |
begin |
|
153 |
dY:= _dY.QWordValue / $100000000; |
|
154 |
if _dY.isNegative then dY:= - dY; |
|
155 |
dX:= _dX.QWordValue / $100000000; |
|
156 |
if _dX.isNegative then dX:= - dX; |
|
157 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) |
|
158 |
end; |
|
159 |
||
160 |
||
161 |
procedure SetLittle(var r: hwFloat); |
|
162 |
begin |
|
163 |
r:= SignAs(cLittle, r) |
|
164 |
end; |
|
165 |
||
166 |
||
167 |
function isPowerOf2(i: Longword): boolean; |
|
168 |
begin |
|
169 |
if i = 0 then exit(true); |
|
170 |
while not odd(i) do i:= i shr 1; |
|
171 |
isPowerOf2:= (i = 1) |
|
172 |
end; |
|
173 |
||
174 |
function toPowerOf2(i: Longword): Longword; |
|
175 |
begin |
|
176 |
toPowerOf2:= 1; |
|
177 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
178 |
end; |
|
179 |
||
180 |
||
181 |
function DecodeBase64(s: shortstring): shortstring; |
|
182 |
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
|
183 |
var i, t, c: Longword; |
|
184 |
begin |
|
185 |
c:= 0; |
|
186 |
for i:= 1 to Length(s) do |
|
187 |
begin |
|
188 |
t:= Pos(s[i], table); |
|
189 |
if s[i] = '=' then inc(c); |
|
190 |
if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0 |
|
191 |
end; |
|
192 |
||
193 |
i:= 1; |
|
194 |
t:= 1; |
|
195 |
while i <= length(s) do |
|
196 |
begin |
|
197 |
DecodeBase64[t ]:= char((byte(s[i ]) shl 2) or (byte(s[i + 1]) shr 4)); |
|
198 |
DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2)); |
|
199 |
DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3]) )); |
|
200 |
inc(t, 3); |
|
201 |
inc(i, 4) |
|
202 |
end; |
|
203 |
||
204 |
if c < 3 then t:= t - c; |
|
205 |
||
206 |
byte(DecodeBase64[0]):= t - 1 |
|
207 |
end; |
|
208 |
||
209 |
||
210 |
function Str2PChar(const s: shortstring): PChar; |
|
211 |
const CharArray: array[byte] of Char = ''; |
|
212 |
begin |
|
213 |
CharArray:= s; |
|
214 |
CharArray[Length(s)]:= #0; |
|
215 |
Str2PChar:= @CharArray |
|
216 |
end; |
|
217 |
||
218 |
||
219 |
function endian(independent: LongWord): LongWord; inline; |
|
220 |
begin |
|
221 |
{$IFDEF ENDIAN_LITTLE} |
|
222 |
endian:= independent; |
|
223 |
{$ELSE} |
|
224 |
endian:= (((independent and $FF000000) shr 24) or |
|
225 |
((independent and $00FF0000) shr 8) or |
|
226 |
((independent and $0000FF00) shl 8) or |
|
227 |
((independent and $000000FF) shl 24)) |
|
228 |
{$ENDIF} |
|
229 |
end; |
|
230 |
||
231 |
||
232 |
procedure AddFileLog(s: shortstring); |
|
233 |
begin |
|
4900 | 234 |
s:= s; |
235 |
{$IFDEF DEBUGFILE} |
|
4374 | 236 |
writeln(f, GameTicks: 6, ': ', s); |
237 |
flush(f) |
|
4900 | 238 |
{$ENDIF} |
4374 | 239 |
end; |
240 |
||
241 |
||
4380 | 242 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont; |
243 |
var l, i : LongInt; |
|
244 |
u: WideChar; |
|
245 |
tmpstr: array[0..256] of WideChar; |
|
246 |
begin |
|
247 |
||
248 |
{$IFNDEF IPHONEOS} |
|
249 |
// remove chinese fonts for now |
|
250 |
if (font >= CJKfnt16) or (length(s) = 0) then |
|
251 |
{$ENDIF} |
|
252 |
exit(font); |
|
253 |
||
254 |
l:= Utf8ToUnicode(@tmpstr, Str2PChar(s), length(s))-1; |
|
255 |
i:= 0; |
|
4737 | 256 |
|
4380 | 257 |
while i < l do |
258 |
begin |
|
259 |
u:= tmpstr[i]; |
|
4737 | 260 |
if (#$1100 <= u) and ( |
261 |
(u <= #$11FF ) or // Hangul Jamo |
|
262 |
((#$2E80 <= u) and (u <= #$2FDF)) or // CJK Radicals Supplement / Kangxi Radicals |
|
4380 | 263 |
((#$2FF0 <= u) and (u <= #$303F)) or // Ideographic Description Characters / CJK Radicals Supplement |
4737 | 264 |
((#$3130 <= u) and (u <= #$318F)) or // Hangul Compatibility Jamo |
4380 | 265 |
((#$31C0 <= u) and (u <= #$31EF)) or // CJK Strokes |
266 |
((#$3200 <= u) and (u <= #$4DBF)) or // Enclosed CJK Letters and Months / CJK Compatibility / CJK Unified Ideographs Extension A |
|
267 |
((#$4E00 <= u) and (u <= #$9FFF)) or // CJK Unified Ideographs |
|
4737 | 268 |
((#$AC00 <= u) and (u <= #$D7AF)) or // Hangul Syllables |
4380 | 269 |
((#$F900 <= u) and (u <= #$FAFF)) or // CJK Compatibility Ideographs |
270 |
((#$FE30 <= u) and (u <= #$FE4F))) // CJK Compatibility Forms |
|
271 |
then exit(THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) )); |
|
272 |
inc(i) |
|
273 |
end; |
|
274 |
exit(font); |
|
275 |
(* two more to check. pascal WideChar is only 16 bit though |
|
276 |
((#$20000 <= u) and (u >= #$2A6DF)) or // CJK Unified Ideographs Extension B |
|
277 |
((#$2F800 <= u) and (u >= #$2FA1F))) // CJK Compatibility Ideographs Supplement *) |
|
278 |
end; |
|
279 |
||
4385 | 280 |
|
281 |
function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt; |
|
282 |
begin |
|
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
283 |
GetLaunchX:= 0 |
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
284 |
(* |
4385 | 285 |
if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then |
286 |
GetLaunchX:= sign(dir) * (8 + hwRound(AngleSin(angle) * Ammoz[at].ejectX) + hwRound(AngleCos(angle) * Ammoz[at].ejectY)) |
|
287 |
else |
|
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
288 |
GetLaunchX:= 0 *) |
4385 | 289 |
end; |
290 |
||
291 |
function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt; |
|
292 |
begin |
|
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
293 |
GetLaunchY:= 0 |
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
294 |
(* |
4385 | 295 |
if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then |
296 |
GetLaunchY:= hwRound(AngleSin(angle) * Ammoz[at].ejectY) - hwRound(AngleCos(angle) * Ammoz[at].ejectX) - 2 |
|
297 |
else |
|
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4453
diff
changeset
|
298 |
GetLaunchY:= 0*) |
4385 | 299 |
end; |
300 |
||
4398
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4385
diff
changeset
|
301 |
function CheckNoTeamOrHH: boolean; |
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4385
diff
changeset
|
302 |
begin |
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4385
diff
changeset
|
303 |
CheckNoTeamOrHH:= (CurrentTeam = nil) or (CurrentHedgehog^.Gear = nil); |
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4385
diff
changeset
|
304 |
end; |
4385 | 305 |
|
4374 | 306 |
procedure initModule; |
307 |
{$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF} |
|
308 |
begin |
|
309 |
{$IFDEF DEBUGFILE} |
|
310 |
{$I-} |
|
311 |
{$IFDEF IPHONEOS} |
|
312 |
Assign(f,'../Documents/hw-' + cLogfileBase + '.log'); |
|
313 |
Rewrite(f); |
|
314 |
{$ELSE} |
|
315 |
if (ParamStr(1) <> '') and (ParamStr(2) <> '') then |
|
316 |
if (ParamCount <> 3) and (ParamCount <> cDefaultParamNum) then |
|
317 |
begin |
|
318 |
for i:= 0 to 7 do |
|
319 |
begin |
|
320 |
assign(f, ExtractFileDir(ParamStr(2)) + '/' + cLogfileBase + inttostr(i) + '.log'); |
|
321 |
rewrite(f); |
|
322 |
if IOResult = 0 then break; |
|
323 |
end; |
|
324 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
|
325 |
end |
|
326 |
else |
|
327 |
begin |
|
328 |
for i:= 0 to 7 do |
|
329 |
begin |
|
330 |
assign(f, ParamStr(1) + '/Logs/' + cLogfileBase + inttostr(i) + '.log'); |
|
331 |
rewrite(f); |
|
332 |
if IOResult = 0 then break; |
|
333 |
end; |
|
334 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
|
335 |
end |
|
336 |
else |
|
337 |
f:= stderr; |
|
338 |
{$ENDIF} |
|
339 |
{$I+} |
|
340 |
{$ENDIF} |
|
341 |
||
342 |
end; |
|
343 |
||
344 |
procedure freeModule; |
|
345 |
begin |
|
346 |
recordFileName:= ''; |
|
347 |
||
348 |
{$IFDEF DEBUGFILE} |
|
349 |
writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft); |
|
350 |
flush(f); |
|
351 |
close(f); |
|
352 |
{$ENDIF} |
|
353 |
end; |
|
354 |
||
4453 | 355 |
end. |