Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Set up initial AI structure
Browse files Browse the repository at this point in the history
Contains a trivial move instruction for now, will make that stuff better Soon:tm:
  • Loading branch information
LunarWatcher committed Oct 15, 2023
1 parent 71ca510 commit 81b46d4
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 96 deletions.
82 changes: 0 additions & 82 deletions .clang-format

This file was deleted.

1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set (SOURCE_FILES
genesis/core/game/chunks/MapLayers.cpp
# AI {{{
genesis/core/game/ai/CreatureController.cpp
genesis/core/game/ai/AIEngine.cpp
# }}}
)

Expand Down
3 changes: 1 addition & 2 deletions src/core/genesis/core/game/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace genesis {
World::World(int width, int height) : generator(std::make_shared<perlin::DumbGenerator>()), layers(MapLayers(width, height)) {
World::INSTANCE = this;

registerKey(GLFW_KEY_W, [](const KeyPressInfo& data) {
if (data.action != GLFW_PRESS) return false;
Expand Down Expand Up @@ -57,7 +56,7 @@ World::World(int width, int height) : generator(std::make_shared<perlin::DumbGen
}

void World::tick() {
this->creatures.tick();
this->creatures.tick(*this);
}

void World::render() {
Expand Down
2 changes: 0 additions & 2 deletions src/core/genesis/core/game/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Chunk;

class World : public Scene {
private:
static inline World* INSTANCE = nullptr;

std::shared_ptr<perlin::NoiseGenerator> generator;
//std::shared_ptr<Framebuffer> frame;

Expand Down
26 changes: 26 additions & 0 deletions src/core/genesis/core/game/ai/AIEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "AIEngine.hpp"

#include "CreatureController.hpp"
#include "genesis/Context.hpp"
#include "genesis/core/game/World.hpp"

namespace genesis {

void AIEngine::simulateColony(CreatureController& cc, World& world) {
auto entities = cc.activeColonyEntities;

for (auto& [groupId, group] : entities) {
if (groupId == 0) {
for (auto& entity : group.getEntities()) {
entity->getPosition().x += 1;
}
}
}

}

void AIEngine::tick(CreatureController& cc, World& world) {
simulateColony(cc, world);
}

}
14 changes: 14 additions & 0 deletions src/core/genesis/core/game/ai/AIEngine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace genesis {

class World;
class CreatureController;
class AIEngine {
private:
void simulateColony(CreatureController& cc, World& world);
public:
void tick(CreatureController& cc, World& world);
};

}
6 changes: 3 additions & 3 deletions src/core/genesis/core/game/ai/AIStructs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ enum class CivRelations {
ALLY,
};

using groupid_t = int;
static inline constexpr auto GROUP_COLONISTS = 0;
using groupid_t = long long;
static inline constexpr groupid_t GROUP_COLONISTS = 0;
// Groups 0-99 are reserved for static groups
static inline constexpr auto GROUP_RANDOM_STARTIDX = 100;
static inline constexpr groupid_t GROUP_RANDOM_STARTIDX = 100;

using RelationMap = std::map<std::pair<groupid_t, groupid_t>, CreatureRelations>;

Expand Down
8 changes: 5 additions & 3 deletions src/core/genesis/core/game/ai/CreatureController.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CreatureController.hpp"
#include "genesis/core/game/World.hpp"
#include "genesis/core/game/ai/CreatureController.hpp"

namespace genesis {
Expand Down Expand Up @@ -33,13 +34,14 @@ groupid_t CreatureController::provisionGroupId() {
}

void CreatureController::render() {
for (auto& [_, creatureGroup] : groups) {
for (auto& [_, creatureGroup] : activeColonyEntities) {
creatureGroup.render();
}
}

void CreatureController::tick() {
for (auto& [_, creatureGroup] : groups) {
void CreatureController::tick(World& world) {
aiEngine.tick(*this, world);
for (auto& [_, creatureGroup] : activeColonyEntities) {
creatureGroup.tick();
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/core/genesis/core/game/ai/CreatureController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include <vector>
#include <atomic>

#include "genesis/core/game/ai/AIEngine.hpp"
#include "genesis/core/game/ai/AIStructs.hpp"
#include "genesis/core/game/entities/GameCreature.hpp"
#include "genesis/rendering/Entity.hpp"

namespace genesis {

class World;
class CreatureGroup {
private:
// TODO: add entity IDs and convert to a map
Expand All @@ -26,6 +28,7 @@ class CreatureGroup {
void tick();

void pushEntity(std::shared_ptr<GameCreature> creature);
auto& getEntities() { return entities; }

};

Expand All @@ -44,13 +47,19 @@ class CreatureController {
RelationMap creatureRelations;

groupid_t provisionGroupId();
AIEngine aiEngine;
public:
std::map<groupid_t, CreatureGroup> groups;
/**
* Represents colony entities. These are part of the actively rendered colony, and
* are therefore use the OpenGL-connected entities.
*/
std::map<groupid_t, CreatureGroup> activeColonyEntities;

CreatureController();

void render();
void tick();
void tick(World& world);

};

}
4 changes: 2 additions & 2 deletions src/core/genesis/core/game/generation/WorldGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::shared_ptr<World> WorldGenerator::newWorld(int width, int height, const std
}

void WorldGenerator::generateCharacters(const glm::vec3& caravanPosition, std::shared_ptr<World> world, int count) {
world->creatures.groups.insert({GROUP_COLONISTS, CreatureGroup {"Colonists", GROUP_COLONISTS}});
world->creatures.activeColonyEntities.insert({GROUP_COLONISTS, CreatureGroup {"Colonists", GROUP_COLONISTS}});
auto pos = caravanPosition;
// TODO: this is why I need a proper layer structure. This fucking sucks ass
// Future me here: there is now a layer system, but it doesn't fully account for entities.
Expand All @@ -68,7 +68,7 @@ void WorldGenerator::generateCharacters(const glm::vec3& caravanPosition, std::s
Context::getInstance().dataLoader.creatures.at("canine")
);
entity->setPosition(pos);
world->creatures.groups.at(GROUP_COLONISTS).pushEntity(entity);
world->creatures.activeColonyEntities.at(GROUP_COLONISTS).pushEntity(entity);
}
}

Expand Down

0 comments on commit 81b46d4

Please sign in to comment.