-
Notifications
You must be signed in to change notification settings - Fork 188
Set spawn command #2374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set spawn command #2374
Changes from all commits
71459fe
3f5f294
58c00b7
9b0cc99
601c11c
08aab5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -420,6 +420,7 @@ pub const Player = struct { // MARK: Player | |
| desiredPos: Vec3d = .{0, 0, 1.7 - standingBoundingBoxExtent[2]}, | ||
| }; | ||
| pub var super: main.server.Entity = .{}; | ||
| pub var playerSpawnPos: Vec3d = .{0, 0, 0}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to rename it to |
||
| pub var eye: EyeData = .{}; | ||
| pub var crouching: bool = false; | ||
| pub var id: u32 = 0; | ||
|
|
@@ -512,6 +513,10 @@ pub const Player = struct { // MARK: Player | |
| } | ||
| } | ||
|
|
||
| pub fn setSpawn(newSpawnpoint: Vec3d) void { | ||
| playerSpawnPos = newSpawnpoint; | ||
| } | ||
|
|
||
| pub fn isCreative() bool { | ||
| return gamemode.load(.monotonic) == .creative; | ||
| } | ||
|
|
@@ -544,7 +549,7 @@ pub const Player = struct { // MARK: Player | |
| } | ||
|
|
||
| pub fn kill() void { | ||
| Player.super.pos = world.?.spawn; | ||
| Player.super.pos = Player.playerSpawnPos; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you add a second variable to store the spawn point? Following on my comment above, please remove both of them.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one for the user/server one for the game/client
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the other one used for anything else? |
||
| Player.super.vel = .{0, 0, 0}; | ||
|
|
||
| Player.super.health = Player.super.maxHealth; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -511,6 +511,7 @@ pub const genericUpdate = struct { // MARK: genericUpdate | |
| time = 3, | ||
| biome = 4, | ||
| particles = 5, | ||
| setSpawn = 6, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not needed, see my comment above, |
||
| }; | ||
|
|
||
| const WorldEditPosition = enum(u2) { | ||
|
|
@@ -579,12 +580,16 @@ pub const genericUpdate = struct { // MARK: genericUpdate | |
| const emitter: particles.Emitter = .init(particleId, collides); | ||
| particles.ParticleSystem.addParticlesFromNetwork(emitter, pos, count); | ||
| }, | ||
| .setSpawn => { | ||
| if(conn.isServerSide()) return error.InvalidPacket; | ||
| game.Player.setSpawn(try reader.readVec(Vec3d)); | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn serverReceive(conn: *Connection, reader: *utils.BinaryReader) !void { | ||
| switch(try reader.readEnum(UpdateType)) { | ||
| .gamemode, .teleport, .time, .biome, .particles => return error.InvalidSide, | ||
| .gamemode, .teleport, .time, .biome, .particles, .setSpawn => return error.InvalidSide, | ||
| .worldEditPos => { | ||
| const typ = try reader.readEnum(WorldEditPosition); | ||
| const pos: ?Vec3i = switch(typ) { | ||
|
|
@@ -607,6 +612,16 @@ pub const genericUpdate = struct { // MARK: genericUpdate | |
| conn.send(.fast, id, &.{@intFromEnum(UpdateType.gamemode), @intFromEnum(gamemode)}); | ||
| } | ||
|
|
||
| pub fn sendSpawnPoint(conn: *Connection, pos: Vec3d) void { | ||
| var writer = utils.BinaryWriter.initCapacity(main.stackAllocator, 25); | ||
| defer writer.deinit(); | ||
|
|
||
| writer.writeEnum(UpdateType, .setSpawn); | ||
| writer.writeVec(Vec3d, pos); | ||
|
|
||
| conn.send(.fast, id, writer.data.items); | ||
| } | ||
|
|
||
| pub fn sendTPCoordinates(conn: *Connection, pos: Vec3d) void { | ||
| var writer = utils.BinaryWriter.initCapacity(main.stackAllocator, 25); | ||
| defer writer.deinit(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ pub const gamemode = @import("gamemode.zig"); | |
| pub const help = @import("help.zig"); | ||
| pub const invite = @import("invite.zig"); | ||
| pub const kill = @import("kill.zig"); | ||
| pub const setSpawn = @import("setSpawn.zig"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep this sorted alphabetically. |
||
| pub const particles = @import("particles.zig"); | ||
| pub const tickspeed = @import("tickspeed.zig"); | ||
| pub const time = @import("time.zig"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const main = @import("main"); | ||
| const User = main.server.User; | ||
|
|
||
| pub const description = "Sets the spawn point for the player"; | ||
| pub const usage = "/setSpawn"; | ||
|
|
||
| pub fn execute(args: []const u8, source: *User) void { | ||
| var x: ?f64 = null; | ||
| var y: ?f64 = null; | ||
| var z: ?f64 = null; | ||
| var split = std.mem.splitScalar(u8, args, ' '); | ||
| while(split.next()) |arg| { | ||
| const num: f64 = std.fmt.parseFloat(f64, arg) catch { | ||
| source.sendMessage("#ff0000Expected number, found \"{s}\"", .{arg}); | ||
| return; | ||
| }; | ||
| if(x == null) { | ||
| x = num; | ||
| } else if(y == null) { | ||
| y = num; | ||
| } else if(z == null) { | ||
| z = num; | ||
| } else { | ||
| source.sendMessage("#ff0000Too many arguments for command /setspawn", .{}); | ||
| return; | ||
| } | ||
| } | ||
| if(x == null or y == null) { | ||
| source.sendMessage("#ff0000Too few arguments for command /setspawn", .{}); | ||
| return; | ||
| } | ||
| if(z == null) { | ||
| z = source.player.pos[2]; | ||
| } | ||
| x = std.math.clamp(x.?, -1e9, 1e9); // TODO: Remove after #310 is implemented | ||
| y = std.math.clamp(y.?, -1e9, 1e9); | ||
| z = std.math.clamp(z.?, -1e9, 1e9); | ||
|
|
||
| main.items.Inventory.Sync.setSpawn(source, .{x.?, y.?, z.?}); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,7 @@ pub const User = struct { // MARK: User | |
| lastRenderDistance: u16 = 0, | ||
| lastPos: Vec3i = @splat(0), | ||
| gamemode: std.atomic.Value(main.game.Gamemode) = .init(.creative), | ||
| playerSpawnPos: Vec3d = .{0, 0, 0}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need two separate spawn points? |
||
| worldEditData: WorldEditData = undefined, | ||
|
|
||
| lastSentBiomeId: u32 = 0xffffffff, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should synchronize the spawn point with the client. I think the client shouldn't even know the spawn point. Instead I'd prefer if the client gets its respawn point sent by the server together with the kill packet.