author | unc0rr |
Sun, 10 Dec 2006 15:55:53 +0000 | |
changeset 295 | 8834f3cb620e |
parent 293 | 00a532e8808e |
child 296 | 607912ccc5af |
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 |
267 | 165 |
9: 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); |
267 | 175 |
PathPrefix:= ParamStr(9); |
176 |
for p:= Succ(Low(TPathType)) to High(TPathType) do |
|
177 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p]; |
|
293 | 178 |
AddClouds |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
179 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
180 |
2: begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
181 |
val(ParamStr(1), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
182 |
GameType:= gmtLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
183 |
if ParamStr(2) <> 'landpreview' then OutError(errmsgShouldntRun, true); |
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 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
186 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
187 |
end |
51 | 188 |
end; |
189 |
||
190 |
procedure ShowMainWindow; |
|
191 |
begin |
|
166
2920ab2bf329
Switching between fullscreen and windowed modes on 'F' key
unc0rr
parents:
161
diff
changeset
|
192 |
if cFullScreen then ParseCommand('fullscr 1') |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
193 |
else ParseCommand('fullscr 0'); |
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
271
diff
changeset
|
194 |
SDL_ShowCursor(0) |
51 | 195 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
196 |
|
160 | 197 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
198 |
procedure Game; |
205 | 199 |
var s: shortstring; |
51 | 200 |
begin |
201 |
WriteToConsole('Init SDL... '); |
|
202 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
203 |
WriteLnToConsole(msgOK); |
|
204 |
||
205 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 206 |
SDLTry(TTF_Init <> -1, true); |
51 | 207 |
WriteLnToConsole(msgOK); |
208 |
||
209 |
ShowMainWindow; |
|
210 |
||
211 |
InitKbdKeyTable; |
|
212 |
InitIPC; |
|
213 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 214 |
|
215 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
216 |
||
217 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 218 |
|
219 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
220 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
221 |
||
51 | 222 |
InitTeams; |
295 | 223 |
ParseCommand('ammstore 9191111111111110'); |
288 | 224 |
AssignStores; |
51 | 225 |
|
226 |
if isSoundEnabled then InitSound; |
|
227 |
InitWorld; |
|
228 |
||
229 |
StoreInit; |
|
230 |
||
231 |
isDeveloperMode:= false; |
|
232 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
233 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
234 |
'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
|
235 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
236 |
|
51 | 237 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
238 |
end; |
51 | 239 |
|
160 | 240 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
241 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
242 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
243 |
InitIPC; |
159 | 244 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
245 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
246 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
247 |
true); |
160 | 248 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
249 |
GenPreview; |
159 | 250 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
251 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 252 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 253 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
254 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
255 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
256 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
257 |
/////////////////////////////// m a i n //////////////////////////////////////// |
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 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
260 |
begin |
268 | 261 |
WriteLnToConsole('-= HedgeWars 0.8.1 =-'); |
262 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
263 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
264 |
Randomize; |
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 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
else Game |
51 | 268 |
end. |