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