author | unc0rr |
Sat, 24 May 2008 17:34:06 +0000 | |
changeset 945 | 4ead9cde4e14 |
parent 916 | 1d0022336fbb |
child 949 | 866729775535 |
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; |
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); |
869 | 237 |
const stc: array [TStatInfoType] of char = 'rDkK'; |
337 | 238 |
begin |
239 |
SendIPC('i' + stc[sit] + s) |
|
240 |
end; |
|
241 |
||
534 | 242 |
function Str2PChar(const s: shortstring): PChar; |
351 | 243 |
const CharArray: array[byte] of Char = ''; |
244 |
begin |
|
245 |
CharArray:= s; |
|
246 |
CharArray[Length(s)]:= #0; |
|
247 |
Str2PChar:= @CharArray |
|
248 |
end; |
|
249 |
||
4 | 250 |
{$IFDEF DEBUGFILE} |
251 |
procedure AddFileLog(s: shortstring); |
|
252 |
begin |
|
253 |
writeln(f, GameTicks: 6, ': ', s); |
|
254 |
flush(f) |
|
255 |
end; |
|
256 |
||
24 | 257 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
258 |
begin |
|
351 | 259 |
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
24 | 260 |
end; |
261 |
||
771 | 262 |
function isPowerOf2(i: Longword): boolean; |
263 |
begin |
|
264 |
if i = 0 then exit(true); |
|
265 |
while (i and 1) = 0 do i:= i shr 1; |
|
266 |
isPowerOf2:= (i = 1) |
|
267 |
end; |
|
268 |
||
269 |
function toPowerOf2(i: Longword): Longword; |
|
270 |
begin |
|
271 |
toPowerOf2:= 1; |
|
272 |
while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1 |
|
273 |
end; |
|
274 |
||
755 | 275 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
753 | 276 |
var mode: LongInt; |
771 | 277 |
tw, th: Longword; |
278 |
tmpp: pointer; |
|
753 | 279 |
begin |
755 | 280 |
new(Surface2Tex); |
281 |
Surface2Tex^.w:= surf^.w; |
|
282 |
Surface2Tex^.h:= surf^.h; |
|
283 |
||
753 | 284 |
if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else |
285 |
if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else |
|
286 |
begin |
|
869 | 287 |
TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true); |
755 | 288 |
Surface2Tex^.id:= 0; |
753 | 289 |
exit |
290 |
end; |
|
291 |
||
755 | 292 |
glGenTextures(1, @Surface2Tex^.id); |
753 | 293 |
|
755 | 294 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
753 | 295 |
|
771 | 296 |
if SDL_MustLock(surf) then |
297 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
298 |
||
299 |
if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then |
|
300 |
begin |
|
301 |
tw:= toPowerOf2(Surf^.w); |
|
302 |
th:= toPowerOf2(Surf^.h); |
|
303 |
||
304 |
GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel); |
|
305 |
||
306 |
gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, |
|
307 |
Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, |
|
308 |
tmpp); |
|
309 |
||
310 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp); |
|
311 |
||
312 |
FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel) |
|
313 |
end else |
|
314 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels); |
|
753 | 315 |
|
754 | 316 |
if SDL_MustLock(surf) then |
317 |
SDL_UnlockSurface(surf); |
|
318 |
||
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
785
diff
changeset
|
319 |
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
|
320 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
753 | 321 |
end; |
322 |
||
759 | 323 |
procedure FreeTexture(tex: PTexture); |
324 |
begin |
|
325 |
glDeleteTextures(1, @tex^.id); |
|
326 |
dispose(tex) |
|
327 |
end; |
|
337 | 328 |
|
371 | 329 |
var i: LongInt; |
488 | 330 |
{$ENDIF} |
306 | 331 |
|
4 | 332 |
initialization |
351 | 333 |
cDrownSpeed.QWordValue:= 257698038;// 0.06 |
488 | 334 |
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 |
335 |
cWindSpeed.QWordValue:= 429496;// 0.0001 |
|
351 | 336 |
cGravity:= cMaxWindSpeed; |
337 |
||
488 | 338 |
{$IFDEF DEBUGFILE} |
337 | 339 |
{$I-} |
497 | 340 |
if ParamCount > 0 then |
341 |
for i:= 0 to 7 do |
|
337 | 342 |
begin |
497 | 343 |
Assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
337 | 344 |
rewrite(f); |
345 |
if IOResult = 0 then break |
|
346 |
end; |
|
347 |
{$I+} |
|
17 | 348 |
|
4 | 349 |
finalization |
916 | 350 |
//uRandom.DumpBuffer; |
4 | 351 |
writeln(f, '-= halt at ',GameTicks,' ticks =-'); |
352 |
Flush(f); |
|
351 | 353 |
close(f) |
4 | 354 |
{$ENDIF} |
355 |
||
356 |
end. |