1 | #include "Common.h"
|
---|
2 |
|
---|
3 | #include "Compiler.h"
|
---|
4 |
|
---|
5 | #if defined WINDOWS
|
---|
6 | #include <winsock2.h>
|
---|
7 | #endif
|
---|
8 |
|
---|
9 | #include <sstream>
|
---|
10 | #include <cmath>
|
---|
11 | #include <ctime>
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | FLOAT_POSITION POSITION::toFloat() {
|
---|
16 | FLOAT_POSITION floatPosition;
|
---|
17 | floatPosition.x = x;
|
---|
18 | floatPosition.y = y;
|
---|
19 |
|
---|
20 | return floatPosition;
|
---|
21 | }
|
---|
22 |
|
---|
23 | POSITION FLOAT_POSITION::toInt() {
|
---|
24 | POSITION position;
|
---|
25 | position.x = x;
|
---|
26 | position.y = y;
|
---|
27 |
|
---|
28 | return position;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void set_nonblock(int sock)
|
---|
32 | {
|
---|
33 | #if defined WINDOWS
|
---|
34 | unsigned long mode = 1;
|
---|
35 | ioctlsocket(sock, FIONBIO, &mode);
|
---|
36 | #elif defined LINUX
|
---|
37 | int flags = fcntl(sock, F_GETFL,0);
|
---|
38 | assert(flags != -1);
|
---|
39 | fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
---|
40 | #endif
|
---|
41 | }
|
---|
42 |
|
---|
43 | unsigned long long getCurrentMillis()
|
---|
44 | {
|
---|
45 | unsigned long long numMilliseconds;
|
---|
46 |
|
---|
47 | #if defined WINDOWS
|
---|
48 | numMilliseconds = GetTickCount();
|
---|
49 | #elif defined LINUX
|
---|
50 | timespec curTime;
|
---|
51 | clock_gettime(CLOCK_REALTIME, &curTime);
|
---|
52 |
|
---|
53 | numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | return numMilliseconds;
|
---|
57 | }
|
---|
58 |
|
---|
59 | string getCurrentDateTimeString() {
|
---|
60 | ostringstream timeString;
|
---|
61 |
|
---|
62 | time_t millis = time(NULL);
|
---|
63 | struct tm *time = localtime(&millis);
|
---|
64 |
|
---|
65 | timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
|
---|
66 |
|
---|
67 | return timeString.str();
|
---|
68 | }
|
---|
69 |
|
---|
70 | float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
|
---|
71 | float xDiff = pos2.x - pos1.x;
|
---|
72 | float yDiff = pos2.y - pos1.y;
|
---|
73 |
|
---|
74 | return sqrt( pow(xDiff,2) + pow(yDiff,2) );
|
---|
75 | }
|
---|
76 |
|
---|
77 | POSITION screenToMap(POSITION pos)
|
---|
78 | {
|
---|
79 | pos.x = pos.x-300;
|
---|
80 | pos.y = pos.y-100;
|
---|
81 |
|
---|
82 | if (pos.x < 0 || pos.y < 0)
|
---|
83 | {
|
---|
84 | pos.x = -1;
|
---|
85 | pos.y = -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | return pos;
|
---|
89 | }
|
---|
90 |
|
---|
91 | POSITION mapToScreen(POSITION pos)
|
---|
92 | {
|
---|
93 | pos.x = pos.x+300;
|
---|
94 | pos.y = pos.y+100;
|
---|
95 |
|
---|
96 | return pos;
|
---|
97 | }
|
---|