summaryrefslogtreecommitdiff
path: root/server/server.h
blob: fecc446e00981476009adc3281ca2d81ef78da84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

#include "raylib.h"
#include <stdbool.h>
#include "shared.h"
#include "queue.h"
#include "net_compat.h"

#define TIMEOUT 10.0
#define VISIBILITY_RANGE 20000.0f

static int networkFd = -1;
static struct sockaddr_in server;

// server owns full NPC state including AI

typedef struct {
    int index;
    float dist;
} NPCDist;

typedef struct {
  int id;
  Vec3 position;
  Vec3 spawnPosition;
  Vec3 target;
  float rotation;
  float scale;
  float moveTimer;
  float waitTimer;
  bool isMoving;
  bool active;
  int health;
  int state;
  int model;
} ServerNPC;

// server tracks each connected player
typedef struct {
    int id;
    bool connected;
    char name[32];
    Vec3 position;
    Vec3 velocity;
    float yaw;
    bool onGround;
    float speed;
    int sprint;
    double lastHeartbeat;
    int characterClass;   // 0=warrior, 1=mage etc
} ServerPlayer;

typedef struct {
    int playerId;
    struct sockaddr_in address;  // their IP and port
    bool connected;
    double lastHeartbeat;        // detect disconnects
} ClientConnection;

extern ClientConnection connections[MAX_PLAYERS];
extern PacketQueue incomingQueue;
extern PacketQueue outgoingQueue;
extern Mesh terrainMesh;
extern Matrix terrainTransform;
extern ServerPlayer players[MAX_PLAYERS];
extern ServerNPC    npcs[MAX_NPCS];
extern int playerCount;
extern int npcCount;

void NetworkInit();
void NetworkShutdown();
void *RecvThread(void *arg);
void *SendThread(void *arg);
void *GameThread(void *arg);
void ProcessIncoming();
void HandleClientInput(ClientPacket *packet);
ServerSnapshot BuildSnapshotForPlayer(int playerId);
void CheckHeartbeats();
void UpdateServerPlayer(ServerPlayer *p, int inputs, float yaw, float dt,
                        Mesh terrainMesh, Matrix terrainTransform);
float GetTerrainHeight(float x, float z);
void UpdateServerNPC(ServerNPC *npc, float dt);
int FindPlayerByAddress(struct sockaddr_in *addr);
void HandleNewConnection(struct sockaddr_in *addr, uint8_t *buffer, int size);