author | unc0rr |
Sun, 10 Dec 2006 18:57:05 +0000 | |
changeset 297 | 279e10ec31b4 |
parent 296 | 607912ccc5af |
child 302 | 7aca131ecd7f |
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; |
|
138 |
SDL_QUITEV: isTerminated:= true |
|
139 |
end; |
|
140 |
CurrTime:= SDL_GetTicks; |
|
141 |
if PrevTime + cTimerInterval <= CurrTime then |
|
142 |
begin |
|
143 |
DoTimer(CurrTime - PrevTime); |
|
144 |
PrevTime:= CurrTime |
|
145 |
end else {sleep(1)}; |
|
146 |
IPCCheckSock |
|
147 |
until isTerminated |
|
148 |
end; |
|
149 |
||
150 |
//////////////////// |
|
151 |
procedure GetParams; |
|
152 |
var c: integer; |
|
267 | 153 |
{$IFDEF DEBUGFILE} |
154 |
i: integer; |
|
155 |
{$ENDIF} |
|
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
156 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
157 |
begin |
51 | 158 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
159 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 160 |
for i:= 0 to ParamCount do |
161 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
162 |
{$ENDIF} |
|
267 | 163 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
164 |
case ParamCount of |
296 | 165 |
11: begin |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
166 |
val(ParamStr(1), cScreenWidth, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
167 |
val(ParamStr(2), cScreenHeight, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
168 |
cBitsStr:= ParamStr(3); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
169 |
val(cBitsStr, cBits, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
170 |
val(ParamStr(4), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
171 |
cFullScreen:= ParamStr(5) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
172 |
isSoundEnabled:= ParamStr(6) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
173 |
cLocaleFName:= ParamStr(7); |
174 | 174 |
val(ParamStr(8), cInitVolume, c); |
296 | 175 |
val(ParamStr(9), cTimerInterval, c); |
176 |
PathPrefix:= ParamStr(10); |
|
177 |
cShowFPS:= ParamStr(11) = '1'; |
|
267 | 178 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
179 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 180 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
181 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
182 |
2: begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
183 |
val(ParamStr(1), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
184 |
GameType:= gmtLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
185 |
if ParamStr(2) <> 'landpreview' then OutError(errmsgShouldntRun, true); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
186 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
187 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
188 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
189 |
end |
51 | 190 |
end; |
191 |
||
192 |
procedure ShowMainWindow; |
|
193 |
begin |
|
166
2920ab2bf329
Switching between fullscreen and windowed modes on 'F' key
unc0rr
parents:
161
diff
changeset
|
194 |
if cFullScreen then ParseCommand('fullscr 1') |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
195 |
else ParseCommand('fullscr 0'); |
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
196 |
SDL_ShowCursor(0) |
51 | 197 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
198 |
|
160 | 199 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
200 |
procedure Game; |
205 | 201 |
var s: shortstring; |
51 | 202 |
begin |
203 |
WriteToConsole('Init SDL... '); |
|
204 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
205 |
WriteLnToConsole(msgOK); |
|
206 |
||
207 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 208 |
SDLTry(TTF_Init <> -1, true); |
51 | 209 |
WriteLnToConsole(msgOK); |
210 |
||
211 |
ShowMainWindow; |
|
212 |
||
213 |
InitKbdKeyTable; |
|
214 |
InitIPC; |
|
215 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 216 |
|
217 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
218 |
||
219 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 220 |
|
221 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
222 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
223 |
||
51 | 224 |
InitTeams; |
295 | 225 |
ParseCommand('ammstore 9191111111111110'); |
288 | 226 |
AssignStores; |
51 | 227 |
|
228 |
if isSoundEnabled then InitSound; |
|
229 |
InitWorld; |
|
230 |
||
231 |
StoreInit; |
|
232 |
||
233 |
isDeveloperMode:= false; |
|
234 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
235 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
236 |
'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
|
237 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
238 |
|
51 | 239 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
240 |
end; |
51 | 241 |
|
160 | 242 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
243 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
244 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
245 |
InitIPC; |
159 | 246 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
247 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
248 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
249 |
true); |
160 | 250 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
251 |
GenPreview; |
159 | 252 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
253 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 254 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 255 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
256 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
257 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
258 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
/////////////////////////////// m a i n //////////////////////////////////////// |
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 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
262 |
begin |
268 | 263 |
WriteLnToConsole('-= HedgeWars 0.8.1 =-'); |
264 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
265 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
266 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
268 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
269 |
else Game |
51 | 270 |
end. |