author | unc0rr |
Mon, 07 Jul 2008 15:39:27 +0000 | |
changeset 1068 | 9a0aa37ea6be |
parent 1066 | 1f1b3686a2b0 |
child 1242 | 4aca5f7b2504 |
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, |
988 | 38 |
uRandom, uAmmos, uTriggers, uStats, uGame, uChat, SDLh; |
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; |
203 |
begin |
|
785 | 204 |
if CurrLine = 0 then GetLastConsoleLine:= ConsoleLines[Pred(cLinesCount)].s |
205 |
else GetLastConsoleLine:= ConsoleLines[Pred(CurrLine)].s |
|
53 | 206 |
end; |
207 |
||
936
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
208 |
procedure StopMessages(Message: Longword); |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
209 |
begin |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
end; |
ba582673db7d
Fix 'AI may break demos playing' message while loading saves
unc0rr
parents:
917
diff
changeset
|
216 |
|
4 | 217 |
{$INCLUDE CCHandlers.inc} |
218 |
||
219 |
initialization |
|
220 |
InitConsole; |
|
205 | 221 |
RegisterVariable('proto' , vtCommand, @chCheckProto , true ); |
167 | 222 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
495
diff
changeset
|
223 |
RegisterVariable('rotmask' , vtCommand, @chRotateMask , true ); |
167 | 224 |
RegisterVariable('addteam' , vtCommand, @chAddTeam , false); |
589 | 225 |
RegisterVariable('addtrig' , vtCommand, @chAddTrigger , false); |
167 | 226 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
227 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
228 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
229 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
614 | 230 |
RegisterVariable('delay' , vtLongInt, @cInactDelay , false); |
231 |
RegisterVariable('casefreq', vtLongInt, @cCaseFactor , false); |
|
622 | 232 |
RegisterVariable('landadds', vtLongInt, @cLandAdditions , false); |
371 | 233 |
RegisterVariable('gmflags' , vtLongInt, @GameFlags , false); |
234 |
RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false); |
|
167 | 235 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
236 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
|
237 |
RegisterVariable('bind' , vtCommand, @chBind , true ); |
|
312 | 238 |
RegisterVariable('addhh' , vtCommand, @chAddHH , false); |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
589
diff
changeset
|
239 |
RegisterVariable('hhcoords', vtCommand, @chSetHHCoords , false); |
288 | 240 |
RegisterVariable('ammstore', vtCommand, @chAddAmmoStore , false); |
1022 | 241 |
RegisterVariable('quit' , vtCommand, @chQuit , true ); |
242 |
RegisterVariable('confirm' , vtCommand, @chConfirm , true ); |
|
626 | 243 |
RegisterVariable('+speedup', vtCommand, @chSpeedup_p , true ); |
244 |
RegisterVariable('-speedup', vtCommand, @chSpeedup_m , true ); |
|
167 | 245 |
RegisterVariable('skip' , vtCommand, @chSkip , false); |
991 | 246 |
RegisterVariable('history' , vtCommand, @chHistory , true ); |
946 | 247 |
RegisterVariable('chat' , vtCommand, @chChat , true ); |
167 | 248 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
249 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , false); |
|
250 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
|
251 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
252 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
253 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
254 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
255 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
256 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
257 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
258 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
259 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
260 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
261 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
262 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
1035 | 263 |
RegisterVariable('taunt' , vtCommand, @chTaunt , false); |
783 | 264 |
RegisterVariable('setweap' , vtCommand, @chSetWeapon , false); |
167 | 265 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
266 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
267 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
268 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
970 | 269 |
RegisterVariable('fullscr' , vtCommand, @chFullScr , true ); |
175 | 270 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
271 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
272 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
273 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
176 | 274 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
281
5b483aa9f2ab
Pause support (mouse cursor is released when the game is paused)
unc0rr
parents:
205
diff
changeset
|
275 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
4 | 276 |
|
277 |
finalization |
|
278 |
FreeVariablesList |
|
279 |
||
280 |
end. |