author | koda |
Sat, 28 May 2011 16:40:23 +0200 | |
changeset 5219 | 9e2a17ab178b |
parent 4976 | 088d40d8aba2 |
child 5352 | 7f57d0c7816a |
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; |
|
26 |
type TVariableType = (vtCommand, vtLongInt, vthwFloat, vtBoolean); |
|
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 |
|
4414 | 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; |
|
71 |
s: shortstring; |
|
72 |
t: PVariable; |
|
73 |
c: char; |
|
74 |
begin |
|
75 |
//WriteLnToConsole(CmdStr); |
|
76 |
if CmdStr[0]=#0 then exit; |
|
77 |
c:= CmdStr[1]; |
|
78 |
if c in ['/', '$'] then Delete(CmdStr, 1, 1) else c:= '/'; |
|
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); |
|
97 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
98 |
end else val(s, PLongInt(t^.Handler)^); |
|
99 |
vthwFloat: if c='$' then |
|
100 |
if s[0]=#0 then |
|
101 |
begin |
|
102 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
|
103 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
104 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
|
105 |
vtBoolean: if c='$' then |
|
106 |
if s[0]=#0 then |
|
107 |
begin |
|
108 |
str(ord(boolean(t^.Handler^)), s); |
|
109 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
110 |
end else |
|
111 |
begin |
|
112 |
val(s, ii); |
|
113 |
boolean(t^.Handler^):= not (ii = 0) |
|
114 |
end; |
|
115 |
end; |
|
116 |
exit |
|
117 |
end else t:= t^.Next |
|
118 |
end; |
|
119 |
case c of |
|
120 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
121 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
122 |
end; |
|
123 |
||
124 |
||
125 |
procedure StopMessages(Message: Longword); |
|
126 |
begin |
|
127 |
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else |
|
128 |
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else |
|
129 |
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else |
|
130 |
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else |
|
131 |
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true) |
|
132 |
end; |
|
133 |
||
134 |
procedure initModule; |
|
135 |
begin |
|
136 |
Variables:= nil; |
|
137 |
isDeveloperMode:= true; |
|
138 |
end; |
|
139 |
||
140 |
procedure freeModule; |
|
141 |
var t, tt: PVariable; |
|
142 |
begin |
|
143 |
tt:= Variables; |
|
144 |
Variables:= nil; |
|
145 |
while tt <> nil do |
|
146 |
begin |
|
147 |
t:= tt; |
|
148 |
tt:= tt^.Next; |
|
149 |
Dispose(t) |
|
150 |
end; |
|
151 |
end; |
|
152 |
||
4406
beb4de0af990
Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents:
4403
diff
changeset
|
153 |
end. |