author | unc0rr |
Thu, 13 Mar 2008 20:21:45 +0000 | |
changeset 812 | cbc392576990 |
parent 803 | 3f73901a350a |
child 814 | 7fb4417b7bc1 |
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', |
52 |
uFloat in 'uFloat.pas'; |
|
51 | 53 |
|
54 |
{$INCLUDE options.inc} |
|
55 |
||
56 |
// also: GSHandlers.inc |
|
57 |
// CCHandlers.inc |
|
58 |
// HHHandlers.inc |
|
357 | 59 |
// SinTable.inc |
271 | 60 |
// proto.inc |
51 | 61 |
|
62 |
||
63 |
procedure OnDestroy; forward; |
|
64 |
||
65 |
//////////////////////////////// |
|
371 | 66 |
procedure DoTimer(Lag: LongInt); |
564 | 67 |
const MusicTimerTicks: Longword = 0; |
51 | 68 |
var s: string; |
69 |
begin |
|
564 | 70 |
inc(RealTicks, Lag); |
71 |
||
72 |
inc(MusicTimerTicks, Lag); |
|
565 | 73 |
if (MusicTimerTicks > 3000) and (GameState > gsStart) then |
564 | 74 |
begin |
75 |
PlayMusic; |
|
76 |
MusicTimerTicks:= 0 |
|
77 |
end; |
|
78 |
||
51 | 79 |
case GameState of |
80 |
gsLandGen: begin |
|
81 |
GenMap; |
|
82 |
GameState:= gsStart; |
|
83 |
end; |
|
84 |
gsStart: begin |
|
565 | 85 |
InitPlaylistChunk(GetRandom(High(LongWord))); |
51 | 86 |
AssignHHCoords; |
87 |
AddMiscGears; |
|
88 |
StoreLoad; |
|
89 |
AdjustColor(cConsoleSplitterColor); |
|
90 |
ResetKbd; |
|
91 |
SoundLoad; |
|
72 | 92 |
if GameType = gmtSave then |
93 |
begin |
|
94 |
isSEBackup:= isSoundEnabled; |
|
95 |
isSoundEnabled:= false |
|
96 |
end; |
|
510 | 97 |
FinishProgress; |
51 | 98 |
GameState:= gsGame |
99 |
end; |
|
100 |
gsGame : begin |
|
167 | 101 |
ProcessKbd; |
51 | 102 |
DoGameTick(Lag); |
803 | 103 |
ProcessVisualGears(Lag); |
51 | 104 |
DrawWorld(Lag, SDLPrimSurface); |
105 |
end; |
|
106 |
gsConsole: begin |
|
107 |
DoGameTick(Lag); |
|
803 | 108 |
ProcessVisualGears(Lag); |
771 | 109 |
DrawWorld(Lag, SDLPrimSurface); |
110 |
DrawConsole(SDLPrimSurface); |
|
51 | 111 |
end; |
112 |
gsExit : begin |
|
113 |
OnDestroy; |
|
114 |
end; |
|
115 |
end; |
|
564 | 116 |
|
753 | 117 |
SDL_GL_SwapBuffers(); |
51 | 118 |
if flagMakeCapture then |
119 |
begin |
|
120 |
flagMakeCapture:= false; |
|
81 | 121 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 122 |
WriteLnToConsole('Saving ' + s); |
489 | 123 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
51 | 124 |
end; |
125 |
end; |
|
126 |
||
127 |
//////////////////// |
|
79 | 128 |
procedure OnDestroy; |
51 | 129 |
begin |
130 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
131 |
if isSoundEnabled then ReleaseSound; |
|
619 | 132 |
StoreRelease; |
133 |
SendKB; |
|
51 | 134 |
CloseIPC; |
135 |
TTF_Quit; |
|
136 |
SDL_Quit; |
|
137 |
halt |
|
138 |
end; |
|
139 |
||
140 |
/////////////////// |
|
141 |
procedure MainLoop; |
|
142 |
var PrevTime, |
|
188 | 143 |
CurrTime: Longword; |
51 | 144 |
event: TSDL_Event; |
145 |
begin |
|
146 |
PrevTime:= SDL_GetTicks; |
|
147 |
repeat |
|
148 |
while SDL_PollEvent(@event) <> 0 do |
|
149 |
case event.type_ of |
|
150 |
SDL_KEYDOWN: case GameState of |
|
151 |
gsGame: if event.key.keysym.sym = 96 then |
|
152 |
begin |
|
153 |
cConsoleYAdd:= cConsoleHeight; |
|
154 |
GameState:= gsConsole |
|
155 |
end; |
|
415 | 156 |
gsConsole: if event.key.keysym.sym = 96 then |
157 |
begin |
|
158 |
GameState:= gsGame; |
|
159 |
cConsoleYAdd:= 0; |
|
160 |
ResetKbd |
|
161 |
end else KeyPressConsole(event.key.keysym.unicode); |
|
51 | 162 |
end; |
308 | 163 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
164 |
cHasFocus:= event.active.gain = 1; |
|
51 | 165 |
SDL_QUITEV: isTerminated:= true |
166 |
end; |
|
167 |
CurrTime:= SDL_GetTicks; |
|
168 |
if PrevTime + cTimerInterval <= CurrTime then |
|
169 |
begin |
|
170 |
DoTimer(CurrTime - PrevTime); |
|
171 |
PrevTime:= CurrTime |
|
434 | 172 |
end else SDL_Delay(1); |
51 | 173 |
IPCCheckSock |
174 |
until isTerminated |
|
175 |
end; |
|
176 |
||
177 |
//////////////////// |
|
178 |
procedure GetParams; |
|
498 | 179 |
var |
267 | 180 |
{$IFDEF DEBUGFILE} |
371 | 181 |
i: LongInt; |
267 | 182 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
183 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
184 |
begin |
51 | 185 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
186 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 187 |
for i:= 0 to ParamCount do |
188 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
189 |
{$ENDIF} |
|
267 | 190 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
191 |
case ParamCount of |
529 | 192 |
13: begin |
498 | 193 |
val(ParamStr(2), cScreenWidth); |
194 |
val(ParamStr(3), cScreenHeight); |
|
497 | 195 |
cBitsStr:= ParamStr(4); |
498 | 196 |
val(cBitsStr, cBits); |
197 |
val(ParamStr(5), ipcPort); |
|
497 | 198 |
cFullScreen:= ParamStr(6) = '1'; |
199 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
200 |
cLocaleFName:= ParamStr(8); |
|
498 | 201 |
val(ParamStr(9), cInitVolume); |
202 |
val(ParamStr(10), cTimerInterval); |
|
497 | 203 |
PathPrefix:= ParamStr(11); |
204 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 205 |
cAltDamage:= ParamStr(13) = '1'; |
267 | 206 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
207 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 208 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
209 |
end; |
497 | 210 |
3: begin |
498 | 211 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
212 |
GameType:= gmtLandPreview; |
497 | 213 |
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
|
214 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
215 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
216 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
217 |
end |
51 | 218 |
end; |
219 |
||
220 |
procedure ShowMainWindow; |
|
221 |
begin |
|
351 | 222 |
if cFullScreen then ParseCommand('fullscr 1', true) |
223 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
224 |
SDL_ShowCursor(0) |
51 | 225 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
226 |
|
160 | 227 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
228 |
procedure Game; |
205 | 229 |
var s: shortstring; |
51 | 230 |
begin |
231 |
WriteToConsole('Init SDL... '); |
|
232 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
233 |
WriteLnToConsole(msgOK); |
|
753 | 234 |
|
377 | 235 |
SDL_EnableUNICODE(1); |
51 | 236 |
|
237 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 238 |
SDLTry(TTF_Init <> -1, true); |
51 | 239 |
WriteLnToConsole(msgOK); |
240 |
||
241 |
ShowMainWindow; |
|
242 |
||
243 |
InitKbdKeyTable; |
|
244 |
InitIPC; |
|
245 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 246 |
|
247 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
248 |
||
249 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 250 |
|
251 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
252 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
253 |
||
51 | 254 |
InitTeams; |
288 | 255 |
AssignStores; |
51 | 256 |
|
257 |
if isSoundEnabled then InitSound; |
|
258 |
InitWorld; |
|
259 |
||
260 |
StoreInit; |
|
261 |
||
262 |
isDeveloperMode:= false; |
|
263 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
264 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
265 |
'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
|
266 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
267 |
|
51 | 268 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
end; |
51 | 270 |
|
160 | 271 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
272 |
procedure GenLandPreview; |
566 | 273 |
var Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
274 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
InitIPC; |
159 | 276 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
277 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
true); |
160 | 280 |
|
566 | 281 |
Preview:= GenPreview; |
159 | 282 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
283 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 284 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 285 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
286 |
end; |
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 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
289 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
290 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
291 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
292 |
begin |
720 | 293 |
WriteLnToConsole('-= HedgeWars 0.9.2 =-'); |
294 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
295 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
296 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
297 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
298 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
299 |
else Game |
51 | 300 |
end. |