ANTWAR C++ SDK
Loading...
Searching...
No Matches
control.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <vector>
14#include "game_info.hpp"
15#include "io.hpp"
16
24{
25private:
29
30 /* Updating process after calling read_round_info() */
31
37 {
38 info.towers = std::move(new_towers);
39 info.next_tower_id = info.towers.empty() ? 0 : info.towers.back().id + 1;
40 }
41
46 void update_ants(const std::vector<Ant>& new_ants)
47 {
48 for (auto& ant : new_ants)
49 update_ant(ant);
50 info.next_ant_id = info.ants.empty() ? 0 : info.ants.back().id + 1;
51 }
52
61 void update_ant(const Ant& a)
62 {
63 auto it = std::find_if(info.ants.begin(), info.ants.end(), [a](const Ant &x)
64 { return x.id == a.id; });
65 if (it != info.ants.end()) // not newly generated
66 {
67 if (!(it->x == a.x && it->y == a.y))
68 it->path.push_back(get_direction(it->x, it->y, a.x, a.y));
69 it->x = a.x, it->y = a.y, it->hp = a.hp, it->age = a.age, it->state = a.state;
70 }
71 else // newly generated
72 {
73 info.ants.emplace(it, a);
74 }
75 }
76
83 void update_coins(int coin0, int coin1)
84 {
85 info.set_coin(0, coin0);
86 info.set_coin(1, coin1);
87 }
88
95 void update_bases_hp(int hp0, int hp1)
96 {
97 info.set_base_hp(0, hp0);
98 info.set_base_hp(1, hp1);
99 }
100
105 : info(init_info.second), self_player_id(init_info.first) {}
106
107public:
108 const int self_player_id;
109
115
120 const GameInfo& get_info() const
121 {
122 return info;
123 }
124
130 {
131 return self_operations;
132 }
133
139 {
140 return opponent_operations;
141 }
142
147 {
148 // 1. Read
149 auto result = ::read_round_info();
150 // 2. Update
151 // 1) Towers
152 update_towers(result.towers);
153 // 2) Ants and Pheromone
154 update_ants(result.ants);
158 // 3) Coins and Bases
159 update_coins(result.coin0, result.coin1);
160 update_bases_hp(result.hp0, result.hp1);
161 // 3. Start Next Round
162 // 1) update round number
163 info.round = result.round;
164 // 2) count down super weapons' cd
166 // 3) clear operations
169 }
170
175 {
177 }
178
183 {
184 // 1) count down opponent's super weapons' left-time
186 // 2) apply opponent's operations
187 for (auto& op: opponent_operations)
189 }
190
199 bool append_self_operation(OperationType type, int arg0 = -1, int arg1 = -1)
200 {
201 return append_self_operation(Operation(type, arg0, arg1));
202 }
203
211 {
213 {
215 return true;
216 }
217 return false;
218 }
219
224 {
225 // 1) count down self's long-lasting weapons' left-time
227 // 2) apply self operations
228 for (auto& op: self_operations)
230 }
231
236 {
238 }
239};
T back(T... args)
T begin(T... args)
An integrated module of IO and game state management with simple interfaces for your convenience....
Definition: control.hpp:24
GameInfo info
Current game information.
Definition: control.hpp:26
const GameInfo & get_info() const
Get information about current game state.
Definition: control.hpp:120
bool append_self_operation(OperationType type, int arg0=-1, int arg1=-1)
Try adding an operation to "self_operations". The operation will be constructed with the given type a...
Definition: control.hpp:199
void update_ant(const Ant &a)
Update "info.ants" with ant "a".
Definition: control.hpp:61
const std::vector< Operation > & get_self_operations() const
Get added operations.
Definition: control.hpp:129
void update_ants(const std::vector< Ant > &new_ants)
Update "info.ants" with vector "new_ants" and reset "info.next_ant_id".
Definition: control.hpp:46
void update_coins(int coin0, int coin1)
Update "info.coins".
Definition: control.hpp:83
void read_round_info()
Read round information from judger and update current game state.
Definition: control.hpp:146
void update_bases_hp(int hp0, int hp1)
Update health points (HP) of "info.base[0]" and "info.base[1]".
Definition: control.hpp:95
Controller(InitInfo init_info)
Construct a new Controller object with given init info.
Definition: control.hpp:104
void apply_opponent_operations()
Apply all the operations in "opponent_operations" to current game state.
Definition: control.hpp:182
std::vector< Operation > self_operations
Self operations which are about to be sent.
Definition: control.hpp:27
Controller()
Construct a new Controller object. Read initializing information from judger and initialize.
Definition: control.hpp:114
const int self_player_id
Your player ID.
Definition: control.hpp:108
void read_opponent_operations()
Read opponent's operations from judger and overwrites "opponent_operations".
Definition: control.hpp:174
void apply_self_operations()
Apply all the operations in "self_operations" to current game state.
Definition: control.hpp:223
const std::vector< Operation > & get_opponent_operations() const
Get saved opponent's operations.
Definition: control.hpp:138
void send_self_operations() const
Send all the operations in "self_operations" (i.e. print to stdout)
Definition: control.hpp:235
std::vector< Operation > opponent_operations
Opponent's operations received from judger.
Definition: control.hpp:28
void update_towers(std::vector< Tower > &new_towers)
Update "info.towers" with vector "new_towers" and reset "info.next_tower_id".
Definition: control.hpp:36
bool append_self_operation(Operation op)
Try adding an operation to "self_operations". The operation has been constructed elsewhere....
Definition: control.hpp:210
T clear(T... args)
int get_direction(int x0, int y0, int x1, int y1)
Get the direction of two adjacent points, starting from the first and pointing to the second.
Definition: common.hpp:178
OperationType
Tag for the type of an operation. The integer values of these enumeration items are also their indexe...
Definition: common.hpp:723
T emplace(T... args)
T empty(T... args)
T end(T... args)
T find_if(T... args)
A bottom module used for game state management.
A module for communicating with Judger.
void send_operations(const std::vector< Operation > &ops)
Send some serialized operations with header to judger.
Definition: io.hpp:220
InitInfo read_init_info()
Read information for initialization.
Definition: io.hpp:27
T move(T... args)
T push_back(T... args)
Basic attacking unit.
Definition: common.hpp:230
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
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
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
void global_pheromone_attenuation()
Global pheromone attenuation.
Definition: game_info.hpp:333
bool is_operation_valid(int player_id, const Operation &op) const
Check operation validness.
Definition: game_info.hpp:369
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
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
void set_base_hp(int player_id, int value)
Set the hp of base for a player.
Definition: game_info.hpp:248
Player's operations. It is able to be applied to the map.
Definition: common.hpp:742