author | unc0rr |
Sun, 06 Jul 2008 17:45:16 +0000 | |
changeset 1054 | 80225c6af656 |
parent 988 | cdff3f6f9b38 |
child 1055 | 9af540b23409 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
883 | 3 |
* Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 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 |
|
4 | 8 |
* |
183 | 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. |
|
4 | 13 |
* |
183 | 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 |
|
4 | 17 |
*) |
18 |
||
19 |
unit uMisc; |
|
20 |
interface |
|
753 | 21 |
uses uConsts, SDLh, uFloat, GL; |
4 | 22 |
{$INCLUDE options.inc} |
1054 | 23 |
var |
24 |
isCursorVisible : boolean = false; |
|
25 |
isTerminated : boolean = false; |
|
26 |
isInLag : boolean = false; |
|
27 |
isPaused : boolean = false; |
|
28 |
isSoundEnabled : boolean = true; |
|
29 |
isSEBackup : boolean = true; |
|
30 |
isInMultiShoot : boolean = false; |
|
31 |
isSpeed : boolean = false; |
|
4 | 32 |
|
1054 | 33 |
GameState : TGameState = Low(TGameState); |
34 |
GameType : TGameType = gmtLocal; |
|
35 |
GameFlags : Longword = 0; |
|
36 |
TurnTimeLeft : Longword = 0; |
|
37 |
cHedgehogTurnTime: Longword = 45000; |
|
38 |
cMaxAIThinkTime : Longword = 9000; |
|
4 | 39 |
|
1054 | 40 |
cCloudsNumber : LongInt = 9; |
41 |
cScreenWidth : LongInt = 1024; |
|
42 |
cScreenHeight : LongInt = 768; |
|
43 |
cBits : LongInt = 16; |
|
44 |
cBitsStr : string[2] = '16'; |
|
45 |
cTagsMask : byte = 7; |
|
74 | 46 |
|
1054 | 47 |
cWaterLine : LongInt = 1024; |
48 |
cVisibleWater : LongInt = 128; |
|
49 |
cGearScrEdgesDist: LongInt = 240; |
|
50 |
cCursorEdgesDist : LongInt = 40; |
|
51 |
cTeamHealthWidth : LongInt = 128; |
|
52 |
cAltDamage : boolean = true; |
|
4 | 53 |
|
1054 | 54 |
GameTicks : LongWord = 0; |
55 |
||
56 |
cSkyColor : Longword = 0; |
|
57 |
cWhiteColor : Longword = $FFFFFFFF; |
|
58 |
cColorNearBlack : Longword = $FF000010; |
|
59 |
cExplosionBorderColor : LongWord = $808080; |
|
4 | 60 |
|
1054 | 61 |
cShowFPS : boolean = true; |
62 |
cCaseFactor : Longword = 5; {0..9} |
|
63 |
cLandAdditions: Longword = 4; |
|
64 |
cFullScreen : boolean = true; |
|
65 |
cLocaleFName : shortstring = 'en.txt'; |
|
66 |
cSeed : shortstring = ''; |
|
67 |
cInitVolume : LongInt = 128; |
|
68 |
cVolumeDelta : LongInt = 0; |
|
69 |
cTimerInterval : Longword = 5; |
|
70 |
cHasFocus : boolean = true; |
|
71 |
cInactDelay : Longword = 1500; |
|
4 | 72 |
|
1054 | 73 |
bBetweenTurns: boolean = false; |
74 |
cHealthDecrease: LongWord = 1; |
|
4 | 75 |
|
614 | 76 |
{$WARNINGS OFF} |
1054 | 77 |
cAirPlaneSpeed: hwFloat = (isNegative: false; QWordValue: 6012954214); // 1.4 |
78 |
cBombsSpeed : hwFloat = (isNegative: false; QWordValue: 429496729); |
|
614 | 79 |
{$WARNINGS ON} |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
80 |
|
4 | 81 |
var |
1054 | 82 |
cSendEmptyPacketTime : LongWord = 2000; |
83 |
cSendCursorPosTime : LongWord = 50; |
|
84 |
ShowCrosshair : boolean; |
|
85 |
cDrownSpeed, |
|
86 |
cMaxWindSpeed, |
|
87 |
cWindSpeed, |
|
88 |
cGravity: hwFloat; |
|
4 | 89 |
|
1054 | 90 |
flagMakeCapture: boolean = false; |
4 | 91 |
|
1054 | 92 |
InitStepsFlags: Longword = 0; |
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
53
diff
changeset
|
93 |
|
1054 | 94 |
RealTicks: Longword = 0; |
836 | 95 |
|
1054 | 96 |
AttackBar: LongInt = 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP |
4 | 97 |
|
371 | 98 |
function hwSign(r: hwFloat): LongInt; |
802
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
788
diff
changeset
|
99 |
function Min(a, b: LongInt): LongInt; |
371 | 100 |
function Max(a, b: LongInt): LongInt; |
351 | 101 |
procedure OutError(Msg: String; isFatalError: boolean); |
4 | 102 |
procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean); |
103 |
procedure SDLTry(Assert: boolean; isFatal: boolean); |
|
316 | 104 |
function IntToStr(n: LongInt): shortstring; |
351 | 105 |
function FloatToStr(n: hwFloat): shortstring; |
775 | 106 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
371 | 107 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
108 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
|
4 | 109 |
procedure AdjustColor(var Color: Longword); |
110 |
{$IFDEF DEBUGFILE} |
|
111 |
procedure AddFileLog(s: shortstring); |
|
24 | 112 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
4 | 113 |
{$ENDIF} |
208 | 114 |
procedure SetKB(n: Longword); |
115 |
procedure SendKB; |
|
351 | 116 |
procedure SetLittle(var r: hwFloat); |
306 | 117 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
753 | 118 |
function Str2PChar(const s: shortstring): PChar; |
755 | 119 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
759 | 120 |
procedure FreeTexture(tex: PTexture); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
916
diff
changeset
|
121 |
function toPowerOf2(i: Longword): Longword; |
949 | 122 |
function DecodeBase64(s: shortstring): shortstring; |
4 | 123 |
|
124 |
var CursorPoint: TPoint; |
|
125 |
TargetPoint: TPoint = (X: NoPointX; Y: 0); |
|
126 |
||
127 |
implementation |
|
771 | 128 |
uses uConsole, uStore, uIO, Math, uRandom, GLU; |
208 | 129 |
var KBnum: Longword = 0; |
4 | 130 |
{$IFDEF DEBUGFILE} |
131 |
var f: textfile; |
|
132 |
{$ENDIF} |
|
133 |
||
371 | 134 |
function hwSign(r: hwFloat): LongInt; |
4 | 135 |
begin |
351 | 136 |
if r.isNegative then hwSign:= -1 else hwSign:= 1 |
4 | 137 |
end; |
138 |
||
371 | 139 |
function Min(a, b: LongInt): LongInt; |
4 | 140 |
begin |
351 | 141 |
if a < b then Min:= a else Min:= b |
4 | 142 |
end; |
143 |
||
371 | 144 |
function Max(a, b: LongInt): LongInt; |
4 | 145 |
begin |
351 | 146 |
if a > b then Max:= a else Max:= b |
4 | 147 |
end; |
148 |
||
351 | 149 |
procedure OutError(Msg: String; isFatalError: boolean); |
4 | 150 |
begin |
151 |
{$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF} |
|
53 | 152 |
WriteLnToConsole(Msg); |
4 | 153 |
if isFatalError then |
154 |
begin |
|
53 | 155 |
SendIPC('E' + GetLastConsoleLine); |
4 | 156 |
SDL_Quit; |
157 |
halt(1) |
|
53 | 158 |
end |
4 | 159 |
end; |
160 |
||
161 |
procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean); |
|
162 |
begin |
|
70 | 163 |
if not Assert then OutError(Msg, isFatal) |
4 | 164 |
end; |
165 |
||
166 |
procedure SDLTry(Assert: boolean; isFatal: boolean); |
|
167 |
begin |
|
168 |
if not Assert then OutError(SDL_GetError, isFatal) |
|
169 |
end; |
|
170 |
||
188 | 171 |
procedure AdjustColor(var Color: Longword); |
4 | 172 |
begin |
173 |
Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF) |
|
174 |
end; |
|
175 |
||
316 | 176 |
function IntToStr(n: LongInt): shortstring; |
4 | 177 |
begin |
351 | 178 |
str(n, IntToStr) |
4 | 179 |
end; |
180 |
||
351 | 181 |
function FloatToStr(n: hwFloat): shortstring; |
4 | 182 |
begin |
351 | 183 |
FloatToStr:= cstr(n) |
4 | 184 |
end; |
185 |
||
775 | 186 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
187 |
var dY, dX: Extended; |
|
188 |
begin |
|
189 |
dY:= _dY.QWordValue / $100000000; |
|
190 |
if _dY.isNegative then dY:= - dY; |
|
191 |
dX:= _dX.QWordValue / $100000000; |
|
192 |
if _dX.isNegative then dX:= - dX; |
|
193 |
DxDy2Angle:= arctan2(dY, dX) * 180 / pi |
|
194 |
end; |
|
195 |
||
371 | 196 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
100 | 197 |
const _16divPI: Extended = 16/pi; |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
198 |
var dY, dX: Extended; |
100 | 199 |
begin |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
200 |
dY:= _dY.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
201 |
if _dY.isNegative then dY:= - dY; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
202 |
dX:= _dX.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
203 |
if _dX.isNegative then dX:= - dX; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
204 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f |
4 | 205 |
end; |
206 |
||
371 | 207 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
100 | 208 |
const MaxAngleDivPI: Extended = cMaxAngle/pi; |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
209 |
var dY, dX: Extended; |
100 | 210 |
begin |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
211 |
dY:= _dY.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
212 |
if _dY.isNegative then dY:= - dY; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
213 |
dX:= _dX.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
214 |
if _dX.isNegative then dX:= - dX; |
438 | 215 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) |
100 | 216 |
end; |
4 | 217 |
|
208 | 218 |
procedure SetKB(n: Longword); |
219 |
begin |
|
220 |
KBnum:= n |
|
221 |
end; |
|
222 |
||
223 |
procedure SendKB; |
|
224 |
var s: shortstring; |
|
225 |
begin |
|
226 |
if KBnum <> 0 then |
|
227 |
begin |
|
228 |
s:= 'K' + inttostr(KBnum); |
|
229 |
SendIPCRaw(@s, Length(s) + 1) |
|
230 |
end |
|
231 |
end; |
|
232 |
||
351 | 233 |
procedure SetLittle(var r: hwFloat); |
300 | 234 |
begin |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
543
diff
changeset
|
235 |
r:= SignAs(cLittle, r) |
300 | 236 |
end; |
237 |
||
337 | 238 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
869 | 239 |
const stc: array [TStatInfoType] of char = 'rDkK'; |
337 | 240 |
begin |
241 |
SendIPC('i' + stc[sit] + s) |
|
242 |
end; |
|
243 |
||
534 | 244 |
function Str2PChar(const s: shortstring): PChar; |
351 | 245 |
const CharArray: array[byte] of Char = ''; |
246 |
begin |
|
247 |
CharArray:= s; |
|
248 |
CharArray[Length(s)]:= #0; |
|
249 |
Str2PChar:= @CharArray |
|
250 |
end; |
|
251 |
||
771 | 252 |
function isPowerOf2(i: Longword): boolean; |
253 |
begin |
|
254 |
if i = 0 then exit(true); |
|
255 |
while (i and 1) = 0 do i:= i shr 1; |
|
256 |
isPowerOf2:= (i = 1) |
|
257 |
end; |
|
258 |
||
259 |
function toPowerOf2(i: Longword): Longword; |
|
260 |
begin |
|
261 |
toPowerOf2:= 1; |
|
262 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
263 |
end; |
|
264 |
||
755 | 265 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
753 | 266 |
var mode: LongInt; |
771 | 267 |
tw, th: Longword; |
268 |
tmpp: pointer; |
|
753 | 269 |
begin |
755 | 270 |
new(Surface2Tex); |
271 |
Surface2Tex^.w:= surf^.w; |
|
272 |
Surface2Tex^.h:= surf^.h; |
|
273 |
||
753 | 274 |
if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else |
275 |
if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else |
|
276 |
begin |
|
869 | 277 |
TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true); |
755 | 278 |
Surface2Tex^.id:= 0; |
753 | 279 |
exit |
280 |
end; |
|
281 |
||
755 | 282 |
glGenTextures(1, @Surface2Tex^.id); |
753 | 283 |
|
755 | 284 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
753 | 285 |
|
771 | 286 |
if SDL_MustLock(surf) then |
287 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
288 |
||
289 |
if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then |
|
290 |
begin |
|
291 |
tw:= toPowerOf2(Surf^.w); |
|
292 |
th:= toPowerOf2(Surf^.h); |
|
293 |
||
294 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
295 |
||
296 |
gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, |
|
297 |
Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, |
|
298 |
tmpp); |
|
299 |
||
300 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp); |
|
301 |
||
302 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
303 |
end else |
|
304 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels); |
|
753 | 305 |
|
754 | 306 |
if SDL_MustLock(surf) then |
307 |
SDL_UnlockSurface(surf); |
|
308 |
||
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset
|
309 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset
|
310 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
753 | 311 |
end; |
312 |
||
759 | 313 |
procedure FreeTexture(tex: PTexture); |
314 |
begin |
|
315 |
glDeleteTextures(1, @tex^.id); |
|
316 |
dispose(tex) |
|
317 |
end; |
|
337 | 318 |
|
949 | 319 |
function DecodeBase64(s: shortstring): shortstring; |
320 |
const table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
|
321 |
var i, t, c: Longword; |
|
322 |
begin |
|
323 |
c:= 0; |
|
324 |
for i:= 1 to Length(s) do |
|
325 |
begin |
|
326 |
t:= Pos(s[i], table); |
|
327 |
if s[i] = '=' then inc(c); |
|
328 |
if t > 0 then byte(s[i]):= t - 1 else byte(s[i]):= 0 |
|
329 |
end; |
|
330 |
||
331 |
i:= 1; |
|
332 |
t:= 1; |
|
333 |
while i <= length(s) do |
|
334 |
begin |
|
335 |
DecodeBase64[t ]:= char((byte(s[i ]) shl 2) or (byte(s[i + 1]) shr 4)); |
|
336 |
DecodeBase64[t + 1]:= char((byte(s[i + 1]) shl 4) or (byte(s[i + 2]) shr 2)); |
|
337 |
DecodeBase64[t + 2]:= char((byte(s[i + 2]) shl 6) or (byte(s[i + 3]) )); |
|
338 |
inc(t, 3); |
|
339 |
inc(i, 4) |
|
340 |
end; |
|
341 |
||
342 |
if c < 3 then t:= t - c; |
|
343 |
||
344 |
byte(DecodeBase64[0]):= t - 1 |
|
345 |
end; |
|
346 |
||
347 |
{$IFDEF DEBUGFILE} |
|
348 |
procedure AddFileLog(s: shortstring); |
|
349 |
begin |
|
350 |
writeln(f, GameTicks: 6, ': ', s); |
|
351 |
flush(f) |
|
352 |
end; |
|
353 |
||
354 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
|
355 |
begin |
|
356 |
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
|
357 |
end; |
|
358 |
||
371 | 359 |
var i: LongInt; |
488 | 360 |
{$ENDIF} |
306 | 361 |
|
4 | 362 |
initialization |
351 | 363 |
cDrownSpeed.QWordValue:= 257698038;// 0.06 |
488 | 364 |
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 |
365 |
cWindSpeed.QWordValue:= 429496;// 0.0001 |
|
351 | 366 |
cGravity:= cMaxWindSpeed; |
367 |
||
488 | 368 |
{$IFDEF DEBUGFILE} |
337 | 369 |
{$I-} |
497 | 370 |
if ParamCount > 0 then |
371 |
for i:= 0 to 7 do |
|
337 | 372 |
begin |
497 | 373 |
Assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
337 | 374 |
rewrite(f); |
375 |
if IOResult = 0 then break |
|
376 |
end; |
|
377 |
{$I+} |
|
17 | 378 |
|
4 | 379 |
finalization |
916 | 380 |
//uRandom.DumpBuffer; |
4 | 381 |
writeln(f, '-= halt at ',GameTicks,' ticks =-'); |
382 |
Flush(f); |
|
351 | 383 |
close(f) |
4 | 384 |
{$ENDIF} |
385 |
||
386 |
end. |