[edfd1d0] | 1 | #include "Common.h"
|
---|
| 2 |
|
---|
[d05086b] | 3 | #include <sstream>
|
---|
[b07eeac] | 4 | #include <cmath>
|
---|
[8f85180] | 5 |
|
---|
| 6 | #if defined WINDOWS
|
---|
| 7 | #include <Windows.h>
|
---|
| 8 | #endif
|
---|
| 9 |
|
---|
[8271c78] | 10 | #include <ctime>
|
---|
| 11 |
|
---|
[b07eeac] | 12 | using namespace std;
|
---|
| 13 |
|
---|
[0693e25] | 14 | FLOAT_POSITION POSITION::toFloat() {
|
---|
| 15 | FLOAT_POSITION floatPosition;
|
---|
| 16 | floatPosition.x = x;
|
---|
| 17 | floatPosition.y = y;
|
---|
| 18 |
|
---|
| 19 | return floatPosition;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | POSITION FLOAT_POSITION::toInt() {
|
---|
| 23 | POSITION position;
|
---|
| 24 | position.x = x;
|
---|
| 25 | position.y = y;
|
---|
| 26 |
|
---|
| 27 | return position;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[edfd1d0] | 30 | void set_nonblock(int sock)
|
---|
| 31 | {
|
---|
[4c202e0] | 32 | #if defined WINDOWS
|
---|
[edfd1d0] | 33 | unsigned long mode = 1;
|
---|
| 34 | ioctlsocket(sock, FIONBIO, &mode);
|
---|
[4c202e0] | 35 | #elif defined LINUX
|
---|
[edfd1d0] | 36 | int flags;
|
---|
| 37 | flags = fcntl(sock, F_GETFL,0);
|
---|
| 38 | assert(flags != -1);
|
---|
| 39 | fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
---|
| 40 | #endif
|
---|
| 41 | }
|
---|
[8f85180] | 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 | }
|
---|
[b07eeac] | 58 |
|
---|
[d05086b] | 59 | string getCurrentDateTimeString() {
|
---|
[8271c78] | 60 | ostringstream timeString;
|
---|
| 61 |
|
---|
[d05086b] | 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 |
|
---|
[b07eeac] | 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 | }
|
---|
[0693e25] | 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 | }
|
---|