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