author | unc0rr |
Sun, 03 Apr 2011 13:31:33 +0400 | |
changeset 5090 | 2922455e606e |
parent 4981 | 0c60ade27a0a |
child 5118 | 9cfdb9bc6140 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
4374 | 19 |
{$INCLUDE "options.inc"} |
4976 | 20 |
|
4374 | 21 |
unit uUtils; |
22 |
||
23 |
interface |
|
24 |
uses uTypes, uFloat, GLunit; |
|
25 |
||
26 |
procedure SplitBySpace(var a, b: shortstring); |
|
27 |
procedure SplitByChar(var a, b: ansistring; c: char); |
|
28 |
||
29 |
function EnumToStr(const en : TGearType) : shortstring; overload; |
|
4453 | 30 |
function EnumToStr(const en : TVisualGearType) : shortstring; overload; |
4374 | 31 |
function EnumToStr(const en : TSound) : shortstring; overload; |
32 |
function EnumToStr(const en : TAmmoType) : shortstring; overload; |
|
33 |
function EnumToStr(const en : THogEffect) : shortstring; overload; |
|
34 |
||
35 |
function Min(a, b: LongInt): LongInt; inline; |
|
36 |
function Max(a, b: LongInt): LongInt; inline; |
|
37 |
||
38 |
function IntToStr(n: LongInt): shortstring; |
|
39 |
function FloatToStr(n: hwFloat): shortstring; |
|
40 |
||
41 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
|
42 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
|
43 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
44 |
||
45 |
procedure SetLittle(var r: hwFloat); |
|
46 |
||
47 |
function Str2PChar(const s: shortstring): PChar; |
|
48 |
function DecodeBase64(s: shortstring): shortstring; |
|
49 |
||
50 |
function isPowerOf2(i: Longword): boolean; |
|
51 |
function toPowerOf2(i: Longword): Longword; inline; |
|
52 |
||
53 |
function endian(independent: LongWord): LongWord; inline; |
|
54 |
||
4380 | 55 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont; |
56 |
||
4374 | 57 |
procedure AddFileLog(s: shortstring); |
58 |
||
4900 | 59 |
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
|
60 |
|
4403 | 61 |
function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt; |
62 |
function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt; |
|
63 |
||
4374 | 64 |
procedure initModule; |
65 |
procedure freeModule; |
|
66 |
||
4385 | 67 |
|
4374 | 68 |
implementation |
69 |
uses typinfo, Math, uConsts, uVariables, SysUtils; |
|
70 |
||
71 |
{$IFDEF DEBUGFILE} |
|
4437
05192cdbce9b
un-break build (wrong merge in uConsole) and update project file with the new sources
koda
parents:
4403
diff
changeset
|
72 |
var f: textfile; |
4374 | 73 |
{$ENDIF} |
74 |
||
75 |
// should this include "strtolower()" for the split string? |
|
76 |
procedure SplitBySpace(var a, b: shortstring); |
|
77 |
var i, t: LongInt; |
|
78 |
begin |
|
79 |
i:= Pos(' ', a); |
|
80 |
if i > 0 then |
|
81 |
begin |
|
82 |
for t:= 1 to Pred(i) do |
|
83 |
if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32); |
|
84 |
b:= copy(a, i + 1, Length(a) - i); |
|
85 |
byte(a[0]):= Pred(i) |
|
86 |
end else b:= ''; |
|
87 |
end; |
|
88 |
||
89 |
procedure SplitByChar(var a, b: ansistring; c: char); |
|
90 |
var i: LongInt; |
|
91 |
begin |
|
92 |
i:= Pos(c, a); |
|
93 |
if i > 0 then |
|
94 |
begin |
|
95 |
b:= copy(a, i + 1, Length(a) - i); |
|
96 |
setlength(a, Pred(i)); |
|
97 |
end else b:= ''; |
|
98 |
end; |
|
99 |
||
100 |
function EnumToStr(const en : TGearType) : shortstring; overload; |
|
101 |
begin |
|
102 |
EnumToStr:= GetEnumName(TypeInfo(TGearType), ord(en)) |
|
103 |
end; |
|
4453 | 104 |
function EnumToStr(const en : TVisualGearType) : shortstring; overload; |
105 |
begin |
|
106 |
EnumToStr:= GetEnumName(TypeInfo(TVisualGearType), ord(en)) |
|
107 |
end; |
|
4374 | 108 |
|
109 |
function EnumToStr(const en : TSound) : shortstring; overload; |
|
110 |
begin |
|
111 |
EnumToStr:= GetEnumName(TypeInfo(TSound), ord(en)) |
|
112 |
end; |
|
113 |
||
114 |
function EnumToStr(const en : TAmmoType) : shortstring; overload; |
|
115 |
begin |
|
116 |
EnumToStr:= GetEnumName(TypeInfo(TAmmoType), ord(en)) |
|
117 |
end; |
|
118 |
||
119 |
function EnumToStr(const en: THogEffect) : shortstring; overload; |
|
120 |
begin |
|
121 |
EnumToStr := GetEnumName(TypeInfo(THogEffect), ord(en)) |
|
122 |
end; |
|
123 |
||
124 |
||
125 |
function Min(a, b: LongInt): LongInt; |
|
126 |
begin |
|
127 |
if a < b then Min:= a else Min:= b |
|
128 |
end; |
|
129 |
||
130 |
function Max(a, b: LongInt): LongInt; |
|
131 |
begin |
|
132 |
if a > b then Max:= a else Max:= b |
|
133 |
end; |
|
134 |
||
135 |
||
136 |
function IntToStr(n: LongInt): shortstring; |
|
137 |
begin |
|
138 |
str(n, IntToStr) |
|
139 |
end; |
|
140 |
||
141 |
function FloatToStr(n: hwFloat): shortstring; |
|
142 |
begin |
|
143 |
FloatToStr:= cstr(n) + '_' + inttostr(Lo(n.QWordValue)) |
|
144 |
end; |
|
145 |
||
146 |
||
147 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
|
148 |
var dY, dX: Extended; |
|
149 |
begin |
|
150 |
dY:= _dY.QWordValue / $100000000; |
|
151 |
if _dY.isNegative then dY:= - dY; |
|
152 |
dX:= _dX.QWordValue / $100000000; |
|
153 |
if _dX.isNegative then dX:= - dX; |
|
154 |
DxDy2Angle:= arctan2(dY, dX) * 180 / pi |
|
155 |
end; |
|
156 |
||
157 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
|
158 |
const _16divPI: Extended = 16/pi; |
|
159 |
var dY, dX: Extended; |
|
160 |
begin |
|
161 |
dY:= _dY.QWordValue / $100000000; |
|
162 |
if _dY.isNegative then dY:= - dY; |
|
163 |
dX:= _dX.QWordValue / $100000000; |
|
164 |
if _dX.isNegative then dX:= - dX; |
|
165 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f |
|
166 |
end; |
|
167 |
||
168 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
169 |
const MaxAngleDivPI: Extended = cMaxAngle/pi; |
|
170 |
var dY, dX: Extended; |
|
171 |
begin |
|
172 |
dY:= _dY.QWordValue / $100000000; |
|
173 |
if _dY.isNegative then dY:= - dY; |
|
174 |
dX:= _dX.QWordValue / $100000000; |
|
175 |
if _dX.isNegative then dX:= - dX; |
|
176 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) |
|
177 |
end; |
|
178 |
||
179 |
||
180 |
procedure SetLittle(var r: hwFloat); |
|
181 |
begin |
|
182 |
r:= SignAs(cLittle, r) |
|
183 |
end; |
|
184 |
||
185 |
||
186 |
function isPowerOf2(i: Longword): boolean; |
|
187 |
begin |
|
4981
0c60ade27a0a
Optimize check (not like it is called much, just ffs; not tested)
unc0rr
parents:
4976
diff
changeset
|
188 |
isPowerOf2:= (i and (i - 1)) = 0 |
4374 | 189 |
end; |
190 |
||
191 |
function toPowerOf2(i: Longword): Longword; |
|
192 |
begin |
|
193 |
toPowerOf2:= 1; |
|
194 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
195 |
end; |
|
196 |
||
197 |
||
198 |
function DecodeBase64(s: shortstring): shortstring; |
|
199 |
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
|
200 |
var i, t, c: Longword; |
|
201 |
begin |
|
202 |
c:= 0; |
|
203 |
for i:= 1 to Length(s) do |
|
204 |
begin |
|
205 |
t:= Pos(s[i], table); |
|
206 |
if s[i] = '=' then inc(c); |
|
207 |
if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0 |
|
208 |
end; |
|
209 |
||
210 |
i:= 1; |
|
211 |
t:= 1; |
|
212 |
while i <= length(s) do |
|
213 |
begin |
|
214 |
DecodeBase64[t ]:= char((byte(s[i ]) shl 2) or (byte(s[i + 1]) shr 4)); |
|
215 |
DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2)); |
|
216 |
DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3]) )); |
|
217 |
inc(t, 3); |
|
218 |
inc(i, 4) |
|
219 |
end; |
|
220 |
||
221 |
if c < 3 then t:= t - c; |
|
222 |
||
223 |
byte(DecodeBase64[0]):= t - 1 |
|
224 |
end; |
|
225 |
||
226 |
||
227 |
function Str2PChar(const s: shortstring): PChar; |
|
228 |
const CharArray: array[byte] of Char = ''; |
|
229 |
begin |
|
230 |
CharArray:= s; |
|
231 |
CharArray[Length(s)]:= #0; |
|
232 |
Str2PChar:= @CharArray |
|
233 |
end; |
|
234 |
||
235 |
||
236 |
function endian(independent: LongWord): LongWord; inline; |
|
237 |
begin |
|
238 |
{$IFDEF ENDIAN_LITTLE} |
|
239 |
endian:= independent; |
|
240 |
{$ELSE} |
|
241 |
endian:= (((independent and $FF000000) shr 24) or |
|
242 |
((independent and $00FF0000) shr 8) or |
|
243 |
((independent and $0000FF00) shl 8) or |
|
244 |
((independent and $000000FF) shl 24)) |
|
245 |
{$ENDIF} |
|
246 |
end; |
|
247 |
||
248 |
||
249 |
procedure AddFileLog(s: shortstring); |
|
250 |
begin |
|
4900 | 251 |
s:= s; |
252 |
{$IFDEF DEBUGFILE} |
|
4374 | 253 |
writeln(f, GameTicks: 6, ': ', s); |
254 |
flush(f) |
|
4900 | 255 |
{$ENDIF} |
4374 | 256 |
end; |
257 |
||
258 |
||
4380 | 259 |
function CheckCJKFont(s: ansistring; font: THWFont): THWFont; |
260 |
var l, i : LongInt; |
|
261 |
u: WideChar; |
|
262 |
tmpstr: array[0..256] of WideChar; |
|
263 |
begin |
|
264 |
||
265 |
{$IFNDEF IPHONEOS} |
|
266 |
// remove chinese fonts for now |
|
267 |
if (font >= CJKfnt16) or (length(s) = 0) then |
|
268 |
{$ENDIF} |
|
269 |
exit(font); |
|
270 |
||
271 |
l:= Utf8ToUnicode(@tmpstr, Str2PChar(s), length(s))-1; |
|
272 |
i:= 0; |
|
4737 | 273 |
|
4380 | 274 |
while i < l do |
275 |
begin |
|
276 |
u:= tmpstr[i]; |
|
4737 | 277 |
if (#$1100 <= u) and ( |
278 |
(u <= #$11FF ) or // Hangul Jamo |
|
279 |
((#$2E80 <= u) and (u <= #$2FDF)) or // CJK Radicals Supplement / Kangxi Radicals |
|
4380 | 280 |
((#$2FF0 <= u) and (u <= #$303F)) or // Ideographic Description Characters / CJK Radicals Supplement |
4737 | 281 |
((#$3130 <= u) and (u <= #$318F)) or // Hangul Compatibility Jamo |
4380 | 282 |
((#$31C0 <= u) and (u <= #$31EF)) or // CJK Strokes |
283 |
((#$3200 <= u) and (u <= #$4DBF)) or // Enclosed CJK Letters and Months / CJK Compatibility / CJK Unified Ideographs Extension A |
|
284 |
((#$4E00 <= u) and (u <= #$9FFF)) or // CJK Unified Ideographs |
|
4737 | 285 |
((#$AC00 <= u) and (u <= #$D7AF)) or // Hangul Syllables |
4380 | 286 |
((#$F900 <= u) and (u <= #$FAFF)) or // CJK Compatibility Ideographs |
287 |
((#$FE30 <= u) and (u <= #$FE4F))) // CJK Compatibility Forms |
|
288 |
then exit(THWFont( ord(font) + ((ord(High(THWFont))+1) div 2) )); |
|
289 |
inc(i) |
|
290 |
end; |
|
291 |
exit(font); |
|
292 |
(* two more to check. pascal WideChar is only 16 bit though |
|
293 |
((#$20000 <= u) and (u >= #$2A6DF)) or // CJK Unified Ideographs Extension B |
|
294 |
((#$2F800 <= u) and (u >= #$2FA1F))) // CJK Compatibility Ideographs Supplement *) |
|
295 |
end; |
|
296 |
||
4385 | 297 |
|
298 |
function GetLaunchX(at: TAmmoType; dir: LongInt; angle: LongInt): LongInt; |
|
299 |
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
|
300 |
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
|
301 |
(* |
4385 | 302 |
if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then |
303 |
GetLaunchX:= sign(dir) * (8 + hwRound(AngleSin(angle) * Ammoz[at].ejectX) + hwRound(AngleCos(angle) * Ammoz[at].ejectY)) |
|
304 |
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
|
305 |
GetLaunchX:= 0 *) |
4385 | 306 |
end; |
307 |
||
308 |
function GetLaunchY(at: TAmmoType; angle: LongInt): LongInt; |
|
309 |
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
|
310 |
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
|
311 |
(* |
4385 | 312 |
if (Ammoz[at].ejectX <> 0) or (Ammoz[at].ejectY <> 0) then |
313 |
GetLaunchY:= hwRound(AngleSin(angle) * Ammoz[at].ejectY) - hwRound(AngleCos(angle) * Ammoz[at].ejectX) - 2 |
|
314 |
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
|
315 |
GetLaunchY:= 0*) |
4385 | 316 |
end; |
317 |
||
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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
end; |
4385 | 322 |
|
4374 | 323 |
procedure initModule; |
324 |
{$IFDEF DEBUGFILE}{$IFNDEF IPHONEOS}var i: LongInt;{$ENDIF}{$ENDIF} |
|
325 |
begin |
|
326 |
{$IFDEF DEBUGFILE} |
|
327 |
{$I-} |
|
328 |
{$IFDEF IPHONEOS} |
|
329 |
Assign(f,'../Documents/hw-' + cLogfileBase + '.log'); |
|
330 |
Rewrite(f); |
|
331 |
{$ELSE} |
|
332 |
if (ParamStr(1) <> '') and (ParamStr(2) <> '') then |
|
333 |
if (ParamCount <> 3) and (ParamCount <> cDefaultParamNum) then |
|
334 |
begin |
|
335 |
for i:= 0 to 7 do |
|
336 |
begin |
|
337 |
assign(f, ExtractFileDir(ParamStr(2)) + '/' + cLogfileBase + inttostr(i) + '.log'); |
|
338 |
rewrite(f); |
|
339 |
if IOResult = 0 then break; |
|
340 |
end; |
|
341 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
|
342 |
end |
|
343 |
else |
|
344 |
begin |
|
345 |
for i:= 0 to 7 do |
|
346 |
begin |
|
347 |
assign(f, ParamStr(1) + '/Logs/' + cLogfileBase + inttostr(i) + '.log'); |
|
348 |
rewrite(f); |
|
349 |
if IOResult = 0 then break; |
|
350 |
end; |
|
351 |
if IOResult <> 0 then f:= stderr; // if everything fails, write to stderr |
|
352 |
end |
|
353 |
else |
|
354 |
f:= stderr; |
|
355 |
{$ENDIF} |
|
356 |
{$I+} |
|
357 |
{$ENDIF} |
|
358 |
||
359 |
end; |
|
360 |
||
361 |
procedure freeModule; |
|
362 |
begin |
|
363 |
recordFileName:= ''; |
|
364 |
||
365 |
{$IFDEF DEBUGFILE} |
|
366 |
writeln(f, 'halt at ', GameTicks, ' ticks. TurnTimeLeft = ', TurnTimeLeft); |
|
367 |
flush(f); |
|
368 |
close(f); |
|
369 |
{$ENDIF} |
|
370 |
end; |
|
371 |
||
4453 | 372 |
end. |