-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyBot.cpp
167 lines (146 loc) · 6.73 KB
/
MyBot.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// (©) Copyright 2019 - Team AASM @ UPB
#include <algorithm>
#include <random>
#include <ctime>
#include "hlt/game.hpp"
#include "hlt/constants.hpp"
#include "hlt/log.hpp"
#include "team_AASM/objective.hpp"
#include "team_AASM/status.hpp"
#include "team_AASM/find_optimal_cell.hpp"
using namespace std;
using namespace hlt;
int main(int argc, char* argv[]) {
vector<Status> ships_status(1000);
unsigned int rng_seed;
if (argc > 1) {
rng_seed = static_cast<unsigned int> (stoul(argv[1]));
} else {
rng_seed = static_cast<unsigned int> (time(nullptr));
}
mt19937 rng(rng_seed);
Game game;
/**
* At this point "game" variable is populated with initial map data.
* This is a good place to do computationally expensive start-up
* pre-processing.
* As soon as you call "ready" function below, the 2 second per turn timer
* will start.
*/
game.ready("Team_AASM_Bot");
log::log("Successfully created bot! My Player ID is "
+ to_string(game.my_id) + ". Bot rng seed is "
+ to_string(rng_seed) + ".");
for (;;) {
game.update_frame();
shared_ptr<Player> me = game.me;
unique_ptr<GameMap>& game_map = game.game_map;
vector<Command> command_queue;
for (const auto& ship_iterator : me->ships) {
/** Commands for ships. */
shared_ptr<Ship> ship = ship_iterator.second;
/** Newly created / unloaded ship. */
if (ships_status[ship->id].objective == Objective::NONE) {
ships_status[ship->id].objective_pos =
optimal_mine_cell(ship, game_map);
ships_status[ship->id].objective = Objective::NAVIGATE;
}
/** Navigate towards designated matrix. */
if (ships_status[ship->id].objective == Objective::NAVIGATE) {
if (ships_status[ship->id].objective_pos == ship->position) {
/** STOP! Reached destination. */
ships_status[ship->id].objective = Objective::MINE;
command_queue.push_back(ship->stay_still());
} else {
if (ship->halite < game_map->at(ship->position)->halite
/ constants::MOVE_COST_RATIO) {
command_queue.push_back(ship->stay_still());
} else {
Direction d = game_map->bruteforce_navigate(ship,
ships_status[ship->id].objective_pos);
command_queue.push_back(ship->move(d));
}
}
} else if (ships_status[ship->id].objective == Objective::MINE) {
/** Mine until ship is full. */
if (ship->halite == constants::MAX_HALITE) {
/** Full ship. */
int min = game_map->height;
Position pos;
int distance;
for (const auto& dropoff : me->dropoffs) {
distance =
game_map->calculate_distance(ship->position,
dropoff.second->position);
if (distance < min) {
min = distance;
pos = dropoff.second->position;
}
}
distance = game_map->calculate_distance(ship->position,
me->shipyard->position);
if (distance < min) {
min = distance;
pos = me->shipyard->position;
}
game_map->at(ship->position)->marked_as_target = false;
ships_status[ship->id].objective = Objective::UNLOAD;
ships_status[ship->id].objective_pos = pos;
} else if (game_map->at(ship)->halite
> constants::HALITE_WORTH_MINING
|| (ship->halite < game_map->at(ship)->halite
/ constants::MOVE_COST_RATIO)) {
/** We should continue mining in this cell. */
command_queue.push_back(ship->stay_still());
} else {
/** Not enough halite in this cell. */
game_map->at(ship->position)->marked_as_target = false;
ships_status[ship->id].objective = Objective::NAVIGATE;
ships_status[ship->id].objective_pos =
optimal_mine_cell(ship, game_map);
Direction d = game_map->bruteforce_navigate(ship,
ships_status[ship->id].objective_pos);
command_queue.push_back(ship->move(d));
}
}
if (ships_status[ship->id].objective == Objective::UNLOAD) {
if (ships_status[ship->id].objective_pos == ship->position) {
ships_status[ship->id].objective = Objective::NAVIGATE;
ships_status[ship->id].objective_pos =
optimal_mine_cell(ship, game_map);
}
if ((ship->halite != constants::MAX_HALITE &&
game_map->at(ship->position)->halite >
constants::HALITE_WORTH_MINING)
|| (ship->halite < game_map->at(ship)->halite
/ constants::MOVE_COST_RATIO)) {
command_queue.push_back(ship->stay_still());
} else {
Direction d;
if (game_map->at(ships_status[ship->id].
objective_pos)->is_occupied()
&& game_map->at(ships_status[ship->id].
objective_pos)->ship->owner != me->id
&& game_map->calculate_distance(ship->position,
ships_status[ship->id].objective_pos) == 1) {
d = game_map->kamikaze_navigate(ship,
ships_status[ship->id].objective_pos);
} else {
d = game_map->bruteforce_navigate(ship,
ships_status[ship->id].objective_pos);
}
command_queue.push_back(ship->move(d));
}
}
}
if (game.turn_number <= 200 && me->halite >= constants::SHIP_COST &&
!game_map->at(me->shipyard)->is_occupied()) {
/** Ship creation. */
command_queue.push_back(me->shipyard->spawn());
}
if (!game.end_turn(command_queue)) {
break;
}
}
return 0;
}