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