author | nemo |
Fri, 15 May 2009 00:51:37 +0000 | |
changeset 2057 | cf0d84479251 |
parent 2042 | 905c554d62e6 |
child 2124 | d39c61eaca15 |
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, |
2042
905c554d62e6
Move Speech to visual gears. This checkin CRASHES on deletion of visual gear outside the doStep
nemo
parents:
2031
diff
changeset
|
39 |
uRandom, uAmmos, uTriggers, uStats, uGame, uChat, SDLh, uSound, uVisualGears; |
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); |
|
1850 | 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 |
byte(a[0]):= Pred(i) |
|
109 |
end else b:= ''; |
|
4 | 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); |
|
1797 | 240 |
RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false); |
614 | 241 |
RegisterVariable('delay' , vtLongInt, @cInactDelay , false); |
242 |
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
|
243 |
RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns , false); |
1895 | 244 |
RegisterVariable('damagepct', vtLongInt, @cDamagePercent, false); |
622 | 245 |
RegisterVariable('landadds', vtLongInt, @cLandAdditions , false); |
371 | 246 |
RegisterVariable('gmflags' , vtLongInt, @GameFlags , false); |
247 |
RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false); |
|
2031
b6f3e56fb100
david_ac's game scheme options for mine delay/quantity, tweaks to sniper rifle
nemo
parents:
2017
diff
changeset
|
248 |
RegisterVariable('minestime', vtLongInt, @cMinesTime, false); |
167 | 249 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
1654 | 250 |
RegisterVariable('voicepack',vtCommand, @chVoicepack , false); |
167 | 251 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
252 |
RegisterVariable('bind' , vtCommand, @chBind , true ); |
|
312 | 253 |
RegisterVariable('addhh' , vtCommand, @chAddHH , false); |
1242 | 254 |
RegisterVariable('hat' , vtCommand, @chSetHat , false); |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
589
diff
changeset
|
255 |
RegisterVariable('hhcoords', vtCommand, @chSetHHCoords , false); |
288 | 256 |
RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false); |
1022 | 257 |
RegisterVariable('quit' , vtCommand, @chQuit , true ); |
258 |
RegisterVariable('confirm' , vtCommand, @chConfirm , true ); |
|
626 | 259 |
RegisterVariable('+speedup', vtCommand, @chSpeedup_p , true ); |
260 |
RegisterVariable('-speedup', vtCommand, @chSpeedup_m , true ); |
|
167 | 261 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
991 | 262 |
RegisterVariable('history' , vtCommand, @chHistory , true ); |
946 | 263 |
RegisterVariable('chat' , vtCommand, @chChat , true ); |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1797
diff
changeset
|
264 |
RegisterVariable('newgrave', vtCommand, @chNewGrave , false); |
167 | 265 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
2017 | 266 |
RegisterVariable('hogsay' , vtCommand, @chHogSay , true ); |
167 | 267 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
1639 | 268 |
RegisterVariable('+precise', vtCommand, @chPrecise_p , false); |
269 |
RegisterVariable('-precise', vtCommand, @chPrecise_m , false); |
|
167 | 270 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
271 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
272 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
273 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
274 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
275 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
276 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
277 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
278 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
279 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
280 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
281 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
282 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
1035 | 283 |
RegisterVariable('taunt' , vtCommand, @chTaunt , false); |
783 | 284 |
RegisterVariable('setweap' , vtCommand, @chSetWeapon , false); |
167 | 285 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
286 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
287 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
288 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
970 | 289 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
175 | 290 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
291 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
292 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
293 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 294 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
205
diff
changeset
|
295 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
4 | 296 |
|
297 |
finalization |
|
298 |
FreeVariablesList |
|
299 |
||
300 |
end. |