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