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