Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 47 additions & 107 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,144 +3,84 @@ name: CMake
on: [push, pull_request]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
build:
# The CMake configure and build commands are platform agnostic and should work equally
# well on Windows or Mac. You can convert this to a matrix build if you need
# cross-platform coverage.
# See: https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#configuring-a-build-matrix
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Create Build Environment
# Some projects don't allow in-source building, so create a separate build directory
# We'll use this as our working directory for all subsequent commands
run: cmake -E make_directory ${{runner.workspace}}/build

- name: Install Dependencies on Ubuntu
- name: Install Dependencies (Linux)
if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install libsdl2-*

- name: Install Dependencies on macOS
if: runner.os == 'macOS'
run: brew install sdl2 sdl2_mixer sdl2_net sdl2_image sdl2_ttf

- name: Configure CMake
# Use a bash shell so we can use the same syntax for environment variable
# access regardless of the host operating system
shell: bash
working-directory: ${{runner.workspace}}/build
# Note the current convention is to use the -S and -B options here to specify source
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
run: |
sudo apt-get update
sudo apt-get install -y libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev xvfb

- name: Build
working-directory: ${{runner.workspace}}/build
shell: bash
# Execute the build. You can specify a specific target with "--target <NAME>"
run: cmake --build . --config $BUILD_TYPE
- name: Install Dependencies (macOS)
if: runner.os == 'macOS'
run: brew install sdl2 sdl2_image sdl2_mixer sdl2_net sdl2_ttf

- name: Copy Libraries (Linux)
if: runner.os == 'Linux'
working-directory: ${{runner.workspace}}/build
- name: Build and Run
shell: bash
# Execute the build. You can specify a specific target with "--target <NAME>"
run: |
wget https://github.com/Rapiz1/DungeonRush/releases/download/v1.0-alpha/linux-lib.tar.gz
tar xzf linux-lib.tar.gz
mv linux-lib/* bin

- uses: actions/upload-artifact@v2
mkdir -p build && rm -rf build/*
cmake -S . -B build -DCMAKE_CXX_FLAGS="-DSOFTWARE_ACC"
cmake --build build
if [ "$RUNNER_OS" == "Linux" ]; then
xvfb-run ./build/bin/dungeon_rush &
else
./build/bin/dungeon_rush &
fi
# Give it some time to run and verify it doesn't crash immediately
sleep 5
kill $! || true

- uses: actions/upload-artifact@v4
with:
name: DungeonRush-${{runner.os}}
path: ${{runner.workspace}}/build/bin
path: build/bin

build-windows:
runs-on: ${{ matrix.os }}
runs-on: windows-latest
strategy:
matrix:
os: [windows-latest]
arch: [i686, x86_64]
defaults:
run:
shell: msys2 {0}
steps:
- uses: msys2/setup-msys2@v2
if: matrix.arch == 'i686'
with:
msystem: MINGW32
update: true
install: base-devel git gcc mingw-w64-${{matrix.arch}}-cmake mingw-w64-${{matrix.arch}}-gcc

- uses: msys2/setup-msys2@v2
if: matrix.arch == 'x86_64'
with:
msystem: MINGW64
msystem: ${{ (matrix.arch == 'i686') && 'MINGW32' || 'MINGW64' }}
update: true
install: base-devel git gcc mingw-w64-${{matrix.arch}}-cmake mingw-w64-${{matrix.arch}}-gcc

- uses: actions/checkout@v2

- name: Install dependencies
run: |
wget https://www.libsdl.org/release/SDL2-devel-2.0.12-mingw.tar.gz
tar xzf SDL2-devel-2.0.12-mingw.tar.gz && cd SDL2-2.0.12
make install-package arch=${{matrix.arch}}-w64-mingw32 prefix=/usr && cd ..
# SDL2_mixer
wget https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-devel-2.0.4-mingw.tar.gz
tar xzf SDL2_mixer-devel-2.0.4-mingw.tar.gz && cd SDL2_mixer-2.0.4
make install-package arch=${{matrix.arch}}-w64-mingw32 prefix=/usr && cd ..
# SDL2_image
wget https://www.libsdl.org/projects/SDL_image/release/SDL2_image-devel-2.0.5-mingw.tar.gz
tar xzf SDL2_image-devel-2.0.5-mingw.tar.gz && cd SDL2_image-2.0.5
make install-package arch=${{matrix.arch}}-w64-mingw32 prefix=/usr && cd ..
# SDL2_ttf
wget https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-devel-2.0.15-mingw.tar.gz
tar xzf SDL2_ttf-devel-2.0.15-mingw.tar.gz && cd SDL2_ttf-2.0.15
make install-package arch=${{matrix.arch}}-w64-mingw32 prefix=/usr && cd ..
# SDL2_net
wget https://www.libsdl.org/projects/SDL_net/release/SDL2_net-devel-2.0.1-mingw.tar.gz
tar xzf SDL2_net-devel-2.0.1-mingw.tar.gz && cd SDL2_net-2.0.1
make install-package arch=${{matrix.arch}}-w64-mingw32 prefix=/usr && cd ..

- name: Create Build Environment
working-directory: ${{github.workspace}}
# Some projects don't allow in-source building, so create a separate build directory
# We'll use this as our working directory for all subsequent commands
run: cmake -E make_directory build

- name: Configure CMake
# Use a bash shell so we can use the same syntax for environment variable
# access regardless of the host operating system
working-directory: ${{github.workspace}}
# Note the current convention is to use the -S and -B options here to specify source
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: cd build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=$BUILD_TYPE
install: >-
base-devel
git
mingw-w64-${{matrix.arch}}-cmake
mingw-w64-${{matrix.arch}}-gcc
mingw-w64-${{matrix.arch}}-SDL2
mingw-w64-${{matrix.arch}}-SDL2_image
mingw-w64-${{matrix.arch}}-SDL2_mixer
mingw-w64-${{matrix.arch}}-SDL2_net
mingw-w64-${{matrix.arch}}-SDL2_ttf

- name: Build
working-directory: ${{github.workspace}}
# Execute the build. You can specify a specific target with "--target <NAME>"
run: cd build && cmake --build . --config $BUILD_TYPE
- uses: actions/checkout@v4

- name: Copy libraries
working-directory: ${{github.workspace}}
- name: Build and Run
run: |
cd build
wget https://github.com/Rapiz1/DungeonRush/releases/download/v1.0-alpha/win-${{matrix.arch}}-lib.tar.gz
tar xzf win-${{matrix.arch}}-lib.tar.gz
mv win-${{matrix.arch}}-lib/* bin
mkdir -p build && rm -rf build/*
cmake -S . -B build -G"MSYS Makefiles" -DCMAKE_CXX_FLAGS="-DSOFTWARE_ACC"
cmake --build build
./build/bin/dungeon_rush &
sleep 5
kill $! || true

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: DungeonRush-${{runner.os}}-${{matrix.arch}}
path: ${{github.workspace}}\build\bin
name: DungeonRush-Windows-${{matrix.arch}}
path: build/bin
38 changes: 26 additions & 12 deletions src/ai.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ai.h"

#include "game.h"
#include "helper.h"
#include "map.h"
#include "res.h"
Expand All @@ -13,13 +14,10 @@ extern std::array<std::array<Item, MAP_SIZE>, MAP_SIZE> itemMap;
extern bool hasMap[MAP_SIZE][MAP_SIZE];
extern std::array<std::array<bool, MAP_SIZE>, MAP_SIZE> hasEnemy;
extern int spikeDamage;
extern int playersCount;
extern const int n, m;
extern const int SCALE_FACTOR;

// Sprite
extern std::shared_ptr<Snake> spriteSnake[SPRITES_MAX_NUM];
extern int spritesCount;
// Use GameContext for entity access
double AI_LOCK_LIMIT;

int trapVerdict(const std::shared_ptr<Sprite>& sprite) {
Expand Down Expand Up @@ -47,14 +45,18 @@ int trapVerdict(const std::shared_ptr<Sprite>& sprite) {
}

int getPowerfulPlayer() {
GameContext& ctx = getGameContext();
int maxNum = 0;
int mxCount = 0;
int id = -1;
for (int i = 0; i < playersCount; i++) {
if (!spriteSnake[i]) {
const int playerCount = ctx.entityManager.playerCount();
const int snakeCount = ctx.entityManager.snakeCount();
for (int i = 0; i < playerCount; i++) {
const auto& snake = ctx.entityManager.getSnake(i);
if (!snake) {
continue;
}
const int num = spriteSnake[i]->num();
const int num = snake->num();
if (num > maxNum) {
maxNum = num;
mxCount = 1;
Expand All @@ -63,17 +65,20 @@ int getPowerfulPlayer() {
mxCount++;
}
}
if (id == -1 || mxCount != 1 || !spriteSnake[id]) {
if (id == -1 || mxCount != 1) {
return -1;
}
return spriteSnake[id]->num() >= AI_LOCK_LIMIT ? id : -1;
const auto& snake = ctx.entityManager.getSnake(id);
return (snake && snake->num() >= AI_LOCK_LIMIT) ? id : -1;
}

int balanceVerdict(const std::shared_ptr<Sprite>& sprite, int id) {
if (id == -1 || !spriteSnake[id] || spriteSnake[id]->sprites().empty()) {
GameContext& ctx = getGameContext();
const auto& playerSnake = ctx.entityManager.getSnake(id);
if (id == -1 || !playerSnake || playerSnake->sprites().empty()) {
return 0;
}
const auto player = spriteSnake[id]->sprites().front();
const auto player = playerSnake->sprites().front();
if (!player) {
return 0;
}
Expand Down Expand Up @@ -127,7 +132,8 @@ int compareChoiceByValue(const void* x, const void* y) {
return b->value - a->value;
}

void AiInput(const std::shared_ptr<Snake>& snake) {
void DefaultAIBehavior::updateInput(
const std::shared_ptr<Snake>& snake) const {
if (!snake || snake->sprites().empty()) {
return;
}
Expand Down Expand Up @@ -175,3 +181,11 @@ void AiInput(const std::shared_ptr<Snake>& snake) {
}
}
}

void AiInput(const std::shared_ptr<Snake>& snake) {
GameContext& ctx = getGameContext();
if (!ctx.aiBehavior) {
ctx.aiBehavior = std::make_shared<DefaultAIBehavior>();
}
ctx.aiBehavior->updateInput(snake);
}
11 changes: 11 additions & 0 deletions src/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ struct Choice {
Direction direction = Direction::Right;
};

class AIBehavior {
public:
virtual ~AIBehavior() = default;
virtual void updateInput(const std::shared_ptr<Snake>& snake) const = 0;
};

class DefaultAIBehavior final : public AIBehavior {
public:
void updateInput(const std::shared_ptr<Snake>& snake) const override;
};

void AiInput(const std::shared_ptr<Snake>& snake);
int getPowerfulPlayer();

Expand Down
Loading
Loading