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