author | unc0rr |
Thu, 01 Dec 2011 11:30:06 +0400 | |
changeset 6473 | 2bed5ba1a7ea |
parent 6450 | 14224c9b4594 |
child 6580 | 6155187bf599 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
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 |
|
8 |
* |
|
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. |
|
13 |
* |
|
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 |
|
17 |
*) |
|
18 |
||
4373 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
21 |
unit uCommands; |
|
22 |
||
23 |
interface |
|
24 |
||
25 |
var isDeveloperMode: boolean; |
|
6450 | 26 |
type TVariableType = (vtCommand, vtLongInt, vtBoolean); |
4373 | 27 |
TCommandHandler = procedure (var params: shortstring); |
28 |
||
29 |
procedure initModule; |
|
30 |
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
|
31 |
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean); |
4373 | 32 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
33 |
procedure StopMessages(Message: Longword); |
|
34 |
||
35 |
implementation |
|
5554
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
36 |
uses Types, uConsts, uVariables, uConsole, uUtils, uDebug; |
4373 | 37 |
|
38 |
type PVariable = ^TVariable; |
|
39 |
TVariable = record |
|
40 |
Next: PVariable; |
|
41 |
Name: string[15]; |
|
42 |
VType: TVariableType; |
|
43 |
Handler: pointer; |
|
44 |
Trusted: boolean; |
|
45 |
end; |
|
46 |
||
47 |
var |
|
48 |
Variables: PVariable; |
|
49 |
||
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
|
50 |
procedure RegisterVariable(Name: shortstring; VType: TVariableType; p: pointer; Trusted: boolean); |
4373 | 51 |
var value: PVariable; |
52 |
begin |
|
53 |
New(value); |
|
54 |
TryDo(value <> nil, 'RegisterVariable: value = nil', true); |
|
55 |
FillChar(value^, sizeof(TVariable), 0); |
|
56 |
value^.Name:= Name; |
|
57 |
value^.VType:= VType; |
|
58 |
value^.Handler:= p; |
|
59 |
value^.Trusted:= Trusted; |
|
60 |
||
61 |
if Variables = nil then Variables:= value |
|
62 |
else begin |
|
63 |
value^.Next:= Variables; |
|
64 |
Variables:= value |
|
65 |
end; |
|
66 |
end; |
|
67 |
||
68 |
||
69 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); |
|
70 |
var ii: LongInt; |
|
5554
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
71 |
s: shortstring; |
4373 | 72 |
t: PVariable; |
73 |
c: char; |
|
74 |
begin |
|
75 |
//WriteLnToConsole(CmdStr); |
|
76 |
if CmdStr[0]=#0 then exit; |
|
77 |
c:= CmdStr[1]; |
|
6450 | 78 |
if (c = '/') or (c = '$') then Delete(CmdStr, 1, 1) else c:= '/'; |
4373 | 79 |
s:= ''; |
80 |
SplitBySpace(CmdStr, s); |
|
4900 | 81 |
AddFileLog('[Cmd] ' + c + CmdStr + ' (' + inttostr(length(s)) + ')'); |
4373 | 82 |
t:= Variables; |
83 |
while t <> nil do |
|
84 |
begin |
|
85 |
if t^.Name = CmdStr then |
|
86 |
begin |
|
87 |
if TrustedSource or t^.Trusted then |
|
88 |
case t^.VType of |
|
89 |
vtCommand: if c='/' then |
|
90 |
begin |
|
91 |
TCommandHandler(t^.Handler)(s); |
|
92 |
end; |
|
93 |
vtLongInt: if c='$' then |
|
94 |
if s[0]=#0 then |
|
95 |
begin |
|
96 |
str(PLongInt(t^.Handler)^, s); |
|
5554
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
97 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
98 |
end else val(s, PLongInt(t^.Handler)^); |
4373 | 99 |
vtBoolean: if c='$' then |
100 |
if s[0]=#0 then |
|
101 |
begin |
|
102 |
str(ord(boolean(t^.Handler^)), s); |
|
5554
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
103 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
b27ed6c6f538
Revert ParseCommandOverride change since it appears to be badly screwing up scripting. Need to find out why. This backs out 7f57d0c7816a and the recent workaround.
nemo
parents:
5352
diff
changeset
|
104 |
end else |
4373 | 105 |
begin |
106 |
val(s, ii); |
|
107 |
boolean(t^.Handler^):= not (ii = 0) |
|
108 |
end; |
|
109 |
end; |
|
110 |
exit |
|
111 |
end else t:= t^.Next |
|
112 |
end; |
|
113 |
case c of |
|
114 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
115 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
116 |
end; |
|
117 |
||
118 |
||
119 |
procedure StopMessages(Message: Longword); |
|
120 |
begin |
|
121 |
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else |
|
122 |
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else |
|
123 |
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else |
|
124 |
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else |
|
125 |
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true) |
|
126 |
end; |
|
127 |
||
128 |
procedure initModule; |
|
129 |
begin |
|
130 |
Variables:= nil; |
|
131 |
isDeveloperMode:= true; |
|
132 |
end; |
|
133 |
||
134 |
procedure freeModule; |
|
135 |
var t, tt: PVariable; |
|
136 |
begin |
|
137 |
tt:= Variables; |
|
138 |
Variables:= nil; |
|
139 |
while tt <> nil do |
|
140 |
begin |
|
141 |
t:= tt; |
|
142 |
tt:= tt^.Next; |
|
143 |
Dispose(t) |
|
144 |
end; |
|
145 |
end; |
|
146 |
||
4406
beb4de0af990
Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents:
4403
diff
changeset
|
147 |
end. |