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 |
unit uConsole;
|
|
35 |
interface
|
|
36 |
uses SDLh;
|
|
37 |
{$INCLUDE options.inc}
|
|
38 |
const isDeveloperMode: boolean = true;
|
|
39 |
type TVariableType = (vtCommand, vtInteger, vtReal, vtBoolean);
|
|
40 |
TCommandHandler = procedure (var params: shortstring);
|
|
41 |
|
|
42 |
procedure DrawConsole(Surface: PSDL_Surface);
|
|
43 |
procedure WriteToConsole(s: shortstring);
|
|
44 |
procedure WriteLnToConsole(s: shortstring);
|
|
45 |
procedure KeyPressConsole(Key: Longword);
|
|
46 |
procedure ParseCommand(CmdStr: shortstring);
|
|
47 |
procedure AfterAttack; // экспортируется только для вызова из CurrAmmoGear
|
|
48 |
|
|
49 |
implementation
|
|
50 |
{$J+}
|
|
51 |
uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uSound, uWorld, uLand;
|
|
52 |
const cLineWidth: integer = 0;
|
|
53 |
cLinesCount = 256;
|
|
54 |
|
|
55 |
type PVariable = ^TVariable;
|
|
56 |
TVariable = record
|
|
57 |
Next: PVariable;
|
|
58 |
Name: string[15];
|
|
59 |
VType: TVariableType;
|
|
60 |
Handler: pointer;
|
|
61 |
end;
|
|
62 |
|
|
63 |
var ConsoleLines: array[byte] of ShortString;
|
|
64 |
CurrLine: integer = 0;
|
|
65 |
InputStr: shortstring;
|
|
66 |
Variables: PVariable = nil;
|
|
67 |
|
|
68 |
function RegisterVariable(Name: string; VType: TVariableType; p: pointer): PVariable;
|
|
69 |
begin
|
17
|
70 |
New(Result);
|
|
71 |
TryDo(Result <> nil, 'RegisterVariable: Result = nil', true);
|
4
|
72 |
FillChar(Result^, sizeof(TVariable), 0);
|
|
73 |
Result.Name:= Name;
|
|
74 |
Result.VType:= VType;
|
|
75 |
Result.Handler:= p;
|
|
76 |
if Variables = nil then Variables:= Result
|
|
77 |
else begin
|
|
78 |
Result.Next:= Variables;
|
|
79 |
Variables:= Result
|
|
80 |
end
|
|
81 |
end;
|
|
82 |
|
|
83 |
procedure FreeVariablesList;
|
|
84 |
var t, tt: PVariable;
|
|
85 |
begin
|
|
86 |
tt:= Variables;
|
|
87 |
Variables:= nil;
|
|
88 |
while tt<>nil do
|
|
89 |
begin
|
|
90 |
t:= tt;
|
|
91 |
tt:= tt.Next;
|
|
92 |
Dispose(t)
|
|
93 |
end;
|
|
94 |
end;
|
|
95 |
|
|
96 |
procedure SplitBySpace(var a, b: shortstring);
|
|
97 |
var i, t: integer;
|
|
98 |
begin
|
|
99 |
i:= Pos(' ', a);
|
|
100 |
if i>0 then
|
|
101 |
begin
|
|
102 |
for t:= 1 to Pred(i) do
|
|
103 |
if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32);
|
|
104 |
b:= copy(a, i + 1, Length(a) - i);
|
|
105 |
while (b[0]<>#0) and (b[1]=#32) do Delete(b, 1, 1);
|
|
106 |
byte(a[0]):= Pred(i)
|
|
107 |
end else b:= '';
|
|
108 |
end;
|
|
109 |
|
|
110 |
procedure DrawConsole(Surface: PSDL_Surface);
|
|
111 |
var x, y: integer;
|
|
112 |
r: TSDL_Rect;
|
|
113 |
begin
|
|
114 |
with r do
|
|
115 |
begin
|
|
116 |
x:= 0;
|
|
117 |
y:= cConsoleHeight;
|
|
118 |
w:= cScreenWidth;
|
|
119 |
h:= 4;
|
|
120 |
end;
|
|
121 |
SDL_FillRect(Surface, @r, cConsoleSplitterColor);
|
|
122 |
for y:= 0 to cConsoleHeight div 256 + 1 do
|
|
123 |
for x:= 0 to cScreenWidth div 256 + 1 do
|
|
124 |
DrawGear(sConsoleBG, x * 256, cConsoleHeight - 256 - y * 256, Surface);
|
|
125 |
for y:= 0 to cConsoleHeight div Fontz[fnt16].Height do
|
|
126 |
DXOutText(4, cConsoleHeight - (y + 2) * (Fontz[fnt16].Height + 2), fnt16, ConsoleLines[(CurrLine - 1 - y + cLinesCount) mod cLinesCount], Surface);
|
|
127 |
DXOutText(4, cConsoleHeight - Fontz[fnt16].Height - 2, fnt16, '> '+InputStr, Surface);
|
|
128 |
end;
|
|
129 |
|
|
130 |
procedure WriteToConsole(s: shortstring);
|
|
131 |
var Len: integer;
|
|
132 |
begin
|
|
133 |
{$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF}
|
|
134 |
Write(s);
|
|
135 |
repeat
|
|
136 |
Len:= cLineWidth - Length(ConsoleLines[CurrLine]);
|
|
137 |
ConsoleLines[CurrLine]:= ConsoleLines[CurrLine] + copy(s, 1, Len);
|
|
138 |
Delete(s, 1, Len);
|
|
139 |
if byte(ConsoleLines[CurrLine][0])=cLineWidth then
|
|
140 |
begin
|
|
141 |
inc(CurrLine);
|
|
142 |
if CurrLine = cLinesCount then CurrLine:= 0;
|
|
143 |
PLongWord(@ConsoleLines[CurrLine])^:= 0
|
|
144 |
end;
|
|
145 |
until Length(s) = 0
|
|
146 |
end;
|
|
147 |
|
|
148 |
procedure WriteLnToConsole(s: shortstring);
|
|
149 |
begin
|
|
150 |
WriteToConsole(s);
|
|
151 |
WriteLn;
|
|
152 |
inc(CurrLine);
|
|
153 |
if CurrLine = cLinesCount then CurrLine:= 0;
|
|
154 |
PLongWord(@ConsoleLines[CurrLine])^:= 0
|
|
155 |
end;
|
|
156 |
|
|
157 |
procedure InitConsole;
|
|
158 |
var i: integer;
|
|
159 |
begin
|
|
160 |
cLineWidth:= cScreenWidth div 10;
|
|
161 |
if cLineWidth > 255 then cLineWidth:= 255;
|
|
162 |
for i:= 0 to Pred(cLinesCount) do PLongWord(@ConsoleLines[i])^:= 0
|
|
163 |
end;
|
|
164 |
|
|
165 |
procedure ParseCommand(CmdStr: shortstring);
|
|
166 |
type PReal = ^real;
|
|
167 |
var i, ii: integer;
|
|
168 |
s: shortstring;
|
|
169 |
t: PVariable;
|
|
170 |
c: char;
|
|
171 |
begin
|
|
172 |
//WriteLnToConsole(CmdStr);
|
|
173 |
if CmdStr[0]=#0 then exit;
|
|
174 |
{$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF}
|
|
175 |
c:= CmdStr[1];
|
|
176 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/';
|
|
177 |
SplitBySpace(CmdStr, s);
|
|
178 |
t:= Variables;
|
|
179 |
while t <> nil do
|
|
180 |
begin
|
|
181 |
if t.Name = CmdStr then
|
|
182 |
begin
|
|
183 |
case t.VType of
|
|
184 |
vtCommand: if c='/' then
|
|
185 |
begin
|
|
186 |
TCommandHandler(t.Handler)(s);
|
|
187 |
end;
|
|
188 |
vtInteger: if c='$' then
|
|
189 |
if s[0]=#0 then
|
|
190 |
begin
|
|
191 |
str(PInteger(t.Handler)^, s);
|
|
192 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
|
|
193 |
end else val(s, PInteger(t.Handler)^, i);
|
|
194 |
vtReal: if c='$' then
|
|
195 |
if s[0]=#0 then
|
|
196 |
begin
|
|
197 |
str(PReal(t.Handler)^:4:6, s);
|
|
198 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
|
|
199 |
end else val(s, PReal(t.Handler)^ , i);
|
|
200 |
vtBoolean: if c='$' then
|
|
201 |
if s[0]=#0 then
|
|
202 |
begin
|
|
203 |
str(ord(boolean(t.Handler^)), s);
|
|
204 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"');
|
|
205 |
end else
|
|
206 |
begin
|
|
207 |
val(s, ii, i);
|
|
208 |
boolean(t.Handler^):= not (ii = 0)
|
|
209 |
end;
|
|
210 |
end;
|
|
211 |
exit
|
|
212 |
end else t:= t.Next
|
|
213 |
end;
|
|
214 |
case c of
|
|
215 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"')
|
|
216 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end
|
|
217 |
end;
|
|
218 |
|
|
219 |
procedure KeyPressConsole(Key: Longword);
|
|
220 |
begin
|
|
221 |
case Key of
|
|
222 |
8: if Length(InputStr)>0 then dec(InputStr[0]);
|
|
223 |
13,271: begin
|
|
224 |
ParseCommand('/say ' + InputStr);
|
|
225 |
InputStr:= ''
|
|
226 |
end;
|
|
227 |
96: begin
|
|
228 |
GameState:= gsGame;
|
|
229 |
cConsoleYAdd:= 0;
|
|
230 |
ResetKbd
|
|
231 |
end;
|
|
232 |
else InputStr:= InputStr + char(Key)
|
|
233 |
end
|
|
234 |
end;
|
|
235 |
|
|
236 |
{$INCLUDE CCHandlers.inc}
|
|
237 |
|
|
238 |
procedure AfterAttack;
|
|
239 |
begin
|
|
240 |
with CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].Gear^,
|
|
241 |
CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog] do
|
|
242 |
begin
|
|
243 |
Inc(AttacksNum);
|
|
244 |
State:= State and not gstAttacking;
|
|
245 |
if Ammo[CurSlot, CurAmmo].NumPerTurn >= AttacksNum then isInMultiShoot:= true
|
|
246 |
else begin
|
|
247 |
TurnTimeLeft:= Ammoz[Ammo[CurSlot, CurAmmo].AmmoType].TimeAfterTurn;
|
|
248 |
State:= State or gstAttacked;
|
|
249 |
OnUsedAmmo(Ammo)
|
|
250 |
end;
|
|
251 |
AttackBar:= 0
|
|
252 |
end
|
|
253 |
end;
|
|
254 |
|
|
255 |
initialization
|
|
256 |
InitConsole;
|
|
257 |
RegisterVariable('quit' , vtCommand, @chQuit );
|
|
258 |
RegisterVariable('capture' , vtCommand, @chCapture );
|
|
259 |
RegisterVariable('addteam' , vtCommand, @chAddTeam );
|
|
260 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal );
|
|
261 |
//RegisterVariable('gravity' , vtReal , @cGravity ); гравитация не должна быть доступна вообще
|
|
262 |
RegisterVariable('c_height', vtInteger, @cConsoleHeight );
|
|
263 |
RegisterVariable('gmflags' , vtInteger, @GameFlags );
|
|
264 |
RegisterVariable('showfps' , vtBoolean, @cShowFPS );
|
|
265 |
RegisterVariable('sound' , vtBoolean, @isSoundEnabled );
|
|
266 |
RegisterVariable('name' , vtCommand, @chName );
|
|
267 |
RegisterVariable('fort' , vtCommand, @chFort );
|
|
268 |
RegisterVariable('grave' , vtCommand, @chGrave );
|
|
269 |
RegisterVariable('bind' , vtCommand, @chBind );
|
|
270 |
RegisterVariable('add' , vtCommand, @chAdd );
|
|
271 |
RegisterVariable('say' , vtCommand, @chSay );
|
|
272 |
RegisterVariable('+left' , vtCommand, @chLeft_p );
|
|
273 |
RegisterVariable('-left' , vtCommand, @chLeft_m );
|
|
274 |
RegisterVariable('+right' , vtCommand, @chRight_p );
|
|
275 |
RegisterVariable('-right' , vtCommand, @chRight_m );
|
|
276 |
RegisterVariable('+up' , vtCommand, @chUp_p );
|
|
277 |
RegisterVariable('-up' , vtCommand, @chUp_m );
|
|
278 |
RegisterVariable('+down' , vtCommand, @chDown_p );
|
|
279 |
RegisterVariable('-down' , vtCommand, @chDown_m );
|
|
280 |
RegisterVariable('+attack' , vtCommand, @chAttack_p );
|
|
281 |
RegisterVariable('-attack' , vtCommand, @chAttack_m );
|
|
282 |
RegisterVariable('color' , vtCommand, @chColor );
|
|
283 |
RegisterVariable('switch' , vtCommand, @chSwitch );
|
|
284 |
RegisterVariable('nextturn', vtCommand, @chNextTurn );
|
|
285 |
RegisterVariable('timer' , vtCommand, @chTimer );
|
|
286 |
RegisterVariable('slot' , vtCommand, @chSlot );
|
|
287 |
RegisterVariable('put' , vtCommand, @chPut );
|
|
288 |
RegisterVariable('ljump' , vtCommand, @chLJump );
|
|
289 |
RegisterVariable('hjump' , vtCommand, @chHJump );
|
|
290 |
|
|
291 |
finalization
|
|
292 |
FreeVariablesList
|
|
293 |
|
|
294 |
end.
|