-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
247 changed files
with
8,749 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Cargo.lock | ||
target/ | ||
.idea/ | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "aicup2019" | ||
version = "1.3.0" | ||
authors = ["kuviman <[email protected]>"] | ||
edition = "2018" | ||
default-run = "aicup2019" | ||
|
||
[features] | ||
default = ["rendering"] | ||
rendering = ["codegame/rendering"] | ||
|
||
[dependencies] | ||
codegame = { git = "https://github.com/codeforces/codegame", default-features = false } | ||
serde = "1" | ||
trans-gen = { git = "https://github.com/kuviman/trans-gen" } | ||
structopt = "0.3" | ||
|
||
[dev-dependencies] | ||
tempfile = "3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
To build, you'll need to install [Rust](https://rustup.rs/). | ||
|
||
To run native build: | ||
|
||
```shell | ||
cargo run --release | ||
``` | ||
|
||
To build web version, first install [`cargo-web`](https://github.com/koute/cargo-web): | ||
|
||
```shell | ||
cargo install cargo-web | ||
``` | ||
|
||
Then build, start local server and open browser: | ||
|
||
```shell | ||
cargo web start --release --open | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
project(aicup2019) | ||
|
||
# OS and compiler checks. | ||
if(WIN32) | ||
add_definitions(-DWIN32) | ||
SET(PROJECT_LIBS Ws2_32.lib) | ||
endif() | ||
|
||
file(GLOB HEADERS "*.hpp" "model/*.hpp" "csimplesocket/*.h") | ||
SET_SOURCE_FILES_PROPERTIES(${HEADERS} PROPERTIES HEADER_FILE_ONLY TRUE) | ||
file(GLOB SRC "*.cpp" "model/*.cpp" "csimplesocket/*.cpp") | ||
add_executable(aicup2019 ${HEADERS} ${SRC}) | ||
TARGET_LINK_LIBRARIES(aicup2019 ${PROJECT_LIBS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "Debug.hpp" | ||
#include "model/PlayerMessageGame.hpp" | ||
|
||
Debug::Debug(const std::shared_ptr<OutputStream> &outputStream) | ||
: outputStream(outputStream) {} | ||
|
||
void Debug::draw(const CustomData &customData) { | ||
outputStream->write(PlayerMessageGame::CustomDataMessage::TAG); | ||
customData.writeTo(*outputStream); | ||
outputStream->flush(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef _DEBUG_HPP_ | ||
#define _DEBUG_HPP_ | ||
|
||
#include "Stream.hpp" | ||
#include "model/CustomData.hpp" | ||
#include <memory> | ||
|
||
class Debug { | ||
public: | ||
Debug(const std::shared_ptr<OutputStream> &outputStream); | ||
void draw(const CustomData &customData); | ||
|
||
private: | ||
std::shared_ptr<OutputStream> outputStream; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM gcc | ||
|
||
RUN apt-get update && apt-get install -y cmake | ||
|
||
COPY . /project | ||
WORKDIR /project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "MyStrategy.hpp" | ||
|
||
MyStrategy::MyStrategy() {} | ||
|
||
double distanceSqr(Vec2Double a, Vec2Double b) { | ||
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); | ||
} | ||
|
||
UnitAction MyStrategy::getAction(const Unit &unit, const Game &game, | ||
Debug &debug) { | ||
const Unit *nearestEnemy = nullptr; | ||
for (const Unit &other : game.units) { | ||
if (other.playerId != unit.playerId) { | ||
if (nearestEnemy == nullptr || | ||
distanceSqr(unit.position, other.position) < | ||
distanceSqr(unit.position, nearestEnemy->position)) { | ||
nearestEnemy = &other; | ||
} | ||
} | ||
} | ||
const LootBox *nearestWeapon = nullptr; | ||
for (const LootBox &lootBox : game.lootBoxes) { | ||
if (std::dynamic_pointer_cast<Item::Weapon>(lootBox.item)) { | ||
if (nearestWeapon == nullptr || | ||
distanceSqr(unit.position, lootBox.position) < | ||
distanceSqr(unit.position, nearestWeapon->position)) { | ||
nearestWeapon = &lootBox; | ||
} | ||
} | ||
} | ||
Vec2Double targetPos = unit.position; | ||
if (unit.weapon == nullptr && nearestWeapon != nullptr) { | ||
targetPos = nearestWeapon->position; | ||
} else if (nearestEnemy != nullptr) { | ||
targetPos = nearestEnemy->position; | ||
} | ||
debug.draw(CustomData::Log( | ||
std::string("Target pos: ") + targetPos.toString())); | ||
Vec2Double aim = Vec2Double(0, 0); | ||
if (nearestEnemy != nullptr) { | ||
aim = Vec2Double(nearestEnemy->position.x - unit.position.x, | ||
nearestEnemy->position.y - unit.position.y); | ||
} | ||
bool jump = targetPos.y > unit.position.y; | ||
if (targetPos.x > unit.position.x && | ||
game.level.tiles[size_t(unit.position.x + 1)][size_t(unit.position.y)] == | ||
Tile::WALL) { | ||
jump = true; | ||
} | ||
if (targetPos.x < unit.position.x && | ||
game.level.tiles[size_t(unit.position.x - 1)][size_t(unit.position.y)] == | ||
Tile::WALL) { | ||
jump = true; | ||
} | ||
UnitAction action; | ||
action.velocity = targetPos.x - unit.position.x; | ||
action.jump = jump; | ||
action.jumpDown = !action.jump; | ||
action.aim = aim; | ||
action.shoot = true; | ||
action.reload = false; | ||
action.swapWeapon = false; | ||
action.plantMine = false; | ||
return action; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef _MY_STRATEGY_HPP_ | ||
#define _MY_STRATEGY_HPP_ | ||
|
||
#include "Debug.hpp" | ||
#include "model/CustomData.hpp" | ||
#include "model/Game.hpp" | ||
#include "model/Unit.hpp" | ||
#include "model/UnitAction.hpp" | ||
|
||
class MyStrategy { | ||
public: | ||
MyStrategy(); | ||
UnitAction getAction(const Unit &unit, const Game &game, Debug &debug); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include "TcpStream.hpp" | ||
#include <cstring> | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#ifdef _WIN32 | ||
typedef int RECV_SEND_T; | ||
#else | ||
typedef ssize_t RECV_SEND_T; | ||
#endif | ||
|
||
TcpStream::TcpStream(const std::string &host, int port) { | ||
#ifdef _WIN32 | ||
WSADATA wsa_data; | ||
if (WSAStartup(MAKEWORD(1, 1), &wsa_data) != 0) { | ||
throw std::runtime_error("Failed to initialize sockets"); | ||
} | ||
#endif | ||
sock = socket(AF_INET, SOCK_STREAM, 0); | ||
if (sock == -1) { | ||
throw std::runtime_error("Failed to create socket"); | ||
} | ||
int yes = 1; | ||
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&yes, sizeof(int)) < | ||
0) { | ||
throw std::runtime_error("Failed to set TCP_NODELAY"); | ||
} | ||
addrinfo hints, *servinfo; | ||
std::memset(&hints, 0, sizeof(hints)); | ||
hints.ai_family = AF_INET; | ||
hints.ai_socktype = SOCK_STREAM; | ||
if (getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, | ||
&servinfo) != 0) { | ||
throw std::runtime_error("Failed to get addr info"); | ||
} | ||
if (connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { | ||
throw std::runtime_error("Failed to connect"); | ||
} | ||
freeaddrinfo(servinfo); | ||
} | ||
|
||
class TcpInputStream : public InputStream { | ||
public: | ||
TcpInputStream(std::shared_ptr<TcpStream> tcpStream) | ||
: tcpStream(tcpStream), bufferPos(0), bufferSize(0) {} | ||
void readBytes(char *buffer, size_t byteCount) { | ||
while (byteCount > 0) { | ||
if (bufferSize > 0) { | ||
if (bufferSize >= byteCount) { | ||
memcpy(buffer, this->buffer + bufferPos, byteCount); | ||
bufferPos += byteCount; | ||
bufferSize -= byteCount; | ||
return; | ||
} | ||
memcpy(buffer, this->buffer + bufferPos, bufferSize); | ||
buffer += bufferSize; | ||
byteCount -= bufferSize; | ||
bufferPos += bufferSize; | ||
bufferSize = 0; | ||
} | ||
if (bufferPos == BUFFER_CAPACITY) { | ||
bufferPos = 0; | ||
} | ||
RECV_SEND_T received = | ||
recv(tcpStream->sock, this->buffer + bufferPos + bufferSize, | ||
BUFFER_CAPACITY - bufferPos - bufferSize, 0); | ||
if (received < 0) { | ||
throw std::runtime_error("Failed to read from socket"); | ||
} | ||
bufferSize += received; | ||
} | ||
} | ||
|
||
private: | ||
static const size_t BUFFER_CAPACITY = 8 * 1024; | ||
char buffer[BUFFER_CAPACITY]; | ||
size_t bufferPos; | ||
size_t bufferSize; | ||
std::shared_ptr<TcpStream> tcpStream; | ||
}; | ||
|
||
class TcpOutputStream : public OutputStream { | ||
public: | ||
TcpOutputStream(std::shared_ptr<TcpStream> tcpStream) | ||
: tcpStream(tcpStream), bufferPos(0), bufferSize(0) {} | ||
void writeBytes(const char *buffer, size_t byteCount) { | ||
while (byteCount > 0) { | ||
size_t capacity = BUFFER_CAPACITY - bufferPos - bufferSize; | ||
if (capacity >= byteCount) { | ||
memcpy(this->buffer + bufferPos + bufferSize, buffer, byteCount); | ||
bufferSize += byteCount; | ||
return; | ||
} | ||
memcpy(this->buffer + bufferPos + bufferSize, buffer, capacity); | ||
bufferSize += capacity; | ||
byteCount -= capacity; | ||
buffer += capacity; | ||
flush(); | ||
} | ||
} | ||
void flush() { | ||
while (bufferSize > 0) { | ||
RECV_SEND_T sent = | ||
send(tcpStream->sock, buffer + bufferPos, bufferSize, 0); | ||
if (sent < 0) { | ||
throw std::runtime_error("Failed to write to socket"); | ||
} | ||
bufferPos += sent; | ||
bufferSize -= sent; | ||
} | ||
bufferPos = 0; | ||
} | ||
|
||
private: | ||
static const size_t BUFFER_CAPACITY = 8 * 1024; | ||
char buffer[BUFFER_CAPACITY]; | ||
size_t bufferPos; | ||
size_t bufferSize; | ||
std::shared_ptr<TcpStream> tcpStream; | ||
}; | ||
|
||
std::shared_ptr<InputStream> | ||
getInputStream(std::shared_ptr<TcpStream> tcpStream) { | ||
return std::shared_ptr<TcpInputStream>(new TcpInputStream(tcpStream)); | ||
} | ||
|
||
std::shared_ptr<OutputStream> | ||
getOutputStream(std::shared_ptr<TcpStream> tcpStream) { | ||
return std::shared_ptr<TcpOutputStream>(new TcpOutputStream(tcpStream)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef _TCP_STREAM_HPP_ | ||
#define _TCP_STREAM_HPP_ | ||
|
||
#ifdef _WIN32 | ||
#ifndef _WIN32_WINNT | ||
#define _WIN32_WINNT 0x0501 | ||
#endif | ||
#include <Ws2tcpip.h> | ||
#include <winsock2.h> | ||
#else | ||
#include <arpa/inet.h> | ||
#include <netdb.h> | ||
#include <netinet/tcp.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
typedef int SOCKET; | ||
#endif | ||
|
||
#include "Stream.hpp" | ||
#include <memory> | ||
#include <string> | ||
|
||
class TcpStream { | ||
public: | ||
TcpStream(const std::string &host, int port); | ||
SOCKET sock; | ||
}; | ||
|
||
std::shared_ptr<InputStream> | ||
getInputStream(std::shared_ptr<TcpStream> tcpStream); | ||
|
||
std::shared_ptr<OutputStream> | ||
getOutputStream(std::shared_ptr<TcpStream> tcpStream); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
set -ex | ||
|
||
if [ "$1" != "base" ]; then | ||
if [[ `ls -1 /src/ | wc -l` -eq 1 ]]; then | ||
cp -f /src/MyStrategy.cpp MyStrategy.cpp | ||
else | ||
rm -rf ./* | ||
cp -rf /src/* ./ | ||
fi | ||
fi | ||
|
||
cmake -DCMAKE_CXX_STANDARD=17 -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_VERBOSE_MAKEFILE=ON . | ||
cmake --build . --config Release | ||
cp aicup2019 /output/ |
Oops, something went wrong.