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