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 |
|
|
218 |
procedure KeyPressConsole(Key: Longword);
|
|
219 |
begin
|
|
220 |
case Key of
|
|
221 |
8: if Length(InputStr)>0 then dec(InputStr[0]);
|
|
222 |
13,271: begin
|
|
223 |
ParseCommand('/say ' + InputStr);
|
|
224 |
InputStr:= ''
|
|
225 |
end;
|
|
226 |
96: begin
|
|
227 |
GameState:= gsGame;
|
|
228 |
cConsoleYAdd:= 0;
|
|
229 |
ResetKbd
|
|
230 |
end;
|
|
231 |
else InputStr:= InputStr + char(Key)
|
|
232 |
end
|
|
233 |
end;
|
|
234 |
|
|
235 |
{$INCLUDE CCHandlers.inc}
|
|
236 |
|
|
237 |
initialization
|
|
238 |
InitConsole;
|
|
239 |
RegisterVariable('quit' , vtCommand, @chQuit );
|
|
240 |
RegisterVariable('capture' , vtCommand, @chCapture );
|
|
241 |
RegisterVariable('addteam' , vtCommand, @chAddTeam );
|
|
242 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal );
|
|
243 |
//RegisterVariable('gravity' , vtReal , @cGravity ); гравитация не должна быть доступна вообще
|
|
244 |
RegisterVariable('c_height', vtInteger, @cConsoleHeight );
|
|
245 |
RegisterVariable('gmflags' , vtInteger, @GameFlags );
|
|
246 |
RegisterVariable('showfps' , vtBoolean, @cShowFPS );
|
|
247 |
RegisterVariable('sound' , vtBoolean, @isSoundEnabled );
|
|
248 |
RegisterVariable('name' , vtCommand, @chName );
|
|
249 |
RegisterVariable('fort' , vtCommand, @chFort );
|
|
250 |
RegisterVariable('grave' , vtCommand, @chGrave );
|
|
251 |
RegisterVariable('bind' , vtCommand, @chBind );
|
|
252 |
RegisterVariable('add' , vtCommand, @chAdd );
|
|
253 |
RegisterVariable('say' , vtCommand, @chSay );
|
|
254 |
RegisterVariable('+left' , vtCommand, @chLeft_p );
|
|
255 |
RegisterVariable('-left' , vtCommand, @chLeft_m );
|
|
256 |
RegisterVariable('+right' , vtCommand, @chRight_p );
|
|
257 |
RegisterVariable('-right' , vtCommand, @chRight_m );
|
|
258 |
RegisterVariable('+up' , vtCommand, @chUp_p );
|
|
259 |
RegisterVariable('-up' , vtCommand, @chUp_m );
|
|
260 |
RegisterVariable('+down' , vtCommand, @chDown_p );
|
|
261 |
RegisterVariable('-down' , vtCommand, @chDown_m );
|
|
262 |
RegisterVariable('+attack' , vtCommand, @chAttack_p );
|
|
263 |
RegisterVariable('-attack' , vtCommand, @chAttack_m );
|
|
264 |
RegisterVariable('color' , vtCommand, @chColor );
|
|
265 |
RegisterVariable('switch' , vtCommand, @chSwitch );
|
|
266 |
RegisterVariable('nextturn', vtCommand, @chNextTurn );
|
|
267 |
RegisterVariable('timer' , vtCommand, @chTimer );
|
|
268 |
RegisterVariable('slot' , vtCommand, @chSlot );
|
|
269 |
RegisterVariable('put' , vtCommand, @chPut );
|
|
270 |
RegisterVariable('ljump' , vtCommand, @chLJump );
|
|
271 |
RegisterVariable('hjump' , vtCommand, @chHJump );
|
|
272 |
|
|
273 |
finalization
|
|
274 |
FreeVariablesList
|
|
275 |
|
|
276 |
end.
|