mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 15:59:59 -04:00
Up to four players, each in their own game and save, meet in a room. Rooms are Steam lobbies and the games talk through Steam's networking, so nobody forwards a port and nobody runs a server. The game signs in to Steam as its test app (480), so Steam shows the player as playing Spacewar. Off by default; the menu has a Co-op tab with Create a room, Join a room by its ID, Invite a friend, the players, chat and Leave, in English, Russian, Brazilian Portuguese and Simplified Chinese. runtime/steam.c loads steam_api64.dll from beside the exe at run time and declares the C functions it calls itself; nothing of the Steamworks SDK is in the repository, and a player copies the file from a Steam game they have. runtime/coop.c is the room: the lobby's owner is the host, lets games of the same protocol and game build in and lists them, and every game takes positions, chat and progress only from the players on that list. runtime/coop_game.c reads Link from the Player actor and shares progress, items, songs and quest items through the game's own flag routines, each value only ever raised; small keys, rupees, ammunition and hearts stay each player's own, and the save is copied to coop-backup before anything of a room reaches it. The other players show as a name with a distance where their Link stands, projected with the upscaler's camera. One Steam account cannot be in a lobby twice, and this PC has not the memory for two games, so the room and the game side were run with one game and tests/coop_peer.py as the second player over runtime/coop_local.c, a stand-in for Steam on 127.0.0.1 that only the environment turns on: the peer was let in, saw the game's Link in Hyrule Field, chat went both ways, the save was copied, one flag the save lacked was set and a flag the game does not share was refused. On Steam itself one game signed in as app 480, made a room, showed its ID and the player's Steam name on the Co-op tab and left. Not run: two Steam accounts in one room, an invite, a join by room ID over Steam, a host that leaves, a removal, and the menu's text probe on this build. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
125 lines
5.5 KiB
C
125 lines
5.5 KiB
C
// Co-op (coop.c): each player runs their own game; a room connects up to four of them through Steam (steam.h). The
|
|
// room is a Steam lobby, its maker's game is the host, and the games send each other where their Link is, chat lines
|
|
// and what the game layer shares (coop_game.c). docs/coop.md is the players' and the reviewers' description.
|
|
//
|
|
// One host thread (the co-op thread) owns Steam and the room. The menu and the game's thread never wait for it: they
|
|
// read the view it publishes at each step and hand it commands it carries out at its next one.
|
|
#pragma once
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include "steam.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
enum {
|
|
COOP_PROTOCOL = 1, // games of another number do not play together
|
|
COOP_PLAYERS = 4,
|
|
COOP_NAME_CAP = STEAM_NAME_CAP,
|
|
COOP_SCENE_CAP = 48,
|
|
COOP_CHAT_CAP = 200, // bytes of a chat line, UTF-8
|
|
COOP_CHAT_LINES = 8,
|
|
COOP_FRIENDS = 64,
|
|
COOP_GAME_KIND = 16, // message kinds from here on belong to the game layer (coop_game.c)
|
|
COOP_MESSAGE_MAX = STEAM_MESSAGE_MAX - 1, // bytes of a message after its kind
|
|
};
|
|
|
|
typedef enum {
|
|
COOP_OFF, // the setting is off: no thread work, Steam untouched
|
|
COOP_NO_STEAM, // on, but Steam is not there (note says why); tried again every few seconds and at coop_retry
|
|
COOP_READY, // Steam is up, in no room
|
|
COOP_ENTERING, // making or joining a room, or waiting for its host to answer
|
|
COOP_IN_ROOM,
|
|
} CoopState;
|
|
|
|
// Why the last attempt ended, for the menu's status line (ui/coop_page.cpp has the texts).
|
|
typedef enum {
|
|
COOP_NOTE_NONE,
|
|
COOP_NOTE_NO_LIBRARY, COOP_NOTE_OLD_LIBRARY, COOP_NOTE_NO_CLIENT, COOP_NOTE_OTHER_OS,
|
|
COOP_NOTE_CREATE_FAILED, COOP_NOTE_NO_ROOM, COOP_NOTE_FULL, COOP_NOTE_REFUSED, COOP_NOTE_NOT_OURS,
|
|
COOP_NOTE_OTHER_VERSION, COOP_NOTE_REMOVED, COOP_NOTE_HOST_SILENT, COOP_NOTE_LOST, COOP_NOTE_BAD_CODE,
|
|
COOP_NOTE_LEFT,
|
|
} CoopNote;
|
|
|
|
// Where a player's Link is, as their game last said.
|
|
enum { COOP_POSE_ADULT = 1, COOP_POSE_PAUSED = 2 };
|
|
typedef struct {
|
|
float pos[3]; // metres, the game's world
|
|
float vel[3]; // metres a second
|
|
float yaw; // radians about the up axis, the way Link faces
|
|
uint8_t flags; // COOP_POSE_*
|
|
char scene[COOP_SCENE_CAP]; // the area's name as the game has it; "": not in the world (title screen, loading)
|
|
} CoopPose;
|
|
|
|
typedef struct {
|
|
uint64_t id;
|
|
char name[COOP_NAME_CAP];
|
|
uint8_t self, host;
|
|
uint8_t has_pose;
|
|
uint32_t pose_ms; // how long ago the pose arrived
|
|
CoopPose pose;
|
|
} CoopPlayer;
|
|
|
|
typedef struct {
|
|
char name[COOP_NAME_CAP];
|
|
char text[COOP_CHAT_CAP + 1];
|
|
uint64_t at_ms; // plat_now_ms when it arrived
|
|
uint8_t notice; // a line of the room itself ("... joined"): name holds the player, text is empty
|
|
uint8_t joined; // for a notice: 1 joined, 0 left
|
|
} CoopChatLine;
|
|
|
|
typedef struct {
|
|
CoopState state;
|
|
CoopNote note;
|
|
char steam_says[160]; // with COOP_NO_STEAM: the Steam layer's own sentence, for the log's reader
|
|
char code[STEAM_CODE_CAP]; // the room's id, "" outside a room
|
|
uint8_t host; // this game is the room's host
|
|
uint8_t overlay; // Steam's own invite window can be shown
|
|
uint8_t local; // the test stand-in for Steam is in use (two games on one PC)
|
|
int n;
|
|
CoopPlayer players[COOP_PLAYERS];
|
|
uint64_t invite_room; // a friend invites this player to a room (0 none) ...
|
|
char invite_from[COOP_NAME_CAP];
|
|
int nchat;
|
|
CoopChatLine chat[COOP_CHAT_LINES]; // oldest first
|
|
uint32_t friends_serial; // changes when coop_friends has a new list
|
|
} CoopView;
|
|
|
|
void coop_init(void); // starts the co-op thread; it idles while the COOP setting is off
|
|
void coop_shutdown(void); // leaves the room and signs out of Steam, before the process ends
|
|
|
|
void coop_view(CoopView *out);
|
|
uint32_t coop_serial(void); // changes whenever the view does: a reader that keeps a copy compares this first
|
|
|
|
// Commands, from any thread; each is carried out at the co-op thread's next step and none waits.
|
|
void coop_create(void);
|
|
int coop_join(const char *code); // 0: the text is no room id (nothing is asked of Steam)
|
|
void coop_join_invite(void); // the room of the invite the view shows
|
|
void coop_decline_invite(void);
|
|
void coop_leave(void);
|
|
void coop_retry(void); // look for Steam again now
|
|
void coop_invite(uint64_t friend_id);
|
|
void coop_invite_window(void);
|
|
void coop_remove(uint64_t player_id); // the host's: that player is out of the room and stays out
|
|
void coop_chat(const char *text);
|
|
void coop_friends_refresh(void);
|
|
int coop_friends(SteamFriend *out, int max); // the list of the last refresh
|
|
|
|
// ---- for the game layer (coop_game.c)
|
|
void coop_pose_set(const CoopPose *pose); // this game's own Link, each frame he is in the world; NULL when he is not
|
|
// A message of the game layer to everyone in the room (to 0) or to one player; kind from COOP_GAME_KIND on, n at most
|
|
// COOP_MESSAGE_MAX. From any thread; it leaves at the co-op thread's next step. 0 outside a room.
|
|
int coop_post(uint64_t to, uint8_t kind, const void *data, size_t n, int reliable);
|
|
// coop_game.c's part, called on the co-op thread: a message of its kinds from a player of the room, and a player who
|
|
// came (the game layer sends that player what it shares) or went.
|
|
void coop_game_message(uint64_t from, uint8_t kind, const uint8_t *data, size_t n);
|
|
void coop_game_joined(uint64_t id);
|
|
void coop_game_room(int in_room);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|