author | displacer |
Sat, 03 Feb 2007 00:19:16 +0000 | |
changeset 380 | 86f3d818da1c |
parent 377 | d9b88dbdf5a9 |
child 393 | db01cc79f278 |
permissions | -rw-r--r-- |
51 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
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; |
|
377 | 143 |
gsConsole: KeyPressConsole(event.key.keysym.unicode); |
51 | 144 |
end; |
308 | 145 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
146 |
cHasFocus:= event.active.gain = 1; |
|
51 | 147 |
SDL_QUITEV: isTerminated:= true |
148 |
end; |
|
149 |
CurrTime:= SDL_GetTicks; |
|
150 |
if PrevTime + cTimerInterval <= CurrTime then |
|
151 |
begin |
|
152 |
DoTimer(CurrTime - PrevTime); |
|
153 |
PrevTime:= CurrTime |
|
154 |
end else {sleep(1)}; |
|
155 |
IPCCheckSock |
|
156 |
until isTerminated |
|
157 |
end; |
|
158 |
||
159 |
//////////////////// |
|
160 |
procedure GetParams; |
|
371 | 161 |
var c: LongInt; |
267 | 162 |
{$IFDEF DEBUGFILE} |
371 | 163 |
i: LongInt; |
267 | 164 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
165 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
166 |
begin |
51 | 167 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
168 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 169 |
for i:= 0 to ParamCount do |
170 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
171 |
{$ENDIF} |
|
267 | 172 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
173 |
case ParamCount of |
296 | 174 |
11: begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
175 |
val(ParamStr(1), cScreenWidth, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
176 |
val(ParamStr(2), cScreenHeight, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
177 |
cBitsStr:= ParamStr(3); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
178 |
val(cBitsStr, cBits, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
179 |
val(ParamStr(4), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
180 |
cFullScreen:= ParamStr(5) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
181 |
isSoundEnabled:= ParamStr(6) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
182 |
cLocaleFName:= ParamStr(7); |
174 | 183 |
val(ParamStr(8), cInitVolume, c); |
296 | 184 |
val(ParamStr(9), cTimerInterval, c); |
185 |
PathPrefix:= ParamStr(10); |
|
186 |
cShowFPS:= ParamStr(11) = '1'; |
|
267 | 187 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
188 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 189 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
190 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
191 |
2: begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
192 |
val(ParamStr(1), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
193 |
GameType:= gmtLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
194 |
if ParamStr(2) <> 'landpreview' then OutError(errmsgShouldntRun, true); |
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 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
197 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
198 |
end |
51 | 199 |
end; |
200 |
||
201 |
procedure ShowMainWindow; |
|
202 |
begin |
|
351 | 203 |
if cFullScreen then ParseCommand('fullscr 1', true) |
204 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
205 |
SDL_ShowCursor(0) |
51 | 206 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
207 |
|
160 | 208 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
209 |
procedure Game; |
205 | 210 |
var s: shortstring; |
51 | 211 |
begin |
212 |
WriteToConsole('Init SDL... '); |
|
213 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
214 |
WriteLnToConsole(msgOK); |
|
377 | 215 |
SDL_EnableUNICODE(1); |
51 | 216 |
|
217 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 218 |
SDLTry(TTF_Init <> -1, true); |
51 | 219 |
WriteLnToConsole(msgOK); |
220 |
||
221 |
ShowMainWindow; |
|
222 |
||
223 |
InitKbdKeyTable; |
|
224 |
InitIPC; |
|
225 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 226 |
|
227 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
228 |
||
229 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 230 |
|
231 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
232 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
233 |
||
51 | 234 |
InitTeams; |
351 | 235 |
ParseCommand('ammstore 91911111111111108', true); |
288 | 236 |
AssignStores; |
51 | 237 |
|
238 |
if isSoundEnabled then InitSound; |
|
239 |
InitWorld; |
|
240 |
||
241 |
StoreInit; |
|
242 |
||
243 |
isDeveloperMode:= false; |
|
244 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
245 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
246 |
'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
|
247 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
248 |
|
51 | 249 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
250 |
end; |
51 | 251 |
|
160 | 252 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
253 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
254 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
255 |
InitIPC; |
159 | 256 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
257 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
258 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
true); |
160 | 260 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
261 |
GenPreview; |
159 | 262 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
263 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 264 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 265 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
266 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
268 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
270 |
//////////////////////////////////////////////////////////////////////////////// |
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 |
begin |
268 | 273 |
WriteLnToConsole('-= HedgeWars 0.8.1 =-'); |
274 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
276 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
277 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
else Game |
51 | 280 |
end. |