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