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