ANTWAR C++ SDK
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <vector>
14#include <string>
15#include <utility>
16#include <iostream>
17#include "common.hpp"
18
19/* Input */
20
22
28{
29 int self_player_id;
30 unsigned long long seed;
31 std::cin >> self_player_id >> seed;
32 return {self_player_id, seed};
33}
34
41{
43 int count, type, arg0, arg1 = -1;
44 std::cin >> count;
45 ops.reserve(count);
46 for (int i = 0; i < count; i++)
47 {
48 std::cin >> type;
49 if (type == UpgradeGeneratedAnt || type == UpgradeGenerationSpeed)
50 {
51 ops.emplace_back(static_cast<OperationType>(type));
52 }
53 else if (type == DowngradeTower)
54 {
55 std::cin >> arg0;
56 ops.emplace_back(static_cast<OperationType>(type), arg0);
57 }
58 else
59 {
60 std::cin >> arg0 >> arg1;
61 ops.emplace_back(static_cast<OperationType>(type), arg0, arg1);
62 }
63 }
64 return ops;
65}
66
71{
72 int round;
73 std::vector<Tower> towers;
75 int coin0, coin1, hp0, hp1;
76};
77
83{
84 RoundInfo info;
85 // Round ID
86 std::cin >> info.round;
87 // Variables
88 int id, player, x, y, type, cd, hp, level, age, state;
89 // Tower
90 int tower_num;
91 std::cin >> tower_num;
92 info.towers.reserve(tower_num);
93 for (int i = 0; i < tower_num; ++i)
94 {
95 std::cin >> id >> player >> x >> y >> type >> cd;
96 info.towers.emplace_back(id, player, x, y, static_cast<TowerType>(type), cd);
97 }
98 // Ant
99 int ant_num;
100 std::cin >> ant_num;
101 info.ants.reserve(ant_num);
102 for (int i = 0; i < ant_num; ++i)
103 {
104 std::cin >> id >> player >> x >> y >> hp >> level >> age >> state;
105 info.ants.emplace_back(id, player, x, y, hp, level, age, static_cast<AntState>(state));
106 }
107 // Coin
108 std::cin >> info.coin0 >> info.coin1;
109 // Base hp
110 std::cin >> info.hp0 >> info.hp1;
111
112 return info;
113}
114
115/* Output helpers */
116
125{
126 std::size_t len = 0;
127 do {
128 ++len;
129 x /= 10;
130 } while (x);
131 return len;
132}
133
141{
142 return str.length();
143}
144
154{
155 std::size_t len = object_length(op.type);
156 if (op.arg0 != Operation::INVALID_ARG)
157 len += 1 + object_length(op.arg0);
158 if (op.arg1 != Operation::INVALID_ARG)
159 len += 1 + object_length(op.arg1);
160 len += 1; // Length of line break
161 return len;
162}
163
172{
173 std::size_t len = 0;
174 for (auto& op: ops)
175 len += object_length(op);
176 return len;
177}
178
185inline void convert_to_big_endian(const void* src, std::size_t size, void* dest)
186{
187 for (std::size_t i = 0; i < size; ++i)
188 static_cast<char*>(dest)[size - i - 1] = static_cast<const char*>(src)[i];
189}
190
191/* Output */
192
197inline void print_header(int size)
198{
199 // Convert into big-endian order
200 char buf[4] = {};
201 convert_to_big_endian(&size, sizeof(size), buf);
202 for (int i = 0; i < 4; ++i)
203 std::cout << buf[i];
204}
205
210inline void send_string(const std::string& str)
211{
213 std::cout << str;
214}
215
221{
222 // Get the total length, including the leading operation num
223 std::size_t op_len = object_length(ops);
224 std::size_t op_num_len = object_length(ops.size()) + 1;
225 int total_len = static_cast<int>(op_num_len + op_len);
226 // Print the header
227 print_header(total_len);
228 // Print the content
229 std::cout << ops.size() << std::endl;
230 for (auto &op : ops)
231 {
232 std::cout << op; // There has been a line break for each operation
233 }
234}
Models and constants.
TowerType
Tag for the type of a tower. The integer values of these enumeration items are also their indexes.
Definition: common.hpp:319
OperationType
Tag for the type of an operation. The integer values of these enumeration items are also their indexe...
Definition: common.hpp:723
@ UpgradeGenerationSpeed
Increase ant producing speed.
Definition: common.hpp:734
@ UpgradeGeneratedAnt
Increase HP of newly generated ants.
Definition: common.hpp:735
@ DowngradeTower
Downgrade/Destroy a tower.
Definition: common.hpp:727
AntState
State of an ant, indicating its life cycle stages.
Definition: common.hpp:218
T emplace_back(T... args)
T endl(T... args)
std::vector< Operation > read_opponent_operations()
Read your opponent's operations and deserialize them. The time to call this function depends on your ...
Definition: io.hpp:40
void convert_to_big_endian(const void *src, std::size_t size, void *dest)
Convert an object into big-endian representation.
Definition: io.hpp:185
std::size_t object_length(int x)
Calculate the length of the serialized result of a non-negative integer, without actually serializing...
Definition: io.hpp:124
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
void print_header(int size)
Print the header, i.e. the total size in big-endian binary representation.
Definition: io.hpp:197
void send_string(const std::string &str)
Send raw string with header to judger.
Definition: io.hpp:210
RoundInfo read_round_info()
Read information at the beginning of a round and deserialize.
Definition: io.hpp:82
T reserve(T... args)
T length(T... args)
Player's operations. It is able to be applied to the map.
Definition: common.hpp:742
static constexpr int INVALID_ARG
Placeholder for the second argument.
Definition: common.hpp:745
A combination of deserialized information about current round state received from judger.
Definition: io.hpp:71