author | unc0rr |
Fri, 24 Aug 2007 20:24:29 +0000 | |
changeset 584 | f381705f1aeb |
parent 566 | 1c1cb593cb81 |
child 611 | 8cf6d27cec86 |
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 |
{$APPTYPE CONSOLE} |
|
25 |
uses |
|
26 |
SDLh, |
|
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); |
|
109 |
DrawWorld(Lag, SDLPrimSurface); |
|
110 |
DrawConsole(SDLPrimSurface); |
|
111 |
end; |
|
112 |
gsExit : begin |
|
113 |
OnDestroy; |
|
114 |
end; |
|
115 |
end; |
|
564 | 116 |
|
51 | 117 |
SDL_Flip(SDLPrimSurface); |
118 |
if flagMakeCapture then |
|
119 |
begin |
|
120 |
flagMakeCapture:= false; |
|
81 | 121 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 122 |
WriteLnToConsole('Saving ' + s); |
489 | 123 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
51 | 124 |
end; |
125 |
end; |
|
126 |
||
127 |
//////////////////// |
|
79 | 128 |
procedure OnDestroy; |
51 | 129 |
begin |
130 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
131 |
if isSoundEnabled then ReleaseSound; |
|
208 | 132 |
StoreRelease;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. |