Example #1
0
        public void ViewVisibleThread()
        {
            Output.WriteLine("[WORLD SERVER]View Visible Thread started");
            while (true)
            {
                ArrayList deadPlayers    = new ArrayList();
                ArrayList removeEntities = new ArrayList();

                // Clean
                lock (WorldSocket.Clients.SyncRoot)
                {
                    foreach (string clientKey in WorldSocket.Clients.Keys)
                    {
                        // Collect dead players to arraylist
                        WorldClient thisclient = WorldSocket.Clients[clientKey] as WorldClient;

                        if (thisclient.Alive == false)
                        {
                            // Add dead Player to the List - we need later to clear them
                            deadPlayers.Add(clientKey);
                        }
                    }
                    cleanDeadPlayers(deadPlayers);
                }

                // Check for Player Views
                lock (WorldSocket.Clients.SyncRoot)
                {
                    foreach (string clientKey in WorldSocket.Clients.Keys)
                    {
                        // get Current client
                        WorldClient currentClient = WorldSocket.Clients[clientKey] as WorldClient;
                        // Loop all Clients and check if we need to create a view for it
                        foreach (string clientOtherKey in WorldSocket.Clients.Keys)
                        {
                            WorldClient otherClient = WorldSocket.Clients[clientOtherKey] as WorldClient;
                            if (otherClient != currentClient)
                            {
                                ClientView clientView = currentClient.viewMan.getViewForEntityAndGo(otherClient.playerData.getEntityId(), NumericalUtils.ByteArrayToUint16(otherClient.playerInstance.GetGoid(), 1));

                                // Create
                                Maths  math           = new Maths();
                                double currentPlayerX = 0;
                                double currentPlayerY = 0;
                                double currentPlayerZ = 0;

                                double otherPlayerX = 0;
                                double otherPlayerY = 0;
                                double otherPlayerZ = 0;

                                NumericalUtils.LtVector3dToDoubles(currentClient.playerInstance.Position.getValue(), ref currentPlayerX, ref currentPlayerY, ref currentPlayerZ);
                                NumericalUtils.LtVector3dToDoubles(currentClient.playerInstance.Position.getValue(), ref otherPlayerX, ref otherPlayerY, ref otherPlayerZ);
                                Maths mathUtils        = new Maths();
                                bool  playerIsInCircle = mathUtils.IsInCircle((float)currentPlayerX, (float)currentPlayerZ, (float)otherPlayerX, (float)otherPlayerZ, 5000);
                                if (clientView.viewCreated == false && currentClient.playerData.getDistrictId() == otherClient.playerData.getDistrictId() && otherClient.playerData.getOnWorld() && currentClient.playerData.getOnWorld() && playerIsInCircle)
                                {
                                    // Spawn player
                                    ServerPackets pak = new ServerPackets();
                                    pak.sendSystemChatMessage(currentClient, "Player " + otherClient.playerInstance.GetName() + " with new View ID " + clientView.ViewID + " jacked in", "BROADCAST");
                                    pak.sendPlayerSpawn(currentClient, otherClient, clientView.ViewID);
                                    clientView.spawnId     = currentClient.playerData.spawnViewUpdateCounter;
                                    clientView.viewCreated = true;
                                }


                                if (clientView.viewCreated && !playerIsInCircle)
                                {
                                    // ToDo: delete mob
                                    ServerPackets packets = new ServerPackets();
                                    packets.sendSystemChatMessage(currentClient, "Player " + otherClient.playerInstance.GetName() + " with View ID " + clientView.ViewID + " jacked out!", "MODAL");
                                    packets.sendDeleteViewPacket(currentClient, clientView.ViewID);
                                    currentClient.viewMan.removeViewByViewId(clientView.ViewID);
                                }
                            }
                        }
                    }
                }



                // Spawn/Update for mobs
                int npcCount = WorldSocket.npcs.Count;
                for (int i = 0; i < npcCount; i++)
                {
                    npc thismob = (npc)WorldSocket.npcs[i];

                    lock (WorldSocket.Clients.SyncRoot)
                    {
                        foreach (string clientKey in WorldSocket.Clients.Keys)
                        {
                            // Loop through all clients
                            WorldClient thisclient = WorldSocket.Clients[clientKey] as WorldClient;

                            if (thisclient.Alive == true)
                            {
                                // Check if

                                if (thisclient.playerData.getOnWorld() == true && thisclient.playerData.waitForRPCShutDown == false)
                                {
                                    Maths  math    = new Maths();
                                    double playerX = 0;
                                    double playerY = 0;
                                    double playerZ = 0;
                                    NumericalUtils.LtVector3dToDoubles(thisclient.playerInstance.Position.getValue(), ref playerX, ref playerY, ref playerZ);
                                    Maths mathUtils     = new Maths();
                                    bool  mobIsInCircle = mathUtils.IsInCircle((float)playerX, (float)playerZ, (float)thismob.getXPos(), (float)thismob.getZPos(), 5000);

                                    // ToDo: Check if mob is in circle of player (radian some value that is in a middle range for example 300m)
                                    // Create
                                    ClientView mobView = thisclient.viewMan.getViewForEntityAndGo(thismob.getEntityId(), NumericalUtils.ByteArrayToUint16(thismob.getGoId(), 1));
                                    if (mobView.viewCreated == false && thismob.getDistrict() == thisclient.playerData.getDistrictId() && thisclient.playerData.getOnWorld() && mobIsInCircle)
                                    {
                                        ServerPackets pak = new ServerPackets();
                                        pak.sendSystemChatMessage(thisclient, "Mob with Name " + thismob.getName() + " with new View ID " + mobView.ViewID + " spawned", "BROADCAST");
                                        ServerPackets mobPak = new ServerPackets();
                                        mobPak.spawnMobView(thisclient, thismob, mobView);
                                        mobView.spawnId     = thisclient.playerData.spawnViewUpdateCounter;
                                        mobView.viewCreated = true;
                                    }

                                    // Update Mob
                                    if (mobView.viewCreated == true && thismob.getDistrict() == thisclient.playerData.getDistrictId() && thisclient.playerData.getOnWorld())
                                    {
                                        // ToDo: We need to involve the Statuslist here and we need to move them finaly
                                        if (thismob.getIsDead() == false)
                                        {
                                            updateMob(thisclient, ref thismob, mobView);
                                        }
                                    }

                                    // Mob moves outside - should delete it
                                    if (mobView.viewCreated == true &&
                                        !mobIsInCircle &&
                                        thismob.getDistrict() == thisclient.playerData.getDistrictId())
                                    {
                                        // ToDo: delete mob
                                        ServerPackets packets = new ServerPackets();
                                        packets.sendDeleteViewPacket(thisclient, mobView.ViewID);
                                        packets.sendSystemChatMessage(thisclient, "MobView (" + thismob.getName() + " LVL: " + thismob.getLevel() + " ) with View ID " + mobView.ViewID + " is out of range and is deleted!", "MODAL");
                                        thisclient.viewMan.removeViewByViewId(mobView.ViewID);
                                    }
                                }
                            }
                        }
                    }
                    thismob.updateClient = false;
                }
                Thread.Sleep(500);
            }
        }
Example #2
0
        public int generatePathTable()
        {
            // We firs test this with just one moove from point x to y
            // To calc this we need to know the time the object needs to reach this point,
            // the new point and the rotation to calc pretty to sync client and server position is right
            // to get a smooth walking :)
            var math = new Maths();

            // First choose a new pos in the range
            var newPos = math.RandomPointOnCircle((float)xBase, (float)yBase, (float)zBase, 5.0f * 100);

            #if DEBUG
            Output.WriteDebugLog("Mob Goes from X: " + getXPos() + " , Z: " + getZPos() + " to X: " + newPos.x +
                                 ", Z: " + newPos.z);
            #endif


            var xNew = (double)newPos.x;
            var zNew = (double)newPos.z;

            destination = newPos;
            // Try to calculate rotation

            // Oh this seems to match ...needs more testing later when we fixed random pos
            var yaw          = Math.Atan((xNew - getXPos()) / (zNew - getZPos())) * 128 / Math.PI;
            var calcRotation = Math.Atan2(Math.Cos(xNew), Math.Sin(zNew) * Math.Sin(zNew)) * 128 / Math.PI;
            var testRot      = Math.Atan2(xNew, zNew) * 180 / Math.PI;
            var testRot2     = Math.Atan2(xNew, zNew) * 128 / Math.PI;

            #if DEBUG
            Output.WriteDebugLog("Test Rot with 360 : " + testRot + "| 255 : " + testRot2 + " AND THE YAW: " + yaw +
                                 " (Cast to uint16 : " + Convert.ToInt16(yaw) + " )");
            #endif


            var yawVal = (int)Convert.ToInt16(yaw);

            if (zNew < getZPos() || xNew < getXPos())
            {
                #if DEBUG
                Output.WriteDebugLog("Need to adjust YAW + 128 from :" + yawVal + " to: " + (yawVal + 128));
                #endif
                yawVal = yawVal + 128;
            }
            else
            {
                #if DEBUG
                Output.WriteDebugLog("Need to adjust YAW - 128 from :" + yawVal + " to: " + (yawVal - 128));
                #endif
                yawVal = yawVal - 128;
            }

            #if DEBUG
            Output.WriteDebugLog("YAW VAL :" + yawVal);
            #endif

            rotation = (ushort)yawVal;
            #if DEBUG
            Output.WriteDebugLog("Calc Rotation : " + calcRotation + " and to UINT : " + (uint)calcRotation);
            #endif

            // Calculate the distance for seconds to move
            var requiredSeconds = (int)(math.distance2Coords((float)xPos, (float)xNew, (float)zPos, (float)zNew) /
                                        0.176 / 500 * 0.9586);
            return(requiredSeconds);
        }