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