Packet_ServerEntity ToNetworkEntity(ServerPlatform platform, ServerEntity entity) { Packet_ServerEntity p = new Packet_ServerEntity(); if (entity.position != null) { p.Position = ToNetworkEntityPosition(platform, entity.position); } if (entity.drawModel != null) { p.DrawModel = new Packet_ServerEntityAnimatedModel(); p.DrawModel.EyeHeight = platform.FloatToInt(entity.drawModel.eyeHeight * 32); p.DrawModel.Model_ = entity.drawModel.model; p.DrawModel.ModelHeight = platform.FloatToInt(entity.drawModel.modelHeight * 32); p.DrawModel.Texture_ = entity.drawModel.texture; p.DrawModel.DownloadSkin = entity.drawModel.downloadSkin ? 1 : 0; } if (entity.drawName != null) { p.DrawName_ = new Packet_ServerEntityDrawName(); p.DrawName_.Name = entity.drawName.name; p.DrawName_.Color = entity.drawName.color; p.DrawName_.OnlyWhenSelected = entity.drawName.onlyWhenSelected; p.DrawName_.ClientAutoComplete = entity.drawName.clientAutoComplete; } if (entity.drawText != null) { p.DrawText = new Packet_ServerEntityDrawText(); p.DrawText.Dx = platform.FloatToInt(entity.drawText.dx * 32); p.DrawText.Dy = platform.FloatToInt(entity.drawText.dy * 32); p.DrawText.Dz = platform.FloatToInt(entity.drawText.dz * 32); p.DrawText.Rotx = platform.FloatToInt(entity.drawText.rotx); p.DrawText.Roty = platform.FloatToInt(entity.drawText.roty); p.DrawText.Rotz = platform.FloatToInt(entity.drawText.rotz); p.DrawText.Text = entity.drawText.text; } if (entity.push != null) { p.Push = new Packet_ServerEntityPush(); p.Push.RangeFloat = platform.FloatToInt(entity.push.range * 32); } p.Usable = entity.usable; if (entity.drawArea != null) { p.DrawArea = new Packet_ServerEntityDrawArea(); p.DrawArea.X = entity.drawArea.x; p.DrawArea.Y = entity.drawArea.y; p.DrawArea.Z = entity.drawArea.z; p.DrawArea.Sizex = entity.drawArea.sizex; p.DrawArea.Sizey = entity.drawArea.sizey; p.DrawArea.Sizez = entity.drawArea.sizez; p.DrawArea.VisibleToClientId = entity.drawArea.visibleToClientId; } return(p); }
internal static Packet_Server EntitySpawn(int id, Packet_ServerEntity entity) { Packet_Server p = new Packet_Server(); p.Id = Packet_ServerIdEnum.EntitySpawn; p.EntitySpawn = new Packet_ServerEntitySpawn(); p.EntitySpawn.Id = id; p.EntitySpawn.Entity_ = entity; return(p); }
void NotifyPlayers(Server server, int clientid) { ClientOnServer c = server.clients[clientid]; // EntitySpawn foreach (var k in server.clients) { if (k.Value.state != ClientStateOnServer.Playing) { continue; } if (c.playersDirty[k.Key]) { Packet_ServerEntity e = ToNetworkEntity(server.serverPlatform, k.Value.entity); server.SendPacket(clientid, ServerPackets.EntitySpawn(k.Key, e)); c.playersDirty[k.Key] = false; } } }
public static Entity ToClientEntity(Game game, Packet_ServerEntity entity, Entity old, bool updatePosition) { if (entity.Position != null) { if (old.position == null || updatePosition) { old.networkPosition = ToClientEntityPosition(entity.Position); old.networkPosition.PositionLoaded = true; old.networkPosition.LastUpdateMilliseconds = game.platform.TimeMillisecondsFromStart(); old.position = ToClientEntityPosition(entity.Position); } } if (entity.DrawModel != null) { old.drawModel = new EntityDrawModel(); old.drawModel.eyeHeight = game.DeserializeFloat(entity.DrawModel.EyeHeight); old.drawModel.ModelHeight = game.DeserializeFloat(entity.DrawModel.ModelHeight); old.drawModel.Texture_ = entity.DrawModel.Texture_; old.drawModel.Model_ = entity.DrawModel.Model_; if (old.drawModel.Model_ == null) { old.drawModel.Model_ = "player.txt"; } old.drawModel.DownloadSkin = entity.DrawModel.DownloadSkin != 0; } if (entity.DrawName_ != null) { old.drawName = new DrawName(); if (entity.DrawName_.Color != null) { old.drawName.Name = game.platform.StringFormat2("{0}{1}", entity.DrawName_.Color, entity.DrawName_.Name); } else { old.drawName.Name = entity.DrawName_.Name; } if (!game.platform.StringStartsWithIgnoreCase(old.drawName.Name, "&")) { old.drawName.Name = game.platform.StringFormat("&f{0}", old.drawName.Name); } old.drawName.OnlyWhenSelected = entity.DrawName_.OnlyWhenSelected; old.drawName.ClientAutoComplete = entity.DrawName_.ClientAutoComplete; } if (entity.DrawText != null) { old.drawText = new EntityDrawText(); old.drawText.text = entity.DrawText.Text; float one_ = 1; old.drawText.dx = one_ * entity.DrawText.Dx / 32; old.drawText.dy = one_ * entity.DrawText.Dy / 32; old.drawText.dz = one_ * entity.DrawText.Dz / 32; } else { old.drawText = null; } if (entity.DrawBlock != null) { } if (entity.Push != null) { old.push = new Packet_ServerExplosion(); old.push.RangeFloat = entity.Push.RangeFloat; } else { old.push = null; } old.usable = entity.Usable; if (entity.DrawArea != null) { //New DrawArea old.drawArea = new EntityDrawArea(); old.drawArea.x = entity.DrawArea.X; old.drawArea.y = entity.DrawArea.Y; old.drawArea.z = entity.DrawArea.Z; old.drawArea.sizex = entity.DrawArea.Sizex; old.drawArea.sizey = entity.DrawArea.Sizey; old.drawArea.sizez = entity.DrawArea.Sizez; } else { //DrawArea deleted/not present old.drawArea = null; } return old; }
void NotifyEntities(Server server, int clientid, float dt) { ClientOnServer c = server.clients[clientid]; c.notifyEntitiesAccum += dt; if (c.notifyEntitiesAccum < (one / EntityPositionUpdatesPerSecond)) { return; } c.notifyEntitiesAccum = 0; // find nearest entities int max = SpawnMaxEntities; ServerEntityId[] nearestEntities = new ServerEntityId[max]; FindNearEntities(server, c, max, nearestEntities); // update entities for (int i = 0; i < max; i++) { ServerEntityId e = nearestEntities[i]; if (e == null) { continue; } for (int k = 0; k < server.modEventHandlers.onentityupdate.Count; k++) { server.modEventHandlers.onentityupdate[k](e); } } // despawn old entities for (int i = 0; i < c.spawnedEntitiesCount; i++) { ServerEntityId e = c.spawnedEntities[i]; if (e == null) { continue; } if (!Contains(nearestEntities, max, e)) { int onClientId = i; c.spawnedEntities[onClientId] = null; server.SendPacket(clientid, ServerPackets.EntityDespawn(64 + onClientId)); } } // spawn new entities for (int i = 0; i < max; i++) { ServerEntityId e = nearestEntities[i]; if (e == null) { continue; } if (!Contains(c.spawnedEntities, max, e)) { int onClientId = IndexOfNull(c.spawnedEntities, c.spawnedEntitiesCount); c.spawnedEntities[onClientId] = e.Clone(); ServerChunk chunk = server.d_Map.GetChunk(e.chunkx * Server.chunksize, e.chunky * Server.chunksize, e.chunkz * Server.chunksize); ServerEntity ee = chunk.Entities[e.id]; Packet_ServerEntity ne = ToNetworkEntity(server.serverPlatform, ee); server.SendPacket(clientid, ServerPackets.EntitySpawn(64 + onClientId, ne)); } } for (int i = 0; i < max; i++) { if (c.updateEntity[i]) { c.updateEntity[i] = false; ServerEntityId e = c.spawnedEntities[i]; ServerChunk chunk = server.d_Map.GetChunk(e.chunkx * Server.chunksize, e.chunky * Server.chunksize, e.chunkz * Server.chunksize); ServerEntity ee = chunk.Entities[e.id]; Packet_ServerEntity ne = ToNetworkEntity(server.serverPlatform, ee); server.SendPacket(clientid, ServerPackets.EntitySpawn(64 + i, ne)); } } }
void ProcessPacket(int client, Packet_Client packet) { switch (packet.GetId()) { case Packet_ClientIdEnum.PlayerIdentification: { if (packet.Identification == null) { return; } SendPacket(client, ServerPackets.Identification(0, MapSizeX, MapSizeY, MapSizeZ, platform.GetGameVersion())); clients[client].Name = packet.Identification.Username; } break; case Packet_ClientIdEnum.RequestBlob: { SendPacket(client, ServerPackets.LevelInitialize()); for (int i = 0; i < blockTypesCount; i++) { Packet_BlockType blocktype = blockTypes[i]; if (blocktype == null) { blocktype = new Packet_BlockType(); } SendPacket(client, ServerPackets.BlockType(i, blocktype)); } SendPacket(client, ServerPackets.BlockTypes()); SendPacket(client, ServerPackets.LevelFinalize()); for (int i = 0; i < clientsCount; i++) { if (clients[i] == null) { continue; } clients[i].glX = spawnGlX; clients[i].glY = spawnGlY; clients[i].glZ = spawnGlZ; Packet_PositionAndOrientation pos = new Packet_PositionAndOrientation(); pos.X = platform.FloatToInt(32 * clients[i].glX); pos.Y = platform.FloatToInt(32 * clients[i].glY); pos.Z = platform.FloatToInt(32 * clients[i].glZ); pos.Pitch = 255 / 2; //SendPacket(client, ServerPackets.Spawn(i, clients[i].Name, pos)); Packet_ServerEntity e = new Packet_ServerEntity(); e.DrawModel = new Packet_ServerEntityAnimatedModel(); e.DrawModel.Model_ = "player.txt"; e.DrawModel.ModelHeight = platform.FloatToInt((one * 17 / 10) * 32); e.DrawModel.EyeHeight = platform.FloatToInt((one * 15 / 10) * 32); e.Position = pos; SendPacket(client, ServerPackets.EntitySpawn(0, e)); SendPacket(client, ServerPackets.PlayerStats(100, 100, 100, 100)); } for (int i = 0; i < modsCount; i++) { mods[i].OnPlayerJoin(client); } clients[client].connected = true; } break; case Packet_ClientIdEnum.Message: { SendPacketToAll(ServerPackets.Message(platform.StringFormat2("{0}: &f{1}", clients[client].Name, packet.Message.Message))); } break; case Packet_ClientIdEnum.SetBlock: { int x = packet.SetBlock.X; int y = packet.SetBlock.Y; int z = packet.SetBlock.Z; int block = packet.SetBlock.BlockType; int mode = packet.SetBlock.Mode; if (mode == Packet_BlockSetModeEnum.Create) { } if (mode == Packet_BlockSetModeEnum.Destroy) { SendPacketToAll(ServerPackets.SetBlock(x, y, z, 0)); } if (mode == Packet_BlockSetModeEnum.Use) { } if (mode == Packet_BlockSetModeEnum.UseWithTool) { } } break; case Packet_ClientIdEnum.PositionandOrientation: { clients[client].glX = one * packet.PositionAndOrientation.X / 32; clients[client].glY = one * packet.PositionAndOrientation.Y / 32; clients[client].glZ = one * packet.PositionAndOrientation.Z / 32; } break; case Packet_ClientIdEnum.InventoryAction: { switch (packet.InventoryAction.Action) { case Packet_InventoryActionTypeEnum.Click: break; } } break; } }
internal static Packet_Server EntitySpawn(int id, Packet_ServerEntity entity) { Packet_Server p = new Packet_Server(); p.Id = Packet_ServerIdEnum.EntitySpawn; p.EntitySpawn = new Packet_ServerEntitySpawn(); p.EntitySpawn.Id = id; p.EntitySpawn.Entity_ = entity; return p; }
Packet_ServerEntity ToNetworkEntity(ServerPlatform platform, ServerEntity entity) { Packet_ServerEntity p = new Packet_ServerEntity(); if (entity.position != null) { p.Position = ToNetworkEntityPosition(platform, entity.position); } if (entity.drawModel != null) { p.DrawModel = new Packet_ServerEntityAnimatedModel(); p.DrawModel.EyeHeight = platform.FloatToInt(entity.drawModel.eyeHeight * 32); p.DrawModel.Model_ = entity.drawModel.model; p.DrawModel.ModelHeight = platform.FloatToInt(entity.drawModel.modelHeight * 32); p.DrawModel.Texture_ = entity.drawModel.texture; p.DrawModel.DownloadSkin = entity.drawModel.downloadSkin ? 1 : 0; } if (entity.drawName != null) { p.DrawName_ = new Packet_ServerEntityDrawName(); p.DrawName_.Name = entity.drawName.name; p.DrawName_.Color = entity.drawName.color; p.DrawName_.OnlyWhenSelected = entity.drawName.onlyWhenSelected; p.DrawName_.ClientAutoComplete = entity.drawName.clientAutoComplete; } if (entity.drawText != null) { p.DrawText = new Packet_ServerEntityDrawText(); p.DrawText.Dx = platform.FloatToInt(entity.drawText.dx * 32); p.DrawText.Dy = platform.FloatToInt(entity.drawText.dy * 32); p.DrawText.Dz = platform.FloatToInt(entity.drawText.dz * 32); p.DrawText.Rotx = platform.FloatToInt(entity.drawText.rotx); p.DrawText.Roty = platform.FloatToInt(entity.drawText.roty); p.DrawText.Rotz = platform.FloatToInt(entity.drawText.rotz); p.DrawText.Text = entity.drawText.text; } if (entity.push != null) { p.Push = new Packet_ServerEntityPush(); p.Push.RangeFloat = platform.FloatToInt(entity.push.range * 32); } p.Usable = entity.usable; if (entity.drawArea != null) { p.DrawArea = new Packet_ServerEntityDrawArea(); p.DrawArea.X = entity.drawArea.x; p.DrawArea.Y = entity.drawArea.y; p.DrawArea.Z = entity.drawArea.z; p.DrawArea.Sizex = entity.drawArea.sizex; p.DrawArea.Sizey = entity.drawArea.sizey; p.DrawArea.Sizez = entity.drawArea.sizez; p.DrawArea.VisibleToClientId = entity.drawArea.visibleToClientId; } return p; }
public static Entity ToClientEntity(Game game, Packet_ServerEntity entity, Entity old, bool updatePosition) { if (entity.Position != null) { if (old.position == null || updatePosition) { old.networkPosition = ToClientEntityPosition(entity.Position); old.networkPosition.PositionLoaded = true; old.networkPosition.LastUpdateMilliseconds = game.platform.TimeMillisecondsFromStart(); old.position = ToClientEntityPosition(entity.Position); } } if (entity.DrawModel != null) { old.drawModel = new EntityDrawModel(); old.drawModel.eyeHeight = game.DeserializeFloat(entity.DrawModel.EyeHeight); old.drawModel.ModelHeight = game.DeserializeFloat(entity.DrawModel.ModelHeight); old.drawModel.Texture_ = entity.DrawModel.Texture_; old.drawModel.Model_ = entity.DrawModel.Model_; if (old.drawModel.Model_ == null) { old.drawModel.Model_ = "player.txt"; } old.drawModel.DownloadSkin = entity.DrawModel.DownloadSkin != 0; } if (entity.DrawName_ != null) { old.drawName = new DrawName(); old.drawName.Name = entity.DrawName_.Name; if (!game.platform.StringStartsWithIgnoreCase(old.drawName.Name, "&")) { old.drawName.Name = game.platform.StringFormat("&f{0}", old.drawName.Name); } old.drawName.OnlyWhenSelected = entity.DrawName_.OnlyWhenSelected; old.drawName.ClientAutoComplete = entity.DrawName_.ClientAutoComplete; } if (entity.DrawText != null) { old.drawText = new EntityDrawText(); old.drawText.text = entity.DrawText.Text; float one_ = 1; old.drawText.dx = one_ * entity.DrawText.Dx / 32; old.drawText.dy = one_ * entity.DrawText.Dy / 32; old.drawText.dz = one_ * entity.DrawText.Dz / 32; } else { old.drawText = null; } if (entity.DrawBlock != null) { } if (entity.Push != null) { old.push = new Packet_ServerExplosion(); old.push.RangeFloat = entity.Push.RangeFloat; } else { old.push = null; } old.usable = entity.Usable; if (entity.DrawArea != null) { //New DrawArea old.drawArea = new EntityDrawArea(); old.drawArea.x = entity.DrawArea.X; old.drawArea.y = entity.DrawArea.Y; old.drawArea.z = entity.DrawArea.Z; old.drawArea.sizex = entity.DrawArea.Sizex; old.drawArea.sizey = entity.DrawArea.Sizey; old.drawArea.sizez = entity.DrawArea.Sizez; } else { //DrawArea deleted/not present old.drawArea = null; } return(old); }