author | vitiv <nikita.utiu@gmail.com> |
Mon, 31 Dec 2012 12:12:23 +0200 | |
changeset 8350 | 14b938faec69 |
parent 7850 | fcbb024090a4 |
child 8498 | eecadca7db50 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
4976 | 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; |
|
7805 | 26 |
var isExternalSource: boolean; |
6898 | 27 |
type TCommandHandler = procedure (var params: shortstring); |
4373 | 28 |
|
29 |
procedure initModule; |
|
30 |
procedure freeModule; |
|
7409 | 31 |
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean; Rand: boolean); |
6898 | 32 |
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean); |
7805 | 33 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); inline; |
34 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource, ExternalSource: boolean); |
|
6919
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
35 |
procedure ParseTeamCommand(s: shortstring); |
4373 | 36 |
procedure StopMessages(Message: Longword); |
37 |
||
38 |
implementation |
|
7407 | 39 |
uses uConsts, uVariables, uConsole, uUtils, uDebug, SDLh; |
4373 | 40 |
|
41 |
type PVariable = ^TVariable; |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
42 |
TVariable = record |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
43 |
Next: PVariable; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
44 |
Name: string[15]; |
6898 | 45 |
Handler: TCommandHandler; |
7409 | 46 |
Trusted, Rand: boolean; |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
47 |
end; |
4373 | 48 |
|
7850 | 49 |
var Variables: PVariable; |
4373 | 50 |
|
6898 | 51 |
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean); |
7407 | 52 |
begin |
53 |
RegisterVariable(Name, p, Trusted, false); |
|
54 |
end; |
|
7409 | 55 |
procedure RegisterVariable(Name: shortstring; p: TCommandHandler; Trusted: boolean; Rand: boolean); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
56 |
var |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
57 |
value: PVariable; |
4373 | 58 |
begin |
59 |
New(value); |
|
60 |
TryDo(value <> nil, 'RegisterVariable: value = nil', true); |
|
61 |
FillChar(value^, sizeof(TVariable), 0); |
|
62 |
value^.Name:= Name; |
|
63 |
value^.Handler:= p; |
|
64 |
value^.Trusted:= Trusted; |
|
7409 | 65 |
value^.Rand:= Rand; |
4373 | 66 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
67 |
if Variables = nil then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
68 |
Variables:= value |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
69 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
70 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
71 |
value^.Next:= Variables; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
72 |
Variables:= value |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
73 |
end; |
4373 | 74 |
end; |
75 |
||
76 |
||
7805 | 77 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource: boolean); inline; |
78 |
begin |
|
79 |
ParseCommand(CmdStr, TrustedSource, false) |
|
80 |
end; |
|
81 |
||
82 |
procedure ParseCommand(CmdStr: shortstring; TrustedSource, ExternalSource: boolean); |
|
6992 | 83 |
var s: shortstring; |
4373 | 84 |
t: PVariable; |
85 |
c: char; |
|
86 |
begin |
|
7806 | 87 |
isExternalSource:= ExternalSource or ((CurrentTeam <> nil) and CurrentTeam^.ExtDriven); |
4373 | 88 |
//WriteLnToConsole(CmdStr); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
89 |
if CmdStr[0]=#0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
90 |
exit; |
4373 | 91 |
c:= CmdStr[1]; |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
92 |
if (c = '/') or (c = '$') then |
6898 | 93 |
Delete(CmdStr, 1, 1); |
4373 | 94 |
s:= ''; |
95 |
SplitBySpace(CmdStr, s); |
|
6898 | 96 |
AddFileLog('[Cmd] ' + CmdStr + ' (' + inttostr(length(s)) + ')'); |
7407 | 97 |
|
4373 | 98 |
t:= Variables; |
99 |
while t <> nil do |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
100 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
101 |
if t^.Name = CmdStr then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
102 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
103 |
if TrustedSource or t^.Trusted then |
7412
9e5aa3c8dc62
Ok. *these* should be safe with just this one extra check...
nemo
parents:
7409
diff
changeset
|
104 |
begin |
7426
55b49cc1f33a
Changes for the benefit of pas2c. Use downto in for loops to avoid repeated calls of Random/GetRandom. Wrap nots.
nemo
parents:
7412
diff
changeset
|
105 |
if t^.Rand and (not CheckNoTeamOrHH) then |
7412
9e5aa3c8dc62
Ok. *these* should be safe with just this one extra check...
nemo
parents:
7409
diff
changeset
|
106 |
CheckSum:= CheckSum xor LongWord(SDLNet_Read32(@CmdStr)) xor LongWord(s[0]) xor GameTicks; |
6898 | 107 |
t^.Handler(s); |
7412
9e5aa3c8dc62
Ok. *these* should be safe with just this one extra check...
nemo
parents:
7409
diff
changeset
|
108 |
end; |
6898 | 109 |
exit |
110 |
end |
|
111 |
else |
|
112 |
t:= t^.Next |
|
113 |
end; |
|
4373 | 114 |
case c of |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
115 |
'$': WriteLnToConsole(errmsgUnknownVariable + ': "$' + CmdStr + '"') |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
116 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
117 |
WriteLnToConsole(errmsgUnknownCommand + ': "/' + CmdStr + '"') end |
4373 | 118 |
end; |
119 |
||
6919
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
120 |
procedure ParseTeamCommand(s: shortstring); |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
121 |
var Trusted: boolean; |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
122 |
begin |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
123 |
Trusted:= (CurrentTeam <> nil) |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
124 |
and (not CurrentTeam^.ExtDriven) |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
125 |
and (CurrentHedgehog^.BotLevel = 0); |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
126 |
ParseCommand(s, Trusted); |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
127 |
if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
128 |
ParseCommand('gencmd R', true) |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
129 |
end; |
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
130 |
|
bf7433e62b9c
in uTouch dont use bools to represent keystrokes, but issue ParseCommands
Xeli
parents:
6898
diff
changeset
|
131 |
|
4373 | 132 |
|
133 |
procedure StopMessages(Message: Longword); |
|
134 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
135 |
if (Message and gmLeft) <> 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
136 |
ParseCommand('/-left', true) |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
137 |
else if (Message and gmRight) <> 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
138 |
ParseCommand('/-right', true) |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
139 |
else if (Message and gmUp) <> 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
140 |
ParseCommand('/-up', true) |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
141 |
else if (Message and gmDown) <> 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
142 |
ParseCommand('/-down', true) |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
143 |
else if (Message and gmAttack) <> 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6450
diff
changeset
|
144 |
ParseCommand('/-attack', true) |
4373 | 145 |
end; |
146 |
||
147 |
procedure initModule; |
|
148 |
begin |
|
149 |
Variables:= nil; |
|
150 |
isDeveloperMode:= true; |
|
151 |
end; |
|
152 |
||
153 |
procedure freeModule; |
|
154 |
var t, tt: PVariable; |
|
155 |
begin |
|
156 |
tt:= Variables; |
|
157 |
Variables:= nil; |
|
158 |
while tt <> nil do |
|
159 |
begin |
|
160 |
t:= tt; |
|
161 |
tt:= tt^.Next; |
|
162 |
Dispose(t) |
|
163 |
end; |
|
164 |
end; |
|
165 |
||
4406
beb4de0af990
Increase teams to 8 to match the 8 colours, fix issue #108, reenable rope length modifier
nemo
parents:
4403
diff
changeset
|
166 |
end. |