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