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