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