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