Ejemplo n.º 1
0
        public static void ProcessDatagram(byte[] datagram)
        {
            var serverUpdate = new ServerUpdate();

            switch ((DatagramID)datagram[0])
            {
            case DatagramID.entityUpdate:
                #region entityUpdate
                var entityUpdate = new EntityUpdate(datagram);

                if (entityUpdate.guid == guid)
                {
                    CwRam.Teleport(entityUpdate.position);
                    break;
                }
                else
                {
                    entityUpdate.Write(cwriter);
                }

                if (players.ContainsKey(entityUpdate.guid))
                {
                    entityUpdate.Merge(players[entityUpdate.guid]);
                }
                else
                {
                    players.Add(entityUpdate.guid, entityUpdate);
                }

                if (entityUpdate.name != null)
                {
                    RefreshPlayerlist();
                }
                break;

                #endregion
            case DatagramID.attack:
                #region attack
                var attack = new Attack(datagram);

                var hit = new Hit()
                {
                    target    = attack.Target,
                    damage    = attack.Damage,
                    critical  = attack.Critical ? 1 : 0,
                    stuntime  = attack.Stuntime,
                    position  = players[attack.Target].position,
                    isYellow  = attack.Skill,
                    type      = attack.Type,
                    showlight = (byte)(attack.ShowLight ? 1 : 0)
                };
                serverUpdate.hits.Add(hit);
                serverUpdate.Write(cwriter);
                break;

                #endregion
            case DatagramID.shoot:
                #region shoot
                var shootDatagram = new Resources.Datagram.Shoot(datagram);

                var shootPacket = new Resources.Packet.Shoot()
                {
                    position   = shootDatagram.Position,
                    velocity   = shootDatagram.Velocity,
                    scale      = shootDatagram.Scale,
                    particles  = shootDatagram.Particles,
                    projectile = shootDatagram.Projectile,
                    chunkX     = (int)shootDatagram.Position.x / 0x1000000,
                    chunkY     = (int)shootDatagram.Position.y / 0x1000000
                };
                serverUpdate.shoots.Add(shootPacket);
                serverUpdate.Write(cwriter);
                break;

                #endregion
            case DatagramID.proc:
                #region proc
                var proc = new Proc(datagram);

                var passiveProc = new PassiveProc()
                {
                    target   = proc.Target,
                    type     = proc.Type,
                    modifier = proc.Modifier,
                    duration = proc.Duration
                };
                serverUpdate.passiveProcs.Add(passiveProc);
                serverUpdate.Write(cwriter);
                break;

                #endregion
            case DatagramID.chat:
                #region chat
                var chat        = new Chat(datagram);
                var chatMessage = new ChatMessage()
                {
                    sender  = chat.Sender,
                    message = chat.Text
                };
                try {
                    chatMessage.Write(cwriter, true);
                }
                catch (Exception ex) {
                    if (!(ex is NullReferenceException || ex is ObjectDisposedException))
                    {
                        throw;
                    }
                }
                if (chat.Sender == 0)
                {
                    form.Log(chat.Text + "\n", Color.Magenta);
                }
                else
                {
                    form.Log(players[chat.Sender].name + ": ", Color.Cyan);
                    form.Log(chat.Text + "\n", Color.White);
                }
                break;

                #endregion
            case DatagramID.time:
                #region time
                var igt = new InGameTime(datagram);

                var time = new Time()
                {
                    time = igt.Time
                };
                time.Write(cwriter);
                break;

                #endregion
            case DatagramID.interaction:
                #region interaction
                var interaction = new Interaction(datagram);

                var entityAction = new EntityAction()
                {
                    chunkX = interaction.ChunkX,
                    chunkY = interaction.ChunkY,
                    index  = interaction.Index,
                    type   = ActionType.staticInteraction
                };
                //serverUpdate..Add();
                //serverUpdate.Write(cwriter);
                break;

                #endregion
            case DatagramID.staticUpdate:
                #region staticUpdate
                var staticUpdate = new StaticUpdate(datagram);

                var staticEntity = new ServerUpdate.StaticEntity()
                {
                    chunkX   = (int)(staticUpdate.Position.x / (65536 * 256)),
                    chunkY   = (int)(staticUpdate.Position.y / (65536 * 256)),
                    id       = staticUpdate.Id,
                    type     = (int)staticUpdate.Type,
                    position = staticUpdate.Position,
                    rotation = (int)staticUpdate.Direction,
                    size     = staticUpdate.Size,
                    closed   = staticUpdate.Closed ? 1 : 0,
                    time     = staticUpdate.Time,
                    guid     = staticUpdate.User
                };
                var staticServerUpdate = new ServerUpdate();
                staticServerUpdate.statics.Add(staticEntity);
                staticServerUpdate.Write(cwriter, true);
                break;

                #endregion
            case DatagramID.block:
                //var block = new Block(datagram);
                //TODO
                break;

            case DatagramID.particle:
                #region particle
                var particleDatagram = new Resources.Datagram.Particle(datagram);

                var particleSubPacket = new ServerUpdate.Particle()
                {
                    position = particleDatagram.Position,
                    velocity = particleDatagram.Velocity,
                    color    = new Resources.Utilities.FloatVector()
                    {
                        x = particleDatagram.Color.R / 255,
                        y = particleDatagram.Color.G / 255,
                        z = particleDatagram.Color.B / 255
                    },
                    alpha  = particleDatagram.Color.A / 255,
                    size   = particleDatagram.Size,
                    count  = particleDatagram.Count,
                    type   = particleDatagram.Type,
                    spread = particleDatagram.Spread
                };
                serverUpdate.particles.Add(particleSubPacket);
                serverUpdate.Write(cwriter, true);
                break;

                #endregion
            case DatagramID.connect:
                #region connect
                var connect = new Connect(datagram);
                guid = connect.Guid;

                var join = new Join()
                {
                    guid = guid,
                    junk = new byte[0x1168]
                };
                join.Write(cwriter, true);

                var mapseed = new MapSeed()
                {
                    seed = connect.Mapseed
                };
                mapseed.Write(cwriter, true);
                break;

                #endregion
            case DatagramID.disconnect:
                #region disconnect
                var disconnect = new Disconnect(datagram);
                var pdc        = new EntityUpdate()
                {
                    guid      = disconnect.Guid,
                    hostility = 255,     //workaround for DC because i dont like packet2
                    HP        = 0
                };
                pdc.Write(cwriter);
                players.Remove(disconnect.Guid);
                RefreshPlayerlist();
                break;

                #endregion
            case DatagramID.specialMove:
                var specialMove = new SpecialMove(datagram);
                switch (specialMove.Id)
                {
                case SpecialMoveID.taunt:
                    if (players.ContainsKey(specialMove.Guid))
                    {
                        CwRam.Teleport(players[specialMove.Guid].position);
                        CwRam.Freeze(5000);
                    }
                    break;

                case SpecialMoveID.cursedArrow:
                    break;

                case SpecialMoveID.arrowRain:
                    break;

                case SpecialMoveID.shrapnel:
                    break;

                case SpecialMoveID.smokeBomb:
                    var smoke = new ServerUpdate();
                    smoke.particles.Add(new ServerUpdate.Particle()
                    {
                        count    = 1000,
                        spread   = 5f,
                        type     = ParticleType.noGravity,
                        size     = 5f,
                        velocity = new Resources.Utilities.FloatVector(),
                        color    = new Resources.Utilities.FloatVector()
                        {
                            x = 1f,
                            y = 1f,
                            z = 1f
                        },
                        alpha    = 1f,
                        position = players[specialMove.Guid].position
                    });
                    smoke.Write(cwriter);
                    break;

                case SpecialMoveID.iceWave:
                    break;

                case SpecialMoveID.confusion:
                    break;

                case SpecialMoveID.shadowStep:
                    break;

                default:
                    break;
                }
                break;

            default:
                break;
            }
        }
Ejemplo n.º 2
0
        public static void ProcessClientPacket(int packetID)
        {
            switch ((PacketID)packetID)
            {
            case PacketID.entityUpdate:
                #region entityUpdate
                var entityUpdate = new EntityUpdate(creader);
                if (players.ContainsKey(entityUpdate.guid))
                {
                    entityUpdate.Filter(players[entityUpdate.guid]);
                    entityUpdate.Merge(players[entityUpdate.guid]);
                }
                else
                {
                    players.Add(entityUpdate.guid, entityUpdate);
                }
                if (entityUpdate.name != null)
                {
                    RefreshPlayerlist();
                }
                SendUDP(entityUpdate.Data);
                break;

                #endregion
            case PacketID.entityAction:
                #region entity action
                EntityAction entityAction = new EntityAction(creader);
                switch (entityAction.type)
                {
                case ActionType.talk:
                    break;

                case ActionType.staticInteraction:
                    break;

                case ActionType.pickup:
                    break;

                case ActionType.drop:         //send item back to dropper because dropping is disabled to prevent chatspam
                    if (form.radioButtonDestroy.Checked)
                    {
                        new ChatMessage()
                        {
                            message = "item destroyed"
                        }.Write(cwriter);
                    }
                    else
                    {
                        var serverUpdate = new ServerUpdate();
                        var pickup       = new ServerUpdate.Pickup()
                        {
                            guid = guid, item = entityAction.item
                        };
                        serverUpdate.pickups.Add(pickup);
                        if (form.radioButtonDuplicate.Checked)
                        {
                            serverUpdate.pickups.Add(pickup);
                        }
                        serverUpdate.Write(cwriter);
                    }
                    break;

                case ActionType.callPet:
                    var petCall = new SpecialMove()
                    {
                        Guid = guid
                    };
                    SendUDP(petCall.data);
                    break;

                default:
                    //unknown type
                    break;
                }
                break;

                #endregion
            case PacketID.hit:
                #region hit
                var hit    = new Hit(creader);
                var attack = new Attack()
                {
                    Target    = (ushort)hit.target,
                    Damage    = hit.damage,
                    Stuntime  = hit.stuntime,
                    Skill     = hit.isYellow,
                    Type      = hit.type,
                    ShowLight = hit.showlight == 1,
                    Critical  = hit.critical == 1
                };
                SendUDP(attack.data);
                lastTarget = attack.Target;
                break;

                #endregion
            case PacketID.passiveProc:
                #region passiveProc
                var passiveProc = new PassiveProc(creader);

                var proc = new Proc()
                {
                    Target   = (ushort)passiveProc.target,
                    Type     = passiveProc.type,
                    Modifier = passiveProc.modifier,
                    Duration = passiveProc.duration
                };
                SendUDP(proc.data);

                break;

                #endregion
            case PacketID.shoot:
                #region shoot
                var shootPacket = new Resources.Packet.Shoot(creader);

                var shootDatagram = new Resources.Datagram.Shoot()
                {
                    Position   = shootPacket.position,
                    Velocity   = shootPacket.velocity,
                    Scale      = shootPacket.scale,
                    Particles  = shootPacket.particles,
                    Projectile = shootPacket.projectile
                };
                SendUDP(shootDatagram.data);
                break;

                #endregion
            case PacketID.chat:
                #region chat
                var chatMessage = new ChatMessage(creader);

                if (chatMessage.message.ToLower() == @"/plane")
                {
                    Console.Beep();
                    var serverUpdate = new ServerUpdate()
                    {
                        blockDeltas = VoxModel.Parse("model.vox"),
                    };
                    foreach (var block in serverUpdate.blockDeltas)
                    {
                        block.position.x += 8286946;
                        block.position.y += 8344456;
                        block.position.z += 220;
                    }

                    serverUpdate.Write(cwriter);
                }
                else
                {
                    var chat = new Chat(chatMessage.message)
                    {
                        Sender = guid    //client doesn't send this //(ushort)chatMessage.sender
                    };
                    SendUDP(chat.data);
                }
                break;

                #endregion
            case PacketID.chunk:
                #region chunk
                var chunk = new Chunk(creader);
                break;

                #endregion
            case PacketID.sector:
                #region sector
                var sector = new Sector(creader);
                break;

                #endregion
            case PacketID.version:
                #region version
                var version = new ProtocolVersion(creader);
                if (version.version != 3)
                {
                    version.version = 3;
                    version.Write(cwriter, true);
                }
                else
                {
                    var connect = new Connect();
                    SendUDP(connect.data);
                }
                break;

                #endregion
            default:
                form.Log("unknown client packet\n", Color.Magenta);
                break;
            }
        }
Ejemplo n.º 3
0
        public void ProcessDatagram(byte[] datagram, Player source)
        {
            switch ((DatagramID)datagram[0])
            {
            case DatagramID.entityUpdate:
                #region entityUpdate
                var entityUpdate = new EntityUpdate(datagram);

                string ACmessage = AntiCheat.Inspect(entityUpdate);
                if (ACmessage != "ok")
                {
                    //var kickMessage = new ChatMessage() {
                    //    message = "illegal " + ACmessage
                    //};
                    //kickMessage.Write(player.writer, true);
                    //Console.WriteLine(player.entityData.name + " kicked for illegal " + kickMessage.message);
                    //Thread.Sleep(100); //thread is about to run out anyway so np
                    //Kick(player);
                    //return;
                }
                if (entityUpdate.name != null)
                {
                    //Announce.Join(entityUpdate.name, player.entityData.name, players);
                }

                entityUpdate.entityFlags |= 1 << 5;  //enable friendly fire flag for pvp
                if (!source.entityData.IsEmpty)      //dont filter the first packet
                //entityUpdate.Filter(player.entityData);
                {
                }
                if (!entityUpdate.IsEmpty)
                {
                    //entityUpdate.Broadcast(players, 0);
                    BroadcastUDP(entityUpdate.Data, source);
                    if (entityUpdate.HP == 0 && source.entityData.HP > 0)
                    {
                        BroadcastUDP(Tomb.Show(source).Data);
                    }
                    else if (source.entityData.HP == 0 && entityUpdate.HP > 0)
                    {
                        BroadcastUDP(Tomb.Hide(source).Data);
                    }
                    entityUpdate.Merge(source.entityData);
                }
                break;

                #endregion
            case DatagramID.attack:
                #region attack
                var attack = new Attack(datagram);
                source.lastTarget = attack.Target;
                if (players.ContainsKey(attack.Target))      //in case the target is a tombstone
                {
                    SendUDP(datagram, players[attack.Target]);
                }
                break;

                #endregion
            case DatagramID.shoot:
                #region shoot
                var shoot = new Resources.Datagram.Shoot(datagram);
                BroadcastUDP(datagram, source);     //pass to all players except source
                break;

                #endregion
            case DatagramID.proc:
                #region proc
                var proc = new Proc(datagram);

                switch (proc.Type)
                {
                case ProcType.bulwalk:
                    SendUDP(new Chat(string.Format("bulwalk: {0}% dmg reduction", 1.0f - proc.Modifier)).data, source);
                    break;

                case ProcType.poison:
                    var poisonTickDamage = new Attack()
                    {
                        Damage = proc.Modifier,
                        Target = proc.Target
                    };
                    var         target = players[poisonTickDamage.Target];
                    Func <bool> tick   = () => {
                        bool f = players.ContainsKey(poisonTickDamage.Target);
                        if (f)
                        {
                            SendUDP(poisonTickDamage.data, target);
                        }
                        return(!f);
                    };
                    Tools.DoLater(tick, 500, 7);
                    //Poison(players[proc.Target], poisonTickDamage);
                    break;

                case ProcType.manashield:
                    SendUDP(new Chat(string.Format("manashield: {0}", proc.Modifier)).data, source);
                    break;

                case ProcType.warFrenzy:
                case ProcType.camouflage:
                case ProcType.fireSpark:
                case ProcType.intuition:
                case ProcType.elusivenes:
                case ProcType.swiftness:
                    break;

                default:

                    break;
                }
                BroadcastUDP(datagram, source);     //pass to all players except source
                break;

                #endregion
            case DatagramID.chat:
                #region chat
                var chat = new Chat(datagram);
                if (chat.Text.StartsWith("/"))
                {
                    Command.Server(chat.Text, source, this);     //wip
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write(players[chat.Sender].entityData.name + ": ");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(chat.Text);
                    BroadcastUDP(datagram, null, true);     //pass to all players
                }
                break;

                #endregion
            case DatagramID.interaction:
                #region interaction
                var interaction = new Interaction(datagram);
                BroadcastUDP(datagram, source);     //pass to all players except source
                break;

                #endregion
            case DatagramID.connect:
                #region connect
                var connect = new Connect(datagram)
                {
                    Guid    = (ushort)source.entityData.guid,
                    Mapseed = Database.mapseed
                };
                SendUDP(connect.data, source);

                foreach (Player player in players.Values)
                {
                    if (player.playing)
                    {
                        SendUDP(player.entityData.Data, source);
                    }
                }
                source.playing = true;
                //Task.Delay(100).ContinueWith(t => Load_world_delayed(source)); //WIP, causes crash when player disconnects before executed
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(source.IpEndPoint.Address + " is now playing");
                break;

                #endregion
            case DatagramID.disconnect:
                #region disconnect
                var disconnect = new Disconnect(datagram);
                source.playing = false;
                BroadcastUDP(datagram, source, true);
                source.entityData = new EntityUpdate()
                {
                    guid = source.entityData.guid
                };

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(source.IpEndPoint.Address + " is now lurking");
                break;

                #endregion
            case DatagramID.specialMove:
                #region specialMove
                var specialMove = new SpecialMove(datagram);
                switch (specialMove.Id)
                {
                case SpecialMoveID.taunt:
                    var targetGuid = specialMove.Guid;
                    specialMove.Guid = (ushort)source.entityData.guid;
                    SendUDP(specialMove.data, players[targetGuid]);
                    break;

                case SpecialMoveID.cursedArrow:
                case SpecialMoveID.arrowRain:
                case SpecialMoveID.shrapnel:
                case SpecialMoveID.smokeBomb:
                case SpecialMoveID.iceWave:
                case SpecialMoveID.confusion:
                case SpecialMoveID.shadowStep:
                    BroadcastUDP(specialMove.data, source);
                    break;

                default:
                    break;
                }
                break;

                #endregion
            case DatagramID.dummy:
                break;

            default:
                Console.WriteLine("unknown DatagramID: " + datagram[0]);
                break;
            }
        }