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