mirror of
https://github.com/mod-playerbots/mod-playerbots.git
synced 2026-01-24 14:06:22 +00:00
[CHORE] Util classes format and simple cleanup with generated resources (#2028)
- Did some basic formatting - Some generated docs - Cleaned header/impl Helper.css - Moved PerfMonitor from util to bot/handler/command Still a freaking mess though, but its a start i guess. Cant ask ppl to add more or make use of those when its so messy.
This commit is contained in:
@@ -5,6 +5,17 @@
|
|||||||
|
|
||||||
#include "Helpers.h"
|
#include "Helpers.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Case-insensitive substring search.
|
||||||
|
*/
|
||||||
char* strstri(char const* haystack, char const* needle)
|
char* strstri(char const* haystack, char const* needle)
|
||||||
{
|
{
|
||||||
if (!*needle)
|
if (!*needle)
|
||||||
@@ -16,7 +27,9 @@ char* strstri(char const* haystack, char const* needle)
|
|||||||
{
|
{
|
||||||
if (tolower(*haystack) == tolower(*needle))
|
if (tolower(*haystack) == tolower(*needle))
|
||||||
{
|
{
|
||||||
char const *h = haystack, *n = needle;
|
char const* h = haystack;
|
||||||
|
char const* n = needle;
|
||||||
|
|
||||||
for (; *h && *n; ++h, ++n)
|
for (; *h && *n; ++h, ++n)
|
||||||
{
|
{
|
||||||
if (tolower(*h) != tolower(*n))
|
if (tolower(*h) != tolower(*n))
|
||||||
@@ -35,16 +48,67 @@ char* strstri(char const* haystack, char const* needle)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim whitespace from the left side of a string (in place).
|
||||||
|
*/
|
||||||
std::string& ltrim(std::string& s)
|
std::string& ltrim(std::string& s)
|
||||||
{
|
{
|
||||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim whitespace from the right side of a string (in place).
|
||||||
|
*/
|
||||||
std::string& rtrim(std::string& s)
|
std::string& rtrim(std::string& s)
|
||||||
{
|
{
|
||||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base(), s.end());
|
s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base(), s.end());
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trim whitespace from both ends of a string (in place).
|
||||||
|
*/
|
||||||
std::string& trim(std::string& s) { return ltrim(rtrim(s)); }
|
std::string& trim(std::string& s) { return ltrim(rtrim(s)); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split a string using a C-string delimiter.
|
||||||
|
*/
|
||||||
|
void split(std::vector<std::string>& dest, std::string const str, char const* delim)
|
||||||
|
{
|
||||||
|
char* pTempStr = strdup(str.c_str());
|
||||||
|
char* pWord = strtok(pTempStr, delim);
|
||||||
|
|
||||||
|
while (pWord != nullptr)
|
||||||
|
{
|
||||||
|
dest.push_back(pWord);
|
||||||
|
pWord = strtok(nullptr, delim);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(pTempStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split a string using a single character delimiter.
|
||||||
|
*/
|
||||||
|
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems)
|
||||||
|
{
|
||||||
|
std::stringstream ss(s);
|
||||||
|
std::string item;
|
||||||
|
|
||||||
|
while (getline(ss, item, delim))
|
||||||
|
{
|
||||||
|
elems.push_back(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return elems;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split a string using a single character delimiter.
|
||||||
|
*/
|
||||||
|
std::vector<std::string> split(std::string const s, char delim)
|
||||||
|
{
|
||||||
|
std::vector<std::string> elems;
|
||||||
|
return split(s, delim, elems);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,50 +6,68 @@
|
|||||||
#ifndef _PLAYERBOT_HELPERS_H
|
#ifndef _PLAYERBOT_HELPERS_H
|
||||||
#define _PLAYERBOT_HELPERS_H
|
#define _PLAYERBOT_HELPERS_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <string>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cctype>
|
|
||||||
#include <functional>
|
|
||||||
#include <locale>
|
|
||||||
#include <map>
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Common.h"
|
/**
|
||||||
|
* Case-insensitive substring search.
|
||||||
|
*
|
||||||
|
* @param haystack The string to search in
|
||||||
|
* @param needle The substring to search for
|
||||||
|
* @return Pointer to the first matching position in haystack, or nullptr if not found.
|
||||||
|
*/
|
||||||
|
char* strstri(char const* haystack, char const* needle);
|
||||||
|
|
||||||
void split(std::vector<std::string>& dest, std::string const str, char const* delim)
|
/**
|
||||||
{
|
* Trim whitespace from the left side of a string (in place).
|
||||||
char* pTempStr = strdup(str.c_str());
|
*
|
||||||
char* pWord = strtok(pTempStr, delim);
|
* @param s The string to trim
|
||||||
|
* @return Reference to the modified string
|
||||||
|
*/
|
||||||
|
std::string& ltrim(std::string& s);
|
||||||
|
|
||||||
while (pWord != nullptr)
|
/**
|
||||||
{
|
* Trim whitespace from the right side of a string (in place).
|
||||||
dest.push_back(pWord);
|
*
|
||||||
pWord = strtok(nullptr, delim);
|
* @param s The string to trim
|
||||||
}
|
* @return Reference to the modified string
|
||||||
|
*/
|
||||||
|
std::string& rtrim(std::string& s);
|
||||||
|
|
||||||
free(pTempStr);
|
/**
|
||||||
}
|
* Trim whitespace from both ends of a string (in place).
|
||||||
|
*
|
||||||
|
* @param s The string to trim
|
||||||
|
* @return Reference to the modified string
|
||||||
|
*/
|
||||||
|
std::string& trim(std::string& s);
|
||||||
|
|
||||||
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems)
|
/**
|
||||||
{
|
* Split a string using a C-string delimiter.
|
||||||
std::stringstream ss(s);
|
*
|
||||||
std::string item;
|
* @param dest Vector to store split tokens
|
||||||
|
* @param str String to split
|
||||||
|
* @param delim C-string delimiter
|
||||||
|
*/
|
||||||
|
void split(std::vector<std::string>& dest, std::string const str, char const* delim);
|
||||||
|
|
||||||
while (getline(ss, item, delim))
|
/**
|
||||||
{
|
* Split a string using a single character delimiter.
|
||||||
elems.push_back(item);
|
*
|
||||||
}
|
* @param s String to split
|
||||||
|
* @param delim Delimiter character
|
||||||
|
* @param elems Vector to store split tokens
|
||||||
|
* @return Reference to the vector containing tokens
|
||||||
|
*/
|
||||||
|
std::vector<std::string>& split(std::string const s, char delim, std::vector<std::string>& elems);
|
||||||
|
|
||||||
return elems;
|
/**
|
||||||
}
|
* Split a string using a single character delimiter.
|
||||||
|
*
|
||||||
std::vector<std::string> split(std::string const s, char delim)
|
* @param s String to split
|
||||||
{
|
* @param delim Delimiter character
|
||||||
std::vector<std::string> elems;
|
* @return Vector containing split tokens
|
||||||
return split(s, delim, elems);
|
*/
|
||||||
}
|
std::vector<std::string> split(std::string const s, char delim);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,16 +6,45 @@
|
|||||||
#ifndef _PLAYERBOT_LAZYCALCULATEDVALUE_H
|
#ifndef _PLAYERBOT_LAZYCALCULATEDVALUE_H
|
||||||
#define _PLAYERBOT_LAZYCALCULATEDVALUE_H
|
#define _PLAYERBOT_LAZYCALCULATEDVALUE_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Lazy calculation helper.
|
||||||
|
*
|
||||||
|
* Stores a function pointer (calculator) and its owner instance, and
|
||||||
|
* calculates the value only when it is requested for the first time.
|
||||||
|
* The result is cached until Reset() is called.
|
||||||
|
*
|
||||||
|
* @tparam TValue Type of the calculated value.
|
||||||
|
* @tparam TOwner Type of the owner class containing the calculator function.
|
||||||
|
*/
|
||||||
template <class TValue, class TOwner>
|
template <class TValue, class TOwner>
|
||||||
class LazyCalculatedValue
|
class LazyCalculatedValue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Type of the calculator function.
|
||||||
|
*
|
||||||
|
* This is a pointer to a member function of TOwner returning TValue.
|
||||||
|
*/
|
||||||
typedef TValue (TOwner::*Calculator)();
|
typedef TValue (TOwner::*Calculator)();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Constructor.
|
||||||
|
*
|
||||||
|
* @param owner Pointer to the owner object.
|
||||||
|
* @param calculator Pointer to the member function used to calculate the value.
|
||||||
|
*/
|
||||||
LazyCalculatedValue(TOwner* owner, Calculator calculator) : calculator(calculator), owner(owner) { Reset(); }
|
LazyCalculatedValue(TOwner* owner, Calculator calculator) : calculator(calculator), owner(owner) { Reset(); }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Get the cached value or calculate it if needed.
|
||||||
|
*
|
||||||
|
* If the value has not been calculated yet, it calls the calculator
|
||||||
|
* on the owner and caches the result.
|
||||||
|
*
|
||||||
|
* @return TValue The calculated or cached value.
|
||||||
|
*/
|
||||||
TValue GetValue()
|
TValue GetValue()
|
||||||
{
|
{
|
||||||
if (!calculated)
|
if (!calculated)
|
||||||
@@ -27,13 +56,19 @@ public:
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reset the cached state.
|
||||||
|
*
|
||||||
|
* After calling Reset(), the next call to GetValue() will recalculate
|
||||||
|
* the value again.
|
||||||
|
*/
|
||||||
void Reset() { calculated = false; }
|
void Reset() { calculated = false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Calculator calculator;
|
Calculator calculator; ///< Pointer to calculator member function
|
||||||
TOwner* owner;
|
TOwner* owner; ///< Owner instance
|
||||||
bool calculated;
|
bool calculated; ///< Whether value has already been calculated
|
||||||
TValue value;
|
TValue value; ///< Cached value
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -45,11 +45,13 @@ void ServerFacade::SetFacingTo(Player* bot, WorldObject* wo, bool force)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
float angle = bot->GetAngle(wo);
|
float angle = bot->GetAngle(wo);
|
||||||
|
|
||||||
// if (!force && bot->isMoving())
|
// if (!force && bot->isMoving())
|
||||||
// bot->SetFacingTo(bot->GetAngle(wo));
|
// bot->SetFacingTo(bot->GetAngle(wo));
|
||||||
// else
|
// else
|
||||||
// {
|
// {
|
||||||
bot->SetOrientation(angle);
|
bot->SetOrientation(angle);
|
||||||
|
|
||||||
if (!bot->IsRooted())
|
if (!bot->IsRooted())
|
||||||
bot->SendMovementFlagUpdate();
|
bot->SendMovementFlagUpdate();
|
||||||
// }
|
// }
|
||||||
@@ -64,16 +66,14 @@ Unit* ServerFacade::GetChaseTarget(Unit* target)
|
|||||||
{
|
{
|
||||||
return static_cast<ChaseMovementGenerator<Player> const*>(movementGen)->GetTarget();
|
return static_cast<ChaseMovementGenerator<Player> const*>(movementGen)->GetTarget();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
return static_cast<ChaseMovementGenerator<Creature> const*>(movementGen)->GetTarget();
|
||||||
return static_cast<ChaseMovementGenerator<Creature> const*>(movementGen)->GetTarget();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerFacade::SendPacket(Player *player, WorldPacket *packet)
|
void ServerFacade::SendPacket(Player* player, WorldPacket* packet)
|
||||||
{
|
{
|
||||||
return player->GetSession()->SendPacket(packet);
|
player->GetSession()->SendPacket(packet);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,24 @@ class Unit;
|
|||||||
class WorldObject;
|
class WorldObject;
|
||||||
class WorldPacket;
|
class WorldPacket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Provides a simplified interface to server engine operations.
|
||||||
|
*
|
||||||
|
* ServerFacade acts as a wrapper around common server functions used by
|
||||||
|
* the Playerbot system. It centralizes utility methods for distance
|
||||||
|
* calculations, facing, chase target retrieval, and packet sending.
|
||||||
|
*/
|
||||||
class ServerFacade
|
class ServerFacade
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServerFacade(){};
|
ServerFacade() {}
|
||||||
virtual ~ServerFacade(){};
|
virtual ~ServerFacade() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get singleton instance.
|
||||||
|
*
|
||||||
|
* @return ServerFacade* Pointer to the singleton instance.
|
||||||
|
*/
|
||||||
static ServerFacade* instance()
|
static ServerFacade* instance()
|
||||||
{
|
{
|
||||||
static ServerFacade instance;
|
static ServerFacade instance;
|
||||||
@@ -25,19 +38,92 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Get 2D distance between a unit and a world object.
|
||||||
|
*
|
||||||
|
* The result is rounded to one decimal place.
|
||||||
|
*
|
||||||
|
* @param unit Source unit.
|
||||||
|
* @param wo Target world object.
|
||||||
|
* @return float Distance in yards.
|
||||||
|
*/
|
||||||
float GetDistance2d(Unit* unit, WorldObject* wo);
|
float GetDistance2d(Unit* unit, WorldObject* wo);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get 2D distance between a unit and coordinates.
|
||||||
|
*
|
||||||
|
* The result is rounded to one decimal place.
|
||||||
|
*
|
||||||
|
* @param unit Source unit.
|
||||||
|
* @param x Target X coordinate.
|
||||||
|
* @param y Target Y coordinate.
|
||||||
|
* @return float Distance in yards.
|
||||||
|
*/
|
||||||
float GetDistance2d(Unit* unit, float x, float y);
|
float GetDistance2d(Unit* unit, float x, float y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compare two distances.
|
||||||
|
*
|
||||||
|
* @param dist1 First distance.
|
||||||
|
* @param dist2 Second distance.
|
||||||
|
* @return true if dist1 < dist2.
|
||||||
|
*/
|
||||||
bool IsDistanceLessThan(float dist1, float dist2);
|
bool IsDistanceLessThan(float dist1, float dist2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compare two distances.
|
||||||
|
*
|
||||||
|
* @param dist1 First distance.
|
||||||
|
* @param dist2 Second distance.
|
||||||
|
* @return true if dist1 > dist2.
|
||||||
|
*/
|
||||||
bool IsDistanceGreaterThan(float dist1, float dist2);
|
bool IsDistanceGreaterThan(float dist1, float dist2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compare two distances.
|
||||||
|
*
|
||||||
|
* @param dist1 First distance.
|
||||||
|
* @param dist2 Second distance.
|
||||||
|
* @return true if dist1 >= dist2.
|
||||||
|
*/
|
||||||
bool IsDistanceGreaterOrEqualThan(float dist1, float dist2);
|
bool IsDistanceGreaterOrEqualThan(float dist1, float dist2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Compare two distances.
|
||||||
|
*
|
||||||
|
* @param dist1 First distance.
|
||||||
|
* @param dist2 Second distance.
|
||||||
|
* @return true if dist1 <= dist2.
|
||||||
|
*/
|
||||||
bool IsDistanceLessOrEqualThan(float dist1, float dist2);
|
bool IsDistanceLessOrEqualThan(float dist1, float dist2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set bot facing towards a world object.
|
||||||
|
*
|
||||||
|
* @param bot Player bot to rotate.
|
||||||
|
* @param wo Target world object.
|
||||||
|
* @param force If true, force facing even while moving.
|
||||||
|
*/
|
||||||
void SetFacingTo(Player* bot, WorldObject* wo, bool force = false);
|
void SetFacingTo(Player* bot, WorldObject* wo, bool force = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the current chase target of a unit.
|
||||||
|
*
|
||||||
|
* @param target Unit that is chasing.
|
||||||
|
* @return Unit* The chase target, or nullptr if not chasing.
|
||||||
|
*/
|
||||||
Unit* GetChaseTarget(Unit* target);
|
Unit* GetChaseTarget(Unit* target);
|
||||||
|
|
||||||
void SendPacket(Player *player, WorldPacket* packet);
|
/**
|
||||||
|
* @brief Send a raw packet to a player.
|
||||||
|
*
|
||||||
|
* @param player Player to receive the packet.
|
||||||
|
* @param packet Packet to send.
|
||||||
|
*/
|
||||||
|
void SendPacket(Player* player, WorldPacket* packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Global singleton accessor. */
|
||||||
#define sServerFacade ServerFacade::instance()
|
#define sServerFacade ServerFacade::instance()
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user