Created
October 19, 2019 14:23
-
-
Save tungaqhd/67bd4bc07d0451d13d3025b997986bbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ai/Game.h> | |
#include <ai/AI.h> | |
#include <time.h> | |
#include <queue> | |
#include <cstdlib> | |
#include <ctime> | |
#define N 105 | |
struct ToaDo | |
{ | |
int x, y, valid, dir = 0; | |
ToaDo(int x1, int y1) | |
{ | |
x = x1, y = y1; | |
if (x < 5 || y < 5 || x > N || y > N) | |
valid = 0; | |
else | |
valid = 1; | |
} | |
}; | |
int cuu = 0; | |
vector<ToaDo> die; | |
int BFS(int x1, int y1, int x2, int y2) | |
{ | |
int history[N][N]; | |
for (int i = 0; i < N; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
history[i][j] = 0; | |
} | |
for (int i = 0; i < die.size(); i++) | |
history[die[i].x][die[i].y] = 1; | |
AI* p_AI = AI::GetInstance(); | |
ToaDo dich = ToaDo(x2, y2); | |
queue< ToaDo > Q; | |
Q.push(ToaDo(x1, y1)); | |
while (!Q.empty()) | |
{ | |
ToaDo tmp = Q.front(); | |
Q.pop(); | |
if (history[tmp.x][tmp.y] == 1) | |
continue; | |
else | |
history[tmp.x][tmp.y] = 1; | |
if (tmp.x == dich.x && tmp.y == dich.y) | |
return tmp.dir; | |
ToaDo ke[4] = { ToaDo(tmp.x, tmp.y - 1), ToaDo(tmp.x + 1, tmp.y), ToaDo(tmp.x, tmp.y + 1), ToaDo(tmp.x - 1, tmp.y) }; | |
for (int i = 0; i < 4; i++) | |
{ | |
if (ke[i].valid == 1 && p_AI->GetBlock(ke[i].x/5, ke[i].y/5) == BLOCK_GROUND) | |
{ | |
if (tmp.dir == 0) | |
ke[i].dir = i + 1; | |
else | |
ke[i].dir = tmp.dir; | |
Q.push(ke[i]); | |
} | |
} | |
} | |
srand(time(NULL)); | |
int a = rand() % 4 + 0; | |
return a; | |
} | |
void AI_Placement() | |
{ | |
/* | |
đặt tank ban đầu | |
TANK_HEAVY // TANK_MEDIUM // TANK_LIGHT | |
DIRECTION_DOWN // DIRECTION_UP // DIRECTION_RIGHT // DIRECTION_LEFT | |
*/ | |
AI* p_AI = AI::GetInstance(); | |
if (p_AI->GetMyTeam() == TEAM_1) { | |
Game::PlaceTank(TANK_LIGHT, 4, 6); | |
Game::PlaceTank(TANK_HEAVY, 1, 6); | |
Game::PlaceTank(TANK_HEAVY, 1, 15); | |
Game::PlaceTank(TANK_HEAVY, 2, 20); | |
} | |
else if (p_AI->GetMyTeam() == TEAM_2) { | |
Game::PlaceTank(TANK_LIGHT, 20, 1); | |
Game::PlaceTank(TANK_HEAVY, 20, 6); | |
Game::PlaceTank(TANK_HEAVY, 20, 15); | |
Game::PlaceTank(TANK_HEAVY, 18, 20); | |
} | |
} | |
void AI_Update() | |
{ | |
AI* p_AI = AI::GetInstance(); | |
std::vector<Strike*> strike = p_AI->GetIncomingStrike(); | |
for (int i = 0; i < strike.size(); i++) | |
{ | |
float x = strike[i]->GetX(); | |
float y = strike[i]->GetY(); | |
int count = strike[i]->GetCountDown(); // Delay (in server loop) before the strike reach the battlefield. | |
int type = strike[i]->GetType(); | |
if (type == POWERUP_AIRSTRIKE) | |
{ | |
// You may want to do something here, like moving your tank away if the strike is on top of your tank. | |
} | |
else if (type == POWERUP_EMP) | |
{ | |
// Run... RUN!!!! | |
} | |
} | |
std::vector<PowerUp*> powerUp = p_AI->GetPowerUpList(); | |
for (int i = 0; i < powerUp.size(); i++) { | |
float x = powerUp[i]->GetX(); | |
float y = powerUp[i]->GetY(); | |
int type = powerUp[i]->GetType(); | |
if (type == POWERUP_AIRSTRIKE) | |
{ | |
// You may want to move your tank to this position to secure this power up. | |
} | |
else if (type == POWERUP_EMP) | |
{ | |
// Do something else | |
} | |
} | |
/* | |
LẶP QUA TỪNG CON TANK CỦA MÌNH, ĐẠI DIỆN BẰNG INDEX I | |
1: TRUE: ĐI | |
2: TRUE: BẮN | |
*/ | |
// tìm tank chết 2 phe | |
for (int i = 0; i < NUMBER_OF_TANK; i++) | |
{ | |
Tank* ta = p_AI->GetMyTank(i); | |
Tank* dich = p_AI->GetEnemyTank(i); | |
float x_ta = ta->GetX(), y_ta = ta->GetY(); | |
float x_d = dich->GetX(), y_d = dich->GetY(); | |
if ((ta == NULL) || (ta->GetHP() == 0)) | |
{ | |
die.push_back(ToaDo(x_ta, y_ta)); | |
} | |
if ((dich == NULL) || (dich->GetHP() == 0)) | |
{ | |
die.push_back(ToaDo(x_d, y_d)); | |
} | |
} | |
for (int i = 0; i < NUMBER_OF_TANK; i++) { | |
Tank* tempTank = p_AI->GetMyTank(i); | |
if ((tempTank == NULL) || (tempTank->GetHP() == 0)) | |
continue; | |
float x = tempTank->GetX(); | |
float y = tempTank->GetY(); | |
// lấy tọa độ địch | |
vector< pair<int, int> > dich; | |
for (int j = 0; j < NUMBER_OF_TANK; ++j) { | |
Tank* tempEnemyTank = p_AI->GetEnemyTank(j); | |
if ((tempEnemyTank == NULL) || (tempEnemyTank->GetHP() == 0)) | |
continue; | |
dich.push_back(make_pair(tempEnemyTank->GetX(), tempEnemyTank->GetY())); | |
} | |
if (p_AI->GetMyTeam() == TEAM_1) { | |
int dir; | |
if (x - (int)x != 0.0 || y - (int)y != 0.0) | |
dir = tempTank->GetDirection(); | |
else | |
dir = BFS(x, y, dich[0].first, dich[0].second); | |
Game::CommandTank(i, dir, true, true); | |
} | |
else { | |
int dir; | |
if (x - (int)x != 0.0 || y - (int)y != 0.0) | |
dir = tempTank->GetDirection(); | |
else | |
dir = BFS(x, y, dich[0].first, dich[0].second); | |
Game::CommandTank(i, dir, true, true); | |
} | |
} | |
if (p_AI->HasAirstrike()) | |
{ | |
for (int i = 0; i < NUMBER_OF_TANK; i++) | |
{ | |
Tank* tempTank = p_AI->GetEnemyTank(i); | |
if (tempTank->GetHP() > 0) { | |
p_AI->UseAirstrike(tempTank->GetX(), tempTank->GetY()); | |
} | |
} | |
} | |
else if (p_AI->HasEMP()) | |
{ | |
for (int i = 0; i < NUMBER_OF_TANK; i++) | |
{ | |
Tank* tempTank = p_AI->GetEnemyTank(i); | |
if (tempTank->GetHP() > 0) | |
{ | |
p_AI->UseEMP(tempTank->GetX(), tempTank->GetY()); | |
} | |
} | |
} | |
Game::GetInstance()->SendCommand(); | |
} | |
//////////////////////////////////////////////////////////// | |
// DON'T TOUCH THIS PART // | |
//////////////////////////////////////////////////////////// | |
int main(int argc, char* argv[]) | |
{ | |
srand(clock()); | |
#ifdef _WIN32 | |
INT rc; | |
WSADATA wsaData; | |
rc = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
if (rc) { | |
printf("WSAStartup Failed.\n"); | |
return 1; | |
} | |
#endif | |
Game::CreateInstance(); | |
Game* p_Game = Game::GetInstance(); | |
// Create connection | |
if (p_Game->Connect(argc, argv) == -1) | |
{ | |
LOG("Failed to connect to server!\n"); | |
return -1; | |
} | |
// Set up function pointers | |
AI::GetInstance()->PlaceTank = &AI_Placement; | |
AI::GetInstance()->Update = &AI_Update; | |
// Polling every 100ms until the connection is dead | |
p_Game->PollingFromServer(); | |
Game::DestroyInstance(); | |
#ifdef _WIN32 | |
WSACleanup(); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment