Merge pull request #130 from gsemaj/npc

NPC Rotation Gruntwork
This commit is contained in:
dongresource 2020-10-07 02:33:39 +02:00 committed by GitHub
commit 6e3d0868cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 2 deletions

View File

@ -4,9 +4,11 @@
#include "PlayerManager.hpp"
#include "TransportManager.hpp"
#include "TableData.hpp"
#include "limits.h"
#include <sstream>
#include <iterator>
#include <cmath>
std::map<std::string, ChatCommand> ChatManager::commands;
@ -160,7 +162,7 @@ void mssCommand(std::string full, std::vector<std::string>& args, CNSocket* sock
return;
}
// mss <route> reload
// mss <route> test
if (args[2] == "test") {
if (route->empty()) {
ChatManager::sendServerMessage(sock, "[MSS] Route " + std::to_string(routeNum) + " is empty");
@ -175,7 +177,7 @@ void mssCommand(std::string full, std::vector<std::string>& args, CNSocket* sock
// for compatibility: mss <route> export
if (args[2] == "export") {
ChatManager::sendServerMessage(sock, "[MSS] export on " + std::to_string(routeNum));
ChatManager::sendServerMessage(sock, "Wrote gruntwork to " + settings::GRUNTWORKJSON);
TableData::flush();
return;
}
@ -185,6 +187,48 @@ void mssCommand(std::string full, std::vector<std::string>& args, CNSocket* sock
}
void npcRotateCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
PlayerView& plrv = PlayerManager::players[sock];
Player* plr = plrv.plr;
BaseNPC* npc = nullptr;
int lastDist = INT_MAX;
for (auto c = plrv.currentChunks.begin(); c != plrv.currentChunks.end(); c++) { // haha get it
Chunk* chunk = *c;
for (auto _npc = chunk->NPCs.begin(); _npc != chunk->NPCs.end(); _npc++) {
BaseNPC* npcTemp = NPCManager::NPCs[*_npc];
int distXY = std::hypot(plr->x - npcTemp->appearanceData.iX, plr->y - npcTemp->appearanceData.iY);
int dist = std::hypot(distXY, plr->z - npcTemp->appearanceData.iZ);
if (dist < lastDist) {
npc = npcTemp;
lastDist = dist;
}
}
}
if (npc == nullptr) {
ChatManager::sendServerMessage(sock, "[NPCR] No NPCs found nearby");
return;
}
int angle = (plr->angle + 180) % 360;
NPCManager::updateNPCPosition(npc->appearanceData.iNPC_ID, npc->appearanceData.iX, npc->appearanceData.iY, npc->appearanceData.iZ, angle);
TableData::RunningNPCRotations[npc->appearanceData.iNPC_ID] = angle;
// update rotation clientside
INITSTRUCT(sP_FE2CL_NPC_ENTER, pkt);
pkt.NPCAppearanceData = npc->appearanceData;
sock->sendPacket((void*)&pkt, P_FE2CL_NPC_ENTER, sizeof(sP_FE2CL_NPC_ENTER));
ChatManager::sendServerMessage(sock, "[NPCR] Successfully set angle to " + std::to_string(angle) + " for NPC " + std::to_string(npc->appearanceData.iNPC_ID));
}
void refreshCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
Player* plr = PlayerManager::getPlayer(sock);
PlayerManager::sendPlayerTo(sock, plr->x, plr->y, plr->z);
}
void flushCommand(std::string full, std::vector<std::string>& args, CNSocket* sock) {
ChatManager::sendServerMessage(sock, "Wrote gruntwork to " + settings::GRUNTWORKJSON);
TableData::flush();
@ -199,9 +243,11 @@ void ChatManager::init() {
registerCommand("access", 100, accessCommand);
// TODO: add help command
registerCommand("mss", 30, mssCommand);
registerCommand("npcr", 30, npcRotateCommand);
registerCommand("flush", 30, flushCommand);
registerCommand("level", 50, levelCommand);
registerCommand("population", 100, populationCommand);
registerCommand("refresh", 100, refreshCommand);
}
void ChatManager::registerCommand(std::string cmd, int requiredLevel, CommandHandler handlr) {

View File

@ -134,6 +134,11 @@ void NPCManager::destroyNPC(int32_t id) {
std::cout << "npc removed!" << std::endl;
}
void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z, int angle) {
NPCs[id]->appearanceData.iAngle = angle;
updateNPCPosition(id, X, Y, Z);
}
void NPCManager::updateNPCPosition(int32_t id, int X, int Y, int Z) {
BaseNPC* npc = NPCs[id];

View File

@ -27,6 +27,7 @@ namespace NPCManager {
void addNPC(std::vector<Chunk*> viewableChunks, int32_t id);
void removeNPC(std::vector<Chunk*> viewableChunks, int32_t id);
void destroyNPC(int32_t);
void updateNPCPosition(int32_t, int X, int Y, int Z, int angle);
void updateNPCPosition(int32_t, int X, int Y, int Z);
void sendToViewable(BaseNPC* npc, void* buf, uint32_t type, size_t size);

View File

@ -13,6 +13,7 @@
#include <fstream>
std::map<int32_t, std::vector<WarpLocation>> TableData::RunningSkywayRoutes;
std::map<int32_t, int> TableData::RunningNPCRotations;
void TableData::init() {
int32_t nextId = 0;
@ -364,6 +365,17 @@ void TableData::loadGruntwork() {
RunningSkywayRoutes[(int)route["iRouteID"]] = points;
}
// npc rotations
auto npcRot = gruntwork["rotations"];
for (auto _rot = npcRot.begin(); _rot != npcRot.end(); _rot++) {
int32_t npcID = _rot.value()["iNPCID"];
int angle = _rot.value()["iAngle"];
if (NPCManager::NPCs.find(npcID) == NPCManager::NPCs.end())
continue; // NPC not found
BaseNPC* npc = NPCManager::NPCs[npcID];
npc->appearanceData.iAngle = angle;
}
std::cout << "[INFO] Loaded gruntwork.json" << std::endl;
}
catch (const std::exception& err) {
@ -396,5 +408,14 @@ void TableData::flush() {
gruntwork["skyway"].push_back(route);
}
for (auto& pair : RunningNPCRotations) {
nlohmann::json rotation;
rotation["iNPCID"] = (int)pair.first;
rotation["iAngle"] = pair.second;
gruntwork["rotations"].push_back(rotation);
}
file << gruntwork << std::endl;
}

View File

@ -6,6 +6,7 @@
namespace TableData {
extern std::map<int32_t, std::vector<WarpLocation>> RunningSkywayRoutes;
extern std::map<int32_t, int> RunningNPCRotations;
void init();
void cleanup();