author | unc0rr |
Sun, 21 Jan 2007 19:51:02 +0000 | |
changeset 351 | 29bc9c36ad5f |
parent 312 | c36d0b34ac3d |
child 371 | 731ad6d27bd1 |
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 |
|
351 | 21 |
uses SDLh, uFloat; |
4 | 22 |
{$INCLUDE options.inc} |
23 |
const isDeveloperMode: boolean = true; |
|
351 | 24 |
type TVariableType = (vtCommand, vtInteger, 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; |
|
4 | 38 |
const cLineWidth: integer = 0; |
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; |
|
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; |
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); |
|
89 |
var i, t: integer; |
|
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); |
|
103 |
var x, y: integer; |
|
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); |
|
123 |
var Len: integer; |
|
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; |
|
150 |
var i: integer; |
|
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; |
|
4 | 159 |
var i, ii: integer; |
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; |
181 |
vtInteger: if c='$' then |
|
182 |
if s[0]=#0 then |
|
183 |
begin |
|
351 | 184 |
str(PInteger(t^.Handler)^, s); |
4 | 185 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
351 | 186 |
end else val(s, PInteger(t^.Handler)^, i); |
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 |
|
200 |
val(s, ii, i); |
|
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); |
237 |
begin |
|
238 |
case Key of |
|
239 |
8: if Length(InputStr)>0 then dec(InputStr[0]); |
|
46 | 240 |
9: AutoComplete; |
4 | 241 |
13,271: begin |
46 | 242 |
if InputStr[1] in ['/', '$'] then |
167 | 243 |
ParseCommand(InputStr, false) |
46 | 244 |
else |
167 | 245 |
ParseCommand('/say ' + InputStr, false); |
4 | 246 |
InputStr:= '' |
247 |
end; |
|
248 |
96: begin |
|
249 |
GameState:= gsGame; |
|
250 |
cConsoleYAdd:= 0; |
|
251 |
ResetKbd |
|
252 |
end; |
|
253 |
else InputStr:= InputStr + char(Key) |
|
254 |
end |
|
255 |
end; |
|
256 |
||
53 | 257 |
function GetLastConsoleLine: shortstring; |
258 |
begin |
|
351 | 259 |
if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)] |
260 |
else GetLastConsoleLine:= ConsoleLines[Pred(CurrLine)] |
|
53 | 261 |
end; |
262 |
||
4 | 263 |
{$INCLUDE CCHandlers.inc} |
264 |
||
265 |
initialization |
|
266 |
InitConsole; |
|
167 | 267 |
RegisterVariable('quit' , vtCommand, @chQuit , true ); |
205 | 268 |
RegisterVariable('proto' , vtCommand, @chCheckProto , true ); |
167 | 269 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
270 |
RegisterVariable('addteam' , vtCommand, @chAddTeam , false); |
|
271 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
|
272 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
273 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
274 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
275 |
RegisterVariable('c_height', vtInteger, @cConsoleHeight , false); |
|
276 |
RegisterVariable('gmflags' , vtInteger, @GameFlags , false); |
|
277 |
RegisterVariable('turntime', vtInteger, @cHedgehogTurnTime, false); |
|
278 |
RegisterVariable('name' , vtCommand, @chName , false); |
|
279 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
|
280 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
|
281 |
RegisterVariable('bind' , vtCommand, @chBind , true ); |
|
312 | 282 |
RegisterVariable('addhh' , vtCommand, @chAddHH , false); |
288 | 283 |
RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false); |
167 | 284 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
285 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
|
286 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
|
287 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
|
288 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
289 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
290 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
291 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
292 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
293 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
294 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
295 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
296 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
297 |
RegisterVariable('color' , vtCommand, @chColor , false); |
|
298 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
299 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
300 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
301 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
|
302 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
303 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
304 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
305 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
|
175 | 306 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
307 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
308 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
309 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 310 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
205
diff
changeset
|
311 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
4 | 312 |
|
313 |
finalization |
|
314 |
FreeVariablesList |
|
315 |
||
316 |
end. |