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