From 317954ef75ec4d951fd7fa70b22b11a0127bdf49 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Tue, 25 Apr 2023 13:41:45 -0400 Subject: [PATCH] input + cleanup --- client/package.json | 2 +- client/src/gateway.ts | 4 +- client/src/index.ts | 71 ++++++++- client/src/protocol/goodbye_reason.ts | 30 ++-- client/src/protocol/input.ts | 48 ++++++ client/src/protocol/message_c2s.ts | 145 +++++++++++++++++- client/src/protocol/message_s2c.ts | 2 +- client/src/protocol/module.ts | 36 +++-- client/src/protocol/planet.ts | 32 ++-- client/src/protocol/player.ts | 20 +-- client/src/protocol/starkingdoms-protocol.ts | 2 +- client/src/protocol/state.ts | 14 +- docker/README.md | 1 + protocol/build.rs | 1 + protocol/src/lib.rs | 16 +- protocol/src/pbuf/goodbye_reason.proto | 13 +- protocol/src/pbuf/input.proto | 9 ++ protocol/src/pbuf/message_c2s.proto | 12 ++ protocol/src/pbuf/module.proto | 13 +- protocol/src/pbuf/planet.proto | 11 +- protocol/src/pbuf/player.proto | 6 +- protocol/src/pbuf/starkingdoms-protocol.proto | 1 + protocol/src/pbuf/state.proto | 5 +- server/src/entity.rs | 28 ++-- server/src/handler.rs | 4 + server/src/manager.rs | 27 ++-- server/src/planet.rs | 6 +- server/src/timer.rs | 18 +-- spacetime | 5 + 29 files changed, 447 insertions(+), 135 deletions(-) create mode 100644 client/src/protocol/input.ts create mode 100644 docker/README.md create mode 100644 protocol/src/pbuf/input.proto diff --git a/client/package.json b/client/package.json index 8f91b75a71aeacc7ccb797e7ac0fc0df7d1385a7..bfa2aea65f558c0148965a2c61f9c713de7459bd 100644 --- a/client/package.json +++ b/client/package.json @@ -7,7 +7,7 @@ "dev": "vite", "build": "tsc && vite build", "preview": "vite preview", - "protobuf": "mkdir -p ./src/protocol && protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./src/protocol -I ../protocol/src/pbuf ../protocol/src/pbuf/starkingdoms-protocol.proto" + "protobuf": "mkdir -p ./src/protocol && protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./src/protocol -I ../protocol/src/pbuf ../protocol/src/pbuf/starkingdoms-protocol.proto && sed -i 's/\\/\\* eslint-disable \\*\\//\\/\\/@ts-nocheck/' src/protocol/*.ts" }, "devDependencies": { "ts-proto": "^1.146.0", diff --git a/client/src/gateway.ts b/client/src/gateway.ts index 29c342706fcc52403777e4c1f4299a88d4127885..cc3f1a2bc86e97577b65b03169d20c48466b920b 100644 --- a/client/src/gateway.ts +++ b/client/src/gateway.ts @@ -74,7 +74,7 @@ export async function gateway_connect(gateway_url: string, username: string): Pr let handshake_start_msg; if (global.can_beam_out) { handshake_start_msg = MessageC2SHello.encode({ - version: 3, + version: 4, requestedUsername: username, nextState: State.Play, user: window.localStorage.getItem("user")!, @@ -82,7 +82,7 @@ export async function gateway_connect(gateway_url: string, username: string): Pr }).finish(); } else { handshake_start_msg = MessageC2SHello.encode({ - version: 3, + version: 4, requestedUsername: username, nextState: State.Play, // @ts-ignore diff --git a/client/src/index.ts b/client/src/index.ts index 76a9d42ec67ae0aead4fbee4a7c5b5fe276c00aa..5ec80c3ef36b382822895db5f4aae61685f5f5be 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -7,9 +7,10 @@ import { MessageC2SAuthenticateAndBeamOut, MessageC2SAuthenticateAndBeamOut_packetInfo, MessageC2SInput, - MessageC2SInput_packetInfo + MessageC2SInput_packetInfo, MessageC2SMouseInput, MessageC2SMouseInput_packetInfo } from "./protocol/message_c2s"; import {encode} from "./serde"; +import {InputType} from "./protocol/input"; logSetup(); const logger = new Logger("client"); @@ -110,6 +111,71 @@ async function client_main(server: string, username: string, texture_quality: st } global.canvas.style.setProperty("background-image", `url("/assets/final/${texture_quality}/starfield.png")`); + let canvas = document.getElementById("canvas")!; + + document.onmousedown = (e) => { + let canvasLeft = canvas.offsetLeft + canvas.clientLeft; + let canvasTop = canvas.offsetTop + canvas.clientTop; + + let screenspaceX = e.pageX - canvasLeft; + let screenspaceY = e.pageY - canvasTop; + + // convert screenspace to worldspace + if (global.me !== null) { + let worldX = screenspaceX + global.me?.x; + let worldY = screenspaceY + global.me?.y; + + let button: InputType; + if (e.button == 0) { + button = InputType.Left; + } else if (e.button == 1) { + button = InputType.Middle; + } else if (e.button == 2) { + button = InputType.Right; + } + + let msg = MessageC2SMouseInput.encode({ + worldposX: worldX, + worldposY: worldY, + released: false, + button: button! + }).finish(); + + global.client?.socket.send(encode(MessageC2SMouseInput_packetInfo.type, msg)) + } + } + + document.onmouseup = (e) => { + let canvasLeft = canvas.offsetLeft + canvas.clientLeft; + let canvasTop = canvas.offsetTop + canvas.clientTop; + + let screenspaceX = e.pageX - canvasLeft; + let screenspaceY = e.pageY - canvasTop; + + // convert screenspace to worldspace + if (global.me !== null) { + let worldX = screenspaceX + global.me?.x; + let worldY = screenspaceY + global.me?.y; + + let button: InputType; + if (e.button == 0) { + button = InputType.Left; + } else if (e.button == 1) { + button = InputType.Middle; + } else if (e.button == 2) { + button = InputType.Right; + } + + let msg = MessageC2SMouseInput.encode({ + worldposX: worldX, + worldposY: worldY, + released: true, + button: button! + }).finish(); + + global.client?.socket.send(encode(MessageC2SMouseInput_packetInfo.type, msg)) + } + } document.onkeydown = (e) => { if (e.code == "ArrowLeft" || e.code == "KeyA") { // arrow-left @@ -213,6 +279,9 @@ async function client_main(server: string, username: string, texture_quality: st global.context.save(); + // x_{screen} = x_{world} - player_{x_{world}} + // x_{world} = x_{screen} + player_{x_{world}} + global.context.translate(module.x - global.me!.x, module.y - global.me!.y); global.context.rotate(module.rotation); diff --git a/client/src/protocol/goodbye_reason.ts b/client/src/protocol/goodbye_reason.ts index 58fcff23e970bb4ff05b13c4a3e3cb4af345f2ed..1eb900315125071cc95ae9da94af63e42458c4a6 100644 --- a/client/src/protocol/goodbye_reason.ts +++ b/client/src/protocol/goodbye_reason.ts @@ -1,35 +1,39 @@ -/* eslint-disable */ +//@ts-nocheck export const protobufPackage = "protocol.goodbye_reason"; export enum GoodbyeReason { - UnsupportedProtocol = 0, - UnexpectedPacket = 1, - UnexpectedNextState = 2, - UsernameTaken = 3, - PingPongTimeout = 4, - Done = 5, + UNKNOWN = 0, + UnsupportedProtocol = 1, + UnexpectedPacket = 2, + UnexpectedNextState = 3, + UsernameTaken = 4, + PingPongTimeout = 5, + Done = 6, UNRECOGNIZED = -1, } export function goodbyeReasonFromJSON(object: any): GoodbyeReason { switch (object) { case 0: + case "UNKNOWN": + return GoodbyeReason.UNKNOWN; + case 1: case "UnsupportedProtocol": return GoodbyeReason.UnsupportedProtocol; - case 1: + case 2: case "UnexpectedPacket": return GoodbyeReason.UnexpectedPacket; - case 2: + case 3: case "UnexpectedNextState": return GoodbyeReason.UnexpectedNextState; - case 3: + case 4: case "UsernameTaken": return GoodbyeReason.UsernameTaken; - case 4: + case 5: case "PingPongTimeout": return GoodbyeReason.PingPongTimeout; - case 5: + case 6: case "Done": return GoodbyeReason.Done; case -1: @@ -41,6 +45,8 @@ export function goodbyeReasonFromJSON(object: any): GoodbyeReason { export function goodbyeReasonToJSON(object: GoodbyeReason): string { switch (object) { + case GoodbyeReason.UNKNOWN: + return "UNKNOWN"; case GoodbyeReason.UnsupportedProtocol: return "UnsupportedProtocol"; case GoodbyeReason.UnexpectedPacket: diff --git a/client/src/protocol/input.ts b/client/src/protocol/input.ts new file mode 100644 index 0000000000000000000000000000000000000000..43db23d6d7e1204cf217c5b76002b18969d5849c --- /dev/null +++ b/client/src/protocol/input.ts @@ -0,0 +1,48 @@ +//@ts-nocheck + +export const protobufPackage = "protocol.input"; + +export enum InputType { + UNKNOWN = 0, + Left = 1, + Middle = 2, + Right = 3, + UNRECOGNIZED = -1, +} + +export function inputTypeFromJSON(object: any): InputType { + switch (object) { + case 0: + case "UNKNOWN": + return InputType.UNKNOWN; + case 1: + case "Left": + return InputType.Left; + case 2: + case "Middle": + return InputType.Middle; + case 3: + case "Right": + return InputType.Right; + case -1: + case "UNRECOGNIZED": + default: + return InputType.UNRECOGNIZED; + } +} + +export function inputTypeToJSON(object: InputType): string { + switch (object) { + case InputType.UNKNOWN: + return "UNKNOWN"; + case InputType.Left: + return "Left"; + case InputType.Middle: + return "Middle"; + case InputType.Right: + return "Right"; + case InputType.UNRECOGNIZED: + default: + return "UNRECOGNIZED"; + } +} diff --git a/client/src/protocol/message_c2s.ts b/client/src/protocol/message_c2s.ts index 3b9dfc1fcf013055c3934b4fc9f62ce1e558c4d2..4f28c5b2f2bba8d69b50bd592a3bf442e502ace0 100644 --- a/client/src/protocol/message_c2s.ts +++ b/client/src/protocol/message_c2s.ts @@ -1,7 +1,7 @@ -// @ts-nocheck - +//@ts-nocheck import * as _m0 from "protobufjs/minimal"; import { GoodbyeReason, goodbyeReasonFromJSON, goodbyeReasonToJSON } from "./goodbye_reason"; +import { InputType, inputTypeFromJSON, inputTypeToJSON } from "./input"; import { State, stateFromJSON, stateToJSON } from "./state"; export const protobufPackage = "protocol.message_c2s"; @@ -13,8 +13,8 @@ export interface MessageC2SHello { requestedUsername: string; /** The state the connection will go into after the handshake. */ nextState: State; - token?: string | undefined; - user?: string | undefined; + token: string; + user: string; } export enum MessageC2SHello_packetInfo { @@ -246,6 +246,46 @@ export function messageC2SAuthenticateAndBeamOut_packetInfoToJSON( } } +export interface MessageC2SMouseInput { + worldposX: number; + worldposY: number; + button: InputType; + released: boolean; +} + +export enum MessageC2SMouseInput_packetInfo { + unknown = 0, + type = 13, + UNRECOGNIZED = -1, +} + +export function messageC2SMouseInput_packetInfoFromJSON(object: any): MessageC2SMouseInput_packetInfo { + switch (object) { + case 0: + case "unknown": + return MessageC2SMouseInput_packetInfo.unknown; + case 13: + case "type": + return MessageC2SMouseInput_packetInfo.type; + case -1: + case "UNRECOGNIZED": + default: + return MessageC2SMouseInput_packetInfo.UNRECOGNIZED; + } +} + +export function messageC2SMouseInput_packetInfoToJSON(object: MessageC2SMouseInput_packetInfo): string { + switch (object) { + case MessageC2SMouseInput_packetInfo.unknown: + return "unknown"; + case MessageC2SMouseInput_packetInfo.type: + return "type"; + case MessageC2SMouseInput_packetInfo.UNRECOGNIZED: + default: + return "UNRECOGNIZED"; + } +} + function createBaseMessageC2SHello(): MessageC2SHello { return { version: 0, requestedUsername: "", nextState: 0, token: "", user: "" }; } @@ -684,6 +724,103 @@ export const MessageC2SAuthenticateAndBeamOut = { }, }; +function createBaseMessageC2SMouseInput(): MessageC2SMouseInput { + return { worldposX: 0, worldposY: 0, button: 0, released: false }; +} + +export const MessageC2SMouseInput = { + encode(message: MessageC2SMouseInput, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { + if (message.worldposX !== 0) { + writer.uint32(9).double(message.worldposX); + } + if (message.worldposY !== 0) { + writer.uint32(17).double(message.worldposY); + } + if (message.button !== 0) { + writer.uint32(24).int32(message.button); + } + if (message.released === true) { + writer.uint32(32).bool(message.released); + } + return writer; + }, + + decode(input: _m0.Reader | Uint8Array, length?: number): MessageC2SMouseInput { + const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); + let end = length === undefined ? reader.len : reader.pos + length; + const message = createBaseMessageC2SMouseInput(); + while (reader.pos < end) { + const tag = reader.uint32(); + switch (tag >>> 3) { + case 1: + if (tag != 9) { + break; + } + + message.worldposX = reader.double(); + continue; + case 2: + if (tag != 17) { + break; + } + + message.worldposY = reader.double(); + continue; + case 3: + if (tag != 24) { + break; + } + + message.button = reader.int32() as any; + continue; + case 4: + if (tag != 32) { + break; + } + + message.released = reader.bool(); + continue; + } + if ((tag & 7) == 4 || tag == 0) { + break; + } + reader.skipType(tag & 7); + } + return message; + }, + + fromJSON(object: any): MessageC2SMouseInput { + return { + worldposX: isSet(object.worldposX) ? Number(object.worldposX) : 0, + worldposY: isSet(object.worldposY) ? Number(object.worldposY) : 0, + button: isSet(object.button) ? inputTypeFromJSON(object.button) : 0, + released: isSet(object.released) ? Boolean(object.released) : false, + }; + }, + + toJSON(message: MessageC2SMouseInput): unknown { + const obj: any = {}; + message.worldposX !== undefined && (obj.worldposX = message.worldposX); + message.worldposY !== undefined && (obj.worldposY = message.worldposY); + message.button !== undefined && (obj.button = inputTypeToJSON(message.button)); + message.released !== undefined && (obj.released = message.released); + return obj; + }, + + create, I>>(base?: I): MessageC2SMouseInput { + return MessageC2SMouseInput.fromPartial(base ?? {}); + }, + + fromPartial, I>>(object: I): MessageC2SMouseInput { + const message = createBaseMessageC2SMouseInput(); + message.worldposX = object.worldposX ?? 0; + message.worldposY = object.worldposY ?? 0; + message.button = object.button ?? 0; + message.released = object.released ?? false; + return message; + }, +}; + type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial = T extends Builtin ? T diff --git a/client/src/protocol/message_s2c.ts b/client/src/protocol/message_s2c.ts index 810bc93259a47a7ad5d9efcafd538d5fdbadaf1b..45c766faea909543e144e5fb2e912a1e97c52f23 100644 --- a/client/src/protocol/message_s2c.ts +++ b/client/src/protocol/message_s2c.ts @@ -1,4 +1,4 @@ -/* eslint-disable */ +//@ts-nocheck import * as _m0 from "protobufjs/minimal"; import { GoodbyeReason, goodbyeReasonFromJSON, goodbyeReasonToJSON } from "./goodbye_reason"; import { Module } from "./module"; diff --git a/client/src/protocol/module.ts b/client/src/protocol/module.ts index 97dae4c6d3f6d6ef19e7562bb3617df9b9d2e8ee..b2ff55acdfefc4a55ab885c9bb9bd9a36f47acf2 100644 --- a/client/src/protocol/module.ts +++ b/client/src/protocol/module.ts @@ -1,24 +1,28 @@ -/* eslint-disable */ +//@ts-nocheck import * as _m0 from "protobufjs/minimal"; export const protobufPackage = "protocol.module"; export enum ModuleType { - Cargo = 0, - LandingThruster = 1, - Hub = 2, + UNKNOWN = 0, + Cargo = 1, + LandingThruster = 2, + Hub = 3, UNRECOGNIZED = -1, } export function moduleTypeFromJSON(object: any): ModuleType { switch (object) { case 0: + case "UNKNOWN": + return ModuleType.UNKNOWN; + case 1: case "Cargo": return ModuleType.Cargo; - case 1: + case 2: case "LandingThruster": return ModuleType.LandingThruster; - case 2: + case 3: case "Hub": return ModuleType.Hub; case -1: @@ -30,6 +34,8 @@ export function moduleTypeFromJSON(object: any): ModuleType { export function moduleTypeToJSON(object: ModuleType): string { switch (object) { + case ModuleType.UNKNOWN: + return "UNKNOWN"; case ModuleType.Cargo: return "Cargo"; case ModuleType.LandingThruster: @@ -59,13 +65,13 @@ export const Module = { writer.uint32(8).int32(message.moduleType); } if (message.rotation !== 0) { - writer.uint32(21).float(message.rotation); + writer.uint32(17).double(message.rotation); } if (message.x !== 0) { - writer.uint32(29).float(message.x); + writer.uint32(25).double(message.x); } if (message.y !== 0) { - writer.uint32(37).float(message.y); + writer.uint32(33).double(message.y); } return writer; }, @@ -85,25 +91,25 @@ export const Module = { message.moduleType = reader.int32() as any; continue; case 2: - if (tag != 21) { + if (tag != 17) { break; } - message.rotation = reader.float(); + message.rotation = reader.double(); continue; case 3: - if (tag != 29) { + if (tag != 25) { break; } - message.x = reader.float(); + message.x = reader.double(); continue; case 4: - if (tag != 37) { + if (tag != 33) { break; } - message.y = reader.float(); + message.y = reader.double(); continue; } if ((tag & 7) == 4 || tag == 0) { diff --git a/client/src/protocol/planet.ts b/client/src/protocol/planet.ts index 22618b074f82da55218c09b19b910a2be05234be..cb8c12f9d73eab7e397da060a3a32fa51a6028a3 100644 --- a/client/src/protocol/planet.ts +++ b/client/src/protocol/planet.ts @@ -1,20 +1,24 @@ -/* eslint-disable */ +//@ts-nocheck import * as _m0 from "protobufjs/minimal"; export const protobufPackage = "protocol.planet"; export enum PlanetType { - Earth = 0, - Moon = 1, + UNKNOWN = 0, + Earth = 1, + Moon = 2, UNRECOGNIZED = -1, } export function planetTypeFromJSON(object: any): PlanetType { switch (object) { case 0: + case "UNKNOWN": + return PlanetType.UNKNOWN; + case 1: case "Earth": return PlanetType.Earth; - case 1: + case 2: case "Moon": return PlanetType.Moon; case -1: @@ -26,6 +30,8 @@ export function planetTypeFromJSON(object: any): PlanetType { export function planetTypeToJSON(object: PlanetType): string { switch (object) { + case PlanetType.UNKNOWN: + return "UNKNOWN"; case PlanetType.Earth: return "Earth"; case PlanetType.Moon: @@ -57,13 +63,13 @@ export const Planet = { writer.uint32(8).int32(message.planetType); } if (message.x !== 0) { - writer.uint32(21).float(message.x); + writer.uint32(17).double(message.x); } if (message.y !== 0) { - writer.uint32(29).float(message.y); + writer.uint32(25).double(message.y); } if (message.radius !== 0) { - writer.uint32(37).float(message.radius); + writer.uint32(33).double(message.radius); } return writer; }, @@ -83,25 +89,25 @@ export const Planet = { message.planetType = reader.int32() as any; continue; case 2: - if (tag != 21) { + if (tag != 17) { break; } - message.x = reader.float(); + message.x = reader.double(); continue; case 3: - if (tag != 29) { + if (tag != 25) { break; } - message.y = reader.float(); + message.y = reader.double(); continue; case 4: - if (tag != 37) { + if (tag != 33) { break; } - message.radius = reader.float(); + message.radius = reader.double(); continue; } if ((tag & 7) == 4 || tag == 0) { diff --git a/client/src/protocol/player.ts b/client/src/protocol/player.ts index af61c6f8729d9cec99d723c0490e4523ae0bceec..6c8a44c7ed614040698e3ae1f55bd84a68d207b0 100644 --- a/client/src/protocol/player.ts +++ b/client/src/protocol/player.ts @@ -1,4 +1,4 @@ -/* eslint-disable */ +//@ts-nocheck import * as _m0 from "protobufjs/minimal"; export const protobufPackage = "protocol.player"; @@ -21,13 +21,13 @@ function createBasePlayer(): Player { export const Player = { encode(message: Player, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { if (message.rotation !== 0) { - writer.uint32(13).float(message.rotation); + writer.uint32(9).double(message.rotation); } if (message.x !== 0) { - writer.uint32(21).float(message.x); + writer.uint32(17).double(message.x); } if (message.y !== 0) { - writer.uint32(29).float(message.y); + writer.uint32(25).double(message.y); } if (message.username !== "") { writer.uint32(34).string(message.username); @@ -43,25 +43,25 @@ export const Player = { const tag = reader.uint32(); switch (tag >>> 3) { case 1: - if (tag != 13) { + if (tag != 9) { break; } - message.rotation = reader.float(); + message.rotation = reader.double(); continue; case 2: - if (tag != 21) { + if (tag != 17) { break; } - message.x = reader.float(); + message.x = reader.double(); continue; case 3: - if (tag != 29) { + if (tag != 25) { break; } - message.y = reader.float(); + message.y = reader.double(); continue; case 4: if (tag != 34) { diff --git a/client/src/protocol/starkingdoms-protocol.ts b/client/src/protocol/starkingdoms-protocol.ts index 814564542868bca3cb52226ce30179767adc31eb..5b687a6cb5e63e30fc8f4dbac9a6aafadb2cdbb4 100644 --- a/client/src/protocol/starkingdoms-protocol.ts +++ b/client/src/protocol/starkingdoms-protocol.ts @@ -1,4 +1,4 @@ -// @ts-nocheck +//@ts-nocheck import * as Long from "long"; import * as _m0 from "protobufjs/minimal"; diff --git a/client/src/protocol/state.ts b/client/src/protocol/state.ts index 08a689f511862152deb29ef2dbfaa118a4a3306b..9e2230898c093210b459f57f812a04f0a8ab7734 100644 --- a/client/src/protocol/state.ts +++ b/client/src/protocol/state.ts @@ -1,19 +1,23 @@ -/* eslint-disable */ +//@ts-nocheck export const protobufPackage = "protocol.state"; export enum State { - Handshake = 0, - Play = 1, + UNKNOWN = 0, + Handshake = 1, + Play = 2, UNRECOGNIZED = -1, } export function stateFromJSON(object: any): State { switch (object) { case 0: + case "UNKNOWN": + return State.UNKNOWN; + case 1: case "Handshake": return State.Handshake; - case 1: + case 2: case "Play": return State.Play; case -1: @@ -25,6 +29,8 @@ export function stateFromJSON(object: any): State { export function stateToJSON(object: State): string { switch (object) { + case State.UNKNOWN: + return "UNKNOWN"; case State.Handshake: return "Handshake"; case State.Play: diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e26fe22d90960f1efe38ec0cb06f58e64813fc97 --- /dev/null +++ b/docker/README.md @@ -0,0 +1 @@ +These files contain essential configuration files used by Ansible to manage and deploy StarKingdoms servers. **DO NOT TOUCH THESE FILES.** \ No newline at end of file diff --git a/protocol/build.rs b/protocol/build.rs index a456eda25274303da528ae990bf8e36d89c79724..70fa20db693354ff8e399dba39f9fd0bee80666b 100644 --- a/protocol/build.rs +++ b/protocol/build.rs @@ -10,5 +10,6 @@ fn main() { .input("src/pbuf/state.proto") .input("src/pbuf/goodbye_reason.proto") .input("src/pbuf/module.proto") + .input("src/pbuf/input.proto") .run_from_script(); } diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 22e5e49cacae6251daee3bb4505f37ba7582f842..44579527cdabaa19376af97b4c50ddbda5f4d5c9 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -1,12 +1,12 @@ use std::error::Error; use protobuf::{Enum, Message}; -use crate::message_c2s::{MessageC2SAuthenticateAndBeamOut, MessageC2SChat, MessageC2SGoodbye, MessageC2SHello, MessageC2SInput, MessageC2SPing}; +use crate::message_c2s::{MessageC2SAuthenticateAndBeamOut, MessageC2SChat, MessageC2SGoodbye, MessageC2SHello, MessageC2SInput, MessageC2SMouseInput, MessageC2SPing}; use crate::message_s2c::{MessageS2CChat, MessageS2CGoodbye, MessageS2CHello, MessageS2CPlanetData, MessageS2CPlayersUpdate, MessageS2CPong, MessageS2CModulesUpdate}; use crate::planet::PlanetType; use crate::starkingdoms_protocol::PacketWrapper; include!(concat!(env!("OUT_DIR"), "/protos/mod.rs")); -pub const PROTOCOL_VERSION: u32 = 3; +pub const PROTOCOL_VERSION: u32 = 4; pub mod api; @@ -17,7 +17,8 @@ pub enum MessageC2S { Chat(MessageC2SChat), Ping(MessageC2SPing), Input(MessageC2SInput), - AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut) + AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut), + MouseInput(MessageC2SMouseInput) } #[derive(Debug)] @@ -55,6 +56,9 @@ impl TryFrom<&[u8]> for MessageC2S { }, _id if _id == message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value() as i64 => { MessageC2S::AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut::parse_from_bytes(&pkt.packet_data)?) + }, + _id if _id == message_c2s::message_c2smouse_input::Packet_info::type_.value() as i64 => { + MessageC2S::MouseInput(MessageC2SMouseInput::parse_from_bytes(&pkt.packet_data)?) } _id => { return Err(format!("Unrecognized C2S packet {}", _id).into()); } }; @@ -85,6 +89,9 @@ impl TryInto> for MessageC2S { }, MessageC2S::AuthenticateAndBeamOut(p) => { (message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value(), p.write_to_bytes()?) + }, + MessageC2S::MouseInput(p) => { + (message_c2s::message_c2smouse_input::Packet_info::type_.value(), p.write_to_bytes()?) } }; @@ -175,7 +182,8 @@ impl planet::PlanetType { pub fn as_texture_id(&self) -> String { match self { PlanetType::Earth => "earth".to_string(), - PlanetType::Moon => "moon".to_string() + PlanetType::Moon => "moon".to_string(), + PlanetType::UNKNOWN => "missing".to_string() } } } diff --git a/protocol/src/pbuf/goodbye_reason.proto b/protocol/src/pbuf/goodbye_reason.proto index 1e23025af05ada10bc885177aeb130af0c495f06..1679dfda8c78b9f92924eb09822235a004b1b934 100644 --- a/protocol/src/pbuf/goodbye_reason.proto +++ b/protocol/src/pbuf/goodbye_reason.proto @@ -2,10 +2,11 @@ syntax = "proto3"; package protocol.goodbye_reason; enum GoodbyeReason { - UnsupportedProtocol = 0; - UnexpectedPacket = 1; - UnexpectedNextState = 2; - UsernameTaken = 3; - PingPongTimeout = 4; - Done = 5; + UNKNOWN = 0; + UnsupportedProtocol = 1; + UnexpectedPacket = 2; + UnexpectedNextState = 3; + UsernameTaken = 4; + PingPongTimeout = 5; + Done = 6; } \ No newline at end of file diff --git a/protocol/src/pbuf/input.proto b/protocol/src/pbuf/input.proto new file mode 100644 index 0000000000000000000000000000000000000000..85f62bfd5955fd7e53d17b26b461e68ab7f00dbb --- /dev/null +++ b/protocol/src/pbuf/input.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package protocol.input; + +enum InputType { + UNKNOWN = 0; + Left = 1; + Middle = 2; + Right = 3; +} \ No newline at end of file diff --git a/protocol/src/pbuf/message_c2s.proto b/protocol/src/pbuf/message_c2s.proto index 18b637f5021c18b44e22c70b011dacb8ef6884bf..e4fa567be51235995b2b8ff74eaa527ed2ea6013 100644 --- a/protocol/src/pbuf/message_c2s.proto +++ b/protocol/src/pbuf/message_c2s.proto @@ -3,6 +3,7 @@ package protocol.message_c2s; import "state.proto"; import "goodbye_reason.proto"; +import "input.proto"; message MessageC2SHello { enum packet_info { unknown = 0; type = 0x01; } @@ -38,6 +39,8 @@ message MessageC2SInput { bool down_pressed = 2; bool left_pressed = 3; bool right_pressed = 4; + + } message MessageC2SAuthenticateAndBeamOut { @@ -45,4 +48,13 @@ message MessageC2SAuthenticateAndBeamOut { string user_id = 1; // The user ID that the client is authenticating as string token = 2; // The token from the authentication server that the user is authenticating as +} + +message MessageC2SMouseInput { + enum packet_info { unknown = 0; type = 0x0d; } + + double worldpos_x = 1; + double worldpos_y = 2; + protocol.input.InputType button = 3; + bool released = 4; } \ No newline at end of file diff --git a/protocol/src/pbuf/module.proto b/protocol/src/pbuf/module.proto index 2755d7cc74e8c0b08a45def914642c74ee342f0a..2a8eb3da2f5e198cd212c0a71e5bd30945558089 100644 --- a/protocol/src/pbuf/module.proto +++ b/protocol/src/pbuf/module.proto @@ -3,13 +3,14 @@ package protocol.module; message Module { ModuleType module_type = 1; - float rotation = 2; - float x = 3; - float y = 4; + double rotation = 2; + double x = 3; + double y = 4; } enum ModuleType { - Cargo = 0; - LandingThruster = 1; - Hub = 2; + UNKNOWN = 0; + Cargo = 1; + LandingThruster = 2; + Hub = 3; } diff --git a/protocol/src/pbuf/planet.proto b/protocol/src/pbuf/planet.proto index 48a1ddda9e4e2a23fc533b6522ad15eeb55badf2..83400ee9a2779135ef0854221f83052e81a2ba0e 100644 --- a/protocol/src/pbuf/planet.proto +++ b/protocol/src/pbuf/planet.proto @@ -3,12 +3,13 @@ package protocol.planet; message Planet { PlanetType planet_type = 1; // Type of the planet - float x = 2; // Translation on the X axis, in game units - float y = 3; // Translation on the Y axis, in game units - float radius = 4; // The radius of the planet extending out from (x, y) + double x = 2; // Translation on the X axis, in game units + double y = 3; // Translation on the Y axis, in game units + double radius = 4; // The radius of the planet extending out from (x, y) } enum PlanetType { - Earth = 0; - Moon = 1; + UNKNOWN = 0; + Earth = 1; + Moon = 2; } \ No newline at end of file diff --git a/protocol/src/pbuf/player.proto b/protocol/src/pbuf/player.proto index d2b8359f6c7b787f98bc80c1f149e893e750f961..1d2652ba9c0156697b060431e654763c93a18fd5 100644 --- a/protocol/src/pbuf/player.proto +++ b/protocol/src/pbuf/player.proto @@ -2,8 +2,8 @@ syntax = "proto3"; package protocol.player; message Player { - float rotation = 1; // The rotation, clockwise, in degrees, of the player - float x = 2; // The translation on the X axis, in game units, of the player - float y = 3; // The translation on the Y axis, in game units, of the player + double rotation = 1; // The rotation, clockwise, in degrees, of the player + double x = 2; // The translation on the X axis, in game units, of the player + double y = 3; // The translation on the Y axis, in game units, of the player string username = 4; // The username of the player } \ No newline at end of file diff --git a/protocol/src/pbuf/starkingdoms-protocol.proto b/protocol/src/pbuf/starkingdoms-protocol.proto index 9381472753e448ea9c4133b2b81e51e915608e4c..b9c270d86776d8e80cf9dbe2f98ad9841c5b2288 100644 --- a/protocol/src/pbuf/starkingdoms-protocol.proto +++ b/protocol/src/pbuf/starkingdoms-protocol.proto @@ -7,6 +7,7 @@ import public "goodbye_reason.proto"; import public "message_s2c.proto"; import public "player.proto"; import public "planet.proto"; +import public "input.proto"; message PacketWrapper { int64 packet_id = 1; // What is the Packet ID of this packet? diff --git a/protocol/src/pbuf/state.proto b/protocol/src/pbuf/state.proto index a9b2e6d0061b0bb113d14f00287cbc95cb65ef79..9aa7e6565426c6dee7d6ddffb333717463bf1dba 100644 --- a/protocol/src/pbuf/state.proto +++ b/protocol/src/pbuf/state.proto @@ -2,6 +2,7 @@ syntax = "proto3"; package protocol.state; enum State { - Handshake = 0; - Play = 1; + UNKNOWN = 0; + Handshake = 1; + Play = 2; } \ No newline at end of file diff --git a/server/src/entity.rs b/server/src/entity.rs index 2ed4f9ef9b08f01e584f08ee516fa1714a0b72c5..95c09f0068db1681d1267c2272992d4b94d76d04 100644 --- a/server/src/entity.rs +++ b/server/src/entity.rs @@ -58,12 +58,11 @@ impl EntityHandler { players } pub fn get_player_from_id(&self, id: EntityId) -> Option { - if let Some(entity) = self.entities.get(&id) { - if let Entity::Player(player) = entity { - return Some(player.clone()); - } + if let Some(Entity::Player(player)) = self.entities.get(&id) { + Some(player.clone()) + } else { + None } - None } pub fn get_player_id(&self, addr: SocketAddr) -> Option { for (id, entity) in self.entities.iter() { @@ -106,10 +105,8 @@ impl EntityHandler { module_count } pub fn get_module_from_id(&self, id: EntityId) -> Option { - if let Some(entity) = self.entities.get(&id) { - if let Entity::Module(module) = entity { - return Some(module.clone()); - } + if let Some(Entity::Module(module)) = self.entities.get(&id) { + return Some(module.clone()); } None } @@ -133,10 +130,8 @@ impl EntityHandler { modules } pub fn get_attached_from_id(&self, id: EntityId) -> Option { - if let Some(entity) = self.entities.get(&id) { - if let Entity::AttachedModule(module) = entity { - return Some(module.clone()); - } + if let Some(Entity::AttachedModule(module)) = self.entities.get(&id) { + return Some(module.clone()); } None } @@ -156,12 +151,11 @@ impl EntityHandler { let mut planets = vec![]; for planet in self.get_planets() { - // TODO: Adjust codegen to use f64 planets.push(starkingdoms_protocol::planet::Planet { planet_type: planet.planet_type.into(), - x: (planet.position.0 * SCALE) as f32, - y: (planet.position.1 * SCALE) as f32, - radius: planet.radius as f32, // DO NOT * SCALE + x: planet.position.0 * SCALE, + y: planet.position.1 * SCALE, + radius: planet.radius, // DO NOT * SCALE. THIS VALUE IS NOT SCALED! special_fields: Default::default(), }); } diff --git a/server/src/handler.rs b/server/src/handler.rs index 72790c4e22c8ab9eb9f871ffa3d96b1f787a6b65..a9ede0e1a968b799454c7fe5c2ab4cf8d6d79f11 100644 --- a/server/src/handler.rs +++ b/server/src/handler.rs @@ -90,6 +90,7 @@ pub async fn handle_client(mgr: ClientManager, entities: Arc unreachable!(), State::Handshake => { match pkt { MessageC2S::Hello(pkt) => { @@ -298,6 +299,9 @@ pub async fn handle_client(mgr: ClientManager, entities: Arc { + debug!("[{}] player input: {:?}", remote_addr, p); } } } diff --git a/server/src/manager.rs b/server/src/manager.rs index de2d08f9fe80e7e80b298be8a62d53946a0c1388..0b97253dcfdf5a00f886c7787b9f6d52f8e367e0 100644 --- a/server/src/manager.rs +++ b/server/src/manager.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::f64::consts::PI; use std::net::SocketAddr; use std::sync::Arc; use nalgebra::point; @@ -39,11 +38,9 @@ impl Player { } pub fn search_modules(&self, entities: &EntityHandler) -> Vec { let mut modules = Vec::new(); - for child in &self.children { - if let Some(attachment) = child { - if let Entity::AttachedModule(child_module) = entities.entities.get(&attachment.child).unwrap() { - modules.append(&mut child_module.search_modules(entities)); - } + for attachment in self.children.iter().flatten() { + if let Entity::AttachedModule(child_module) = entities.entities.get(&attachment.child).unwrap() { + modules.append(&mut child_module.search_modules(entities)); } } modules @@ -79,7 +76,7 @@ impl AttachedModule { let loose_id = entities.get_from_module(&module).expect("loose module does not exist"); let loose_body = data.rigid_body_set.get(module.handle).expect("loose module does not exist"); - let mut parent_entity = entity_map.get_mut(&parent).expect("parent id does not exist"); + let parent_entity = entity_map.get_mut(&parent).expect("parent id does not exist"); let parent_handle = match parent_entity { Entity::Player(player) => { player.handle @@ -91,8 +88,8 @@ impl AttachedModule { panic!("unexpected parent"); } }; - let parent_body = data.rigid_body_set - .get(parent_handle).unwrap(); + //let parent_body = data.rigid_body_set + // .get(parent_handle).unwrap(); // create attachment module let module_collider = ColliderBuilder::cuboid(25.0 / SCALE, 25.0 / SCALE) .mass_properties(loose_body.mass_properties().local_mprops) @@ -147,7 +144,7 @@ impl AttachedModule { //let loose_id = entities.get_from_module(&module).expect("loose module does not exist"); //let loose_body = data.rigid_body_set.get(module.handle).expect("loose module does not exist"); - let mut parent_entity = entity_map.get_mut(&parent).expect("parent id does not exist"); + let parent_entity = entity_map.get_mut(&parent).expect("parent id does not exist"); let parent_handle = match parent_entity { Entity::Player(player) => { player.handle @@ -214,14 +211,12 @@ impl AttachedModule { lifetime: 10., } } + pub fn search_modules(&self, entities: &EntityHandler) -> Vec { let mut modules = Vec::new(); - for child in &self.children { - if let Some(attachment) = child { - let child_module = entities.entities.get(&attachment.child).unwrap(); - if let Entity::AttachedModule(child_module) = entities.entities.get(&attachment.child).unwrap() { - modules.append(&mut child_module.search_modules(entities)); - } + for attachment in self.children.iter().flatten() { + if let Entity::AttachedModule(child_module) = entities.entities.get(&attachment.child).unwrap() { + modules.append(&mut child_module.search_modules(entities)); } } modules diff --git a/server/src/planet.rs b/server/src/planet.rs index 3c229069aa7225c4cdc416a69ae8e5c3aaa05336..36927569ebaaba18cdf4eb61681f53ac53c58d77 100644 --- a/server/src/planet.rs +++ b/server/src/planet.rs @@ -111,9 +111,9 @@ impl Planets { // TODO: Adjust codegen to use f64 planets.push(starkingdoms_protocol::planet::Planet { planet_type: planet.planet_type.into(), - x: (planet.position.0 * SCALE) as f32, - y: (planet.position.1 * SCALE) as f32, - radius: planet.radius as f32, // DO NOT * SCALE + x: planet.position.0 * SCALE, + y: planet.position.1 * SCALE, + radius: planet.radius, // DO NOT * SCALE - THIS VALUE IS NOT SCALED! special_fields: Default::default(), }); } diff --git a/server/src/timer.rs b/server/src/timer.rs index 3857917523aafc1887f28adee2d3d38bda67fcb2..da273b29cb7fdd054b5eb31202ad95dd61198a53 100644 --- a/server/src/timer.rs +++ b/server/src/timer.rs @@ -1,5 +1,5 @@ use std::{time::Duration, sync::Arc, f64::consts::PI}; -use log::{debug, warn, info}; +use log::{warn, info}; use nalgebra::{vector, point}; use rand::Rng; use rapier2d_f64::prelude::{PhysicsPipeline, ColliderBuilder, RigidBodyBuilder, MassProperties, RigidBodyHandle}; @@ -97,7 +97,7 @@ pub async fn timer_main(mgr: ClientManager, physics_data_orig: Arc