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