author | displacer |
Thu, 06 Dec 2007 20:25:32 +0000 | |
changeset 630 | 38338573e09a |
parent 619 | 758da3d985fa |
child 720 | 97a9d67d5c3e |
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; |
|
619 | 131 |
StoreRelease; |
132 |
SendKB; |
|
51 | 133 |
CloseIPC; |
134 |
TTF_Quit; |
|
135 |
SDL_Quit; |
|
136 |
halt |
|
137 |
end; |
|
138 |
||
139 |
/////////////////// |
|
140 |
procedure MainLoop; |
|
141 |
var PrevTime, |
|
188 | 142 |
CurrTime: Longword; |
51 | 143 |
event: TSDL_Event; |
144 |
begin |
|
145 |
PrevTime:= SDL_GetTicks; |
|
146 |
repeat |
|
147 |
while SDL_PollEvent(@event) <> 0 do |
|
148 |
case event.type_ of |
|
149 |
SDL_KEYDOWN: case GameState of |
|
150 |
gsGame: if event.key.keysym.sym = 96 then |
|
151 |
begin |
|
152 |
cConsoleYAdd:= cConsoleHeight; |
|
153 |
GameState:= gsConsole |
|
154 |
end; |
|
415 | 155 |
gsConsole: if event.key.keysym.sym = 96 then |
156 |
begin |
|
157 |
GameState:= gsGame; |
|
158 |
cConsoleYAdd:= 0; |
|
159 |
ResetKbd |
|
160 |
end else KeyPressConsole(event.key.keysym.unicode); |
|
51 | 161 |
end; |
308 | 162 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
163 |
cHasFocus:= event.active.gain = 1; |
|
51 | 164 |
SDL_QUITEV: isTerminated:= true |
165 |
end; |
|
166 |
CurrTime:= SDL_GetTicks; |
|
167 |
if PrevTime + cTimerInterval <= CurrTime then |
|
168 |
begin |
|
169 |
DoTimer(CurrTime - PrevTime); |
|
170 |
PrevTime:= CurrTime |
|
434 | 171 |
end else SDL_Delay(1); |
51 | 172 |
IPCCheckSock |
173 |
until isTerminated |
|
174 |
end; |
|
175 |
||
176 |
//////////////////// |
|
177 |
procedure GetParams; |
|
498 | 178 |
var |
267 | 179 |
{$IFDEF DEBUGFILE} |
371 | 180 |
i: LongInt; |
267 | 181 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
182 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
183 |
begin |
51 | 184 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
185 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 186 |
for i:= 0 to ParamCount do |
187 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
188 |
{$ENDIF} |
|
267 | 189 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
190 |
case ParamCount of |
529 | 191 |
13: begin |
498 | 192 |
val(ParamStr(2), cScreenWidth); |
193 |
val(ParamStr(3), cScreenHeight); |
|
497 | 194 |
cBitsStr:= ParamStr(4); |
498 | 195 |
val(cBitsStr, cBits); |
196 |
val(ParamStr(5), ipcPort); |
|
497 | 197 |
cFullScreen:= ParamStr(6) = '1'; |
198 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
199 |
cLocaleFName:= ParamStr(8); |
|
498 | 200 |
val(ParamStr(9), cInitVolume); |
201 |
val(ParamStr(10), cTimerInterval); |
|
497 | 202 |
PathPrefix:= ParamStr(11); |
203 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 204 |
cAltDamage:= ParamStr(13) = '1'; |
267 | 205 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
206 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 207 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
208 |
end; |
497 | 209 |
3: begin |
498 | 210 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
211 |
GameType:= gmtLandPreview; |
497 | 212 |
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
|
213 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
214 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
215 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
216 |
end |
51 | 217 |
end; |
218 |
||
219 |
procedure ShowMainWindow; |
|
220 |
begin |
|
351 | 221 |
if cFullScreen then ParseCommand('fullscr 1', true) |
222 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
223 |
SDL_ShowCursor(0) |
51 | 224 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
225 |
|
160 | 226 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
227 |
procedure Game; |
205 | 228 |
var s: shortstring; |
51 | 229 |
begin |
230 |
WriteToConsole('Init SDL... '); |
|
231 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
232 |
WriteLnToConsole(msgOK); |
|
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 |
426 | 291 |
WriteLnToConsole('-= HedgeWars 0.9 =-'); |
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. |