author | unc0rr |
Thu, 09 Oct 2008 16:05:01 +0000 | |
changeset 1335 | c795cbc752c1 |
parent 1128 | 9b3f42bf4eef |
child 1524 | 5a652a465559 |
permissions | -rw-r--r-- |
498 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy 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 |
uses |
|
25 |
SDLh, |
|
753 | 26 |
GL, |
51 | 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', |
|
802
ed5450a89b96
Start implementing 'visual gears' - gears, that don't need to be synchronized (clouds and flakes)
unc0rr
parents:
771
diff
changeset
|
34 |
uVisualGears in 'uVisualGears.pas', |
51 | 35 |
uConsole in 'uConsole.pas', |
36 |
uKeys in 'uKeys.pas', |
|
37 |
uTeams in 'uTeams.pas', |
|
38 |
uSound in 'uSound.pas', |
|
39 |
uRandom in 'uRandom.pas', |
|
40 |
uAI in 'uAI.pas', |
|
79 | 41 |
uAIMisc in 'uAIMisc.pas', |
42 |
uAIAmmoTests in 'uAIAmmoTests.pas', |
|
43 |
uAIActions in 'uAIActions.pas', |
|
51 | 44 |
uCollisions in 'uCollisions.pas', |
45 |
uLand in 'uLand.pas', |
|
46 |
uLandTemplates in 'uLandTemplates.pas', |
|
54 | 47 |
uLandObjects in 'uLandObjects.pas', |
80 | 48 |
uLandGraphics in 'uLandGraphics.pas', |
288 | 49 |
uLocale in 'uLocale.pas', |
316 | 50 |
uAmmos in 'uAmmos.pas', |
351 | 51 |
uSHA in 'uSHA.pas', |
814
7fb4417b7bc1
Start implementing better statistics implementation (does nothing yet)
unc0rr
parents:
803
diff
changeset
|
52 |
uFloat in 'uFloat.pas', |
942 | 53 |
uStats in 'uStats.pas', |
54 |
uChat in 'uChat.pas'; |
|
51 | 55 |
|
56 |
{$INCLUDE options.inc} |
|
57 |
||
58 |
// also: GSHandlers.inc |
|
59 |
// CCHandlers.inc |
|
60 |
// HHHandlers.inc |
|
357 | 61 |
// SinTable.inc |
271 | 62 |
// proto.inc |
51 | 63 |
|
64 |
||
65 |
procedure OnDestroy; forward; |
|
66 |
||
67 |
//////////////////////////////// |
|
371 | 68 |
procedure DoTimer(Lag: LongInt); |
1080 | 69 |
var s: string; |
51 | 70 |
begin |
564 | 71 |
inc(RealTicks, Lag); |
72 |
||
51 | 73 |
case GameState of |
74 |
gsLandGen: begin |
|
75 |
GenMap; |
|
76 |
GameState:= gsStart; |
|
77 |
end; |
|
78 |
gsStart: begin |
|
949 | 79 |
AddClouds; |
51 | 80 |
AssignHHCoords; |
81 |
AddMiscGears; |
|
82 |
StoreLoad; |
|
83 |
ResetKbd; |
|
84 |
SoundLoad; |
|
72 | 85 |
if GameType = gmtSave then |
86 |
begin |
|
87 |
isSEBackup:= isSoundEnabled; |
|
88 |
isSoundEnabled:= false |
|
89 |
end; |
|
510 | 90 |
FinishProgress; |
1097 | 91 |
PlayMusic; |
51 | 92 |
GameState:= gsGame |
93 |
end; |
|
1022 | 94 |
gsConfirm, |
947 | 95 |
gsGame: begin |
167 | 96 |
ProcessKbd; |
51 | 97 |
DoGameTick(Lag); |
803 | 98 |
ProcessVisualGears(Lag); |
956 | 99 |
DrawWorld(Lag); |
51 | 100 |
end; |
947 | 101 |
gsChat: begin |
102 |
DoGameTick(Lag); |
|
103 |
ProcessVisualGears(Lag); |
|
956 | 104 |
DrawWorld(Lag); |
947 | 105 |
end; |
106 |
gsExit: begin |
|
51 | 107 |
OnDestroy; |
108 |
end; |
|
109 |
end; |
|
564 | 110 |
|
753 | 111 |
SDL_GL_SwapBuffers(); |
51 | 112 |
if flagMakeCapture then |
113 |
begin |
|
1080 | 114 |
flagMakeCapture:= false; |
115 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.tga'; |
|
51 | 116 |
WriteLnToConsole('Saving ' + s); |
1080 | 117 |
MakeScreenshot(s); |
118 |
// SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1) |
|
119 |
end; |
|
51 | 120 |
end; |
121 |
||
122 |
//////////////////// |
|
79 | 123 |
procedure OnDestroy; |
51 | 124 |
begin |
125 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
126 |
if isSoundEnabled then ReleaseSound; |
|
619 | 127 |
StoreRelease; |
128 |
SendKB; |
|
51 | 129 |
CloseIPC; |
130 |
TTF_Quit; |
|
131 |
SDL_Quit; |
|
132 |
halt |
|
133 |
end; |
|
134 |
||
1120 | 135 |
//////////////////////////////// |
136 |
procedure Resize(w, h: LongInt); |
|
137 |
begin |
|
138 |
cScreenWidth:= w; |
|
139 |
cScreenHeight:= h; |
|
140 |
if cFullScreen then |
|
141 |
ParseCommand('/fullscr 1', true) |
|
142 |
else |
|
143 |
ParseCommand('/fullscr 0', true); |
|
144 |
end; |
|
145 |
||
51 | 146 |
/////////////////// |
147 |
procedure MainLoop; |
|
148 |
var PrevTime, |
|
188 | 149 |
CurrTime: Longword; |
51 | 150 |
event: TSDL_Event; |
151 |
begin |
|
152 |
PrevTime:= SDL_GetTicks; |
|
153 |
repeat |
|
154 |
while SDL_PollEvent(@event) <> 0 do |
|
1120 | 155 |
case event.type_ of |
156 |
SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode); |
|
157 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
|
158 |
cHasFocus:= event.active.gain = 1; |
|
1127 | 159 |
//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450)); |
1120 | 160 |
SDL_QUITEV: isTerminated:= true |
161 |
end; |
|
51 | 162 |
CurrTime:= SDL_GetTicks; |
163 |
if PrevTime + cTimerInterval <= CurrTime then |
|
164 |
begin |
|
165 |
DoTimer(CurrTime - PrevTime); |
|
166 |
PrevTime:= CurrTime |
|
434 | 167 |
end else SDL_Delay(1); |
51 | 168 |
IPCCheckSock |
169 |
until isTerminated |
|
170 |
end; |
|
171 |
||
172 |
//////////////////// |
|
173 |
procedure GetParams; |
|
889 | 174 |
var |
267 | 175 |
{$IFDEF DEBUGFILE} |
371 | 176 |
i: LongInt; |
267 | 177 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
178 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
179 |
begin |
51 | 180 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
181 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 182 |
for i:= 0 to ParamCount do |
183 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
184 |
{$ENDIF} |
|
267 | 185 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
186 |
case ParamCount of |
1128 | 187 |
15: begin |
498 | 188 |
val(ParamStr(2), cScreenWidth); |
189 |
val(ParamStr(3), cScreenHeight); |
|
1121
d595dc56b4f3
Remember initial resolution settings to use when switching to fullscreen mode
unc0rr
parents:
1120
diff
changeset
|
190 |
cInitWidth:= cScreenWidth; |
d595dc56b4f3
Remember initial resolution settings to use when switching to fullscreen mode
unc0rr
parents:
1120
diff
changeset
|
191 |
cInitHeight:= cScreenHeight; |
497 | 192 |
cBitsStr:= ParamStr(4); |
498 | 193 |
val(cBitsStr, cBits); |
194 |
val(ParamStr(5), ipcPort); |
|
497 | 195 |
cFullScreen:= ParamStr(6) = '1'; |
196 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
197 |
cLocaleFName:= ParamStr(8); |
|
498 | 198 |
val(ParamStr(9), cInitVolume); |
199 |
val(ParamStr(10), cTimerInterval); |
|
497 | 200 |
PathPrefix:= ParamStr(11); |
201 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 202 |
cAltDamage:= ParamStr(13) = '1'; |
949 | 203 |
UserNick:= DecodeBase64(ParamStr(14)); |
1128 | 204 |
isMusicEnabled:= ParamStr(15) = '1'; |
267 | 205 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
949 | 206 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p] |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
207 |
end; |
497 | 208 |
3: begin |
498 | 209 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
210 |
GameType:= gmtLandPreview; |
497 | 211 |
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
|
212 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
213 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
214 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
215 |
end |
51 | 216 |
end; |
217 |
||
218 |
procedure ShowMainWindow; |
|
219 |
begin |
|
351 | 220 |
if cFullScreen then ParseCommand('fullscr 1', true) |
221 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
222 |
SDL_ShowCursor(0) |
51 | 223 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
224 |
|
160 | 225 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
226 |
procedure Game; |
205 | 227 |
var s: shortstring; |
51 | 228 |
begin |
229 |
WriteToConsole('Init SDL... '); |
|
230 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
231 |
WriteLnToConsole(msgOK); |
|
753 | 232 |
|
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 |
907 | 291 |
WriteLnToConsole('-= Hedgewars ' + cVersionString + ' =-'); |
720 | 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. |