author | nemo |
Wed, 10 Aug 2011 11:45:02 -0400 | |
changeset 5533 | cf32b5928cbe |
parent 5352 | 7f57d0c7816a |
child 5554 | b27ed6c6f538 |
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 |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
36 |
uses Types, uConsts, uVariables, uConsole, uUtils, uDebug, uScript; |
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; |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
71 |
s, i, o: shortstring; |
4373 | 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 |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
88 |
begin |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
89 |
if (c <> '$') or (s[0] <> #0) then s:= ParseCommandOverride(CmdStr, s); |
4373 | 90 |
case t^.VType of |
91 |
vtCommand: if c='/' then |
|
92 |
begin |
|
93 |
TCommandHandler(t^.Handler)(s); |
|
94 |
end; |
|
95 |
vtLongInt: if c='$' then |
|
96 |
if s[0]=#0 then |
|
97 |
begin |
|
98 |
str(PLongInt(t^.Handler)^, s); |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
99 |
i:= inttostr(PLongInt(t^.Handler)^); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
100 |
o:= ParseCommandOverride(CmdStr, i); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
101 |
if i <> o then val(o, PLongInt(t^.Handler)^) |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
102 |
else WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
103 |
end |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
104 |
else val(s, PLongInt(t^.Handler)^); |
4373 | 105 |
vthwFloat: if c='$' then |
106 |
if s[0]=#0 then |
|
107 |
begin |
|
108 |
//str(PhwFloat(t^.Handler)^:4:6, s); |
|
109 |
WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
|
110 |
end else; //val(s, PhwFloat(t^.Handler)^, i); |
|
111 |
vtBoolean: if c='$' then |
|
112 |
if s[0]=#0 then |
|
113 |
begin |
|
114 |
str(ord(boolean(t^.Handler^)), s); |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
115 |
if boolean(t^.Handler^) then i:= '1' |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
116 |
else i:= '0'; |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
117 |
o:= ParseCommandOverride(CmdStr, i); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
118 |
if i <> o then |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
119 |
begin |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
120 |
val(o, ii); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
121 |
boolean(t^.Handler^):= not (ii = 0) |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
122 |
end |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
123 |
else WriteLnToConsole('$' + CmdStr + ' is "' + s + '"'); |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
124 |
end |
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
125 |
else |
4373 | 126 |
begin |
127 |
val(s, ii); |
|
128 |
boolean(t^.Handler^):= not (ii = 0) |
|
129 |
end; |
|
130 |
end; |
|
5352
7f57d0c7816a
Fix random weapons with per-hog ammo, fix ammo store loadout number in scripting for per-clan and per-hog ammo, add an advanced script hook into parsecommand to override values, add check for empty map in chSetMap, load script earlier in game params from frontend
nemo
parents:
4976
diff
changeset
|
131 |
end; |
4373 | 132 |
exit |
133 |
end else t:= t^.Next |
|
134 |
end; |
|
135 |
case c of |
|
136 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
|
137 |
else WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
|
138 |
end; |
|
139 |
||
140 |
||
141 |
procedure StopMessages(Message: Longword); |
|
142 |
begin |
|
143 |
if (Message and gmLeft) <> 0 then ParseCommand('/-left', true) else |
|
144 |
if (Message and gmRight) <> 0 then ParseCommand('/-right', true) else |
|
145 |
if (Message and gmUp) <> 0 then ParseCommand('/-up', true) else |
|
146 |
if (Message and gmDown) <> 0 then ParseCommand('/-down', true) else |
|
147 |
if (Message and gmAttack) <> 0 then ParseCommand('/-attack', true) |
|
148 |
end; |
|
149 |
||
150 |
procedure initModule; |
|
151 |
begin |
|
152 |
Variables:= nil; |
|
153 |
isDeveloperMode:= true; |
|
154 |
end; |
|
155 |
||
156 |
procedure freeModule; |
|
157 |
var t, tt: PVariable; |
|
158 |
begin |
|
159 |
tt:= Variables; |
|
160 |
Variables:= nil; |
|
161 |
while tt <> nil do |
|
162 |
begin |
|
163 |
t:= tt; |
|
164 |
tt:= tt^.Next; |
|
165 |
Dispose(t) |
|
166 |
end; |
|
167 |
end; |
|
168 |
||
4406
beb4de0af990
Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents:
4403
diff
changeset
|
169 |
end. |