author | unc0rr |
Tue, 04 Mar 2008 20:26:52 +0000 | |
changeset 794 | 4dfc653ed92c |
parent 771 | 86fbe8753a7c |
child 802 | ed5450a89b96 |
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); |
|
771 | 106 |
DrawWorld(Lag, SDLPrimSurface); |
107 |
DrawConsole(SDLPrimSurface); |
|
51 | 108 |
end; |
109 |
gsExit : begin |
|
110 |
OnDestroy; |
|
111 |
end; |
|
112 |
end; |
|
564 | 113 |
|
753 | 114 |
SDL_GL_SwapBuffers(); |
51 | 115 |
if flagMakeCapture then |
116 |
begin |
|
117 |
flagMakeCapture:= false; |
|
81 | 118 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 119 |
WriteLnToConsole('Saving ' + s); |
489 | 120 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
51 | 121 |
end; |
122 |
end; |
|
123 |
||
124 |
//////////////////// |
|
79 | 125 |
procedure OnDestroy; |
51 | 126 |
begin |
127 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
128 |
if isSoundEnabled then ReleaseSound; |
|
619 | 129 |
StoreRelease; |
130 |
SendKB; |
|
51 | 131 |
CloseIPC; |
132 |
TTF_Quit; |
|
133 |
SDL_Quit; |
|
134 |
halt |
|
135 |
end; |
|
136 |
||
137 |
/////////////////// |
|
138 |
procedure MainLoop; |
|
139 |
var PrevTime, |
|
188 | 140 |
CurrTime: Longword; |
51 | 141 |
event: TSDL_Event; |
142 |
begin |
|
143 |
PrevTime:= SDL_GetTicks; |
|
144 |
repeat |
|
145 |
while SDL_PollEvent(@event) <> 0 do |
|
146 |
case event.type_ of |
|
147 |
SDL_KEYDOWN: case GameState of |
|
148 |
gsGame: if event.key.keysym.sym = 96 then |
|
149 |
begin |
|
150 |
cConsoleYAdd:= cConsoleHeight; |
|
151 |
GameState:= gsConsole |
|
152 |
end; |
|
415 | 153 |
gsConsole: if event.key.keysym.sym = 96 then |
154 |
begin |
|
155 |
GameState:= gsGame; |
|
156 |
cConsoleYAdd:= 0; |
|
157 |
ResetKbd |
|
158 |
end else KeyPressConsole(event.key.keysym.unicode); |
|
51 | 159 |
end; |
308 | 160 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
161 |
cHasFocus:= event.active.gain = 1; |
|
51 | 162 |
SDL_QUITEV: isTerminated:= true |
163 |
end; |
|
164 |
CurrTime:= SDL_GetTicks; |
|
165 |
if PrevTime + cTimerInterval <= CurrTime then |
|
166 |
begin |
|
167 |
DoTimer(CurrTime - PrevTime); |
|
168 |
PrevTime:= CurrTime |
|
434 | 169 |
end else SDL_Delay(1); |
51 | 170 |
IPCCheckSock |
171 |
until isTerminated |
|
172 |
end; |
|
173 |
||
174 |
//////////////////// |
|
175 |
procedure GetParams; |
|
498 | 176 |
var |
267 | 177 |
{$IFDEF DEBUGFILE} |
371 | 178 |
i: LongInt; |
267 | 179 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
180 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
181 |
begin |
51 | 182 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
183 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 184 |
for i:= 0 to ParamCount do |
185 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
186 |
{$ENDIF} |
|
267 | 187 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
188 |
case ParamCount of |
529 | 189 |
13: begin |
498 | 190 |
val(ParamStr(2), cScreenWidth); |
191 |
val(ParamStr(3), cScreenHeight); |
|
497 | 192 |
cBitsStr:= ParamStr(4); |
498 | 193 |
val(cBitsStr, cBits); |
194 |
val(ParamStr(5), ipcPort); |
|
497 | 195 |
cFullScreen:= ParamStr(6) = '1'; |
196 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
197 |
cLocaleFName:= ParamStr(8); |
|
498 | 198 |
val(ParamStr(9), cInitVolume); |
199 |
val(ParamStr(10), cTimerInterval); |
|
497 | 200 |
PathPrefix:= ParamStr(11); |
201 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 202 |
cAltDamage:= ParamStr(13) = '1'; |
267 | 203 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
204 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 205 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
206 |
end; |
497 | 207 |
3: begin |
498 | 208 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
209 |
GameType:= gmtLandPreview; |
497 | 210 |
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
|
211 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
212 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
213 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
214 |
end |
51 | 215 |
end; |
216 |
||
217 |
procedure ShowMainWindow; |
|
218 |
begin |
|
351 | 219 |
if cFullScreen then ParseCommand('fullscr 1', true) |
220 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
221 |
SDL_ShowCursor(0) |
51 | 222 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
223 |
|
160 | 224 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
225 |
procedure Game; |
205 | 226 |
var s: shortstring; |
51 | 227 |
begin |
228 |
WriteToConsole('Init SDL... '); |
|
229 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
230 |
WriteLnToConsole(msgOK); |
|
753 | 231 |
|
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 |
720 | 290 |
WriteLnToConsole('-= HedgeWars 0.9.2 =-'); |
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. |