-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutilities.cpp
More file actions
44 lines (38 loc) · 1.11 KB
/
utilities.cpp
File metadata and controls
44 lines (38 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "tww.h"
void debug(const char* format, ...) {
if (DEBUG) {
va_list ap;
va_start(ap, format);
fprintf(stdout, (char *) "-##DEBUG##-: ");
vfprintf(stdout, format, ap);
va_end(ap);
fprintf(stdout, "\n");
}
}
void null_terminate(char *string, unsigned int position) {
string[position] = '\0';
}
size_t calculatePaddingSize(int length) {
size_t paddingSize = 0;
if ((paddingSize = length % sizeof(int)) == 0) {
return paddingSize;
}
paddingSize = sizeof(int) - paddingSize;
debug("Padding SIZE: %d", paddingSize);
return paddingSize;
}
int random(int low, int high) {
int n = (rand() % (high - low + 1)) + low;
debug("generated random number %d in range (%d, %d)", n, low, high);
return n;
}
uint32_t calc_p2p_id(unsigned char *name) {
unsigned int hashval;
for (hashval = 0; *name != 0; name++) {
hashval = *name + 31*hashval;
}
return hashval % 1024;
}
void printUserData(struct p2p_user_data user) {
debug("user data:%s hp:%d exp:%d, (%u, %u)", user.name, user.hp, user.exp, user.x, user.y);
}