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