author | unc0rr |
Sun, 21 Nov 2010 19:07:43 +0300 | |
changeset 4401 | 9cb6990af584 |
parent 4398 | 36d7e4b6ca81 |
child 4402 | 54a78ec6aac4 |
permissions | -rw-r--r-- |
4373 | 1 |
{$INCLUDE "options.inc"} |
2 |
||
3 |
unit uCommands; |
|
4 |
||
5 |
interface |
|
6 |
||
7 |
var isDeveloperMode: boolean; |
|
8 |
type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean); |
|
9 |
TCommandHandler = procedure (var params: shortstring); |
|
10 |
||
11 |
procedure initModule; |
|
12 |
procedure freeModule; |
|
4398
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4396
diff
changeset
|
13 |
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean); |
4373 | 14 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
15 |
procedure StopMessages(Message: Longword); |
|
16 |
procedure doPut(putX, putY: LongInt; fromAI: boolean); |
|
17 |
||
18 |
implementation |
|
4401 | 19 |
uses Types, uConsts, uIO, uMobile, |
20 |
uRandom, uChat, SDLh, uScript, uTypes, |
|
4398
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4396
diff
changeset
|
21 |
uVariables, uConsole, uUtils; |
4373 | 22 |
|
23 |
type PVariable = ^TVariable; |
|
24 |
TVariable = record |
|
25 |
Next: PVariable; |
|
26 |
Name: string[15]; |
|
27 |
VType: TVariableType; |
|
28 |
Handler: pointer; |
|
29 |
Trusted: boolean; |
|
30 |
end; |
|
31 |
||
32 |
var |
|
33 |
Variables: PVariable; |
|
34 |
||
4398
36d7e4b6ca81
Move some command handlers out of uCommands into more appropriate places, thus removing some dependencies. Ideally uCommands shouldn't depend on anything (except for uTypes and uConsts probably)
unc0rr
parents:
4396
diff
changeset
|
35 |
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean); |
4373 | 36 |
var value: PVariable; |
37 |
begin |
|
38 |
New(value); |
|
39 |
TryDo(value <> nil, 'RegisterVariable: value = nil', true); |
|
40 |
FillChar(value^, sizeof(TVariable), 0); |
|
41 |
value^.Name:= Name; |
|
42 |
value^.VType:= VType; |
|
43 |
value^.Handler:= p; |
|
44 |
value^.Trusted:= Trusted; |
|
45 |
||
46 |
if Variables = nil then Variables:= value |
|
47 |
else begin |
|
48 |
value^.Next:= Variables; |
|
49 |
Variables:= value |
|
50 |
end; |
|
51 |
end; |
|
52 |
||
53 |
||
54 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
|
55 |
var ii: LongInt; |
|
56 |
s: shortstring; |
|
57 |
t: PVariable; |
|
58 |
c: char; |
|
59 |
begin |
|
60 |
//WriteLnToConsole(CmdStr); |
|
61 |
if CmdStr[0]=#0 then exit; |
|
62 |
{$IFDEF DEBUGFILE}AddFileLog('ParseCommand "' + CmdStr + '"');{$ENDIF} |
|
63 |
c:= CmdStr[1]; |
|
64 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/'; |
|
65 |
s:= ''; |
|
66 |
SplitBySpace(CmdStr, s); |
|
67 |
t:= Variables; |
|
68 |
while t <> nil do |
|
69 |
begin |
|
70 |
if t^.Name = CmdStr then |
|
71 |
begin |
|
72 |
if TrustedSource or t^.Trusted then |
|
73 |
case t^.VType of |
|
74 |
vtCommand: if c='/' then |
|
75 |
begin |
|
76 |
TCommandHandler(t^.Handler)(s); |
|
77 |
end; |
|
78 |
vtLongInt: if c='$' then |
|
79 |
if s[0]=#0 then |
|
80 |
begin |
|
81 |
str(PLongInt(t^.Handler)^, s); |
|
82 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
83 |
end else val(s, PLongInt(t^.Handler)^); |
|
84 |
vthwFloat: if c='$' then |
|
85 |
if s[0]=#0 then |
|
86 |
begin |
|
87 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
|
88 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
89 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
|
90 |
vtBoolean: if c='$' then |
|
91 |
if s[0]=#0 then |
|
92 |
begin |
|
93 |
str(ord(boolean(t^.Handler^)), s); |
|
94 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
95 |
end else |
|
96 |
begin |
|
97 |
val(s, ii); |
|
98 |
boolean(t^.Handler^):= not (ii = 0) |
|
99 |
end; |
|
100 |
end; |
|
101 |
exit |
|
102 |
end else t:= t^.Next |
|
103 |
end; |
|
104 |
case c of |
|
105 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
106 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
107 |
end; |
|
108 |
||
109 |
||
110 |
procedure StopMessages(Message: Longword); |
|
111 |
begin |
|
112 |
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else |
|
113 |
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else |
|
114 |
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else |
|
115 |
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else |
|
116 |
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true) |
|
117 |
end; |
|
118 |
||
119 |
{$INCLUDE "CCHandlers.inc"} |
|
120 |
||
121 |
procedure initModule; |
|
122 |
begin |
|
123 |
Variables:= nil; |
|
124 |
isDeveloperMode:= true; |
|
125 |
||
126 |
// NOTE: please, keep most frequently used commands on bottom |
|
127 |
RegisterVariable('flag' , vtCommand, @chFlag , false); |
|
128 |
RegisterVariable('script' , vtCommand, @chScript , false); |
|
129 |
RegisterVariable('proto' , vtCommand, @chCheckProto , true ); |
|
130 |
RegisterVariable('spectate', vtBoolean, @fastUntilLag , false); |
|
131 |
RegisterVariable('capture' , vtCommand, @chCapture , true ); |
|
132 |
RegisterVariable('rotmask' , vtCommand, @chRotateMask , true ); |
|
133 |
RegisterVariable('rdriven' , vtCommand, @chTeamLocal , false); |
|
134 |
RegisterVariable('map' , vtCommand, @chSetMap , false); |
|
135 |
RegisterVariable('theme' , vtCommand, @chSetTheme , false); |
|
136 |
RegisterVariable('seed' , vtCommand, @chSetSeed , false); |
|
137 |
RegisterVariable('template_filter', vtLongInt, @cTemplateFilter, false); |
|
138 |
RegisterVariable('mapgen' , vtLongInt, @cMapGen , false); |
|
139 |
RegisterVariable('maze_size',vtLongInt, @cMazeSize , false); |
|
140 |
RegisterVariable('delay' , vtLongInt, @cInactDelay , false); |
|
141 |
RegisterVariable('ready' , vtLongInt, @cReadyDelay , false); |
|
142 |
RegisterVariable('casefreq', vtLongInt, @cCaseFactor , false); |
|
143 |
RegisterVariable('healthprob', vtLongInt, @cHealthCaseProb, false); |
|
144 |
RegisterVariable('hcaseamount', vtLongInt, @cHealthCaseAmount, false); |
|
145 |
RegisterVariable('sd_turns', vtLongInt, @cSuddenDTurns , false); |
|
146 |
RegisterVariable('waterrise', vtLongInt, @cWaterRise , false); |
|
147 |
RegisterVariable('healthdec', vtLongInt, @cHealthDecrease, false); |
|
148 |
RegisterVariable('damagepct',vtLongInt, @cDamagePercent , false); |
|
149 |
RegisterVariable('minedudpct',vtLongInt,@cMineDudPercent, false); |
|
150 |
RegisterVariable('minesnum', vtLongInt, @cLandMines , false); |
|
151 |
RegisterVariable('explosives',vtLongInt,@cExplosives , false); |
|
152 |
RegisterVariable('gmflags' , vtLongInt, @GameFlags , false); |
|
153 |
RegisterVariable('trflags' , vtLongInt, @TrainingFlags , false); |
|
154 |
RegisterVariable('turntime', vtLongInt, @cHedgehogTurnTime, false); |
|
155 |
RegisterVariable('minestime',vtLongInt, @cMinesTime , false); |
|
156 |
RegisterVariable('fort' , vtCommand, @chFort , false); |
|
157 |
RegisterVariable('grave' , vtCommand, @chGrave , false); |
|
158 |
RegisterVariable('hat' , vtCommand, @chSetHat , false); |
|
159 |
RegisterVariable('quit' , vtCommand, @chQuit , true ); |
|
160 |
RegisterVariable('confirm' , vtCommand, @chConfirm , true ); |
|
161 |
RegisterVariable('+speedup', vtCommand, @chSpeedup_p , true ); |
|
162 |
RegisterVariable('-speedup', vtCommand, @chSpeedup_m , true ); |
|
163 |
RegisterVariable('zoomin' , vtCommand, @chZoomIn , true ); |
|
164 |
RegisterVariable('zoomout' , vtCommand, @chZoomOut , true ); |
|
165 |
RegisterVariable('zoomreset',vtCommand, @chZoomReset , true ); |
|
166 |
RegisterVariable('history' , vtCommand, @chHistory , true ); |
|
167 |
RegisterVariable('chat' , vtCommand, @chChat , true ); |
|
168 |
RegisterVariable('say' , vtCommand, @chSay , true ); |
|
169 |
RegisterVariable('team' , vtCommand, @chTeamSay , true ); |
|
170 |
RegisterVariable('ammomenu', vtCommand, @chAmmoMenu , true); |
|
171 |
RegisterVariable('+precise', vtCommand, @chPrecise_p , false); |
|
172 |
RegisterVariable('-precise', vtCommand, @chPrecise_m , false); |
|
173 |
RegisterVariable('+left' , vtCommand, @chLeft_p , false); |
|
174 |
RegisterVariable('-left' , vtCommand, @chLeft_m , false); |
|
175 |
RegisterVariable('+right' , vtCommand, @chRight_p , false); |
|
176 |
RegisterVariable('-right' , vtCommand, @chRight_m , false); |
|
177 |
RegisterVariable('+up' , vtCommand, @chUp_p , false); |
|
178 |
RegisterVariable('-up' , vtCommand, @chUp_m , false); |
|
179 |
RegisterVariable('+down' , vtCommand, @chDown_p , false); |
|
180 |
RegisterVariable('-down' , vtCommand, @chDown_m , false); |
|
181 |
RegisterVariable('+attack' , vtCommand, @chAttack_p , false); |
|
182 |
RegisterVariable('-attack' , vtCommand, @chAttack_m , false); |
|
183 |
RegisterVariable('switch' , vtCommand, @chSwitch , false); |
|
184 |
RegisterVariable('nextturn', vtCommand, @chNextTurn , false); |
|
185 |
RegisterVariable('timer' , vtCommand, @chTimer , false); |
|
186 |
RegisterVariable('taunt' , vtCommand, @chTaunt , false); |
|
187 |
RegisterVariable('setweap' , vtCommand, @chSetWeapon , false); |
|
188 |
RegisterVariable('slot' , vtCommand, @chSlot , false); |
|
189 |
RegisterVariable('put' , vtCommand, @chPut , false); |
|
190 |
RegisterVariable('ljump' , vtCommand, @chLJump , false); |
|
191 |
RegisterVariable('hjump' , vtCommand, @chHJump , false); |
|
192 |
RegisterVariable('+volup' , vtCommand, @chVol_p , true ); |
|
193 |
RegisterVariable('-volup' , vtCommand, @chVol_m , true ); |
|
194 |
RegisterVariable('+voldown', vtCommand, @chVol_m , true ); |
|
195 |
RegisterVariable('-voldown', vtCommand, @chVol_p , true ); |
|
196 |
RegisterVariable('findhh' , vtCommand, @chFindhh , true ); |
|
197 |
RegisterVariable('pause' , vtCommand, @chPause , true ); |
|
198 |
RegisterVariable('+cur_u' , vtCommand, @chCurU_p , true ); |
|
199 |
RegisterVariable('-cur_u' , vtCommand, @chCurU_m , true ); |
|
200 |
RegisterVariable('+cur_d' , vtCommand, @chCurD_p , true ); |
|
201 |
RegisterVariable('-cur_d' , vtCommand, @chCurD_m , true ); |
|
202 |
RegisterVariable('+cur_l' , vtCommand, @chCurL_p , true ); |
|
203 |
RegisterVariable('-cur_l' , vtCommand, @chCurL_m , true ); |
|
204 |
RegisterVariable('+cur_r' , vtCommand, @chCurR_p , true ); |
|
205 |
RegisterVariable('-cur_r' , vtCommand, @chCurR_m , true ); |
|
206 |
end; |
|
207 |
||
208 |
procedure freeModule; |
|
209 |
var t, tt: PVariable; |
|
210 |
begin |
|
211 |
tt:= Variables; |
|
212 |
Variables:= nil; |
|
213 |
while tt <> nil do |
|
214 |
begin |
|
215 |
t:= tt; |
|
216 |
tt:= tt^.Next; |
|
217 |
Dispose(t) |
|
218 |
end; |
|
219 |
end; |
|
220 |
||
4401 | 221 |
end. |