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