4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 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 hedgewars;
|
|
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',
|
|
51 |
uAIActions in 'uAIActions.pas',
|
|
52 |
uAIMisc in 'uAIMisc.pas',
|
|
53 |
uAIAmmoTests in 'uAIAmmoTests.pas',
|
|
54 |
uCollisions in 'uCollisions.pas',
|
|
55 |
uLand in 'uLand.pas';
|
|
56 |
|
|
57 |
{$INCLUDE options.inc}
|
|
58 |
|
|
59 |
// also: GSHandlers.inc
|
|
60 |
// CCHandlers.inc
|
|
61 |
// HHHandlers.inc
|
|
62 |
|
|
63 |
|
|
64 |
procedure OnDestroy; forward;
|
|
65 |
|
|
66 |
////////////////////////////////
|
|
67 |
procedure DoTimer(Lag: integer); // - обработка таймера
|
|
68 |
const cCons: boolean = false;
|
|
69 |
var s: string;
|
|
70 |
begin
|
|
71 |
case GameState of
|
|
72 |
gsLandGen: begin
|
|
73 |
if (GameFlags and gfForts) = 0 then GenLandSurface
|
|
74 |
else MakeFortsMap;
|
|
75 |
GameState:= gsStart;
|
|
76 |
end;
|
|
77 |
gsStart: begin
|
|
78 |
AssignHHCoords;
|
10
|
79 |
AddMiscGears;
|
4
|
80 |
AdjustColor(cColorNearBlack);
|
|
81 |
AdjustColor(cWhiteColor);
|
|
82 |
StoreLoad;
|
|
83 |
AdjustColor(cConsoleSplitterColor);
|
|
84 |
ResetKbd;
|
|
85 |
SoundLoad;
|
|
86 |
PlayMusic;
|
|
87 |
GameState:= gsGame
|
|
88 |
end;
|
|
89 |
gsGame : begin
|
|
90 |
ProcessKbd;
|
|
91 |
DoGameTick(Lag);
|
|
92 |
DrawWorld(Lag, SDLPrimSurface);
|
|
93 |
end;
|
|
94 |
gsConsole: begin
|
|
95 |
DoGameTick(Lag);
|
|
96 |
DrawWorld(Lag, SDLPrimSurface);
|
|
97 |
DrawConsole(SDLPrimSurface);
|
|
98 |
end;
|
|
99 |
gsExit : begin
|
|
100 |
OnDestroy;
|
|
101 |
end;
|
|
102 |
end;
|
|
103 |
SDL_Flip(SDLPrimSurface);
|
|
104 |
if flagMakeCapture then
|
|
105 |
begin
|
|
106 |
flagMakeCapture:= false;
|
|
107 |
s:= 'hw_' + ParamStr(5) + '_' + inttostr(GameTicks) + '.bmp';
|
|
108 |
WriteLnToConsole('Saving ' + s);
|
|
109 |
SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(PChar(s), 'wb'), 1)
|
|
110 |
end;
|
|
111 |
end;
|
|
112 |
|
|
113 |
////////////////////
|
|
114 |
procedure OnDestroy; // - очищаем память
|
|
115 |
begin
|
|
116 |
{$IFDEF DEBUGFILE}AddFileLog('Freeing resources...');{$ENDIF}
|
|
117 |
if isSoundEnabled then ReleaseSound;
|
|
118 |
StoreRelease;
|
|
119 |
CloseIPC;
|
|
120 |
TTF_Quit;
|
|
121 |
SDL_Quit;
|
|
122 |
halt
|
|
123 |
end;
|
|
124 |
|
|
125 |
///////////////////
|
|
126 |
procedure MainLoop;
|
|
127 |
var PrevTime,
|
|
128 |
CurrTime: Cardinal;
|
|
129 |
event: TSDL_Event;
|
|
130 |
begin
|
|
131 |
PrevTime:= SDL_GetTicks;
|
|
132 |
repeat
|
|
133 |
while SDL_PollEvent(@event) <> 0 do
|
|
134 |
case event.type_ of
|
|
135 |
SDL_KEYDOWN: case GameState of
|
|
136 |
gsGame: if event.key.keysym.sym = 96 then
|
|
137 |
begin
|
|
138 |
cConsoleYAdd:= cConsoleHeight;
|
|
139 |
GameState:= gsConsole
|
|
140 |
end;
|
|
141 |
gsConsole: KeyPressConsole(event.key.keysym.sym);
|
|
142 |
end;
|
|
143 |
SDL_QUITEV: isTerminated:= true
|
|
144 |
end;
|
|
145 |
CurrTime:= SDL_GetTicks;
|
|
146 |
if PrevTime + cTimerInterval <= CurrTime then
|
|
147 |
begin
|
|
148 |
DoTimer(CurrTime - PrevTime);
|
|
149 |
PrevTime:= CurrTime
|
|
150 |
end else {sleep(1)};
|
|
151 |
IPCCheckSock
|
|
152 |
until isTerminated
|
|
153 |
end;
|
|
154 |
|
|
155 |
////////////////////
|
|
156 |
procedure GetParams;
|
|
157 |
var c: integer;
|
|
158 |
{$IFDEF DEBUGFILE}
|
|
159 |
i: integer;
|
|
160 |
begin
|
|
161 |
for i:= 0 to ParamCount do
|
|
162 |
AddFileLog(inttostr(i) + ': ' + ParamStr(i));
|
|
163 |
{$ELSE}
|
|
164 |
begin
|
|
165 |
{$ENDIF}
|
|
166 |
if ParamCount=6 then
|
|
167 |
begin
|
|
168 |
//TODO: сделать передачу через IPC
|
|
169 |
val(ParamStr(1), cScreenWidth, c);
|
|
170 |
val(ParamStr(2), cScreenHeight, c);
|
|
171 |
Pathz[ptThemeCurrent]:= Pathz[ptThemes] + ParamStr(3)+'/';
|
|
172 |
val(ParamStr(4), ipcPort, c);
|
|
173 |
SetRandomParams(ParamStr(5), rndfillstr);
|
|
174 |
cFullScreen:= ParamStr(6)[1] = '1'
|
|
175 |
end else OutError(errmsgShouldntRun, true);
|
|
176 |
end;
|
|
177 |
|
|
178 |
procedure ShowMainWindow;
|
|
179 |
var flags: Longword;
|
|
180 |
begin
|
|
181 |
flags:= SDL_HWSURFACE or SDL_DOUBLEBUF or SDL_HWACCEL;
|
|
182 |
if cFullScreen then flags:= flags or SDL_FULLSCREEN
|
|
183 |
else SDL_WM_SetCaption('Hedgewars', nil);
|
|
184 |
SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, cBits, flags);
|
|
185 |
TryDo(SDLPrimSurface <> nil, errmsgCreateSurface, true);
|
|
186 |
PixelFormat:= SDLPrimSurface.format;
|
|
187 |
SDL_ShowCursor(0);
|
|
188 |
end;
|
|
189 |
////////////////////////////////////////////////////////////////////////////////
|
|
190 |
/////////////////////////////// m a i n ////////////////////////////////////////
|
|
191 |
////////////////////////////////////////////////////////////////////////////////
|
|
192 |
{$INCLUDE revision.inc}
|
|
193 |
|
|
194 |
begin
|
|
195 |
WriteLnToConsole('HedgeWars 0.1, svn '+cRevision);
|
|
196 |
WriteLnToConsole(' -= by unC0Rr =- ');
|
|
197 |
GetParams;
|
|
198 |
Randomize;
|
|
199 |
|
|
200 |
WriteToConsole('Init SDL... ');
|
|
201 |
SDLTry(SDL_Init(SDL_INIT_VIDEO) >= 0, true);
|
|
202 |
WriteLnToConsole(msgOK);
|
|
203 |
|
|
204 |
WriteToConsole('Init SDL_ttf... ');
|
|
205 |
SDLTry(TTF_Init >= 0, true);
|
|
206 |
WriteLnToConsole(msgOK);
|
|
207 |
|
|
208 |
ShowMainWindow;
|
|
209 |
|
|
210 |
InitKbdKeyTable;
|
|
211 |
InitIPC;
|
|
212 |
WriteLnToConsole(msgGettingConfig);
|
|
213 |
SendIPCAndWaitReply('C'); // запрос конфига игры
|
|
214 |
InitTeams;
|
|
215 |
|
|
216 |
if isSoundEnabled then InitSound;
|
|
217 |
InitWorld;
|
|
218 |
|
|
219 |
StoreInit;
|
|
220 |
|
|
221 |
isDeveloperMode:= false;
|
|
222 |
|
|
223 |
MainLoop
|
|
224 |
|
|
225 |
end.
|