Horizon Official Technical Documentation
Utility.hpp File Reference
#include <cstdint>
#include <ctime>
#include <chrono>
#include <cstring>
+ Include dependency graph for Utility.hpp:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

uint32_t getMSTime ()
 
uint32_t getMSTimeDiff (uint32_t oldMSTime, uint32_t newMSTime)
 
uint32_t GetMSTimeDiffToNow (uint32_t oldMSTime)
 
int64_t get_sys_time ()
 
uint32_t rgb2bgr (uint32_t rgb)
 
const char * TimeStamp2String (char *str, size_t size, time_t timestamp, const char *format)
 
uint16_t ntows (uint16_t netshort)
 
void PackPosition (int8_t *p, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t sx0, uint8_t sy0)
 
void PackPosition (int8_t *p, uint16_t x, uint16_t y, uint8_t dir)
 
void UnpackPosition (const uint8_t *p, uint16_t *x, uint16_t *y, uint8_t *dir)
 
unsigned int GetULong (unsigned char *p)
 
float GetFloat (const unsigned char *buf)
 
int16_t MakeShortLE (int16_t val)
 
int32_t MakeLongLE (int32_t val)
 
uint16_t GetUShort (const unsigned char *buf)
 
uint32_t GetULong (const unsigned char *buf)
 
uint64_t GetULongLong (const unsigned char *buf)
 
int32_t GetLong (const unsigned char *buf)
 
int64_t GetLongLong (const unsigned char *buf)
 
uint32_t Concatenate (uint32_t a, uint32_t b)
 

Function Documentation

◆ Concatenate()

uint32_t Concatenate ( uint32_t  a,
uint32_t  b 
)
240{
241 uint32_t magnitude = 1;
242 while(magnitude <= b)
243 magnitude *= 10;
244 return magnitude * a + b;
245}

◆ get_sys_time()

int64_t get_sys_time ( )
69{
70#ifdef WIN32
71 // Windows: GetTickCount/GetTickCount64: Return the number of
72 // milliseconds that have elapsed since the system was started.
73
74 // TODO: GetTickCount/GetTickCount64 has a resolution of only 10~15ms.
75 // Ai4rei recommends that we replace it with either performance
76 // counters or multimedia timers if we want it to be more accurate.
77 // I'm leaving this for a future follow-up patch.
78
79 // GetTickCount64 is only available in Windows Vista / Windows Server
80 // 2008 or newer. Since we still support older versions, this runtime
81 // check is required in order not to crash.
82 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms724411%28v=vs.85%29.aspx
83 static bool first = true;
84 static ULONGLONG(WINAPI *pGetTickCount64)(void) = nullptr;
85
86 if (first) {
87 HMODULE hlib = GetModuleHandle(TEXT("KERNEL32.DLL"));
88 if (hlib != nullptr)
89 pGetTickCount64 = (ULONGLONG(WINAPI *)(void))GetProcAddress(hlib, "GetTickCount64");
90 first = false;
91 }
92 if (pGetTickCount64)
93 return (int64_t)pGetTickCount64();
94 // 32-bit fall back. Note: This will wrap around every ~49 days since system startup!!!
95 return (int64_t)GetTickCount();
96#elif defined(CLOCK_MONOTONIC)
97 // Monotonic clock: Implementation-defined.
98 // Clock that cannot be set and represents monotonic time since some
99 // unspecified starting point. This clock is not affected by
100 // discontinuous jumps in the system time (e.g., if the system
101 // administrator manually changes the clock), but is affected by
102 // the incremental adjustments performed by adjtime(3) and NTP.
103 struct timespec tval;
104 clock_gettime(CLOCK_MONOTONIC, &tval);
105 // int64 cast to avoid overflows on platforms where time_t is 32 bit
106 return (int64_t) (tval.tv_sec * 1000 + tval.tv_nsec / 1000000);
107#else
108 // Fall back, regular clock: Number of milliseconds since epoch.
109 // The time returned by gettimeofday() is affected by discontinuous
110 // jumps in the system time (e.g., if the system administrator
111 // manually changes the system time). If you need a monotonically
112 // increasing clock, see clock_gettime(2).
113 struct timeval tval;
114 gettimeofday(&tval, nullptr);
115 // int64 cast to avoid overflows on platforms where time_t is 32 bit
116 return (int64_t) tval.tv_sec * 1000 + tval.tv_usec / 1000;
117#endif
118}

Referenced by Horizon::Zone::ZC_NOTIFY_TIME::deliver(), Horizon::Zone::ZC_ACCEPT_ENTER::deliver(), Horizon::Zone::ZC_ACCEPT_ENTER2::deliver(), Horizon::Zone::ZC_ACCEPT_ENTER3::deliver(), Horizon::Zone::CombatRegistry::SkillResultOperation::execute(), Horizon::Zone::CombatRegistry::MeleeResultOperation::execute(), Horizon::Zone::ScriptManager::initialize_basic_state(), GridUnitMovementNotifier::notify(), and Horizon::Zone::Unit::walk().

+ Here is the caller graph for this function:

◆ GetFloat()

float GetFloat ( const unsigned char *  buf)
176{
177 uint32_t val = GetULong((unsigned char *) buf);
178 return *((float *) (void *) &val);
179}
unsigned int GetULong(unsigned char *p)
Definition: Utility.cpp:169

References GetULong().

Referenced by Horizon::Libraries::MapCache::GetMapFromGRF().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetLong()

int32_t GetLong ( const unsigned char *  buf)
230{
231 return (int32_t) GetULong(buf);
232}

References GetULong().

Referenced by GRF::load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetLongLong()

int64_t GetLongLong ( const unsigned char *  buf)
235{
236 return (int64_t) GetULongLong(buf);
237}
uint64_t GetULongLong(const unsigned char *buf)
Definition: Utility.cpp:216

References GetULongLong().

Referenced by GRF::load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMSTime()

uint32_t getMSTime ( )
40{
41 using namespace std::chrono;
42
43 static const steady_clock::time_point ApplicationStartTime = steady_clock::now();
44
45 return uint32_t(duration_cast<milliseconds>(steady_clock::now() - ApplicationStartTime).count());
46}
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100

References GridTypeListIterator::count().

Referenced by GetMSTimeDiffToNow().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMSTimeDiff()

uint32_t getMSTimeDiff ( uint32_t  oldMSTime,
uint32_t  newMSTime 
)
49{
50 // getMSTime() have limited data range and this is case when it overflow in this tick
51 if (oldMSTime > newMSTime)
52 return (0xFFFFFFFF - oldMSTime) + newMSTime;
53 else
54 return newMSTime - oldMSTime;
55}

Referenced by GetMSTimeDiffToNow().

+ Here is the caller graph for this function:

◆ GetMSTimeDiffToNow()

uint32_t GetMSTimeDiffToNow ( uint32_t  oldMSTime)
63{
64 return getMSTimeDiff(oldMSTime, getMSTime());
65}
uint32_t getMSTime()
Definition: Utility.cpp:39
uint32_t getMSTimeDiff(uint32_t oldMSTime, uint32_t newMSTime)
Definition: Utility.cpp:48

References getMSTime(), and getMSTimeDiff().

+ Here is the call graph for this function:

◆ GetULong() [1/2]

uint32_t GetULong ( const unsigned char *  buf)
209{
210 return (((uint32_t) (buf[0])))
211 | (((uint32_t)(buf[1])) << 0x08)
212 | (((uint32_t)(buf[2])) << 0x10)
213 | (((uint32_t)(buf[3])) << 0x18);
214}

◆ GetULong() [2/2]

unsigned int GetULong ( unsigned char *  p)
170{
171 return (p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24);
172}

Referenced by GetFloat(), GetLong(), Horizon::Libraries::MapCache::GetMapFromGRF(), and GRF::load().

+ Here is the caller graph for this function:

◆ GetULongLong()

uint64_t GetULongLong ( const unsigned char *  buf)
217{
218 return (((uint64_t)(buf[0])))
219 | (((uint64_t)(buf[1])) << 0x08)
220 | (((uint64_t)(buf[2])) << 0x10)
221 | (((uint64_t)(buf[3])) << 0x18)
222 | (((uint64_t)(buf[4])) << 0x20)
223 | (((uint64_t)(buf[5])) << 0x28)
224 | (((uint64_t)(buf[6])) << 0x30)
225 | (((uint64_t)(buf[7])) << 0x38);
226}

Referenced by GetLongLong().

+ Here is the caller graph for this function:

◆ GetUShort()

uint16_t GetUShort ( const unsigned char *  buf)
203{
204 return (((uint16_t) (buf[0]))) | (((uint16_t)(buf[1])) << 0x08);
205}

◆ MakeLongLE()

int32_t MakeLongLE ( int32_t  val)
192{
193 unsigned char buf[4];
194 buf[0] = (unsigned char)( (val & 0x000000FF) );
195 buf[1] = (unsigned char)( (val & 0x0000FF00) >> 0x08 );
196 buf[2] = (unsigned char)( (val & 0x00FF0000) >> 0x10 );
197 buf[3] = (unsigned char)( (val & 0xFF000000) >> 0x18 );
198 return *((int32_t *)buf);
199}

◆ MakeShortLE()

int16_t MakeShortLE ( int16_t  val)
183{
184 unsigned char buf[2];
185 buf[0] = (unsigned char)( (val & 0x00FF) );
186 buf[1] = (unsigned char)( (val & 0xFF00) >> 0x08 );
187 return *((int16_t *)buf);
188}

◆ ntows()

uint16_t ntows ( uint16_t  netshort)
137{
138 return (uint16_t) (((netshort & 0xFF) << 8) | ((netshort & 0xFF00) >> 8));
139}

◆ PackPosition() [1/2]

void PackPosition ( int8_t *  p,
uint16_t  x,
uint16_t  y,
uint8_t  dir 
)

◆ PackPosition() [2/2]

void PackPosition ( int8_t *  p,
uint16_t  x0,
uint16_t  y0,
uint16_t  x1,
uint16_t  y1,
uint8_t  sx0,
uint8_t  sy0 
)
159 {
160 p[0] = (int8_t) (x0 >> 2);
161 p[1] = (int8_t) ((x0 << 6) | ((y0 >> 4) & 0x3f));
162 p[2] = (int8_t) ((y0 << 4) | ((x1 >> 6) & 0x0f));
163 p[3] = (int8_t) ((x1 << 2) | ((y1 >> 8) & 0x03));
164 p[4] = (int8_t) y1;
165 p[5] = (int8_t) ((sx0 << 4) | (sy0 & 0x0f));
166}

◆ rgb2bgr()

uint32_t rgb2bgr ( uint32_t  rgb)
58{
59 return (((rgb) & 0x0000FF) << 16 | ((rgb) & 0x00FF00) | ((rgb) & 0xFF0000) >> 16);
60}

◆ TimeStamp2String()

const char * TimeStamp2String ( char *  str,
size_t  size,
time_t  timestamp,
const char *  format 
)
121{
122 size_t len = 0;
123
124 if (str == nullptr)
125 return nullptr;
126
127 len = strftime(str, size, format, localtime(&timestamp));
128
129 memset(str + len, '\0', size - len);
130
131 return str;
132}

◆ UnpackPosition()

void UnpackPosition ( const uint8_t *  p,
uint16_t *  x,
uint16_t *  y,
uint8_t *  dir 
)
149{
150 if (x != nullptr)
151 *x = ((p[0] & 0xff) << 2) | (p[1] >> 6);
152 if (y != nullptr)
153 *y = ((p[1] & 0x3f) << 4) | (p[2] >> 4);
154 if (dir != nullptr)
155 *dir = (p[2] & 0x0f);
156}

Referenced by Horizon::Zone::CZ_REQUEST_MOVE::deserialize(), Horizon::Zone::CZ_REQUEST_MOVE2::deserialize(), and Horizon::Zone::CZ_REQUEST_MOVENPC::deserialize().

+ Here is the caller graph for this function: