author | unc0rr |
Sun, 01 Jun 2008 18:54:10 +0000 | |
changeset 969 | 7e8a87136fae |
parent 956 | 19003f7fc174 |
child 988 | cdff3f6f9b38 |
permissions | -rw-r--r-- |
498 | 1 |
(* |
51 | 2 |
* Hedgewars, a worms-like game |
393 | 3 |
* Copyright (c) 2004-2007 Andrey Korotaev <unC0Rr@gmail.com> |
51 | 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 |
|
51 | 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. |
|
51 | 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 |
|
51 | 17 |
*) |
18 |
||
355 | 19 |
{$IFNDEF FPC} |
20 |
WriteLn('Only Freepascal supported'); |
|
21 |
{$ENDIF} |
|
22 |
||
51 | 23 |
program hwengine; |
24 |
uses |
|
25 |
SDLh, |
|
753 | 26 |
GL, |
51 | 27 |
uConsts in 'uConsts.pas', |
28 |
uGame in 'uGame.pas', |
|
29 |
uMisc in 'uMisc.pas', |
|
30 |
uStore in 'uStore.pas', |
|
31 |
uWorld in 'uWorld.pas', |
|
32 |
uIO in 'uIO.pas', |
|
33 |
uGears in 'uGears.pas', |
|
802
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
771
diff
changeset
|
34 |
uVisualGears in 'uVisualGears.pas', |
51 | 35 |
uConsole in 'uConsole.pas', |
36 |
uKeys in 'uKeys.pas', |
|
37 |
uTeams in 'uTeams.pas', |
|
38 |
uSound in 'uSound.pas', |
|
39 |
uRandom in 'uRandom.pas', |
|
40 |
uAI in 'uAI.pas', |
|
79 | 41 |
uAIMisc in 'uAIMisc.pas', |
42 |
uAIAmmoTests in 'uAIAmmoTests.pas', |
|
43 |
uAIActions in 'uAIActions.pas', |
|
51 | 44 |
uCollisions in 'uCollisions.pas', |
45 |
uLand in 'uLand.pas', |
|
46 |
uLandTemplates in 'uLandTemplates.pas', |
|
54 | 47 |
uLandObjects in 'uLandObjects.pas', |
80 | 48 |
uLandGraphics in 'uLandGraphics.pas', |
288 | 49 |
uLocale in 'uLocale.pas', |
316 | 50 |
uAmmos in 'uAmmos.pas', |
351 | 51 |
uSHA in 'uSHA.pas', |
814
7fb4417b7bc1
Start implementing better statistics implementation (does nothing yet)
unc0rr
parents:
803
diff
changeset
|
52 |
uFloat in 'uFloat.pas', |
942 | 53 |
uStats in 'uStats.pas', |
54 |
uChat in 'uChat.pas'; |
|
51 | 55 |
|
56 |
{$INCLUDE options.inc} |
|
57 |
||
58 |
// also: GSHandlers.inc |
|
59 |
// CCHandlers.inc |
|
60 |
// HHHandlers.inc |
|
357 | 61 |
// SinTable.inc |
271 | 62 |
// proto.inc |
51 | 63 |
|
64 |
||
65 |
procedure OnDestroy; forward; |
|
66 |
||
67 |
//////////////////////////////// |
|
371 | 68 |
procedure DoTimer(Lag: LongInt); |
564 | 69 |
const MusicTimerTicks: Longword = 0; |
931 | 70 |
//var s: string; |
51 | 71 |
begin |
564 | 72 |
inc(RealTicks, Lag); |
73 |
||
74 |
inc(MusicTimerTicks, Lag); |
|
565 | 75 |
if (MusicTimerTicks > 3000) and (GameState > gsStart) then |
564 | 76 |
begin |
77 |
PlayMusic; |
|
78 |
MusicTimerTicks:= 0 |
|
79 |
end; |
|
80 |
||
51 | 81 |
case GameState of |
82 |
gsLandGen: begin |
|
83 |
GenMap; |
|
84 |
GameState:= gsStart; |
|
85 |
end; |
|
86 |
gsStart: begin |
|
565 | 87 |
InitPlaylistChunk(GetRandom(High(LongWord))); |
949 | 88 |
AddClouds; |
51 | 89 |
AssignHHCoords; |
90 |
AddMiscGears; |
|
91 |
StoreLoad; |
|
92 |
AdjustColor(cConsoleSplitterColor); |
|
93 |
ResetKbd; |
|
94 |
SoundLoad; |
|
72 | 95 |
if GameType = gmtSave then |
96 |
begin |
|
97 |
isSEBackup:= isSoundEnabled; |
|
98 |
isSoundEnabled:= false |
|
99 |
end; |
|
510 | 100 |
FinishProgress; |
51 | 101 |
GameState:= gsGame |
102 |
end; |
|
947 | 103 |
gsGame: begin |
167 | 104 |
ProcessKbd; |
51 | 105 |
DoGameTick(Lag); |
803 | 106 |
ProcessVisualGears(Lag); |
956 | 107 |
DrawWorld(Lag); |
51 | 108 |
end; |
109 |
gsConsole: begin |
|
110 |
DoGameTick(Lag); |
|
803 | 111 |
ProcessVisualGears(Lag); |
956 | 112 |
DrawWorld(Lag); |
771 | 113 |
DrawConsole(SDLPrimSurface); |
51 | 114 |
end; |
947 | 115 |
gsChat: begin |
116 |
DoGameTick(Lag); |
|
117 |
ProcessVisualGears(Lag); |
|
956 | 118 |
DrawWorld(Lag); |
947 | 119 |
end; |
120 |
gsExit: begin |
|
51 | 121 |
OnDestroy; |
122 |
end; |
|
123 |
end; |
|
564 | 124 |
|
753 | 125 |
SDL_GL_SwapBuffers(); |
51 | 126 |
if flagMakeCapture then |
127 |
begin |
|
887
cfed7e92afd7
Temporarily disable screenshots as they cause segfault
unc0rr
parents:
814
diff
changeset
|
128 |
{ flagMakeCapture:= false; |
81 | 129 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 130 |
WriteLnToConsole('Saving ' + s); |
489 | 131 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
887
cfed7e92afd7
Temporarily disable screenshots as they cause segfault
unc0rr
parents:
814
diff
changeset
|
132 |
} end; |
51 | 133 |
end; |
134 |
||
135 |
//////////////////// |
|
79 | 136 |
procedure OnDestroy; |
51 | 137 |
begin |
138 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
139 |
if isSoundEnabled then ReleaseSound; |
|
619 | 140 |
StoreRelease; |
141 |
SendKB; |
|
51 | 142 |
CloseIPC; |
143 |
TTF_Quit; |
|
144 |
SDL_Quit; |
|
145 |
halt |
|
146 |
end; |
|
147 |
||
148 |
/////////////////// |
|
149 |
procedure MainLoop; |
|
150 |
var PrevTime, |
|
188 | 151 |
CurrTime: Longword; |
51 | 152 |
event: TSDL_Event; |
153 |
begin |
|
154 |
PrevTime:= SDL_GetTicks; |
|
155 |
repeat |
|
156 |
while SDL_PollEvent(@event) <> 0 do |
|
157 |
case event.type_ of |
|
946 | 158 |
SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode); |
308 | 159 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
160 |
cHasFocus:= event.active.gain = 1; |
|
51 | 161 |
SDL_QUITEV: isTerminated:= true |
162 |
end; |
|
163 |
CurrTime:= SDL_GetTicks; |
|
164 |
if PrevTime + cTimerInterval <= CurrTime then |
|
165 |
begin |
|
166 |
DoTimer(CurrTime - PrevTime); |
|
167 |
PrevTime:= CurrTime |
|
434 | 168 |
end else SDL_Delay(1); |
51 | 169 |
IPCCheckSock |
170 |
until isTerminated |
|
171 |
end; |
|
172 |
||
173 |
//////////////////// |
|
174 |
procedure GetParams; |
|
889 | 175 |
var |
267 | 176 |
{$IFDEF DEBUGFILE} |
371 | 177 |
i: LongInt; |
267 | 178 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
179 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
180 |
begin |
51 | 181 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
182 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 183 |
for i:= 0 to ParamCount do |
184 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
185 |
{$ENDIF} |
|
267 | 186 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
187 |
case ParamCount of |
949 | 188 |
14: begin |
498 | 189 |
val(ParamStr(2), cScreenWidth); |
190 |
val(ParamStr(3), cScreenHeight); |
|
497 | 191 |
cBitsStr:= ParamStr(4); |
498 | 192 |
val(cBitsStr, cBits); |
193 |
val(ParamStr(5), ipcPort); |
|
497 | 194 |
cFullScreen:= ParamStr(6) = '1'; |
195 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
196 |
cLocaleFName:= ParamStr(8); |
|
498 | 197 |
val(ParamStr(9), cInitVolume); |
198 |
val(ParamStr(10), cTimerInterval); |
|
497 | 199 |
PathPrefix:= ParamStr(11); |
200 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 201 |
cAltDamage:= ParamStr(13) = '1'; |
949 | 202 |
UserNick:= DecodeBase64(ParamStr(14)); |
267 | 203 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
949 | 204 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p] |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
205 |
end; |
497 | 206 |
3: begin |
498 | 207 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
208 |
GameType:= gmtLandPreview; |
497 | 209 |
if ParamStr(3) <> 'landpreview' then OutError(errmsgShouldntRun, true); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
210 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
211 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
212 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
213 |
end |
51 | 214 |
end; |
215 |
||
216 |
procedure ShowMainWindow; |
|
217 |
begin |
|
351 | 218 |
if cFullScreen then ParseCommand('fullscr 1', true) |
219 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
220 |
SDL_ShowCursor(0) |
51 | 221 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
222 |
|
160 | 223 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
224 |
procedure Game; |
205 | 225 |
var s: shortstring; |
51 | 226 |
begin |
227 |
WriteToConsole('Init SDL... '); |
|
228 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
229 |
WriteLnToConsole(msgOK); |
|
753 | 230 |
|
377 | 231 |
SDL_EnableUNICODE(1); |
51 | 232 |
|
233 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 234 |
SDLTry(TTF_Init <> -1, true); |
51 | 235 |
WriteLnToConsole(msgOK); |
236 |
||
237 |
ShowMainWindow; |
|
238 |
||
239 |
InitKbdKeyTable; |
|
240 |
InitIPC; |
|
241 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 242 |
|
243 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
244 |
||
245 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 246 |
|
247 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
248 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
249 |
||
51 | 250 |
InitTeams; |
288 | 251 |
AssignStores; |
51 | 252 |
|
253 |
if isSoundEnabled then InitSound; |
|
254 |
InitWorld; |
|
255 |
||
256 |
StoreInit; |
|
257 |
||
258 |
isDeveloperMode:= false; |
|
259 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
260 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
261 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
262 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
263 |
|
51 | 264 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
265 |
end; |
51 | 266 |
|
160 | 267 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
268 |
procedure GenLandPreview; |
566 | 269 |
var Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
270 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
271 |
InitIPC; |
159 | 272 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
273 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
274 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
true); |
160 | 276 |
|
566 | 277 |
Preview:= GenPreview; |
159 | 278 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 280 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 281 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
282 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
283 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
284 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
285 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
286 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
287 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
288 |
begin |
907 | 289 |
WriteLnToConsole('-= Hedgewars ' + cVersionString + ' =-'); |
720 | 290 |
WriteLnToConsole(' -= by unC0Rr =- '); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
291 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
292 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
293 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
294 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
295 |
else Game |
51 | 296 |
end. |