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