author | unc0rr |
Sat, 22 Sep 2007 17:50:14 +0000 | |
changeset 611 | 8cf6d27cec86 |
parent 566 | 1c1cb593cb81 |
child 619 | 758da3d985fa |
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, |
|
26 |
uConsts in 'uConsts.pas', |
|
27 |
uGame in 'uGame.pas', |
|
28 |
uMisc in 'uMisc.pas', |
|
29 |
uStore in 'uStore.pas', |
|
30 |
uWorld in 'uWorld.pas', |
|
31 |
uIO in 'uIO.pas', |
|
32 |
uGears in 'uGears.pas', |
|
33 |
uConsole in 'uConsole.pas', |
|
34 |
uKeys in 'uKeys.pas', |
|
35 |
uTeams in 'uTeams.pas', |
|
36 |
uSound in 'uSound.pas', |
|
37 |
uRandom in 'uRandom.pas', |
|
38 |
uAI in 'uAI.pas', |
|
79 | 39 |
uAIMisc in 'uAIMisc.pas', |
40 |
uAIAmmoTests in 'uAIAmmoTests.pas', |
|
41 |
uAIActions in 'uAIActions.pas', |
|
51 | 42 |
uCollisions in 'uCollisions.pas', |
43 |
uLand in 'uLand.pas', |
|
44 |
uLandTemplates in 'uLandTemplates.pas', |
|
54 | 45 |
uLandObjects in 'uLandObjects.pas', |
80 | 46 |
uLandGraphics in 'uLandGraphics.pas', |
288 | 47 |
uLocale in 'uLocale.pas', |
316 | 48 |
uAmmos in 'uAmmos.pas', |
351 | 49 |
uSHA in 'uSHA.pas', |
50 |
uFloat in 'uFloat.pas'; |
|
51 | 51 |
|
52 |
{$INCLUDE options.inc} |
|
53 |
||
54 |
// also: GSHandlers.inc |
|
55 |
// CCHandlers.inc |
|
56 |
// HHHandlers.inc |
|
357 | 57 |
// SinTable.inc |
271 | 58 |
// proto.inc |
51 | 59 |
|
60 |
||
61 |
procedure OnDestroy; forward; |
|
62 |
||
63 |
//////////////////////////////// |
|
371 | 64 |
procedure DoTimer(Lag: LongInt); |
564 | 65 |
const MusicTimerTicks: Longword = 0; |
51 | 66 |
var s: string; |
67 |
begin |
|
564 | 68 |
inc(RealTicks, Lag); |
69 |
||
70 |
inc(MusicTimerTicks, Lag); |
|
565 | 71 |
if (MusicTimerTicks > 3000) and (GameState > gsStart) then |
564 | 72 |
begin |
73 |
PlayMusic; |
|
74 |
MusicTimerTicks:= 0 |
|
75 |
end; |
|
76 |
||
51 | 77 |
case GameState of |
78 |
gsLandGen: begin |
|
79 |
GenMap; |
|
80 |
GameState:= gsStart; |
|
81 |
end; |
|
82 |
gsStart: begin |
|
565 | 83 |
InitPlaylistChunk(GetRandom(High(LongWord))); |
51 | 84 |
AssignHHCoords; |
85 |
AddMiscGears; |
|
86 |
AdjustColor(cColorNearBlack); |
|
56 | 87 |
AdjustColor(cWaterColor); |
51 | 88 |
AdjustColor(cWhiteColor); |
89 |
StoreLoad; |
|
90 |
AdjustColor(cConsoleSplitterColor); |
|
91 |
ResetKbd; |
|
92 |
SoundLoad; |
|
72 | 93 |
if GameType = gmtSave then |
94 |
begin |
|
95 |
isSEBackup:= isSoundEnabled; |
|
96 |
isSoundEnabled:= false |
|
97 |
end; |
|
510 | 98 |
FinishProgress; |
51 | 99 |
GameState:= gsGame |
100 |
end; |
|
101 |
gsGame : begin |
|
167 | 102 |
ProcessKbd; |
51 | 103 |
DoGameTick(Lag); |
104 |
DrawWorld(Lag, SDLPrimSurface); |
|
105 |
end; |
|
106 |
gsConsole: begin |
|
107 |
DoGameTick(Lag); |
|
108 |
DrawWorld(Lag, SDLPrimSurface); |
|
109 |
DrawConsole(SDLPrimSurface); |
|
110 |
end; |
|
111 |
gsExit : begin |
|
112 |
OnDestroy; |
|
113 |
end; |
|
114 |
end; |
|
564 | 115 |
|
51 | 116 |
SDL_Flip(SDLPrimSurface); |
117 |
if flagMakeCapture then |
|
118 |
begin |
|
119 |
flagMakeCapture:= false; |
|
81 | 120 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 121 |
WriteLnToConsole('Saving ' + s); |
489 | 122 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
51 | 123 |
end; |
124 |
end; |
|
125 |
||
126 |
//////////////////// |
|
79 | 127 |
procedure OnDestroy; |
51 | 128 |
begin |
129 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
130 |
if isSoundEnabled then ReleaseSound; |
|
208 | 131 |
StoreRelease;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); |
|
377 | 232 |
SDL_EnableUNICODE(1); |
51 | 233 |
|
234 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 235 |
SDLTry(TTF_Init <> -1, true); |
51 | 236 |
WriteLnToConsole(msgOK); |
237 |
||
238 |
ShowMainWindow; |
|
239 |
||
240 |
InitKbdKeyTable; |
|
241 |
InitIPC; |
|
242 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 243 |
|
244 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
245 |
||
246 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 247 |
|
248 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
249 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
250 |
||
51 | 251 |
InitTeams; |
288 | 252 |
AssignStores; |
51 | 253 |
|
254 |
if isSoundEnabled then InitSound; |
|
255 |
InitWorld; |
|
256 |
||
257 |
StoreInit; |
|
258 |
||
259 |
isDeveloperMode:= false; |
|
260 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
261 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
262 |
'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
|
263 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
264 |
|
51 | 265 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
266 |
end; |
51 | 267 |
|
160 | 268 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
procedure GenLandPreview; |
566 | 270 |
var Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
271 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
272 |
InitIPC; |
159 | 273 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
274 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
276 |
true); |
160 | 277 |
|
566 | 278 |
Preview:= GenPreview; |
159 | 279 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
280 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 281 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 282 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
283 |
end; |
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 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
286 |
/////////////////////////////// m a i n //////////////////////////////////////// |
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 |
begin |
426 | 290 |
WriteLnToConsole('-= HedgeWars 0.9 =-'); |
291 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
292 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
293 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
294 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
295 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
296 |
else Game |
51 | 297 |
end. |