author | unc0rr |
Thu, 24 Apr 2008 14:36:38 +0000 | |
changeset 863 | dfa3aa51ecb5 |
parent 840 | 3c0028245ff3 |
child 865 | a4a5ec6c61d4 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2004-2007 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; |
|
614 | 64 |
cCaseFactor : Longword = 6; {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 |
function rndSign(num: hwFloat): hwFloat; |
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); |
4 | 121 |
|
122 |
var CursorPoint: TPoint; |
|
123 |
TargetPoint: TPoint = (X: NoPointX; Y: 0); |
|
124 |
||
125 |
implementation |
|
771 | 126 |
uses uConsole, uStore, uIO, Math, uRandom, GLU; |
208 | 127 |
var KBnum: Longword = 0; |
4 | 128 |
{$IFDEF DEBUGFILE} |
129 |
var f: textfile; |
|
130 |
{$ENDIF} |
|
131 |
||
371 | 132 |
function hwSign(r: hwFloat): LongInt; |
4 | 133 |
begin |
351 | 134 |
if r.isNegative then hwSign:= -1 else hwSign:= 1 |
4 | 135 |
end; |
136 |
||
371 | 137 |
function Min(a, b: LongInt): LongInt; |
4 | 138 |
begin |
351 | 139 |
if a < b then Min:= a else Min:= b |
4 | 140 |
end; |
141 |
||
371 | 142 |
function Max(a, b: LongInt): LongInt; |
4 | 143 |
begin |
351 | 144 |
if a > b then Max:= a else Max:= b |
4 | 145 |
end; |
146 |
||
351 | 147 |
procedure OutError(Msg: String; isFatalError: boolean); |
4 | 148 |
begin |
149 |
{$IFDEF DEBUGFILE}AddFileLog(Msg);{$ENDIF} |
|
53 | 150 |
WriteLnToConsole(Msg); |
4 | 151 |
if isFatalError then |
152 |
begin |
|
53 | 153 |
SendIPC('E' + GetLastConsoleLine); |
4 | 154 |
SDL_Quit; |
155 |
halt(1) |
|
53 | 156 |
end |
4 | 157 |
end; |
158 |
||
159 |
procedure TryDo(Assert: boolean; Msg: string; isFatal: boolean); |
|
160 |
begin |
|
70 | 161 |
if not Assert then OutError(Msg, isFatal) |
4 | 162 |
end; |
163 |
||
164 |
procedure SDLTry(Assert: boolean; isFatal: boolean); |
|
165 |
begin |
|
166 |
if not Assert then OutError(SDL_GetError, isFatal) |
|
167 |
end; |
|
168 |
||
188 | 169 |
procedure AdjustColor(var Color: Longword); |
4 | 170 |
begin |
171 |
Color:= SDL_MapRGB(PixelFormat, (Color shr 16) and $FF, (Color shr 8) and $FF, Color and $FF) |
|
172 |
end; |
|
173 |
||
316 | 174 |
function IntToStr(n: LongInt): shortstring; |
4 | 175 |
begin |
351 | 176 |
str(n, IntToStr) |
4 | 177 |
end; |
178 |
||
351 | 179 |
function FloatToStr(n: hwFloat): shortstring; |
4 | 180 |
begin |
351 | 181 |
FloatToStr:= cstr(n) |
4 | 182 |
end; |
183 |
||
775 | 184 |
function DxDy2Angle(const _dY, _dX: hwFloat): GLfloat; |
185 |
var dY, dX: Extended; |
|
186 |
begin |
|
187 |
dY:= _dY.QWordValue / $100000000; |
|
188 |
if _dY.isNegative then dY:= - dY; |
|
189 |
dX:= _dX.QWordValue / $100000000; |
|
190 |
if _dX.isNegative then dX:= - dX; |
|
191 |
DxDy2Angle:= arctan2(dY, dX) * 180 / pi |
|
192 |
end; |
|
193 |
||
371 | 194 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
100 | 195 |
const _16divPI: Extended = 16/pi; |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
196 |
var dY, dX: Extended; |
100 | 197 |
begin |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
198 |
dY:= _dY.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
199 |
if _dY.isNegative then dY:= - dY; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
200 |
dX:= _dX.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
201 |
if _dX.isNegative then dX:= - dX; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
202 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f |
4 | 203 |
end; |
204 |
||
371 | 205 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
100 | 206 |
const MaxAngleDivPI: Extended = cMaxAngle/pi; |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
207 |
var dY, dX: Extended; |
100 | 208 |
begin |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
209 |
dY:= _dY.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
210 |
if _dY.isNegative then dY:= - dY; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
211 |
dX:= _dX.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
212 |
if _dX.isNegative then dX:= - dX; |
438 | 213 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) |
100 | 214 |
end; |
4 | 215 |
|
208 | 216 |
procedure SetKB(n: Longword); |
217 |
begin |
|
218 |
KBnum:= n |
|
219 |
end; |
|
220 |
||
221 |
procedure SendKB; |
|
222 |
var s: shortstring; |
|
223 |
begin |
|
224 |
if KBnum <> 0 then |
|
225 |
begin |
|
226 |
s:= 'K' + inttostr(KBnum); |
|
227 |
SendIPCRaw(@s, Length(s) + 1) |
|
228 |
end |
|
229 |
end; |
|
230 |
||
351 | 231 |
procedure SetLittle(var r: hwFloat); |
300 | 232 |
begin |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
543
diff
changeset
|
233 |
r:= SignAs(cLittle, r) |
300 | 234 |
end; |
235 |
||
337 | 236 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
237 |
const stc: array [TStatInfoType] of char = 'rDK'; |
|
238 |
begin |
|
239 |
SendIPC('i' + stc[sit] + s) |
|
240 |
end; |
|
241 |
||
351 | 242 |
function rndSign(num: hwFloat): hwFloat; |
243 |
begin |
|
244 |
num.isNegative:= getrandom(2) = 0; |
|
245 |
rndSign:= num |
|
246 |
end; |
|
247 |
||
534 | 248 |
function Str2PChar(const s: shortstring): PChar; |
351 | 249 |
const CharArray: array[byte] of Char = ''; |
250 |
begin |
|
251 |
CharArray:= s; |
|
252 |
CharArray[Length(s)]:= #0; |
|
253 |
Str2PChar:= @CharArray |
|
254 |
end; |
|
255 |
||
4 | 256 |
{$IFDEF DEBUGFILE} |
257 |
procedure AddFileLog(s: shortstring); |
|
258 |
begin |
|
259 |
writeln(f, GameTicks: 6, ': ', s); |
|
260 |
flush(f) |
|
261 |
end; |
|
262 |
||
24 | 263 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
264 |
begin |
|
351 | 265 |
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
24 | 266 |
end; |
267 |
||
771 | 268 |
function isPowerOf2(i: Longword): boolean; |
269 |
begin |
|
270 |
if i = 0 then exit(true); |
|
271 |
while (i and 1) = 0 do i:= i shr 1; |
|
272 |
isPowerOf2:= (i = 1) |
|
273 |
end; |
|
274 |
||
275 |
function toPowerOf2(i: Longword): Longword; |
|
276 |
begin |
|
277 |
toPowerOf2:= 1; |
|
278 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
279 |
end; |
|
280 |
||
755 | 281 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
753 | 282 |
var mode: LongInt; |
771 | 283 |
tw, th: Longword; |
284 |
tmpp: pointer; |
|
753 | 285 |
begin |
755 | 286 |
new(Surface2Tex); |
287 |
Surface2Tex^.w:= surf^.w; |
|
288 |
Surface2Tex^.h:= surf^.h; |
|
289 |
||
753 | 290 |
if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else |
291 |
if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else |
|
292 |
begin |
|
785 | 293 |
TryDo(false, 'Surface2Tex: BytePerPixel not in [3, 4]', true); |
755 | 294 |
Surface2Tex^.id:= 0; |
753 | 295 |
exit |
296 |
end; |
|
297 |
||
755 | 298 |
glGenTextures(1, @Surface2Tex^.id); |
753 | 299 |
|
755 | 300 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
753 | 301 |
|
771 | 302 |
if SDL_MustLock(surf) then |
303 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
304 |
||
305 |
if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then |
|
306 |
begin |
|
307 |
tw:= toPowerOf2(Surf^.w); |
|
308 |
th:= toPowerOf2(Surf^.h); |
|
309 |
||
310 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
311 |
||
312 |
gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, |
|
313 |
Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, |
|
314 |
tmpp); |
|
315 |
||
316 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp); |
|
317 |
||
318 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
319 |
end else |
|
320 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels); |
|
753 | 321 |
|
754 | 322 |
if SDL_MustLock(surf) then |
323 |
SDL_UnlockSurface(surf); |
|
324 |
||
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset
|
325 |
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
|
326 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
753 | 327 |
end; |
328 |
||
759 | 329 |
procedure FreeTexture(tex: PTexture); |
330 |
begin |
|
331 |
glDeleteTextures(1, @tex^.id); |
|
332 |
dispose(tex) |
|
333 |
end; |
|
337 | 334 |
|
371 | 335 |
var i: LongInt; |
488 | 336 |
{$ENDIF} |
306 | 337 |
|
4 | 338 |
initialization |
351 | 339 |
cDrownSpeed.QWordValue:= 257698038;// 0.06 |
488 | 340 |
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 |
341 |
cWindSpeed.QWordValue:= 429496;// 0.0001 |
|
351 | 342 |
cGravity:= cMaxWindSpeed; |
343 |
||
488 | 344 |
{$IFDEF DEBUGFILE} |
337 | 345 |
{$I-} |
497 | 346 |
if ParamCount > 0 then |
347 |
for i:= 0 to 7 do |
|
337 | 348 |
begin |
497 | 349 |
Assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
337 | 350 |
rewrite(f); |
351 |
if IOResult = 0 then break |
|
352 |
end; |
|
353 |
{$I+} |
|
17 | 354 |
|
4 | 355 |
finalization |
356 |
writeln(f, '-= halt at ',GameTicks,' ticks =-'); |
|
357 |
Flush(f); |
|
351 | 358 |
close(f) |
4 | 359 |
{$ENDIF} |
360 |
||
361 |
end. |