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