author | unc0rr |
Sun, 26 Nov 2006 20:33:46 +0000 | |
changeset 279 | 5df0ab96b60a |
parent 271 | f2f9a3d5b441 |
child 281 | 5b483aa9f2ab |
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') |
2920ab2bf329
Switching between fullscreen and windowed modes on 'F' key
unc0rr
parents:
161
diff
changeset
|
191 |
else ParseCommand('fullscr 0') |
51 | 192 |
end; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
193 |
|
160 | 194 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
195 |
procedure Game; |
205 | 196 |
var s: shortstring; |
51 | 197 |
begin |
198 |
WriteToConsole('Init SDL... '); |
|
199 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
200 |
WriteLnToConsole(msgOK); |
|
201 |
||
202 |
WriteToConsole('Init SDL_ttf... '); |
|
200 | 203 |
SDLTry(TTF_Init <> -1, true); |
51 | 204 |
WriteLnToConsole(msgOK); |
205 |
||
206 |
ShowMainWindow; |
|
207 |
||
208 |
InitKbdKeyTable; |
|
209 |
InitIPC; |
|
210 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 211 |
|
212 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
213 |
||
214 |
SendIPCAndWaitReply('C'); // ask for game config |
|
205 | 215 |
|
216 |
s:= 'eproto ' + inttostr(cNetProtoVersion); |
|
217 |
SendIPCRaw(@s[0], Length(s) + 1); // send proto version |
|
218 |
||
51 | 219 |
InitTeams; |
220 |
||
221 |
if isSoundEnabled then InitSound; |
|
222 |
InitWorld; |
|
223 |
||
224 |
StoreInit; |
|
225 |
||
226 |
isDeveloperMode:= false; |
|
227 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
228 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
229 |
'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
|
230 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
231 |
|
51 | 232 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
233 |
end; |
51 | 234 |
|
160 | 235 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
236 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
237 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
238 |
InitIPC; |
159 | 239 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
240 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
241 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
242 |
true); |
160 | 243 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
244 |
GenPreview; |
159 | 245 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
246 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 247 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 248 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
249 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
250 |
|
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 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
253 |
//////////////////////////////////////////////////////////////////////////////// |
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 |
begin |
268 | 256 |
WriteLnToConsole('-= HedgeWars 0.8.1 =-'); |
257 |
WriteLnToConsole(' -= by unC0Rr =- '); |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
258 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
Randomize; |
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 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
262 |
else Game |
51 | 263 |
end. |