author | unc0rr |
Thu, 05 Oct 2006 16:33:18 +0000 | |
changeset 183 | 57c2ef19f719 |
parent 176 | 533d03041dcd |
child 196 | 993cf173218b |
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+} |
|
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
53
diff
changeset
|
36 |
uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uSound, 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 ); |
264 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
|
265 |
RegisterVariable('addteam' , vtCommand, @chAddTeam , false); |
|
266 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
|
267 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
268 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
269 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
270 |
RegisterVariable('c_height', vtInteger, @cConsoleHeight , false); |
|
271 |
RegisterVariable('gmflags' , vtInteger, @GameFlags , false); |
|
272 |
RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime, false); |
|
273 |
RegisterVariable('name' , vtCommand, @chName , false); |
|
274 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
|
275 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
|
276 |
RegisterVariable('bind' , vtCommand, @chBind , true ); |
|
277 |
RegisterVariable('add' , vtCommand, @chAdd , false); |
|
278 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
|
279 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
|
280 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
|
281 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
|
282 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
283 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
284 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
285 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
286 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
287 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
288 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
289 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
290 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
291 |
RegisterVariable('color' , vtCommand, @chColor , false); |
|
292 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
293 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
294 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
295 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
|
296 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
297 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
298 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
299 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
|
175 | 300 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
301 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
302 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
303 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 304 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
4 | 305 |
|
306 |
finalization |
|
307 |
FreeVariablesList |
|
308 |
||
309 |
end. |