author | koda |
Sat, 15 Jan 2011 21:32:44 +0100 | |
branch | 0.9.15 |
changeset 4751 | 849740a91d36 |
parent 4650 | 0167cbb6aee3 |
child 4900 | 8ad0e23e6d63 |
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 |
||
17 |
implementation |
|
4414 | 18 |
uses Types, uConsts, uVariables, uConsole, uUtils, uDebug; |
4373 | 19 |
|
20 |
type PVariable = ^TVariable; |
|
21 |
TVariable = record |
|
22 |
Next: PVariable; |
|
23 |
Name: string[15]; |
|
24 |
VType: TVariableType; |
|
25 |
Handler: pointer; |
|
26 |
Trusted: boolean; |
|
27 |
end; |
|
28 |
||
29 |
var |
|
30 |
Variables: PVariable; |
|
31 |
||
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
|
32 |
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean); |
4373 | 33 |
var value: PVariable; |
34 |
begin |
|
35 |
New(value); |
|
36 |
TryDo(value <> nil, 'RegisterVariable: value = nil', true); |
|
37 |
FillChar(value^, sizeof(TVariable), 0); |
|
38 |
value^.Name:= Name; |
|
39 |
value^.VType:= VType; |
|
40 |
value^.Handler:= p; |
|
41 |
value^.Trusted:= Trusted; |
|
42 |
||
43 |
if Variables = nil then Variables:= value |
|
44 |
else begin |
|
45 |
value^.Next:= Variables; |
|
46 |
Variables:= value |
|
47 |
end; |
|
48 |
end; |
|
49 |
||
50 |
||
51 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
|
52 |
var ii: LongInt; |
|
53 |
s: shortstring; |
|
54 |
t: PVariable; |
|
55 |
c: char; |
|
56 |
begin |
|
57 |
//WriteLnToConsole(CmdStr); |
|
58 |
if CmdStr[0]=#0 then exit; |
|
59 |
c:= CmdStr[1]; |
|
60 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/'; |
|
61 |
s:= ''; |
|
62 |
SplitBySpace(CmdStr, s); |
|
4650 | 63 |
{$IFDEF DEBUGFILE}AddFileLog('[Cmd] ' + c + CmdStr + ' (' + inttostr(length(s)) + ')');{$ENDIF} |
4373 | 64 |
t:= Variables; |
65 |
while t <> nil do |
|
66 |
begin |
|
67 |
if t^.Name = CmdStr then |
|
68 |
begin |
|
69 |
if TrustedSource or t^.Trusted then |
|
70 |
case t^.VType of |
|
71 |
vtCommand: if c='/' then |
|
72 |
begin |
|
73 |
TCommandHandler(t^.Handler)(s); |
|
74 |
end; |
|
75 |
vtLongInt: if c='$' then |
|
76 |
if s[0]=#0 then |
|
77 |
begin |
|
78 |
str(PLongInt(t^.Handler)^, s); |
|
79 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
80 |
end else val(s, PLongInt(t^.Handler)^); |
|
81 |
vthwFloat: if c='$' then |
|
82 |
if s[0]=#0 then |
|
83 |
begin |
|
84 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
|
85 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
86 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
|
87 |
vtBoolean: if c='$' then |
|
88 |
if s[0]=#0 then |
|
89 |
begin |
|
90 |
str(ord(boolean(t^.Handler^)), s); |
|
91 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
92 |
end else |
|
93 |
begin |
|
94 |
val(s, ii); |
|
95 |
boolean(t^.Handler^):= not (ii = 0) |
|
96 |
end; |
|
97 |
end; |
|
98 |
exit |
|
99 |
end else t:= t^.Next |
|
100 |
end; |
|
101 |
case c of |
|
102 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
103 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
104 |
end; |
|
105 |
||
106 |
||
107 |
procedure StopMessages(Message: Longword); |
|
108 |
begin |
|
109 |
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else |
|
110 |
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else |
|
111 |
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else |
|
112 |
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else |
|
113 |
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true) |
|
114 |
end; |
|
115 |
||
116 |
procedure initModule; |
|
117 |
begin |
|
118 |
Variables:= nil; |
|
119 |
isDeveloperMode:= true; |
|
120 |
end; |
|
121 |
||
122 |
procedure freeModule; |
|
123 |
var t, tt: PVariable; |
|
124 |
begin |
|
125 |
tt:= Variables; |
|
126 |
Variables:= nil; |
|
127 |
while tt <> nil do |
|
128 |
begin |
|
129 |
t:= tt; |
|
130 |
tt:= tt^.Next; |
|
131 |
Dispose(t) |
|
132 |
end; |
|
133 |
end; |
|
134 |
||
4406
beb4de0af990
Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents:
4403
diff
changeset
|
135 |
end. |