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