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