author | unc0rr |
Sun, 06 Jul 2008 23:32:10 +0000 | |
changeset 1063 | a86328091611 |
parent 1022 | f6a3fcb329ca |
child 1066 | 1f1b3686a2b0 |
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 |
ResetKbd; |
|
93 |
SoundLoad; |
|
72 | 94 |
if GameType = gmtSave then |
95 |
begin |
|
96 |
isSEBackup:= isSoundEnabled; |
|
97 |
isSoundEnabled:= false |
|
98 |
end; |
|
510 | 99 |
FinishProgress; |
51 | 100 |
GameState:= gsGame |
101 |
end; |
|
1022 | 102 |
gsConfirm, |
947 | 103 |
gsGame: begin |
167 | 104 |
ProcessKbd; |
51 | 105 |
DoGameTick(Lag); |
803 | 106 |
ProcessVisualGears(Lag); |
956 | 107 |
DrawWorld(Lag); |
51 | 108 |
end; |
947 | 109 |
gsChat: begin |
110 |
DoGameTick(Lag); |
|
111 |
ProcessVisualGears(Lag); |
|
956 | 112 |
DrawWorld(Lag); |
947 | 113 |
end; |
114 |
gsExit: begin |
|
51 | 115 |
OnDestroy; |
116 |
end; |
|
117 |
end; |
|
564 | 118 |
|
753 | 119 |
SDL_GL_SwapBuffers(); |
51 | 120 |
if flagMakeCapture then |
121 |
begin |
|
887
cfed7e92afd7
Temporarily disable screenshots as they cause segfault
unc0rr
parents:
814
diff
changeset
|
122 |
{ flagMakeCapture:= false; |
81 | 123 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 124 |
WriteLnToConsole('Saving ' + s); |
489 | 125 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
887
cfed7e92afd7
Temporarily disable screenshots as they cause segfault
unc0rr
parents:
814
diff
changeset
|
126 |
} end; |
51 | 127 |
end; |
128 |
||
129 |
//////////////////// |
|
79 | 130 |
procedure OnDestroy; |
51 | 131 |
begin |
132 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
133 |
if isSoundEnabled then ReleaseSound; |
|
619 | 134 |
StoreRelease; |
135 |
SendKB; |
|
51 | 136 |
CloseIPC; |
137 |
TTF_Quit; |
|
138 |
SDL_Quit; |
|
139 |
halt |
|
140 |
end; |
|
141 |
||
142 |
/////////////////// |
|
143 |
procedure MainLoop; |
|
144 |
var PrevTime, |
|
188 | 145 |
CurrTime: Longword; |
51 | 146 |
event: TSDL_Event; |
147 |
begin |
|
148 |
PrevTime:= SDL_GetTicks; |
|
149 |
repeat |
|
150 |
while SDL_PollEvent(@event) <> 0 do |
|
151 |
case event.type_ of |
|
946 | 152 |
SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode); |
308 | 153 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
154 |
cHasFocus:= event.active.gain = 1; |
|
51 | 155 |
SDL_QUITEV: isTerminated:= true |
156 |
end; |
|
157 |
CurrTime:= SDL_GetTicks; |
|
158 |
if PrevTime + cTimerInterval <= CurrTime then |
|
159 |
begin |
|
160 |
DoTimer(CurrTime - PrevTime); |
|
161 |
PrevTime:= CurrTime |
|
434 | 162 |
end else SDL_Delay(1); |
51 | 163 |
IPCCheckSock |
164 |
until isTerminated |
|
165 |
end; |
|
166 |
||
167 |
//////////////////// |
|
168 |
procedure GetParams; |
|
889 | 169 |
var |
267 | 170 |
{$IFDEF DEBUGFILE} |
371 | 171 |
i: LongInt; |
267 | 172 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
173 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
174 |
begin |
51 | 175 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
176 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 177 |
for i:= 0 to ParamCount do |
178 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
179 |
{$ENDIF} |
|
267 | 180 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
181 |
case ParamCount of |
949 | 182 |
14: begin |
498 | 183 |
val(ParamStr(2), cScreenWidth); |
184 |
val(ParamStr(3), cScreenHeight); |
|
497 | 185 |
cBitsStr:= ParamStr(4); |
498 | 186 |
val(cBitsStr, cBits); |
187 |
val(ParamStr(5), ipcPort); |
|
497 | 188 |
cFullScreen:= ParamStr(6) = '1'; |
189 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
190 |
cLocaleFName:= ParamStr(8); |
|
498 | 191 |
val(ParamStr(9), cInitVolume); |
192 |
val(ParamStr(10), cTimerInterval); |
|
497 | 193 |
PathPrefix:= ParamStr(11); |
194 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 195 |
cAltDamage:= ParamStr(13) = '1'; |
949 | 196 |
UserNick:= DecodeBase64(ParamStr(14)); |
267 | 197 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
949 | 198 |
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
|
199 |
end; |
497 | 200 |
3: begin |
498 | 201 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
202 |
GameType:= gmtLandPreview; |
497 | 203 |
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
|
204 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
205 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
206 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
207 |
end |
51 | 208 |
end; |
209 |
||
210 |
procedure ShowMainWindow; |
|
211 |
begin |
|
351 | 212 |
if cFullScreen then ParseCommand('fullscr 1', true) |
213 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
214 |
SDL_ShowCursor(0) |
51 | 215 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
216 |
|
160 | 217 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
218 |
procedure Game; |
205 | 219 |
var s: shortstring; |
51 | 220 |
begin |
221 |
WriteToConsole('Init SDL... '); |
|
222 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
223 |
WriteLnToConsole(msgOK); |
|
753 | 224 |
|
377 | 225 |
SDL_EnableUNICODE(1); |
51 | 226 |
|
227 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 228 |
SDLTry(TTF_Init <> -1, true); |
51 | 229 |
WriteLnToConsole(msgOK); |
230 |
||
231 |
ShowMainWindow; |
|
232 |
||
233 |
InitKbdKeyTable; |
|
234 |
InitIPC; |
|
235 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 236 |
|
237 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
238 |
||
239 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 240 |
|
241 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
242 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
243 |
||
51 | 244 |
InitTeams; |
288 | 245 |
AssignStores; |
51 | 246 |
|
247 |
if isSoundEnabled then InitSound; |
|
248 |
InitWorld; |
|
249 |
||
250 |
StoreInit; |
|
251 |
||
252 |
isDeveloperMode:= false; |
|
253 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
254 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
255 |
'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
|
256 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
257 |
|
51 | 258 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
end; |
51 | 260 |
|
160 | 261 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
262 |
procedure GenLandPreview; |
566 | 263 |
var Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
264 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
265 |
InitIPC; |
159 | 266 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
268 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
true); |
160 | 270 |
|
566 | 271 |
Preview:= GenPreview; |
159 | 272 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
273 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 274 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 275 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
276 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
277 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
280 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
281 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
282 |
begin |
907 | 283 |
WriteLnToConsole('-= Hedgewars ' + cVersionString + ' =-'); |
720 | 284 |
WriteLnToConsole(' -= by unC0Rr =- '); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
285 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
286 |
Randomize; |
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 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
289 |
else Game |
51 | 290 |
end. |