aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfusion32 <marcopuzziello@gmail.com>2025-05-25 22:59:27 -0300
committerfusion32 <marcopuzziello@gmail.com>2025-05-25 22:59:27 -0300
commitc2f41059c71a0c7bfc0b64a7f53334d3bfd0ee76 (patch)
tree289089acc4aa6e6228bf8bf207db50ddaf481d90 /src
parentad8213f35523cbb07f418ae275af448a47cc0288 (diff)
downloadgame-c2f41059c71a0c7bfc0b64a7f53334d3bfd0ee76.tar.gz
game-c2f41059c71a0c7bfc0b64a7f53334d3bfd0ee76.zip
impl a couple more `util.cc` functions
Diffstat (limited to 'src')
-rw-r--r--src/common.hh18
-rw-r--r--src/stubs.hh1
-rw-r--r--src/util.cc23
3 files changed, 41 insertions, 1 deletions
diff --git a/src/common.hh b/src/common.hh
index e113e41..58a28c3 100644
--- a/src/common.hh
+++ b/src/common.hh
@@ -134,6 +134,8 @@ void SetErrorFunction(TErrorFunction *Function);
void SetPrintFunction(TPrintFunction *Function);
void error(const char *Text, ...) ATTR_PRINTF(1, 2);
void print(int Level, const char *Text, ...) ATTR_PRINTF(2, 3);
+int random(int Min, int Max);
+bool FileExists(const char *FileName);
bool isSpace(int c);
bool isAlpha(int c);
@@ -147,6 +149,22 @@ int stricmp(const char *s1, const char *s2, int Max = INT_MAX);
char *findFirst(char *s, char c);
char *findLast(char *s, char c);
+template<typename T>
+void RandomShuffle(T *Buffer, int Size){
+ if(Buffer == NULL){
+ error("RandomShuffle: Buffer ist NULL.\n");
+ return;
+ }
+
+ int Max = Size - 1;
+ for(int Min = 0; Min < Max; Min += 1){
+ int Swap = random(Min, Max);
+ if(Swap != Min){
+ std::swap(Buffer[Min], Buffer[Swap]);
+ }
+ }
+}
+
struct TReadStream {
// VIRTUAL FUNCTIONS
// =========================================================================
diff --git a/src/stubs.hh b/src/stubs.hh
index 72c898d..bc89d12 100644
--- a/src/stubs.hh
+++ b/src/stubs.hh
@@ -26,7 +26,6 @@ extern void CronExpire(Object Obj, int Delay);
extern uint32 CronInfo(Object Obj, bool Delete);
extern uint32 CronStop(Object Obj);
extern void DeleteDynamicString(uint32 Number);
-extern bool FileExists(const char *FileName);
extern TCreature *GetCreature(uint32 CreatureID);
extern const char *GetDynamicString(uint32 Number);
extern TConnection *GetFirstConnection(void);
diff --git a/src/util.cc b/src/util.cc
index 418fb98..e0e335b 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -1,5 +1,7 @@
#include "common.hh"
+#include <sys/stat.h>
+
static void (*ErrorFunction)(const char *Text) = NULL;
static void (*PrintFunction)(int Level, const char *Text) = NULL;
@@ -41,6 +43,27 @@ void print(int Level, const char *Text, ...){
}
}
+int random(int Min, int Max){
+ int Range = (Max - Min) + 1;
+ int Result = Min;
+ if(Range > 0){
+ Result += rand() % Range;
+ }
+ return Result;
+}
+
+bool FileExists(const char *FileName){
+ struct stat Buffer;
+ bool Result = true;
+ if(stat(FileName, &Buffer) != 0){
+ if(errno != ENOENT){
+ error("FileExists: Unerwarteter Fehlercode %d.\n", errno);
+ }
+ Result = false;
+ }
+ return Result;
+}
+
// String Utility
// =============================================================================
bool isSpace(int c){