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