Exemple #1
0
        public GameEntity(GamePoint3D Position, GamePoint2D Size, int Id, GameWorld gameWorld)
        {
            pos          = Position;
            id           = Id;
            spd          = new GamePoint3D();
            frc          = new GamePoint3D(1.2d, 1.2d, 1.01d); //friction system needs to be changed to support different areas, like just going through air
            base_spd     = new GamePoint3D(1d, 0.5d, 0.75d);   //    or walking on something, which have different frictions
            spd_deadzone = new GamePoint3D(0.2d, 0.2d, 0.3d);
            direction    = 0f;
            pitch        = 0f;
            size         = Size;
            precision    = 0.2f; //higher number = chunkier collision checks (faster but crappier)

            BufferStream buff = new BufferStream(1024, 1);

            buff.Write((ushort)4);
            buff.Write(id);
            buff.Write(pos.X);
            buff.Write(pos.Y);
            buff.Write(pos.Z);
            buff.Write(size.X);
            buff.Write(size.Y);
            buff.Write(direction);
            buff.Write(pitch);
            gameWorld.sendToAllClients(buff);
            buff.Deallocate();
        }
Exemple #2
0
        public bool removeObject(int id)
        {
            if (objectMap.ContainsKey(id))
            {
                objectMap.Remove(id);

                BufferStream buff = new BufferStream(1024, 1);
                buff.Write((ushort)2);
                buff.Write((uint)id);
                sendToAllClients(buff);
                buff.Deallocate();
                return(true);
            }
            return(false);
        }
Exemple #3
0
        public bool removeEntity(int Id)
        {
            //returns if successful
            if (entityMap.ContainsKey(Id))
            {
                entityMap.Remove(Id);

                BufferStream buff = new BufferStream(8, 1);
                buff.Write((ushort)5);
                buff.Write((uint)Id); //InvalidOperationException
                sendToAllClients(buff);
                buff.Deallocate();
                return(true);
            }
            return(false);
        }
Exemple #4
0
 public void sendUpdates(List <int> alreadyDone = null)
 {
     if (alreadyDone == null)
     {
         alreadyDone = new List <int>();
     }
     try
     {
         foreach (KeyValuePair <int, GameClient> pair in clientMap)
         {
             if (!alreadyDone.Contains(pair.Key))
             {
                 int          count = pair.Value.updatedQueue.Count;
                 BufferStream buff_ = new BufferStream(12 + (52 * count), 1);
                 buff_.Seek(0);
                 buff_.Write((ushort)0);
                 buff_.Write(pair.Value.updatedQueue.Count);
                 int updates = GameGeometry.clamp(96 - count, 0, 96);
                 while (updates < 96)
                 {
                     int ent_ = pair.Value.updatedQueue.Dequeue();
                     if (entityMap.ContainsKey(ent_))
                     {
                         GameEntity ent = entityMap[ent_];
                         buff_.Write(ent.id);
                         buff_.Write(ent.pos.X);
                         buff_.Write(ent.pos.Y);
                         buff_.Write(ent.pos.Z);
                         buff_.Write(ent.size.X);
                         buff_.Write(ent.size.Y);
                         buff_.Write(ent.direction);
                         buff_.Write(ent.pitch);
                         updates++;
                     }
                 }
                 sendToClient(pair.Value, buff_);
                 buff_.Deallocate();
                 alreadyDone.Add(pair.Key);
             }
         }
     }
     catch (InvalidOperationException)
     {
         mainProgram.WriteLine("error-client map probably changed");
         sendUpdates(alreadyDone);
     }
 }
Exemple #5
0
        public GameObject(GamePoint3D Position, GamePoint3D Size, int Id, GameWorld gameWorld)
        {
            position = Position;
            size     = Size;
            id       = Id;

            BufferStream buff = new BufferStream(64, 1);

            buff.Write((ushort)1);
            buff.Write((uint)id);
            buff.Write(position.X);
            buff.Write(position.Y);
            buff.Write(position.Z);
            buff.Write(size.X);
            buff.Write(size.Y);
            buff.Write(size.Z);
            gameWorld.sendToAllClients(buff);
            buff.Deallocate();
        }
Exemple #6
0
        private static void event_connected(TcpClientHandler client)
        {
            mainProgram.WriteLine("Client connected from " + getIp(client).ToString());
            //we'll put our code to initiate the player client here
            int          entid_ = gameWorld.createPlayer(new GamePoint3D(0d, 0d, 512d), client);
            int          sz_    = 16 + (gameWorld.entityMap.Count * 52) + (gameWorld.objectMap.Count * 52);
            BufferStream buff   = new BufferStream(sz_, 1);

            buff.Write((ushort)3);
            buff.Write(entid_);
            buff.Write((uint)gameWorld.entityMap.Count);
            foreach (KeyValuePair <int, GameEntity> pair in gameWorld.entityMap)
            {
                buff.Write(pair.Value.id);
                buff.Write(pair.Value.pos.X);
                buff.Write(pair.Value.pos.Y);
                buff.Write(pair.Value.pos.Z);
                buff.Write(pair.Value.size.X);
                buff.Write(pair.Value.size.Y);
                buff.Write(pair.Value.direction);
                buff.Write(pair.Value.pitch);
            }
            buff.Write((uint)gameWorld.objectMap.Count);
            foreach (KeyValuePair <int, GameObject> pair in gameWorld.objectMap)
            {
                buff.Write(pair.Value.id);
                buff.Write(pair.Value.position.X);
                buff.Write(pair.Value.position.Y);
                buff.Write(pair.Value.position.Z);
                buff.Write(pair.Value.size.X);
                buff.Write(pair.Value.size.Y);
                buff.Write(pair.Value.size.Z);
            }
            gameWorld.sendToClient(client, buff);
            buff.Deallocate();
            mainProgram.WriteLine("World and client data sent to socket " + client.Socket.ToString());
        }
Exemple #7
0
        private static void event_received(TcpClientHandler client, BufferStream readBuffer)
        {
            int plid = gameWorld.getPlayer(client.Socket);

            //this following line is probably absolutely essential to deal with the GMS packets
            readBuffer.Seek(12);
            BufferStream buff_ = null;
            //get the message id, so we know what to do with the packet
            ushort msgid; readBuffer.Read(out msgid);

            switch (msgid)
            {
            case 0:     //client controls update
                /* so how this works, is that the control info is sent in a single byte, where each bit represents a movement or something, seen below
                 *  0000000X - forward
                 *  000000X0 - backward
                 *  00000X00 - strafe left
                 *  0000X000 - strafe right
                 *  000X0000 - up ~jump
                 *  00X00000 - down ~crouch
                 */
                byte   in_; readBuffer.Read(out in_);
                bool[] actions = GameGeometry.parse_binary(in_);
                if (plid >= 0)
                {
                    gameWorld.clientMap[plid].inputMap.setInput("forward", actions[0]);
                    gameWorld.clientMap[plid].inputMap.setInput("backward", actions[1]);
                    gameWorld.clientMap[plid].inputMap.setInput("left", actions[2]);
                    gameWorld.clientMap[plid].inputMap.setInput("right", actions[3]);
                    gameWorld.clientMap[plid].inputMap.setInput("up", actions[4]);
                    gameWorld.clientMap[plid].inputMap.setInput("down", actions[5]);
                }
                break;

            case 1:     //client view update
                //we should be getting two floats, one for direction and one for pitch
                float dir_; readBuffer.Read(out dir_);
                float pit_; readBuffer.Read(out pit_);
                if (plid >= 0)
                {
                    gameWorld.clientMap[plid].inputMap.setInput("view_x", dir_);
                    gameWorld.clientMap[plid].inputMap.setInput("view_y", pit_);
                }
                break;

            case 2:     //client sent back a ping
                GameClient client_ = gameWorld.getClientFromSocket(client.Socket);
                client_.pingWatch.Stop();
                mainProgram.WriteLine("Socket " + client.Socket.ToString() + " ping is " + client_.pingWatch.ElapsedMilliseconds);
                break;

            case 3:     //client is disconnecting
                buff_ = new BufferStream(2, 1);
                buff_.Write((ushort)7);
                gameWorld.sendToClient(client, buff_);
                client.Connected = false;
                break;

            case 4:     //client requested an entity
                int entId; readBuffer.Read(out entId);
                buff_ = new BufferStream(64, 1);
                buff_.Write((ushort)4);
                buff_.Write(gameWorld.entityMap[entId].id);
                buff_.Write(gameWorld.entityMap[entId].pos.X);
                buff_.Write(gameWorld.entityMap[entId].pos.Y);
                buff_.Write(gameWorld.entityMap[entId].pos.Z);
                buff_.Write(gameWorld.entityMap[entId].size.X);
                buff_.Write(gameWorld.entityMap[entId].size.Y);
                buff_.Write(gameWorld.entityMap[entId].direction);
                buff_.Write(gameWorld.entityMap[entId].pitch);
                gameWorld.sendToClient(client, buff_);
                break;

            default:
                mainProgram.WriteLine("invalid packet received");
                break;
            }
            if (buff_ != null)
            {
                buff_.Deallocate();
            }
        }
Exemple #8
0
        public static void DoCommand(string consoleInput)
        {
            List <string> cmdList = new List <string>();

            while (consoleInput != "")
            {
                cmdList.Add(CommandSystem.ReadCommand(consoleInput, out consoleInput));
            }
            GameClient client_;

            if (cmdList.Count == 0)
            {
                return;
            }
            switch (cmdList[0])
            {
            case "":
                break;

            case "ping":
                if (cmdList.Count != 2)
                {
                    break;
                }
                client_ = mainProgram.gameWorld.getClientFromSocket(cmdList[1]);
                if (client_ != null)
                {
                    BufferStream buff_ = new BufferStream(8, 1);
                    buff_.Write((ushort)6);
                    mainProgram.gameWorld.sendToClient(client_, buff_);
                    client_.pingWatch.Reset();
                    client_.pingWatch.Start();
                    buff_.Deallocate();
                }
                break;

            case "teleport":
                if (cmdList.Count != 5)
                {
                    break;
                }
                client_ = mainProgram.gameWorld.getClientFromSocket(cmdList[1]);
                if (client_ == null)
                {
                    break;
                }
                double     x_, y_, z_;
                GameEntity ent_ = mainProgram.gameWorld.getEntity(client_.entityId);
                try
                {
                    if (cmdList[2] == "~")
                    {
                        x_ = ent_.pos.X;
                    }
                    else
                    {
                        x_ = Convert.ToDouble(cmdList[2]);
                    }
                    if (cmdList[3] == "~")
                    {
                        y_ = ent_.pos.Y;
                    }
                    else
                    {
                        y_ = Convert.ToDouble(cmdList[3]);
                    }
                    if (cmdList[4] == "~")
                    {
                        z_ = ent_.pos.Z;
                    }
                    else
                    {
                        z_ = Convert.ToDouble(cmdList[4]);
                    }
                }
                catch (FormatException)
                {
                    mainProgram.WriteLine("error-improper coordinate(s)");
                    break;
                }

                ent_.pos = new GamePoint3D(x_, y_, z_);
                break;

            case "players":
                mainProgram.WriteLine("Current players [" + mainProgram.gameWorld.clientMap.Count.ToString() + @"\" + mainProgram.settings.maxConnections.ToString() + "]:");
                for (int i = 0; i < mainProgram.gameWorld.clientMap.Count; i++)
                {
                    mainProgram.WriteLine(" -#{0}. Socket {1}, ip {2}", i, mainProgram.gameWorld.clientMap[i].clientHandler.Socket, mainProgram.getIp(mainProgram.gameWorld.clientMap[i].clientHandler).ToString());
                }
                if (mainProgram.gameWorld.clientMap.Count == 0)
                {
                    mainProgram.WriteLine(" -Currently no players in this server");
                }
                break;

            case "stop":
                mainProgram.game_server.Close();
                break;

            case "help":
                mainProgram.WriteLine("commands:{0}{1}{0}{2}{0}{3}{0}{4}{0}{5}", Environment.NewLine,
                                      " -ping [socket] //pings a socket, writes the ping",
                                      " -teleport [socket] [x] [y] [z] //teleports a player to a location",
                                      " -players //lists all of the clients currently in the server",
                                      " -help //lists all the commands available",
                                      " -stop //closes the server");
                break;

            default:
                string outCmd = cmdList[0] + " [";
                for (int i = 1; i < cmdList.Count - 1; i++)
                {
                    outCmd += cmdList[i] + ",";
                }
                outCmd += cmdList[cmdList.Count - 1];
                mainProgram.WriteLine("unknown command " + outCmd + "]");
                break;
            }
        }