ANTWAR C++ SDK
Loading...
Searching...
No Matches
simulate.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "game_info.hpp"
14#include "control.hpp"
15
21{
26};
27
34{
35private:
38
39 /* Round settlement process */
40
53 {
54 /* Lightning Storm Attack */
55
57 {
58 if(sw.type != SuperWeaponType::LightningStorm)
59 continue;
60 for (Ant &ant : info.ants)
61 {
62 if (sw.is_in_range(ant.x, ant.y)
63 && ant.player != sw.player)
64 {
65 ant.hp = 0;
66 ant.state = AntState::Fail;
67 info.update_coin(sw.player, ant.reward());
68 }
69 }
70 }
71
72 /* Tower Attack */
73
74 // Set deflector property
75 for (Ant& ant: info.ants)
76 ant.deflector = info.is_shielded_by_deflector(ant);
77 // Attack
78 for (Tower& tower: info.towers)
79 {
80 // Skip if shielded by EMP
81 if (info.is_shielded_by_emp(tower))
82 continue;
83 // Try to attack
84 auto targets = tower.attack(info.ants);
85 // Get coins if tower killed the target
86 for (int idx: targets)
87 {
88 if (info.ants[idx].state == AntState::Fail)
89 info.update_coin(tower.player, info.ants[idx].reward());
90 }
91 // Reset tower's damage (clear buff effect)
92 tower.damage = TOWER_INFO[tower.type].attack;
93 }
94 // Reset deflector property
95 for (Ant& ant: info.ants)
96 ant.deflector = false;
97 }
98
110 {
111 for (Ant& ant: info.ants)
112 {
113 // Update age regardless of the state
114 ant.age++;
115 // 1) No other action for dead ants
116 if (ant.state == AntState::Fail)
117 continue;
118 // 2) Check if too old
119 if (ant.age > Ant::AGE_LIMIT)
120 ant.state = AntState::TooOld;
121 // 3) Move if possible (alive)
122 if (ant.state == AntState::Alive)
123 ant.move(info.next_move(ant));
124 // 4) Check if success (Mark success even if it reaches the age limit)
125 if (ant.x == Base::POSITION[!ant.player][0] && ant.y == Base::POSITION[!ant.player][1])
126 {
127 ant.state = AntState::Success;
128 info.update_base_hp(!ant.player, -1);
129 info.update_coin(ant.player, 5);
130 // If hp of one side's base reaches 0, game over
131 if (info.bases[!ant.player].hp <= 0)
132 return (ant.player == 0) ? GameState::Player0Win : GameState::Player1Win;
133 }
134 // 5) Unfreeze if frozen
135 if (ant.state == AntState::Frozen)
136 ant.state = AntState::Alive;
137 }
138 return GameState::Running;
139 }
140
146 {
147 for (auto& base: info.bases)
148 {
149 auto ant = base.generate_ant(info.next_ant_id, info.round);
150 if (ant)
151 {
152 info.ants.push_back(std::move(ant.value()));
154 }
155 }
156 }
157
162 void get_basic_income(int player_id)
163 {
164 info.update_coin(player_id, BASIC_INCOME);
165 }
166
167 /* Game judger */
168
175 {
176 if (info.bases[0].hp < info.bases[1].hp)
177 return GameState::Player1Win;
178 else if (info.bases[0].hp > info.bases[1].hp)
179 return GameState::Player0Win;
180 else
181 return GameState::Undecided;
182 }
183
184public:
190
196 {
197 return info;
198 }
199
206 {
207 return operations[player_id];
208 }
209
217 bool add_operation_of_player(int player_id, Operation op)
218 {
219 if (info.is_operation_valid(player_id, operations[player_id], op))
220 {
221 operations[player_id].push_back(op);
222 return true;
223 }
224 return false;
225 }
226
231 void apply_operations_of_player(int player_id)
232 {
233 // 1) count down long-lasting weapons' left-time
235 // 2) apply opponent's operations
236 for (auto& op: operations[player_id])
237 info.apply_operation(player_id, op);
238 }
239
246 {
247 // 1) Judge winner at MAX_ROUND
248 if (info.round == MAX_ROUND)
249 return judge_winner();
250 // 2) Towers attack ants
251 attack_ants();
252 // 3) Ants move
253 GameState state = move_ants();
254 if (state != GameState::Running)
255 return state;
256 // 4) Update pheromone
259 // 5) Clear dead and succeeded ants
261 // 6) Barracks generate new ants
263 // 7) Get basic income
266 // 8) Start next round
267 info.round++;
268 // 9) Count down super weapons' cd
270 // 10) Clear operations
271 operations[0].clear();
272 operations[1].clear();
273
274 return GameState::Running;
275 }
276};
An integrated module for simulation with simple interfaces for your convenience. Built from the game ...
Definition: simulate.hpp:34
void generate_ants()
Bases try generating new ants.
Definition: simulate.hpp:145
GameState move_ants()
Make alive ants move according to pheromone, without modifying pheromone.
Definition: simulate.hpp:109
GameInfo info
Game state.
Definition: simulate.hpp:36
void apply_operations_of_player(int player_id)
Apply all operations in "operations[player_id]" to current state.
Definition: simulate.hpp:231
GameState next_round()
Update game state at the end of current round. This function is called after both players have applie...
Definition: simulate.hpp:245
const GameInfo & get_info()
Get information about current game state.
Definition: simulate.hpp:195
void attack_ants()
Lightning storms snd towers try attacking ants.
Definition: simulate.hpp:52
Simulator(const GameInfo &info)
Construct a new Simulator object from a GameInfo instance. Current game state will be copied.
Definition: simulate.hpp:189
bool add_operation_of_player(int player_id, Operation op)
Try adding an operation to "operations[player_id]". The operation has been constructed elsewhere....
Definition: simulate.hpp:217
const std::vector< Operation > & get_operations_of_player(int player_id) const
Get added operations of a player.
Definition: simulate.hpp:205
std::vector< Operation > operations[2]
Players' operations which are about to be applied to current game state.
Definition: simulate.hpp:37
void get_basic_income(int player_id)
Get the basic income for a player and add it to corresponding coins.
Definition: simulate.hpp:162
GameState judge_winner() const
Judge winner at MAX_ROUND.
Definition: simulate.hpp:174
T clear(T... args)
constexpr TowerInfo TOWER_INFO[]
Static information of all types of tower.
Definition: common.hpp:352
static constexpr int MAX_ROUND
Max number of rounds.
Definition: common.hpp:22
An integrated module of IO and encapsulated game state management.
A bottom module used for game state management.
T move(T... args)
T push_back(T... args)
GameState
Enumerate values showing whether the game is running, and with detailed reasons if the game ends.
Definition: simulate.hpp:21
@ Player1Win
Game ends when player 1 wins the game.
Definition: simulate.hpp:23
@ Player0Win
Game ends when player 0 wins the game.
Definition: simulate.hpp:22
@ Running
Game is still running.
Definition: simulate.hpp:24
@ Undecided
Game ends due to round limit exceeded. Further checking for the winner is needed.
Definition: simulate.hpp:25
Basic attacking unit.
Definition: common.hpp:230
int reward() const
Reward for killing this ant.
Definition: common.hpp:272
void move(int direction)
Move the ant in a specified direction.
Definition: common.hpp:254
static constexpr int POSITION[2][2]
Positions for both players.
Definition: common.hpp:621
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
void update_base_hp(int player_id, int change)
Update the hp of base for a player.
Definition: game_info.hpp:256
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
std::vector< SuperWeapon > super_weapons
Super weapons being used.
Definition: game_info.hpp:35
void global_pheromone_attenuation()
Global pheromone attenuation.
Definition: game_info.hpp:333
void update_coin(int player_id, int change)
Update the number of coins for a player.
Definition: game_info.hpp:240
bool is_operation_valid(int player_id, const Operation &op) const
Check operation validness.
Definition: game_info.hpp:369
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 next_move(const Ant &ant) const
Get next moving direction for an ant based on the probability, randomly.
Definition: game_info.hpp:581
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
Base bases[2]
Bases of both sides: "bases[player_id]".
Definition: game_info.hpp:32
Player's operations. It is able to be applied to the map.
Definition: common.hpp:742
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
std::vector< int > attack(std::vector< Ant > &ants)
Try to attack ants around, and update CD time.
Definition: common.hpp:402