OSDN Git Service

Update copyright year
[radegast/radegast.git] / Radegast / Core / Commands / GoCommand.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
9 //     * Redistributions of source code must retain the above copyright notice,
10 //       this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //     * Neither the name of the application "Radegast", nor the names of its
15 //       contributors may be used to endorse or promote products derived from
16 //       this software without specific prior written permission.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Collections.Generic;
33 using System.Text;
34 using System.Text.RegularExpressions;
35 using System.Windows.Forms;
36
37 using OpenMetaverse;
38
39 namespace Radegast.Commands
40 {
41     public class GoCommand : RadegastCommand
42     {
43         Regex subCommand;
44         RegexOptions regexOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase;
45         TabsConsole TC { get { return Instance.TabConsole; } }
46         ObjectsConsole Objects;
47         ChatConsole Chat;
48         Vector3 targetPos = Vector3.Zero;
49         ConsoleWriteLine wl;
50
51         public GoCommand(RadegastInstance instance)
52             : base(instance)
53         {
54             Name = "go";
55             Description = "Moves avatar";
56             Usage = "go [tp] (distance|xyz|object|person|help) [additional args] (type \"" + CommandsManager.CmdPrefix + "go help\" for full usage)";
57
58             subCommand = new Regex(@"(?<subcmd>.*)\s*=\s*(?<subarg>.*)", regexOptions);
59             Chat = (ChatConsole)TC.Tabs["chat"].Control;
60         }
61
62         public override void Dispose()
63         {
64             Objects = null;
65             Chat = null;
66             base.Dispose();
67         }
68
69         void PrintUsage()
70         {
71             wl("Wrong arguments for \"go\" command. For detailed description type: " + CommandsManager.CmdPrefix + "go help");
72         }
73
74         void PrintFullUsage()
75         {
76             wl(@"Usage:
77
78 {0}go [tp] (distance|xyz|object|person|help) [additional args]
79 - tp is an optional parameter after go command. If speciefied use teleport instead of walking to reach the destination when parcel permits it.
80
81 Distance mode:
82 Specifies distance in meters to move with optional direction. If direction is not specified we move forward.
83 Examples:
84 {0}go 10 -- moves 10m forward
85 {0}go 15 e -- moves 15m to the east
86 {0}go tp 20 se -- teleports 20m to the southeast of our current position
87
88 XYZ mode:
89 Moves to X, Y and optionally Z coordinate
90 Examples:
91 {0}go xyz 128,128  -- walks toward the center of the sim, using our current elevation (Z)
92 {0}go tp xyz 32,32,128 -- teleports to postion 32,32,128 within our current region
93
94 Object mode:
95 Moves towards a named object
96 Examples:
97 {0}go object desk chair -- walk toward the closest object whose name begins with ""desk chair""
98 {0}go tp object dance floor -- teleports to the closest object whose name beings with ""dance floor""
99
100 Person mode:
101 Moves toward a person
102 Examples:
103 {0}go person Latif -- walk toward the closest person whose name begins with Latif
104 {0}go tp person John -- teleports to the closest person whose name begins with John", CommandsManager.CmdPrefix);
105         }
106
107         public override void Execute(string name, string[] cmdArgs, ConsoleWriteLine WriteLine)
108         {
109             if (Chat.InvokeRequired)
110             {
111                 if (!Instance.MonoRuntime || Chat.IsHandleCreated)
112                     Chat.Invoke(new MethodInvoker(() => Execute(name, cmdArgs, WriteLine)));
113                 return;
114             }
115             wl = WriteLine;
116
117             string cmd = string.Join(" ", cmdArgs);
118             List<string> args = new List<string>(Regex.Split(cmd, @"\s", regexOptions));
119
120             if (args.Count == 0) { PrintUsage(); return; }
121
122             bool useTP = false;
123             if (args[0] == "tp")
124             {
125                 useTP = true;
126                 args.RemoveAt(0);
127             }
128
129             if (args.Count == 0) { PrintUsage(); return; }
130
131             string subcmd = args[0];
132             args.RemoveAt(0);
133             string subarg = string.Empty;
134
135             // Move certain distance
136             int distance = 0;
137             if (int.TryParse(subcmd, out distance))
138             {
139                 if (distance < 1) return;
140                 Quaternion heading = Client.Self.Movement.BodyRotation;
141                 KnownHeading kh = null;
142
143                 if (args.Count > 0)
144                 {
145                     kh = StateManager.KnownHeadings.Find((KnownHeading h) => { return h.ID == args[0].ToUpper(); });
146                     if (kh != null)
147                         heading = kh.Heading;
148                 }
149
150                 targetPos = Client.Self.SimPosition + new Vector3((float)distance, 0f, 0f) * heading;
151                 Client.Self.Movement.BodyRotation = Client.Self.Movement.HeadRotation = heading;
152                 Client.Self.Movement.Camera.LookAt(Client.Self.SimPosition, targetPos);
153                 Client.Self.Movement.SendUpdate(true);
154                 WriteLine("Going {0} to {1:0},{2:0},{3:0}", kh == null ? string.Empty : kh.Name, targetPos.X, targetPos.Y, targetPos.Z);
155                 Instance.State.MoveTo(targetPos, useTP);
156                 return;
157             }
158
159             if (subcmd == "help")
160             {
161                 PrintFullUsage();
162                 return;
163             }
164
165             if (args.Count == 0) { PrintUsage(); return; }
166             subarg = string.Join(" ", args.ToArray());
167
168             // Move towards
169             switch (subcmd)
170             {
171                 case "xyz":
172                     string[] coords = Regex.Split(subarg, @"\D+");
173                     if (coords.Length < 2) { PrintUsage(); return; }
174                     int x = int.Parse(coords[0]);
175                     int y = int.Parse(coords[1]);
176                     int z = coords.Length > 2 ? int.Parse(coords[2]) : (int)Client.Self.SimPosition.Z;
177                     targetPos = new Vector3(x, y, z);
178                     WriteLine("Going to {0:0},{1:0},{2:0}", targetPos.X, targetPos.Y, targetPos.Z);
179                     Instance.State.MoveTo(targetPos, useTP);
180                     return;
181                 
182                 case "person":
183                     List<UUID> people = Chat.GetAvatarList();
184                     UUID person = people.Find((UUID id) => { return Instance.Names.Get(id).ToLower().StartsWith(subarg.ToLower()); });
185                     if (person == UUID.Zero)
186                     {
187                         WriteLine("Could not find {0}", subarg);
188                         return;
189                     }
190                     string pname = Instance.Names.Get(person);
191
192                     targetPos = Vector3.Zero;
193                     
194                     if (!Instance.State.TryFindAvatar(person, out targetPos))
195                     {
196                         WriteLine("Could not locate {0}", pname);
197                         return;
198                     }
199
200                     WriteLine("Going to {3} at {0:0},{1:0},{2:0}", targetPos.X, targetPos.Y, targetPos.Z, pname);
201                     Instance.State.MoveTo(targetPos, useTP);
202
203                     return;
204
205                 case "object":
206
207                     if (!TC.TabExists("objects"))
208                     {
209                         RadegastTab tab = TC.AddTab("objects", "Objects", new ObjectsConsole(Instance));
210                         tab.AllowClose = true;
211                         tab.AllowDetach = true;
212                         tab.Visible = true;
213                         tab.AllowHide = false;
214                         ((ObjectsConsole)tab.Control).RefreshObjectList();
215                         WriteLine("Objects list was not active. Started getting object names, please try again in a minute.");
216                         TC.Tabs["chat"].Select();
217                         return;
218                     }
219
220                     Objects = (ObjectsConsole)TC.Tabs["objects"].Control;
221                     List<Primitive> prims = Objects.GetObjectList();
222
223                     Primitive target = prims.Find((Primitive prim) =>
224                         {
225                             return prim.Properties != null
226                                 && prim.Properties.Name.ToLower().Contains(subarg.ToLower());
227                         });
228
229                     if (target == null)
230                     {
231                         WriteLine("Could not find '{0}' nearby", subarg);
232                         return;
233                     }
234
235                     targetPos = target.Position;
236
237                     WriteLine("Going to object '{0}' at {1:0},{2:0},{3:0}", target.Properties.Name, targetPos.X, targetPos.Y, targetPos.Z);
238                     Instance.State.MoveTo(targetPos, useTP);
239                     return;
240
241                 default:
242                     WriteLine("Unrecognized go command {0}", subcmd);
243                     return;
244             }
245
246         }
247     }
248 }