author | unc0rr |
Fri, 22 Sep 2006 18:09:26 +0000 | |
changeset 160 | 207f520b9e83 |
parent 159 | 63909aecb0ed |
child 161 | d8870bbf960e |
permissions | -rw-r--r-- |
51 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2004, 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* Distributed under the terms of the BSD-modified licence: |
|
6 |
* |
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 |
* of this software and associated documentation files (the "Software"), to deal |
|
9 |
* with the Software without restriction, including without limitation the |
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is |
|
12 |
* furnished to do so, subject to the following conditions: |
|
13 |
* |
|
14 |
* 1. Redistributions of source code must retain the above copyright notice, |
|
15 |
* this list of conditions and the following disclaimer. |
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
|
17 |
* this list of conditions and the following disclaimer in the documentation |
|
18 |
* and/or other materials provided with the distribution. |
|
19 |
* 3. The name of the author may not be used to endorse or promote products |
|
20 |
* derived from this software without specific prior written permission. |
|
21 |
* |
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32 |
*) |
|
33 |
||
34 |
program hwengine; |
|
35 |
{$APPTYPE CONSOLE} |
|
36 |
uses |
|
37 |
SDLh, |
|
38 |
uConsts in 'uConsts.pas', |
|
39 |
uGame in 'uGame.pas', |
|
40 |
uMisc in 'uMisc.pas', |
|
41 |
uStore in 'uStore.pas', |
|
42 |
uWorld in 'uWorld.pas', |
|
43 |
uIO in 'uIO.pas', |
|
44 |
uGears in 'uGears.pas', |
|
45 |
uConsole in 'uConsole.pas', |
|
46 |
uKeys in 'uKeys.pas', |
|
47 |
uTeams in 'uTeams.pas', |
|
48 |
uSound in 'uSound.pas', |
|
49 |
uRandom in 'uRandom.pas', |
|
50 |
uAI in 'uAI.pas', |
|
79 | 51 |
uAIMisc in 'uAIMisc.pas', |
52 |
uAIAmmoTests in 'uAIAmmoTests.pas', |
|
53 |
uAIActions in 'uAIActions.pas', |
|
51 | 54 |
uCollisions in 'uCollisions.pas', |
55 |
uLand in 'uLand.pas', |
|
56 |
uLandTemplates in 'uLandTemplates.pas', |
|
54 | 57 |
uLandObjects in 'uLandObjects.pas', |
80 | 58 |
uLandGraphics in 'uLandGraphics.pas', |
59 |
uLocale in 'uLocale.pas'; |
|
51 | 60 |
|
61 |
{$INCLUDE options.inc} |
|
62 |
||
63 |
// also: GSHandlers.inc |
|
64 |
// CCHandlers.inc |
|
65 |
// HHHandlers.inc |
|
66 |
||
67 |
||
68 |
procedure OnDestroy; forward; |
|
69 |
||
70 |
//////////////////////////////// |
|
79 | 71 |
procedure DoTimer(Lag: integer); |
51 | 72 |
var s: string; |
73 |
begin |
|
74 |
case GameState of |
|
75 |
gsLandGen: begin |
|
76 |
GenMap; |
|
77 |
GameState:= gsStart; |
|
78 |
end; |
|
79 |
gsStart: begin |
|
80 |
AssignHHCoords; |
|
81 |
AddMiscGears; |
|
82 |
AdjustColor(cColorNearBlack); |
|
56 | 83 |
AdjustColor(cWaterColor); |
51 | 84 |
AdjustColor(cWhiteColor); |
85 |
StoreLoad; |
|
86 |
AdjustColor(cConsoleSplitterColor); |
|
87 |
ResetKbd; |
|
88 |
SoundLoad; |
|
89 |
PlayMusic; |
|
72 | 90 |
if GameType = gmtSave then |
91 |
begin |
|
92 |
isSEBackup:= isSoundEnabled; |
|
93 |
isSoundEnabled:= false |
|
94 |
end; |
|
51 | 95 |
GameState:= gsGame |
96 |
end; |
|
97 |
gsGame : begin |
|
98 |
ProcessKbd; |
|
99 |
DoGameTick(Lag); |
|
100 |
DrawWorld(Lag, SDLPrimSurface); |
|
101 |
end; |
|
102 |
gsConsole: begin |
|
103 |
DoGameTick(Lag); |
|
104 |
DrawWorld(Lag, SDLPrimSurface); |
|
105 |
DrawConsole(SDLPrimSurface); |
|
106 |
end; |
|
107 |
gsExit : begin |
|
108 |
OnDestroy; |
|
109 |
end; |
|
110 |
end; |
|
111 |
SDL_Flip(SDLPrimSurface); |
|
112 |
if flagMakeCapture then |
|
113 |
begin |
|
114 |
flagMakeCapture:= false; |
|
81 | 115 |
s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.bmp'; |
51 | 116 |
WriteLnToConsole('Saving ' + s); |
117 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(PChar(s), 'wb'), 1) |
|
118 |
end; |
|
119 |
end; |
|
120 |
||
121 |
//////////////////// |
|
79 | 122 |
procedure OnDestroy; |
51 | 123 |
begin |
124 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF} |
|
125 |
if isSoundEnabled then ReleaseSound; |
|
126 |
StoreRelease; |
|
127 |
CloseIPC; |
|
128 |
TTF_Quit; |
|
129 |
SDL_Quit; |
|
130 |
halt |
|
131 |
end; |
|
132 |
||
133 |
/////////////////// |
|
134 |
procedure MainLoop; |
|
135 |
var PrevTime, |
|
136 |
CurrTime: Cardinal; |
|
137 |
event: TSDL_Event; |
|
138 |
begin |
|
139 |
PrevTime:= SDL_GetTicks; |
|
140 |
repeat |
|
141 |
while SDL_PollEvent(@event) <> 0 do |
|
142 |
case event.type_ of |
|
143 |
SDL_KEYDOWN: case GameState of |
|
144 |
gsGame: if event.key.keysym.sym = 96 then |
|
145 |
begin |
|
146 |
cConsoleYAdd:= cConsoleHeight; |
|
147 |
GameState:= gsConsole |
|
148 |
end; |
|
149 |
gsConsole: KeyPressConsole(event.key.keysym.sym); |
|
150 |
end; |
|
151 |
SDL_QUITEV: isTerminated:= true |
|
152 |
end; |
|
153 |
CurrTime:= SDL_GetTicks; |
|
154 |
if PrevTime + cTimerInterval <= CurrTime then |
|
155 |
begin |
|
156 |
DoTimer(CurrTime - PrevTime); |
|
157 |
PrevTime:= CurrTime |
|
158 |
end else {sleep(1)}; |
|
159 |
IPCCheckSock |
|
160 |
until isTerminated |
|
161 |
end; |
|
162 |
||
163 |
//////////////////// |
|
164 |
procedure GetParams; |
|
165 |
var c: integer; |
|
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
166 |
i: integer; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
167 |
p: TPathType; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
168 |
begin |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
169 |
PathPrefix:= ParamStr(0); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
170 |
for i:= 1 to Length(PathPrefix) do |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
171 |
if PathPrefix[i] = '\' then PathPrefix[i]:= '/'; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
172 |
i:= Length(PathPrefix); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
173 |
while (i > 0) and not (PathPrefix[i] = '/') do dec(i); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
174 |
Delete(PathPrefix, i, Length(PathPrefix) - i + 1); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
175 |
dec(i); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
176 |
while (i > 0) and not (PathPrefix[i] = '/') do dec(i); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
177 |
Delete(PathPrefix, i, Length(PathPrefix) - i + 1); |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
178 |
PathPrefix:= PathPrefix + '/share/hedgewars/'; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
179 |
for p:= Low(TPathType) to High(TPathType) do |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
180 |
if p <> ptMapCurrent then Pathz[p]:= PathPrefix + Pathz[p]; |
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
181 |
|
51 | 182 |
{$IFDEF DEBUGFILE} |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
81
diff
changeset
|
183 |
AddFileLog('Prefix: "' + PathPrefix +'"'); |
51 | 184 |
for i:= 0 to ParamCount do |
185 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
|
186 |
{$ENDIF} |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
187 |
case ParamCount of |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
188 |
7: begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
189 |
val(ParamStr(1), cScreenWidth, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
190 |
val(ParamStr(2), cScreenHeight, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
191 |
cBitsStr:= ParamStr(3); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
192 |
val(cBitsStr, cBits, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
193 |
val(ParamStr(4), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
194 |
cFullScreen:= ParamStr(5) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
195 |
isSoundEnabled:= ParamStr(6) = '1'; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
196 |
cLocaleFName:= ParamStr(7); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
197 |
end; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
198 |
2: begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
199 |
val(ParamStr(1), ipcPort, c); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
200 |
GameType:= gmtLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
201 |
if ParamStr(2) <> 'landpreview' then OutError(errmsgShouldntRun, true); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
202 |
end |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
203 |
else |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
204 |
OutError(errmsgShouldntRun, true) |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
205 |
end |
51 | 206 |
end; |
207 |
||
208 |
procedure ShowMainWindow; |
|
209 |
var flags: Longword; |
|
210 |
begin |
|
211 |
flags:= SDL_HWSURFACE or SDL_DOUBLEBUF or SDL_HWACCEL; |
|
212 |
if cFullScreen then flags:= flags or SDL_FULLSCREEN |
|
213 |
else SDL_WM_SetCaption('Hedgewars', nil); |
|
214 |
SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, cBits, flags); |
|
215 |
TryDo(SDLPrimSurface <> nil, errmsgCreateSurface, true); |
|
216 |
PixelFormat:= SDLPrimSurface.format; |
|
217 |
SDL_ShowCursor(0); |
|
218 |
end; |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
219 |
|
160 | 220 |
/////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
221 |
procedure Game; |
51 | 222 |
begin |
223 |
WriteToConsole('Init SDL... '); |
|
224 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true); |
|
225 |
WriteLnToConsole(msgOK); |
|
226 |
||
227 |
WriteToConsole('Init SDL_ttf... '); |
|
228 |
SDLTry(TTF_Init >= 0, true); |
|
229 |
WriteLnToConsole(msgOK); |
|
230 |
||
231 |
ShowMainWindow; |
|
232 |
||
233 |
InitKbdKeyTable; |
|
234 |
InitIPC; |
|
235 |
WriteLnToConsole(msgGettingConfig); |
|
80 | 236 |
|
237 |
LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName); |
|
238 |
||
239 |
SendIPCAndWaitReply('C'); // ask for game config |
|
51 | 240 |
InitTeams; |
241 |
||
242 |
if isSoundEnabled then InitSound; |
|
243 |
InitWorld; |
|
244 |
||
245 |
StoreInit; |
|
246 |
||
247 |
isDeveloperMode:= false; |
|
248 |
||
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
249 |
TryDo(InitStepsFlags = cifAllInited, |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
250 |
'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
|
251 |
true); |
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
54
diff
changeset
|
252 |
|
51 | 253 |
MainLoop |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
254 |
end; |
51 | 255 |
|
160 | 256 |
///////////////////////// |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
257 |
procedure GenLandPreview; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
258 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
259 |
InitIPC; |
159 | 260 |
IPCWaitPongEvent; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
261 |
TryDo(InitStepsFlags = cifRandomize, |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
262 |
'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
263 |
true); |
160 | 264 |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
265 |
GenPreview; |
159 | 266 |
WriteLnToConsole('Sending preview...'); |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
267 |
SendIPCRaw(@Preview, sizeof(Preview)); |
159 | 268 |
WriteLnToConsole('Preview sent, disconnect'); |
158 | 269 |
CloseIPC |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
270 |
end; |
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 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
273 |
/////////////////////////////// m a i n //////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
274 |
//////////////////////////////////////////////////////////////////////////////// |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
275 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
276 |
begin |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
277 |
WriteLnToConsole('-= HedgeWars 0.2 =-'); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
278 |
WriteLnToConsole(' -= by unC0Rr =- '); |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
279 |
GetParams; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
280 |
Randomize; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
281 |
|
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
282 |
if GameType = gmtLandPreview then GenLandPreview |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
109
diff
changeset
|
283 |
else Game |
51 | 284 |
end. |