author | unc0rr |
Sun, 27 Jan 2008 15:02:12 +0000 | |
changeset 762 | 5ecf042f6113 |
parent 759 | 8da05b316d81 |
child 771 | 86fbe8753a7c |
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; |
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 |
|
351 | 126 |
uses uConsole, uStore, uIO, Math, uRandom; |
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 |
||
371 | 184 |
function DxDy2Angle32(const _dY, _dX: hwFloat): LongInt; |
100 | 185 |
const _16divPI: Extended = 16/pi; |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
186 |
var dY, dX: Extended; |
100 | 187 |
begin |
370
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
188 |
dY:= _dY.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
189 |
if _dY.isNegative then dY:= - dY; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
190 |
dX:= _dX.QWordValue / $100000000; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
191 |
if _dX.isNegative then dX:= - dX; |
c75410fe3133
- Repair bots: they can walk and use bazooka, possible cannot jump (why?)
unc0rr
parents:
351
diff
changeset
|
192 |
DxDy2Angle32:= trunc(arctan2(dY, dX) * _16divPI) and $1f |
4 | 193 |
end; |
194 |
||
371 | 195 |
function DxDy2AttackAngle(const _dY, _dX: hwFloat): LongInt; |
100 | 196 |
const MaxAngleDivPI: Extended = cMaxAngle/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; |
438 | 203 |
DxDy2AttackAngle:= trunc(arctan2(dY, dX) * MaxAngleDivPI) |
100 | 204 |
end; |
4 | 205 |
|
208 | 206 |
procedure SetKB(n: Longword); |
207 |
begin |
|
208 |
KBnum:= n |
|
209 |
end; |
|
210 |
||
211 |
procedure SendKB; |
|
212 |
var s: shortstring; |
|
213 |
begin |
|
214 |
if KBnum <> 0 then |
|
215 |
begin |
|
216 |
s:= 'K' + inttostr(KBnum); |
|
217 |
SendIPCRaw(@s, Length(s) + 1) |
|
218 |
end |
|
219 |
end; |
|
220 |
||
351 | 221 |
procedure SetLittle(var r: hwFloat); |
300 | 222 |
begin |
553
5478386d935f
- Switch to bazooka (or whatever) after use of some weapon (fixes problem with bots)
unc0rr
parents:
543
diff
changeset
|
223 |
r:= SignAs(cLittle, r) |
300 | 224 |
end; |
225 |
||
337 | 226 |
procedure SendStat(sit: TStatInfoType; s: shortstring); |
227 |
const stc: array [TStatInfoType] of char = 'rDK'; |
|
228 |
begin |
|
229 |
SendIPC('i' + stc[sit] + s) |
|
230 |
end; |
|
231 |
||
351 | 232 |
function rndSign(num: hwFloat): hwFloat; |
233 |
begin |
|
234 |
num.isNegative:= getrandom(2) = 0; |
|
235 |
rndSign:= num |
|
236 |
end; |
|
237 |
||
534 | 238 |
function Str2PChar(const s: shortstring): PChar; |
351 | 239 |
const CharArray: array[byte] of Char = ''; |
240 |
begin |
|
241 |
CharArray:= s; |
|
242 |
CharArray[Length(s)]:= #0; |
|
243 |
Str2PChar:= @CharArray |
|
244 |
end; |
|
245 |
||
4 | 246 |
{$IFDEF DEBUGFILE} |
247 |
procedure AddFileLog(s: shortstring); |
|
248 |
begin |
|
249 |
writeln(f, GameTicks: 6, ': ', s); |
|
250 |
flush(f) |
|
251 |
end; |
|
252 |
||
24 | 253 |
function RectToStr(Rect: TSDL_Rect): shortstring; |
254 |
begin |
|
351 | 255 |
RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')' |
24 | 256 |
end; |
257 |
||
755 | 258 |
function Surface2Tex(surf: PSDL_Surface): PTexture; |
753 | 259 |
var mode: LongInt; |
260 |
texId: GLuint; |
|
261 |
begin |
|
754 | 262 |
if SDL_MustLock(surf) then |
263 |
SDLTry(SDL_LockSurface(surf) >= 0, true); |
|
264 |
||
755 | 265 |
new(Surface2Tex); |
266 |
Surface2Tex^.w:= surf^.w; |
|
267 |
Surface2Tex^.h:= surf^.h; |
|
268 |
||
753 | 269 |
if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else |
270 |
if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else |
|
271 |
begin |
|
755 | 272 |
TryDo(false, 'Surface2Tex: BytePerPixel not in [3, 4]', false); |
273 |
Surface2Tex^.id:= 0; |
|
753 | 274 |
exit |
275 |
end; |
|
276 |
||
755 | 277 |
glGenTextures(1, @Surface2Tex^.id); |
753 | 278 |
|
755 | 279 |
glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id); |
753 | 280 |
|
281 |
glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels); |
|
282 |
||
754 | 283 |
if SDL_MustLock(surf) then |
284 |
SDL_UnlockSurface(surf); |
|
285 |
||
762 | 286 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
287 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) |
|
753 | 288 |
end; |
289 |
||
759 | 290 |
procedure FreeTexture(tex: PTexture); |
291 |
begin |
|
292 |
glDeleteTextures(1, @tex^.id); |
|
293 |
dispose(tex) |
|
294 |
end; |
|
337 | 295 |
|
371 | 296 |
var i: LongInt; |
488 | 297 |
{$ENDIF} |
306 | 298 |
|
4 | 299 |
initialization |
351 | 300 |
cDrownSpeed.QWordValue:= 257698038;// 0.06 |
488 | 301 |
cMaxWindSpeed.QWordValue:= 2147484;// 0.0005 |
302 |
cWindSpeed.QWordValue:= 429496;// 0.0001 |
|
351 | 303 |
cGravity:= cMaxWindSpeed; |
304 |
||
488 | 305 |
{$IFDEF DEBUGFILE} |
337 | 306 |
{$I-} |
497 | 307 |
if ParamCount > 0 then |
308 |
for i:= 0 to 7 do |
|
337 | 309 |
begin |
497 | 310 |
Assign(f, ParamStr(1) + '/debug' + inttostr(i) + '.txt'); |
337 | 311 |
rewrite(f); |
312 |
if IOResult = 0 then break |
|
313 |
end; |
|
314 |
{$I+} |
|
17 | 315 |
|
4 | 316 |
finalization |
317 |
writeln(f, '-= halt at ',GameTicks,' ticks =-'); |
|
318 |
Flush(f); |
|
351 | 319 |
close(f) |
4 | 320 |
{$ENDIF} |
321 |
||
322 |
end. |