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