using System;
using System.Timers;
using System.Collections.Generic;
namespace MCDzienny
{
public class CmdPVP : Command
{
List<Pos> position = new List<Pos>();
Timer timer = new Timer(100);
public int interval = 0;
public override string name { get { return "pvp"; } }
public override string shortcut { get { return ""; } }
public override string type { get { return "other"; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
public override bool ConsoleAccess { get { return false; } }
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Player.players.ForEachSync(p =>
{
if (p.ExtraData.ContainsKey("playing"))
{
if ((bool)p.ExtraData["playing"] == true)
{
if (p.onTrain == false)
{
p.onTrain = true;
p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(p_Blockchange);
}
ushort pos1 = (ushort)(p.pos[0] / 32), pos2 = (ushort)(p.pos[1] / 32), pos3 = (ushort)(p.pos[2] / 32), pos4 = (ushort)(pos2 - 1);
p.level.Blockchange(pos1, pos2, pos3, Block.glass, true, "");
p.level.Blockchange(pos1, pos4, pos3, Block.glass, true, "");
Pos pos;
pos.x = pos1; pos.y = pos2; pos.z = pos3;
if (!position.Contains(pos))
{
position.Add(pos);
}
pos.y -= 1;
if (!position.Contains(pos))
{
position.Add(pos);
}
interval += 1;
if (interval >= 5)
{
position.ForEach(delegate(Pos here)
{
p.level.Blockchange(here.x, here.y, here.z, Block.air, true, "");
});
interval = 0;
}
}
}
});
}
void p_Blockchange(Player p, ushort x, ushort y, ushort z, byte type)
{
if (p.level.GetTile(x, y, z) == Block.glass)
{
CheckWho(p, x, y, z);
}
}
void CheckWho(Player p, ushort x, ushort y, ushort z)
{
Player.players.ForEach(who =>
{
if (who.ExtraData.ContainsKey("playing"))
{
if ((bool)who.ExtraData["playing"] == true)
{
ushort pos1 = (ushort)(who.pos[0] / 32), pos2 = (ushort)(who.pos[1] / 32), pos3 = (ushort)(who.pos[2] / 32);
double distance = Math.Sqrt(Math.Pow(Math.Abs(pos1 - x), 2) + Math.Pow(Math.Abs(pos2 - y), 2) + Math.Pow(Math.Abs(pos3 - z), 2));
if (distance <= 2)
{
GetHit(p, who);
}
}
}
});
}
void GetHit(Player p, Player who)
{
if (who != p)
{
who.health -= 10;
ushort pos1 = (ushort)(p.pos[0] / 32), pos2 = (ushort)(p.pos[1] / 32), pos3 = (ushort)(p.pos[2] / 32), whopos1 = (ushort)(who.pos[0] / 32), whopos2 = (ushort)(who.pos[1] / 32), whopos3 = (ushort)(who.pos[2] / 32);
if (pos1 > whopos1)
whopos1 -= 1;
else if (pos1 < whopos1)
whopos1 += 1;
if (pos3 > whopos3)
whopos3 -= 1;
else if (pos3 < whopos3)
whopos3 += 1;
Command.all.Find("move").Use(who, whopos1 + " " + whopos2 + " " + whopos3);
Player.SendMessage(who, who.health.ToString() + " life left.");
if (who.health <= 0)
{
Player.GlobalMessage(who.PublicName + " was dominated by " + p.PublicName);
Command.all.Find("spawn").Use(who, "");
who.health = 100;
}
}
}
public override void Use(Player p, string message)
{
string[] split = message.Trim().Split(' ');
switch (split[0])
{
case "join":
if (!p.ExtraData.ContainsKey("playing"))
{
p.ExtraData.Add("playing", true);
}
p.ExtraData["playing"] = true;
Player.GlobalMessage(p.PublicName + " has joined the pvp game.");
return;
case "leave":
if (!p.ExtraData.ContainsKey("playing"))
{
p.ExtraData.Add("playing", false);
}
p.ExtraData["playing"] = false;
Player.GlobalMessage(p.PublicName + " has left the pvp game.");
p.ClearBlockchange();
return;
case "begin":
case "start":
if (p.group.Permission >= LevelPermission.Operator)
{
timer.Start();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
Player.GlobalMessage("PVP has begun");
}
return;
case "end":
case "stop":
if (p.group.Permission >= LevelPermission.Operator)
{
timer.Stop();
timer.Elapsed -= new ElapsedEventHandler(timer_Elapsed);
Player.GlobalMessage("PVP has ended");
}
return;
}
Help(p);
}
public override void Help(Player p)
{
Player.SendMessage(p, "/pvp join/leave -- joins/leaves the pvp game");
if (p.group.Permission >= LevelPermission.Operator)
Player.SendMessage(p, "/pvp start/end -- starts/end the pvp game");
}
public struct Pos
{
public ushort x, y, z;
}
}
}
Users browsing this forum: No registered users and 7 guests