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