author | unc0rr |
Tue, 03 Jul 2007 17:03:41 +0000 | |
changeset 550 | f6a18d2bab00 |
parent 529 | 812682c1ab62 |
child 564 | 17fb45c670c1 |
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 |
529 | 180 |
13: 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'; |
|
529 | 193 |
cAltDamage:= ParamStr(13) = '1'; |
267 | 194 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
195 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 196 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
197 |
end; |
497 | 198 |
3: begin |
498 | 199 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
200 |
GameType:= gmtLandPreview; |
497 | 201 |
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
|
202 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
203 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
204 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
205 |
end |
51 | 206 |
end; |
207 |
||
208 |
procedure ShowMainWindow; |
|
209 |
begin |
|
351 | 210 |
if cFullScreen then ParseCommand('fullscr 1', true) |
211 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
212 |
SDL_ShowCursor(0) |
51 | 213 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
214 |
|
160 | 215 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
216 |
procedure Game; |
205 | 217 |
var s: shortstring; |
51 | 218 |
begin |
219 |
WriteToConsole('Init SDL... '); |
|
220 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
221 |
WriteLnToConsole(msgOK); |
|
377 | 222 |
SDL_EnableUNICODE(1); |
51 | 223 |
|
224 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 225 |
SDLTry(TTF_Init <> -1, true); |
51 | 226 |
WriteLnToConsole(msgOK); |
227 |
||
228 |
ShowMainWindow; |
|
229 |
||
230 |
InitKbdKeyTable; |
|
231 |
InitIPC; |
|
232 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 233 |
|
234 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
235 |
||
236 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 237 |
|
238 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
239 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
240 |
||
51 | 241 |
InitTeams; |
288 | 242 |
AssignStores; |
51 | 243 |
|
244 |
if isSoundEnabled then InitSound; |
|
245 |
InitWorld; |
|
246 |
||
247 |
StoreInit; |
|
248 |
||
249 |
isDeveloperMode:= false; |
|
250 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
251 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
252 |
'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
|
253 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
254 |
|
51 | 255 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
256 |
end; |
51 | 257 |
|
160 | 258 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
260 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
261 |
InitIPC; |
159 | 262 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
263 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
264 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
265 |
true); |
160 | 266 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
GenPreview; |
159 | 268 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 270 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 271 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
272 |
end; |
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 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
/////////////////////////////// m a i n //////////////////////////////////////// |
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 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
begin |
426 | 279 |
WriteLnToConsole('-= HedgeWars 0.9 =-'); |
280 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
281 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
282 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
283 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
284 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
285 |
else Game |
51 | 286 |
end. |