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