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