PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Commands/CmdCtf.cs

https://github.com/cazzar/MCaznowl-Lava
C# | 318 lines | 281 code | 19 blank | 18 comment | 82 complexity | 4500db8f4e060aef2edc6f2fa29c2236 MD5 | raw file
  1/*
  2	Copyright 2010 MCLawl (Modified for use with MCForge)
  3	
  4	Dual-licensed under the	Educational Community License, Version 2.0 and
  5	the GNU General Public License, Version 3 (the "Licenses"); you may
  6	not use this file except in compliance with the Licenses. You may
  7	obtain a copy of the Licenses at
  8	
  9	http://www.opensource.org/licenses/ecl2.php
 10	http://www.gnu.org/licenses/gpl-3.0.html
 11	
 12	Unless required by applicable law or agreed to in writing,
 13	software distributed under the Licenses are distributed on an "AS IS"
 14	BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 15	or implied. See the Licenses for the specific language governing
 16	permissions and limitations under the Licenses.
 17*/
 18using System;
 19using System.Collections.Generic;
 20using System.Linq;
 21using System.Text;
 22
 23namespace MCForge
 24{
 25    public class CmdCTF : Command
 26    {
 27        public override string name { get { return "ctf"; } }
 28        public override string shortcut { get { return ""; } }
 29        public override string type { get { return "other"; } }
 30        public override bool museumUsable { get { return false; } }
 31        public CmdCTF() { }
 32        public override LevelPermission defaultRank { get { return LevelPermission.Admin; } }
 33        public override void Use(Player p, string message)
 34        {
 35            int num = message.Split(' ').Length;
 36            if (num == 3)
 37            {
 38                string[] strings = message.Split(' ');
 39
 40                for (int i = 0; i < num; i++)
 41                {
 42                    strings[i] = strings[i].ToLower();
 43                }
 44
 45                if (strings[0] == "team")
 46                {
 47                    if (strings[1] == "add")
 48                    {
 49                        string color = c.Parse(strings[2]);
 50                        if (color == ""){Player.SendMessage(p, "Invalid team color chosen."); return;}
 51                        char teamCol = (char)color[1];
 52                        switch (teamCol)
 53                        {
 54                            case '2':
 55                            case '5':
 56                            case '8':
 57                            case '9':
 58                            case 'c':
 59                            case 'e':
 60                            case 'f':
 61                                AddTeam(p, color);
 62                                break;
 63                            default:
 64                                Player.SendMessage(p, "Invalid team color chosen.");
 65                                return;
 66                        }
 67                    }
 68                    else if (strings[1] == "remove")
 69                    {
 70                        string color = c.Parse(strings[2]);
 71                        if (color == "") { Player.SendMessage(p, "Invalid team color chosen."); return; }
 72                        char teamCol = (char)color[1];
 73                        switch (teamCol)
 74                        {
 75                            case '2':
 76                            case '5':
 77                            case '8':
 78                            case '9':
 79                            case 'c':
 80                            case 'e':
 81                            case 'f':
 82                                RemoveTeam(p, color);
 83                                break;
 84                            default:
 85                                Player.SendMessage(p, "Invalid team color chosen.");
 86                                return;
 87                        }
 88                    }
 89                }
 90            }
 91            else if (num == 2)
 92            {
 93                string[] strings = message.Split(' ');
 94
 95                for (int i = 0; i < num; i++)
 96                {
 97                    strings[i] = strings[i].ToLower();
 98                }
 99
100                if (strings[0] == "debug")
101                {
102                    Debug(p, strings[1]);
103                }
104                else if (strings[0] == "flag")
105                {
106                    string color = c.Parse(strings[1]);
107                    if (color == "") { Player.SendMessage(p, "Invalid team color chosen."); return; }
108                    char teamCol = (char)color[1];
109                    if (p.level.ctfgame.teams.Find(team => team.color == teamCol) == null) { Player.SendMessage(p, "Invalid team color chosen."); return; }
110                    CatchPos cpos;
111                    cpos.x = 0; cpos.y = 0; cpos.z = 0; cpos.color = color; p.blockchangeObject = cpos;
112                    Player.SendMessage(p, "Place a block to determine where to place the flag.");
113                    p.ClearBlockchange();
114                    p.Blockchange += new Player.BlockchangeEventHandler(FlagBlockChange);
115                }
116                else if (strings[0] == "spawn")
117                {
118                    string color = c.Parse(strings[1]);
119                    if (color == "") { Player.SendMessage(p, "Invalid team color chosen."); return; }
120                    char teamCol = (char)color[1];
121                    if (p.level.ctfgame.teams.Find(team => team.color == teamCol) == null) { Player.SendMessage(p, "Invalid team color chosen."); return; }
122                    AddSpawn(p, color);
123                    
124                }
125                else if (strings[0] == "points")
126                {
127                    int i = 0;
128                    Int32.TryParse(strings[1], out i);
129                    if (i == 0) { Player.SendMessage(p, "You must choose a points value greater than 0!"); return; }
130                    p.level.ctfgame.maxPoints = i;
131                    Player.SendMessage(p, "Max round points has been set to " + i);
132                }
133            }
134            else if (num == 1)
135            {
136                if (message.ToLower() == "start")
137                {
138                    if (!p.level.ctfmode)
139                    {
140                        p.level.ctfmode = true;
141                    }
142                    p.level.ctfgame.gameOn = true;
143                    p.level.ctfgame.GameStart();
144                }
145                else if (message.ToLower() == "stop")
146                {
147                    if (p.level.ctfmode)
148                    {
149                        p.level.ctfmode = false;
150                    }
151                    p.level.ctfmode = false;
152                    p.level.ctfgame.gameOn = false;
153                    p.level.ChatLevel(p.color + p.name + Server.DefaultColor + " has ended the game");
154                }
155                else if (message.ToLower() == "ff")
156                {
157                    if (p.level.ctfgame.friendlyfire)
158                    {
159                        p.level.ChatLevel("Friendly fire has been disabled.");
160                        p.level.ctfgame.friendlyfire = false;
161                    }
162                    else
163                    {
164                        p.level.ChatLevel("Friendly fire has been enabled.");
165                        p.level.ctfgame.friendlyfire = true;
166                    }
167                }
168                else if (message.ToLower() == "clear")
169                {
170                    List<Team> storedT = new List<Team>();
171                    for (int i = 0; i < p.level.ctfgame.teams.Count; i++)
172                    {
173                        storedT.Add(p.level.ctfgame.teams[i]);
174                    }
175                    foreach (Team t in storedT)
176                    {
177                        p.level.ctfgame.RemoveTeam("&" + t.color);
178                    }
179                    p.level.ctfgame.onTeamCheck.Stop();
180                    p.level.ctfgame.onTeamCheck.Dispose();
181                    p.level.ctfgame.gameOn = false;
182                    p.level.ctfmode = false;
183                    p.level.ctfgame = new CTFGame();
184                    p.level.ctfgame.mapOn = p.level;
185                    Player.SendMessage(p, "CTF data has been cleared.");
186                }
187
188                else if (message.ToLower() == "")
189                {
190                    if (p.level.ctfmode)
191                    {
192                        p.level.ctfmode = false;
193                        p.level.ChatLevel("CTF Mode has been disabled.");
194
195                    }
196                    else if (!p.level.ctfmode)
197                    {
198                        p.level.ctfmode = true;
199                        p.level.ChatLevel("CTF Mode has been enabled.");
200                    }
201                }
202            }
203        }
204        public void AddSpawn(Player p, string color)
205        {
206            char teamCol = (char)color[1];
207            ushort x, y, z, rotx;
208            x = (ushort)(p.pos[0] / 32);
209            y = (ushort)(p.pos[1] / 32);
210            z = (ushort)(p.pos[2] / 32);
211            rotx = (ushort)(p.rot[0]);
212            p.level.ctfgame.teams.Find(team => team.color == teamCol).AddSpawn(x, y, z, rotx, 0);
213            Player.SendMessage(p, "Added spawn for " + p.level.ctfgame.teams.Find(team => team.color == teamCol).teamstring);
214        }
215
216        public void AddTeam(Player p, string color)
217        {
218            char teamCol = (char)color[1];
219            if (p.level.ctfgame.teams.Find(team => team.color == teamCol)!= null){Player.SendMessage(p, "That team already exists."); return;}
220            p.level.ctfgame.AddTeam(color);
221        }
222
223        public void RemoveTeam(Player p, string color)
224        {
225            char teamCol = (char)color[1];
226            if (p.level.ctfgame.teams.Find(team => team.color == teamCol) == null) { Player.SendMessage(p, "That team does not exist."); return; }
227            p.level.ctfgame.RemoveTeam(color);
228        }
229
230        public void AddFlag(Player p, string col, ushort x, ushort y, ushort z)
231        {
232            char teamCol = (char)col[1];
233            Team workTeam = p.level.ctfgame.teams.Find(team => team.color == teamCol);
234
235            workTeam.flagBase[0] = x;
236            workTeam.flagBase[1] = y;
237            workTeam.flagBase[2] = z;
238
239            workTeam.flagLocation[0] = x;
240            workTeam.flagLocation[1] = y;
241            workTeam.flagLocation[2] = z;
242            workTeam.Drawflag();
243        }
244
245        public void Debug(Player p, string col)
246        {
247            if (col.ToLower() == "flags")
248            {
249                foreach (Team team in p.level.ctfgame.teams)
250                {
251                    Player.SendMessage(p, "Drawing flag for " + team.teamstring);
252                    team.Drawflag();
253                }
254                return;
255            }
256            else if (col.ToLower() == "spawn")
257            {
258                foreach (Team team in p.level.ctfgame.teams)
259                {
260                    foreach (Player player in team.players)
261                    {
262                        team.SpawnPlayer(player);
263                    }
264                }
265                return;
266            }
267            string color = c.Parse(col);
268            char teamCol = (char)color[1];
269            Team workTeam = p.level.ctfgame.teams.Find(team => team.color == teamCol);
270            string debugteams = "";
271            for (int i = 0; i < p.level.ctfgame.teams.Count; i++)
272            {
273                debugteams += p.level.ctfgame.teams[i].teamstring + ", ";
274            }
275            Player.SendMessage(p, "Player Debug: Team: " + p.team.teamstring/* + ", hasFlag: " + p.hasflag.teamstring + ", carryingFlag: " + p.carryingFlag*/);
276            Player.SendMessage(p, "CTFGame teams: " + debugteams);
277            string playerlist = "";
278            foreach (Player player in workTeam.players)
279            {
280                playerlist += player.name + ", ";
281            }
282            Player.SendMessage(p, "Player list: " + playerlist);
283            Player.SendMessage(p, "Points: " + workTeam.points + ", MapOn: " + workTeam.mapOn.name + ", flagishome: " + workTeam.flagishome + ", spawnset: " + workTeam.spawnset);
284            Player.SendMessage(p, "FlagBase[0]: " + workTeam.flagBase[0] + ", [1]: " + workTeam.flagBase[1] + ", [2]: " + workTeam.flagBase[2]);
285            Player.SendMessage(p, "FlagLocation[0]: " + workTeam.flagLocation[0] + ", [1]: " + workTeam.flagLocation[1] + ", [2]: " + workTeam.flagLocation[2]);
286         //   Player.SendMessage(p, "Spawn[0]: " + workTeam.spawn[0] + ", [1]: " + workTeam.spawn[1] + ", [2]: " + workTeam.spawn[2] + ", [3]: " + workTeam.spawn[3] + ", [4]: " + workTeam.spawn[4]);
287        }
288
289
290        void FlagBlockChange(Player p, ushort x, ushort y, ushort z, byte type)
291        {
292            CatchPos bp = (CatchPos)p.blockchangeObject;
293            byte b = p.level.GetTile(x, y, z);
294            p.SendBlockchange(x, y, z, b);
295            p.ClearBlockchange();
296            AddFlag(p, bp.color, x, y, z);
297        }
298
299
300        public override void Help(Player p)
301        {
302            Player.SendMessage(p, "/ctf team add [color] - Initializes team of specified color.");
303            Player.SendMessage(p, "/ctf team remove [color] - Removes team of specified color.");
304            Player.SendMessage(p, "/ctf flag [color] - Sets the flag base for specified team.");
305            Player.SendMessage(p, "/ctf spawn [color] - Adds a spawn for the team specified from where you are standing.");
306            Player.SendMessage(p, "/ctf points [num] - Sets max round points.  Default is 3.");
307            Player.SendMessage(p, "/ctf ff - Enables or disables friendly fire.  Default is off.");
308            Player.SendMessage(p, "/ctf - Turns CTF mode on for the map.  Must be enabled to play!");
309            Player.SendMessage(p, "/ctf start - Starts the game!");
310            Player.SendMessage(p, "/ctf stop - Stops the game.");
311            Player.SendMessage(p, "/ctf clear - Removes all CTF data from map.  Use sparingly.");
312            Player.SendMessage(p, "/ctf debug [flags/spawn] - To debug flags or spawn.");
313            Player.SendMessage(p, "/help team - To see info about team joining leaving etc.");
314        }
315
316        public struct CatchPos { public ushort x, y, z; public string color;}
317    }
318}