21#include "optional.hpp"
47 for(
int i = 0; i < 2; i++)
61 template<
typename T,
typename Pred>
66 return make_optional<T>(*it);
77 template<
typename T,
typename Pred>
106 return find_all(
ants, [x, y](
const Ant &a){
return a.x == x && a.y == y; });
128 { return a.id == id; });
156 towers, [x, y](
const Tower &t) {
return t.x == x && t.y == y; });
168 towers, [
id](
const Tower &t) {
return t.id == id; });
194 { return t.id == id; });
209 { return t.id == id; });
212 if (it->is_downgrade_valid())
219 void upgrade_generation_speed(
int player_id)
224 void upgrade_generated_ant(
int player_id)
234 coins[player_id] = value;
242 coins[player_id] += change;
250 bases[player_id].hp = value;
258 bases[player_id].hp += change;
270 if (it->state == AntState::Success || it->state == AntState::Fail || it->state == AntState::TooOld)
293 static constexpr double TAU[] = {0.0, 10.0, -5, -3};
296 if (ant.state == AntState::Alive || ant.state == AntState::Frozen)
300 int tau = TAU[ant.state];
301 int player = ant.player;
305 for (
int move: ant.path)
310 visited[x][y] =
true;
312 if (
pheromone[player][x][y] < PHEROMONE_MIN)
316 x +=
OFFSET[y % 2][move][0];
317 y +=
OFFSET[y % 2][move][1];
321 assert(x == ant.x && y == ant.y);
325 if (
pheromone[player][x][y] < PHEROMONE_MIN)
335 for (
int i = 0; i < 2; ++i)
339 PHEROMONE_ATTENUATING_RATIO *
pheromone[i][j][k]
340 + (1 - PHEROMONE_ATTENUATING_RATIO) * PHEROMONE_INIT;
355 [player_id](
const Tower& tower) {
356 return tower.player == player_id;
380 return t && t.value().player == player_id
381 && t.value().is_upgrade_type_valid(op.arg1)
387 return t && t.value().player == player_id
415 bool collide =
false;
419 case OperationType::BuildTower:
421 return op.type == BuildTower && op.arg0 == new_op.arg0 && op.arg1 == new_op.arg1;
425 case OperationType::UpgradeTower:
426 case OperationType::DowngradeTower:
428 return (op.type == UpgradeTower || op.type == DowngradeTower) && op.arg0 == new_op.arg0;
432 case OperationType::UpgradeGeneratedAnt:
433 case OperationType::UpgradeGenerationSpeed:
435 return op.type == UpgradeGeneratedAnt || op.type == UpgradeGenerationSpeed;
439 case OperationType::UseLightningStorm:
440 case OperationType::UseEmpBlaster:
441 case OperationType::UseDeflector:
442 case OperationType::UseEmergencyEvasion:
444 return op.type == new_op.type;
486 if (t.value().type == TowerType::Basic)
520 case OperationType::BuildTower:
523 case OperationType::DowngradeTower:
526 if (t.value().type == TowerType::Basic)
537 return income +
coins[player_id] >= 0;
566 upgrade_generation_speed(player_id);
569 upgrade_generated_ant(player_id);
584 static constexpr double ETA[] = {1.25, 1.00, 0.75};
585 static constexpr int ETA_OFFSET = 1;
590 int cur_dist =
distance(ant.x, ant.y, target_x, target_y);
593 double phero[6][2] = {};
594 static constexpr int WEIGHTED = 0, ORIGINAL = 1;
595 std::fill(&phero[0][0], &phero[0][0] +
sizeof(phero) /
sizeof(
double), -1.0);
598 for (
int i = 0; i < 6; ++i)
601 int x = ant.x +
OFFSET[ant.y % 2][i][0],
602 y = ant.y +
OFFSET[ant.y % 2][i][1];
604 if ((!ant.path.
empty() && ant.path.
back() == (i + 3) % 6) || !
is_path(x, y))
607 int next_dist =
distance(x, y, target_x, target_y);
608 double weight = ETA[next_dist - cur_dist + ETA_OFFSET];
610 phero[i][WEIGHTED] = weight *
pheromone[ant.player][x][y];
611 phero[i][ORIGINAL] =
pheromone[ant.player][x][y];
616 [phero] (
const double ph1[],
const double ph2[]) {
618 if (ph1[WEIGHTED] != ph2[WEIGHTED])
619 return ph1[WEIGHTED] < ph2[WEIGHTED];
621 if (ph1[ORIGINAL] != ph2[ORIGINAL])
622 return ph1[ORIGINAL] < ph2[ORIGINAL];
660 return TOWER_BUILD_PRICE_BASE *
std::pow(TOWER_BUILD_PRICE_RATIO, tower_num);
673 case TowerType::Heavy:
674 case TowerType::Quick:
675 case TowerType::Mortar:
676 return LEVEL2_TOWER_UPGRADE_PRICE;
678 case TowerType::HeavyPlus:
680 case TowerType::Cannon:
681 case TowerType::QuickPlus:
682 case TowerType::Double:
683 case TowerType::Sniper:
684 case TowerType::MortarPlus:
685 case TowerType::Pulse:
686 case TowerType::Missile:
687 return LEVEL3_TOWER_UPGRADE_PRICE;
701 case 0:
return LEVEL2_BASE_UPGRADE_PRICE;
702 case 1:
return LEVEL3_BASE_UPGRADE_PRICE;
727 if (sw.type == EmergencyEvasion)
731 if (sw.
is_in_range(ant.x, ant.y) && ant.player == sw.player)
749 return weapon.type == EmpBlaster && weapon.player != player_id && weapon.is_in_range(x, y);
769 return weapon.type == Deflector && weapon.player == a.player && weapon.is_in_range(a.x, a.y);
780 if (it->player != player_id)
788 if (it->left_time <= 0)
800 for (
int i = 0; i < 2; ++i)
801 for (
int j = 1; j < 5; ++j)
816 fout <<
"id\tplayer\tx\ty\ttype\tcd" <<
std::endl;
817 for (
auto& tower :
towers)
818 fout << tower.id <<
'\t' << tower.player <<
"\t\t" << tower.x <<
'\t' << tower.y <<
'\t' << tower.type <<
'\t' << tower.cd <<
std::endl;
821 fout <<
"id\tplayer\tx\ty\thp\tage\tstate" <<
std::endl;
822 for (
auto& ant :
ants)
823 fout << ant.id <<
'\t' << ant.player <<
"\t\t" << ant.x <<
'\t' << ant.y <<
'\t' << ant.hp <<
'\t' << ant.age <<
'\t' << ant.state <<
std::endl;
843 for (
auto& tower :
towers)
844 fout << tower.id <<
' '
845 << tower.player <<
' '
852 for (
auto& ant :
ants)
853 fout << ant.id <<
' '
866 for (
int player = 0; player < 2; ++player)
882 void dump(
const char filename[])
const
bool is_highland(int player, int x, int y)
Check if a player can build towers at given position.
Definition: common.hpp:163
SuperWeaponType
Tag for the type of a super weapon. The integer values of these enumeration items are also their inde...
Definition: common.hpp:670
static constexpr int SUPER_WEAPON_INFO[5][4]
Static information of all types of super weapons.
Definition: common.hpp:682
TowerType
Tag for the type of a tower. The integer values of these enumeration items are also their indexes.
Definition: common.hpp:319
static constexpr int MAP_SIZE
Size of the map.
Definition: common.hpp:46
static constexpr int OFFSET[2][6][2]
The offsets between the coordinates of the current point and its surrounding 6 points.
Definition: common.hpp:103
@ UpgradeTower
Upgrade a tower.
Definition: common.hpp:726
@ UseEmpBlaster
Use an EMP blaster.
Definition: common.hpp:730
@ UpgradeGenerationSpeed
Increase ant producing speed.
Definition: common.hpp:734
@ UpgradeGeneratedAnt
Increase HP of newly generated ants.
Definition: common.hpp:735
@ UseDeflector
Use a deflector.
Definition: common.hpp:731
@ UseEmergencyEvasion
Use an emergency evasion.
Definition: common.hpp:732
@ BuildTower
Build a tower.
Definition: common.hpp:725
@ DowngradeTower
Downgrade/Destroy a tower.
Definition: common.hpp:727
@ UseLightningStorm
Use a lightning storm.
Definition: common.hpp:729
int distance(int x0, int y0, int x1, int y1)
Get the distance between two points on the map (NOT Euclidean distance).
Definition: common.hpp:114
bool is_path(int x, int y)
Check if the given position is reachable for ants.
Definition: common.hpp:150
bool is_valid_pos(int x, int y)
Check if the given coordinates refers to a valid point on the map.
Definition: common.hpp:137
T emplace_back(T... args)
T setprecision(T... args)
Basic attacking unit.
Definition: common.hpp:230
Target to protect or to destroy.
Definition: common.hpp:613
void upgrade_generated_ant()
Upgrade ant generation speed.
Definition: common.hpp:656
int gen_speed_level
Level of production speed.
Definition: common.hpp:617
void upgrade_generation_speed()
Upgrade ant generation speed.
Definition: common.hpp:648
static constexpr int POSITION[2][2]
Positions for both players.
Definition: common.hpp:621
int ant_level
Level of produced ants.
Definition: common.hpp:618
A module used for game state management, providing interfaces for accessing and modifying various typ...
Definition: game_info.hpp:28
int round
Current round number.
Definition: game_info.hpp:29
bool is_shielded_by_emp(int player_id, int x, int y) const
Check whether a point is shielded by EmpBluster for a player.
Definition: game_info.hpp:746
bool is_shielded_by_deflector(const Ant &a) const
Check whether an ant is shielded by Deflector for a player.
Definition: game_info.hpp:766
optional< Ant > ant_of_id(int id) const
Find the ant of a specific ID.
Definition: game_info.hpp:115
std::vector< Tower > all_towers() const
Get all towers on the map.
Definition: game_info.hpp:141
void update_pheromone(const Ant &ant)
Update pheromone based on the state of an ant.
Definition: game_info.hpp:290
void update_base_hp(int player_id, int change)
Update the hp of base for a player.
Definition: game_info.hpp:256
void dump(std::ofstream &fout) const
Dump current information with ofstream.
Definition: game_info.hpp:837
void dump(const char filename[]) const
Dump current information to file.
Definition: game_info.hpp:882
void count_down_super_weapons_cd()
Count down cd of all types of super weapons.
Definition: game_info.hpp:798
std::vector< Ant > ants
All ants on the map.
Definition: game_info.hpp:31
int next_ant_id
ID of the next generated ant.
Definition: game_info.hpp:38
bool is_shielded_by_emp(const Tower &tower) const
Check whether a tower is shielded by EmpBluster.
Definition: game_info.hpp:757
void update_pheromone_for_ants()
Update pheromone for each ant.
Definition: game_info.hpp:280
void clear_dead_and_succeeded_ants()
Clear ants of state "Success", "Fail" or "TooOld".
Definition: game_info.hpp:266
std::vector< SuperWeapon > super_weapons
Super weapons being used.
Definition: game_info.hpp:35
int get_operation_income(int player_id, const Operation &op) const
Get the income of an operation BEFORE applied. The income could be negative, which means the operatio...
Definition: game_info.hpp:475
void global_pheromone_attenuation()
Global pheromone attenuation.
Definition: game_info.hpp:333
optional< T > find_one(const std::vector< T > &v, Pred pred) const
Find no more than one element in the given vector for which a predicate is true.
Definition: game_info.hpp:62
static int build_tower_cost(int tower_num)
Calculate the cost of building a tower for a player.
Definition: game_info.hpp:658
void update_coin(int player_id, int change)
Update the number of coins for a player.
Definition: game_info.hpp:240
void build_tower(int id, int player, int x, int y, TowerType type=TowerType::Basic)
Emplace a new tower at the back of vector "towers".
Definition: game_info.hpp:181
void downgrade_or_destroy_tower(int id)
Find the tower of a specific ID and downgrade it if possible. Otherwise, erase it from vector "towers...
Definition: game_info.hpp:206
double pheromone[2][MAP_SIZE][MAP_SIZE]
Pheromone of each point on the map: "pheromone[player_id][x][y]".
Definition: game_info.hpp:34
bool is_operation_valid(int player_id, const std::vector< Operation > &ops, const Operation &new_op) const
Check whether the newly added operation is valid, considering not only the operation itself,...
Definition: game_info.hpp:412
std::vector< Ant > all_ants() const
Get all ants on the map.
Definition: game_info.hpp:93
static int destroy_tower_income(int tower_num)
Calculate the income of detroying a tower for a player.
Definition: game_info.hpp:638
bool is_operation_valid(int player_id, const Operation &op) const
Check operation validness.
Definition: game_info.hpp:369
static int use_super_weapon_cost(int type)
Calculate the cost of using a super weapon.
Definition: game_info.hpp:712
int ant_of_id_by_index(int id) const
Find the ant of a specific ID and get its index in vector "ants".
Definition: game_info.hpp:125
int super_weapon_cd[2][SuperWeaponCount]
Super weapon cooldown of both sides: "super_weapon_cd[player_id]".
Definition: game_info.hpp:36
int next_tower_id
ID of the next built tower.
Definition: game_info.hpp:39
std::vector< Tower > towers
All towers on the map.
Definition: game_info.hpp:30
void apply_operation(int player_id, const Operation &op)
Change buildings, targets and coin values based on the given operation.
Definition: game_info.hpp:545
int tower_num_of_player(int player_id) const
Count the number of towers of a player.
Definition: game_info.hpp:350
optional< Tower > tower_of_id(int id) const
Find the tower of a specific ID.
Definition: game_info.hpp:165
bool check_affordable(int player_id, const std::vector< Operation > &ops) const
Check whether a player can afford a set of operations.
Definition: game_info.hpp:511
std::vector< Ant > ant_at(int x, int y) const
Find all ants at a specific point.
Definition: game_info.hpp:104
void use_super_weapon(SuperWeaponType type, int player, int x, int y)
Handle the operation of using a super weapon.
Definition: game_info.hpp:722
int next_move(const Ant &ant) const
Get next moving direction for an ant based on the probability, randomly.
Definition: game_info.hpp:581
int coins[2]
Coins of both sides: "coins[player_id]".
Definition: game_info.hpp:33
std::vector< T > find_all(const std::vector< T > &v, Pred pred) const
Find all elements in the given vector for which a predicate is true.
Definition: game_info.hpp:78
void upgrade_tower(int id, TowerType type)
Find the tower of a specific ID, then upgrade it.
Definition: game_info.hpp:191
void count_down_super_weapons_left_time(int player_id)
Count down left_time of super weapons for a player. Clear it if timeout.
Definition: game_info.hpp:776
void set_coin(int player_id, int value)
Set the number of coins for a player.
Definition: game_info.hpp:232
static int downgrade_tower_income(int type)
Calculate the income of downgrading a tower for a player.
Definition: game_info.hpp:648
static int upgrade_base_cost(int level)
Calculate the cost of upgrading a base's generation speed or level of generated ants.
Definition: game_info.hpp:697
static int upgrade_tower_cost(int type)
Calculate the cost of upgrading a tower.
Definition: game_info.hpp:668
Base bases[2]
Bases of both sides: "bases[player_id]".
Definition: game_info.hpp:32
void show() const
Print current information to file "info.out".
Definition: game_info.hpp:810
void set_base_hp(int player_id, int value)
Set the hp of base for a player.
Definition: game_info.hpp:248
optional< Tower > tower_at(int x, int y) const
Find the tower at a specific point.
Definition: game_info.hpp:153
Player's operations. It is able to be applied to the map.
Definition: common.hpp:742
Random noise generator.
Definition: common.hpp:772
Great choice to knockout your opponent.
Definition: common.hpp:694
bool is_in_range(int x, int y) const
Check whether given position is in the range of effect.
Definition: common.hpp:710
Defense unit. Only choice to get yourself armed to the teeth.
Definition: common.hpp:375