/turret - place a turret that shoots nearby players

/turret - place a turret that shoots nearby players

Postby tommyz_ » 19 May 2014, 01:38

One of my old commands, and a little buggy because when you turn off the turret, it turns them all off at once, and when you place a turret after, they all start shooting again. The only way reset the turrets so far, would be to reset the server, and then place new turrets. These turrets also do not save with the server and are only temporary. This command would be good for some sort of gungame server or warzone style server. Enjoy!!

How it works is that you just type /turret, and it will ask you to place a turret in any location, and then it automatically starts shooting. It uses an AI system to track nearby players in order to shoot them, similiar to the botai tracking code. It also uses the gun code to shoot.

Code: Select all
//Created by tommyz_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Timers;

namespace MCDzienny
{
    public class CmdTurret : Command
    {
        public override string name { get { return "turret"; } }
        public override string shortcut { get { return "t"; } }
        public override string type { get { return "gun"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
        public CmdTurret() { }
        public Level pr;
        public Player R;

        //Keep shooting people every number of seconds:
        public static System.Timers.Timer checkplayer = new System.Timers.Timer(1000);
        public List<CmdTurret> Turrets = new List<CmdTurret>();
        public override void Use(Player p, string message)
        {
            if (message == "stop")
            {
                p.aiming = false;

                foreach (CmdTurret tu in Turrets)
                { checkplayer.Stop(); Turrets.Remove(tu); }
                p.SendMessage("All of your turrets have been shut off."); return;
            }
            Level l;
            l = p.level;
            pr = p.level;
            R = p;

           // if (!l.AllowGuns) { Player.SendMessage(p, "All Guns on this level are Disabled!"); return; }
           // if (!Server.AllowGuns) { Player.SendMessage(p, "All Server Guns are Disabled!"); return; }
           // if (!Server.gun) { Player.SendMessage(p, "This Gun Command is Disabled!!"); return; }

            if (p.hasflag != null) { Player.SendMessage(p, "You can't use a gun while you have the flag!"); return; }
            Pos cpos;


            p.ClearBlockchange();


            cpos.ending = 0;
            if (message.ToLower() == "destroy") cpos.ending = 1;
            else if (message.ToLower() == "explode") cpos.ending = 2;
            else if (message.ToLower() == "laser") cpos.ending = 3;
            else if (message.ToLower() == "teleport" || message.ToLower() == "tp") cpos.ending = -1;
            else if (message != "") { Help(p); return; }

            cpos.x = 0; cpos.y = 0; cpos.z = 0; p.blockchangeObject = cpos;
            p.ClearBlockchange();
            // ushort[] click = p.lastClick;

            p.SendMessage("Place a block anywhere to place a turret.");

            //Blockchange1(p, p.pos[0], p.pos[1], p.pos[2], Block.rock);


            // p.manualChange(click[0], click[1], click[2], 0, Block.rock);
            //  p.manualChange += new Player.BlockchangeEventHandler(Blockchange1);

            p.Blockchange += new Player.BlockchangeEventHandler(Blockchange);

            p.aiming = true;
        }
        public void Blockchange(Player p, ushort x, ushort y, ushort z, byte type)
        {
            p.SendMessage("Turret Placed!"); p.ClearBlockchange();
            Turrets.Add(this);
            StartBlockTimer(p, p.pos[0], p.pos[1], p.pos[2], Block.rock);

        }
        public void StartBlockTimer(Player p, ushort x, ushort y, ushort z, byte type)
        {
            foreach (CmdTurret a in Turrets)
            {
                if (a != null)
                {
                    checkplayer.Elapsed += delegate
                    {

                        foreach (Player who in Player.players)
                        {                    //&& p != who)
                            if (who.level == p.level)//quick code
                            {
                                ShootPlayer2(p, x, y, z, type, who.pos[0], who.pos[1], who.pos[2], who);
                                // p.level.BlockchangeLevel += new Level.BlockchangeEventHandler(Blockchange2);
                            }
                        }
                    };
                    checkplayer.Start();
                }
            }
        }

        // public void Blockchange2(Player who, ushort x, ushort y, ushort z, byte type)
        // { ShootPlayer2(who, x, y, z, type, who.rot[0], who.rot[1]); }


        public void ShootPlayer2(Player p, ushort x, ushort y, ushort z, byte type, ushort whox, ushort whoy, ushort whoz, Player who)
        {
            //get player postion, click to get block position and place the block down         
            //start a test by shooting a gun from a remote location. 
            //test the set of the position with a block then continually shoot from that same postion.
            //make an AI that would find if players were near to shoot them.
            //starts a timer thread to check for players within a distance.

            // ushort x1 = 0; ushort y1 = 0; ushort z1 = 0;
            // x1 = (ushort)(p.pos[0] / 32);
            // y1 = (ushort)((p.pos[1] / 32) - 1);
            // z1 = (ushort)(p.pos[2] / 32);

            byte by = p.level.GetTile(x, y, z);



            //p.SendBlockchange((ushort)(x1 + 8), (ushort)(y1 + 8), (ushort)(z1 -8), by);
            //Command.all.Find("click").Use(p, (x + 8) + " " + (y + 8) + " " + (z - 8), true);
            // p.manualChange(ushort(), (y + ay), (z - ay), 0, Block.rock);
            Pos bp = (Pos)p.blockchangeObject;
            // byte bx = (byte)whox;
            //byte byx = (byte)whoy;

            byte[] foundRot = new byte[2] { 0, 0 };
            ushort[] foundPos = new ushort[3] { 0, 0, 0 };
            bool movement = false;
            int currentNum, foundNum = (32 * 75);
            byte[] rot = new byte[2] { 0, 0 };

            //convert player2 position to a rotation of the turret pointer according to pos of block xyz
            currentNum = Math.Abs(who.pos[0] - x) + Math.Abs(who.pos[1] - y) + Math.Abs(who.pos[2] - z);
            if (currentNum < foundNum)
            {
                foundNum = currentNum;
                foundPos = who.pos;
                foundRot = who.rot;
                movement = true;
                rot[1] = (byte)(255 - foundRot[1]);
                if (foundRot[0] < 128) rot[0] = (byte)(foundRot[0] + 128);
                else rot[0] = (byte)(foundRot[0] - 128);
            }
            //finding the angle of rotation in 3D view.  This may require 2 solutions.
            // double Rotx = Math.Tan((double)(who.pos[0] / y));
            //  double Roty = Math.Tan((double)(who.pos[1] / x));
            //Rotx = Math.Atan(who.pos[0] / y);
            // Roty = Math.Atan(who.pos[1] / x);

            // rot[0] = (byte)Rotx;
            // rot[1] = (byte)Roty;
            int xx;

            // ushort x1 = 0; ushort y1 = 0; ushort z1 = 0;
            // x1 = (ushort)(whox / 32);
            // y1 = (ushort)((whoy / 32) - 1);
            //  z1 = (ushort)(whoz / 32);

            double a = Math.Sin(((double)(128 - rot[0]) / 256) * 2 * Math.PI);
            double b = Math.Cos(((double)(128 - rot[0]) / 256) * 2 * Math.PI);
            double c = Math.Cos(((double)(rot[1] + 64) / 256) * 2 * Math.PI);
            // p.SendMessage("p.rot0= " + p.rot[0] + " p.rot1= " + p.rot[1] + " p.pos0= " + p.pos[0] + " p.pos1= " + p.pos[1] + " p.pos2= " + p.pos[2]);
            double bigDiag = Math.Sqrt(Math.Sqrt(p.level.width * p.level.width + p.level.height * p.level.height) + p.level.depth * p.level.depth + p.level.width * p.level.width);

            List<CatchPos> previous = new List<CatchPos>();
            List<CatchPos> allBlocks = new List<CatchPos>();
            CatchPos pos;

            if (p.modeType != Block.air)
                type = p.modeType;

            Thread gunThread = new Thread(new ThreadStart(delegate
            {
                //Set turret at block placed position:
                ushort startX = (ushort)(x / 32);
                ushort startY = (ushort)(y / 32);
                ushort startZ = (ushort)(z / 32);

                pos.x = (ushort)Math.Round(startX + (double)(a * 3));
                pos.y = (ushort)Math.Round(startY + (double)(c * 3));
                pos.z = (ushort)Math.Round(startZ + (double)(b * 3));

                for (double t = 4; bigDiag > t; t++)
                {
                    pos.x = (ushort)Math.Round(startX + (double)(a * t));
                    pos.y = (ushort)Math.Round(startY + (double)(c * t));
                    pos.z = (ushort)Math.Round(startZ + (double)(b * t));

                    by = p.level.GetTile(pos.x, pos.y, pos.z);

                    if (by != Block.air && !allBlocks.Contains(pos))
                    {
                        if (p.level.physics < 2 || bp.ending <= 0)
                        {
                            break;
                        }
                        else
                        {
                            if (bp.ending == 1)
                            {
                                if ((!Block.LavaKill(by) && !Block.NeedRestart(by)) && by != Block.glass)
                                {
                                    break;
                                }
                            }
                            else if (p.level.physics >= 3)
                            {
                                if (by != Block.glass)
                                {
                                    p.level.MakeExplosion(pos.x, pos.y, pos.z, 1);
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    p.level.Blockchange(pos.x, pos.y, pos.z, type);
                    previous.Add(pos);
                    allBlocks.Add(pos);

                    bool comeOut = false;
                    foreach (Player pl in Player.players)
                    {
                        if (pl.level == p.level)
                        {
                            if ((ushort)(pl.pos[0] / 32) == pos.x || (ushort)(pl.pos[0] / 32 + 1) == pos.x || (ushort)(pl.pos[0] / 32 - 1) == pos.x)
                            {
                                if ((ushort)(pl.pos[1] / 32) == pos.y || (ushort)(pl.pos[1] / 32 + 1) == pos.y || (ushort)(pl.pos[1] / 32 - 1) == pos.y)
                                {
                                    if ((ushort)(pl.pos[2] / 32) == pos.z || (ushort)(pl.pos[2] / 32 + 1) == pos.z || (ushort)(pl.pos[2] / 32 - 1) == pos.z)
                                    {
                                        if (p.level.ctfmode && !p.level.ctfgame.friendlyfire && p.team == pl.team)
                                        {
                                            comeOut = true;
                                            break;
                                        }
                                        if (p.level.ctfmode)
                                        {
                                            pl.health = pl.health - 25;
                                            if (pl.health > 0)
                                            {
                                                pl.SendMessage("You have been shot!  You have &c" + pl.health + Server.DefaultColor + " health remaining.");
                                                comeOut = true;
                                                break;
                                            }
                                        }


                                        if (p.level.physics >= 3 && bp.ending >= 2)
                                            pl.HandleDeath(Block.stone, " was blown up by a turret!", true);
                                        else
                                            pl.HandleDeath(Block.stone, " was shot by a turret!");                                     

                                        comeOut = true;



                                    }
                                }
                            }
                        }
                    }
                    if (comeOut) break;

                    if (t > 12 && bp.ending != 3)
                    {
                        pos = previous[0];
                        p.level.Blockchange(pos.x, pos.y, pos.z, Block.air);
                        previous.Remove(pos);
                    }

                    if (bp.ending != 3) Thread.Sleep(20);
                }

                if (bp.ending == -1)
                    try
                    {
                        unchecked { p.SendPos((byte)-1, (ushort)(previous[previous.Count - 3].x * 32), (ushort)(previous[previous.Count - 3].y * 32 + 32), (ushort)(previous[previous.Count - 3].z * 32), rot[0], rot[1]); }
                    }
                    catch { }
                if (bp.ending == 3) Thread.Sleep(400);

                foreach (CatchPos pos1 in previous)
                {
                    p.level.Blockchange(pos1.x, pos1.y, pos1.z, Block.air);
                    if (bp.ending != 3) Thread.Sleep(20);
                }
            }));
            gunThread.Start();
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/turret [at end] - Allows you to place turrets on the ground to shoot at people");
            Player.SendMessage(p, "/turret stop - stops all your turrets from shooting.");
            Player.SendMessage(p, "Available [at end] values: &cexplode, destroy, laser, tp");
        }

        public struct CatchPos { public ushort x, y, z; }
        public struct Pos { public ushort x, y, z; public int ending; }
    }
}
tommyz_
 
Posts: 12
Joined: 17 May 2014, 02:46

Postby joppiesaus » 19 May 2014, 19:10

My first reaction:
Spoiler:


Awesome! Sublime! <ok>
You might want to make a class or struct inside CmdTurret called Turret, and put in there "pr" and "R", and change the List<CmdTurret> to List<Turret>
I didn't really study the code further though.

Other than that, I will try it out soon! It's awesome! :)
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: /turret - place a turret that shoots nearby players

Postby tommyz_ » 20 May 2014, 03:15

Thanks! :D
Hey ya that turret list keeps track of all the turrets and stores them in a list, so that when we do /turret off it turns them all off in the list.
tommyz_
 
Posts: 12
Joined: 17 May 2014, 02:46


Return to Custom Commands

Who is online

Users browsing this forum: No registered users and 1 guest

cron