Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions config/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,64 +53,64 @@
},

"npcs": {
"initialNPCCount": 50,
"maxNPCsPerChunk": 5,
"spawnInterval": 5.0,
"despawnDistance": 150.0
"initial_count": 50,
"max_per_chunk": 5,
"spawn_interval": 5.0,
"despawn_distance": 150.0
},

"mobs": {
"enabled": true,
"spawnZones": [
"spawn_zones": [
{
"name": "goblin_forest",
"center": [100.0, 10.0, 100.0],
"radius": 50.0,
"mobType": 0,
"minLevel": 1,
"maxLevel": 5,
"maxMobs": 15,
"respawnTime": 30.0
"type": 0,
"max": 15,
"min_level": 1,
"max_level": 5,
"respawn_time": 30.0
},
{
"name": "orc_camp",
"center": [500.0, 15.0, 500.0],
"radius": 75.0,
"mobType": 1,
"minLevel": 5,
"maxLevel": 15,
"maxMobs": 10,
"respawnTime": 45.0
"type": 1,
"max": 10,
"min_level": 5,
"max_level": 15,
"respawn_time": 45.0
},
{
"name": "dragon_lair",
"center": [1000.0, 30.0, 1000.0],
"radius": 100.0,
"mobType": 2,
"minLevel": 20,
"maxLevel": 30,
"maxMobs": 3,
"respawnTime": 300.0
"type": 2,
"max": 3,
"min_level": 20,
"max_level": 30,
"respawn_time": 300.0
},
{
"name": "slime_swamp",
"center": [-200.0, 5.0, -200.0],
"radius": 60.0,
"mobType": 3,
"minLevel": 1,
"maxLevel": 10,
"maxMobs": 20,
"respawnTime": 20.0
"type": 3,
"max": 20,
"min_level": 1,
"max_level": 10,
"respawn_time": 20.0
}
],
"experienceMultiplier": 1.0,
"lootDropChance": 1.0
"experience_multiplier": 1.0,
"loot_drop_chance": 1.0
},

"collision": {
"enabled": true,
"gridCellSize": 10.0,
"maxCollisionChecks": 1000
"cell_size": 10.0,
"max_checks": 1000
},

"database": {
Expand All @@ -120,10 +120,10 @@
"name": "data/game.db",
"user": "gameuser",
"password": "password",
"connection_pool":{
"pool":{
"enabled": false,
"pool_size": 10,
"pool_threads": 2,
"size": 10,
"threads": 2,
"reconnect_attempts": 3,
"min_connections": 5,
"max_connections": 20,
Expand All @@ -144,10 +144,12 @@
"console_output": true
},

"pythonScripting": {
"enabled": true,
"scriptDirectory": "scripts",
"hotReload": true
"scripting": {
"python": {
"enabled": true,
"directory": "scripts",
"hot_reload": true
}
},

"game": {
Expand Down
15 changes: 9 additions & 6 deletions include/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,21 @@ class ConfigManager {
bool GetBool(const std::string& key, bool defaultValue = false) const;
std::string GetString(const std::string& key, const std::string& defaultValue = "") const;
std::vector<std::string> GetStringArray(const std::string& key) const;
nlohmann::json GetJson(const std::string& key) const;
nlohmann::json GetJson(const std::string& key, const nlohmann::json& default_value = nlohmann::json()) const;
bool HasKey(const std::string& key) const;

private:
mutable std::mutex configMutex_;
nlohmann::json config_;
std::string configPath_;

std::pair<std::string, std::optional<size_t>> ParseSegment(const std::string& seg) const;
std::vector<std::string> SplitPath(const std::string& path) const;

ConfigManager() = default;
ConfigManager(const ConfigManager&) = delete;
ConfigManager& operator=(const ConfigManager&) = delete;

bool HasProcessConfig() const;
bool ValidateConfig(const nlohmann::json& config) const;

mutable std::mutex configMutex_;
nlohmann::json config_;
std::string configPath_;
};
7 changes: 7 additions & 0 deletions include/game/GameData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ struct PortalData {
bool active = false;
};

struct ChunkParams {
uint64_t timestamp;
uint64_t session_id;
int size;
float spacing;
};

struct ChunkData {
uint64_t timestamp;
uint64_t session_id;
Expand Down
5 changes: 4 additions & 1 deletion include/game/GameLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GameLogic : public LogicCore, public std::enable_shared_from_this<GameLogi
void SendAuthenticationFailure(uint64_t sessionId, const std::string& message);

void SetSendAuthenticationResponseCallback(std::function<void(uint64_t sessionId, const std::string& message, uint64_t playerId)> cb);
void SetSendChunkParamsCallback(std::function<void(uint64_t sessionId, const ChunkParams&)> cb);
void SetSendChunkCallback(std::function<void(uint64_t sessionId, const ChunkData&)> cb);
void SetSendCollisionResponseCallback(std::function<void(uint64_t session_id, const CollisionResult& result)> cb);

Expand All @@ -81,7 +82,8 @@ class GameLogic : public LogicCore, public std::enable_shared_from_this<GameLogi
void SetSendInventoryResponseCallback(std::function<void(uint64_t session_id, const InventoryData& response)> cb);

void OnAuthentication(const AuthenticationData& data);
void OnChunkRequest(const ChunkData& data);
void OnChunkParams(const ChunkParams& req);
void OnChunkData(const ChunkData& data);
void OnCollisionCheck(const CollisionData& data);
void OnPlayerPosition(const PlayerPositionData& data);
void OnPlayerState(const PlayerStateData& data);
Expand Down Expand Up @@ -131,6 +133,7 @@ class GameLogic : public LogicCore, public std::enable_shared_from_this<GameLogi
DatabaseService* dbService_ = nullptr;

std::function<void(uint64_t, const std::string&, uint64_t)> sendAuthResponseCb_;
std::function<void(uint64_t, const ChunkParams&)> sendChunkParamsCb_;
std::function<void(uint64_t, const ChunkData&)> sendChunkCb_;
std::function<void(uint64_t, const CollisionResult&)> sendCollisionResponseCb_;

Expand Down
7 changes: 3 additions & 4 deletions include/network/BinaryProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ namespace BinaryProtocol {
MESSAGE_TYPE_COLLISION_CHECK = 50,

// World messages
MESSAGE_TYPE_CHUNK_DATA = 100,
MESSAGE_TYPE_CHUNK_REQUEST = 101,
MESSAGE_TYPE_TERRAIN_HEIGHT = 102,
MESSAGE_TYPE_BIOME_DATA = 103,
MESSAGE_TYPE_CHUNK_PARAMS = 100,
MESSAGE_TYPE_CHUNK_DATA = 101,
MESSAGE_TYPE_BIOME_DATA = 102,

// Player messages
MESSAGE_TYPE_PLAYER_POSITION = 200,
Expand Down
40 changes: 22 additions & 18 deletions include/process/ProcessPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class ProcessPool {
bool SendToWorker(int workerId, const std::string& message);
std::string ReceiveFromMaster();

bool IsWorkersReady() const;
void WaitForWorkers();
bool IsWorkerAlive(int workerId) const;
void RestartWorker(int workerId);

Expand All @@ -69,24 +71,6 @@ class ProcessPool {
void UnblockSignals(const sigset_t* oldset);

private:
struct WorkerInfo {
pid_t pid;
int groupIdx;
int localWorkerId;
WorkerGroupConfig config;
};

void MasterProcess();
void WorkerProcess(int globalWorkerId, const WorkerGroupConfig& config);
void SetupSignalHandlers();
void CleanupDeadWorkers();
void CloseAllPipes();
void CreateWorkerPipe(int globalWorkerId);

bool WriteAll(int fd, const void* buffer, size_t count);
bool ReadAll(int fd, void* buffer, size_t count, bool nonBlocking = false);
bool DrainPipe(int fd, size_t bytesToDrain);

std::vector<WorkerGroupConfig> groups_;
int totalWorkers_;

Expand All @@ -99,6 +83,13 @@ class ProcessPool {
std::atomic<bool> running_{false};
std::atomic<bool> shutdownRequested_{false};

struct WorkerInfo {
pid_t pid;
int groupIdx;
int localWorkerId;
WorkerGroupConfig config;
};

std::vector<WorkerInfo> workers_;
std::vector<int> workerPipes_;

Expand All @@ -109,4 +100,17 @@ class ProcessPool {

uint32_t maxMessageSize_{1024 * 1024};
uint32_t receiveTimeoutMs_{1000};

std::atomic<bool> workersReady_{false};

void MasterProcess();
void WorkerProcess(int globalWorkerId, const WorkerGroupConfig& config);
void SetupSignalHandlers();
void CleanupDeadWorkers();
void CloseAllPipes();
void CreateWorkerPipe(int globalWorkerId);

bool WriteAll(int fd, const void* buffer, size_t count);
bool ReadAll(int fd, void* buffer, size_t count, bool nonBlocking = false);
bool DrainPipe(int fd, size_t bytesToDrain);
};
Loading
Loading