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