author | unc0rr |
Mon, 28 Jan 2008 18:22:35 +0000 | |
changeset 775 | 23c253aae336 |
parent 771 | 86fbe8753a7c |
child 780 | f45b0e513dfd |
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 |
cWaterColor : Longword = $FF005ACE; |
59 |
cWhiteColor : Longword = $FFFFFFFF; |
|
60 |
cConsoleSplitterColor : Longword = $FFFF0000; |
|
61 |
cColorNearBlack : Longword = $FF000010; |
|
4 | 62 |
cExplosionBorderColor : LongWord = $808080; |
63 |
||
64 |
cShowFPS : boolean = true; |
|
614 | 65 |
cCaseFactor : Longword = 6; {0..9} |
622 | 66 |
cLandAdditions: Longword = 4; |
4 | 67 |
cFullScreen : boolean = true; |
80 | 68 |
cLocaleFName : shortstring = 'en.txt'; |
81 | 69 |
cSeed : shortstring = ''; |
371 | 70 |
cInitVolume : LongInt = 128; |
71 |
cVolumeDelta : LongInt = 0; |
|
296 | 72 |
cTimerInterval : Longword = 5; |
351 | 73 |
cHasFocus : boolean = true; |
614 | 74 |
cInactDelay : Longword = 1500; |
4 | 75 |
|
614 | 76 |
{$WARNINGS OFF} |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
77 |
cAirPlaneSpeed: hwFloat = (isNegative: false; QWordValue: 6012954214); // 1.4 |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
78 |
cBombsSpeed : hwFloat = (isNegative: false; QWordValue: 429496729); |
614 | 79 |
{$WARNINGS ON} |
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
80 |
|
4 | 81 |
var |
82 |
cSendEmptyPacketTime : LongWord = 2000; |
|
83 |
cSendCursorPosTime : LongWord = 50; |
|
13 | 84 |
ShowCrosshair : boolean; |
351 | 85 |
cDrownSpeed, |
86 |
cMaxWindSpeed, |
|
87 |
cWindSpeed, |
|
88 |
cGravity: hwFloat; |
|
4 | 89 |
|
90 |
flagMakeCapture: boolean = false; |
|
91 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
53
diff
changeset
|
92 |
InitStepsFlags: Longword = 0; |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
53
diff
changeset
|
93 |
|
564 | 94 |
RealTicks: Longword = 0; |
95 |
||
371 | 96 |
AttackBar: LongInt = 0; // 0 - none, 1 - just bar at the right-down corner, 2 - like in WWP |
4 | 97 |
|
371 | 98 |
function hwSign(r: hwFloat): LongInt; |
534 | 99 |
function Min(a, b: LongInt): LongInt; |
371 | 100 |
function Max(a, b: LongInt): LongInt; |
351 | 101 |
function rndSign(num: hwFloat): hwFloat; |
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); |
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); |
238 |
const stc: array [TStatInfoType] of char = 'rDK'; |
|
239 |
begin |
|
240 |
SendIPC('i' + stc[sit] + s) |
|
241 |
end; |
|
242 |
||
351 | 243 |
function rndSign(num: hwFloat): hwFloat; |
244 |
begin |
|
245 |
num.isNegative:= getrandom(2) = 0; |
|
246 |
rndSign:= num |
|
247 |
end; |
|
248 |
||
534 | 249 |
function Str2PChar(const s: shortstring): PChar; |
351 | 250 |
const CharArray: array[byte] of Char = ''; |
251 |
begin |
|
252 |
CharArray:= s; |
|
253 |
CharArray[Length(s)]:= #0; |
|
254 |
Str2PChar:= @CharArray |
|
255 |
end; |
|
256 |
||
4 | 257 |
{$IFDEF DEBUGFILE} |
258 |
procedure AddFileLog(s: shortstring); |
|
259 |
begin |
|
260 |
writeln(f, GameTicks: 6, ': ', s); |
|
261 |
flush(f) |
|
262 |
end; |
|
263 |
||
24 | 264 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
265 |
begin |
|
351 | 266 |
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
24 | 267 |
end; |
268 |
||
771 | 269 |
function isPowerOf2(i: Longword): boolean; |
270 |
begin |
|
271 |
if i = 0 then exit(true); |
|
272 |
while (i and 1) = 0 do i:= i shr 1; |
|
273 |
isPowerOf2:= (i = 1) |
|
274 |
end; |
|
275 |
||
276 |
function toPowerOf2(i: Longword): Longword; |
|
277 |
begin |
|
278 |
toPowerOf2:= 1; |
|
279 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
280 |
end; |
|
281 |
||
755 | 282 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
753 | 283 |
var mode: LongInt; |
284 |
texId: GLuint; |
|
771 | 285 |
tw, th: Longword; |
286 |
tmpp: pointer; |
|
753 | 287 |
begin |
755 | 288 |
new(Surface2Tex); |
289 |
Surface2Tex^.w:= surf^.w; |
|
290 |
Surface2Tex^.h:= surf^.h; |
|
291 |
||
753 | 292 |
if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else |
293 |
if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else |
|
294 |
begin |
|
771 | 295 |
TryDo(false, 'Surface2Tex: BytePerPixel not in [3, 4', false); |
755 | 296 |
Surface2Tex^.id:= 0; |
753 | 297 |
exit |
298 |
end; |
|
299 |
||
755 | 300 |
glGenTextures(1, @Surface2Tex^.id); |
753 | 301 |
|
755 | 302 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
753 | 303 |
|
771 | 304 |
if SDL_MustLock(surf) then |
305 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
306 |
||
307 |
if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then |
|
308 |
begin |
|
309 |
tw:= toPowerOf2(Surf^.w); |
|
310 |
th:= toPowerOf2(Surf^.h); |
|
311 |
||
312 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
313 |
||
314 |
gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, |
|
315 |
Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, |
|
316 |
tmpp); |
|
317 |
||
318 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp); |
|
319 |
||
320 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
321 |
end else |
|
322 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels); |
|
753 | 323 |
|
754 | 324 |
if SDL_MustLock(surf) then |
325 |
SDL_UnlockSurface(surf); |
|
326 |
||
771 | 327 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
762 | 328 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
753 | 329 |
end; |
330 |
||
759 | 331 |
procedure FreeTexture(tex: PTexture); |
332 |
begin |
|
333 |
glDeleteTextures(1, @tex^.id); |
|
334 |
dispose(tex) |
|
335 |
end; |
|
337 | 336 |
|
371 | 337 |
var i: LongInt; |
488 | 338 |
{$ENDIF} |
306 | 339 |
|
4 | 340 |
initialization |
351 | 341 |
cDrownSpeed.QWordValue:= 257698038;// 0.06 |
488 | 342 |
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 |
343 |
cWindSpeed.QWordValue:= 429496;// 0.0001 |
|
351 | 344 |
cGravity:= cMaxWindSpeed; |
345 |
||
488 | 346 |
{$IFDEF DEBUGFILE} |
337 | 347 |
{$I-} |
497 | 348 |
if ParamCount > 0 then |
349 |
for i:= 0 to 7 do |
|
337 | 350 |
begin |
497 | 351 |
Assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
337 | 352 |
rewrite(f); |
353 |
if IOResult = 0 then break |
|
354 |
end; |
|
355 |
{$I+} |
|
17 | 356 |
|
4 | 357 |
finalization |
358 |
writeln(f, '-= halt at ',GameTicks,' ticks =-'); |
|
359 |
Flush(f); |
|
351 | 360 |
close(f) |
4 | 361 |
{$ENDIF} |
362 |
||
363 |
end. |