Skip to content

Commit

Permalink
Updated all other files with new core functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mercere99 committed Oct 25, 2023
1 parent 7bb48ad commit fcc65a1
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 114 deletions.
14 changes: 1 addition & 13 deletions source/Agents/AStarAgent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,13 @@ class AStarAgent : public cse491::AgentBase {
cse491::GridPosition goal_position; ///< Where the agent wants to end up
int recalculate_after_x_turns = 100; ///< How often agent recalculates moves
int current_move_num = 0; ///< What move # we are currently on
const cse491::WorldBase *world = nullptr; ///< What world this agent is a part of

public:
AStarAgent(size_t id, const std::string &name) : AgentBase(id, name) {
}

~AStarAgent() = default;

/**
* @brief Set the word object
*
* @param world world this agent is a part of
*/
void SetWorld(const cse491::WorldBase *const _world) { this->world = _world; }

/// @brief This agent needs a specific set of actions to function.
/// @return Success.
bool Initialize() override {
Expand Down Expand Up @@ -68,18 +60,14 @@ class AStarAgent : public cse491::AgentBase {
* @brief Update the path to go to goal position
*/
void RecalculatePath() {
path = GetShortestPath(GetPosition(), goal_position, world, this);
path = GetShortestPath(GetPosition(), goal_position, GetWorld(), *this);
current_move_num = 0;
}
/// Choose the action to take a step in the appropriate direction.
size_t SelectAction(const cse491::WorldGrid & /*grid*/,
const cse491::type_options_t & /* type_options*/,
const cse491::item_set_t & /* item_set*/,
const cse491::agent_set_t & /* agent_set*/) override {
if (world == nullptr) {
// Do nothing if the agent doesn't know about its world
return 0;
}
// We are taking an action so another turn has passed
++current_move_num;
// If the last step failed, or we need a new path the then regenerate the path
Expand Down
12 changes: 6 additions & 6 deletions source/Agents/AgentLibary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ struct CompareNodes {
/// exist
std::vector<cse491::GridPosition> GetShortestPath(const cse491::GridPosition &start,
const cse491::GridPosition &end,
const cse491::WorldBase *world,
const cse491::AgentBase *agent) {
const cse491::WorldBase &world,
const cse491::AgentBase &agent) {
// Generated with the help of chat.openai.com
const size_t rows = world->GetGrid().GetWidth();
const size_t cols = world->GetGrid().GetHeight();
const size_t rows = world.GetGrid().GetWidth();
const size_t cols = world.GetGrid().GetHeight();
std::vector<cse491::GridPosition> path;
// If the start or end is not valid then return empty list
if (!(world->GetGrid().IsValid(start) && world->GetGrid().IsValid(end)))
if (!(world.GetGrid().IsValid(start) && world.GetGrid().IsValid(end)))
return path;

// Define possible movements (up, down, left, right)
Expand Down Expand Up @@ -104,7 +104,7 @@ std::vector<cse491::GridPosition> GetShortestPath(const cse491::GridPosition &st
for (int i = 0; i < 4; ++i) {
cse491::GridPosition newPos(current->position.GetX() + dx[i], current->position.GetY() + dy[i]);
// Check if the neighbor is within bounds and is a valid move
if (world->GetGrid().IsValid(newPos) && world->IsTraversable(agent, newPos)) {
if (world.GetGrid().IsValid(newPos) && world.IsTraversable(agent, newPos)) {
double newG = current->g + 1; // Assuming a cost of 1 to move to a neighbor
double newH = std::abs(newPos.GetX() - endNode->position.GetX()) +
std::abs(newPos.GetY() - endNode->position.GetY()); // Manhattan distance
Expand Down
2 changes: 1 addition & 1 deletion source/DataCollection/AgentReciever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace DataCollection {
* @param pos The grid position to be stored.
* @param last_action The last action ID associated with the agent.
*/
void StoreData(std::string name, cse491::GridPosition pos, int last_action) {
void StoreData(std::string name, cse491::GridPosition pos, [[maybe_unused]] int last_action) {
std::shared_ptr<AgentData> agent = GetAgent(name);
agent->StorePositions(pos);
// agent->StoreAction(last_action);
Expand Down
2 changes: 1 addition & 1 deletion source/Interfaces/NetWorth/NetworkInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace netWorth{

public:
NetworkingInterface() = default;
~NetworkingInterface() = default;
virtual ~NetworkingInterface() = default;

/**
* Receives a socket that has been connected between client and server
Expand Down
2 changes: 1 addition & 1 deletion source/Interfaces/NetWorth/server/ServerInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace netWorth{

public:
ServerInterface() = default;
~ServerInterface() = default;
virtual ~ServerInterface() = default;

/**
* The initial connection for the server to a client
Expand Down
2 changes: 1 addition & 1 deletion source/Worlds/BiomeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <set>
#include <tuple>
#include <random>
#include "BiomeGenerator.h"
#include "BiomeGenerator.hpp"

using std::vector;

Expand Down
4 changes: 2 additions & 2 deletions source/Worlds/BiomeGenerator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file BiomeGenerator.h
* @file BiomeGenerator.hpp
* @author Paul Schulte, Milan Mihailovic, ChatGPT
*
* Uses perlin noise to create dirt and grass on a grid
Expand Down Expand Up @@ -58,7 +58,7 @@ struct Point {
class BiomeGenerator {
private:
const double frequency = 8.0; ///< [0.1, 64.0]
const int octaves = 8; ///< [1, 16]
// const int octaves = 8; ///< [1, 16]

PerlinNoise perlinNoise; ///< The Perlin Noise procedural generation algorithm

Expand Down
20 changes: 4 additions & 16 deletions source/Worlds/GenerativeWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#pragma once

#include <cassert>
#include <random>

#include "../core/WorldBase.hpp"

Expand All @@ -28,10 +27,6 @@ class GenerativeWorld : public WorldBase {
size_t grass_id;
size_t dirt_id;

unsigned int seed; ///< Seed used for generator
std::mt19937 randomGenerator;


/// Provide the agent with movement actions.
void ConfigAgent(AgentBase & agent) override {
agent.AddAction("up", MOVE_UP);
Expand All @@ -43,11 +38,7 @@ class GenerativeWorld : public WorldBase {
}

public:
/**
* Initializes world with cell types and random generator
* @param seed Seed used for RNG. Default is current time
*/
explicit GenerativeWorld(unsigned int seed = time(nullptr)) : seed(seed) {
explicit GenerativeWorld(unsigned int seed = time(nullptr)) : WorldBase(seed) {
floor_id = AddCellType("floor", "Floor that you can easily walk over.", ' ');
wall_id = AddCellType("wall", "Impenetrable wall that you must find a way around.", '#');

Expand All @@ -61,8 +52,6 @@ class GenerativeWorld : public WorldBase {
dirt_id = AddCellType("dirt", "Dirt you can walk on.", '~');

main_grid.Read("../assets/grids/generated_maze.grid", type_options);

randomGenerator.seed(seed);
}
~GenerativeWorld() = default;

Expand All @@ -85,11 +74,10 @@ class GenerativeWorld : public WorldBase {

// Check if the agent is trying to move onto a spike.
if (main_grid.At(new_position) == spike_id) {
std::cout << "Game over, try again!" << std::endl;
exit(0); // Halting the program

std::cout << "Game over, try again!" << std::endl;
exit(0); // Halting the program

return false;
return false;
}

// agent is moving onto a key tile and picking it up
Expand Down
2 changes: 1 addition & 1 deletion source/Worlds/MazeWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MazeWorld : public WorldBase {
}

/// Can walk on all tiles except for walls
bool IsTraversable(const AgentBase * /*agent*/, cse491::GridPosition pos) const override {
bool IsTraversable(const AgentBase & /*agent*/, cse491::GridPosition pos) const override {
return main_grid.At(pos) != wall_id;
}
};
Expand Down
43 changes: 3 additions & 40 deletions source/Worlds/SecondWorld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ namespace group4 {
if (main_grid.At(new_position) == entity_id) {

auto item_found = std::find_if(item_set.begin(), item_set.end(), [&new_position]
(const std::unique_ptr<cse491::Entity>& item) {
return item->GetPosition() == new_position;
(const std::unique_ptr<cse491::ItemBase> & item) {
return item && item->GetPosition() == new_position;
});

std::cout << "You found " << (*item_found)->GetName() << "!" << std::endl;

// Move the ownership of the item to the agents inventory
inventory.push_back(std::move(*(item_found)));

CleanEntities();

// Change the grid position to floor_id so it's not seen on the grid
main_grid.At(new_position) = floor_id;

Expand All @@ -102,49 +100,14 @@ namespace group4 {
return true;
}

/**
* Adds an entity to the non-agent entity list
* @param entity The entity we are adding
*/
size_t AddEntity(std::unique_ptr<cse491::Entity> &entity) {
// Sets the '+' as the item in grid
main_grid.At(entity->GetPosition()) = entity_id;

// Moves the ownership of the entity into item_set
size_t id = entity->GetID();
item_set.push_back(std::move(entity));
return id;
}

/**
* Cleans itemset of all null entities
*/
void CleanEntities() {
std::erase_if(item_set, [] (const std::unique_ptr<cse491::Entity> &entityPtr) {
return entityPtr == nullptr;
});

}

/**
* Removes an Entity from itemset (testing)
* @param removeID The ID of the entity we're removing
*/
void RemoveEntity(size_t removeID) {
std::erase_if(item_set, [removeID] (const std::unique_ptr<cse491::Entity> &entityPtr) {
return entityPtr->GetID() == removeID;
});

CleanEntities();
}


/**
* Prints the entities in item_set (testing)
*/
void PrintEntities() {
for (const auto &elem : item_set)
{
if (!elem) continue;
std::cout << elem->GetName() << "\n";
}
std::cout << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions source/core/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace cse491 {
/// @brief Available CellTypes will be passed around as a vector of options.
using type_options_t = std::vector<CellType>;

class Entity;
class ItemBase;
/// @brief Sets of items will be represented as vectors of pointers to the base class
using item_set_t = std::vector<std::unique_ptr<Entity>>;
using item_set_t = std::vector<std::unique_ptr<ItemBase>>;

class AgentBase;
/// @brief Sets of agents will be represented as vectors of pointers to the base class
Expand Down
1 change: 1 addition & 0 deletions source/core/InterfaceBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>

#include "AgentBase.hpp"
#include "ItemBase.hpp"

namespace cse491 {

Expand Down
1 change: 0 additions & 1 deletion source/group_1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ int main() {
world.AddAgent<cse491::PacingAgent>("Pacer 1").SetPosition(3, 1);
auto & astar_agent =
static_cast<walle::AStarAgent&>(world.AddAgent<walle::AStarAgent>("AStar 1"));
astar_agent.SetWorld(&world);
astar_agent.SetPosition(7, 3);
astar_agent.SetGoalPosition(7, 8);
astar_agent.RecalculatePath();
Expand Down
12 changes: 6 additions & 6 deletions source/group_4_interface_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ int main()
world.AddAgent<i_2D::MainInterface>("Interface").SetProperty<char>("symbol", '@');

// Adding power sword with id = 1; name = sword of power
auto powerSword = std::make_unique<cse491::Entity>(1, "Sword of Power");
auto powerSword = std::make_unique<cse491::ItemBase>(1, "Sword of Power");
powerSword->SetProperty("Damage", 20.0);
powerSword->SetPosition(1, 2);
world.AddEntity(powerSword);
world.AddItem(std::move(powerSword));

// Adding fire sword with id = 2; name = Inferno Slicer
auto infernoSlicer = std::make_unique<cse491::Entity>(2, "Inferno Slicer");
auto infernoSlicer = std::make_unique<cse491::ItemBase>(2, "Inferno Slicer");
infernoSlicer->SetProperties("Damage", 12.5, "Speed", 15.0, "Burning Duration", 2.5);
infernoSlicer->SetPosition(3, 4);
world.AddEntity(infernoSlicer);
world.AddItem(std::move(infernoSlicer));

// Adding a piece of armor with id = 10; name = Daedric
auto daedricArmor = std::make_unique<cse491::Entity>(10, "Daedric Armor");
auto daedricArmor = std::make_unique<cse491::ItemBase>(10, "Daedric Armor");
daedricArmor->SetProperties("Health", 99, "Extra Inv. Space", 5);
daedricArmor->SetPosition(5, 0);
world.AddEntity(daedricArmor);
world.AddItem(std::move(daedricArmor));

world.Run();
}
12 changes: 6 additions & 6 deletions source/group_4_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ int main()
world.AddAgent<cse491::TrashInterface>("Interface").SetProperty("symbol", '@');

// Adding power sword with id = 1; name = sword of power
auto powerSword = std::make_unique<cse491::Entity>(1, "Sword of Power");
auto powerSword = std::make_unique<cse491::ItemBase>(1, "Sword of Power");
powerSword->SetProperty("Damage", 20.0);
powerSword->SetPosition(1, 2);
world.AddEntity(powerSword);
world.AddItem(std::move(powerSword));

// Adding fire sword with id = 2; name = Inferno Slicer
auto infernoSlicer = std::make_unique<cse491::Entity>(2, "Inferno Slicer");
auto infernoSlicer = std::make_unique<cse491::ItemBase>(2, "Inferno Slicer");
infernoSlicer->SetProperties("Damage", 12.5, "Speed", 15.0, "Burning Duration", 2.5);
infernoSlicer->SetPosition(3, 4);
world.AddEntity(infernoSlicer);
world.AddItem(std::move(infernoSlicer));

// Adding a piece of armor with id = 10; name = Daedric
auto daedricArmor = std::make_unique<cse491::Entity>(10, "Daedric Armor");
auto daedricArmor = std::make_unique<cse491::ItemBase>(10, "Daedric Armor");
daedricArmor->SetProperties("Health", 99, "Extra Inv. Space", 5);
daedricArmor->SetPosition(5, 0);
world.AddEntity(daedricArmor);
world.AddItem(std::move(daedricArmor));

world.Run();
}
2 changes: 1 addition & 1 deletion source/group_6_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Agents/PacingAgent.hpp"
#include "Interfaces/TrashInterface.hpp"
#include "Worlds/GenerativeWorld.hpp"
#include "Worlds/BiomeGenerator.h"
#include "Worlds/BiomeGenerator.hpp"

int main() {
static const unsigned int SEED = 973;
Expand Down
Loading

0 comments on commit fcc65a1

Please sign in to comment.