From d964a83d6d4b505b44b42b8ce7c36b7e796233ff Mon Sep 17 00:00:00 2001 From: dongresource Date: Tue, 25 Aug 2020 03:34:53 +0200 Subject: [PATCH 1/5] Respawn points work now. Note that some of them weren't present in clientnpc and will need to be manually added later. --- src/NPCManager.cpp | 5 +++++ src/NPCManager.hpp | 6 +++++- src/PlayerManager.cpp | 22 +++++++++++++++++++--- src/PlayerManager.hpp | 6 +++++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 98d999e..22ce2ff 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -5,11 +5,13 @@ #include #include #include +#include #include "contrib/JSON.hpp" std::map NPCManager::NPCs; std::map NPCManager::Warps; +std::vector NPCManager::RespawnPoints; void NPCManager::init() { // load NPCs from NPCs.json into our NPC manager @@ -24,6 +26,9 @@ void NPCManager::init() { for (nlohmann::json::iterator npc = npcData.begin(); npc != npcData.end(); npc++) { BaseNPC tmp(npc.value()["x"], npc.value()["y"], npc.value()["z"], npc.value()["id"]); NPCs[tmp.appearanceData.iNPC_ID] = tmp; + + if (npc.value()["id"] == 641 || npc.value()["id"] == 642) + RespawnPoints.push_back({npc.value()["x"], npc.value()["y"], ((int)npc.value()["z"]) + RESURRECT_HEIGHT}); } std::cout << "[INFO] populated " << NPCs.size() << " NPCs" << std::endl; diff --git a/src/NPCManager.hpp b/src/NPCManager.hpp index b1897b1..12f17a3 100644 --- a/src/NPCManager.hpp +++ b/src/NPCManager.hpp @@ -5,7 +5,11 @@ #include "NPC.hpp" #include +#include +#define RESURRECT_HEIGHT 400 + +// this should really be called vec3 or something... struct WarpLocation { int x, y, z; }; @@ -13,9 +17,9 @@ struct WarpLocation { namespace NPCManager { extern std::map NPCs; extern std::map Warps; + extern std::vector RespawnPoints; void init(); void updatePlayerNPCS(CNSocket* sock, PlayerView& plr); void npcWarpManager(CNSocket* sock, CNPacketData* data); - } diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 293b3b4..1d6989d 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -575,6 +575,7 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) { return; Player *plr = PlayerManager::getPlayer(sock); + WarpLocation target = PlayerManager::getRespawnPoint(plr); // players respawn at same spot they died at for now... sP_CL2FE_REQ_PC_REGEN* reviveData = (sP_CL2FE_REQ_PC_REGEN*)data->buf; @@ -582,9 +583,9 @@ void PlayerManager::revivePlayer(CNSocket* sock, CNPacketData* data) { response.bMoveLocation = reviveData->eIL; response.PCRegenData.iMapNum = reviveData->iIndex; response.PCRegenData.iHP = 1000 * plr->level; - response.PCRegenData.iX = plr->x; - response.PCRegenData.iY = plr->y; - response.PCRegenData.iZ = plr->z; + response.PCRegenData.iX = target.x; + response.PCRegenData.iY = target.y; + response.PCRegenData.iZ = target.z; sock->sendPacket((void*)&response, P_FE2CL_REP_PC_REGEN_SUCC, sizeof(sP_FE2CL_REP_PC_REGEN_SUCC)); } @@ -636,4 +637,19 @@ void PlayerManager::setSpecialSwitchPlayer(CNSocket* sock, CNPacketData* data) { Player *PlayerManager::getPlayer(CNSocket* key) { return players[key].plr; } + +WarpLocation PlayerManager::getRespawnPoint(Player *plr) { + WarpLocation best; + uint32_t curDist, bestDist = UINT32_MAX; + + for (auto targ : NPCManager::RespawnPoints) { + curDist = sqrt(pow(plr->x - targ.x, 2) + pow(plr->y - targ.y, 2)); + if (curDist < bestDist) { + best = targ; + bestDist = curDist; + } + } + + return best; +} #pragma endregion diff --git a/src/PlayerManager.hpp b/src/PlayerManager.hpp index f719b6a..78f904b 100644 --- a/src/PlayerManager.hpp +++ b/src/PlayerManager.hpp @@ -8,6 +8,8 @@ #include #include +struct WarpLocation; + struct PlayerView { std::list viewable; std::list viewableNPCs; @@ -22,7 +24,6 @@ namespace PlayerManager { void addPlayer(CNSocket* key, Player plr); void removePlayer(CNSocket* key); - Player *getPlayer(CNSocket* key); void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z); std::list getNearbyPlayers(int X, int Y, int dist); @@ -47,4 +48,7 @@ namespace PlayerManager { void enterPlayerVehicle(CNSocket* sock, CNPacketData* data); void exitPlayerVehicle(CNSocket* sock, CNPacketData* data); + + Player *getPlayer(CNSocket* key); + WarpLocation getRespawnPoint(Player *plr); } From 6f1a72ca0f91ca53fd6e24ab0c13f4737a35daa7 Mon Sep 17 00:00:00 2001 From: dongresource Date: Tue, 25 Aug 2020 03:42:52 +0200 Subject: [PATCH 2/5] Shut Computress up. Setting all bits in the first use flags disables tutorial messages. --- src/PlayerManager.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index 1d6989d..c17952f 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -227,6 +227,10 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) { response.PCLoadData2CL.aQuestFlag[0] = -1; + // shut computress up + response.PCLoadData2CL.iFirstUseFlag1 = UINT64_MAX; + response.PCLoadData2CL.iFirstUseFlag2 = UINT64_MAX; + plr.iID = response.iID; plr.SerialKey = enter->iEnterSerialKey; plr.HP = response.PCLoadData2CL.iHP; From 113ce0af075ae6be9f3b8d1594c287b04f928a5e Mon Sep 17 00:00:00 2001 From: dongresource Date: Tue, 25 Aug 2020 03:45:04 +0200 Subject: [PATCH 3/5] Load the NPC and warp JSON files according to the config file. NPC data was being read from the config, but ignored. --- config.ini | 2 ++ src/NPCManager.cpp | 4 ++-- src/settings.cpp | 2 ++ src/settings.hpp | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config.ini b/config.ini index 7ab905e..65173ca 100644 --- a/config.ini +++ b/config.ini @@ -23,6 +23,8 @@ view=20000 motd=Welcome to OpenFusion! # NPC json data npcdata=NPCs.json +# warp target json data +warpdata=warps.json # spawn coordinates (Z is height) # the supplied defaults are at City Hall diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 22ce2ff..1cbf973 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -17,7 +17,7 @@ void NPCManager::init() { // load NPCs from NPCs.json into our NPC manager try { - std::ifstream inFile("NPCs.json"); + std::ifstream inFile(settings::NPCJSON); nlohmann::json npcData; // read file into json @@ -37,7 +37,7 @@ void NPCManager::init() { } try { - std::ifstream infile("warps.json"); + std::ifstream infile(settings::WARPJSON); nlohmann::json warpData; // read file into json diff --git a/src/settings.cpp b/src/settings.cpp index efa5c09..7564ab5 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -18,6 +18,7 @@ int settings::SPAWN_Y = 268451; int settings::SPAWN_Z = -4210; std::string settings::GMPASS = "pass"; std::string settings::NPCJSON = "NPCs.json"; +std::string settings::WARPJSON = "warps.json"; std::string settings::MOTDSTRING = "Welcome to OpenFusion!"; void settings::init() { @@ -43,6 +44,7 @@ void settings::init() { SPAWN_Z = reader.GetInteger("shard", "spawnz", SPAWN_Z); GMPASS = reader.Get("login", "pass", GMPASS); NPCJSON = reader.Get("shard", "npcdata", NPCJSON); + WARPJSON = reader.Get("shard", "warpdata", WARPJSON); MOTDSTRING = reader.Get("shard", "motd", MOTDSTRING); } diff --git a/src/settings.hpp b/src/settings.hpp index ef9e54c..ac0186e 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -12,6 +12,7 @@ namespace settings { extern int SPAWN_Z; extern std::string MOTDSTRING; extern std::string NPCJSON; + extern std::string WARPJSON; extern std::string GMPASS; void init(); From 9aa9b76826474edf60a2469805a576f94d3f7073 Mon Sep 17 00:00:00 2001 From: dongresource Date: Tue, 25 Aug 2020 04:17:47 +0200 Subject: [PATCH 4/5] Made the random characters level 36. They are meant to make testing faster, after all. No point in restricting them from fully testing items. --- src/CNLoginServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CNLoginServer.cpp b/src/CNLoginServer.cpp index 724468c..15a14ce 100644 --- a/src/CNLoginServer.cpp +++ b/src/CNLoginServer.cpp @@ -58,7 +58,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { for (int i = 0; i < charCount; i++) { sP_LS2CL_REP_CHAR_INFO charInfo = sP_LS2CL_REP_CHAR_INFO(); charInfo.iSlot = (int8_t)i + 1; - charInfo.iLevel = (int16_t)1; + charInfo.iLevel = (int16_t)36; charInfo.sPC_Style.iPC_UID = rand(); // unique identifier for the character charInfo.sPC_Style.iNameCheck = 1; charInfo.sPC_Style.iGender = (i%2)+1; // can be 1(boy) or 2(girl) From b79bc56b31c2d5476c2eb90582c95e3dafc34fce Mon Sep 17 00:00:00 2001 From: dongresource Date: Tue, 25 Aug 2020 04:28:42 +0200 Subject: [PATCH 5/5] Implement NPC spawning. Protected by a simplified GM system. Either everyone is a GM (local servers) or nobody is (public servers). --- config.ini | 2 ++ src/CNLoginServer.cpp | 4 ++-- src/NPCManager.cpp | 28 +++++++++++++++++++++++++--- src/NPCManager.hpp | 3 ++- src/settings.cpp | 3 ++- src/settings.hpp | 1 + 6 files changed, 34 insertions(+), 7 deletions(-) diff --git a/config.ini b/config.ini index 65173ca..a74402a 100644 --- a/config.ini +++ b/config.ini @@ -25,6 +25,8 @@ motd=Welcome to OpenFusion! npcdata=NPCs.json # warp target json data warpdata=warps.json +# is everyone a GM? +gm=true # spawn coordinates (Z is height) # the supplied defaults are at City Hall diff --git a/src/CNLoginServer.cpp b/src/CNLoginServer.cpp index 15a14ce..1570ebc 100644 --- a/src/CNLoginServer.cpp +++ b/src/CNLoginServer.cpp @@ -92,7 +92,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { loginSessions[sock].characters[UID].z = charInfo.iZ; loginSessions[sock].characters[UID].PCStyle = charInfo.sPC_Style; loginSessions[sock].characters[UID].PCStyle2 = charInfo.sPC_Style2; - loginSessions[sock].characters[UID].IsGM = false; + loginSessions[sock].characters[UID].IsGM = settings::GM; for (int i = 0; i < AEQUIP_COUNT; i++) { // setup equips @@ -227,7 +227,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { loginSessions[sock].characters[UID].Equip[2].iType = 2; loginSessions[sock].characters[UID].Equip[3].iID = character->sOn_Item.iEquipFootID; // foot! loginSessions[sock].characters[UID].Equip[3].iType = 3; - loginSessions[sock].characters[UID].IsGM = false; + loginSessions[sock].characters[UID].IsGM = settings::GM; sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_CREATE_SUCC, sizeof(sP_LS2CL_REP_CHAR_CREATE_SUCC)); break; diff --git a/src/NPCManager.cpp b/src/NPCManager.cpp index 1cbf973..7685d3e 100644 --- a/src/NPCManager.cpp +++ b/src/NPCManager.cpp @@ -55,7 +55,8 @@ void NPCManager::init() { } - REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpManager); + REGISTER_SHARD_PACKET(P_CL2FE_REQ_PC_WARP_USE_NPC, npcWarpHandler); + REGISTER_SHARD_PACKET(P_CL2FE_REQ_NPC_SUMMON, npcSummonHandler); } void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) { @@ -107,8 +108,7 @@ void NPCManager::updatePlayerNPCS(CNSocket* sock, PlayerView& view) { PlayerManager::players[sock].viewableNPCs = view.viewableNPCs; } -void NPCManager::npcWarpManager(CNSocket* sock, CNPacketData* data) -{ +void NPCManager::npcWarpHandler(CNSocket* sock, CNPacketData* data) { if (data->size != sizeof(sP_CL2FE_REQ_PC_WARP_USE_NPC)) return; // malformed packet @@ -127,3 +127,25 @@ void NPCManager::npcWarpManager(CNSocket* sock, CNPacketData* data) sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC)); } + +void NPCManager::npcSummonHandler(CNSocket *sock, CNPacketData *data) { + if (data->size != sizeof(sP_CL2FE_REQ_NPC_SUMMON)) + return; // malformed packet + + sP_CL2FE_REQ_NPC_SUMMON* req = (sP_CL2FE_REQ_NPC_SUMMON*)data->buf; + INITSTRUCT(sP_FE2CL_NPC_ENTER, resp); + Player *plr = PlayerManager::getPlayer(sock); + + // permission & sanity check + if (!plr->IsGM || req->iNPCType >= NPCs.size()) + return; + + resp.NPCAppearanceData.iNPC_ID = rand(); // cpunch-style + resp.NPCAppearanceData.iNPCType = req->iNPCType; + resp.NPCAppearanceData.iHP = 1000; // TODO: placeholder + resp.NPCAppearanceData.iX = plr->x; + resp.NPCAppearanceData.iY = plr->y; + resp.NPCAppearanceData.iZ = plr->z; + + sock->sendPacket((void*)&resp, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER)); +} diff --git a/src/NPCManager.hpp b/src/NPCManager.hpp index 12f17a3..b6a3c10 100644 --- a/src/NPCManager.hpp +++ b/src/NPCManager.hpp @@ -21,5 +21,6 @@ namespace NPCManager { void init(); void updatePlayerNPCS(CNSocket* sock, PlayerView& plr); - void npcWarpManager(CNSocket* sock, CNPacketData* data); + void npcWarpHandler(CNSocket* sock, CNPacketData* data); + void npcSummonHandler(CNSocket* sock, CNPacketData* data); } diff --git a/src/settings.cpp b/src/settings.cpp index 7564ab5..fcb4606 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -20,6 +20,7 @@ std::string settings::GMPASS = "pass"; std::string settings::NPCJSON = "NPCs.json"; std::string settings::WARPJSON = "warps.json"; std::string settings::MOTDSTRING = "Welcome to OpenFusion!"; +bool settings::GM = false; void settings::init() { INIReader reader("config.ini"); @@ -46,5 +47,5 @@ void settings::init() { NPCJSON = reader.Get("shard", "npcdata", NPCJSON); WARPJSON = reader.Get("shard", "warpdata", WARPJSON); MOTDSTRING = reader.Get("shard", "motd", MOTDSTRING); - + GM = reader.GetBoolean("shard", "gm", GM); } diff --git a/src/settings.hpp b/src/settings.hpp index ac0186e..163939b 100644 --- a/src/settings.hpp +++ b/src/settings.hpp @@ -14,6 +14,7 @@ namespace settings { extern std::string NPCJSON; extern std::string WARPJSON; extern std::string GMPASS; + extern bool GM; void init(); }