author | unc0rr |
Fri, 25 Jul 2008 19:26:44 +0000 | |
changeset 1097 | 06b15817b8a0 |
parent 1080 | 8735046fc698 |
child 1120 | eb5a9f86f9c6 |
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 |
||
135 |
/////////////////// |
|
136 |
procedure MainLoop; |
|
137 |
var PrevTime, |
|
188 | 138 |
CurrTime: Longword; |
51 | 139 |
event: TSDL_Event; |
140 |
begin |
|
141 |
PrevTime:= SDL_GetTicks; |
|
142 |
repeat |
|
143 |
while SDL_PollEvent(@event) <> 0 do |
|
144 |
case event.type_ of |
|
946 | 145 |
SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode); |
308 | 146 |
SDL_ACTIVEEVENT: if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then |
147 |
cHasFocus:= event.active.gain = 1; |
|
51 | 148 |
SDL_QUITEV: isTerminated:= true |
149 |
end; |
|
150 |
CurrTime:= SDL_GetTicks; |
|
151 |
if PrevTime + cTimerInterval <= CurrTime then |
|
152 |
begin |
|
153 |
DoTimer(CurrTime - PrevTime); |
|
154 |
PrevTime:= CurrTime |
|
434 | 155 |
end else SDL_Delay(1); |
51 | 156 |
IPCCheckSock |
157 |
until isTerminated |
|
158 |
end; |
|
159 |
||
160 |
//////////////////// |
|
161 |
procedure GetParams; |
|
889 | 162 |
var |
267 | 163 |
{$IFDEF DEBUGFILE} |
371 | 164 |
i: LongInt; |
267 | 165 |
{$ENDIF} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
166 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
167 |
begin |
51 | 168 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
169 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 170 |
for i:= 0 to ParamCount do |
171 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
172 |
{$ENDIF} |
|
267 | 173 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
174 |
case ParamCount of |
949 | 175 |
14: begin |
498 | 176 |
val(ParamStr(2), cScreenWidth); |
177 |
val(ParamStr(3), cScreenHeight); |
|
497 | 178 |
cBitsStr:= ParamStr(4); |
498 | 179 |
val(cBitsStr, cBits); |
180 |
val(ParamStr(5), ipcPort); |
|
497 | 181 |
cFullScreen:= ParamStr(6) = '1'; |
182 |
isSoundEnabled:= ParamStr(7) = '1'; |
|
183 |
cLocaleFName:= ParamStr(8); |
|
498 | 184 |
val(ParamStr(9), cInitVolume); |
185 |
val(ParamStr(10), cTimerInterval); |
|
497 | 186 |
PathPrefix:= ParamStr(11); |
187 |
cShowFPS:= ParamStr(12) = '1'; |
|
529 | 188 |
cAltDamage:= ParamStr(13) = '1'; |
949 | 189 |
UserNick:= DecodeBase64(ParamStr(14)); |
267 | 190 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
949 | 191 |
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
|
192 |
end; |
497 | 193 |
3: begin |
498 | 194 |
val(ParamStr(2), ipcPort); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
195 |
GameType:= gmtLandPreview; |
497 | 196 |
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
|
197 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
198 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
199 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
200 |
end |
51 | 201 |
end; |
202 |
||
203 |
procedure ShowMainWindow; |
|
204 |
begin |
|
351 | 205 |
if cFullScreen then ParseCommand('fullscr 1', true) |
206 |
else ParseCommand('fullscr 0', true); |
|
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
207 |
SDL_ShowCursor(0) |
51 | 208 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
209 |
|
160 | 210 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
211 |
procedure Game; |
205 | 212 |
var s: shortstring; |
51 | 213 |
begin |
214 |
WriteToConsole('Init SDL... '); |
|
215 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
216 |
WriteLnToConsole(msgOK); |
|
753 | 217 |
|
377 | 218 |
SDL_EnableUNICODE(1); |
51 | 219 |
|
220 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 221 |
SDLTry(TTF_Init <> -1, true); |
51 | 222 |
WriteLnToConsole(msgOK); |
223 |
||
224 |
ShowMainWindow; |
|
225 |
||
226 |
InitKbdKeyTable; |
|
227 |
InitIPC; |
|
228 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 229 |
|
230 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
231 |
||
232 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 233 |
|
234 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
235 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
236 |
||
51 | 237 |
InitTeams; |
288 | 238 |
AssignStores; |
51 | 239 |
|
240 |
if isSoundEnabled then InitSound; |
|
241 |
InitWorld; |
|
242 |
||
243 |
StoreInit; |
|
244 |
||
245 |
isDeveloperMode:= false; |
|
246 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
247 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
248 |
'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
|
249 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
250 |
|
51 | 251 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
252 |
end; |
51 | 253 |
|
160 | 254 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
255 |
procedure GenLandPreview; |
566 | 256 |
var Preview: TPreview; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
257 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
258 |
InitIPC; |
159 | 259 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
260 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
261 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
262 |
true); |
160 | 263 |
|
566 | 264 |
Preview:= GenPreview; |
159 | 265 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
266 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 267 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 268 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
end; |
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 |
/////////////////////////////// m a i n //////////////////////////////////////// |
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 |
begin |
907 | 276 |
WriteLnToConsole('-= Hedgewars ' + cVersionString + ' =-'); |
720 | 277 |
WriteLnToConsole(' -= by unC0Rr =- '); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
280 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
281 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
282 |
else Game |
51 | 283 |
end. |