author | unc0rr |
Mon, 26 Jan 2009 15:53:43 +0000 | |
changeset 1760 | 55a1edd97911 |
parent 1654 | 9cfa6f23e767 |
child 1782 | e7589e37a6d6 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy 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 |
|
988 | 21 |
uses 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 WriteToConsole(s: shortstring); |
|
28 |
procedure WriteLnToConsole(s: shortstring); |
|
351 | 29 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
936
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
30 |
procedure StopMessages(Message: Longword); |
53 | 31 |
function GetLastConsoleLine: shortstring; |
4 | 32 |
|
543
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
33 |
procedure doPut(putX, putY: LongInt; fromAI: boolean); |
465e2ec8f05f
- Better randomness of placing hedgehogs on the land
unc0rr
parents:
539
diff
changeset
|
34 |
|
4 | 35 |
implementation |
36 |
{$J+} |
|
288 | 37 |
uses uMisc, uStore, Types, uConsts, uGears, uTeams, uIO, uKeys, uWorld, uLand, |
1654 | 38 |
uRandom, uAmmos, uTriggers, uStats, uGame, uChat, SDLh, uSound; |
950 | 39 |
|
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 |
end; |
|
4 | 54 |
|
785 | 55 |
var ConsoleLines: array[byte] of TTextLine; |
371 | 56 |
CurrLine: LongInt = 0; |
4 | 57 |
Variables: PVariable = nil; |
58 |
||
785 | 59 |
procedure SetLine(var tl: TTextLine; str: shortstring); |
60 |
begin |
|
61 |
with tl do |
|
62 |
s:= str; |
|
63 |
end; |
|
64 |
||
167 | 65 |
function RegisterVariable(Name: string; VType: TVariableType; p: pointer; Trusted: boolean): PVariable; |
351 | 66 |
var Result: PVariable; |
4 | 67 |
begin |
17 | 68 |
New(Result); |
69 |
TryDo(Result <> nil, 'RegisterVariable: Result = nil', true); |
|
4 | 70 |
FillChar(Result^, sizeof(TVariable), 0); |
351 | 71 |
Result^.Name:= Name; |
72 |
Result^.VType:= VType; |
|
73 |
Result^.Handler:= p; |
|
74 |
Result^.Trusted:= Trusted; |
|
167 | 75 |
|
4 | 76 |
if Variables = nil then Variables:= Result |
77 |
else begin |
|
351 | 78 |
Result^.Next:= Variables; |
4 | 79 |
Variables:= Result |
351 | 80 |
end; |
81 |
||
82 |
RegisterVariable:= Result |
|
4 | 83 |
end; |
84 |
||
85 |
procedure FreeVariablesList; |
|
86 |
var t, tt: PVariable; |
|
87 |
begin |
|
88 |
tt:= Variables; |
|
89 |
Variables:= nil; |
|
351 | 90 |
while tt <> nil do |
4 | 91 |
begin |
92 |
t:= tt; |
|
351 | 93 |
tt:= tt^.Next; |
4 | 94 |
Dispose(t) |
95 |
end; |
|
96 |
end; |
|
97 |
||
98 |
procedure SplitBySpace(var a, b: shortstring); |
|
371 | 99 |
var i, t: LongInt; |
4 | 100 |
begin |
101 |
i:= Pos(' ', a); |
|
102 |
if i>0 then |
|
103 |
begin |
|
104 |
for t:= 1 to Pred(i) do |
|
105 |
if (a[t] >= 'A')and(a[t] <= 'Z') then Inc(a[t], 32); |
|
106 |
b:= copy(a, i + 1, Length(a) - i); |
|
107 |
while (b[0]<>#0) and (b[1]=#32) do Delete(b, 1, 1); |
|
108 |
byte(a[0]):= Pred(i) |
|
109 |
end else b:= ''; |
|
110 |
end; |
|
111 |
||
112 |
procedure WriteToConsole(s: shortstring); |
|
371 | 113 |
var Len: LongInt; |
4 | 114 |
begin |
115 |
{$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF} |
|
116 |
Write(s); |
|
117 |
repeat |
|
785 | 118 |
Len:= cLineWidth - Length(ConsoleLines[CurrLine].s); |
119 |
SetLine(ConsoleLines[CurrLine], ConsoleLines[CurrLine].s + copy(s, 1, Len)); |
|
4 | 120 |
Delete(s, 1, Len); |
785 | 121 |
if byte(ConsoleLines[CurrLine].s[0]) = cLineWidth then |
4 | 122 |
begin |
123 |
inc(CurrLine); |
|
124 |
if CurrLine = cLinesCount then CurrLine:= 0; |
|
785 | 125 |
PByte(@ConsoleLines[CurrLine].s)^:= 0 |
4 | 126 |
end; |
127 |
until Length(s) = 0 |
|
128 |
end; |
|
129 |
||
130 |
procedure WriteLnToConsole(s: shortstring); |
|
131 |
begin |
|
132 |
WriteToConsole(s); |
|
133 |
WriteLn; |
|
134 |
inc(CurrLine); |
|
135 |
if CurrLine = cLinesCount then CurrLine:= 0; |
|
785 | 136 |
PByte(@ConsoleLines[CurrLine].s)^:= 0 |
4 | 137 |
end; |
138 |
||
139 |
procedure InitConsole; |
|
371 | 140 |
var i: LongInt; |
4 | 141 |
begin |
142 |
cLineWidth:= cScreenWidth div 10; |
|
143 |
if cLineWidth > 255 then cLineWidth:= 255; |
|
785 | 144 |
for i:= 0 to Pred(cLinesCount) do PByte(@ConsoleLines[i])^:= 0 |
4 | 145 |
end; |
146 |
||
351 | 147 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
148 |
type PhwFloat = ^hwFloat; |
|
495 | 149 |
var ii: LongInt; |
4 | 150 |
s: shortstring; |
151 |
t: PVariable; |
|
152 |
c: char; |
|
153 |
begin |
|
154 |
//WriteLnToConsole(CmdStr); |
|
155 |
if CmdStr[0]=#0 then exit; |
|
156 |
{$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF} |
|
157 |
c:= CmdStr[1]; |
|
158 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/'; |
|
159 |
SplitBySpace(CmdStr, s); |
|
160 |
t:= Variables; |
|
161 |
while t <> nil do |
|
162 |
begin |
|
351 | 163 |
if t^.Name = CmdStr then |
4 | 164 |
begin |
351 | 165 |
if TrustedSource or t^.Trusted then |
166 |
case t^.VType of |
|
4 | 167 |
vtCommand: if c='/' then |
168 |
begin |
|
351 | 169 |
TCommandHandler(t^.Handler)(s); |
4 | 170 |
end; |
371 | 171 |
vtLongInt: if c='$' then |
4 | 172 |
if s[0]=#0 then |
173 |
begin |
|
371 | 174 |
str(PLongInt(t^.Handler)^, s); |
4 | 175 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
495 | 176 |
end else val(s, PLongInt(t^.Handler)^); |
177 |
vthwFloat: if c='$' then |
|
4 | 178 |
if s[0]=#0 then |
179 |
begin |
|
351 | 180 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
4 | 181 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
351 | 182 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
4 | 183 |
vtBoolean: if c='$' then |
184 |
if s[0]=#0 then |
|
185 |
begin |
|
351 | 186 |
str(ord(boolean(t^.Handler^)), s); |
4 | 187 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
188 |
end else |
|
189 |
begin |
|
495 | 190 |
val(s, ii); |
351 | 191 |
boolean(t^.Handler^):= not (ii = 0) |
4 | 192 |
end; |
193 |
end; |
|
194 |
exit |
|
351 | 195 |
end else t:= t^.Next |
4 | 196 |
end; |
197 |
case c of |
|
198 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
199 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
200 |
end; |
|
201 |
||
53 | 202 |
function GetLastConsoleLine: shortstring; |
1242 | 203 |
var Result: shortstring; |
204 |
i: LongWord; |
|
53 | 205 |
begin |
1242 | 206 |
i:= (CurrLine + cLinesCount - 2) mod cLinesCount; |
207 |
Result:= ConsoleLines[i].s; |
|
208 |
||
1277 | 209 |
Result:= Result + #10; |
1242 | 210 |
|
211 |
i:= (CurrLine + cLinesCount - 1) mod cLinesCount; |
|
212 |
Result:= Result + ConsoleLines[i].s; |
|
213 |
||
214 |
GetLastConsoleLine:= Result |
|
53 | 215 |
end; |
216 |
||
936
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
217 |
procedure StopMessages(Message: Longword); |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
218 |
begin |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
end; |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
225 |
|
4 | 226 |
{$INCLUDE CCHandlers.inc} |
227 |
||
228 |
initialization |
|
229 |
InitConsole; |
|
205 | 230 |
RegisterVariable('proto' , vtCommand, @chCheckProto , true ); |
1560
e140bc57ff68
Quick replay round to spectators until current move
unc0rr
parents:
1277
diff
changeset
|
231 |
RegisterVariable('spectate', vtBoolean, @fastUntilLag , false); |
167 | 232 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
495
diff
changeset
|
233 |
RegisterVariable('rotmask' , vtCommand, @chRotateMask , true ); |
167 | 234 |
RegisterVariable('addteam' , vtCommand, @chAddTeam , false); |
589 | 235 |
RegisterVariable('addtrig' , vtCommand, @chAddTrigger , false); |
167 | 236 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
237 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
238 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
239 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
614 | 240 |
RegisterVariable('delay' , vtLongInt, @cInactDelay , false); |
241 |
RegisterVariable('casefreq', vtLongInt, @cCaseFactor , false); |
|
622 | 242 |
RegisterVariable('landadds', vtLongInt, @cLandAdditions , false); |
371 | 243 |
RegisterVariable('gmflags' , vtLongInt, @GameFlags , false); |
244 |
RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false); |
|
167 | 245 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
1654 | 246 |
RegisterVariable('voicepack',vtCommand, @chVoicepack , false); |
167 | 247 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
248 |
RegisterVariable('bind' , vtCommand, @chBind , true ); |
|
312 | 249 |
RegisterVariable('addhh' , vtCommand, @chAddHH , false); |
1242 | 250 |
RegisterVariable('hat' , vtCommand, @chSetHat , false); |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
589
diff
changeset
|
251 |
RegisterVariable('hhcoords', vtCommand, @chSetHHCoords , false); |
288 | 252 |
RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false); |
1022 | 253 |
RegisterVariable('quit' , vtCommand, @chQuit , true ); |
254 |
RegisterVariable('confirm' , vtCommand, @chConfirm , true ); |
|
626 | 255 |
RegisterVariable('+speedup', vtCommand, @chSpeedup_p , true ); |
256 |
RegisterVariable('-speedup', vtCommand, @chSpeedup_m , true ); |
|
167 | 257 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
991 | 258 |
RegisterVariable('history' , vtCommand, @chHistory , true ); |
946 | 259 |
RegisterVariable('chat' , vtCommand, @chChat , true ); |
167 | 260 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
261 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
|
1639 | 262 |
RegisterVariable('+precise', vtCommand, @chPrecise_p , false); |
263 |
RegisterVariable('-precise', vtCommand, @chPrecise_m , false); |
|
167 | 264 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
265 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
266 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
267 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
268 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
269 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
270 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
271 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
272 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
273 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
274 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
275 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
276 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
1035 | 277 |
RegisterVariable('taunt' , vtCommand, @chTaunt , false); |
783 | 278 |
RegisterVariable('setweap' , vtCommand, @chSetWeapon , false); |
167 | 279 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
280 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
281 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
282 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
970 | 283 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
175 | 284 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
285 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
286 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
287 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 288 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
205
diff
changeset
|
289 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
4 | 290 |
|
291 |
finalization |
|
292 |
FreeVariablesList |
|
293 |
||
294 |
end. |