author | nemo |
Mon, 16 Nov 2009 17:40:17 +0000 | |
changeset 2626 | cb64997c7c59 |
parent 2599 | c7153d2348f3 |
child 2630 | 079ef82eac75 |
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; |
2599 | 22 |
{$INCLUDE "options.inc"} |
4 | 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 |
288 | 37 |
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
|
38 |
uRandom, uAmmos, uTriggers, uStats, uGame, uChat, SDLh, uSound, uVisualGears; |
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); |
|
1850 | 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 |
byte(a[0]):= Pred(i) |
|
108 |
end else b:= ''; |
|
4 | 109 |
end; |
110 |
||
111 |
procedure WriteToConsole(s: shortstring); |
|
371 | 112 |
var Len: LongInt; |
4 | 113 |
begin |
114 |
{$IFDEF DEBUGFILE}AddFileLog('Console write: ' + s);{$ENDIF} |
|
115 |
Write(s); |
|
116 |
repeat |
|
785 | 117 |
Len:= cLineWidth - Length(ConsoleLines[CurrLine].s); |
118 |
SetLine(ConsoleLines[CurrLine], ConsoleLines[CurrLine].s + copy(s, 1, Len)); |
|
4 | 119 |
Delete(s, 1, Len); |
785 | 120 |
if byte(ConsoleLines[CurrLine].s[0]) = cLineWidth then |
4 | 121 |
begin |
122 |
inc(CurrLine); |
|
123 |
if CurrLine = cLinesCount then CurrLine:= 0; |
|
785 | 124 |
PByte(@ConsoleLines[CurrLine].s)^:= 0 |
4 | 125 |
end; |
126 |
until Length(s) = 0 |
|
127 |
end; |
|
128 |
||
129 |
procedure WriteLnToConsole(s: shortstring); |
|
130 |
begin |
|
131 |
WriteToConsole(s); |
|
132 |
WriteLn; |
|
133 |
inc(CurrLine); |
|
134 |
if CurrLine = cLinesCount then CurrLine:= 0; |
|
785 | 135 |
PByte(@ConsoleLines[CurrLine].s)^:= 0 |
4 | 136 |
end; |
137 |
||
138 |
procedure InitConsole; |
|
371 | 139 |
var i: LongInt; |
4 | 140 |
begin |
141 |
cLineWidth:= cScreenWidth div 10; |
|
142 |
if cLineWidth > 255 then cLineWidth:= 255; |
|
785 | 143 |
for i:= 0 to Pred(cLinesCount) do PByte(@ConsoleLines[i])^:= 0 |
4 | 144 |
end; |
145 |
||
351 | 146 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
147 |
type PhwFloat = ^hwFloat; |
|
495 | 148 |
var ii: LongInt; |
4 | 149 |
s: shortstring; |
150 |
t: PVariable; |
|
151 |
c: char; |
|
152 |
begin |
|
153 |
//WriteLnToConsole(CmdStr); |
|
154 |
if CmdStr[0]=#0 then exit; |
|
155 |
{$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF} |
|
156 |
c:= CmdStr[1]; |
|
157 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/'; |
|
158 |
SplitBySpace(CmdStr, s); |
|
159 |
t:= Variables; |
|
160 |
while t <> nil do |
|
161 |
begin |
|
351 | 162 |
if t^.Name = CmdStr then |
4 | 163 |
begin |
351 | 164 |
if TrustedSource or t^.Trusted then |
165 |
case t^.VType of |
|
4 | 166 |
vtCommand: if c='/' then |
167 |
begin |
|
351 | 168 |
TCommandHandler(t^.Handler)(s); |
4 | 169 |
end; |
371 | 170 |
vtLongInt: if c='$' then |
4 | 171 |
if s[0]=#0 then |
172 |
begin |
|
371 | 173 |
str(PLongInt(t^.Handler)^, s); |
4 | 174 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
495 | 175 |
end else val(s, PLongInt(t^.Handler)^); |
176 |
vthwFloat: if c='$' then |
|
4 | 177 |
if s[0]=#0 then |
178 |
begin |
|
351 | 179 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
4 | 180 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
351 | 181 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
4 | 182 |
vtBoolean: if c='$' then |
183 |
if s[0]=#0 then |
|
184 |
begin |
|
351 | 185 |
str(ord(boolean(t^.Handler^)), s); |
4 | 186 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
187 |
end else |
|
188 |
begin |
|
495 | 189 |
val(s, ii); |
351 | 190 |
boolean(t^.Handler^):= not (ii = 0) |
4 | 191 |
end; |
192 |
end; |
|
193 |
exit |
|
351 | 194 |
end else t:= t^.Next |
4 | 195 |
end; |
196 |
case c of |
|
197 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
198 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
199 |
end; |
|
200 |
||
53 | 201 |
function GetLastConsoleLine: shortstring; |
1242 | 202 |
var Result: shortstring; |
203 |
i: LongWord; |
|
53 | 204 |
begin |
1242 | 205 |
i:= (CurrLine + cLinesCount - 2) mod cLinesCount; |
206 |
Result:= ConsoleLines[i].s; |
|
207 |
||
1277 | 208 |
Result:= Result + #10; |
1242 | 209 |
|
210 |
i:= (CurrLine + cLinesCount - 1) mod cLinesCount; |
|
211 |
Result:= Result + ConsoleLines[i].s; |
|
212 |
||
213 |
GetLastConsoleLine:= Result |
|
53 | 214 |
end; |
215 |
||
936
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
216 |
procedure StopMessages(Message: Longword); |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
217 |
begin |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
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
|
223 |
end; |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
224 |
|
2599 | 225 |
{$INCLUDE "CCHandlers.inc"} |
4 | 226 |
|
227 |
initialization |
|
228 |
InitConsole; |
|
205 | 229 |
RegisterVariable('proto' , vtCommand, @chCheckProto , true ); |
1560
e140bc57ff68
Quick replay round to spectators until current move
unc0rr
parents:
1277
diff
changeset
|
230 |
RegisterVariable('spectate', vtBoolean, @fastUntilLag , false); |
167 | 231 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
495
diff
changeset
|
232 |
RegisterVariable('rotmask' , vtCommand, @chRotateMask , true ); |
167 | 233 |
RegisterVariable('addteam' , vtCommand, @chAddTeam , false); |
589 | 234 |
RegisterVariable('addtrig' , vtCommand, @chAddTrigger , false); |
167 | 235 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
236 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
237 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
238 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
1797 | 239 |
RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false); |
614 | 240 |
RegisterVariable('delay' , vtLongInt, @cInactDelay , false); |
241 |
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
|
242 |
RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns , false); |
2578 | 243 |
RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false); |
622 | 244 |
RegisterVariable('landadds', vtLongInt, @cLandAdditions , false); |
371 | 245 |
RegisterVariable('gmflags' , vtLongInt, @GameFlags , false); |
2428 | 246 |
RegisterVariable('trflags' , vtLongInt, @TrainingFlags , false); |
371 | 247 |
RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false); |
2578 | 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 ); |
|
2162 | 261 |
RegisterVariable('zoomin' , vtCommand, @chZoomIn , true ); |
262 |
RegisterVariable('zoomout' , vtCommand, @chZoomOut , true ); |
|
2379 | 263 |
RegisterVariable('zoomreset',vtCommand, @chZoomReset , true ); |
167 | 264 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
991 | 265 |
RegisterVariable('history' , vtCommand, @chHistory , true ); |
946 | 266 |
RegisterVariable('chat' , vtCommand, @chChat , true ); |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1797
diff
changeset
|
267 |
RegisterVariable('newgrave', vtCommand, @chNewGrave , false); |
167 | 268 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
2017 | 269 |
RegisterVariable('hogsay' , vtCommand, @chHogSay , true ); |
2130
708758635955
Please don't take away my checkin privileges - Tiy asked me to keep working on this
nemo
parents:
2124
diff
changeset
|
270 |
RegisterVariable('team' , vtCommand, @chTeamSay , true ); |
167 | 271 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
1639 | 272 |
RegisterVariable('+precise', vtCommand, @chPrecise_p , false); |
273 |
RegisterVariable('-precise', vtCommand, @chPrecise_m , false); |
|
167 | 274 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
275 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
276 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
277 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
278 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
279 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
280 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
281 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
282 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
283 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
284 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
285 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
286 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
1035 | 287 |
RegisterVariable('taunt' , vtCommand, @chTaunt , false); |
783 | 288 |
RegisterVariable('setweap' , vtCommand, @chSetWeapon , false); |
167 | 289 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
290 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
291 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
292 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
970 | 293 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
175 | 294 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
295 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
296 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
297 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 298 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
205
diff
changeset
|
299 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
2578 | 300 |
RegisterVariable('+cur_u' , vtCommand, @chCurU_p , true ); |
301 |
RegisterVariable('-cur_u' , vtCommand, @chCurU_m , true ); |
|
302 |
RegisterVariable('+cur_d' , vtCommand, @chCurD_p , true ); |
|
303 |
RegisterVariable('-cur_d' , vtCommand, @chCurD_m , true ); |
|
304 |
RegisterVariable('+cur_l' , vtCommand, @chCurL_p , true ); |
|
305 |
RegisterVariable('-cur_l' , vtCommand, @chCurL_m , true ); |
|
306 |
RegisterVariable('+cur_r' , vtCommand, @chCurR_p , true ); |
|
307 |
RegisterVariable('-cur_r' , vtCommand, @chCurR_m , true ); |
|
4 | 308 |
|
309 |
finalization |
|
310 |
FreeVariablesList |
|
311 |
||
312 |
end. |